php实现的网络相册图片防盗链完美破解方法
程序员文章站
2022-04-17 18:36:52
本文实例讲述了php实现的网络相册图片防盗链完美破解方法。分享给大家供大家参考。具体如下:
网络相册图片防盗链破解程序 - php版 这个防盗链破解版可以完美破解当下比较...
本文实例讲述了php实现的网络相册图片防盗链完美破解方法。分享给大家供大家参考。具体如下:
网络相册图片防盗链破解程序 - php版 这个防盗链破解版可以完美破解当下比较流行的: 百度相册,网易相册,360我喜欢等网站图片. 还可以实现简单的图片防盗链. 因为这个类是先进行获取远程图片, 然后再把图片发送到客户端,所以,算是进行了两次流量的传送.因此,会浪费空间流量,接下来,会开发缓存功能,这样可以实现节约流量!
<?php /** * 网络相册图片防盗链破解程序 - php版 * * 使用方法: * * http://yourdomain/url.php?url=http://hiphotos.baidu.com/verdana/pic/item/baidupicture.jpg&referer= * 其中url是指需要破解的图片url,而referer是为了兼容一些不需要设置来路域名才能显示的相册,例如360我喜欢网,必须设置来路为空才能正常浏览. 所以,此时应该设置referer为1 * * @author 雪狐博客 * @version 1.0 * @since july 16, 2012 * @url http://www.xuehuwang.com */ class frivoller { /** * http 版本号 (1.0, 1.1) , 百度使用的是 version 1.1 * * @var string */ protected $version; /** * 进行http请求后响应的数据 * * @var 字符串格式 */ protected $body; /** * 需要获取的远程url * * @var 字符串格式 */ protected $link; /** * an array that containing any of the various components of the url. * * @var array */ protected $components; /** * http请求时host数据 * * @var 字符串 */ protected $host; /** * the path of required file. * (e.g. '/verdana/abpic/item/mygirl.png') * * @var string */ protected $path; /** * the http referer, extra it from original url * * @var string */ protected $referer; /** * the http method, 'get' for default * * @var string */ protected $method = 'get'; /** * the http port, 80 for default * * @var int */ protected $port = 80; /** * timeout period on a stream * * @var int */ protected $timeout = 100; /** * the filename of image * * @var string */ protected $filename; /** * the contenttype of image file. * image/jpeg, image/gif, image/png, image * * @var string */ protected $contenttype; /** * frivoller constructor * * @param string $link */ public function __construct($link,$referer='') { $this->referer = $referer; // parse the http link $this->parselink($link); // begin to fetch the image $stream = pfsockopen($this->host, $this->port, $errno, $errstr, $this->timeout); if (!$stream){ header("content-type: $this->contenttype;"); echo $this->curlget($link); }else{ fwrite($stream, $this->buildheaders()); $this->body = ""; $img_size = get_headers($link,true); while (!feof($stream)) { $this->body .= fgets($stream, $img_size['content-length']); //fwrite($jpg,fread($stream, $img_size['content-length'])); } $content = explode("\r\n\r\n", $this->body, 2); $this->body = $content[1]; fclose($stream); // send 'contenttype' header for saving this file correctly // 如果不发送ct,则在试图保存图片时,ie7 会发生错误 (800700de) // flock, firefox 则没有这个问题,opera 没有测试 header("content-type: $this->contenttype;"); header("cache-control: max-age=315360000"); echo $this->body; //保存图片 //file_put_contents('hello.jpg', $this->body); } } /** * compose http request header * * @return string */ private function buildheaders() { $request = "$this->method $this->path http/1.1\r\n"; $request .= "host: $this->host\r\n"; $request .= "accept-encoding: gzip, deflate\r\n"; $request .= "user-agent: mozilla/5.0 (windows; u; windows nt 6.0; zh-cn; rv:1.9.0.1) gecko/2008070208 firefox/3.0.1\r\n"; $request .= "content-type: image/jpeg\r\n"; $request .= "accept: */*\r\n"; $request .= "keep-alive: 300\r\n"; $request .= "referer: $this->referer\r\n"; $request .= "cache-control: max-age=315360000\r\n"; $request .= "connection: close\r\n\r\n"; return $request; } /** * strip initial header and filesize info */ private function extractbody(&$body) { // the status of link if(strpos($body, '200 ok') > 0) { // strip header $endpos = strpos($body, "\r\n\r\n"); $body = substr($body, $endpos + 4); // strip filesize at nextline $body = substr($body, strpos($body, "\r\n") + 2); } } /** * extra the http url * * @param $link */ private function parselink($link) { $this->link = $link; $this->components = parse_url($this->link); $this->host = $this->components['host']; $this->path = $this->components['path']; if(empty($this->referer)){ $this->referer = $this->components['scheme'] . '://' . $this->components['host']; }elseif($this->referer == '1'){ $this->referer = ''; } $this->filename = basename($this->path); // extract the content type $ext = substr(strrchr($this->path, '.'), 1); if ($ext == 'jpg' or $ext == 'jpeg') { $this->contenttype = 'image/pjpeg'; } elseif ($ext == 'gif') { $this->contenttype = 'image/gif'; } elseif ($ext == 'png') { $this->contenttype = 'image/x-png'; } elseif ($ext == 'bmp') { $this->contenttype = 'image/bmp'; } else { $this->contenttype = 'application/octet-stream'; } } //抓取网页内容 function curlget($url){ $url = str_replace('&','&',$url); $curl = curl_init(); curl_setopt($curl, curlopt_url, $url); curl_setopt($curl, curlopt_header, false); curl_setopt($curl, curlopt_referer,$url); curl_setopt($curl, curlopt_useragent, "mozilla/4.0 (compatible; msie 6.0; seaport/1.2; windows nt 5.1; sv1; infopath.2)"); curl_setopt($curl, curlopt_cookiejar, 'cookie.txt'); curl_setopt($curl, curlopt_cookiefile, 'cookie.txt'); curl_setopt($curl, curlopt_returntransfer, 1); curl_setopt($curl, curlopt_followlocation, 0); $values = curl_exec($curl); curl_close($curl); return $values; } } /** * 取得根域名 * * @author lonely * @create 2011-3-11 * @version 0.11 * @lastupdate lonely * @package sl */ class rootdomain{ private static $self; private $domain=null; private $host=null; private $state_domain; private $top_domain; /** * 取得域名分析实例 * enter description here ... */ public static function instace(){ if(!self::$self) self::$self=new self(); return self::$self; } public function __construct(){ $this->state_domain=array( 'al','dz','af','ar','ae','aw','om','az','eg','et','ie','ee','ad','ao','ai','ag','at','au','mo','bb','pg','bs','pk','py','ps','bh','pa','br','by','bm','bg','mp','bj','be','is','pr','ba','pl','bo','bz','bw','bt','bf','bi','bv','kp','gq','dk','de','tl','tp','tg','dm','do','ru','ec','er','fr','fo','pf','gf','tf','va','ph','fj','fi','cv','fk','gm','cg','cd','co','cr','gg','gd','gl','ge','cu','gp','gu','gy','kz','ht','kr','nl','an','hm','hn','ki','dj','kg','gn','gw','ca','gh','ga','kh','cz','zw','cm','qa','ky','km','ci','kw','cc','hr','ke','ck','lv','ls','la','lb','lt','lr','ly','li','re','lu','rw','ro','mg','im','mv','mt','mw','my','ml','mk','mh','mq','yt','mu','mr','us','um','as','vi','mn','ms','bd','pe','fm','mm','md','ma','mc','mz','mx','nr','np','ni','ne','ng','nu','no','nf','na','za','aq','gs','eu','pw','pn','pt','jp','se','ch','sv','ws','yu','sl','sn','cy','sc','sa','cx','st','sh','kn','lc','sm','pm','vc','lk','sk','si','sj','sz','sd','sr','sb','so','tj','tw','th','tz','to','tc','tt','tn','tv','tr','tm','tk','wf','vu','gt','ve','bn','ug','ua','uy','uz','es','eh','gr','hk','sg','nc','nz','hu','sy','jm','am','ac','ye','iq','ir','il','it','in','id','uk','vg','io','jo','vn','zm','je','td','gi','cl','cf','cn','yr' ); $this->top_domain=array('com','arpa','edu','gov','int','mil','net','org','biz','info','pro','name','museum','coop','aero','xxx','idv','me','mobi'); $this->url=$_server['http_host']; } /** * 设置url * enter description here ... * @param string $url */ public function seturl($url=null){ $url=$url?$url:$this->url; if(empty($url))return $this; if(!preg_match("/^http:/is", $url)) $url="http://".$url; $url=parse_url(strtolower($url)); $urlarr=explode(".", $url['host']); $count=count($urlarr); if ($count<=2){ $this->domain=$url['host']; }else if ($count>2){ $last=array_pop($urlarr); $last_1=array_pop($urlarr); if(in_array($last, $this->top_domain)){ $this->domain=$last_1.'.'.$last; $this->host=implode('.', $urlarr); }else if (in_array($last, $this->state_domain)){ $last_2=array_pop($urlarr); if(in_array($last_1, $this->top_domain)){ $this->domain=$last_2.'.'.$last_1.'.'.$last; $this->host=implode('.', $urlarr); }else{ $this->host=implode('.', $urlarr).$last_2; $this->domain=$last_1.'.'.$last; } } } return $this; } /** * 取得域名 * enter description here ... */ public function getdomain(){ return $this->domain; } /** * 取得主机 * enter description here ... */ public function gethost(){ return $this->host; } } $referer = array('xuehuwang.com','zangbala.cn','qianzhebaikou.net','sinaapp.com','163.com','sina.com.cn','weibo.com','abc.com'); // get the url, maybe you should check the given url if (isset($_get['url']) and $_get['url'] != '') { //获取来路域名 $site = (isset($_server['http_referer']) && !empty($_server['http_referer'])) ? $_server['http_referer'] : ''; //匹配是否是一个图片链接 if(preg_match('/(http|https|ftp|rtsp|mms):(\/\/|\\\\){1}((\w)+[.]){1,}([a-za-z]|[0-9]{1,3})(\s*\/)((\s)+[.]{1}(gif|jpg|png|bmp))/i',$_get['url'])){ if(!empty($site)){ $tempu = parse_url($site); $host = $tempu['host']; $root = new rootdomain(); $root->seturl($site); if(in_array($root->getdomain(),$referer)){ $img_referer = (isset($_get['referer']) && !empty($_get['referer']))? trim($_get['referer']) : ''; new frivoller($_get['url'],$img_referer); } }else{ $img_referer = (isset($_get['referer']) && !empty($_get['referer']))? trim($_get['referer']) : ''; new frivoller($_get['url'],$img_referer); } } } ?>
希望本文所述对大家的php程序设计有所帮助。
上一篇: 下雨的平安夜别走四楼
下一篇: 让人怀念的80后顺口溜大全