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