PageRenderTime 67ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 1ms

/core/model/phpthumb/phpthumb.class.php

http://github.com/modxcms/revolution
PHP | 4443 lines | 3658 code | 487 blank | 298 comment | 873 complexity | 7c87218066514b922c8b6d56f41cd943 MD5 | raw file
Possible License(s): GPL-2.0, Apache-2.0, BSD-3-Clause, LGPL-2.1

Large files files are truncated, but you can click here to view the full file

  1. <?php
  2. //////////////////////////////////////////////////////////////
  3. // phpThumb() by James Heinrich <info@silisoftware.com> //
  4. // available at http://phpthumb.sourceforge.net //
  5. // and/or https://github.com/JamesHeinrich/phpThumb //
  6. //////////////////////////////////////////////////////////////
  7. /// //
  8. // See: phpthumb.readme.txt for usage instructions //
  9. // ///
  10. //////////////////////////////////////////////////////////////
  11. if (!class_exists('phpthumb_functions'))
  12. {
  13. ob_start();
  14. if(!include_once __DIR__ . '/phpthumb.functions.php')
  15. {
  16. ob_end_flush();
  17. die('failed to include_once("' . __DIR__ . '/phpthumb.functions.php")');
  18. }
  19. ob_end_clean();
  20. }
  21. class phpthumb {
  22. // public:
  23. // START PARAMETERS (for object mode and phpThumb.php)
  24. // See phpthumb.readme.txt for descriptions of what each of these values are
  25. public $src = null; // SouRCe filename
  26. public $new = null; // NEW image (phpThumb.php only)
  27. public $w = null; // Width
  28. public $h = null; // Height
  29. public $wp = null; // Width (Portrait Images Only)
  30. public $hp = null; // Height (Portrait Images Only)
  31. public $wl = null; // Width (Landscape Images Only)
  32. public $hl = null; // Height (Landscape Images Only)
  33. public $ws = null; // Width (Square Images Only)
  34. public $hs = null; // Height (Square Images Only)
  35. public $f = null; // output image Format
  36. public $q = 75; // jpeg output Quality
  37. public $sx = null; // Source crop top-left X position
  38. public $sy = null; // Source crop top-left Y position
  39. public $sw = null; // Source crop Width
  40. public $sh = null; // Source crop Height
  41. public $zc = null; // Zoom Crop
  42. public $bc = null; // Border Color
  43. public $bg = null; // BackGround color
  44. public $fltr = array(); // FiLTeRs
  45. public $goto = null; // GO TO url after processing
  46. public $err = null; // default ERRor image filename
  47. public $xto = null; // extract eXif Thumbnail Only
  48. public $ra = null; // Rotate by Angle
  49. public $ar = null; // Auto Rotate
  50. public $aoe = null; // Allow Output Enlargement
  51. public $far = null; // Fixed Aspect Ratio
  52. public $iar = null; // Ignore Aspect Ratio
  53. public $maxb = null; // MAXimum Bytes
  54. public $down = null; // DOWNload thumbnail filename
  55. public $md5s = null; // MD5 hash of Source image
  56. public $sfn = 0; // Source Frame Number
  57. public $dpi = 150; // Dots Per Inch for vector source formats
  58. public $sia = null; // Save Image As filename
  59. public $file = null; // >>>deprecated, DO NOT USE, will be removed in future versions<<<
  60. public $phpThumbDebug = null;
  61. // END PARAMETERS
  62. // public:
  63. // START CONFIGURATION OPTIONS (for object mode only)
  64. // See phpThumb.config.php for descriptions of what each of these settings do
  65. // * Directory Configuration
  66. public $config_cache_directory = null;
  67. public $config_cache_directory_depth = 0;
  68. public $config_cache_disable_warning = true;
  69. public $config_cache_source_enabled = false;
  70. public $config_cache_source_directory = null;
  71. public $config_temp_directory = null;
  72. public $config_document_root = null;
  73. // * Default output configuration:
  74. public $config_output_format = 'jpeg';
  75. public $config_output_maxwidth = 0;
  76. public $config_output_maxheight = 0;
  77. public $config_output_interlace = true;
  78. // * Error message configuration
  79. public $config_error_image_width = 400;
  80. public $config_error_image_height = 100;
  81. public $config_error_message_image_default = '';
  82. public $config_error_bgcolor = 'CCCCFF';
  83. public $config_error_textcolor = 'FF0000';
  84. public $config_error_fontsize = 1;
  85. public $config_error_die_on_error = false;
  86. public $config_error_silent_die_on_error = false;
  87. public $config_error_die_on_source_failure = true;
  88. // * Anti-Hotlink Configuration:
  89. public $config_nohotlink_enabled = true;
  90. public $config_nohotlink_valid_domains = array();
  91. public $config_nohotlink_erase_image = true;
  92. public $config_nohotlink_text_message = 'Off-server thumbnailing is not allowed';
  93. // * Off-server Linking Configuration:
  94. public $config_nooffsitelink_enabled = false;
  95. public $config_nooffsitelink_valid_domains = array();
  96. public $config_nooffsitelink_require_refer = false;
  97. public $config_nooffsitelink_erase_image = true;
  98. public $config_nooffsitelink_watermark_src = '';
  99. public $config_nooffsitelink_text_message = 'Off-server linking is not allowed';
  100. // * Border & Background default colors
  101. public $config_border_hexcolor = '000000';
  102. public $config_background_hexcolor = 'FFFFFF';
  103. // * TrueType Fonts
  104. public $config_ttf_directory = './fonts';
  105. public $config_max_source_pixels = null;
  106. public $config_use_exif_thumbnail_for_speed = false;
  107. public $config_allow_local_http_src = false;
  108. public $config_imagemagick_path = null;
  109. public $config_prefer_imagemagick = true;
  110. public $config_imagemagick_use_thumbnail = true;
  111. public $config_cache_maxage = null;
  112. public $config_cache_maxsize = null;
  113. public $config_cache_maxfiles = null;
  114. public $config_cache_source_filemtime_ignore_local = false;
  115. public $config_cache_source_filemtime_ignore_remote = true;
  116. public $config_cache_default_only_suffix = false;
  117. public $config_cache_force_passthru = true;
  118. public $config_cache_prefix = ''; // default value set in the constructor below
  119. // * MySQL
  120. public $config_mysql_extension = null;
  121. public $config_mysql_query = null;
  122. public $config_mysql_hostname = null;
  123. public $config_mysql_username = null;
  124. public $config_mysql_password = null;
  125. public $config_mysql_database = null;
  126. // * Security
  127. public $config_high_security_enabled = true;
  128. public $config_high_security_password = null;
  129. public $config_high_security_url_separator = '&';
  130. public $config_disable_debug = true;
  131. public $config_allow_src_above_docroot = false;
  132. public $config_allow_src_above_phpthumb = true;
  133. public $config_auto_allow_symlinks = true; // allow symlink target directories without explicitly whitelisting them
  134. public $config_additional_allowed_dirs = array(); // additional directories to allow source images to be read from
  135. public $config_file_create_mask = 0755;
  136. public $config_dir_create_mask = 0755;
  137. // * HTTP fopen
  138. public $config_http_fopen_timeout = 10;
  139. public $config_http_follow_redirect = true;
  140. // * Compatability
  141. public $config_disable_pathinfo_parsing = false;
  142. public $config_disable_imagecopyresampled = false;
  143. public $config_disable_onlycreateable_passthru = false;
  144. public $config_disable_realpath = false;
  145. public $config_http_user_agent = 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7.12) Gecko/20050915 Firefox/1.0.7';
  146. // END CONFIGURATION OPTIONS
  147. // public: error messages (read-only; persistant)
  148. public $debugmessages = array();
  149. public $debugtiming = array();
  150. public $fatalerror = null;
  151. // private: (should not be modified directly)
  152. public $thumbnailQuality = 75;
  153. public $thumbnailFormat = null;
  154. public $sourceFilename = null;
  155. public $rawImageData = null;
  156. public $IMresizedData = null;
  157. public $outputImageData = null;
  158. public $useRawIMoutput = false;
  159. public $gdimg_output = null;
  160. public $gdimg_source = null;
  161. public $getimagesizeinfo = null;
  162. public $source_width = null;
  163. public $source_height = null;
  164. public $thumbnailCropX = null;
  165. public $thumbnailCropY = null;
  166. public $thumbnailCropW = null;
  167. public $thumbnailCropH = null;
  168. public $exif_thumbnail_width = null;
  169. public $exif_thumbnail_height = null;
  170. public $exif_thumbnail_type = null;
  171. public $exif_thumbnail_data = null;
  172. public $exif_raw_data = null;
  173. public $thumbnail_width = null;
  174. public $thumbnail_height = null;
  175. public $thumbnail_image_width = null;
  176. public $thumbnail_image_height = null;
  177. public $tempFilesToDelete = array();
  178. public $cache_filename = null;
  179. public $AlphaCapableFormats = array( 'png', 'ico', 'gif', 'webp');
  180. public $is_alpha = false;
  181. public $iswindows = null;
  182. public $issafemode = null;
  183. public $php_memory_limit = null;
  184. public $phpthumb_version = '1.7.15-202004301145';
  185. //////////////////////////////////////////////////////////////////////
  186. // public: constructor
  187. public function __construct() {
  188. $this->phpThumb();
  189. }
  190. public function phpThumb() {
  191. $this->DebugTimingMessage('phpThumb() constructor', __FILE__, __LINE__);
  192. $this->DebugMessage('phpThumb() v'.$this->phpthumb_version, __FILE__, __LINE__);
  193. foreach (array(ini_get('memory_limit'), get_cfg_var('memory_limit')) as $php_config_memory_limit) {
  194. if ('' !== $php_config_memory_limit) {
  195. if (strtoupper($php_config_memory_limit[ strlen($php_config_memory_limit) - 1 ]) == 'G') { // PHP memory limit expressed in Gigabytes
  196. $php_config_memory_limit = (int) substr($php_config_memory_limit, 0, -1) * 1073741824;
  197. } elseif (strtoupper($php_config_memory_limit[ strlen($php_config_memory_limit) - 1 ]) == 'M') { // PHP memory limit expressed in Megabytes
  198. $php_config_memory_limit = (int) substr($php_config_memory_limit, 0, -1) * 1048576;
  199. }
  200. $this->php_memory_limit = max($this->php_memory_limit, $php_config_memory_limit);
  201. }
  202. }
  203. if ($this->php_memory_limit > 0) { // could be "-1" for "no limit"
  204. $this->config_max_source_pixels = round($this->php_memory_limit * 0.20); // 20% of memory_limit
  205. }
  206. $this->iswindows = (bool) (strtoupper(substr(PHP_OS, 0, 3)) == 'WIN');
  207. $this->issafemode = (bool) preg_match('#(1|ON)#i', ini_get('safe_mode'));
  208. $this->config_document_root = (!empty($_SERVER['DOCUMENT_ROOT']) ? $_SERVER['DOCUMENT_ROOT'] : $this->config_document_root);
  209. $this->config_cache_prefix = ( isset($_SERVER['SERVER_NAME']) ? $_SERVER['SERVER_NAME'].'_' : '');
  210. $this->purgeTempFiles(); // purge existing temp files if re-initializing object
  211. $php_sapi_name = strtolower(function_exists('php_sapi_name') ? PHP_SAPI : '');
  212. if ($php_sapi_name == 'cli') {
  213. $this->config_allow_src_above_docroot = true;
  214. }
  215. if (!$this->config_disable_debug) {
  216. // if debug mode is enabled, force phpThumbDebug output, do not allow normal thumbnails to be generated
  217. $this->phpThumbDebug = (null === $this->phpThumbDebug ? 9 : max(1, (int) $this->phpThumbDebug));
  218. }
  219. }
  220. public function __destruct() {
  221. $this->purgeTempFiles();
  222. }
  223. // public:
  224. public function purgeTempFiles() {
  225. foreach ($this->tempFilesToDelete as $tempFileToDelete) {
  226. if (file_exists($tempFileToDelete)) {
  227. $this->DebugMessage('Deleting temp file "'.$tempFileToDelete.'"', __FILE__, __LINE__);
  228. @unlink($tempFileToDelete);
  229. }
  230. }
  231. $this->tempFilesToDelete = array();
  232. return true;
  233. }
  234. // public:
  235. public function setSourceFilename($sourceFilename) {
  236. //$this->resetObject();
  237. //$this->rawImageData = null;
  238. $this->sourceFilename = $sourceFilename;
  239. $this->src = $sourceFilename;
  240. if (null === $this->config_output_format) {
  241. $sourceFileExtension = strtolower(substr(strrchr($sourceFilename, '.'), 1));
  242. if (preg_match('#^[a-z]{3,4}$#', $sourceFileExtension)) {
  243. $this->config_output_format = $sourceFileExtension;
  244. $this->DebugMessage('setSourceFilename('.$sourceFilename.') set $this->config_output_format to "'.$sourceFileExtension.'"', __FILE__, __LINE__);
  245. } else {
  246. $this->DebugMessage('setSourceFilename('.$sourceFilename.') did NOT set $this->config_output_format to "'.$sourceFileExtension.'" because it did not seem like an appropriate image format', __FILE__, __LINE__);
  247. }
  248. }
  249. $this->DebugMessage('setSourceFilename('.$sourceFilename.') set $this->sourceFilename to "'.$this->sourceFilename.'"', __FILE__, __LINE__);
  250. return true;
  251. }
  252. // public:
  253. public function setSourceData($rawImageData, $sourceFilename='') {
  254. //$this->resetObject();
  255. //$this->sourceFilename = null;
  256. $this->rawImageData = $rawImageData;
  257. $this->DebugMessage('setSourceData() setting $this->rawImageData ('.strlen($this->rawImageData).' bytes; magic="'.substr($this->rawImageData, 0, 4).'" ('.phpthumb_functions::HexCharDisplay(substr($this->rawImageData, 0, 4)).'))', __FILE__, __LINE__);
  258. if ($this->config_cache_source_enabled) {
  259. $sourceFilename = ($sourceFilename ? $sourceFilename : md5($rawImageData));
  260. if (!is_dir($this->config_cache_source_directory)) {
  261. $this->ErrorImage('$this->config_cache_source_directory ('.$this->config_cache_source_directory.') is not a directory');
  262. } elseif (!@is_writable($this->config_cache_source_directory)) {
  263. $this->ErrorImage('$this->config_cache_source_directory ('.$this->config_cache_source_directory.') is not writable');
  264. }
  265. $this->DebugMessage('setSourceData() attempting to save source image to "'.$this->config_cache_source_directory.DIRECTORY_SEPARATOR.urlencode($sourceFilename).'"', __FILE__, __LINE__);
  266. if ($fp = @fopen($this->config_cache_source_directory.DIRECTORY_SEPARATOR.urlencode($sourceFilename), 'wb')) {
  267. fwrite($fp, $rawImageData);
  268. fclose($fp);
  269. } elseif (!$this->phpThumbDebug) {
  270. $this->ErrorImage('setSourceData() failed to write to source cache ('.$this->config_cache_source_directory.DIRECTORY_SEPARATOR.urlencode($sourceFilename).')');
  271. }
  272. }
  273. return true;
  274. }
  275. // public:
  276. public function setSourceImageResource($gdimg) {
  277. //$this->resetObject();
  278. $this->gdimg_source = $gdimg;
  279. return true;
  280. }
  281. // public:
  282. public function setParameter($param, $value) {
  283. if ($param == 'src') {
  284. $this->setSourceFilename($this->ResolveFilenameToAbsolute($value));
  285. } elseif (@is_array($this->$param)) {
  286. if (is_array($value)) {
  287. foreach ($value as $arraykey => $arrayvalue) {
  288. array_push($this->$param, $arrayvalue);
  289. }
  290. } else {
  291. array_push($this->$param, $value);
  292. }
  293. } else {
  294. $this->$param = $value;
  295. }
  296. return true;
  297. }
  298. // public:
  299. public function getParameter($param) {
  300. //if (property_exists('phpThumb', $param)) {
  301. return $this->$param;
  302. //}
  303. //$this->DebugMessage('setParameter() attempting to get non-existant parameter "'.$param.'"', __FILE__, __LINE__);
  304. //return false;
  305. }
  306. // public:
  307. public function GenerateThumbnail() {
  308. $this->setOutputFormat();
  309. $this->phpThumbDebug('8a');
  310. $this->ResolveSource();
  311. $this->phpThumbDebug('8b');
  312. $this->SetCacheFilename();
  313. $this->phpThumbDebug('8c');
  314. $this->ExtractEXIFgetImageSize();
  315. $this->phpThumbDebug('8d');
  316. if ($this->useRawIMoutput) {
  317. $this->DebugMessage('Skipping rest of GenerateThumbnail() because ($this->useRawIMoutput == true)', __FILE__, __LINE__);
  318. return true;
  319. }
  320. $this->phpThumbDebug('8e');
  321. if (!$this->SourceImageToGD()) {
  322. $this->DebugMessage('SourceImageToGD() failed', __FILE__, __LINE__);
  323. return false;
  324. }
  325. $this->phpThumbDebug('8f');
  326. $this->Rotate();
  327. $this->phpThumbDebug('8g');
  328. $this->CreateGDoutput();
  329. $this->phpThumbDebug('8h');
  330. // default values, also applicable for far="C"
  331. $destination_offset_x = round(($this->thumbnail_width - $this->thumbnail_image_width) / 2);
  332. $destination_offset_y = round(($this->thumbnail_height - $this->thumbnail_image_height) / 2);
  333. if (($this->far == 'L') || ($this->far == 'TL') || ($this->far == 'BL')) {
  334. $destination_offset_x = 0;
  335. }
  336. if (($this->far == 'R') || ($this->far == 'TR') || ($this->far == 'BR')) {
  337. $destination_offset_x = round($this->thumbnail_width - $this->thumbnail_image_width);
  338. }
  339. if (($this->far == 'T') || ($this->far == 'TL') || ($this->far == 'TR')) {
  340. $destination_offset_y = 0;
  341. }
  342. if (($this->far == 'B') || ($this->far == 'BL') || ($this->far == 'BR')) {
  343. $destination_offset_y = round($this->thumbnail_height - $this->thumbnail_image_height);
  344. }
  345. // // copy/resize image to appropriate dimensions
  346. // $borderThickness = 0;
  347. // if (!empty($this->fltr)) {
  348. // foreach ($this->fltr as $key => $value) {
  349. // if (preg_match('#^bord\|([0-9]+)#', $value, $matches)) {
  350. // $borderThickness = $matches[1];
  351. // break;
  352. // }
  353. // }
  354. // }
  355. // if ($borderThickness > 0) {
  356. // //$this->DebugMessage('Skipping ImageResizeFunction() because BorderThickness="'.$borderThickness.'"', __FILE__, __LINE__);
  357. // $this->thumbnail_image_height /= 2;
  358. // }
  359. $this->ImageResizeFunction(
  360. $this->gdimg_output,
  361. $this->gdimg_source,
  362. $destination_offset_x,
  363. $destination_offset_y,
  364. $this->thumbnailCropX,
  365. $this->thumbnailCropY,
  366. $this->thumbnail_image_width,
  367. $this->thumbnail_image_height,
  368. $this->thumbnailCropW,
  369. $this->thumbnailCropH
  370. );
  371. $this->DebugMessage('memory_get_usage() after copy-resize = '.(function_exists('memory_get_usage') ? @memory_get_usage() : 'n/a'), __FILE__, __LINE__);
  372. imagedestroy($this->gdimg_source);
  373. $this->DebugMessage('memory_get_usage() after imagedestroy = '.(function_exists('memory_get_usage') ? @memory_get_usage() : 'n/a'), __FILE__, __LINE__);
  374. $this->phpThumbDebug('8i');
  375. $this->AntiOffsiteLinking();
  376. $this->phpThumbDebug('8j');
  377. $this->ApplyFilters();
  378. $this->phpThumbDebug('8k');
  379. $this->AlphaChannelFlatten();
  380. $this->phpThumbDebug('8l');
  381. $this->MaxFileSize();
  382. $this->phpThumbDebug('8m');
  383. $this->DebugMessage('GenerateThumbnail() completed successfully', __FILE__, __LINE__);
  384. return true;
  385. }
  386. // public:
  387. public function RenderOutput() {
  388. if (!$this->useRawIMoutput && !is_resource($this->gdimg_output)) {
  389. $this->DebugMessage('RenderOutput() failed because !is_resource($this->gdimg_output)', __FILE__, __LINE__);
  390. return false;
  391. }
  392. if (!$this->thumbnailFormat) {
  393. $this->DebugMessage('RenderOutput() failed because $this->thumbnailFormat is empty', __FILE__, __LINE__);
  394. return false;
  395. }
  396. if ($this->useRawIMoutput) {
  397. $this->DebugMessage('RenderOutput copying $this->IMresizedData ('.strlen($this->IMresizedData).' bytes) to $this->outputImage', __FILE__, __LINE__);
  398. $this->outputImageData = $this->IMresizedData;
  399. return true;
  400. }
  401. $builtin_formats = array();
  402. if (function_exists('imagetypes')) {
  403. $imagetypes = imagetypes();
  404. $builtin_formats['wbmp'] = (bool) ($imagetypes & IMG_WBMP);
  405. $builtin_formats['jpg'] = (bool) ($imagetypes & IMG_JPG);
  406. $builtin_formats['gif'] = (bool) ($imagetypes & IMG_GIF);
  407. $builtin_formats['png'] = (bool) ($imagetypes & IMG_PNG);
  408. if (defined('IMG_WEBP')) {
  409. $builtin_formats['webp'] = (bool) ($imagetypes & IMG_WEBP); // PHP 5.6.25, 7.0.10
  410. }
  411. if (defined('IMG_BMP')) {
  412. $builtin_formats['bmp'] = (bool) ($imagetypes & IMG_BMP); // PHP 7.2.0
  413. }
  414. }
  415. $this->DebugMessage('imageinterlace($this->gdimg_output, '. (int) $this->config_output_interlace .')', __FILE__, __LINE__);
  416. imageinterlace($this->gdimg_output, (int) $this->config_output_interlace);
  417. $this->DebugMessage('RenderOutput() attempting image'.strtolower(@$this->thumbnailFormat).'($this->gdimg_output)', __FILE__, __LINE__);
  418. ob_start();
  419. switch ($this->thumbnailFormat) {
  420. case 'wbmp':
  421. if (empty($builtin_formats['wbmp'])) {
  422. $this->DebugMessage('GD does not have required built-in support for WBMP output', __FILE__, __LINE__);
  423. ob_end_clean();
  424. return false;
  425. }
  426. imagewbmp($this->gdimg_output, null, $this->thumbnailQuality);
  427. $this->outputImageData = ob_get_contents();
  428. break;
  429. case 'jpeg':
  430. case 'jpg': // should be "jpeg" not "jpg" but just in case...
  431. if (empty($builtin_formats['jpg'])) {
  432. $this->DebugMessage('GD does not have required built-in support for JPEG output', __FILE__, __LINE__);
  433. ob_end_clean();
  434. return false;
  435. }
  436. imagejpeg($this->gdimg_output, null, $this->thumbnailQuality);
  437. $this->outputImageData = ob_get_contents();
  438. break;
  439. case 'png':
  440. if (empty($builtin_formats['png'])) {
  441. $this->DebugMessage('GD does not have required built-in support for PNG output', __FILE__, __LINE__);
  442. ob_end_clean();
  443. return false;
  444. }
  445. if (phpthumb_functions::version_compare_replacement(PHP_VERSION, '5.1.2', '>=')) {
  446. // https://github.com/JamesHeinrich/phpThumb/issues/24
  447. /* http://php.net/manual/en/function.imagepng.php:
  448. from php source (gd.h):
  449. 2.0.12: Compression level: 0-9 or -1, where 0 is NO COMPRESSION at all,
  450. :: 1 is FASTEST but produces larger files, 9 provides the best
  451. :: compression (smallest files) but takes a long time to compress, and
  452. :: -1 selects the default compiled into the zlib library.
  453. Conclusion: Based on the Zlib manual (http://www.zlib.net/manual.html) the default compression level is set to 6.
  454. */
  455. if (($this->thumbnailQuality >= -1) && ($this->thumbnailQuality <= 9)) {
  456. $PNGquality = $this->thumbnailQuality;
  457. } else {
  458. $this->DebugMessage('Specified thumbnailQuality "'.$this->thumbnailQuality.'" is outside the accepted range (0-9, or -1). Using 6 as default value.', __FILE__, __LINE__);
  459. $PNGquality = 6;
  460. }
  461. imagepng($this->gdimg_output, null, $PNGquality);
  462. } else {
  463. imagepng($this->gdimg_output);
  464. }
  465. $this->outputImageData = ob_get_contents();
  466. break;
  467. case 'gif':
  468. if (empty($builtin_formats['gif'])) {
  469. $this->DebugMessage('GD does not have required built-in support for GIF output', __FILE__, __LINE__);
  470. ob_end_clean();
  471. return false;
  472. }
  473. imagegif($this->gdimg_output);
  474. $this->outputImageData = ob_get_contents();
  475. break;
  476. case 'webp':
  477. if (empty($builtin_formats['webp'])) {
  478. $this->DebugMessage('GD does not have required built-in support for WebP output', __FILE__, __LINE__);
  479. ob_end_clean();
  480. return false;
  481. }
  482. imagewebp($this->gdimg_output);
  483. $this->outputImageData = ob_get_contents();
  484. break;
  485. case 'bmp':
  486. if (!empty($builtin_formats['bmp'])) {
  487. imagebmp($this->gdimg_output);
  488. $this->outputImageData = ob_get_contents();
  489. break;
  490. }
  491. $this->DebugMessage('GD does not have required built-in support for BMP output', __FILE__, __LINE__);
  492. if (!@include_once __DIR__ .'/phpthumb.bmp.php' ) {
  493. $this->DebugMessage('Error including "'. __DIR__ .'/phpthumb.bmp.php" which is required for BMP format output', __FILE__, __LINE__);
  494. ob_end_clean();
  495. return false;
  496. }
  497. $phpthumb_bmp = new phpthumb_bmp();
  498. $this->outputImageData = $phpthumb_bmp->GD2BMPstring($this->gdimg_output);
  499. unset($phpthumb_bmp);
  500. break;
  501. case 'ico':
  502. if (!@include_once __DIR__ .'/phpthumb.ico.php' ) {
  503. $this->DebugMessage('Error including "'. __DIR__ .'/phpthumb.ico.php" which is required for ICO format output', __FILE__, __LINE__);
  504. ob_end_clean();
  505. return false;
  506. }
  507. $phpthumb_ico = new phpthumb_ico();
  508. $arrayOfOutputImages = array($this->gdimg_output);
  509. $this->outputImageData = $phpthumb_ico->GD2ICOstring($arrayOfOutputImages);
  510. unset($phpthumb_ico);
  511. break;
  512. default:
  513. $this->DebugMessage('RenderOutput failed because $this->thumbnailFormat "'.$this->thumbnailFormat.'" is not valid', __FILE__, __LINE__);
  514. ob_end_clean();
  515. return false;
  516. }
  517. ob_end_clean();
  518. if (!$this->outputImageData) {
  519. $this->DebugMessage('RenderOutput() for "'.$this->thumbnailFormat.'" failed', __FILE__, __LINE__);
  520. ob_end_clean();
  521. return false;
  522. }
  523. $this->DebugMessage('RenderOutput() completing with $this->outputImageData = '.strlen($this->outputImageData).' bytes', __FILE__, __LINE__);
  524. return true;
  525. }
  526. // public:
  527. public function RenderToFile($filename) {
  528. if (preg_match('#^[a-z0-9]+://#i', $filename)) {
  529. $this->DebugMessage('RenderToFile() failed because $filename ('.$filename.') is a URL', __FILE__, __LINE__);
  530. return false;
  531. }
  532. // render thumbnail to this file only, do not cache, do not output to browser
  533. //$renderfilename = $this->ResolveFilenameToAbsolute(dirname($filename)).DIRECTORY_SEPARATOR.basename($filename);
  534. $renderfilename = $filename;
  535. if (($filename[0] != '/') && ($filename[0] != '\\') && ($filename[1] != ':')) {
  536. $renderfilename = $this->ResolveFilenameToAbsolute($renderfilename);
  537. }
  538. if (!@is_writable(dirname($renderfilename))) {
  539. $this->DebugMessage('RenderToFile() failed because "'.dirname($renderfilename).'/" is not writable', __FILE__, __LINE__);
  540. return false;
  541. }
  542. if (@is_file($renderfilename) && !@is_writable($renderfilename)) {
  543. $this->DebugMessage('RenderToFile() failed because "'.$renderfilename.'" is not writable', __FILE__, __LINE__);
  544. return false;
  545. }
  546. if ($this->RenderOutput()) {
  547. if (file_put_contents($renderfilename, $this->outputImageData)) {
  548. @chmod($renderfilename, $this->getParameter('config_file_create_mask'));
  549. $this->DebugMessage('RenderToFile('.$renderfilename.') succeeded', __FILE__, __LINE__);
  550. return true;
  551. }
  552. if (!@file_exists($renderfilename)) {
  553. $this->DebugMessage('RenderOutput ['.$this->thumbnailFormat.'('.$renderfilename.')] did not appear to fail, but the output image does not exist either...', __FILE__, __LINE__);
  554. }
  555. } else {
  556. $this->DebugMessage('RenderOutput ['.$this->thumbnailFormat.'('.$renderfilename.')] failed', __FILE__, __LINE__);
  557. }
  558. return false;
  559. }
  560. // public:
  561. public function OutputThumbnail() {
  562. $this->purgeTempFiles();
  563. if (!$this->useRawIMoutput && !is_resource($this->gdimg_output)) {
  564. $this->DebugMessage('OutputThumbnail() failed because !is_resource($this->gdimg_output)', __FILE__, __LINE__);
  565. return false;
  566. }
  567. if (headers_sent()) {
  568. return $this->ErrorImage('OutputThumbnail() failed - headers already sent');
  569. }
  570. $downloadfilename = phpthumb_functions::SanitizeFilename(is_string($this->sia) ? $this->sia : ($this->down ? $this->down : 'phpThumb_generated_thumbnail'.'.'.$this->thumbnailFormat));
  571. $this->DebugMessage('Content-Disposition header filename set to "'.$downloadfilename.'"', __FILE__, __LINE__);
  572. if ($downloadfilename) {
  573. header('Content-Disposition: '.($this->down ? 'attachment' : 'inline').'; filename="'.$downloadfilename.'"');
  574. } else {
  575. $this->DebugMessage('failed to send Content-Disposition header because $downloadfilename is empty', __FILE__, __LINE__);
  576. }
  577. if ($this->useRawIMoutput) {
  578. header('Content-Type: '.phpthumb_functions::ImageTypeToMIMEtype($this->thumbnailFormat));
  579. echo $this->IMresizedData;
  580. } else {
  581. $this->DebugMessage('imageinterlace($this->gdimg_output, '. (int) $this->config_output_interlace .')', __FILE__, __LINE__);
  582. imageinterlace($this->gdimg_output, (int) $this->config_output_interlace);
  583. switch ($this->thumbnailFormat) {
  584. case 'jpeg':
  585. header('Content-Type: '.phpthumb_functions::ImageTypeToMIMEtype($this->thumbnailFormat));
  586. $ImageOutFunction = 'image'.$this->thumbnailFormat;
  587. @$ImageOutFunction($this->gdimg_output, null, $this->thumbnailQuality);
  588. break;
  589. case 'png':
  590. case 'gif':
  591. case 'webp':
  592. $ImageOutFunction = 'image'.$this->thumbnailFormat;
  593. if (!function_exists($ImageOutFunction)) {
  594. $this->DebugMessage($ImageOutFunction.' is not available', __FILE__, __LINE__);
  595. return false;
  596. }
  597. header('Content-Type: '.phpthumb_functions::ImageTypeToMIMEtype($this->thumbnailFormat));
  598. @$ImageOutFunction($this->gdimg_output);
  599. break;
  600. case 'bmp':
  601. if (function_exists('imagebmp')) {
  602. header('Content-Type: '.phpthumb_functions::ImageTypeToMIMEtype($this->thumbnailFormat));
  603. imagebmp($this->gdimg_output);
  604. break;
  605. }
  606. if (!@include_once __DIR__ .'/phpthumb.bmp.php' ) {
  607. $this->DebugMessage('Error including "'. __DIR__ .'/phpthumb.bmp.php" which is required for BMP format output', __FILE__, __LINE__);
  608. return false;
  609. }
  610. $phpthumb_bmp = new phpthumb_bmp();
  611. if (is_object($phpthumb_bmp)) {
  612. $bmp_data = $phpthumb_bmp->GD2BMPstring($this->gdimg_output);
  613. unset($phpthumb_bmp);
  614. if (!$bmp_data) {
  615. $this->DebugMessage('$phpthumb_bmp->GD2BMPstring() failed', __FILE__, __LINE__);
  616. return false;
  617. }
  618. header('Content-Type: '.phpthumb_functions::ImageTypeToMIMEtype($this->thumbnailFormat));
  619. echo $bmp_data;
  620. } else {
  621. $this->DebugMessage('new phpthumb_bmp() failed', __FILE__, __LINE__);
  622. return false;
  623. }
  624. break;
  625. case 'ico':
  626. if (!@include_once __DIR__ .'/phpthumb.ico.php' ) {
  627. $this->DebugMessage('Error including "'. __DIR__ .'/phpthumb.ico.php" which is required for ICO format output', __FILE__, __LINE__);
  628. return false;
  629. }
  630. $phpthumb_ico = new phpthumb_ico();
  631. if (is_object($phpthumb_ico)) {
  632. $arrayOfOutputImages = array($this->gdimg_output);
  633. $ico_data = $phpthumb_ico->GD2ICOstring($arrayOfOutputImages);
  634. unset($phpthumb_ico);
  635. if (!$ico_data) {
  636. $this->DebugMessage('$phpthumb_ico->GD2ICOstring() failed', __FILE__, __LINE__);
  637. return false;
  638. }
  639. header('Content-Type: '.phpthumb_functions::ImageTypeToMIMEtype($this->thumbnailFormat));
  640. echo $ico_data;
  641. } else {
  642. $this->DebugMessage('new phpthumb_ico() failed', __FILE__, __LINE__);
  643. return false;
  644. }
  645. break;
  646. default:
  647. $this->DebugMessage('OutputThumbnail failed because $this->thumbnailFormat "'.$this->thumbnailFormat.'" is not valid', __FILE__, __LINE__);
  648. return false;
  649. break;
  650. }
  651. }
  652. return true;
  653. }
  654. // public:
  655. public function CleanUpCacheDirectory() {
  656. $this->DebugMessage('CleanUpCacheDirectory() set to purge ('.(null === $this->config_cache_maxage ? 'NULL' : number_format($this->config_cache_maxage / 86400, 1)).' days; '.(null === $this->config_cache_maxsize ? 'NULL' : number_format($this->config_cache_maxsize / 1048576, 2)).' MB; '.(null === $this->config_cache_maxfiles ? 'NULL' : number_format($this->config_cache_maxfiles)).' files)', __FILE__, __LINE__);
  657. if (!is_writable($this->config_cache_directory)) {
  658. $this->DebugMessage('CleanUpCacheDirectory() skipped because "'.$this->config_cache_directory.'" is not writable', __FILE__, __LINE__);
  659. return true;
  660. }
  661. // cache status of cache directory for 1 hour to avoid hammering the filesystem functions
  662. $phpThumbCacheStats_filename = $this->config_cache_directory.DIRECTORY_SEPARATOR.'phpThumbCacheStats.txt';
  663. if (file_exists($phpThumbCacheStats_filename) && is_readable($phpThumbCacheStats_filename) && (filemtime($phpThumbCacheStats_filename) >= (time() - 3600))) {
  664. $this->DebugMessage('CleanUpCacheDirectory() skipped because "'.$phpThumbCacheStats_filename.'" is recently modified', __FILE__, __LINE__);
  665. return true;
  666. }
  667. if (!@touch($phpThumbCacheStats_filename)) {
  668. $this->DebugMessage('touch('.$phpThumbCacheStats_filename.') failed', __FILE__, __LINE__);
  669. }
  670. $DeletedKeys = array();
  671. $AllFilesInCacheDirectory = array();
  672. if (($this->config_cache_maxage > 0) || ($this->config_cache_maxsize > 0) || ($this->config_cache_maxfiles > 0)) {
  673. $CacheDirOldFilesAge = array();
  674. $CacheDirOldFilesSize = array();
  675. $AllFilesInCacheDirectory = phpthumb_functions::GetAllFilesInSubfolders($this->config_cache_directory);
  676. foreach ($AllFilesInCacheDirectory as $fullfilename) {
  677. if (preg_match('#'.preg_quote($this->config_cache_prefix).'#i', $fullfilename) && file_exists($fullfilename)) {
  678. $CacheDirOldFilesAge[$fullfilename] = @fileatime($fullfilename);
  679. if ($CacheDirOldFilesAge[$fullfilename] == 0) {
  680. $CacheDirOldFilesAge[$fullfilename] = @filemtime($fullfilename);
  681. }
  682. $CacheDirOldFilesSize[$fullfilename] = @filesize($fullfilename);
  683. }
  684. }
  685. if (empty($CacheDirOldFilesSize)) {
  686. $this->DebugMessage('CleanUpCacheDirectory() skipped because $CacheDirOldFilesSize is empty (phpthumb_functions::GetAllFilesInSubfolders('.$this->config_cache_directory.') found no files)', __FILE__, __LINE__);
  687. return true;
  688. }
  689. $DeletedKeys['zerobyte'] = array();
  690. foreach ($CacheDirOldFilesSize as $fullfilename => $filesize) {
  691. // purge all zero-size files more than an hour old (to prevent trying to delete just-created and/or in-use files)
  692. $cutofftime = time() - 3600;
  693. if (($filesize == 0) && ($CacheDirOldFilesAge[$fullfilename] < $cutofftime)) {
  694. $this->DebugMessage('deleting "'.$fullfilename.'"', __FILE__, __LINE__);
  695. if (@unlink($fullfilename)) {
  696. $DeletedKeys['zerobyte'][] = $fullfilename;
  697. unset($CacheDirOldFilesSize[$fullfilename]);
  698. unset($CacheDirOldFilesAge[$fullfilename]);
  699. }
  700. }
  701. }
  702. $this->DebugMessage('CleanUpCacheDirectory() purged '.count($DeletedKeys['zerobyte']).' zero-byte files', __FILE__, __LINE__);
  703. asort($CacheDirOldFilesAge);
  704. if ($this->config_cache_maxfiles > 0) {
  705. $TotalCachedFiles = count($CacheDirOldFilesAge);
  706. $DeletedKeys['maxfiles'] = array();
  707. foreach ($CacheDirOldFilesAge as $fullfilename => $filedate) {
  708. if ($TotalCachedFiles > $this->config_cache_maxfiles) {
  709. $this->DebugMessage('deleting "'.$fullfilename.'"', __FILE__, __LINE__);
  710. if (@unlink($fullfilename)) {
  711. $TotalCachedFiles--;
  712. $DeletedKeys['maxfiles'][] = $fullfilename;
  713. }
  714. } else {
  715. // there are few enough files to keep the rest
  716. break;
  717. }
  718. }
  719. $this->DebugMessage('CleanUpCacheDirectory() purged '.count($DeletedKeys['maxfiles']).' files based on (config_cache_maxfiles='.$this->config_cache_maxfiles.')', __FILE__, __LINE__);
  720. foreach ($DeletedKeys['maxfiles'] as $fullfilename) {
  721. unset($CacheDirOldFilesAge[$fullfilename]);
  722. unset($CacheDirOldFilesSize[$fullfilename]);
  723. }
  724. }
  725. if ($this->config_cache_maxage > 0) {
  726. $mindate = time() - $this->config_cache_maxage;
  727. $DeletedKeys['maxage'] = array();
  728. foreach ($CacheDirOldFilesAge as $fullfilename => $filedate) {
  729. if ($filedate > 0) {
  730. if ($filedate < $mindate) {
  731. $this->DebugMessage('deleting "'.$fullfilename.'"', __FILE__, __LINE__);
  732. if (@unlink($fullfilename)) {
  733. $DeletedKeys['maxage'][] = $fullfilename;
  734. }
  735. } else {
  736. // the rest of the files are new enough to keep
  737. break;
  738. }
  739. }
  740. }
  741. $this->DebugMessage('CleanUpCacheDirectory() purged '.count($DeletedKeys['maxage']).' files based on (config_cache_maxage='.$this->config_cache_maxage.')', __FILE__, __LINE__);
  742. foreach ($DeletedKeys['maxage'] as $fullfilename) {
  743. unset($CacheDirOldFilesAge[$fullfilename]);
  744. unset($CacheDirOldFilesSize[$fullfilename]);
  745. }
  746. }
  747. if ($this->config_cache_maxsize > 0) {
  748. $TotalCachedFileSize = array_sum($CacheDirOldFilesSize);
  749. $DeletedKeys['maxsize'] = array();
  750. foreach ($CacheDirOldFilesAge as $fullfilename => $filedate) {
  751. if ($TotalCachedFileSize > $this->config_cache_maxsize) {
  752. $this->DebugMessage('deleting "'.$fullfilename.'"', __FILE__, __LINE__);
  753. if (@unlink($fullfilename)) {
  754. $TotalCachedFileSize -= $CacheDirOldFilesSize[$fullfilename];
  755. $DeletedKeys['maxsize'][] = $fullfilename;
  756. }
  757. } else {
  758. // the total filesizes are small enough to keep the rest of the files
  759. break;
  760. }
  761. }
  762. $this->DebugMessage('CleanUpCacheDirectory() purged '.count($DeletedKeys['maxsize']).' files based on (config_cache_maxsize='.$this->config_cache_maxsize.')', __FILE__, __LINE__);
  763. foreach ($DeletedKeys['maxsize'] as $fullfilename) {
  764. unset($CacheDirOldFilesAge[$fullfilename]);
  765. unset($CacheDirOldFilesSize[$fullfilename]);
  766. }
  767. }
  768. } else {
  769. $this->DebugMessage('skipping CleanUpCacheDirectory() because config set to not use it', __FILE__, __LINE__);
  770. }
  771. $totalpurged = 0;
  772. foreach ($DeletedKeys as $key => $value) {
  773. $totalpurged += count($value);
  774. }
  775. $this->DebugMessage('CleanUpCacheDirectory() purged '.$totalpurged.' files (from '.count($AllFilesInCacheDirectory).') based on config settings', __FILE__, __LINE__);
  776. if ($totalpurged > 0) {
  777. $empty_dirs = array();
  778. foreach ($AllFilesInCacheDirectory as $fullfilename) {
  779. if (is_dir($fullfilename)) {
  780. $empty_dirs[$this->realPathSafe($fullfilename)] = 1;
  781. } else {
  782. unset($empty_dirs[$this->realPathSafe(dirname($fullfilename))]);
  783. }
  784. }
  785. krsort($empty_dirs);
  786. $totalpurgeddirs = 0;
  787. foreach ($empty_dirs as $empty_dir => $dummy) {
  788. if ($empty_dir == $this->config_cache_directory) {
  789. // shouldn't happen, but just in case, don't let it delete actual cache directory
  790. continue;
  791. } elseif (@rmdir($empty_dir)) {
  792. $totalpurgeddirs++;
  793. } else {
  794. $this->DebugMessage('failed to rmdir('.$empty_dir.')', __FILE__, __LINE__);
  795. }
  796. }
  797. $this->DebugMessage('purged '.$totalpurgeddirs.' empty directories', __FILE__, __LINE__);
  798. }
  799. return true;
  800. }
  801. //////////////////////////////////////////////////////////////////////
  802. // private: re-initializator (call between rendering multiple images with one object)
  803. public function resetObject() {
  804. $class_vars = get_class_vars(get_class($this));
  805. foreach ($class_vars as $key => $value) {
  806. // do not clobber debug or config info
  807. if (!preg_match('#^(config_|debug|fatalerror)#i', $key)) {
  808. $this->$key = $value;
  809. }
  810. }
  811. $this->phpThumb(); // re-initialize some class variables
  812. return true;
  813. }
  814. //////////////////////////////////////////////////////////////////////
  815. public function ResolveSource() {
  816. if (is_resource($this->gdimg_source)) {
  817. $this->DebugMessage('ResolveSource() exiting because is_resource($this->gdimg_source)', __FILE__, __LINE__);
  818. return true;
  819. }
  820. if ($this->rawImageData) {
  821. $this->sourceFilename = null;
  822. $this->DebugMessage('ResolveSource() exiting because $this->rawImageData is set ('.number_format(strlen($this->rawImageData)).' bytes)', __FILE__, __LINE__);
  823. return true;
  824. }
  825. if ($this->sourceFilename) {
  826. $this->sourceFilename = $this->ResolveFilenameToAbsolute($this->sourceFilename);
  827. $this->DebugMessage('$this->sourceFilename set to "'.$this->sourceFilename.'"', __FILE__, __LINE__);
  828. } elseif ($this->src) {
  829. $this->sourceFilename = $this->ResolveFilenameToAbsolute($this->src);
  830. $this->DebugMessage('$this->sourceFilename set to "'.$this->sourceFilename.'" from $this->src ('.$this->src.')', __FILE__, __LINE__);
  831. } else {
  832. return $this->ErrorImage('$this->sourceFilename and $this->src are both empty');
  833. }
  834. if ($this->iswindows && ((substr($this->sourceFilename, 0, 2) == '//') || (substr($this->sourceFilename, 0, 2) == '\\\\'))) {
  835. // Windows \\share\filename.ext
  836. } elseif (preg_match('#^[a-z0-9]+://#i', $this->sourceFilename, $protocol_matches)) {
  837. if (preg_match('#^(f|ht)tps?\://#i', $this->sourceFilename)) {
  838. // URL
  839. if ($this->config_http_user_agent) {
  840. ini_set('user_agent', $this->config_http_user_agent);
  841. }
  842. } else {
  843. return $this->ErrorImage('only FTP and HTTP/HTTPS protocols are allowed, "'.$protocol_matches[1].'" is not');
  844. }
  845. } elseif (!@file_exists($this->sourceFilename)) {
  846. return $this->ErrorImage('"'.$this->sourceFilename.'" does not exist');
  847. } elseif (!@is_file($this->sourceFilename)) {
  848. return $this->ErrorImage('"'.$this->sourceFilename.'" is not a file');
  849. }
  850. return true;
  851. }
  852. public function setOutputFormat() {
  853. static $alreadyCalled = false;
  854. if ($this->thumbnailFormat && $alreadyCalled) {
  855. return true;
  856. }
  857. $alreadyCalled = true;
  858. $AvailableImageOutputFormats = array();
  859. $AvailableImageOutputFormats[] = 'text';
  860. if (@is_readable( __DIR__ .'/phpthumb.ico.php')) {
  861. $AvailableImageOutputFormats[] = 'ico';
  862. }
  863. if (@is_readable( __DIR__ .'/phpthumb.bmp.php')) {
  864. $AvailableImageOutputFormats[] = 'bmp';
  865. }
  866. $this->thumbnailFormat = 'ico';
  867. // Set default output format based on what image types are available
  868. if (function_exists('imagetypes')) {
  869. $imagetypes = imagetypes();
  870. if ($imagetypes & IMG_WBMP) {
  871. $this->thumbnailFormat = 'wbmp';
  872. $AvailableImageOutputFormats[] = 'wbmp';
  873. }
  874. if ($imagetypes & IMG_GIF) {
  875. $this->thumbnailFormat = 'gif';
  876. $AvailableImageOutputFormats[] = 'gif';
  877. }
  878. if ($imagetypes & IMG_WEBP) {
  879. $this->thumbnailFormat = 'webp';
  880. $AvailableImageOutputFormats[] = 'webp';
  881. }
  882. if ($imagetypes & IMG_PNG) {
  883. $this->thumbnailFormat = 'png';
  884. $AvailableImageOutputFormats[] = 'png';
  885. }
  886. if ($imagetypes & IMG_JPG) {
  887. $this->thumbnailFormat = 'jpeg';
  888. $AvailableImageOutputFormats[] = 'jpeg';
  889. }
  890. } else {
  891. $this->DebugMessage('imagetypes() does not exist - GD support might not be enabled?', __FILE__, __LINE__);
  892. }
  893. if ($this->ImageMagickVersion()) {
  894. $IMformats = array('jpeg', 'png', 'gif', 'bmp', 'ico', 'wbmp', 'webp');
  895. $this->DebugMessage('Addding ImageMagick formats to $AvailableImageOutputFormats ('.implode(';', $AvailableImageOutputFormats).')', __FILE__, __LINE__);
  896. foreach ($IMformats as $key => $format) {
  897. $AvailableImageOutputFormats[] = $format;
  898. }
  899. }
  900. $AvailableImageOutputFormats = array_unique($AvailableImageOutputFormats);
  901. $this->DebugMessage('$AvailableImageOutputFormats = array('.implode(';', $AvailableImageOutputFormats).')', __FILE__, __LINE__);
  902. $this->f = preg_replace('#[^a-z]#', '', strtolower($this->f));
  903. if (strtolower($this->config_output_format) == 'jpg') {
  904. $this->config_output_format = 'jpeg';
  905. }
  906. if (strtolower($this->f) == 'jpg') {
  907. $this->f = 'jpeg';
  908. }
  909. if (phpthumb_functions::CaseInsensitiveInArray($this->config_output_format, $AvailableImageOutputFormats)) {
  910. // set output format to config default if that format is available
  911. $this->DebugMessage('$this->thumbnailFormat set to $this->config_output_format "'.strtolower($this->config_output_format).'"', __FILE__, __LINE__);
  912. $this->thumbnailFormat = strtolower($this->config_output_format);
  913. } elseif ($this->config_output_format) {
  914. $this->DebugMessage('$this->thumbnailFormat staying as "'.$this->thumbnailFormat.'" because $this->config_output_format ('.strtolower($this->config_output_format).') is not in $AvailableImageOutputFormats', __FILE__, __LINE__);
  915. }
  916. if ($this->f && phpthumb_functions::CaseInsensitiveInArray($this->f, $AvailableImageOutputFormats) ) {
  917. // override output format if $this->f is set and that format is available
  918. $this->DebugMessage('$this->thumbnailFormat set to $this->f "'.strtolower($this->f).'"', __FILE__, __LINE__);
  919. $this->thumbnailFormat = strtolower($this->f);
  920. } elseif ($this->f) {
  921. $this->DebugMessage('$this->thumbnailFormat staying as "'.$this->thumbnailFormat.'" because $this->f ('.strtolower($this->f).') is not in $AvailableImageOutputFormats', __FILE__, __LINE__);
  922. }
  923. // for JPEG images, quality 1 (worst) to 99 (best)
  924. // quality < 25 is nasty, with not much size savings - not recommended
  925. // problems with 100 - invalid JPEG?
  926. $this->thumbnailQuality = max(1, min(99, ($this->q ? (int) $this->q : 75)));
  927. $this->DebugMessage('$this->thumbnailQuality set to "'.$this->thumbnailQuality.'"', __FILE__, __LINE__);
  928. return true;
  929. }
  930. public function setCacheDirectory() {
  931. // resolve cache directory to absolute pathname
  932. $this->DebugMessage('setCacheDirectory() starting with config_cache_directory = "'.$this->config_cache_directory.'"', __FILE__, __LINE__);
  933. if ($this->config_cache_directory[ 0 ] == '.') {
  934. if (preg_match('#^(f|ht)tps?\://#i', $this->src)) {
  935. if (!$this->config_cache_disable_warning) {
  936. $this->ErrorImage('$this->config_cache_directory ('.$this->config_cache_directory.') cannot be used for remote images. Adjust "cache_directory" or "cache_disable_warning" in phpThumb.config.php');
  937. }
  938. } elseif ($this->src) {
  939. // resolve relative cache directory to source image
  940. $this->config_cache_directory = dirname($this->ResolveFilenameToAbsolute($this->src)).DIRECTORY_SEPARATOR.$this->config_cache_directory;
  941. } else {
  942. // $this->new is probably set
  943. }
  944. }
  945. if (substr($this->config_cache_directory, -1) == '/') {
  946. $this->config_cache_directory = substr($this->config_cache_directory, 0, -1);
  947. }
  948. if ($this->iswindows) {
  949. $this->config_cache_directory = str_replace('/', DIRECTORY_SEPARATOR, $this->config_cache_directory);
  950. }
  951. if ($this->config_cache_directory) {
  952. $real_cache_path = $this->realPathSafe($this->config_cache_directory);
  953. if (!$real_cache_path) {
  954. $this->DebugMessage('$this->realPathSafe($this->config_cache_directory) failed for "'.$this->config_cache_directory.'"', __FILE__, __LINE__);
  955. if (!is_dir($this->config_cache_directory)) {
  956. $this->DebugMessage('!is_dir('.$this->config_cache_directory.')', __FILE__, __LINE__);
  957. }
  958. }
  959. if ($real_cache_path) {
  960. $this->DebugMessage('setting config_cache_directory to $this->realPathSafe('.$this->config_cache_directory.') = "'.$real_cache_path.'"', __FILE__, __LINE__);
  961. $this->config_cache_directory = $real_cache_path;
  962. }
  963. }
  964. if (!is_dir($this->config_cache_directory)) {
  965. if (!$this->config_cache_disable_warning) {
  966. $this->ErrorImage('$this->config_cache_directory ('.$this->config_cache_directory.') does not exist. Adjust "cache_directory" or "cache_disable_warning" in phpThumb.config.php');
  967. }
  968. $this->DebugMessage('$this->config_cache_directory ('.$this->config_cache_directory.') is not a directory', __FILE__, __LINE__);
  969. $this->config_cache_directory = null;
  970. } elseif (!@is_writable($this->config_cache_directory)) {
  971. $this->DebugMessage('$this->config_cache_directory is not writable ('.$this->config_cache_directory.')', __FILE__, __LINE__);
  972. }
  973. $this->InitializeTempDirSetting();
  974. if (!@is_dir($this->config_temp_directory) && !@is_writable($this->config_temp_directory) && @is_dir($this->config_cache_directory) && @is_writable($this->config_cache_directory)) {
  975. $this->DebugMessage('setting $this->config_temp_directory = $this->config_cache_directory ('.$this->config_cache_directory.')', __FILE__, __LINE__);
  976. $this->config_temp_directory = $this->config_cache_directory;
  977. }
  978. return true;
  979. }
  980. /* Takes the array of path segments up to now, and the next segment (maybe a modifier: empty, . or ..)
  981. Applies it, adding or removing from $segments as a result. Returns nothing. */
  982. // http://support.silisoftware.com/phpBB3/viewtopic.php?t=961
  983. public function applyPathSegment(&$segments, $segment) {
  984. if ($segment == '.') {
  985. return; // always remove
  986. }
  987. if ($segment == '') {
  988. $test = array_pop($segments);
  989. if (null === $test) {
  990. $segments[] = $segment; // keep the first empty block
  991. } elseif ($test == '') {
  992. $test = array_pop($segments);
  993. if (null === $test) {
  994. $segments[] = $test;
  995. $segments[] = $segment; // keep the second one too
  996. } else { // put both back and ignore segment
  997. $segments[] = $test;
  998. $segments[] = $test;
  999. }
  1000. } else {
  1001. $segments[] = $test; // ignore empty blocks
  1002. }
  1003. } else {
  1004. if ($segment == '..') {
  1005. $test = array_pop($segments);
  1006. if (null === $test) {
  1007. $segments[] = $segment;
  1008. } elseif ($test == '..') {
  1009. $segments[] = $test;
  1010. $segments[] = $segment;
  1011. } else {
  1012. if ($test == '') {
  1013. $segments[] = $test;
  1014. } // else nothing, remove both
  1015. }
  1016. } else {
  1017. $segments[] = $segment;
  1018. }
  1019. }
  1020. }
  1021. /* Takes array of path components, normalizes it: removes empty slots and '.', collapses '..' and folder names. Returns array. */
  1022. // http://support.silisoftware.com/phpBB3/viewtopic.php?t=961
  1023. public function normalizePath($segments) {
  1024. $parts = array();
  1025. foreach ($segments as $segment) {
  1026. $this->applyPathSegment($parts, $segment);
  1027. }
  1028. return $parts;
  1029. }
  1030. /* True if the provided path points (without resolving symbolic links) into one of the allowed directories. */
  1031. // http://support.silisoftware.com/phpBB3/viewtopic.php?t=961
  1032. public function matchPath($path, $allowed_dirs) {
  1033. if (!empty($allowed_dirs)) {
  1034. foreach ($allowed_dirs as $one_dir) {
  1035. if (preg_match('#^'.preg_quote(str_replace(DIRECTORY_SEPARATOR, '/', $this->realPathSafe($one_dir))).'#', $path)) {
  1036. return true;
  1037. }
  1038. }
  1039. }
  1040. return false;
  1041. }
  1042. /* True if the provided path points inside one of open_basedirs (or if open_basedirs are disabled) */
  1043. // http://support.silisoftware.com/phpBB3/viewtopic.php?t=961
  1044. public function isInOpenBasedir($path) {
  1045. static $open_basedirs = null;
  1046. if (null === $open_basedirs) {
  1047. $ini_text = ini_get('open_basedir');
  1048. $this->DebugMessage('open_basedir: "'.$ini_text.'"', __FILE__, __LINE__);
  1049. $open_basedirs = array();
  1050. if (strlen($ini_text) > 0) {
  1051. foreach (preg_split('#[;:]#', $ini_text) as $key => $value) {
  1052. $open_basedirs[$key] = $this->realPathSafe($value);
  1053. }
  1054. }
  1055. }
  1056. return (empty($open_basedirs) || $this->matchPath($path, $open_basedirs));
  1057. }
  1058. /* Resolves all symlinks in $path, checking that each continuous part ends in an allowed zone. Returns null, if any component leads outside of allowed zone. */
  1059. // http://support.silisoftware.com/phpBB3/viewtopic.php?t=961
  1060. public function resolvePath($path, $allowed_dirs) {
  1061. $this->DebugMessage('resolvePath: '.$path.' (allowed_dirs: '.print_r($allowed_dirs, true).')', __FILE__, __LINE__);
  1062. // add base path to the top of the list
  1063. if (!$this->config_allow_src_above_docroot) {
  1064. array_unshift($allowed_dirs, $this->realPathSafe($this->config_document_root));

Large files files are truncated, but you can click here to view the full file