/wp-content/plugins/magic-fields-2/thirdparty/phpthumb/phpThumb.php

https://bitbucket.org/rossberenson/michael-karas · PHP · 118 lines · 89 code · 16 blank · 13 comment · 13 complexity · 5beb08d1b3db999b7494881c1d12ab61 MD5 · raw file

  1. <?php
  2. ob_start();
  3. //use wp-load. Normally right here, but if it's not...
  4. if( file_exists('../../../../../wp-load.php')){
  5. require_once('../../../../../wp-load.php');
  6. $loaded = true;
  7. } elseif( file_exists( dirname(__FILE__).'/../../mf-config.php')){
  8. include_once(dirname(__FILE__).'/../../mf-config.php');
  9. include_once('./mf-config.php');
  10. require_once(MF_WP_LOAD);
  11. $loaded = true;
  12. }
  13. if($loaded !== true){
  14. die('Could not load wp-load.php, edit/add mf-config.php and define MF_WP_LOAD to point to a valid wp-load file');
  15. }
  16. ob_end_clean();
  17. $MFthumb = MF_PATH.'/MF_thumb.php';
  18. require_once($MFthumb);
  19. //Default Values
  20. $default = array(
  21. 'iar' => 0,
  22. 'far' => 0,
  23. 'zc' => 1,
  24. 'q' => 95,
  25. 'w' => 0,
  26. 'h' => 0,
  27. 'src' => ''
  28. );
  29. //getting the name of the image
  30. $content_dir = str_replace('/', '\/', str_replace( ABSPATH, '', WP_CONTENT_DIR ));
  31. preg_match('/\/' .$content_dir .'\/([0-9\_a-z\/\-\.]+\.(jpg|jpeg|png|gif))/i',$_GET['src'],$match);
  32. $image_name_clean = $match[1];
  33. $extension = $match[2];
  34. //is wp mu o wp network
  35. if(isset($current_blog)){
  36. $image_name_clean = preg_replace('/blogs.dir\/(\d+)\//','',$image_name_clean);
  37. }
  38. //Getting the original size of the image
  39. if( preg_match('/'.MF_FILES_NAME.'/',$image_name_clean) ){
  40. $image_name_clean = preg_replace('/'.MF_FILES_NAME.'\//','',$image_name_clean);
  41. $file = MF_FILES_DIR.$image_name_clean;
  42. }else{
  43. $file = WP_CONTENT_DIR.DS.$image_name_clean;
  44. }
  45. if(file_exists($file) && (empty($_GET['w']) || empty($_GET['h']))){
  46. $size = @getimagesize($file);
  47. $default['w'] = $size[0];
  48. $default['h'] = $size[1];
  49. }
  50. //TODO: sanitize the variables
  51. $params = array();
  52. foreach($_GET as $key => $value){
  53. if(in_array($key,array('zc','w','h','q','src','far','iar'))){
  54. $params[$key] = $value;
  55. }
  56. }
  57. $params = array_merge($default,$params);
  58. $md5_params = md5("w=".$params['w']."&h=".$params['h']."&q=".$params['q']."&zc=".$params['zc']."&far=".$params['far']."&iar=".$params['iar']);
  59. //The file must be "jpg" or "png" or "gif"
  60. if(!in_array(strtolower($extension),array('jpg','jpeg','png','gif'))){
  61. return false;
  62. }
  63. //
  64. $image_sin = preg_split('/\//',$image_name_clean);
  65. $new_image_clean = $image_sin[count($image_sin)-1];
  66. //name with a png extension
  67. $image_name = $md5_params."_".$new_image_clean;
  68. //this code can be refactored
  69. if(file_exists(MF_CACHE_DIR.$image_name)){
  70. //Displaying the image
  71. $size = getimagesize(MF_CACHE_DIR.$image_name);
  72. $handle = fopen(MF_CACHE_DIR.$image_name, "rb");
  73. $contents = NULL;
  74. while (!feof($handle)) {
  75. $contents .= fread($handle, 1024);
  76. }
  77. fclose($handle);
  78. header("Cache-Control: public");
  79. header ("Content-type: image/".$extension);
  80. header("Content-Disposition: inline; filename=\"".MF_CACHE_DIR.$image_name."\"");
  81. header('Content-Length: ' . filesize(MF_CACHE_DIR.$image_name));
  82. echo $contents;
  83. }else{
  84. //generating the image
  85. $thumb = new mfthumb();
  86. $thumb_path = $thumb->image_resize($file,$params['w'],$params['h'],$params['zc'],$params['far'],$params['iar'],MF_CACHE_DIR.$image_name);
  87. //Displaying the image
  88. if(file_exists($thumb_path)){
  89. $size = getimagesize($thumb_path);
  90. $handle = fopen($thumb_path, "rb");
  91. $contents = NULL;
  92. while (!feof($handle)) {
  93. $contents .= fread($handle, filesize($thumb_path));
  94. }
  95. fclose($handle);
  96. header("Cache-Control: public");
  97. header ("Content-type: image/".$extension);
  98. header("Content-Disposition: inline; filename=\"".$thumb_path."\"");
  99. header('Content-Length: ' . filesize($thumb_path));
  100. echo $contents;
  101. }
  102. }