Advertisement
Guest User

Untitled

a guest
Jun 25th, 2017
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.80 KB | None | 0 0
  1. <?php
  2.  
  3. class SecureImage
  4. {
  5.     /**
  6.     *   @author       - RyanTheGreat                                              
  7.     *   @information  - Secure Image class designed to secure images for storage on server
  8.     *   @lastmodified - 10/5/2010                                                
  9.     *   @majorchanges - None                                                      
  10.     *   @filelocation - webroot/includes/classes                            
  11.     */
  12.        
  13.         protected $_imgOriginalImage;
  14.         protected $_imgSecureImage;
  15.         protected $_strOriginalName;
  16.         protected $_boolStrict;
  17.         protected $_approvedExtension;
  18.         protected $_intMaxWidth;
  19.         protected $_intMaxHeight;
  20.         protected $_allowedExtensions = array('.jpg', '.jpeg', '.gif', '.png');
  21.        
  22.         public function __construct($imgOriginalImage, $strImageName, $boolStrict, $intMaxWidth = 0, $intMaxHeight = 0)
  23.         {
  24.             $this -> _imgOriginalImage   = $imgOriginalImage; //The actual image in its original form.
  25.             $this -> _strOriginalName    = $strImageName; //The images original name, as supplied by the user
  26.             $this -> _boolStrict         = $boolStrict;
  27.             $this -> _intMaxWidth        = $intMaxWidth;
  28.             $this -> _intMaxHeight       = $intMaxHeight;
  29.         }
  30.        
  31.         //public function to give access to the SecureImage routines
  32.         public function getSecureImage()
  33.         {
  34.             $passedCheck = $this -> verifyImageType($this -> _imgOriginalImage, $this -> _strOriginalName);
  35.             if($passedCheck === true)
  36.             {
  37.                 //passed the original checks, now for the extended protection against maliciously coded images.
  38.                 $successfullyCreated = $this -> createSecureImage();
  39.                 if($successfullyCreated)
  40.                 {
  41.                     return array($this -> _imgSecureImages, $this -> _approvedExtension, $this -> strOrignalName);             
  42.                 }
  43.                 else
  44.                 {
  45.                     return 1;              
  46.                 }
  47.             }
  48.             return $passedCheck;               
  49.         }
  50.        
  51.         //This function creates a new image based off the data supplied in the form it is expected to be in, by rearranging the bytes.
  52.         //This adds protection for any attacks the work based off parsers that grab their headers from the top and ignore junk at the bottom.
  53.         //i.e. Maliciously coded images, GIFARs, etc.
  54.         protected function createSecureImage()
  55.         {
  56.             //different functionality based off image type
  57.             switch($this -> _approvedExtension)
  58.             {
  59.                 case '.jpg':
  60.                     $this -> _imgSecureImage = imagecreatefromjpeg($this -> _imgOriginalImage);
  61.                     break;
  62.                 case '.jpeg':
  63.                     $this -> _imgSecureImage = imagecreatefromjpeg($this -> _imgOriginalImage);
  64.                     break;
  65.                 case '.gif':
  66.                     $this -> _imgSecureImage = imagecreatefromgif($this -> _imgOriginalImage);
  67.                     break;
  68.                 case '.png':
  69.                     $this -> _imgSecureImage = imagecreatefrompng($this -> _imgOriginalImage);
  70.                     break;
  71.                 default:
  72.                     return false;
  73.                     break;
  74.             }
  75.             if($this -> _imgSecureImage === false)
  76.             {
  77.                 return false;          
  78.             }
  79.             return true;                    
  80.         }
  81.        
  82.         //this function actually writes the Image resource to a file location, or can be used to output to the buffer by leaving the file location null
  83.         public function writeImageToFile($saveLocation = NULL)
  84.         {
  85.             //if writing to buffer, don't forget headers first :)
  86.             if($this -> _approvedExtension == '.jpg' || $this -> _allowedExtension == '.jpeg')
  87.             {
  88.                 imagejpeg($this -> _imgSecureImage, $saveLocation);    
  89.             }
  90.             else if($this -> _approvedExtension == '.gif')
  91.             {
  92.                 imagegif($this -> _imgSecureImage, $saveLocation);             
  93.             }
  94.             else if($this -> _approvedExtension == '.png')
  95.             {
  96.                 imagepng($this -> _imgSecureImage, $saveLocation); 
  97.             }
  98.             else
  99.             {
  100.                 //probably mistakenly called this before the getSecureImage function, return false to indicate epicfail
  101.                 return false;          
  102.             }
  103.             imagedestroy($this -> _imgSecureImage);
  104.             return true;                                                            
  105.         }
  106.        
  107.         //this function verifys the image type based off MIME type and extension supplied by user
  108.        
  109.         protected function verifyImageType($imgVerifyImage, $strVerifyName)
  110.         {
  111.             $imgData = getimagesize($imgVerifyImage);
  112.             //if there is maximum dimensions set, check them
  113.             if($this -> _intMaxWidth != 0)
  114.             {
  115.                 if($imgData[0] > $this -> _intMaxWidth)
  116.                 {
  117.                     return 2;              
  118.                 }      
  119.             }
  120.             else if($this -> _intMaxHeight != 0)
  121.             {
  122.                 if($imgData[1] > $this -> _intMaxHeight)
  123.                 {
  124.                     return 3;              
  125.                 }              
  126.             }
  127.             //verify the extension is among the approved
  128.             $extension = strtolower(trim(strrchr($strVerifyName, '.')));
  129.             if(!in_array($extension, $this -> _allowedExtensions))
  130.             {
  131.                 return 4;          
  132.             }
  133.             $this -> _approvedExtension = $extension;
  134.            
  135.             //Next, verify the image data supplied, keep in mind: alone this is easily spoofed, so this is used as defense in depth.
  136.             if($imgData === false || empty($imgData))
  137.             {
  138.                 //failed a basic test for image headers, definitely a no-go.
  139.                 return 5;          
  140.             }
  141.            
  142.             if($this -> _boolStrict)
  143.             {
  144.                 //This isn't necessarily all that strict of a checking procedure, but may not need this functionality everywhere, so it is optional. I suggest using it.
  145.                 //Note: If you allow more image types than the default, modify this section accordingly.
  146.                 if(($extension == '.jpeg' && $imgData[2] != IMG_JPEG) || ($extension == '.jpg' && $imgData[2] != IMG_JPG) || ($extension == '.gif' && $imgData[2] != IMG_GIF) || ($extension == '.png' && $imgData[2] != IMG_PNG))
  147.                 {
  148.                     return IMG_PNG;            
  149.                 }
  150.             }
  151.             return true;                                           
  152.         }
  153. }
  154. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement