PageRenderTime 56ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/thumb/phpThumb.php

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