Advertisement
Vodkaholic

Untitled

Jun 10th, 2011
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 20.02 KB | None | 0 0
  1. <?php
  2. /**
  3. * TimThumb script created by Ben Gillbanks, originally created by Tim McDaniels and Darren Hoyt
  4. * http://code.google.com/p/timthumb/
  5. *
  6. * GNU General Public License, version 2
  7. * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  8. *
  9. * Examples and documentation available on the project homepage
  10. * http://www.binarymoon.co.uk/projects/timthumb/
  11. */
  12.  
  13. define ('CACHE_SIZE', 500000); // number of files to store before clearing cache
  14. define ('CACHE_CLEAR', 200); // maximum number of files to delete on each cache clear
  15. define ('CACHE_USE', TRUE); // use the cache files? (mostly for testing)
  16. define ('CACHE_MAX_AGE', 864000); // time to cache in the browser
  17. define ('VERSION', '1.26'); // version number (to force a cache refresh)
  18. define ('DIRECTORY_CACHE', './cache'); // cache directory
  19. define ('MAX_WIDTH', 1500); // maximum image width
  20. define ('MAX_HEIGHT', 1500); // maximum image height
  21. define ('ALLOW_EXTERNAL', true); // allow external website (override security precaution - not advised!)
  22. define ('MEMORY_LIMIT', '128M'); // set PHP memory limit
  23. define ('MAX_FILE_SIZE', 15000000); // file size limit to prevent possible DOS attacks (roughly 1.5 megabytes)
  24. define ('CURL_TIMEOUT', 10); // timeout duration. Tweak as you require (lower = better)
  25.  
  26. // external domains that are allowed to be displayed on your website
  27. $allowedSites = array (
  28. );
  29.  
  30. // STOP MODIFYING HERE!
  31. // --------------------
  32.  
  33. // sort out image source
  34. $src = get_request ('src', '');
  35. if ($src == '' || strlen ($src) <= 3) {
  36. display_error ('no image specified');
  37. }
  38.  
  39. // clean params before use
  40. $src = clean_source ($src);
  41.  
  42. // get mime type of src
  43. $mime_type = mime_type ($src);
  44.  
  45. // used for external websites only
  46. $external_data_string = '';
  47.  
  48. // generic file handle for reading and writing to files
  49. $fh = '';
  50.  
  51. // check to see if this image is in the cache already
  52. // if already cached then display the image and die
  53. check_cache ($mime_type);
  54.  
  55. // cache doesn't exist and then process everything
  56. // check to see if GD function exist
  57. if (!function_exists ('imagecreatetruecolor')) {
  58. display_error ('GD Library Error: imagecreatetruecolor does not exist - please contact your webhost and ask them to install the GD library');
  59. }
  60.  
  61. if (function_exists ('imagefilter') && defined ('IMG_FILTER_NEGATE')) {
  62. $imageFilters = array (
  63. 1 => array (IMG_FILTER_NEGATE, 0),
  64. 2 => array (IMG_FILTER_GRAYSCALE, 0),
  65. 3 => array (IMG_FILTER_BRIGHTNESS, 1),
  66. 4 => array (IMG_FILTER_CONTRAST, 1),
  67. 5 => array (IMG_FILTER_COLORIZE, 4),
  68. 6 => array (IMG_FILTER_EDGEDETECT, 0),
  69. 7 => array (IMG_FILTER_EMBOSS, 0),
  70. 8 => array (IMG_FILTER_GAUSSIAN_BLUR, 0),
  71. 9 => array (IMG_FILTER_SELECTIVE_BLUR, 0),
  72. 10 => array (IMG_FILTER_MEAN_REMOVAL, 0),
  73. 11 => array (IMG_FILTER_SMOOTH, 0),
  74. );
  75. }
  76.  
  77. // get standard input properties
  78. $new_width = (int) abs (get_request ('w', 0));
  79. $new_height = (int) abs (get_request ('h', 0));
  80. $zoom_crop = (int) get_request ('zc', 1);
  81. $quality = (int) abs (get_request ('q', 90));
  82. $align = get_request ('a', 'c');
  83. $filters = get_request ('f', '');
  84. $sharpen = (bool) get_request ('s', 0);
  85.  
  86. // set default width and height if neither are set already
  87. if ($new_width == 0 && $new_height == 0) {
  88. $new_width = 100;
  89. $new_height = 100;
  90. }
  91.  
  92. // ensure size limits can not be abused
  93. $new_width = min ($new_width, MAX_WIDTH);
  94. $new_height = min ($new_height, MAX_HEIGHT);
  95.  
  96. // set memory limit to be able to have enough space to resize larger images
  97. ini_set ('memory_limit', MEMORY_LIMIT);
  98.  
  99. if (file_exists ($src)) {
  100.  
  101. // open the existing image
  102. $image = open_image ($mime_type, $src);
  103. if ($image === false) {
  104. display_error ('Unable to open image : ' . $src);
  105. }
  106.  
  107. // Get original width and height
  108. $width = imagesx ($image);
  109. $height = imagesy ($image);
  110. $origin_x = 0;
  111. $origin_y = 0;
  112.  
  113. // generate new w/h if not provided
  114. if ($new_width && !$new_height) {
  115. $new_height = floor ($height * ($new_width / $width));
  116. } else if ($new_height && !$new_width) {
  117. $new_width = floor ($width * ($new_height / $height));
  118. }
  119.  
  120. // scale down and add borders
  121. if ($zoom_crop == 3) {
  122.  
  123. $final_height = $height * ($new_width / $width);
  124.  
  125. if ($final_height > $new_height) {
  126. $new_width = $width * ($new_height / $height);
  127. } else {
  128. $new_height = $final_height;
  129. }
  130.  
  131. }
  132.  
  133. // create a new true color image
  134. $canvas = imagecreatetruecolor ($new_width, $new_height);
  135. imagealphablending ($canvas, false);
  136.  
  137. // Create a new transparent color for image
  138. $color = imagecolorallocatealpha ($canvas, 0, 0, 0, 127);
  139.  
  140. // Completely fill the background of the new image with allocated color.
  141. imagefill ($canvas, 0, 0, $color);
  142.  
  143. // scale down and add borders
  144. if ($zoom_crop == 2) {
  145.  
  146. $final_height = $height * ($new_width / $width);
  147.  
  148. if ($final_height > $new_height) {
  149.  
  150. $origin_x = $new_width / 2;
  151. $new_width = $width * ($new_height / $height);
  152. $origin_x = round ($origin_x - ($new_width / 2));
  153.  
  154. } else {
  155.  
  156. $origin_y = $new_height / 2;
  157. $new_height = $final_height;
  158. $origin_y = round ($origin_y - ($new_height / 2));
  159.  
  160. }
  161.  
  162. }
  163.  
  164. // Restore transparency blending
  165. imagesavealpha ($canvas, true);
  166.  
  167. if ($zoom_crop > 0) {
  168.  
  169. $src_x = $src_y = 0;
  170. $src_w = $width;
  171. $src_h = $height;
  172.  
  173. $cmp_x = $width / $new_width;
  174. $cmp_y = $height / $new_height;
  175.  
  176. // calculate x or y coordinate and width or height of source
  177. if ($cmp_x > $cmp_y) {
  178.  
  179. $src_w = round ($width / $cmp_x * $cmp_y);
  180. $src_x = round (($width - ($width / $cmp_x * $cmp_y)) / 2);
  181.  
  182. } else if ($cmp_y > $cmp_x) {
  183.  
  184. $src_h = round ($height / $cmp_y * $cmp_x);
  185. $src_y = round (($height - ($height / $cmp_y * $cmp_x)) / 2);
  186.  
  187. }
  188.  
  189. // positional cropping!
  190. switch ($align) {
  191. case 't':
  192. case 'tl':
  193. case 'lt':
  194. case 'tr':
  195. case 'rt':
  196. $src_y = 0;
  197. break;
  198.  
  199. case 'b':
  200. case 'bl':
  201. case 'lb':
  202. case 'br':
  203. case 'rb':
  204. $src_y = $height - $src_h;
  205. break;
  206.  
  207. case 'l':
  208. case 'tl':
  209. case 'lt':
  210. case 'bl':
  211. case 'lb':
  212. $src_x = 0;
  213. break;
  214.  
  215. case 'r':
  216. case 'tr':
  217. case 'rt':
  218. case 'br':
  219. case 'rb':
  220. $src_x = $width - $new_width;
  221. $src_x = $width - $src_w;
  222. break;
  223.  
  224. default:
  225. break;
  226. }
  227.  
  228. imagecopyresampled ($canvas, $image, $origin_x, $origin_y, $src_x, $src_y, $new_width, $new_height, $src_w, $src_h);
  229.  
  230. } else {
  231.  
  232. // copy and resize part of an image with resampling
  233. imagecopyresampled ($canvas, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
  234.  
  235. }
  236.  
  237. if ($filters != '' && function_exists ('imagefilter') && defined ('IMG_FILTER_NEGATE')) {
  238. // apply filters to image
  239. $filterList = explode ('|', $filters);
  240. foreach ($filterList as $fl) {
  241.  
  242. $filterSettings = explode (',', $fl);
  243. if (isset ($imageFilters[$filterSettings[0]])) {
  244.  
  245. for ($i = 0; $i < 4; $i ++) {
  246. if (!isset ($filterSettings[$i])) {
  247. $filterSettings[$i] = null;
  248. } else {
  249. $filterSettings[$i] = (int) $filterSettings[$i];
  250. }
  251. }
  252.  
  253. switch ($imageFilters[$filterSettings[0]][1]) {
  254.  
  255. case 1:
  256.  
  257. imagefilter ($canvas, $imageFilters[$filterSettings[0]][0], $filterSettings[1]);
  258. break;
  259.  
  260. case 2:
  261.  
  262. imagefilter ($canvas, $imageFilters[$filterSettings[0]][0], $filterSettings[1], $filterSettings[2]);
  263. break;
  264.  
  265. case 3:
  266.  
  267. imagefilter ($canvas, $imageFilters[$filterSettings[0]][0], $filterSettings[1], $filterSettings[2], $filterSettings[3]);
  268. break;
  269.  
  270. case 4:
  271.  
  272. imagefilter ($canvas, $imageFilters[$filterSettings[0]][0], $filterSettings[1], $filterSettings[2], $filterSettings[3], $filterSettings[4]);
  273. break;
  274.  
  275. default:
  276.  
  277. imagefilter ($canvas, $imageFilters[$filterSettings[0]][0]);
  278. break;
  279.  
  280. }
  281. }
  282. }
  283. }
  284.  
  285. // sharpen image
  286. if ($sharpen && function_exists ('imageconvolution')) {
  287.  
  288. $sharpenMatrix = array (
  289. array (-1,-1,-1),
  290. array (-1,16,-1),
  291. array (-1,-1,-1),
  292. );
  293.  
  294. $divisor = 8;
  295. $offset = 0;
  296.  
  297. imageconvolution ($canvas, $sharpenMatrix, $divisor, $offset);
  298.  
  299. }
  300.  
  301. // output image to browser based on mime type
  302. show_image ($mime_type, $canvas);
  303.  
  304. // remove image from memory
  305. imagedestroy ($canvas);
  306.  
  307. // if not in cache then clear some space and generate a new file
  308. clean_cache ();
  309.  
  310. die ();
  311.  
  312. } else {
  313.  
  314. if (strlen ($src)) {
  315. display_error ('image ' . $src . ' not found');
  316. } else {
  317. display_error ('no source specified');
  318. }
  319.  
  320. }
  321.  
  322.  
  323. /**
  324. *
  325. * @global <type> $quality
  326. * @param <type> $mime_type
  327. * @param <type> $image_resized
  328. */
  329. function show_image ($mime_type, $image_resized) {
  330.  
  331. global $quality;
  332.  
  333. $cache_file = get_cache_file ($mime_type);
  334.  
  335. if (strpos ($mime_type, 'jpeg') > 1) {
  336. imagejpeg ($image_resized, $cache_file, $quality);
  337. } else {
  338. imagepng ($image_resized, $cache_file, floor ($quality * 0.09));
  339. }
  340.  
  341. show_cache_file ($mime_type);
  342.  
  343. }
  344.  
  345.  
  346. /**
  347. *
  348. * @param <type> $property
  349. * @param <type> $default
  350. * @return <type>
  351. */
  352. function get_request ($property, $default = 0) {
  353.  
  354. if (isset ($_GET[$property])) {
  355. return $_GET[$property];
  356. } else {
  357. return $default;
  358. }
  359.  
  360. }
  361.  
  362.  
  363. /**
  364. *
  365. * @param <type> $mime_type
  366. * @param <type> $src
  367. * @return <type>
  368. */
  369. function open_image ($mime_type, $src) {
  370.  
  371. if (strpos ($mime_type, 'jpeg') !== false) {
  372. $image = imagecreatefromjpeg ($src);
  373. } elseif (strpos ($mime_type, 'png') !== false) {
  374. $image = imagecreatefrompng ($src);
  375. } elseif (strpos ($mime_type, 'gif') !== false) {
  376. $image = imagecreatefromgif ($src);
  377. }
  378.  
  379. return $image;
  380.  
  381. }
  382.  
  383. /**
  384. * clean out old files from the cache
  385. * you can change the number of files to store and to delete per loop in the defines at the top of the code
  386. *
  387. * @return <type>
  388. */
  389. function clean_cache () {
  390.  
  391. // add an escape
  392. // Reduces the amount of cache clearing to save some processor speed
  393. if (rand (1, 50) > 10) {
  394. return true;
  395. }
  396.  
  397. flush ();
  398.  
  399. $files = glob (DIRECTORY_CACHE . '/*', GLOB_BRACE);
  400.  
  401. if (count ($files) > CACHE_SIZE) {
  402.  
  403. $yesterday = time () - (24 * 60 * 60);
  404.  
  405. usort ($files, 'filemtime_compare');
  406. $i = 0;
  407.  
  408. foreach ($files as $file) {
  409.  
  410. $i ++;
  411.  
  412. if ($i >= CACHE_CLEAR) {
  413. return;
  414. }
  415.  
  416. if (@filemtime ($file) > $yesterday) {
  417. return;
  418. }
  419.  
  420. if (file_exists ($file)) {
  421. unlink ($file);
  422. }
  423.  
  424. }
  425.  
  426. }
  427.  
  428. }
  429.  
  430.  
  431. /**
  432. * compare the file time of two files
  433. *
  434. * @param <type> $a
  435. * @param <type> $b
  436. * @return <type>
  437. */
  438. function filemtime_compare ($a, $b) {
  439.  
  440. $break = explode ('/', $_SERVER['SCRIPT_FILENAME']);
  441. $filename = $break[count ($break) - 1];
  442. $filepath = str_replace ($filename, '', $_SERVER['SCRIPT_FILENAME']);
  443.  
  444. $file_a = realpath ($filepath . $a);
  445. $file_b = realpath ($filepath . $b);
  446.  
  447. return filemtime ($file_a) - filemtime ($file_b);
  448.  
  449. }
  450.  
  451.  
  452. /**
  453. * determine the file mime type
  454. *
  455. * @param <type> $file
  456. * @return <type>
  457. */
  458. function mime_type ($file) {
  459.  
  460. $file_infos = getimagesize ($file);
  461. $mime_type = $file_infos['mime'];
  462.  
  463. // no mime type
  464. if (empty ($mime_type)) {
  465. display_error ('no mime type specified');
  466. }
  467.  
  468. // use mime_type to determine mime type
  469. if (!preg_match ("/jpg|jpeg|gif|png/i", $mime_type)) {
  470. display_error ('Invalid src mime type: ' . $mime_type);
  471. }
  472.  
  473. return strtolower ($mime_type);
  474.  
  475. }
  476.  
  477.  
  478. /**
  479. *
  480. * @param <type> $mime_type
  481. */
  482. function check_cache ($mime_type) {
  483.  
  484. if (CACHE_USE) {
  485.  
  486. if (!show_cache_file ($mime_type)) {
  487. // make sure cache dir exists
  488. if (!file_exists (DIRECTORY_CACHE)) {
  489. // give 777 permissions so that developer can overwrite
  490. // files created by web server user
  491. mkdir (DIRECTORY_CACHE);
  492. chmod (DIRECTORY_CACHE, 0777);
  493. }
  494. }
  495.  
  496. }
  497.  
  498. }
  499.  
  500.  
  501. /**
  502. *
  503. * @param <type> $mime_type
  504. * @return <type>
  505. */
  506. function show_cache_file ($mime_type) {
  507.  
  508. // use browser cache if available to speed up page load
  509. if (!empty ($_SERVER['HTTP_IF_MODIFIED_SINCE'])) {
  510. if (strtotime ($_SERVER['HTTP_IF_MODIFIED_SINCE']) < strtotime ('now')) {
  511. header ('HTTP/1.1 304 Not Modified');
  512. die ();
  513. }
  514. }
  515.  
  516. $cache_file = get_cache_file ($mime_type);
  517.  
  518. if (file_exists ($cache_file)) {
  519.  
  520. // change the modified headers
  521. $gmdate_expires = gmdate ('D, d M Y H:i:s', strtotime ('now +10 days')) . ' GMT';
  522. $gmdate_modified = gmdate ('D, d M Y H:i:s') . ' GMT';
  523.  
  524. // send content headers then display image
  525. header ('Content-Type: ' . $mime_type);
  526. header ('Accept-Ranges: bytes');
  527. header ('Last-Modified: ' . $gmdate_modified);
  528. header ('Content-Length: ' . filesize ($cache_file));
  529. header ('Cache-Control: max-age=' . CACHE_MAX_AGE . ', must-revalidate');
  530. header ('Expires: ' . $gmdate_expires);
  531.  
  532. if (!@readfile ($cache_file)) {
  533. $content = file_get_contents ($cache_file);
  534. if ($content != FALSE) {
  535. echo $content;
  536. } else {
  537. display_error ('cache file could not be loaded');
  538. }
  539. }
  540.  
  541. die ();
  542.  
  543. }
  544.  
  545. return FALSE;
  546.  
  547. }
  548.  
  549.  
  550. /**
  551. *
  552. * @staticvar string $cache_file
  553. * @param <type> $mime_type
  554. * @return string
  555. */
  556. function get_cache_file ($mime_type) {
  557.  
  558. static $cache_file;
  559. global $src;
  560.  
  561. $file_type = '.png';
  562.  
  563. if (strpos ($mime_type, 'jpeg') > 1) {
  564. $file_type = '.jpg';
  565. }
  566.  
  567. if (!$cache_file) {
  568. // filemtime is used to make sure updated files get recached
  569. $cache_file = DIRECTORY_CACHE . '/' . md5 ($_SERVER ['QUERY_STRING'] . VERSION . filemtime ($src)) . $file_type;
  570. }
  571.  
  572. return $cache_file;
  573.  
  574. }
  575.  
  576.  
  577. /**
  578. *
  579. * @param <type> $url
  580. * @return <type>
  581. */
  582. function validate_url ($url) {
  583. $pattern = "/\b(?:(?:https?):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i";
  584. return preg_match ($pattern, $url);
  585. }
  586.  
  587.  
  588. /**
  589. *
  590. * @global array $allowedSites
  591. * @param string $src
  592. * @return string
  593. */
  594. function check_external ($src) {
  595.  
  596. global $allowedSites;
  597.  
  598. // work out file details
  599. $fileDetails = pathinfo ($src);
  600. $filename = 'external_' . md5 ($src);
  601. // $local_filepath = DIRECTORY_CACHE . '/' . $filename . '.' . strtolower ($fileDetails['extension']);
  602.  
  603. // only do this stuff the file doesn't already exist
  604. if (!file_exists ($local_filepath)) {
  605.  
  606. if (strpos (strtolower ($src), 'http://') !== false || strpos (strtolower ($src), 'https://') !== false) {
  607.  
  608. if (!validate_url ($src)) {
  609. display_error ('invalid url');
  610. }
  611.  
  612. $url_info = parse_url ($src);
  613.  
  614. // convert youtube video urls
  615. // need to tidy up the code
  616.  
  617. if ($url_info['host'] == 'www.youtube.com' || $url_info['host'] == 'youtube.com') {
  618. parse_str ($url_info['query']);
  619.  
  620. if (isset ($v)) {
  621. $src = 'http://img.youtube.com/vi/' . $v . '/0.jpg';
  622. $url_info['host'] = 'img.youtube.com';
  623. }
  624. }
  625.  
  626. // check allowed sites (if required)
  627. if (ALLOW_EXTERNAL) {
  628.  
  629. $isAllowedSite = true;
  630.  
  631. } else {
  632.  
  633. $isAllowedSite = false;
  634. foreach ($allowedSites as $site) {
  635. if (strpos (strtolower ($url_info['host']), $site) !== false) {
  636. $isAllowedSite = true;
  637. }
  638. }
  639.  
  640. }
  641.  
  642. // if allowed
  643. if ($isAllowedSite) {
  644.  
  645. if (function_exists ('curl_init')) {
  646.  
  647. global $fh;
  648.  
  649. $fh = fopen ($local_filepath, 'w');
  650. $ch = curl_init ($src);
  651.  
  652. curl_setopt ($ch, CURLOPT_TIMEOUT, CURL_TIMEOUT);
  653. curl_setopt ($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7.5) Gecko/20041107 Firefox/1.0');
  654. curl_setopt ($ch, CURLOPT_URL, $src);
  655. curl_setopt ($ch, CURLOPT_RETURNTRANSFER, TRUE);
  656. curl_setopt ($ch, CURLOPT_HEADER, 0);
  657. curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
  658. curl_setopt ($ch, CURLOPT_FILE, $fh);
  659. curl_setopt ($ch, CURLOPT_WRITEFUNCTION, 'curl_write');
  660.  
  661. // error so die
  662. if (curl_exec ($ch) === FALSE) {
  663. unlink ($local_filepath);
  664. touch ($local_filepath);
  665. display_error ('error reading file ' . $src . ' from remote host: ' . curl_error ($ch));
  666. }
  667.  
  668. curl_close ($ch);
  669. fclose ($fh);
  670.  
  671. } else {
  672.  
  673. if (!$img = file_get_contents ($src)) {
  674. display_error ('remote file for ' . $src . ' can not be accessed. It is likely that the file permissions are restricted');
  675. }
  676.  
  677. if (file_put_contents ($local_filepath, $img) == FALSE) {
  678. display_error ('error writing temporary file');
  679. }
  680.  
  681. }
  682.  
  683. if (!file_exists ($local_filepath)) {
  684. display_error ('local file for ' . $src . ' can not be created');
  685. }
  686.  
  687. $src = $local_filepath;
  688.  
  689. } else {
  690.  
  691. display_error ('remote host "' . $url_info['host'] . '" not allowed');
  692.  
  693. }
  694.  
  695. }
  696.  
  697. } else {
  698.  
  699. $src = $local_filepath;
  700.  
  701. }
  702.  
  703. return $src;
  704.  
  705. }
  706.  
  707.  
  708. /**
  709. * callback for curl command to receive external images
  710. * limit the amount of data downloaded from external servers
  711. *
  712. * @global <type> $data_string
  713. * @param <type> $handle
  714. * @param <type> $data
  715. * @return <type>
  716. */
  717. function curl_write ($handle, $data) {
  718.  
  719. global $external_data_string, $fh;
  720.  
  721. fwrite ($fh, $data);
  722. $external_data_string .= $data;
  723.  
  724. if (strlen ($external_data_string) > MAX_FILE_SIZE) {
  725. return 0;
  726. } else {
  727. return strlen ($data);
  728. }
  729.  
  730. }
  731.  
  732.  
  733. /**
  734. * tidy up the image source url
  735. *
  736. * @param <type> $src
  737. * @return string
  738. */
  739. function clean_source ($src) {
  740.  
  741. $host = str_replace ('www.', '', $_SERVER['HTTP_HOST']);
  742. $regex = "/^(http(s|):\/\/)(www\.|)" . $host . "\//i";
  743.  
  744. $src = preg_replace ($regex, '', $src);
  745. $src = strip_tags ($src);
  746. $src = check_external ($src);
  747.  
  748. // remove slash from start of string
  749. if (strpos ($src, '/') === 0) {
  750. $src = substr ($src, -(strlen ($src) - 1));
  751. }
  752.  
  753. // don't allow users the ability to use '../'
  754. // in order to gain access to files below document root
  755. $src = preg_replace ("/\.\.+\//", "", $src);
  756.  
  757. // get path to image on file system
  758. $src = get_document_root ($src) . '/' . $src;
  759.  
  760. if (!is_file ($src)) {
  761. display_error ('source is not a valid file');
  762. }
  763.  
  764. if (filesize ($src) > MAX_FILE_SIZE) {
  765. display_error ('source file is too big (filesize > MAX_FILE_SIZE)');
  766. }
  767.  
  768. if (filesize ($src) <= 0) {
  769. display_error ('source file <= 0 bytes. Possible external file download error (file is too large)');
  770. }
  771.  
  772. return realpath ($src);
  773.  
  774. }
  775.  
  776.  
  777. /**
  778. *
  779. * @param <type> $src
  780. * @return string
  781. */
  782. function get_document_root ($src) {
  783.  
  784. // check for unix servers
  785. if (file_exists ($_SERVER['DOCUMENT_ROOT'] . '/' . $src)) {
  786. return $_SERVER['DOCUMENT_ROOT'];
  787. }
  788.  
  789. // check from script filename (to get all directories to timthumb location)
  790. $parts = array_diff (explode ('/', $_SERVER['SCRIPT_FILENAME']), explode ('/', $_SERVER['DOCUMENT_ROOT']));
  791.  
  792. $path = $_SERVER['DOCUMENT_ROOT'];
  793.  
  794. foreach ($parts as $part) {
  795. $path .= '/' . $part;
  796. if (file_exists ($path . '/' . $src)) {
  797. return $path;
  798. }
  799. }
  800.  
  801. // special check for microsoft servers
  802. if (!isset ($_SERVER['DOCUMENT_ROOT'])) {
  803. $path = str_replace ("/", "\\", $_SERVER['ORIG_PATH_INFO']);
  804. $path = str_replace ($path, '', $_SERVER['SCRIPT_FILENAME']);
  805.  
  806. if (file_exists ($path . '/' . $src)) {
  807. return $path;
  808. }
  809. }
  810.  
  811. display_error ('file not found');
  812.  
  813. }
  814.  
  815.  
  816. /**
  817. * generic error message
  818. *
  819. * @param <type> $errorString
  820. */
  821. function display_error ($errorString = '') {
  822.  
  823. header ('HTTP/1.1 400 Bad Request');
  824. echo '<pre>' . htmlentities ($errorString);
  825. echo '<br />Query String : ' . htmlentities ($_SERVER['QUERY_STRING']);
  826. echo '<br />TimThumb version : ' . VERSION . '</pre>';
  827. die ();
  828.  
  829. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement