| 1 | 2 | Manuela | <?php
 | 
      
        | 2 |  |  | /**
 | 
      
        | 3 |  |  |  * PhpThumb Base Class Definition File
 | 
      
        | 4 |  |  |  *
 | 
      
        | 5 |  |  |  * This file contains the definition for the ThumbBase object
 | 
      
        | 6 |  |  |  *
 | 
      
        | 7 |  |  |  * PHP Version 5 with GD 2.0+
 | 
      
        | 8 |  |  |  * PhpThumb : PHP Thumb Library <http://phpthumb.gxdlabs.com>
 | 
      
        | 9 |  |  |  * Copyright (c) 2009, Ian Selby/Gen X Design
 | 
      
        | 10 |  |  |  *
 | 
      
        | 11 |  |  |  * Author(s): Ian Selby <ian@gen-x-design.com>
 | 
      
        | 12 |  |  |  *
 | 
      
        | 13 |  |  |  * Licensed under the MIT License
 | 
      
        | 14 |  |  |  * Redistributions of files must retain the above copyright notice.
 | 
      
        | 15 |  |  |  *
 | 
      
        | 16 |  |  |  * @author Ian Selby <ian@gen-x-design.com>
 | 
      
        | 17 |  |  |  * @copyright Copyright (c) 2009 Gen X Design
 | 
      
        | 18 |  |  |  * @link http://phpthumb.gxdlabs.com
 | 
      
        | 19 |  |  |  * @license http://www.opensource.org/licenses/mit-license.php The MIT License
 | 
      
        | 20 |  |  |  * @version 3.0
 | 
      
        | 21 |  |  |  * @package PhpThumb
 | 
      
        | 22 |  |  |  * @filesource
 | 
      
        | 23 |  |  |  */
 | 
      
        | 24 |  |  | 
 | 
      
        | 25 |  |  | /**
 | 
      
        | 26 |  |  |  * ThumbBase Class Definition
 | 
      
        | 27 |  |  |  *
 | 
      
        | 28 |  |  |  * This is the base class that all implementations must extend.  It contains the
 | 
      
        | 29 |  |  |  * core variables and functionality common to all implementations, as well as the functions that
 | 
      
        | 30 |  |  |  * allow plugins to augment those classes.
 | 
      
        | 31 |  |  |  *
 | 
      
        | 32 |  |  |  * @package PhpThumb
 | 
      
        | 33 |  |  |  * @subpackage Core
 | 
      
        | 34 |  |  |  */
 | 
      
        | 35 |  |  | abstract class ThumbBase
 | 
      
        | 36 |  |  | {
 | 
      
        | 37 |  |  |     /**
 | 
      
        | 38 |  |  |      * All imported objects
 | 
      
        | 39 |  |  |      *
 | 
      
        | 40 |  |  |      * An array of imported plugin objects
 | 
      
        | 41 |  |  |      *
 | 
      
        | 42 |  |  |      * @var array
 | 
      
        | 43 |  |  |      */
 | 
      
        | 44 |  |  |     protected $imported;
 | 
      
        | 45 |  |  |     /**
 | 
      
        | 46 |  |  |      * All imported object functions
 | 
      
        | 47 |  |  |      *
 | 
      
        | 48 |  |  |      * An array of all methods added to this class by imported plugin objects
 | 
      
        | 49 |  |  |      *
 | 
      
        | 50 |  |  |      * @var array
 | 
      
        | 51 |  |  |      */
 | 
      
        | 52 |  |  |     protected $importedFunctions;
 | 
      
        | 53 |  |  |     /**
 | 
      
        | 54 |  |  |      * The last error message raised
 | 
      
        | 55 |  |  |      *
 | 
      
        | 56 |  |  |      * @var string
 | 
      
        | 57 |  |  |      */
 | 
      
        | 58 |  |  |     protected $errorMessage;
 | 
      
        | 59 |  |  |     /**
 | 
      
        | 60 |  |  |      * Whether or not the current instance has any errors
 | 
      
        | 61 |  |  |      *
 | 
      
        | 62 |  |  |      * @var bool
 | 
      
        | 63 |  |  |      */
 | 
      
        | 64 |  |  |     protected $hasError;
 | 
      
        | 65 |  |  |     /**
 | 
      
        | 66 |  |  |      * The name of the file we're manipulating
 | 
      
        | 67 |  |  |      *
 | 
      
        | 68 |  |  |      * This must include the path to the file (absolute paths recommended)
 | 
      
        | 69 |  |  |      *
 | 
      
        | 70 |  |  |      * @var string
 | 
      
        | 71 |  |  |      */
 | 
      
        | 72 |  |  |     protected $fileName;
 | 
      
        | 73 |  |  |     /**
 | 
      
        | 74 |  |  |      * What the file format is (mime-type)
 | 
      
        | 75 |  |  |      *
 | 
      
        | 76 |  |  |      * @var string
 | 
      
        | 77 |  |  |      */
 | 
      
        | 78 |  |  |     protected $format;
 | 
      
        | 79 |  |  |     /**
 | 
      
        | 80 |  |  |      * Whether or not the image is hosted remotely
 | 
      
        | 81 |  |  |      *
 | 
      
        | 82 |  |  |      * @var bool
 | 
      
        | 83 |  |  |      */
 | 
      
        | 84 |  |  |     protected $remoteImage;
 | 
      
        | 85 |  |  |     /**
 | 
      
        | 86 |  |  |      * Whether or not the current image is an actual file, or the raw file data
 | 
      
        | 87 |  |  |      *
 | 
      
        | 88 |  |  |      * By "raw file data" it's meant that we're actually passing the result of something
 | 
      
        | 89 |  |  |      * like file_get_contents() or perhaps from a database blob
 | 
      
        | 90 |  |  |      *
 | 
      
        | 91 |  |  |      * @var bool
 | 
      
        | 92 |  |  |      */
 | 
      
        | 93 |  |  |     protected $isDataStream;
 | 
      
        | 94 |  |  | 
 | 
      
        | 95 |  |  |     /**
 | 
      
        | 96 |  |  |      * Class constructor
 | 
      
        | 97 |  |  |      *
 | 
      
        | 98 |  |  |      * @return ThumbBase
 | 
      
        | 99 |  |  |      */
 | 
      
        | 100 |  |  |     public function __construct ($fileName, $isDataStream = false)
 | 
      
        | 101 |  |  |     {
 | 
      
        | 102 |  |  |         $this->imported                = array();
 | 
      
        | 103 |  |  |         $this->importedFunctions    = array();
 | 
      
        | 104 |  |  |         $this->errorMessage            = null;
 | 
      
        | 105 |  |  |         $this->hasError                = false;
 | 
      
        | 106 |  |  |         $this->fileName                = $fileName;
 | 
      
        | 107 |  |  |         $this->remoteImage            = false;
 | 
      
        | 108 |  |  |         $this->isDataStream            = $isDataStream;
 | 
      
        | 109 |  |  | 
 | 
      
        | 110 |  |  |         $this->fileExistsAndReadable();
 | 
      
        | 111 |  |  |     }
 | 
      
        | 112 |  |  | 
 | 
      
        | 113 |  |  |     /**
 | 
      
        | 114 |  |  |      * Imports plugins in $registry to the class
 | 
      
        | 115 |  |  |      *
 | 
      
        | 116 |  |  |      * @param array $registry
 | 
      
        | 117 |  |  |      */
 | 
      
        | 118 |  |  |     public function importPlugins ($registry)
 | 
      
        | 119 |  |  |     {
 | 
      
        | 120 |  |  |         foreach ($registry as $plugin => $meta)
 | 
      
        | 121 |  |  |         {
 | 
      
        | 122 |  |  |             $this->imports($plugin);
 | 
      
        | 123 |  |  |         }
 | 
      
        | 124 |  |  |     }
 | 
      
        | 125 |  |  | 
 | 
      
        | 126 |  |  |     /**
 | 
      
        | 127 |  |  |      * Imports a plugin
 | 
      
        | 128 |  |  |      *
 | 
      
        | 129 |  |  |      * This is where all the plugins magic happens!  This function "loads" the plugin functions, making them available as
 | 
      
        | 130 |  |  |      * methods on the class.
 | 
      
        | 131 |  |  |      *
 | 
      
        | 132 |  |  |      * @param string $object The name of the object to import / "load"
 | 
      
        | 133 |  |  |      */
 | 
      
        | 134 |  |  |     protected function imports ($object)
 | 
      
        | 135 |  |  |     {
 | 
      
        | 136 |  |  |         // the new object to import
 | 
      
        | 137 |  |  |         $newImport             = new $object();
 | 
      
        | 138 |  |  |         // the name of the new object (class name)
 | 
      
        | 139 |  |  |         $importName            = get_class($newImport);
 | 
      
        | 140 |  |  |         // the new functions to import
 | 
      
        | 141 |  |  |         $importFunctions     = get_class_methods($newImport);
 | 
      
        | 142 |  |  | 
 | 
      
        | 143 |  |  |         // add the object to the registry
 | 
      
        | 144 |  |  |         array_push($this->imported, array($importName, $newImport));
 | 
      
        | 145 |  |  | 
 | 
      
        | 146 |  |  |         // add the methods to the registry
 | 
      
        | 147 |  |  |         foreach ($importFunctions as $key => $functionName)
 | 
      
        | 148 |  |  |         {
 | 
      
        | 149 |  |  |             $this->importedFunctions[$functionName] = &$newImport;
 | 
      
        | 150 |  |  |         }
 | 
      
        | 151 |  |  |     }
 | 
      
        | 152 |  |  | 
 | 
      
        | 153 |  |  |     /**
 | 
      
        | 154 |  |  |      * Checks to see if $this->fileName exists and is readable
 | 
      
        | 155 |  |  |      *
 | 
      
        | 156 |  |  |      */
 | 
      
        | 157 |  |  |     protected function fileExistsAndReadable ()
 | 
      
        | 158 |  |  |     {
 | 
      
        | 159 |  |  |         if ($this->isDataStream === true)
 | 
      
        | 160 |  |  |         {
 | 
      
        | 161 |  |  |             return;
 | 
      
        | 162 |  |  |         }
 | 
      
        | 163 |  |  | 
 | 
      
        | 164 |  |  |         if (stristr($this->fileName, 'http://') !== false)
 | 
      
        | 165 |  |  |         {
 | 
      
        | 166 |  |  |             $this->remoteImage = true;
 | 
      
        | 167 |  |  |             return;
 | 
      
        | 168 |  |  |         }
 | 
      
        | 169 |  |  | 
 | 
      
        | 170 |  |  |         if (!file_exists($this->fileName))
 | 
      
        | 171 |  |  |         {
 | 
      
        | 172 |  |  |             $this->triggerError('Image file not found: ' . $this->fileName);
 | 
      
        | 173 |  |  |         }
 | 
      
        | 174 |  |  |         elseif (!is_readable($this->fileName))
 | 
      
        | 175 |  |  |         {
 | 
      
        | 176 |  |  |             $this->triggerError('Image file not readable: ' . $this->fileName);
 | 
      
        | 177 |  |  |         }
 | 
      
        | 178 |  |  |     }
 | 
      
        | 179 |  |  | 
 | 
      
        | 180 |  |  |     /**
 | 
      
        | 181 |  |  |      * Sets $this->errorMessage to $errorMessage and throws an exception
 | 
      
        | 182 |  |  |      *
 | 
      
        | 183 |  |  |      * Also sets $this->hasError to true, so even if the exceptions are caught, we don't
 | 
      
        | 184 |  |  |      * attempt to proceed with any other functions
 | 
      
        | 185 |  |  |      *
 | 
      
        | 186 |  |  |      * @param string $errorMessage
 | 
      
        | 187 |  |  |      */
 | 
      
        | 188 |  |  |     protected function triggerError ($errorMessage)
 | 
      
        | 189 |  |  |     {
 | 
      
        | 190 |  |  |         $this->hasError     = true;
 | 
      
        | 191 |  |  |         $this->errorMessage    = $errorMessage;
 | 
      
        | 192 |  |  | 
 | 
      
        | 193 |  |  |         throw new Exception ($errorMessage);
 | 
      
        | 194 |  |  |     }
 | 
      
        | 195 |  |  | 
 | 
      
        | 196 |  |  |     /**
 | 
      
        | 197 |  |  |      * Calls plugin / imported functions
 | 
      
        | 198 |  |  |      *
 | 
      
        | 199 |  |  |      * This is also where a fair amount of plugins magaic happens.  This magic method is called whenever an "undefined" class
 | 
      
        | 200 |  |  |      * method is called in code, and we use that to call an imported function.
 | 
      
        | 201 |  |  |      *
 | 
      
        | 202 |  |  |      * You should NEVER EVER EVER invoke this function manually.  The universe will implode if you do... seriously ;)
 | 
      
        | 203 |  |  |      *
 | 
      
        | 204 |  |  |      * @param string $method
 | 
      
        | 205 |  |  |      * @param array $args
 | 
      
        | 206 |  |  |      */
 | 
      
        | 207 |  |  |     public function __call ($method, $args)
 | 
      
        | 208 |  |  |     {
 | 
      
        | 209 |  |  |         if( array_key_exists($method, $this->importedFunctions))
 | 
      
        | 210 |  |  |         {
 | 
      
        | 211 |  |  |             $args[] = $this;
 | 
      
        | 212 |  |  |             return call_user_func_array(array($this->importedFunctions[$method], $method), $args);
 | 
      
        | 213 |  |  |         }
 | 
      
        | 214 |  |  | 
 | 
      
        | 215 |  |  |         throw new BadMethodCallException ('Call to undefined method/class function: ' . $method);
 | 
      
        | 216 |  |  |     }
 | 
      
        | 217 |  |  | 
 | 
      
        | 218 |  |  |     /**
 | 
      
        | 219 |  |  |      * Returns $imported.
 | 
      
        | 220 |  |  |      * @see ThumbBase::$imported
 | 
      
        | 221 |  |  |      * @return array
 | 
      
        | 222 |  |  |      */
 | 
      
        | 223 |  |  |     public function getImported ()
 | 
      
        | 224 |  |  |     {
 | 
      
        | 225 |  |  |         return $this->imported;
 | 
      
        | 226 |  |  |     }
 | 
      
        | 227 |  |  | 
 | 
      
        | 228 |  |  |     /**
 | 
      
        | 229 |  |  |      * Returns $importedFunctions.
 | 
      
        | 230 |  |  |      * @see ThumbBase::$importedFunctions
 | 
      
        | 231 |  |  |      * @return array
 | 
      
        | 232 |  |  |      */
 | 
      
        | 233 |  |  |     public function getImportedFunctions ()
 | 
      
        | 234 |  |  |     {
 | 
      
        | 235 |  |  |         return $this->importedFunctions;
 | 
      
        | 236 |  |  |     }
 | 
      
        | 237 |  |  | 
 | 
      
        | 238 |  |  |     /**
 | 
      
        | 239 |  |  |      * Returns $errorMessage.
 | 
      
        | 240 |  |  |      *
 | 
      
        | 241 |  |  |      * @see ThumbBase::$errorMessage
 | 
      
        | 242 |  |  |      */
 | 
      
        | 243 |  |  |     public function getErrorMessage ()
 | 
      
        | 244 |  |  |     {
 | 
      
        | 245 |  |  |         return $this->errorMessage;
 | 
      
        | 246 |  |  |     }
 | 
      
        | 247 |  |  | 
 | 
      
        | 248 |  |  |     /**
 | 
      
        | 249 |  |  |      * Sets $errorMessage.
 | 
      
        | 250 |  |  |      *
 | 
      
        | 251 |  |  |      * @param object $errorMessage
 | 
      
        | 252 |  |  |      * @see ThumbBase::$errorMessage
 | 
      
        | 253 |  |  |      */
 | 
      
        | 254 |  |  |     public function setErrorMessage ($errorMessage)
 | 
      
        | 255 |  |  |     {
 | 
      
        | 256 |  |  |         $this->errorMessage = $errorMessage;
 | 
      
        | 257 |  |  |     }
 | 
      
        | 258 |  |  | 
 | 
      
        | 259 |  |  |     /**
 | 
      
        | 260 |  |  |      * Returns $fileName.
 | 
      
        | 261 |  |  |      *
 | 
      
        | 262 |  |  |      * @see ThumbBase::$fileName
 | 
      
        | 263 |  |  |      */
 | 
      
        | 264 |  |  |     public function getFileName ()
 | 
      
        | 265 |  |  |     {
 | 
      
        | 266 |  |  |         return $this->fileName;
 | 
      
        | 267 |  |  |     }
 | 
      
        | 268 |  |  | 
 | 
      
        | 269 |  |  |     /**
 | 
      
        | 270 |  |  |      * Sets $fileName.
 | 
      
        | 271 |  |  |      *
 | 
      
        | 272 |  |  |      * @param object $fileName
 | 
      
        | 273 |  |  |      * @see ThumbBase::$fileName
 | 
      
        | 274 |  |  |      */
 | 
      
        | 275 |  |  |     public function setFileName ($fileName)
 | 
      
        | 276 |  |  |     {
 | 
      
        | 277 |  |  |         $this->fileName = $fileName;
 | 
      
        | 278 |  |  |     }
 | 
      
        | 279 |  |  | 
 | 
      
        | 280 |  |  |     /**
 | 
      
        | 281 |  |  |      * Returns $format.
 | 
      
        | 282 |  |  |      *
 | 
      
        | 283 |  |  |      * @see ThumbBase::$format
 | 
      
        | 284 |  |  |      */
 | 
      
        | 285 |  |  |     public function getFormat ()
 | 
      
        | 286 |  |  |     {
 | 
      
        | 287 |  |  |         return $this->format;
 | 
      
        | 288 |  |  |     }
 | 
      
        | 289 |  |  | 
 | 
      
        | 290 |  |  |     /**
 | 
      
        | 291 |  |  |      * Sets $format.
 | 
      
        | 292 |  |  |      *
 | 
      
        | 293 |  |  |      * @param object $format
 | 
      
        | 294 |  |  |      * @see ThumbBase::$format
 | 
      
        | 295 |  |  |      */
 | 
      
        | 296 |  |  |     public function setFormat ($format)
 | 
      
        | 297 |  |  |     {
 | 
      
        | 298 |  |  |         $this->format = $format;
 | 
      
        | 299 |  |  |     }
 | 
      
        | 300 |  |  | 
 | 
      
        | 301 |  |  |     /**
 | 
      
        | 302 |  |  |      * Returns $hasError.
 | 
      
        | 303 |  |  |      *
 | 
      
        | 304 |  |  |      * @see ThumbBase::$hasError
 | 
      
        | 305 |  |  |      */
 | 
      
        | 306 |  |  |     public function getHasError ()
 | 
      
        | 307 |  |  |     {
 | 
      
        | 308 |  |  |         return $this->hasError;
 | 
      
        | 309 |  |  |     }
 | 
      
        | 310 |  |  | 
 | 
      
        | 311 |  |  |     /**
 | 
      
        | 312 |  |  |      * Sets $hasError.
 | 
      
        | 313 |  |  |      *
 | 
      
        | 314 |  |  |      * @param object $hasError
 | 
      
        | 315 |  |  |      * @see ThumbBase::$hasError
 | 
      
        | 316 |  |  |      */
 | 
      
        | 317 |  |  |     public function setHasError ($hasError)
 | 
      
        | 318 |  |  |     {
 | 
      
        | 319 |  |  |         $this->hasError = $hasError;
 | 
      
        | 320 |  |  |     }
 | 
      
        | 321 |  |  | 
 | 
      
        | 322 |  |  | 
 | 
      
        | 323 |  |  | }
 |