PageRenderTime 1108ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/Quản lý website tin tức thời trang và cuộc sống PHP/code_goclamdep/wp-content/themes/VN-News/scripts/timthumb.php

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