[1] | 1 | <?php |
---|
| 2 | /** |
---|
[42] | 3 | * ImageThumb.inc.php |
---|
[136] | 4 | * code by strangecode :: www.strangecode.com :: this document contains copyrighted information |
---|
[1] | 5 | * |
---|
| 6 | * @author Quinn Comendant <quinn@strangecode.com> |
---|
[119] | 7 | * @requires Netpbm <http://sourceforge.net/projects/netpbm/> and libjpeg or GD. |
---|
| 8 | * @version 2.0 |
---|
[1] | 9 | */ |
---|
| 10 | |
---|
[152] | 11 | // Image proprtion options. |
---|
[1] | 12 | define('IMAGETHUMB_FIT_WIDTH', 1); |
---|
| 13 | define('IMAGETHUMB_FIT_HEIGHT', 2); |
---|
| 14 | define('IMAGETHUMB_FIT_LARGER', 3); |
---|
| 15 | define('IMAGETHUMB_STRETCH', 4); |
---|
| 16 | define('IMAGETHUMB_NO_SCALE', 5); |
---|
[152] | 17 | |
---|
| 18 | // Image resize options. |
---|
[119] | 19 | define('IMAGETHUMB_METHOD_NETPBM', 6); |
---|
| 20 | define('IMAGETHUMB_METHOD_GD', 7); |
---|
[1] | 21 | |
---|
| 22 | class ImageThumb { |
---|
[119] | 23 | |
---|
| 24 | // General object parameters. |
---|
| 25 | var $_params = array( |
---|
| 26 | // The location for images to create thumbnails from. |
---|
| 27 | 'source_dir' => null, |
---|
[42] | 28 | |
---|
[119] | 29 | // Existing files will be overwritten when there is a name conflict? |
---|
| 30 | 'allow_overwriting' => false, |
---|
[42] | 31 | |
---|
[119] | 32 | // The file permissions of the uploaded files. Remember, files will be owned by the web server user. |
---|
| 33 | 'dest_file_perms' => 0600, |
---|
[42] | 34 | |
---|
[119] | 35 | // Permissions of autocreated directories. Must be at least 0700 with owner=apache. |
---|
[152] | 36 | 'dest_dir_perms' => 0700, |
---|
[42] | 37 | |
---|
[199] | 38 | // Require file to have one of the following file name extensions. |
---|
[119] | 39 | 'valid_file_extensions' => array('jpg', 'jpeg', 'gif', 'png'), |
---|
| 40 | |
---|
| 41 | // Method to use for resizing. (IMAGETHUMB_METHOD_NETPBM or IMAGETHUMB_METHOD_GD) |
---|
| 42 | 'resize_method' => IMAGETHUMB_METHOD_NETPBM, |
---|
[1] | 43 | |
---|
[119] | 44 | // Netpbm and libjpeg binary locations. |
---|
| 45 | 'anytopnm_binary' => '/usr/bin/anytopnm', |
---|
| 46 | 'pnmscale_binary' => '/usr/bin/pnmscale', |
---|
| 47 | 'cjpeg_binary' => '/usr/bin/cjpeg', |
---|
[42] | 48 | |
---|
[119] | 49 | // Which messages do we pass to raiseMsg? Use one of the MSG_* constants or false to disable. |
---|
| 50 | 'display_messages' => MSG_ALL, |
---|
| 51 | ); |
---|
| 52 | |
---|
| 53 | // Default image size specs. |
---|
[152] | 54 | var $_default_image_specs = array( |
---|
| 55 | // The destination for an image thumbnail size. |
---|
| 56 | // Use initial / to specify absolute paths, leave off to specify a path relative to source_dir (eg: ../thumbs). |
---|
[119] | 57 | 'dest_dir' => null, |
---|
[120] | 58 | |
---|
| 59 | // Destination file types. (IMG_JPG, IMG_PNG, IMG_GIF, IMG_WBMP) |
---|
| 60 | 'dest_file_type' => IMG_JPG, |
---|
| 61 | |
---|
| 62 | // Destination file types. ('jpg', 'png', 'gif', 'wbmp') |
---|
[199] | 63 | 'dest_file_extension' => 'jpg', |
---|
[120] | 64 | |
---|
[119] | 65 | // Type of scaling to perform, and sizes used to calculate max dimentions. |
---|
| 66 | 'scaling_type' => IMAGETHUMB_FIT_LARGER, |
---|
| 67 | 'width' => null, |
---|
| 68 | 'height' => null, |
---|
[120] | 69 | |
---|
[119] | 70 | // Percentage quality of image compression output 0-100. |
---|
| 71 | 'quality' => 65, |
---|
[120] | 72 | |
---|
[119] | 73 | // Create progressive jpegs? |
---|
| 74 | 'progressive' => false, |
---|
[120] | 75 | |
---|
| 76 | // If using GD method, apply sharpen filter. Requires PHP > 5.1. |
---|
| 77 | 'sharpen' => true, |
---|
| 78 | |
---|
| 79 | // Integers between 1-100, useful values are 65-85. |
---|
| 80 | 'sharpen_value' => 75, |
---|
| 81 | |
---|
[119] | 82 | // If source image is smaller than thumbnail, allow upscaling? |
---|
| 83 | 'allow_upscaling' => false, |
---|
[120] | 84 | |
---|
[119] | 85 | // If thumb exists and filesize is smaller than this, do not overwrite the thumb. |
---|
| 86 | 'keep_filesize' => null, |
---|
| 87 | ); |
---|
[1] | 88 | |
---|
[119] | 89 | // Final specifications for image sizes, set with setSpec(). |
---|
[152] | 90 | var $_image_specs = array(); |
---|
[119] | 91 | |
---|
[1] | 92 | /** |
---|
[119] | 93 | * Set (or overwrite existing) parameters by passing an array of new parameters. |
---|
[1] | 94 | * |
---|
[119] | 95 | * @access public |
---|
| 96 | * @param array $params Array of parameters (key => val pairs). |
---|
[1] | 97 | */ |
---|
[119] | 98 | function setParam($params) |
---|
[1] | 99 | { |
---|
[136] | 100 | $app =& App::getInstance(); |
---|
| 101 | |
---|
[119] | 102 | if (isset($params) && is_array($params)) { |
---|
| 103 | |
---|
[152] | 104 | // Enforce valid source_dir parameter. |
---|
[119] | 105 | if (isset($params['source_dir'])) { |
---|
| 106 | $params['source_dir'] = realpath($params['source_dir']); |
---|
[152] | 107 | // Source must be directory. |
---|
[119] | 108 | if (!is_dir($params['source_dir'])) { |
---|
[152] | 109 | $app->logMsg(sprintf('Attempting to auto-create source directory: %s', $params['source_dir']), LOG_NOTICE, __FILE__, __LINE__); |
---|
| 110 | if (phpversion() > '5') { |
---|
| 111 | // Recursive. |
---|
| 112 | mkdir($params['source_dir'], isset($params['dest_dir_perms']) ? $params['dest_dir_perms'] : $this->getParam('dest_dir_perms'), true); |
---|
| 113 | } else { |
---|
| 114 | mkdir($params['source_dir'], isset($params['dest_dir_perms']) ? $params['dest_dir_perms'] : $this->getParam('dest_dir_perms')); |
---|
| 115 | } |
---|
| 116 | if (!is_dir($params['source_dir'])) { |
---|
| 117 | $app->logMsg(sprintf('Source directory invalid: %s', $params['source_dir']), LOG_ERR, __FILE__, __LINE__); |
---|
| 118 | trigger_error(sprintf('Source directory invalid: %s', $params['source_dir']), E_USER_ERROR); |
---|
| 119 | } |
---|
[119] | 120 | } |
---|
[152] | 121 | // Source must be readable. |
---|
[119] | 122 | if (!is_readable($params['source_dir'])) { |
---|
[136] | 123 | $app->logMsg(sprintf('Source directory not readable: %s', $params['source_dir']), LOG_ERR, __FILE__, __LINE__); |
---|
[119] | 124 | trigger_error(sprintf('Source directory not readable: %s', $params['source_dir']), E_USER_ERROR); |
---|
| 125 | } |
---|
| 126 | } |
---|
| 127 | |
---|
| 128 | // Merge new parameters with old overriding only those passed. |
---|
| 129 | $this->_params = array_merge($this->_params, $params); |
---|
| 130 | } else { |
---|
[136] | 131 | $app->logMsg(sprintf('Parameters are not an array: %s', $params), LOG_ERR, __FILE__, __LINE__); |
---|
[1] | 132 | } |
---|
| 133 | } |
---|
| 134 | |
---|
| 135 | /** |
---|
[119] | 136 | * Return the value of a parameter, if it exists. |
---|
[1] | 137 | * |
---|
[119] | 138 | * @access public |
---|
| 139 | * @param string $param Which parameter to return. |
---|
| 140 | * @return mixed Configured parameter value. |
---|
[1] | 141 | */ |
---|
[119] | 142 | function getParam($param) |
---|
[1] | 143 | { |
---|
[136] | 144 | $app =& App::getInstance(); |
---|
| 145 | |
---|
[119] | 146 | if (isset($this->_params[$param])) { |
---|
| 147 | return $this->_params[$param]; |
---|
| 148 | } else { |
---|
[146] | 149 | $app->logMsg(sprintf('Parameter is not set: %s', $param), LOG_DEBUG, __FILE__, __LINE__); |
---|
[119] | 150 | return null; |
---|
[1] | 151 | } |
---|
| 152 | } |
---|
| 153 | |
---|
| 154 | /** |
---|
| 155 | * Set the specification of thumbnails. |
---|
| 156 | * |
---|
| 157 | * @access public |
---|
[235] | 158 | * @param array $spec The specifications for a size of output image. |
---|
| 159 | * @param int $index The position of the specification in the spec array |
---|
| 160 | * Use to overwrite existing spev array values. |
---|
[1] | 161 | */ |
---|
[120] | 162 | function setSpec($spec, $index=null) |
---|
[1] | 163 | { |
---|
[136] | 164 | $app =& App::getInstance(); |
---|
| 165 | |
---|
[119] | 166 | // A little sanity checking. |
---|
[152] | 167 | if (!isset($spec['dest_dir']) || '' == trim($spec['dest_dir'])) { |
---|
[136] | 168 | $app->logMsg('setSpec error: dest_dir not specified.', LOG_ERR, __FILE__, __LINE__); |
---|
[152] | 169 | } else { |
---|
| 170 | $spec['dest_dir'] = trim($spec['dest_dir']); |
---|
[1] | 171 | } |
---|
[120] | 172 | if (isset($spec['dest_file_type'])) { |
---|
| 173 | switch ($spec['dest_file_type']) { |
---|
| 174 | case IMG_JPG : |
---|
| 175 | if (imagetypes() & IMG_JPG == 0) { |
---|
[136] | 176 | $app->logMsg(sprintf('IMG_JPG is not supported by this version of PHP GD.', null), LOG_ERR, __FILE__, __LINE__); |
---|
[120] | 177 | } |
---|
[199] | 178 | $spec['dest_file_extension'] = 'jpg'; |
---|
[120] | 179 | break; |
---|
| 180 | case IMG_PNG : |
---|
| 181 | if (imagetypes() & IMG_PNG == 0) { |
---|
[136] | 182 | $app->logMsg(sprintf('IMG_PNG is not supported by this version of PHP GD.', null), LOG_ERR, __FILE__, __LINE__); |
---|
[120] | 183 | } |
---|
[199] | 184 | $spec['dest_file_extension'] = 'png'; |
---|
[120] | 185 | break; |
---|
| 186 | case IMG_GIF : |
---|
| 187 | if (imagetypes() & IMG_GIF == 0) { |
---|
[136] | 188 | $app->logMsg(sprintf('IMG_GIF is not supported by this version of PHP GD.', null), LOG_ERR, __FILE__, __LINE__); |
---|
[120] | 189 | } |
---|
[199] | 190 | $spec['dest_file_extension'] = 'gif'; |
---|
[120] | 191 | break; |
---|
| 192 | case IMG_WBMP : |
---|
| 193 | if (imagetypes() & IMG_WBMP == 0) { |
---|
[136] | 194 | $app->logMsg(sprintf('IMG_WBMP is not supported by this version of PHP GD.', null), LOG_ERR, __FILE__, __LINE__); |
---|
[120] | 195 | } |
---|
[199] | 196 | $spec['dest_file_extension'] = 'wbmp'; |
---|
[120] | 197 | break; |
---|
| 198 | default : |
---|
[136] | 199 | $app->logMsg(sprintf('Invalid dest_file_type: %s', $spec['dest_file_type']), LOG_ERR, __FILE__, __LINE__); |
---|
[120] | 200 | break; |
---|
| 201 | } |
---|
| 202 | } |
---|
[119] | 203 | if (!isset($spec['width']) || !is_int($spec['width'])) { |
---|
[136] | 204 | $app->logMsg('setSpec error: width not specified.', LOG_ERR, __FILE__, __LINE__); |
---|
[119] | 205 | } |
---|
| 206 | if (!isset($spec['height']) || !is_int($spec['height'])) { |
---|
[136] | 207 | $app->logMsg('setSpec error: height not specified.', LOG_ERR, __FILE__, __LINE__); |
---|
[119] | 208 | } |
---|
[120] | 209 | if (isset($spec['quality']) && IMG_JPG != $spec['dest_file_type']) { |
---|
[136] | 210 | $app->logMsg('The "quality" specification is not used unless IMG_JPG is the dest_file_type.', LOG_INFO, __FILE__, __LINE__); |
---|
[119] | 211 | } |
---|
[120] | 212 | if (isset($spec['progressive']) && IMG_JPG != $spec['dest_file_type']) { |
---|
[136] | 213 | $app->logMsg('The "progressive" specification is not used unless IMG_JPG is the dest_file_type.', LOG_INFO, __FILE__, __LINE__); |
---|
[119] | 214 | } |
---|
[120] | 215 | |
---|
[152] | 216 | // Add to _image_specs array. |
---|
| 217 | if (isset($index) && isset($this->_image_specs[$index])) { |
---|
| 218 | // Merge with existing spec if index is provided. |
---|
| 219 | $final_spec = array_merge($this->_image_specs[$index], $spec); |
---|
| 220 | $this->_image_specs[$index] = $final_spec; |
---|
[120] | 221 | } else { |
---|
[152] | 222 | // Merge with spec defaults. |
---|
| 223 | $final_spec = array_merge($this->_default_image_specs, $spec); |
---|
| 224 | $this->_image_specs[] = $final_spec; |
---|
[120] | 225 | } |
---|
[121] | 226 | |
---|
| 227 | return $final_spec; |
---|
[1] | 228 | } |
---|
[256] | 229 | |
---|
| 230 | /* |
---|
| 231 | * Retreive a value of a thumb specification. |
---|
| 232 | * |
---|
| 233 | * @access public |
---|
| 234 | * @param string $key Key to return. See _default_image_specs above for a list. |
---|
| 235 | * @param int $index The index in the spec array of the value to retreive. The first if not specified. |
---|
| 236 | * @return mixed Value of requested index. |
---|
| 237 | * @author Quinn Comendant <quinn@strangecode.com> |
---|
| 238 | * @version 1.0 |
---|
| 239 | * @since 08 May 2007 15:26:39 |
---|
| 240 | */ |
---|
| 241 | function getSpec($key, $index=null) |
---|
| 242 | { |
---|
| 243 | $index = isset($index) ? $index : 0; |
---|
| 244 | return $this->_image_specs[$index][$key]; |
---|
| 245 | } |
---|
[1] | 246 | |
---|
| 247 | /** |
---|
[119] | 248 | * Process an entire directory of images. |
---|
[1] | 249 | * |
---|
| 250 | * @access public |
---|
| 251 | * @return bool true on success, false on failure. |
---|
| 252 | */ |
---|
[121] | 253 | function processAll($runtime_specs=null) |
---|
[1] | 254 | { |
---|
[136] | 255 | $app =& App::getInstance(); |
---|
| 256 | |
---|
[1] | 257 | // Ensure we have a source. |
---|
[119] | 258 | if ('' == $this->getParam('source_dir')) { |
---|
[136] | 259 | $app->logMsg(sprintf('Source directory not set before processing.'), LOG_ERR, __FILE__, __LINE__); |
---|
[1] | 260 | return false; |
---|
| 261 | } |
---|
[119] | 262 | |
---|
| 263 | // Get all files in source directory. |
---|
| 264 | $dir_handle = opendir($this->getParam('source_dir')); |
---|
| 265 | while ($dir_handle && ($file = readdir($dir_handle)) !== false) { |
---|
| 266 | // If the file name does not start with a dot (. or .. or .htaccess). |
---|
[247] | 267 | if (!preg_match('/^\./', $file) && in_array(mb_strtolower(mb_substr($file, mb_strrpos($file, '.') + 1)), $this->getParam('valid_file_extensions'))) { |
---|
[119] | 268 | $files[] = $file; |
---|
[1] | 269 | } |
---|
| 270 | } |
---|
[42] | 271 | |
---|
[119] | 272 | // Process each found file. |
---|
| 273 | if (is_array($files) && !empty($files)) { |
---|
| 274 | $return_val = 0; |
---|
| 275 | foreach ($files as $file_name) { |
---|
[121] | 276 | $return_val += $this->processFile($file_name, $runtime_specs); |
---|
[119] | 277 | } |
---|
[120] | 278 | $this->_raiseMsg(sprintf(_("Resized %s images."), sizeof($files)), MSG_SUCCESS, __FILE__, __LINE__); |
---|
[119] | 279 | return 0 === $return_val; |
---|
| 280 | } else { |
---|
[152] | 281 | $app->logMsg(sprintf('No source images found in directory: %s', $this->getParam('source_dir')), LOG_NOTICE, __FILE__, __LINE__); |
---|
[119] | 282 | return false; |
---|
| 283 | } |
---|
[1] | 284 | } |
---|
| 285 | |
---|
| 286 | /** |
---|
| 287 | * Generate thumbnails for the specified file. |
---|
| 288 | * |
---|
| 289 | * @access public |
---|
[199] | 290 | * @param string $file_name Name of file with extension. |
---|
[121] | 291 | * @param array $runtime_specs Array of specifications that will override all configured specifications. |
---|
[1] | 292 | * @return bool true on success, false on failure. |
---|
| 293 | */ |
---|
[121] | 294 | function processFile($file_name, $runtime_specs=null) |
---|
[1] | 295 | { |
---|
[136] | 296 | $app =& App::getInstance(); |
---|
| 297 | |
---|
[119] | 298 | // Source file determinted by provided file_name. |
---|
| 299 | $source_file = realpath(sprintf('%s/%s', $this->getParam('source_dir'), $file_name)); |
---|
| 300 | |
---|
| 301 | // Ensure we have a source. |
---|
[152] | 302 | if (sizeof($this->_image_specs) < 1) { |
---|
[121] | 303 | if (is_array($runtime_specs)) { |
---|
| 304 | $this->setSpec($runtime_specs, 0); |
---|
| 305 | } else { |
---|
[136] | 306 | $app->logMsg(sprintf('Image specifications not set before processing.'), LOG_ERR, __FILE__, __LINE__); |
---|
[121] | 307 | return false; |
---|
| 308 | } |
---|
[1] | 309 | } |
---|
[42] | 310 | |
---|
[1] | 311 | // Ensure we have a source. |
---|
[119] | 312 | if ('' == $this->getParam('source_dir')) { |
---|
[136] | 313 | $app->logMsg(sprintf('Source directory not set before processing.'), LOG_ERR, __FILE__, __LINE__); |
---|
[1] | 314 | return false; |
---|
| 315 | } |
---|
[42] | 316 | |
---|
[1] | 317 | // Confirm source image exists. |
---|
[119] | 318 | if (!file_exists($source_file)) { |
---|
[199] | 319 | $this->_raiseMsg(sprintf(_("Image resizing failed: source image <em>%s</em> was not found."), $file_name), MSG_ERR, __FILE__, __LINE__); |
---|
[152] | 320 | $app->logMsg(sprintf('Source image not found: %s', $source_file), LOG_WARNING, __FILE__, __LINE__); |
---|
[1] | 321 | return false; |
---|
| 322 | } |
---|
[42] | 323 | |
---|
[1] | 324 | // Confirm source image is readable. |
---|
[119] | 325 | if (!is_readable($source_file)) { |
---|
[199] | 326 | $this->_raiseMsg(sprintf(_("Image resizing failed: source image <em>%s</em> is not readable."), $file_name), MSG_ERR, __FILE__, __LINE__); |
---|
[152] | 327 | $app->logMsg(sprintf('Source image not readable: %s', $source_file), LOG_WARNING, __FILE__, __LINE__); |
---|
[1] | 328 | return false; |
---|
| 329 | } |
---|
[42] | 330 | |
---|
[1] | 331 | // Confirm source image contains data. |
---|
[119] | 332 | if (filesize($source_file) <= 0) { |
---|
[199] | 333 | $this->_raiseMsg(sprintf(_("Image resizing failed: source image <em>%s</em> is zero bytes."), $file_name), MSG_ERR, __FILE__, __LINE__); |
---|
[152] | 334 | $app->logMsg(sprintf('Source image is zero bytes: %s', $source_file), LOG_WARNING, __FILE__, __LINE__); |
---|
[1] | 335 | return false; |
---|
| 336 | } |
---|
[42] | 337 | |
---|
[1] | 338 | // Confirm source image has a valid file extension. |
---|
[119] | 339 | if (!$this->_validFileExtension($file_name)) { |
---|
[199] | 340 | $this->_raiseMsg(sprintf(_("Image resizing failed: source image <em>%s</em> not a valid type. It must have one of the following file name extensions: %s"), $file_name, join(', ', $this->getParam('valid_file_extensions'))), MSG_ERR, __FILE__, __LINE__); |
---|
[154] | 341 | $app->logMsg(sprintf('Image resizing failed: source image not of valid type: %s', $source_file), LOG_WARNING, __FILE__, __LINE__); |
---|
[1] | 342 | return false; |
---|
| 343 | } |
---|
[42] | 344 | |
---|
[119] | 345 | // Ensure destination directories are created. This will only be called once per page load. |
---|
[152] | 346 | if (!$this->_createDestDirs()) { |
---|
[278] | 347 | $app->logMsg('Image resizing failed: unable to create dest dirs.', LOG_WARNING, __FILE__, __LINE__); |
---|
[152] | 348 | return false; |
---|
| 349 | } |
---|
[119] | 350 | |
---|
| 351 | // To keep this script running even if user tries to stop browser. |
---|
| 352 | ignore_user_abort(true); |
---|
[121] | 353 | ini_set('max_execution_time', 300); |
---|
| 354 | ini_set('max_input_time', 300); |
---|
[1] | 355 | |
---|
| 356 | // This remains zero until something goes wrong. |
---|
[119] | 357 | $return_val = 0; |
---|
[42] | 358 | |
---|
[152] | 359 | foreach ($this->_image_specs as $index => $spec) { |
---|
[119] | 360 | |
---|
[121] | 361 | if (is_array($runtime_specs)) { |
---|
| 362 | // Override with runtime specs. |
---|
| 363 | $spec = $this->setSpec($runtime_specs, $index); |
---|
| 364 | } |
---|
| 365 | |
---|
[199] | 366 | // Destination filename uses the extension defined by dest_file_extension. |
---|
[152] | 367 | if ('/' == $spec['dest_dir']{0}) { |
---|
| 368 | // Absolute path. |
---|
[247] | 369 | $dest_file = sprintf('%s/%s.%s', $spec['dest_dir'], mb_substr($file_name, 0, mb_strrpos($file_name, '.')), $spec['dest_file_extension']); |
---|
[152] | 370 | } else { |
---|
| 371 | // Relative path. |
---|
[247] | 372 | $dest_file = sprintf('%s/%s/%s.%s', $this->getParam('source_dir'), $spec['dest_dir'], mb_substr($file_name, 0, mb_strrpos($file_name, '.')), $spec['dest_file_extension']); |
---|
[152] | 373 | } |
---|
| 374 | |
---|
| 375 | // Ensure destination directory exists and is writable. |
---|
| 376 | if (!is_dir(dirname($dest_file)) || !is_writable(dirname($dest_file))) { |
---|
| 377 | $this->_createDestDirs($dest_file); |
---|
| 378 | if (!is_dir(dirname($dest_file)) || !is_writable(dirname($dest_file))) { |
---|
| 379 | $app->logMsg(sprintf('Image resizing failed, dest_dir invalid: %s', dirname($dest_file)), LOG_ERR, __FILE__, __LINE__); |
---|
| 380 | $return_val++; |
---|
| 381 | continue; |
---|
| 382 | } |
---|
| 383 | } |
---|
[42] | 384 | |
---|
[119] | 385 | // Skip existing thumbnails with file size below $spec['keep_filesize']. |
---|
| 386 | if (isset($spec['keep_filesize']) && file_exists($dest_file)) { |
---|
| 387 | $file_size = filesize($dest_file); |
---|
| 388 | if (false !== $file_size && $file_size < $spec['keep_filesize']) { |
---|
[201] | 389 | $app->logMsg(sprintf('Skipping thumbnail %s. File already exists and file size is less than %s bytes.', $spec['dest_dir'] . '/' . $file_name, $spec['keep_filesize']), LOG_INFO, __FILE__, __LINE__); |
---|
[1] | 390 | continue; |
---|
| 391 | } |
---|
| 392 | } |
---|
[42] | 393 | |
---|
[119] | 394 | // Determine if original file size is smaller than specified thumbnail size. Do not scale-up if $spec['allow_upscaling'] config is set to false. |
---|
| 395 | $image_size = getimagesize($source_file); |
---|
| 396 | if ($image_size['0'] <= $spec['width'] && $image_size['1'] <= $spec['height'] && !$spec['allow_upscaling']) { |
---|
| 397 | $spec['scaling_type'] = IMAGETHUMB_NO_SCALE; |
---|
[201] | 398 | $app->logMsg(sprintf('Image %s smaller than specified %s thumbnail size. Keeping original size.', $file_name, basename($spec['dest_dir'])), LOG_INFO, __FILE__, __LINE__); |
---|
[1] | 399 | } |
---|
[42] | 400 | |
---|
[119] | 401 | // DO IT! Based on available method. |
---|
| 402 | if (IMAGETHUMB_METHOD_NETPBM === $this->getParam('resize_method') && file_exists($this->getParam('anytopnm_binary')) && file_exists($this->getParam('pnmscale_binary')) && file_exists($this->getParam('cjpeg_binary'))) { |
---|
| 403 | // Resize using Netpbm binaries. |
---|
[201] | 404 | $app->logMsg(sprintf('Resizing with Netpbm: %s', $source_file), LOG_DEBUG, __FILE__, __LINE__); |
---|
[119] | 405 | $return_val += $this->_resizeWithNetpbm($source_file, $dest_file, $spec); |
---|
| 406 | } else if (IMAGETHUMB_METHOD_GD === $this->getParam('resize_method') && extension_loaded('gd')) { |
---|
| 407 | // Resize with GD. |
---|
[201] | 408 | $app->logMsg(sprintf('Resizing with GD: %s', $source_file), LOG_DEBUG, __FILE__, __LINE__); |
---|
[119] | 409 | $return_val += $this->_resizeWithGD($source_file, $dest_file, $spec); |
---|
[1] | 410 | } else { |
---|
[152] | 411 | $app->logMsg(sprintf('Image thumbnailing canceled. Neither Netpbm or GD is available.', null), LOG_ERR, __FILE__, __LINE__); |
---|
[119] | 412 | return false; |
---|
[1] | 413 | } |
---|
[119] | 414 | } |
---|
[42] | 415 | |
---|
[152] | 416 | // If > 0, there was a problem thumbnailing. |
---|
[119] | 417 | return 0 === $return_val; |
---|
| 418 | } |
---|
| 419 | |
---|
| 420 | /* |
---|
| 421 | * Use the Netpbm and libjpg cjpeg tools to generate a rescaled compressed image. |
---|
| 422 | * This is the preferred method over GD which has (supposedly) less quality. |
---|
| 423 | * |
---|
| 424 | * @access private |
---|
| 425 | * @param string $source_file Full path to source image file. |
---|
| 426 | * @param string $dest_file Full path to destination image file. |
---|
| 427 | * @param array $spec Array of image size specifications. |
---|
[120] | 428 | * @return int 0 if no error, n > 0 if errors. |
---|
[119] | 429 | * @author Quinn Comendant <quinn@strangecode.com> |
---|
| 430 | * @version 1.0 |
---|
| 431 | * @since 19 May 2006 13:55:46 |
---|
| 432 | */ |
---|
| 433 | function _resizeWithNetpbm($source_file, $dest_file, $spec) |
---|
| 434 | { |
---|
[136] | 435 | $app =& App::getInstance(); |
---|
| 436 | |
---|
[119] | 437 | // Define pnmscale arguments. |
---|
| 438 | switch ($spec['scaling_type']) { |
---|
| 439 | case IMAGETHUMB_FIT_WIDTH : |
---|
| 440 | $pnmscale_args = sprintf(' -width %s ', escapeshellarg($spec['width'])); |
---|
| 441 | break; |
---|
| 442 | case IMAGETHUMB_FIT_HEIGHT : |
---|
| 443 | $pnmscale_args = sprintf(' -height %s ', escapeshellarg($spec['height'])); |
---|
| 444 | break; |
---|
| 445 | case IMAGETHUMB_FIT_LARGER : |
---|
| 446 | $pnmscale_args = sprintf(' -xysize %s %s ', escapeshellarg($spec['width']), escapeshellarg($spec['height'])); |
---|
| 447 | break; |
---|
| 448 | case IMAGETHUMB_STRETCH : |
---|
| 449 | $pnmscale_args = sprintf(' -width %s -height %s ', escapeshellarg($spec['width']), escapeshellarg($spec['height'])); |
---|
| 450 | break; |
---|
| 451 | case IMAGETHUMB_NO_SCALE : |
---|
| 452 | default : |
---|
| 453 | $pnmscale_args = ' 1 '; |
---|
| 454 | break; |
---|
[1] | 455 | } |
---|
[42] | 456 | |
---|
[119] | 457 | // Define cjpeg arguments. |
---|
| 458 | $cjpeg_args = sprintf(' -optimize -quality %s ', escapeshellarg($spec['quality'])); |
---|
| 459 | $cjpeg_args .= (true === $spec['progressive']) ? ' -progressive ' : ''; |
---|
| 460 | |
---|
| 461 | // Format the command that creates the thumbnail. |
---|
[199] | 462 | $command = sprintf('%s %s | %s %s | %s %s > %s', |
---|
[119] | 463 | escapeshellcmd($this->getParam('anytopnm_binary')), |
---|
| 464 | escapeshellcmd($source_file), |
---|
| 465 | escapeshellcmd($this->getParam('pnmscale_binary')), |
---|
| 466 | escapeshellcmd($pnmscale_args), |
---|
| 467 | escapeshellcmd($this->getParam('cjpeg_binary')), |
---|
| 468 | escapeshellcmd($cjpeg_args), |
---|
[199] | 469 | escapeshellcmd($dest_file) |
---|
[119] | 470 | ); |
---|
[136] | 471 | $app->logMsg(sprintf('ImageThumb Netpbm command: %s', $command), LOG_DEBUG, __FILE__, __LINE__); |
---|
[119] | 472 | |
---|
| 473 | // Execute! |
---|
| 474 | exec($command, $output, $return_val); |
---|
| 475 | |
---|
| 476 | if (0 === $return_val) { |
---|
| 477 | // Success! |
---|
| 478 | // Make the thumbnail writable so the user can delete it over ftp without being 'apache'. |
---|
| 479 | chmod($dest_file, $this->getParam('dest_file_perms')); |
---|
[136] | 480 | $app->logMsg(sprintf('Successfully resized image %s', $spec['dest_dir'] . '/' . basename($dest_file), $return_val), LOG_DEBUG, __FILE__, __LINE__); |
---|
[119] | 481 | } else { |
---|
| 482 | // An error occurred. |
---|
[136] | 483 | $app->logMsg(sprintf('Image %s failed resizing with return value: %s%s', $spec['dest_dir'] . '/' . basename($dest_file), $return_val, empty($output) ? '' : ' (' . getDump($output) . ')'), LOG_ERR, __FILE__, __LINE__); |
---|
[119] | 484 | } |
---|
| 485 | |
---|
| 486 | // Return from the command will be > 0 if there was an error. |
---|
| 487 | return $return_val; |
---|
[1] | 488 | } |
---|
| 489 | |
---|
[119] | 490 | /* |
---|
| 491 | * Use PHP's built-in GD tools to generate a rescaled compressed image. |
---|
| 492 | * |
---|
| 493 | * @access private |
---|
| 494 | * @param string $source_file Full path to source image file. |
---|
| 495 | * @param string $dest_file Full path to destination image file. |
---|
| 496 | * @param array $spec Array of image size specifications. |
---|
[120] | 497 | * @return int 0 if no error, n > 0 if errors. |
---|
[119] | 498 | * @author Quinn Comendant <quinn@strangecode.com> |
---|
| 499 | * @version 1.0 |
---|
| 500 | * @since 19 May 2006 15:46:02 |
---|
| 501 | */ |
---|
| 502 | function _resizeWithGD($source_file, $dest_file, $spec) |
---|
[1] | 503 | { |
---|
[136] | 504 | $app =& App::getInstance(); |
---|
| 505 | |
---|
[119] | 506 | // Get original file dimensions and type. |
---|
| 507 | list($source_image_width, $source_image_height, $source_image_type) = getimagesize($source_file); |
---|
| 508 | |
---|
| 509 | // Define destination image dimentions. |
---|
| 510 | switch ($spec['scaling_type']) { |
---|
| 511 | case IMAGETHUMB_FIT_WIDTH : |
---|
[120] | 512 | $dest_image_width = $spec['width']; |
---|
[119] | 513 | $dest_image_height = $source_image_height * ($spec['width'] / $source_image_width); |
---|
| 514 | break; |
---|
| 515 | case IMAGETHUMB_FIT_HEIGHT : |
---|
[120] | 516 | $dest_image_height = $spec['height']; |
---|
[119] | 517 | $dest_image_width = $source_image_width * ($spec['height'] / $source_image_height); |
---|
| 518 | break; |
---|
| 519 | case IMAGETHUMB_FIT_LARGER : |
---|
[120] | 520 | if (($source_image_width * ($spec['height'] / $source_image_height)) <= $spec['width']) { |
---|
| 521 | // Height is larger. |
---|
| 522 | $dest_image_height = $spec['height']; |
---|
| 523 | $dest_image_width = $source_image_width * ($spec['height'] / $source_image_height); |
---|
[119] | 524 | } else { |
---|
[120] | 525 | // Width is larger. |
---|
| 526 | $dest_image_width = $spec['width']; |
---|
| 527 | $dest_image_height = $source_image_height * ($spec['width'] / $source_image_width); |
---|
[119] | 528 | } |
---|
| 529 | break; |
---|
| 530 | case IMAGETHUMB_STRETCH : |
---|
| 531 | $dest_image_width = $spec['width']; |
---|
| 532 | $dest_image_height = $spec['height']; |
---|
| 533 | break; |
---|
| 534 | case IMAGETHUMB_NO_SCALE : |
---|
| 535 | default : |
---|
| 536 | $dest_image_width = $source_image_width; |
---|
| 537 | $dest_image_height = $source_image_height; |
---|
| 538 | break; |
---|
| 539 | } |
---|
| 540 | |
---|
| 541 | // Create source image data in memory. |
---|
| 542 | switch ($source_image_type) { |
---|
[224] | 543 | case IMAGETYPE_JPEG : // = 2 |
---|
[120] | 544 | $source_image_resource = imagecreatefromjpeg($source_file); |
---|
[119] | 545 | break; |
---|
[224] | 546 | case IMAGETYPE_PNG : // = 3 |
---|
[120] | 547 | $source_image_resource = imagecreatefrompng($source_file); |
---|
[119] | 548 | break; |
---|
[224] | 549 | case IMAGETYPE_GIF : // = 1 |
---|
[120] | 550 | $source_image_resource = imagecreatefromgif($source_file); |
---|
[119] | 551 | break; |
---|
[224] | 552 | case IMAGETYPE_WBMP : // = 15 |
---|
[120] | 553 | $source_image_resource = imagecreatefromwbmp($source_file); |
---|
[224] | 554 | case IMAGETYPE_SWF : // = 4 |
---|
| 555 | case IMAGETYPE_PSD : // = 5 |
---|
| 556 | case IMAGETYPE_BMP : // = 6 |
---|
| 557 | case IMAGETYPE_TIFF_II : // = 7 |
---|
| 558 | case IMAGETYPE_TIFF_MM : // = 8 |
---|
| 559 | case IMAGETYPE_JPC : // = 9 |
---|
| 560 | case IMAGETYPE_JP2 : // = 10 |
---|
| 561 | case IMAGETYPE_JPX : // = 11 |
---|
| 562 | case IMAGETYPE_JB2 : // = 12 |
---|
| 563 | case IMAGETYPE_SWC : // = 13 |
---|
| 564 | case IMAGETYPE_IFF : // = 14 |
---|
| 565 | case IMAGETYPE_XBM : // = 16 |
---|
[119] | 566 | default : |
---|
[136] | 567 | $app->logMsg(sprintf('Source image type %s not supported.', $source_image_type), LOG_WARNING, __FILE__, __LINE__); |
---|
[120] | 568 | return 1; |
---|
[119] | 569 | break; |
---|
[1] | 570 | } |
---|
[120] | 571 | if (!$source_image_resource) { |
---|
[136] | 572 | $app->logMsg(sprintf('Error creating %s image in memory from %s', $source_image_type, $source_file), LOG_WARNING, __FILE__, __LINE__); |
---|
[120] | 573 | return 1; |
---|
[119] | 574 | } |
---|
| 575 | |
---|
| 576 | // Create destination image data in memory. |
---|
[120] | 577 | $dest_image_resource = imagecreatetruecolor($dest_image_width, $dest_image_height); |
---|
[42] | 578 | |
---|
[119] | 579 | // Resample! |
---|
[120] | 580 | if (!imagecopyresampled($dest_image_resource, $source_image_resource, 0, 0, 0, 0, $dest_image_width, $dest_image_height, $source_image_width, $source_image_height)) { |
---|
[136] | 581 | $app->logMsg(sprintf('Error resampling image %s', $source_file), LOG_WARNING, __FILE__, __LINE__); |
---|
[152] | 582 | // Always cleanup images from memory. |
---|
| 583 | imagedestroy($source_image_resource); |
---|
| 584 | imagedestroy($dest_image_resource); |
---|
[120] | 585 | return 1; |
---|
[1] | 586 | } |
---|
[120] | 587 | |
---|
| 588 | // Sharpen image using a custom filter matrix. |
---|
| 589 | if (phpversion() > '5.1' && true === $spec['sharpen'] && $spec['sharpen_value'] > 0) { |
---|
[256] | 590 | $sharpen_value = round((((48 - 10) / (100 - 1)) * (100 - $spec['sharpen_value'])) + 10); /// WTF is this math? |
---|
[120] | 591 | imageconvolution($dest_image_resource, array(array(-1,-1,-1),array(-1,$sharpen_value,-1),array(-1,-1,-1)), ($sharpen_value - 8), 0); |
---|
| 592 | } |
---|
[42] | 593 | |
---|
[119] | 594 | // Save image. |
---|
| 595 | $return_val = true; |
---|
[120] | 596 | switch ($spec['dest_file_type']) { |
---|
[119] | 597 | case IMG_JPG : |
---|
[120] | 598 | imageinterlace($dest_image_resource, (true == $spec['progressive'] ? 1 : 0)); |
---|
| 599 | $return_val = imagejpeg($dest_image_resource, $dest_file, $spec['quality']); |
---|
[119] | 600 | break; |
---|
| 601 | case IMG_PNG : |
---|
[120] | 602 | $return_val = imagepng($dest_image_resource, $dest_file); |
---|
[119] | 603 | break; |
---|
| 604 | case IMG_GIF : |
---|
[120] | 605 | $return_val = imagegif($dest_image_resource, $dest_file); |
---|
[119] | 606 | break; |
---|
| 607 | case IMG_WBMP : |
---|
[120] | 608 | $return_val = imagewbmp($dest_image_resource, $dest_file); |
---|
[119] | 609 | break; |
---|
| 610 | default : |
---|
[136] | 611 | $app->logMsg(sprintf('Destination image type %s not supported for image %s.', $spec['dest_file_type'], $dest_file), LOG_WARNING, __FILE__, __LINE__); |
---|
[152] | 612 | // Always cleanup images from memory. |
---|
| 613 | imagedestroy($source_image_resource); |
---|
| 614 | imagedestroy($dest_image_resource); |
---|
[120] | 615 | return 1; |
---|
[119] | 616 | break; |
---|
| 617 | } |
---|
| 618 | |
---|
[152] | 619 | // Always cleanup images from memory. |
---|
| 620 | imagedestroy($source_image_resource); |
---|
| 621 | imagedestroy($dest_image_resource); |
---|
| 622 | |
---|
[119] | 623 | if ($return_val) { |
---|
| 624 | // Success! |
---|
| 625 | // Make the thumbnail writable so the user can delete it over ftp without being 'apache'. |
---|
[152] | 626 | if (!chmod($dest_file, $this->getParam('dest_file_perms'))) { |
---|
| 627 | $app->logMsg(sprintf('chmod failed on file: %s', $dest_file), LOG_ERR, __FILE__, __LINE__); |
---|
| 628 | } |
---|
| 629 | $app->logMsg(sprintf('Successfully resized image: %s', $dest_file), LOG_DEBUG, __FILE__, __LINE__); |
---|
[120] | 630 | return 0; |
---|
[1] | 631 | } else { |
---|
[119] | 632 | // An error occurred. |
---|
[152] | 633 | $app->logMsg(sprintf('Failed resizing image: %s', $dest_file), LOG_ERR, __FILE__, __LINE__); |
---|
[120] | 634 | return 1; |
---|
[1] | 635 | } |
---|
| 636 | } |
---|
| 637 | |
---|
| 638 | /** |
---|
| 639 | * Delete the thumbnails for the specified file name. |
---|
| 640 | * |
---|
| 641 | * @access public |
---|
[199] | 642 | * @param string $file_name The file name to delete, with extension. |
---|
[1] | 643 | * @return bool true on success, false on failure. |
---|
| 644 | */ |
---|
| 645 | function deleteThumbs($file_name) |
---|
| 646 | { |
---|
[136] | 647 | $app =& App::getInstance(); |
---|
| 648 | |
---|
[1] | 649 | // Ensure we have a source. |
---|
[119] | 650 | if ('' == $this->getParam('source_dir')) { |
---|
[136] | 651 | $app->logMsg(sprintf('Source directory not set before processing.'), LOG_ERR, __FILE__, __LINE__); |
---|
[1] | 652 | return false; |
---|
| 653 | } |
---|
[42] | 654 | |
---|
[119] | 655 | $return_val = 0; |
---|
[152] | 656 | foreach ($this->_image_specs as $spec) { |
---|
| 657 | if ('/' == $spec['dest_dir']{0}) { |
---|
| 658 | // Absolute path. |
---|
[247] | 659 | $dest_file = realpath(sprintf('%s/%s.%s', $spec['dest_dir'], mb_substr($file_name, 0, mb_strrpos($file_name, '.')), $spec['dest_file_extension'])); |
---|
[152] | 660 | } else { |
---|
| 661 | // Relative path. |
---|
[247] | 662 | $dest_file = realpath(sprintf('%s/%s/%s.%s', $this->getParam('source_dir'), $spec['dest_dir'], mb_substr($file_name, 0, mb_strrpos($file_name, '.')), $spec['dest_file_extension'])); |
---|
[152] | 663 | } |
---|
[119] | 664 | if (file_exists($dest_file)) { |
---|
| 665 | if (!unlink($dest_file)) { |
---|
| 666 | $return_val++; |
---|
[141] | 667 | $app->logMsg(sprintf('Delete thumbs failed: %s', $dest_file), LOG_WARNING, __FILE__, __LINE__); |
---|
[1] | 668 | } |
---|
| 669 | } |
---|
| 670 | } |
---|
[204] | 671 | $app->logMsg(sprintf('Thumbnails deleted for file: %s', $file_name), LOG_INFO, __FILE__, __LINE__); |
---|
[119] | 672 | return 0 === $return_val; |
---|
[1] | 673 | } |
---|
| 674 | |
---|
| 675 | /** |
---|
| 676 | * Delete the source image with the specified file name. |
---|
| 677 | * |
---|
| 678 | * @access public |
---|
[199] | 679 | * @param string $file_name The file name to delete, with extension. |
---|
[1] | 680 | * @return bool true on success, false on failure. |
---|
| 681 | */ |
---|
| 682 | function deleteOriginal($file_name) |
---|
| 683 | { |
---|
[136] | 684 | $app =& App::getInstance(); |
---|
| 685 | |
---|
[1] | 686 | // Ensure we have a source. |
---|
[119] | 687 | if ('' == $this->getParam('source_dir')) { |
---|
[136] | 688 | $app->logMsg(sprintf('Source directory not set before processing.'), LOG_ERR, __FILE__, __LINE__); |
---|
[1] | 689 | return false; |
---|
| 690 | } |
---|
[42] | 691 | |
---|
[201] | 692 | // Ensure we have a source. |
---|
| 693 | if ('' == $file_name) { |
---|
| 694 | $app->logMsg(sprintf('Cannot delete, filename empty.'), LOG_WARNING, __FILE__, __LINE__); |
---|
| 695 | return false; |
---|
| 696 | } |
---|
| 697 | |
---|
[119] | 698 | $source_file = realpath(sprintf('%s/%s', $this->getParam('source_dir'), $file_name)); |
---|
[201] | 699 | if (is_file($source_file) && unlink($source_file)) { |
---|
| 700 | $app->logMsg(sprintf('Original file successfully deleted: %s', $file_name), LOG_INFO, __FILE__, __LINE__); |
---|
| 701 | return true; |
---|
| 702 | } else { |
---|
[141] | 703 | $app->logMsg(sprintf('Delete original failed: %s', $source_file), LOG_WARNING, __FILE__, __LINE__); |
---|
[1] | 704 | return false; |
---|
| 705 | } |
---|
| 706 | } |
---|
[42] | 707 | |
---|
[1] | 708 | /** |
---|
| 709 | * Returns true if file exists. |
---|
| 710 | * |
---|
| 711 | * @access public |
---|
[199] | 712 | * @param string $file_name The file name to test, with extension. |
---|
[1] | 713 | * @return bool true on success, false on failure. |
---|
| 714 | */ |
---|
| 715 | function exists($file_name) |
---|
| 716 | { |
---|
[136] | 717 | $app =& App::getInstance(); |
---|
| 718 | |
---|
[1] | 719 | // Ensure we have a source. |
---|
[119] | 720 | if ('' == $this->getParam('source_dir')) { |
---|
[136] | 721 | $app->logMsg(sprintf('Source directory not set before processing.'), LOG_ERR, __FILE__, __LINE__); |
---|
[1] | 722 | return false; |
---|
| 723 | } |
---|
[42] | 724 | |
---|
[119] | 725 | $source_file = realpath(sprintf('%s/%s', $this->getParam('source_dir'), $file_name)); |
---|
| 726 | return file_exists($source_file); |
---|
[1] | 727 | } |
---|
[42] | 728 | |
---|
[1] | 729 | /** |
---|
[199] | 730 | * Tests if extension of $file_name is in the array valid_file_extensions. |
---|
[1] | 731 | * |
---|
| 732 | * @access public |
---|
| 733 | * @param string $file_name A file name. |
---|
| 734 | * @return bool True on success, false on failure. |
---|
| 735 | */ |
---|
[119] | 736 | function _validFileExtension($file_name) |
---|
[1] | 737 | { |
---|
| 738 | preg_match('/.*?\.(\w+)$/i', $file_name, $ext); |
---|
[247] | 739 | return !empty($ext) && in_array(mb_strtolower($ext[1]), $this->getParam('valid_file_extensions')); |
---|
[1] | 740 | } |
---|
[42] | 741 | |
---|
[1] | 742 | /** |
---|
[119] | 743 | * Make directory for each specified thumbnail size, if it doesn't exist. |
---|
[1] | 744 | * |
---|
[119] | 745 | * @access public |
---|
| 746 | * @return bool true on success, false on failure. |
---|
| 747 | */ |
---|
[152] | 748 | function _createDestDirs($filename=null) |
---|
[119] | 749 | { |
---|
[136] | 750 | $app =& App::getInstance(); |
---|
| 751 | |
---|
[152] | 752 | // Keep track of directories we've already created. |
---|
| 753 | $dd_hash = md5(isset($filename) ? dirname($filename) : 'none'); |
---|
| 754 | static $already_checked = array(); |
---|
[119] | 755 | |
---|
[152] | 756 | $return_val = 0; |
---|
| 757 | |
---|
| 758 | if (!isset($already_checked[$dd_hash])) { |
---|
[119] | 759 | // Ensure we have a source. |
---|
| 760 | if ('' == $this->getParam('source_dir')) { |
---|
[136] | 761 | $app->logMsg(sprintf('Source directory not set before creating destination directories.'), LOG_ERR, __FILE__, __LINE__); |
---|
[119] | 762 | return false; |
---|
| 763 | } |
---|
[278] | 764 | |
---|
[119] | 765 | // Loop through specs and ensure all dirs are created. |
---|
[152] | 766 | foreach ($this->_image_specs as $spec) { |
---|
| 767 | if (isset($filename)) { |
---|
| 768 | $dest_dir = dirname($filename); |
---|
| 769 | } else { |
---|
| 770 | if ('/' == $spec['dest_dir']{0}) { |
---|
| 771 | // Absolute path. |
---|
| 772 | $dest_dir = $spec['dest_dir']; |
---|
| 773 | } else { |
---|
| 774 | // Relative path. |
---|
| 775 | $dest_dir = sprintf('%s/%s', $this->getParam('source_dir'), $spec['dest_dir']); |
---|
[119] | 776 | } |
---|
| 777 | } |
---|
[152] | 778 | if (!file_exists($dest_dir)) { |
---|
[278] | 779 | $app->logMsg(sprintf('Creating dest dir: %s', $dest_dir), LOG_DEBUG, __FILE__, __LINE__); |
---|
[154] | 780 | if (phpversion() > '5') { |
---|
[152] | 781 | // Recursive. |
---|
[154] | 782 | if (!file_exists($dest_dir) && !($ret = mkdir($dest_dir, $this->getParam('dest_dir_perms'), true))) { |
---|
[152] | 783 | $return_val++; |
---|
| 784 | $app->logMsg(sprintf('mkdir failure: %s', $dest_dir), LOG_ERR, __FILE__, __LINE__); |
---|
| 785 | } |
---|
| 786 | } else { |
---|
| 787 | // Recursive mkdir for php 4. |
---|
| 788 | $path = ''; |
---|
[278] | 789 | $depth = 0; |
---|
| 790 | $ret = true; |
---|
[152] | 791 | foreach (array_diff(explode('/', $dest_dir), array('')) as $dir) { |
---|
| 792 | $path .= '/' . $dir; |
---|
[278] | 793 | $depth++; |
---|
| 794 | /// FIXME: This is a dirty way to make this work when open_basedir prevents looking at the first 3 directories in an absolute path. |
---|
| 795 | if ($depth > 3 && !($ret = file_exists($path) ? true : mkdir($path, $this->getParam('dest_dir_perms')))) { |
---|
[152] | 796 | $return_val++; |
---|
| 797 | $app->logMsg(sprintf('mkdir failure: %s', $path), LOG_ERR, __FILE__, __LINE__); |
---|
| 798 | break; |
---|
[278] | 799 | } else { |
---|
| 800 | $app->logMsg(sprintf('mkdir attempt: %s', $path), LOG_DEBUG, __FILE__, __LINE__); |
---|
[152] | 801 | } |
---|
| 802 | } |
---|
| 803 | } |
---|
| 804 | |
---|
| 805 | if ($ret) { |
---|
| 806 | $app->logMsg(sprintf('mkdir success: %s', $dest_dir), LOG_DEBUG, __FILE__, __LINE__); |
---|
| 807 | } |
---|
[278] | 808 | } else { |
---|
| 809 | $app->logMsg(sprintf('Dest dir exists: %s', $dest_dir), LOG_DEBUG, __FILE__, __LINE__); |
---|
[152] | 810 | } |
---|
[119] | 811 | } |
---|
| 812 | } |
---|
| 813 | |
---|
[152] | 814 | $already_checked[$dd_hash] = true; |
---|
| 815 | |
---|
| 816 | // If > 0, there was a problem creating dest dirs. |
---|
| 817 | return 0 === $return_val; |
---|
[119] | 818 | } |
---|
| 819 | |
---|
| 820 | /** |
---|
[136] | 821 | * An alias for $app->raiseMsg that only sends messages configured by display_messages. |
---|
[119] | 822 | * |
---|
[1] | 823 | * @access public |
---|
| 824 | * |
---|
| 825 | * @param string $message The text description of the message. |
---|
| 826 | * @param int $type The type of message: MSG_NOTICE, |
---|
| 827 | * MSG_SUCCESS, MSG_WARNING, or MSG_ERR. |
---|
| 828 | * @param string $file __FILE__. |
---|
| 829 | * @param string $line __LINE__. |
---|
| 830 | */ |
---|
[119] | 831 | function _raiseMsg($message, $type, $file, $line) |
---|
[1] | 832 | { |
---|
[136] | 833 | $app =& App::getInstance(); |
---|
| 834 | |
---|
[119] | 835 | if ($this->getParam('display_messages') === true || (is_int($this->getParam('display_messages')) && $this->getParam('display_messages') & $type > 0)) { |
---|
[136] | 836 | $app->raiseMsg($message, $type, $file, $line); |
---|
[1] | 837 | } |
---|
| 838 | } |
---|
| 839 | |
---|
| 840 | } // End of class. |
---|
[278] | 841 | ?> |
---|