Advertisement
Vodkaholic

Untitled

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