php实现的网络相册图片防盗链完美破解方法

这篇文章主要介绍了php实现的网络相册图片防盗链完美破解方法,可实现针对设置了防盗链网络相册图片的抓取功能,非常具有实用价值,需要的朋友可以参考下,本文实例讲述了php实现的网络相册图片防盗链完美破解方法,分享给大家供大家参考,具体如下:

网络相册图片防盗链破解程序 - PHP版 这个防盗链破解版可以完美破解当下比较流行的: 百度相册,网易相册,360我喜欢等网站图片. 还可以实现简单的图片防盗链. 因为这个类是先进行获取远程图片, 然后再把图片发送到客户端,所以,算是进行了两次流量的传送.因此,会浪费空间流量,接下来,会开发缓存功能,这样可以实现节约流量!

  1. <?php
  2. /**
  3. * 网络相册图片防盗链破解程序 - PHP版
  4. *
  5. * 使用方法:
  6. *
  7. * http://yourdomain/url.php?url=http://hiphotos.baidu.com/verdana/pic/item/baidupicture.jpg&referer=
  8. * 其中url是指需要破解的图片URL,而referer是为了兼容一些不需要设置来路域名才能显示的相册,例如360我喜欢网,必须设置来路为空才能正常浏览. 所以,此时应该设置referer为1
  9. *
  10. * @author 雪狐博客
  11. * @version 1.0
  12. * @since July 16, 2012
  13. * @URL http://www.xuehuwang.com
  14. */
  15. class Frivoller
  16. {
  17. /**
  18. * HTTP 版本号 (1.0, 1.1) , 百度使用的是 version 1.1
  19. *
  20. * @var string
  21. */
  22. protected $version;
  23. /**
  24. * 进行HTTP请求后响应的数据
  25. *
  26. * @var 字符串格式
  27. */
  28. protected $body;
  29. /**
  30. * 需要获取的远程URL
  31. *
  32. * @var 字符串格式
  33. */
  34. protected $link;
  35. /**
  36. * An array that containing any of the various components of the URL.
  37. *
  38. * @var array
  39. */
  40. protected $components;
  41. /**
  42. * HTTP请求时HOST数据
  43. *
  44. * @var 字符串
  45. */
  46. protected $host;
  47. /**
  48. * The path of required file.
  49. * (e.g. '/verdana/abpic/item/mygirl.png')
  50. *
  51. * @var string
  52. */
  53. protected $path;
  54. /**
  55. * The HTTP referer, extra it from original URL
  56. *
  57. * @var string
  58. */
  59. protected $referer;
  60. /**
  61. * The HTTP method, 'GET' for default
  62. *
  63. * @var string
  64. */
  65. protected $method = 'GET';
  66. /**
  67. * The HTTP port, 80 for default
  68. *
  69. * @var int
  70. */
  71. protected $port = 80;
  72. /**
  73. * Timeout period on a stream
  74. *
  75. * @var int
  76. */
  77. protected $timeout = 100;
  78. /**
  79. * The filename of image
  80. *
  81. * @var string
  82. */
  83. protected $filename;
  84. /**
  85. * The ContentType of image file.
  86. * image/jpeg, image/gif, image/png, image
  87. *
  88. * @var string
  89. */
  90. protected $contentType;
  91. /**
  92. * Frivoller constructor
  93. *
  94. * @param string $link
  95. */
  96. public function __construct($link,$referer='')
  97. {
  98. $this->referer = $referer;
  99. // parse the http link
  100. $this->parseLink($link);
  101. // begin to fetch the image
  102. $stream = pfsockopen($this->host, $this->port, $errno, $errstr, $this->timeout);
  103. if (!$stream){
  104. header("Content-Type: $this->contentType;");
  105. echo $this->CurlGet($link);
  106. }else{
  107. fwrite($stream, $this->buildHeaders());
  108. $this->body = "";
  109. $img_size = get_headers($link,true);
  110. while (!feof($stream)) {
  111. $this->body .= fgets($stream, $img_size['Content-Length']);
  112. //fwrite($jpg,fread($stream, $img_size['Content-Length']));
  113. }
  114. $content = explode("\r\n\r\n", $this->body, 2);
  115. $this->body = $content[1];
  116. fclose($stream);
  117. // send 'ContentType' header for saving this file correctly
  118. // 如果不发送CT,则在试图保存图片时,IE7 会发生错误 (800700de)
  119. // Flock, Firefox 则没有这个问题,Opera 没有测试
  120. header("Content-Type: $this->contentType;");
  121. header("Cache-Control: max-age=315360000");
  122. echo $this->body;
  123. //保存图片
  124. //file_put_contents('hello.jpg', $this->body);
  125. }
  126. }
  127. /**
  128. * Compose HTTP request header
  129. *
  130. * @return string
  131. */
  132. private function buildHeaders()
  133. {
  134. $request = "$this->method $this->path HTTP/1.1\r\n";
  135. $request .= "Host: $this->host\r\n";
  136. $request .= "Accept-Encoding: gzip, deflate\r\n";
  137. $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";
  138. $request .= "Content-Type: image/jpeg\r\n";
  139. $request .= "Accept: */*\r\n";
  140. $request .= "Keep-Alive: 300\r\n";
  141. $request .= "Referer: $this->referer\r\n";
  142. $request .= "Cache-Control: max-age=315360000\r\n";
  143. $request .= "Connection: close\r\n\r\n";
  144. return $request;
  145. }
  146. /**
  147. * Strip initial header and filesize info
  148. */
  149. private function extractBody(&$body)
  150. {
  151. // The status of link
  152. if(strpos($body, '200 OK') > 0) {
  153. // strip header
  154. $endpos = strpos($body, "\r\n\r\n");
  155. $body = substr($body, $endpos + 4);
  156. // strip filesize at nextline
  157. $body = substr($body, strpos($body, "\r\n") + 2);
  158. }
  159. }
  160. /**
  161. * Extra the http url
  162. *
  163. * @param $link
  164. */
  165. private function parseLink($link)
  166. {
  167. $this->link = $link;
  168. $this->components = parse_url($this->link);
  169. $this->host = $this->components['host'];
  170. $this->path = $this->components['path'];
  171. if(emptyempty($this->referer)){
  172. $this->referer = $this->components['scheme'] . '://' . $this->components['host'];
  173. }elseif($this->referer == '1'){
  174. $this->referer = '';
  175. }
  176. $this->filename = basename($this->path);
  177. // extract the content type
  178. $ext = substr(strrchr($this->path, '.'), 1);
  179. if ($ext == 'jpg' or $ext == 'jpeg') {
  180. $this->contentType = 'image/pjpeg';
  181. }
  182. elseif ($ext == 'gif') {
  183. $this->contentType = 'image/gif';
  184. }
  185. elseif ($ext == 'png') {
  186. $this->contentType = 'image/x-png';
  187. }
  188. elseif ($ext == 'bmp') {
  189. $this->contentType = 'image/bmp';
  190. }
  191. else {
  192. $this->contentType = 'application/octet-stream';
  193. }
  194. }
  195. //抓取网页内容
  196. function CurlGet($url){
  197. $url = str_replace('&','&',$url);
  198. $curl = curl_init();
  199. curl_setopt($curl, CURLOPT_URL, $url);
  200. curl_setopt($curl, CURLOPT_HEADER, false);
  201. curl_setopt($curl, CURLOPT_REFERER,$url);
  202. curl_setopt($curl, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 6.0; SeaPort/1.2; Windows NT 5.1; SV1; InfoPath.2)");
  203. curl_setopt($curl, CURLOPT_COOKIEJAR, 'cookie.txt');
  204. curl_setopt($curl, CURLOPT_COOKIEFILE, 'cookie.txt');
  205. curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  206. curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 0);
  207. $values = curl_exec($curl);
  208. curl_close($curl);
  209. return $values;
  210. }
  211. }
  212. /**
  213. * 取得根域名
  214. *
  215. * @author lonely
  216. * @create 2011-3-11
  217. * @version 0.11
  218. * @lastupdate lonely
  219. * @package Sl
  220. */
  221. class RootDomain{
  222. private static $self;
  223. private $domain=null;
  224. private $host=null;
  225. private $state_domain;
  226. private $top_domain;
  227. /**
  228. * 取得域名分析实例
  229. * Enter description here ...
  230. */
  231. public static function instace(){
  232. if(!self::$self)
  233. self::$self=new self();
  234. return self::$self;
  235. }
  236. public function __construct(){
  237. $this->state_domain=array(
  238. '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'
  239. );
  240. $this->top_domain=array('com','arpa','edu','gov','int','mil','net','org','biz','info','pro','name','museum','coop','aero','xxx','idv','me','mobi');
  241. $this->url=$_SERVER['HTTP_HOST'];
  242. }
  243. /**
  244. * 设置URL
  245. * Enter description here ...
  246. * @param string $url
  247. */
  248. public function setUrl($url=null){
  249. $url=$url?$url:$this->url;
  250. if(emptyempty($url))return $this;
  251. if(!preg_match("/^http:/is", $url))
  252. $url="http://".$url;
  253. $url=parse_url(strtolower($url));
  254. $urlarr=explode(".", $url['host']);
  255. $count=count($urlarr);
  256. if ($count<=2){
  257. $this->domain=$url['host'];
  258. }else if ($count>2){
  259. $last=array_pop($urlarr);
  260. $last_1=array_pop($urlarr);
  261. if(in_array($last, $this->top_domain)){
  262. $this->domain=$last_1.'.'.$last;
  263. $this->host=implode('.', $urlarr);
  264. }else if (in_array($last, $this->state_domain)){
  265. $last_2=array_pop($urlarr);
  266. if(in_array($last_1, $this->top_domain)){
  267. $this->domain=$last_2.'.'.$last_1.'.'.$last;
  268. $this->host=implode('.', $urlarr);
  269. }else{
  270. $this->host=implode('.', $urlarr).$last_2;
  271. $this->domain=$last_1.'.'.$last;
  272. }
  273. }
  274. }
  275. return $this;
  276. }
  277. /**
  278. * 取得域名
  279. * Enter description here ...
  280. */
  281. public function getDomain(){
  282. return $this->domain;
  283. }
  284. /**
  285. * 取得主机
  286. * Enter description here ...
  287. */
  288. public function getHost(){
  289. return $this->host;
  290. }
  291. }
  292. $referer = array('xuehuwang.com','zangbala.cn','qianzhebaikou.net','sinaapp.com','163.com','sina.com.cn','weibo.com','abc.com');
  293. // Get the url, maybe you should check the given url
  294. if (isset($_GET['url']) and $_GET['url'] != '') {
  295. //获取来路域名
  296. $site = (isset($_SERVER['HTTP_REFERER']) && !emptyempty($_SERVER['HTTP_REFERER'])) ? $_SERVER['HTTP_REFERER'] : '';
  297. //匹配是否是一个图片链接
  298. 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'])){
  299. if(!emptyempty($site)){
  300. $tempu = parse_url($site);
  301. $host = $tempu['host'];
  302. $root = new RootDomain();
  303. $root->setUrl($site);
  304. if(in_array($root->getDomain(),$referer)){
  305. $img_referer = (isset($_GET['referer']) && !emptyempty($_GET['referer']))? trim($_GET['referer']) : '';
  306. new Frivoller($_GET['url'],$img_referer);
  307. }
  308. }else{
  309. $img_referer = (isset($_GET['referer']) && !emptyempty($_GET['referer']))? trim($_GET['referer']) : '';
  310. new Frivoller($_GET['url'],$img_referer);
  311. }
  312. }
  313. }
  314. ?>