PageRenderTime 51ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 1ms

/ php-ppcms/lib/phpThumb_1.7.3/phpThumb.php

http://php-ppcms.googlecode.com/
PHP | 598 lines | 472 code | 60 blank | 66 comment | 147 complexity | 45e8abcb7e7016c326ab4bd66c742a12 MD5 | raw file
Possible License(s): LGPL-2.1, GPL-2.0
  1. <?php
  2. //////////////////////////////////////////////////////////////
  3. /// phpThumb() by James Heinrich <info@silisoftware.com> //
  4. // available at http://phpthumb.sourceforge.net ///
  5. //////////////////////////////////////////////////////////////
  6. /// //
  7. // See: phpthumb.changelog.txt for recent changes //
  8. // See: phpthumb.readme.txt for usage instructions //
  9. // ///
  10. //////////////////////////////////////////////////////////////
  11. error_reporting(E_ALL);
  12. ini_set('display_errors', '1');
  13. if (!@ini_get('safe_mode')) {
  14. set_time_limit(60); // shouldn't take nearly this long in most cases, but with many filter and/or a slow server...
  15. }
  16. ini_set('magic_quotes_runtime', '0');
  17. if (@ini_get('magic_quotes_runtime')) {
  18. die('"magic_quotes_runtime" is set in php.ini, cannot run phpThumb with this enabled');
  19. }
  20. $starttime = array_sum(explode(' ', microtime()));
  21. // this script relies on the superglobal arrays, fake it here for old PHP versions
  22. if (phpversion() < '4.1.0') {
  23. $_SERVER = $HTTP_SERVER_VARS;
  24. $_GET = $HTTP_GET_VARS;
  25. }
  26. // instantiate a new phpThumb() object
  27. ob_start();
  28. if (!include_once(dirname(__FILE__).'/phpthumb.class.php')) {
  29. ob_end_flush();
  30. die('failed to include_once("'.realpath(dirname(__FILE__).'/phpthumb.class.php').'")');
  31. }
  32. ob_end_clean();
  33. $phpThumb = new phpThumb();
  34. $phpThumb->DebugTimingMessage('phpThumb.php start', __FILE__, __LINE__, $starttime);
  35. // phpThumbDebug[0] used to be here, but may reveal too much
  36. // info when high_security_mode should be enabled (not set yet)
  37. if (file_exists(dirname(__FILE__).'/phpThumb.config.php')) {
  38. ob_start();
  39. if (include_once(dirname(__FILE__).'/phpThumb.config.php')) {
  40. // great
  41. } else {
  42. ob_end_flush();
  43. $phpThumb->ErrorImage('failed to include_once('.dirname(__FILE__).'/phpThumb.config.php) - realpath="'.realpath(dirname(__FILE__).'/phpThumb.config.php').'"');
  44. }
  45. ob_end_clean();
  46. } elseif (file_exists(dirname(__FILE__).'/phpThumb.config.php.default')) {
  47. $phpThumb->ErrorImage('Please rename "phpThumb.config.php.default" to "phpThumb.config.php"');
  48. } else {
  49. $phpThumb->ErrorImage('failed to include_once('.dirname(__FILE__).'/phpThumb.config.php) - realpath="'.realpath(dirname(__FILE__).'/phpThumb.config.php').'"');
  50. }
  51. if (!@$PHPTHUMB_CONFIG['disable_pathinfo_parsing'] && (empty($_GET) || isset($_GET['phpThumbDebug'])) && !empty($_SERVER['PATH_INFO'])) {
  52. $_SERVER['PHP_SELF'] = str_replace($_SERVER['PATH_INFO'], '', @$_SERVER['PHP_SELF']);
  53. $args = explode(';', substr($_SERVER['PATH_INFO'], 1));
  54. $phpThumb->DebugMessage('PATH_INFO.$args set to ('.implode(')(', $args).')', __FILE__, __LINE__);
  55. if (!empty($args)) {
  56. $_GET['src'] = @$args[count($args) - 1];
  57. if (eregi('^new\=([a-z0-9]+)', $_GET['src'], $matches)) {
  58. unset($_GET['src']);
  59. $_GET['new'] = $matches[1];
  60. }
  61. }
  62. if (eregi('^([0-9]*)x?([0-9]*)$', @$args[count($args) - 2], $matches)) {
  63. $_GET['w'] = $matches[1];
  64. $_GET['h'] = $matches[2];
  65. $phpThumb->DebugMessage('PATH_INFO."w"x"h" set to "'.$_GET['w'].'"x"'.$_GET['h'].'"', __FILE__, __LINE__);
  66. }
  67. for ($i = 0; $i < count($args) - 2; $i++) {
  68. @list($key, $value) = explode('=', @$args[$i]);
  69. if (substr($key, -2) == '[]') {
  70. $array_key_name = substr($key, 0, -2);
  71. $_GET[$array_key_name][] = $value;
  72. $phpThumb->DebugMessage('PATH_INFO."'.$array_key_name.'[]" = "'.$value.'"', __FILE__, __LINE__);
  73. } else {
  74. $_GET[$key] = $value;
  75. $phpThumb->DebugMessage('PATH_INFO."'.$key.'" = "'.$value.'"', __FILE__, __LINE__);
  76. }
  77. }
  78. }
  79. if (@$PHPTHUMB_CONFIG['high_security_enabled']) {
  80. if (!@$_GET['hash']) {
  81. $phpThumb->ErrorImage('ERROR: missing hash');
  82. } elseif (strlen($PHPTHUMB_CONFIG['high_security_password']) < 5) {
  83. $phpThumb->ErrorImage('ERROR: strlen($PHPTHUMB_CONFIG[high_security_password]) < 5');
  84. } elseif ($_GET['hash'] != md5(str_replace('&hash='.$_GET['hash'], '', $_SERVER['QUERY_STRING']).$PHPTHUMB_CONFIG['high_security_password'])) {
  85. $phpThumb->ErrorImage('ERROR: invalid hash');
  86. }
  87. }
  88. ////////////////////////////////////////////////////////////////
  89. // Debug output, to try and help me diagnose problems
  90. $phpThumb->DebugTimingMessage('phpThumbDebug[0]', __FILE__, __LINE__);
  91. if (@$_GET['phpThumbDebug'] == '0') {
  92. $phpThumb->phpThumbDebug();
  93. }
  94. ////////////////////////////////////////////////////////////////
  95. // returned the fixed string if the evil "magic_quotes_gpc" setting is on
  96. if (get_magic_quotes_gpc()) {
  97. $RequestVarsToStripSlashes = array('src', 'wmf', 'file', 'err', 'goto', 'down');
  98. foreach ($RequestVarsToStripSlashes as $dummy => $key) {
  99. if (isset($_GET[$key])) {
  100. $_GET[$key] = stripslashes($_GET[$key]);
  101. }
  102. }
  103. }
  104. if (!@$_SERVER['PATH_INFO'] && !@$_SERVER['QUERY_STRING']) {
  105. $phpThumb->ErrorImage('phpThumb() v'.$phpThumb->phpthumb_version.'<br><a href="http://phpthumb.sourceforge.net">http://phpthumb.sourceforge.net</a><br><br>ERROR: no parameters specified');
  106. }
  107. if (@$_GET['src'] && isset($_GET['md5s']) && empty($_GET['md5s'])) {
  108. if (eregi('^(f|ht)tps?://', $_GET['src'])) {
  109. if ($rawImageData = phpthumb_functions::SafeURLread($_GET['src'], $error)) {
  110. $md5s = md5($rawImageData);
  111. }
  112. } else {
  113. $SourceFilename = $phpThumb->ResolveFilenameToAbsolute($_GET['src']);
  114. if (is_readable($SourceFilename)) {
  115. $md5s = phpthumb_functions::md5_file_safe($SourceFilename);
  116. } else {
  117. $phpThumb->ErrorImage('ERROR: "'.$SourceFilename.'" cannot be read');
  118. }
  119. }
  120. if (@$_SERVER['HTTP_REFERER']) {
  121. $phpThumb->ErrorImage('&md5s='.$md5s);
  122. } else {
  123. die('&md5s='.$md5s);
  124. }
  125. }
  126. if (!empty($PHPTHUMB_CONFIG)) {
  127. foreach ($PHPTHUMB_CONFIG as $key => $value) {
  128. $keyname = 'config_'.$key;
  129. $phpThumb->setParameter($keyname, $value);
  130. if (!eregi('password', $key)) {
  131. $phpThumb->DebugMessage('setParameter('.$keyname.', '.$phpThumb->phpThumbDebugVarDump($value).')', __FILE__, __LINE__);
  132. }
  133. }
  134. } else {
  135. $phpThumb->DebugMessage('$PHPTHUMB_CONFIG is empty', __FILE__, __LINE__);
  136. }
  137. if (@$_GET['src'] && !@$PHPTHUMB_CONFIG['allow_local_http_src'] && eregi('^http://'.@$_SERVER['HTTP_HOST'].'(.+)', @$_GET['src'], $matches)) {
  138. $phpThumb->ErrorImage('It is MUCH better to specify the "src" parameter as "'.$matches[1].'" instead of "'.$matches[0].'".'."\n\n".'If you really must do it this way, enable "allow_local_http_src" in phpThumb.config.php');
  139. }
  140. ////////////////////////////////////////////////////////////////
  141. // Debug output, to try and help me diagnose problems
  142. $phpThumb->DebugTimingMessage('phpThumbDebug[1]', __FILE__, __LINE__);
  143. if (@$_GET['phpThumbDebug'] == '1') {
  144. $phpThumb->phpThumbDebug();
  145. }
  146. ////////////////////////////////////////////////////////////////
  147. $parsed_url_referer = parse_url(@$_SERVER['HTTP_REFERER']);
  148. if ($phpThumb->config_nooffsitelink_require_refer && !in_array(@$parsed_url_referer['host'], $phpThumb->config_nohotlink_valid_domains)) {
  149. $phpThumb->ErrorImage('config_nooffsitelink_require_refer enabled and '.(@$parsed_url_referer['host'] ? '"'.$parsed_url_referer['host'].'" is not an allowed referer' : 'no HTTP_REFERER exists'));
  150. }
  151. $parsed_url_src = parse_url(@$_GET['src']);
  152. if ($phpThumb->config_nohotlink_enabled && $phpThumb->config_nohotlink_erase_image && eregi('^(f|ht)tps?://', @$_GET['src']) && !in_array(@$parsed_url_src['host'], $phpThumb->config_nohotlink_valid_domains)) {
  153. $phpThumb->ErrorImage($phpThumb->config_nohotlink_text_message);
  154. }
  155. if ($phpThumb->config_mysql_query) {
  156. if ($cid = @mysql_connect($phpThumb->config_mysql_hostname, $phpThumb->config_mysql_username, $phpThumb->config_mysql_password)) {
  157. if (@mysql_select_db($phpThumb->config_mysql_database, $cid)) {
  158. if ($result = @mysql_query($phpThumb->config_mysql_query, $cid)) {
  159. if ($row = @mysql_fetch_array($result)) {
  160. mysql_free_result($result);
  161. mysql_close($cid);
  162. $phpThumb->setSourceData($row[0]);
  163. unset($row);
  164. } else {
  165. mysql_free_result($result);
  166. mysql_close($cid);
  167. $phpThumb->ErrorImage('no matching data in database.');
  168. }
  169. } else {
  170. mysql_close($cid);
  171. $phpThumb->ErrorImage('Error in MySQL query: "'.mysql_error($cid).'"');
  172. }
  173. } else {
  174. mysql_close($cid);
  175. $phpThumb->ErrorImage('cannot select MySQL database: "'.mysql_error($cid).'"');
  176. }
  177. } else {
  178. $phpThumb->ErrorImage('cannot connect to MySQL server');
  179. }
  180. unset($_GET['id']);
  181. }
  182. ////////////////////////////////////////////////////////////////
  183. // Debug output, to try and help me diagnose problems
  184. $phpThumb->DebugTimingMessage('phpThumbDebug[2]', __FILE__, __LINE__);
  185. if (@$_GET['phpThumbDebug'] == '2') {
  186. $phpThumb->phpThumbDebug();
  187. }
  188. ////////////////////////////////////////////////////////////////
  189. if (@$PHPTHUMB_CONFIG['cache_default_only_suffix'] && (strpos($PHPTHUMB_CONFIG['cache_default_only_suffix'], '*') !== false)) {
  190. $PHPTHUMB_DEFAULTS_DISABLEGETPARAMS = true;
  191. }
  192. $allowedGETparameters = array('src', 'new', 'w', 'h', 'wp', 'hp', 'wl', 'hl', 'ws', 'hs', 'f', 'q', 'sx', 'sy', 'sw', 'sh', 'zc', 'bc', 'bg', 'bgt', 'fltr', 'file', 'goto', 'err', 'xto', 'ra', 'ar', 'aoe', 'far', 'iar', 'maxb', 'down', 'phpThumbDebug', 'hash', 'md5s');
  193. if (!empty($PHPTHUMB_DEFAULTS) && is_array($PHPTHUMB_DEFAULTS)) {
  194. $phpThumb->DebugMessage('setting $PHPTHUMB_DEFAULTS['.implode(';', array_keys($PHPTHUMB_DEFAULTS)).']', __FILE__, __LINE__);
  195. foreach ($PHPTHUMB_DEFAULTS as $key => $value) {
  196. if ($PHPTHUMB_DEFAULTS_GETSTRINGOVERRIDE || !isset($_GET[$key])) {
  197. $_GET[$key] = $value;
  198. $phpThumb->DebugMessage('PHPTHUMB_DEFAULTS assigning ('.$value.') to $_GET['.$key.']', __FILE__, __LINE__);
  199. //$phpThumb->DebugMessage('PHPTHUMB_DEFAULTS.setParameter('.$key.', '.$phpThumb->phpThumbDebugVarDump($value).')', __FILE__, __LINE__);
  200. //$phpThumb->setParameter($key, $value);
  201. }
  202. }
  203. }
  204. foreach ($_GET as $key => $value) {
  205. if (@$PHPTHUMB_DEFAULTS_DISABLEGETPARAMS && ($key != 'src')) {
  206. // disabled, do not set parameter
  207. $phpThumb->DebugMessage('ignoring $_GET['.$key.'] because of $PHPTHUMB_DEFAULTS_DISABLEGETPARAMS', __FILE__, __LINE__);
  208. } elseif (in_array($key, $allowedGETparameters)) {
  209. $phpThumb->DebugMessage('setParameter('.$key.', '.$phpThumb->phpThumbDebugVarDump($value).')', __FILE__, __LINE__);
  210. $phpThumb->setParameter($key, $value);
  211. } else {
  212. $phpThumb->ErrorImage('Forbidden parameter: '.$key);
  213. }
  214. }
  215. ////////////////////////////////////////////////////////////////
  216. // Debug output, to try and help me diagnose problems
  217. $phpThumb->DebugTimingMessage('phpThumbDebug[3]', __FILE__, __LINE__);
  218. if (@$_GET['phpThumbDebug'] == '3') {
  219. $phpThumb->phpThumbDebug();
  220. }
  221. ////////////////////////////////////////////////////////////////
  222. //if (!@$_GET['phpThumbDebug'] && !$phpThumb->sourceFilename && !function_exists('ImageJPEG') && !function_exists('ImagePNG') && !function_exists('ImageGIF')) {
  223. if (!@$_GET['phpThumbDebug'] && !is_file($phpThumb->sourceFilename) && !phpthumb_functions::gd_version()) {
  224. if (!headers_sent()) {
  225. // base64-encoded error image in GIF format
  226. $ERROR_NOGD = 'R0lGODlhIAAgALMAAAAAABQUFCQkJDY2NkZGRldXV2ZmZnJycoaGhpSUlKWlpbe3t8XFxdXV1eTk5P7+/iwAAAAAIAAgAAAE/vDJSau9WILtTAACUinDNijZtAHfCojS4W5H+qxD8xibIDE9h0OwWaRWDIljJSkUJYsN4bihMB8th3IToAKs1VtYM75cyV8sZ8vygtOE5yMKmGbO4jRdICQCjHdlZzwzNW4qZSQmKDaNjhUMBX4BBAlmMywFSRWEmAI6b5gAlhNxokGhooAIK5o/pi9vEw4Lfj4OLTAUpj6IabMtCwlSFw0DCKBoFqwAB04AjI54PyZ+yY3TD0ss2YcVmN/gvpcu4TOyFivWqYJlbAHPpOntvxNAACcmGHjZzAZqzSzcq5fNjxFmAFw9iFRunD1epU6tsIPmFCAJnWYE0FURk7wJDA0MTKpEzoWAAskiAAA7';
  227. header('Content-Type: image/gif');
  228. echo base64_decode($ERROR_NOGD);
  229. } else {
  230. echo '*** ERROR: No PHP-GD support available ***';
  231. }
  232. exit;
  233. }
  234. // check to see if file can be output from source with no processing or caching
  235. $CanPassThroughDirectly = true;
  236. if ($phpThumb->rawImageData) {
  237. // data from SQL, should be fine
  238. } elseif (eregi('^(f|ht)tp\://', $phpThumb->src)) {
  239. $phpThumb->DebugMessage('$CanPassThroughDirectly=false because eregi("^(f|ht)tp\://", '.$phpThumb->src.')', __FILE__, __LINE__);
  240. $CanPassThroughDirectly = false;
  241. } elseif (!@is_file($phpThumb->sourceFilename)) {
  242. $phpThumb->DebugMessage('$CanPassThroughDirectly=false because !@is_file('.$phpThumb->sourceFilename.')', __FILE__, __LINE__);
  243. $CanPassThroughDirectly = false;
  244. } elseif (!@is_readable($phpThumb->sourceFilename)) {
  245. $phpThumb->DebugMessage('$CanPassThroughDirectly=false because !@is_readable('.$phpThumb->sourceFilename.')', __FILE__, __LINE__);
  246. $CanPassThroughDirectly = false;
  247. }
  248. foreach ($_GET as $key => $value) {
  249. switch ($key) {
  250. case 'src':
  251. // allowed
  252. break;
  253. case 'w':
  254. case 'h':
  255. // might be OK if exactly matches original
  256. break;
  257. case 'phpThumbDebug':
  258. // handled in direct-passthru code
  259. break;
  260. default:
  261. // all other parameters will cause some processing,
  262. // therefore cannot pass through original image unmodified
  263. $CanPassThroughDirectly = false;
  264. $UnAllowedGET[] = $key;
  265. break;
  266. }
  267. }
  268. if (!empty($UnAllowedGET)) {
  269. $phpThumb->DebugMessage('$CanPassThroughDirectly=false because $_GET['.implode(';', array_unique($UnAllowedGET)).'] are set', __FILE__, __LINE__);
  270. }
  271. ////////////////////////////////////////////////////////////////
  272. // Debug output, to try and help me diagnose problems
  273. $phpThumb->DebugTimingMessage('phpThumbDebug[4]', __FILE__, __LINE__);
  274. if (@$_GET['phpThumbDebug'] == '4') {
  275. $phpThumb->phpThumbDebug();
  276. }
  277. ////////////////////////////////////////////////////////////////
  278. function SendSaveAsFileHeaderIfNeeded() {
  279. if (headers_sent()) {
  280. return false;
  281. }
  282. global $phpThumb;
  283. if (@$_GET['down']) {
  284. $downloadfilename = ereg_replace('[/\\:\*\?"<>|]', '_', $_GET['down']);
  285. if (phpthumb_functions::version_compare_replacement(phpversion(), '4.1.0', '>=')) {
  286. $downloadfilename = trim($downloadfilename, '.');
  287. }
  288. if (@$downloadfilename) {
  289. $phpThumb->DebugMessage('SendSaveAsFileHeaderIfNeeded() sending header: Content-Disposition: attachment; filename="'.$downloadfilename.'"', __FILE__, __LINE__);
  290. header('Content-Disposition: attachment; filename="'.$downloadfilename.'"');
  291. return true;
  292. }
  293. }
  294. $phpThumb->DebugMessage('SendSaveAsFileHeaderIfNeeded() sending header: Content-Disposition: inline', __FILE__, __LINE__);
  295. header('Content-Disposition: inline');
  296. return true;
  297. }
  298. $phpThumb->DebugMessage('$CanPassThroughDirectly="'.intval($CanPassThroughDirectly).'" && $phpThumb->src="'.$phpThumb->src.'"', __FILE__, __LINE__);
  299. while ($CanPassThroughDirectly && $phpThumb->src) {
  300. // no parameters set, passthru
  301. $SourceFilename = $phpThumb->ResolveFilenameToAbsolute($phpThumb->src);
  302. // security and size checks
  303. if ($phpThumb->getimagesizeinfo = @GetImageSize($SourceFilename)) {
  304. $phpThumb->DebugMessage('Direct passthru GetImageSize() returned [w='.$phpThumb->getimagesizeinfo[0].';h='.$phpThumb->getimagesizeinfo[1].';t='.$phpThumb->getimagesizeinfo[2].']', __FILE__, __LINE__);
  305. if (!@$_GET['w'] && !@$_GET['wp'] && !@$_GET['wl'] && !@$_GET['ws'] && !@$_GET['h'] && !@$_GET['hp'] && !@$_GET['hl'] && !@$_GET['hs']) {
  306. // no resizing needed
  307. $phpThumb->DebugMessage('Passing "'.$SourceFilename.'" through directly, no resizing required ("'.$phpThumb->getimagesizeinfo[0].'"x"'.$phpThumb->getimagesizeinfo[1].'")', __FILE__, __LINE__);
  308. } elseif (($phpThumb->getimagesizeinfo[0] <= @$_GET['w']) && ($phpThumb->getimagesizeinfo[1] <= @$_GET['h']) && ((@$_GET['w'] == $phpThumb->getimagesizeinfo[0]) || (@$_GET['h'] == $phpThumb->getimagesizeinfo[1]))) {
  309. // image fits into 'w'x'h' box, and at least one dimension matches exactly, therefore no resizing needed
  310. $phpThumb->DebugMessage('Passing "'.$SourceFilename.'" through directly, no resizing required ("'.$phpThumb->getimagesizeinfo[0].'"x"'.$phpThumb->getimagesizeinfo[1].'" fits inside "'.@$_GET['w'].'"x"'.@$_GET['h'].'")', __FILE__, __LINE__);
  311. } else {
  312. $phpThumb->DebugMessage('Not passing "'.$SourceFilename.'" through directly because resizing required (from "'.$phpThumb->getimagesizeinfo[0].'"x"'.$phpThumb->getimagesizeinfo[1].'" to "'.@$_GET['w'].'"x"'.@$_GET['h'].'")', __FILE__, __LINE__);
  313. break;
  314. }
  315. switch ($phpThumb->getimagesizeinfo[2]) {
  316. case 1: // GIF
  317. case 2: // JPG
  318. case 3: // PNG
  319. // great, let it through
  320. break;
  321. default:
  322. // browser probably can't handle format, remangle it to JPEG/PNG/GIF
  323. $phpThumb->DebugMessage('Not passing "'.$SourceFilename.'" through directly because $phpThumb->getimagesizeinfo[2] = "'.$phpThumb->getimagesizeinfo[2].'"', __FILE__, __LINE__);
  324. break 2;
  325. }
  326. $ImageCreateFunctions = array(1=>'ImageCreateFromGIF', 2=>'ImageCreateFromJPEG', 3=>'ImageCreateFromPNG');
  327. $theImageCreateFunction = @$ImageCreateFunctions[$phpThumb->getimagesizeinfo[2]];
  328. if ($phpThumb->config_disable_onlycreateable_passthru || (function_exists($theImageCreateFunction) && ($dummyImage = @$theImageCreateFunction($SourceFilename)))) {
  329. // great
  330. if (@is_resource($dummyImage)) {
  331. unset($dummyImage);
  332. }
  333. if (headers_sent()) {
  334. $phpThumb->ErrorImage('Headers already sent ('.basename(__FILE__).' line '.__LINE__.')');
  335. exit;
  336. }
  337. if (@$_GET['phpThumbDebug']) {
  338. $phpThumb->DebugTimingMessage('skipped direct $SourceFilename passthru', __FILE__, __LINE__);
  339. $phpThumb->DebugMessage('Would have passed "'.$SourceFilename.'" through directly, but skipping due to phpThumbDebug', __FILE__, __LINE__);
  340. break;
  341. }
  342. SendSaveAsFileHeaderIfNeeded();
  343. header('Last-Modified: '.gmdate('D, d M Y H:i:s', @filemtime($SourceFilename)).' GMT');
  344. if ($contentType = phpthumb_functions::ImageTypeToMIMEtype(@$phpThumb->getimagesizeinfo[2])) {
  345. header('Content-Type: '.$contentType);
  346. }
  347. @readfile($SourceFilename);
  348. exit;
  349. } else {
  350. $phpThumb->DebugMessage('Not passing "'.$SourceFilename.'" through directly because ($phpThumb->config_disable_onlycreateable_passthru = "'.$phpThumb->config_disable_onlycreateable_passthru.'") and '.$theImageCreateFunction.'() failed', __FILE__, __LINE__);
  351. break;
  352. }
  353. } else {
  354. $phpThumb->DebugMessage('Not passing "'.$SourceFilename.'" through directly because GetImageSize() failed', __FILE__, __LINE__);
  355. break;
  356. }
  357. break;
  358. }
  359. ////////////////////////////////////////////////////////////////
  360. // Debug output, to try and help me diagnose problems
  361. $phpThumb->DebugTimingMessage('phpThumbDebug[5]', __FILE__, __LINE__);
  362. if (@$_GET['phpThumbDebug'] == '5') {
  363. $phpThumb->phpThumbDebug();
  364. }
  365. ////////////////////////////////////////////////////////////////
  366. function RedirectToCachedFile() {
  367. global $phpThumb, $PHPTHUMB_CONFIG;
  368. $nice_cachefile = str_replace(DIRECTORY_SEPARATOR, '/', $phpThumb->cache_filename);
  369. $nice_docroot = str_replace(DIRECTORY_SEPARATOR, '/', rtrim($PHPTHUMB_CONFIG['document_root'], '/\\'));
  370. $parsed_url = @parse_url(@$_SERVER['HTTP_REFERER']);
  371. $nModified = filemtime($phpThumb->cache_filename);
  372. if ($phpThumb->config_nooffsitelink_enabled && @$_SERVER['HTTP_REFERER'] && !in_array(@$parsed_url['host'], $phpThumb->config_nooffsitelink_valid_domains)) {
  373. $phpThumb->DebugMessage('Would have used cached (image/'.$phpThumb->thumbnailFormat.') file "'.$phpThumb->cache_filename.'" (Last-Modified: '.gmdate('D, d M Y H:i:s', $nModified).' GMT), but skipping because $_SERVER[HTTP_REFERER] ('.@$_SERVER['HTTP_REFERER'].') is not in $phpThumb->config_nooffsitelink_valid_domains ('.implode(';', $phpThumb->config_nooffsitelink_valid_domains).')', __FILE__, __LINE__);
  374. } elseif ($phpThumb->phpThumbDebug) {
  375. $phpThumb->DebugTimingMessage('skipped using cached image', __FILE__, __LINE__);
  376. $phpThumb->DebugMessage('Would have used cached file, but skipping due to phpThumbDebug', __FILE__, __LINE__);
  377. $phpThumb->DebugMessage('* Would have sent headers (1): Last-Modified: '.gmdate('D, d M Y H:i:s', $nModified).' GMT', __FILE__, __LINE__);
  378. if ($getimagesize = @GetImageSize($phpThumb->cache_filename)) {
  379. $phpThumb->DebugMessage('* Would have sent headers (2): Content-Type: '.phpthumb_functions::ImageTypeToMIMEtype($getimagesize[2]), __FILE__, __LINE__);
  380. }
  381. if (ereg('^'.preg_quote($nice_docroot).'(.*)$', $nice_cachefile, $matches)) {
  382. $phpThumb->DebugMessage('* Would have sent headers (3): Location: '.dirname($matches[1]).'/'.urlencode(basename($matches[1])), __FILE__, __LINE__);
  383. } else {
  384. $phpThumb->DebugMessage('* Would have sent data: readfile('.$phpThumb->cache_filename.')', __FILE__, __LINE__);
  385. }
  386. } else {
  387. if (headers_sent()) {
  388. $phpThumb->ErrorImage('Headers already sent ('.basename(__FILE__).' line '.__LINE__.')');
  389. exit;
  390. }
  391. SendSaveAsFileHeaderIfNeeded();
  392. header('Last-Modified: '.gmdate('D, d M Y H:i:s', $nModified).' GMT');
  393. if (@$_SERVER['HTTP_IF_MODIFIED_SINCE'] && ($nModified == strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE'])) && @$_SERVER['SERVER_PROTOCOL']) {
  394. header($_SERVER['SERVER_PROTOCOL'].' 304 Not Modified');
  395. exit;
  396. }
  397. if ($getimagesize = @GetImageSize($phpThumb->cache_filename)) {
  398. header('Content-Type: '.phpthumb_functions::ImageTypeToMIMEtype($getimagesize[2]));
  399. } elseif (eregi('\.ico$', $phpThumb->cache_filename)) {
  400. header('Content-Type: image/x-icon');
  401. }
  402. if (!@$PHPTHUMB_CONFIG['cache_force_passthru'] && ereg('^'.preg_quote($nice_docroot).'(.*)$', $nice_cachefile, $matches)) {
  403. header('Location: '.dirname($matches[1]).'/'.urlencode(basename($matches[1])));
  404. } else {
  405. @readfile($phpThumb->cache_filename);
  406. }
  407. exit;
  408. }
  409. return true;
  410. }
  411. // check to see if file already exists in cache, and output it with no processing if it does
  412. $phpThumb->SetCacheFilename();
  413. if (@is_file($phpThumb->cache_filename)) {
  414. RedirectToCachedFile();
  415. } else {
  416. $phpThumb->DebugMessage('Cached file "'.$phpThumb->cache_filename.'" does not exist, processing as normal', __FILE__, __LINE__);
  417. }
  418. ////////////////////////////////////////////////////////////////
  419. // Debug output, to try and help me diagnose problems
  420. $phpThumb->DebugTimingMessage('phpThumbDebug[6]', __FILE__, __LINE__);
  421. if (@$_GET['phpThumbDebug'] == '6') {
  422. $phpThumb->phpThumbDebug();
  423. }
  424. ////////////////////////////////////////////////////////////////
  425. if ($phpThumb->rawImageData) {
  426. // great
  427. } elseif (@$_GET['new']) {
  428. // generate a blank image resource of the specified size/background color/opacity
  429. if (($phpThumb->w <= 0) || ($phpThumb->h <= 0)) {
  430. $phpThumb->ErrorImage('"w" and "h" parameters required for "new"');
  431. }
  432. @list($bghexcolor, $opacity) = explode('|', $_GET['new']);
  433. if (!phpthumb_functions::IsHexColor($bghexcolor)) {
  434. $phpThumb->ErrorImage('BGcolor parameter for "new" is not valid');
  435. }
  436. $opacity = (strlen($opacity) ? $opacity : 100);
  437. if ($phpThumb->gdimg_source = phpthumb_functions::ImageCreateFunction($phpThumb->w, $phpThumb->h)) {
  438. $alpha = (100 - min(100, max(0, $opacity))) * 1.27;
  439. if ($alpha) {
  440. $phpThumb->setParameter('is_alpha', true);
  441. ImageAlphaBlending($phpThumb->gdimg_source, false);
  442. ImageSaveAlpha($phpThumb->gdimg_source, true);
  443. }
  444. $new_background_color = phpthumb_functions::ImageHexColorAllocate($phpThumb->gdimg_source, $bghexcolor, false, $alpha);
  445. ImageFilledRectangle($phpThumb->gdimg_source, 0, 0, $phpThumb->w, $phpThumb->h, $new_background_color);
  446. } else {
  447. $phpThumb->ErrorImage('failed to create "new" image ('.$phpThumb->w.'x'.$phpThumb->h.')');
  448. }
  449. } elseif (!$phpThumb->src) {
  450. $phpThumb->ErrorImage('Usage: '.$_SERVER['PHP_SELF'].'?src=/path/and/filename.jpg'."\n".'read Usage comments for details');
  451. } elseif (eregi('^(f|ht)tp\://', $phpThumb->src)) {
  452. if ($phpThumb->config_http_user_agent) {
  453. ini_set('user_agent', $phpThumb->config_http_user_agent);
  454. }
  455. $basename = preg_quote(basename($phpThumb->src));
  456. $encoded = rawurlencode(basename($phpThumb->src));
  457. $HTTPurl = ereg_replace('(.*)'.$basename.'$', '\\1'.$encoded, $phpThumb->src);
  458. if ($rawImageData = phpthumb_functions::SafeURLread($HTTPurl, $error)) {
  459. $phpThumb->setSourceData($rawImageData, urlencode($phpThumb->src));
  460. } else {
  461. $phpThumb->ErrorImage($error);
  462. }
  463. }
  464. ////////////////////////////////////////////////////////////////
  465. // Debug output, to try and help me diagnose problems
  466. $phpThumb->DebugTimingMessage('phpThumbDebug[7]', __FILE__, __LINE__);
  467. if (@$_GET['phpThumbDebug'] == '7') {
  468. $phpThumb->phpThumbDebug();
  469. }
  470. ////////////////////////////////////////////////////////////////
  471. $phpThumb->GenerateThumbnail();
  472. ////////////////////////////////////////////////////////////////
  473. // Debug output, to try and help me diagnose problems
  474. $phpThumb->DebugTimingMessage('phpThumbDebug[8]', __FILE__, __LINE__);
  475. if (@$_GET['phpThumbDebug'] == '8') {
  476. $phpThumb->phpThumbDebug();
  477. }
  478. ////////////////////////////////////////////////////////////////
  479. if ($phpThumb->config_allow_parameter_file && $phpThumb->file) {
  480. $phpThumb->RenderToFile($phpThumb->ResolveFilenameToAbsolute($phpThumb->file));
  481. if ($phpThumb->config_allow_parameter_goto && $phpThumb->goto && eregi('^(f|ht)tps?://', $phpThumb->goto)) {
  482. // redirect to another URL after image has been rendered to file
  483. header('Location: '.$phpThumb->goto);
  484. exit;
  485. }
  486. } elseif (@$PHPTHUMB_CONFIG['high_security_enabled'] && @$_GET['nocache']) {
  487. // cache disabled, don't write cachefile
  488. } else {
  489. if ((file_exists($phpThumb->cache_filename) && is_writable($phpThumb->cache_filename)) || is_writable(dirname($phpThumb->cache_filename))) {
  490. $phpThumb->CleanUpCacheDirectory();
  491. if ($phpThumb->RenderToFile($phpThumb->cache_filename) && is_readable($phpThumb->cache_filename)) {
  492. chmod($phpThumb->cache_filename, 0644);
  493. RedirectToCachedFile();
  494. } else {
  495. $phpThumb->DebugMessage('Failed: RenderToFile('.$phpThumb->cache_filename.')', __FILE__, __LINE__);
  496. }
  497. } else {
  498. $phpThumb->DebugMessage('Cannot write to $phpThumb->cache_filename ('.$phpThumb->cache_filename.') because that directory ('.dirname($phpThumb->cache_filename).') is not writable', __FILE__, __LINE__);
  499. }
  500. }
  501. ////////////////////////////////////////////////////////////////
  502. // Debug output, to try and help me diagnose problems
  503. $phpThumb->DebugTimingMessage('phpThumbDebug[9]', __FILE__, __LINE__);
  504. if (@$_GET['phpThumbDebug'] == '9') {
  505. $phpThumb->phpThumbDebug();
  506. }
  507. ////////////////////////////////////////////////////////////////
  508. if (!$phpThumb->OutputThumbnail()) {
  509. $phpThumb->ErrorImage('Error in OutputThumbnail():'."\n".$phpThumb->debugmessages[(count($phpThumb->debugmessages) - 1)]);
  510. }
  511. ////////////////////////////////////////////////////////////////
  512. // Debug output, to try and help me diagnose problems
  513. $phpThumb->DebugTimingMessage('phpThumbDebug[10]', __FILE__, __LINE__);
  514. if (@$_GET['phpThumbDebug'] == '10') {
  515. $phpThumb->phpThumbDebug();
  516. }
  517. ////////////////////////////////////////////////////////////////
  518. ?>