OSDN Git Service

xctcc/embrr
[embrj/master.git] / img.php
1 <?php
2 function curl_redirect_exec($ch) {
3     curl_setopt($ch, CURLOPT_HEADER, TRUE);
4     curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
5     $data = curl_exec($ch);
6     $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
7     if ($http_code == 301 || $http_code == 302) {
8         list($header) = explode("\r\n\r\n", $data, 2);
9         $matches = array();
10         //this part has been changes from the original
11         preg_match("/(Location:|URI:)[^(\n)]*/", $header, $matches);
12         $url = trim(str_replace($matches[1],"",$matches[0]));
13         //end changes
14         $url_parsed = parse_url($url);
15         if (isset($url_parsed)) {
16             curl_setopt($ch, CURLOPT_URL, $url);
17             return curl_redirect_exec($ch);
18         }
19     }
20     return $data;
21 }
22
23 if(isset($_GET['imgurl']))
24 {
25     $url = $_GET['imgurl'];
26     $ch = curl_init($url);
27     curl_setopt($ch,CURLOPT_FOLLOWLOCATION,TRUE); //301&302
28     $ret = curl_redirect_exec($ch);
29     $Httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
30     $Hsize = curl_getinfo($ch,CURLINFO_HEADER_SIZE);
31     curl_close($ch);
32     if($Httpcode == '200')
33     {
34         $header = substr($ret,0,$Hsize);
35         $pat = '/(Content-Type:\s?image\/\w+)/i';
36         $matchRet = preg_match_all($pat,$header,$m);
37         if($matchRet)
38         {
39             $header = $m[0][0];
40             $ret = substr($ret,$Hsize);
41             Header($header);
42             echo $ret;
43         }
44         else
45         {
46             echo 'image not found';
47         }
48     }
49     else
50     {
51         echo 'image loading error, code: '.$Httpcode;
52     }
53 }
54 ?>