PageRenderTime 70ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

/includes/phpthumb/phpThumb.php

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