Guest User

byrev-wp-picshield

a guest
Apr 6th, 2016
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 10.06 KB | None | 0 0
  1. <?php
  2. /*
  3. NOTE: If you use this code or part of this code in your program/scripts,
  4. I would be glad if you publish that using this script "ByRev-WP-PICShield" and the author's name: Emilian Robert Vicol.
  5. It is not an absolute obligation but prove you respect the work of others!
  6. */
  7. define('_GTFO_KEY','8T5e-PlQE');
  8.  
  9. if (!array_key_exists('key', $_GET) OR ($_GET['key'] != _GTFO_KEY) ){
  10.     header('HTTP/1.0 401 Unauthorized');
  11.     echo 'Unauthorized!';
  12.     die(401);
  13. }
  14.  
  15. define('_WATERMARK_ENABLED', true);
  16. define('_REDIRECT_DIRECT_LINK_IMAGES_FROM_GOOGLE', true);
  17. define('_HOTLINK_CACHE_FOLDER', '~hotlink-cache');
  18. define('_IMAGE_SOURCE_TRANSPARENCY', 65);
  19. define('_BLEND_BAR_WATERMARK', true);
  20. define('_BLEND_BAR_OPACITY', 25);
  21. define('_WRITE_HOST_SOURCE', true);
  22. define('_WATERMARK_POSITION', 0);
  23. define('_WRITE_CREDIT_PLUGIN', true);
  24. define('_MAXIMUM_MEGAPIXELS_SIZE', 8);
  25. define('_WATERMARK_PNG_FILE', 'watermark.png');
  26. define('_SEND_HOTLINK_GTFO_HEADER_SIGNATURE', false);
  27. define('_WRITE_TIME_CACHED_OVER_IMAGE', false);
  28. define('_PRINT_QR_HOST', false);
  29. define('_LOG_REFERER_AND_TARGET_IMG', false);
  30. define('_LOG_REFERER_TABLE', '** disabled **');
  31. define('_WATER_MARK_PASS_THROUGH', true);
  32. define('_WATER_MARK_REDIRECT_302_MODE', false);
  33. define('_WP_PICSHIELD_VERSION', '1.9.7');
  34. define('_WP_PICSHIELD_FORCE_WATERMARK', isset($_GET['fw']));
  35. define('_WP_PICSHIELD_FORCE_DEBUG', isset($_GET['fd']));
  36.  
  37. $image = strip_tags( $_GET['src'] );
  38.  
  39. if (_SEND_HOTLINK_GTFO_HEADER_SIGNATURE) {
  40.     header('X-Protect: ByREV WP-PICShield , HOTLINK Defence');
  41. }
  42.  
  43. #~~~~ headers = watermarked file NOT cache
  44. header("Cache-Control: no-cache, must-revalidate");
  45. header("Pragma: no-cache");
  46. header("Expires: Sat, 26 Jul 1997 05:00:00 GMT");
  47.  
  48. function _mydb_open($filename, $row_size) {
  49.     return array('handle'=>fopen($filename, "r+"), 'length_row'=>$row_size, 'filename'=>$filename);
  50. }
  51.  
  52. function _mydb_close($db) {
  53.     return fclose($db['handle']);
  54. }
  55.  
  56. function _mydb_write_row($db, $data) {
  57.     fseek($db['handle'], 0, SEEK_END);
  58.     fwrite($db['handle'], $data, $db['length_row']);
  59. }
  60.  
  61. function log_referer_and_target_img($redirect=false) {
  62. /*
  63.     Note: This facility is not yet officially launched, so you do not have access to save and view the logs for now,
  64.     because still working on the script.
  65.     TOS ~ 30 days. (publication of the first version: 15 ... 30 March)
  66. */
  67. }
  68.  
  69. #~~~~  REDIRECT DIRECT LINK IMAGES with GOOGLE IMAGES REFERRER
  70. global $request_trap;
  71. $request_trap = array();
  72.  
  73. $request_trap[] = array( 'HTTP_ACCEPT'=> array('#^image#i', true) );
  74. $request_trap[] = array( 'HTTP_REFERER'=> array('#google.(.*)/blank.html#i', true) );
  75.  
  76. function is_image_loading() {
  77. if (!array_key_exists('HTTP_REFERER', $_SERVER)) return true;
  78. global $request_trap;
  79.     foreach ($request_trap as $patterns):
  80.         $n_matches = 0;
  81.         foreach ($patterns as $key=>$pattern_info) :                       
  82.             if (isset($_SERVER[$key])) :
  83.                 $subject = $_SERVER[$key];
  84.                 $result = preg_match($pattern_info[0], $subject, $matches);
  85.                 $result = (!$pattern_info[1]) ? !$result : $result;
  86.                 if ($result) $n_matches++;
  87.             endif;
  88.         endforeach;        
  89.         if ($n_matches == count($patterns)) return true;
  90.     endforeach;
  91. return false;
  92. }
  93.  
  94. function is_image_click() {
  95.     return !is_image_loading();
  96. }
  97.  
  98. if (_REDIRECT_DIRECT_LINK_IMAGES_FROM_GOOGLE AND !_WP_PICSHIELD_FORCE_WATERMARK) :
  99.     if (is_image_click()) {
  100.         //log_referer_and_target_img(true);
  101.         require_once('byrev-wp-image2url.php');
  102.         #~~~ script end here; redirection!
  103.     }
  104. endif;
  105.  
  106. #~~~ log referer after redirect; so, no conversion;
  107. if (_LOG_REFERER_AND_TARGET_IMG) {
  108.     //log_referer_and_target_img(false);
  109. }
  110.  
  111. #~~~~ if watermak is disabled
  112. function img_get_mime_type($file)
  113. {
  114.     $mime_types = array( "gif"=>"image/gif", "png"=>"image/png", "jpeg"=>"image/jpg", "jpg"=>"image/jpg", );
  115.     $file_exp = explode('.',$file);
  116.     $extension = strtolower(end($file_exp));
  117.    
  118.     if (array_key_exists($extension, $mime_types)) {
  119.         return $mime_types[$extension];
  120.     }  
  121.     return "application/force-download";
  122. }
  123.  
  124. if (!_WATERMARK_ENABLED) {
  125.     $mime = img_get_mime_type($image);
  126.     header('Content-type: '.$mime);
  127.     fpassthru(fopen($image, 'rb'));
  128.     exit();
  129. }
  130.  
  131. #~~~~ ~~~~ ~~~~
  132.  
  133. $dir = dirname(__FILE__);
  134. $watermarked = $dir.'/'._HOTLINK_CACHE_FOLDER.'/'.$image;
  135.  
  136. #~~ create WATERMARK cache folders if not exists;
  137. $watermark_folder = dirname($watermarked);
  138. if(!is_dir($watermark_folder)) {
  139.     mkdir($watermark_folder, 0777, true);
  140. }
  141.  
  142. function byrev_image_type_to_extension($imagetype) {
  143.    if(empty($imagetype)) return false;
  144.    switch($imagetype) {
  145.        case IMAGETYPE_GIF    : return 'gif';
  146.        case IMAGETYPE_JPEG    : return 'jpeg';
  147.        case IMAGETYPE_PNG    : return 'png';
  148.        case IMAGETYPE_XBM    : return 'xbm';
  149.        case IMAGETYPE_WBMP    : return 'wbmp';
  150.        default                : return false;
  151.    }
  152. }
  153.  
  154. function get_qr_data($dest_img_x, $dest_img_y, $text) {
  155.     #~~~ calculate and set maxim qr size;
  156.     //$sizex = $dest_img_x >> 3;
  157.     $sizex = round($dest_img_x / 6);
  158.     if ( ($sizex*$sizex) > 300000) { $sizex = 500; }
  159.    
  160.     $size          = $sizex.'x'.$sizex;
  161.     $ret['qr-url'] = $rootUrl = "http://chart.googleapis.com/chart?cht=qr&chs=$size&chl=$text&choe=UTF-8&chld=H|1";
  162.    
  163.     $ret['x-merge'] = ($dest_img_x - $sizex) - 10;
  164.     $ret['y-merge'] = ($dest_img_y - $sizex) - 10;
  165.     $ret['size-xy'] = $sizex;
  166.    
  167.     return $ret;
  168. }
  169.  
  170. #~~ if file exist, is serverd from disk, else is generated only once;
  171. if(file_exists($image)&& !file_exists($watermarked)){
  172.     #~~~ image type
  173.     $image_size = getimagesize($image);
  174.  
  175.     $image_mime = $image_size['mime'];
  176.    
  177.     $image_type = $image_size[2];
  178.     $image_extension = byrev_image_type_to_extension($image_type);
  179.    
  180.     #~~~ invalid file-type; "print" file and exit!
  181.     if ($image_extension === FALSE) {
  182.         $mime = img_get_mime_type($image);
  183.         header('Content-type: '.$mime);
  184.         fpassthru(fopen($image, 'rb'));
  185.         exit();
  186.     }
  187.    
  188.     #~~~ ok, valid file, continue ..   
  189.     $imagecreatefrom = 'imagecreatefrom'.$image_extension;
  190.     if (!function_exists($imagecreatefrom)) {
  191.         header("Content-type: {$image_mime}");
  192.         fpassthru(fopen($image, 'rb'));
  193.         exit;
  194.     }  
  195.        
  196.    
  197.     $sizex = $image_size[0];
  198.     $sizey = $image_size[1];   
  199.    
  200.     #~~ file si to big, may not be enough memory ... / exit
  201.     $max_size = _MAXIMUM_MEGAPIXELS_SIZE * 1048576;
  202.     if (($sizex*$sizey) > $max_size) {
  203.         header("Content-type: {$image_mime}");
  204.         fpassthru(fopen($image, 'rb'));
  205.         exit;
  206.     }
  207.        
  208.     $_font_size = ($sizex < 480) ? 2 : (($sizex < 640)? 3 : 5);
  209.  
  210.     //~~~ create images;
  211.     $photo = $imagecreatefrom($image);
  212.    
  213.     $sizex = imagesx($photo);
  214.     $sizey = imagesy($photo);  
  215.    
  216.     $watermark = imagecreatefrompng(_WATERMARK_PNG_FILE);
  217.     imagealphablending($photo, true);
  218.    
  219.     $w = imagesx($watermark);
  220.     $h = imagesy($watermark);
  221.  
  222.     $percent = $sizex / (($w>$h)?$w:$h);
  223.     $nw = floor($w*$percent);
  224.     $nh = floor($h*$percent);
  225.  
  226.     switch (_WATERMARK_POSITION) {
  227.         case 0: $_watermark_ypos = 10; break;
  228.         case 1: $_watermark_ypos = round( ($sizey/2) - ($nh/2) ) ; break;
  229.         case 2: $_watermark_ypos = $sizey - $nh - 40; break;
  230.     }  
  231.    
  232.     #~~~ set original image source opacity
  233.     $opacity_img = imagecreatetruecolor($sizex, $sizey);
  234.     imagecopymerge($photo, $opacity_img, 0, 0, 0, 0, $sizex, $sizey, _IMAGE_SOURCE_TRANSPARENCY);
  235.  
  236.     #~~~ set higher opacity band
  237.     if (_BLEND_BAR_WATERMARK) :
  238.         $opacity_img = imagecreatetruecolor($nw, $nh);
  239.         imagecopymerge($photo, $opacity_img, 0, $_watermark_ypos, 0, 0, $nw, $nh, _BLEND_BAR_OPACITY); 
  240.     endif;
  241.     imagedestroy($opacity_img);
  242.  
  243.     #~~~ resize watermak with great deal of clarity
  244.     $image_p = imagecreatetruecolor($nw, $nh);
  245.     ImageAlphaBlending($image_p,false);
  246.     ImageSaveAlpha($image_p,true);
  247.     imagecopyresampled($image_p, $watermark, 0, 0, 0, 0, $nw, $nh, $w, $h);
  248.     imagedestroy($watermark);
  249.        
  250.     #~~~ put watermak over image source
  251.     imagecopy($photo, $image_p, 0, $_watermark_ypos, 0, 0, $nw, $nh);
  252.     imagedestroy($image_p);
  253.  
  254.     #~~~ write credit over hotlinked image
  255.     if (_WRITE_CREDIT_PLUGIN):
  256.         $textcolor = imagecolorallocate($photo, 128, 128, 128);
  257.         imagestring($photo, $_font_size, 5, $sizey-17, 'Protected by: ByREV WP-PICShield - HOTLINK Defence', $textcolor);  
  258.         $pos_x_host_source = 35;
  259.     else:
  260.         $pos_x_host_source = 20;
  261.     endif;
  262.    
  263.     $_site_url = 'http://'. $_SERVER['SERVER_NAME'];
  264.    
  265.     #~~~ write image source    
  266.     $_strimg = "";
  267.     if (_WRITE_HOST_SOURCE):
  268.         $_strimg .= $_site_url;
  269.     endif;
  270.    
  271.     if (_WRITE_TIME_CACHED_OVER_IMAGE):
  272.         $_strimg .= ' - '.date("M.j.Y/g:i a");
  273.     endif;
  274.    
  275.     if ($_strimg != ""):
  276.         $textcolor = imagecolorallocate($photo, 192, 192, 192);
  277.         imagestring($photo, $_font_size, 5, $sizey - $pos_x_host_source, $_strimg, $textcolor);
  278.     endif;  
  279.    
  280.     #~~~ print qr code
  281.     if (_PRINT_QR_HOST) :
  282.         $qr_info = get_qr_data($sizex, $sizey, $_site_url);
  283.        
  284.         $src = imagecreatefrompng($qr_info['qr-url']);
  285.         imagealphablending($src, true);
  286.        
  287.         imagecopymerge($photo, $src, $qr_info['x-merge'], $qr_info['y-merge'], 0, 0, $qr_info['size-xy'], $qr_info['size-xy'], 30);
  288.         imagedestroy($src);        
  289.     endif;
  290.    
  291.     #~~~~ for spy result in serach engines !!!
  292.     if (_WP_PICSHIELD_FORCE_DEBUG) :
  293.         $padding = 110;
  294.         $y = $padding;
  295.         $w = imagecolorallocate($photo, 255, 255, 255);
  296.         $y = $padding;
  297.         foreach ($_SERVER as $key=>$_TXT) :
  298.             if (is_array($_TXT)) $_TXT = serialize($_TXT);
  299.             imagestring($photo, 2, 10, $y,  $key.': '.$_TXT, $w);
  300.             $y += 14;
  301.         endforeach;
  302.     endif;
  303.     #~~~~  
  304.  
  305.     // Output to the browser
  306.     imagejpeg($photo, $watermarked, 75);
  307.     imagedestroy($photo);
  308. }
  309.  
  310. if (_WATER_MARK_PASS_THROUGH):
  311.     header('Content-type: image/jpeg');
  312.     fpassthru(fopen($watermarked, 'rb'));
  313. else:
  314.     $new_url = 'http'.(empty($_SERVER['HTTPS'])?'':'s').'://'.$_SERVER['SERVER_NAME'].'/'._HOTLINK_CACHE_FOLDER.$_SERVER['REQUEST_URI'];
  315.     if (_WATER_MARK_REDIRECT_302_MODE) {
  316.         header("HTTP/1.1 302 Found");
  317.         $response = "";
  318.     } else {
  319.         header("HTTP/1.1 307 Temporary Redirect");
  320.         $response = 'This object may be found <a href="'.$new_url.'">here</a>';
  321.     }
  322.     header("Cache-Control: no-cache, must-revalidate");
  323.     header("Pragma: no-cache");
  324.     header("Expires: Sat, 26 Jul 1997 05:00:00 GMT");
  325.     header("Location: ". $new_url);
  326.     if ($response !="") echo $response;
  327. endif;
  328. ?>
Add Comment
Please, Sign In to add comment