PageRenderTime 1832ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/tools/wikiportrait/inc/timthumb.php

https://github.com/ChuguluGames/mediawiki-svn
PHP | 392 lines | 212 code | 123 blank | 57 comment | 44 complexity | 05a903bc7cfdcc51e95e424f5e87d2e0 MD5 | raw file
  1. <?php
  2. // TimThumb script created by Tim McDaniels and Darren Hoyt with tweaks by Ben Gillbanks
  3. // http://code.google.com/p/timthumb/
  4. // MIT License: http://www.opensource.org/licenses/mit-license.php
  5. /* Parameters allowed: */
  6. // w: width
  7. // h: height
  8. // zc: zoom crop (0 or 1)
  9. // q: quality (default is 75 and max is 100)
  10. // HTML example: <img src="/scripts/timthumb.php?src=/images/whatever.jpg&w=150&h=200&zc=1" alt="" />
  11. error_reporting( E_ALL );
  12. if ( !isset( $_REQUEST["src"] ) ) {
  13. die( "no image specified" );
  14. }
  15. // clean params before use
  16. $src = clean_source( $_REQUEST[ "src" ] );
  17. // set document root
  18. $doc_root = get_document_root( $src );
  19. // get path to image on file system
  20. $src = $doc_root . '/' . $src;
  21. $new_width = preg_replace( "/[^0-9]+/", "", get_request( 'w', 100 ) );
  22. $new_height = preg_replace( "/[^0-9]+/", "", get_request( 'h', 100 ) );
  23. $zoom_crop = preg_replace( "/[^0-9]+/", "", get_request( 'zc', 1 ) );
  24. $quality = preg_replace( "/[^0-9]+/", "", get_request( '9', 80 ) );
  25. // set path to cache directory (default is ./cache)
  26. // this can be changed to a different location
  27. $cache_dir = '../cache';
  28. // get mime type of src
  29. $mime_type = mime_type( $src );
  30. // check to see if this image is in the cache already
  31. // check_cache($cache_dir, $mime_type);
  32. // make sure that the src is gif/jpg/png
  33. if ( !valid_src_mime_type( $mime_type ) ) {
  34. die( "Invalid src mime type: $mime_type" );
  35. }
  36. // check to see if GD function exist
  37. if ( !function_exists( 'imagecreatetruecolor' ) ) {
  38. die( "GD Library Error: imagecreatetruecolor does not exist" );
  39. }
  40. if ( strlen( $src ) && file_exists( $src ) ) {
  41. // open the existing image
  42. $image = open_image( $mime_type, $src );
  43. if ( $image === false ) {
  44. die( 'Unable to open image : ' . $src );
  45. }
  46. // Get original width and height
  47. $width = imagesx( $image );
  48. $height = imagesy( $image );
  49. // don't allow new width or height to be greater than the original
  50. if ( $new_width > $width ) {
  51. $new_width = $width;
  52. }
  53. if ( $new_height > $height ) {
  54. $new_height = $height;
  55. }
  56. // generate new w/h if not provided
  57. if ( $new_width && !$new_height ) {
  58. $new_height = $height * ( $new_width / $width );
  59. } elseif ( $new_height && !$new_width ) {
  60. $new_width = $width * ( $new_height / $height );
  61. } elseif ( !$new_width && !$new_height ) {
  62. $new_width = $width;
  63. $new_height = $height;
  64. }
  65. // create a new true color image
  66. $canvas = imagecreatetruecolor( $new_width, $new_height );
  67. if ( $zoom_crop ) {
  68. $src_x = $src_y = 0;
  69. $src_w = $width;
  70. $src_h = $height;
  71. $cmp_x = $width / $new_width;
  72. $cmp_y = $height / $new_height;
  73. // calculate x or y coordinate and width or height of source
  74. if ( $cmp_x > $cmp_y ) {
  75. $src_w = round( ( $width / $cmp_x * $cmp_y ) );
  76. $src_x = round( ( $width - ( $width / $cmp_x * $cmp_y ) ) / 2 );
  77. } elseif ( $cmp_y > $cmp_x ) {
  78. $src_h = round( ( $height / $cmp_y * $cmp_x ) );
  79. $src_y = round( ( $height - ( $height / $cmp_y * $cmp_x ) ) / 2 );
  80. }
  81. imagecopyresampled( $canvas, $image, 0, 0, $src_x, $src_y, $new_width, $new_height, $src_w, $src_h );
  82. } else {
  83. // copy and resize part of an image with resampling
  84. imagecopyresampled( $canvas, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height );
  85. }
  86. // output image to browser based on mime type
  87. show_image( $mime_type, $canvas, $quality, $cache_dir );
  88. // remove image from memory
  89. imagedestroy( $canvas );
  90. } else {
  91. if ( strlen( $src ) ) {
  92. die( $src . ' not found.' );
  93. } else {
  94. die( 'no source specified.' );
  95. }
  96. }
  97. function show_image( $mime_type, $image_resized, $quality, $cache_dir ) {
  98. // check to see if we can write to the cache directory
  99. $is_writable = 0;
  100. $cache_file_name = $cache_dir . '/' . get_cache_file();
  101. if ( touch( $cache_file_name ) ) {
  102. // give 666 permissions so that the developer
  103. // can overwrite web server user
  104. chmod( $cache_file_name, 0666 );
  105. $is_writable = 1;
  106. } else {
  107. $cache_file_name = NULL;
  108. header( 'Content-type: ' . $mime_type );
  109. }
  110. if ( stristr( $mime_type, 'gif' ) ) {
  111. imagegif( $image_resized, $cache_file_name );
  112. } elseif ( stristr( $mime_type, 'jpeg' ) ) {
  113. imagejpeg( $image_resized, $cache_file_name, $quality );
  114. } elseif ( stristr( $mime_type, 'png' ) ) {
  115. $quality = floor( $quality * 0.09 );
  116. imagepng( $image_resized, $cache_file_name, $quality );
  117. }
  118. if ( $is_writable ) {
  119. show_cache_file( $cache_dir, $mime_type );
  120. }
  121. die();
  122. }
  123. function get_request( $property, $default = 0 ) {
  124. if ( isset( $_REQUEST[$property] ) ) {
  125. return $_REQUEST[$property];
  126. } else {
  127. return $default;
  128. }
  129. }
  130. function open_image( $mime_type, $src ) {
  131. if ( stristr( $mime_type, 'gif' ) ) {
  132. $image = imagecreatefromgif( $src );
  133. } elseif ( stristr( $mime_type, 'jpeg' ) ) {
  134. @ini_set( 'gd.jpeg_ignore_warning', 1 );
  135. $image = imagecreatefromjpeg( $src );
  136. } elseif ( stristr( $mime_type, 'png' ) ) {
  137. $image = imagecreatefrompng( $src );
  138. }
  139. return $image;
  140. }
  141. function mime_type( $file ) {
  142. $os = strtolower( php_uname() );
  143. $mime_type = '';
  144. // use PECL fileinfo to determine mime type
  145. if ( function_exists( 'finfo_open' ) ) {
  146. $finfo = finfo_open( FILEINFO_MIME );
  147. $mime_type = finfo_file( $finfo, $file );
  148. finfo_close( $finfo );
  149. }
  150. // try to determine mime type by using unix file command
  151. // this should not be executed on windows
  152. if ( !valid_src_mime_type( $mime_type ) && !( eregi( 'windows', $os ) ) ) {
  153. if ( preg_match( "/freebsd|linux/", $os ) ) {
  154. $mime_type = trim( @shell_exec( 'file -bi $file' ) );
  155. }
  156. }
  157. // use file's extension to determine mime type
  158. if ( !valid_src_mime_type( $mime_type ) ) {
  159. // set defaults
  160. $mime_type = 'image/jpeg';
  161. // file details
  162. $fileDetails = pathinfo( $file );
  163. $ext = strtolower( $fileDetails["extension"] );
  164. // mime types
  165. $types = array(
  166. 'jpg' => 'image/jpeg',
  167. 'jpeg' => 'image/jpeg',
  168. 'png' => 'image/png',
  169. 'gif' => 'image/gif'
  170. );
  171. if ( strlen( $ext ) && strlen( $types[$ext] ) ) {
  172. $mime_type = $types[$ext];
  173. }
  174. }
  175. return $mime_type;
  176. }
  177. function valid_src_mime_type( $mime_type ) {
  178. if ( preg_match( "/jpg|jpeg|gif|png/i", $mime_type ) ) {
  179. return true;
  180. }
  181. return false;
  182. }
  183. function check_cache( $cache_dir, $mime_type ) {
  184. // make sure cache dir exists
  185. if ( !file_exists( $cache_dir ) ) {
  186. // give 777 permissions so that developer can overwrite
  187. // files created by web server user
  188. mkdir( $cache_dir );
  189. chmod( $cache_dir, 0777 );
  190. }
  191. show_cache_file( $cache_dir, $mime_type );
  192. }
  193. function show_cache_file( $cache_dir, $mime_type ) {
  194. $cache_file = $cache_dir . '/' . get_cache_file();
  195. if ( file_exists( $cache_file ) ) {
  196. if ( isset( $_SERVER[ "HTTP_IF_MODIFIED_SINCE" ] ) ) {
  197. // check for updates
  198. $if_modified_since = preg_replace( '/;.*$/', '', $_SERVER[ "HTTP_IF_MODIFIED_SINCE" ] );
  199. $gmdate_mod = gmdate( 'D, d M Y H:i:s', filemtime( $cache_file ) );
  200. if ( strstr( $gmdate_mod, 'GMT' ) ) {
  201. $gmdate_mod .= " GMT";
  202. }
  203. if ( $if_modified_since == $gmdate_mod ) {
  204. header( "HTTP/1.1 304 Not Modified" );
  205. exit;
  206. }
  207. }
  208. $fileSize = filesize( $cache_file );
  209. // send headers then display image
  210. header( "Content-Type: " . $mime_type );
  211. // header("Accept-Ranges: bytes");
  212. header( "Last-Modified: " . gmdate( 'D, d M Y H:i:s', filemtime( $cache_file ) ) . " GMT" );
  213. header( "Content-Length: " . $fileSize );
  214. header( "Cache-Control: max-age=9999, must-revalidate" );
  215. header( "Expires: " . gmdate( "D, d M Y H:i:s", time() + 9999 ) . "GMT" );
  216. readfile( $cache_file );
  217. die();
  218. }
  219. }
  220. function get_cache_file () {
  221. global $quality;
  222. static $cache_file;
  223. if ( !$cache_file ) {
  224. $frags = split( "\.", $_REQUEST['src'] );
  225. $ext = strtolower( $frags[ count( $frags ) - 1 ] );
  226. if ( !valid_extension( $ext ) ) { $ext = 'jpg'; }
  227. $cachename = get_request( 'src', 'timthumb' ) . get_request( 'w', 100 ) . get_request( 'h', 100 ) . get_request( 'zc', 1 ) . get_request( '9', 80 );
  228. $cache_file = md5( $cachename ) . '.' . $ext;
  229. }
  230. return $cache_file;
  231. }
  232. function valid_extension ( $ext ) {
  233. if ( preg_match( "/jpg|jpeg|png|gif/i", $ext ) ) return 1;
  234. return 0;
  235. }
  236. function clean_source ( $src ) {
  237. // remove http/ https/ ftp
  238. $src = preg_replace( "/^((ht|f)tp(s|):\/\/)/i", "", $src );
  239. // remove domain name from the source url
  240. $host = $_SERVER["HTTP_HOST"];
  241. $src = str_replace( $host, "", $src );
  242. $host = str_replace( "www.", "", $host );
  243. $src = str_replace( $host, "", $src );
  244. // $src = preg_replace( "/(?:^\/+|\.{2,}\/+?)/", "", $src );
  245. // $src = preg_replace( '/^\w+:\/\/[^\/]+/', '', $src );
  246. // don't allow users the ability to use '../'
  247. // in order to gain access to files below document root
  248. // src should be specified relative to document root like:
  249. // src=images/img.jpg or src=/images/img.jpg
  250. // not like:
  251. // src=../images/img.jpg
  252. $src = preg_replace( "/\.\.+\//", "", $src );
  253. return $src;
  254. }
  255. function get_document_root ( $src ) {
  256. if ( @file_exists( $_SERVER['DOCUMENT_ROOT'] . '/' . $src ) ) {
  257. return $_SERVER['DOCUMENT_ROOT'];
  258. }
  259. // the relative paths below are useful if timthumb is moved outside of document root
  260. // specifically if installed in wordpress themes like mimbo pro:
  261. // /wp-content/themes/mimbopro/scripts/timthumb.php
  262. $paths = array( '..', '../..', '../../..', '../../../..' );
  263. foreach ( $paths as $path ) {
  264. if ( @file_exists( $path . '/' . $src ) ) {
  265. return $path;
  266. }
  267. }
  268. }
  269. ?>