Heim > php教程 > PHP源码 > Hauptteil

百度网盘的外链代理

PHP中文网
Freigeben: 2016-05-25 17:04:52
Original
1803 Leute haben es durchsucht

 [文件] index.php 

<?php
// STARTOFFILE

// URL的正则
$url = &#39;/http:\/\/pan\.baidu\.com.*(?:shareid=(\d+)&(?:uk=(\d+?)))/Uis&#39;;

// 文件夹的正则
$folder = &#39;/\/(get|show|folder)\/(\d+)\/(\d+)\/(.+)(?:\/(\d+)[\/]((?:.+)\.(?:.+?))|\/(\d+?)[\/]{0,1}|\/)/Uis&#39;;

// 单文件的正则
$file = &#39;/\/(get|show)\/(\d+)\/(\d+?)(?:[\/]((?:.+)\.(?:.+?))|[\/]{0,1})/Uis&#39;;

//文件夹指定文件名的正则
$fileName = &#39;/\/(file)\/(\d+)\/(\d+)\/(.+)\/(.+?)/Uis&#39;;

//文件夹指定文件名的正则(扩展:为图片是增加尺寸,质量等参数)
$picName = &#39;/\/pic\/(\d+)(?#shareid)\/(\d+)(?#uk)\/(.+)(?#path)\/(c\d+?)(u\d+?)q(\d+?)(?#图片参数)\/(.+?)(?#文件名)/Uis&#39;;

$preg=array(&#39;url&#39;=>$url,&#39;folder&#39;=>$folder,&#39;file&#39;=>$file,&#39;picName&#39;=>$picName,&#39;fileName&#39;=>$fileName);

class BaiduPanProxy{
    private $uk;
    private $shareid;
    private $path; //文件夹路径
    private $file; //指定输出为图片时的文件名
    private $size; //指定输出为图片时的尺寸
    private $method; //输出方法
    private $folderIndex; //文件夹的文件索引号
    private $folder;//文件夹的json内容
    private $url=&#39;&#39;;
    private $preg=array();
    // public $trueLinkPreg=&#39;|(http:[\\\/]{2,6}www\.baidupcs\.com[\\\/]{1,3}file[\\\/]{1,3}.*)\\\"|U&#39;; //已失效
    public $trueLinkPreg=&#39;|(http:[\\\/]{2,6}d\.pcs\.baidu\.com[\\\/]{1,3}file[\\\/]{1,3}.*)\\\"|U&#39;;

    public function __construct($url,$preg) {
        $this->url = $url;
        $this->preg = $preg;
    }
    private function parseUrl(){
        foreach ($this->preg  as $k=>$value) {
            if(0 != preg_match ( $value, $this->url, $match )){
                switch ($k) {
                    case &#39;url&#39;:
                        list ( , $this->shareid, $this->uk) = $match;
                        $this->method=&#39;get&#39;;//此情况下默认是get,不能自定义
                        $this->matchBy=&#39;url&#39;;
                        break;

                    case &#39;folder&#39;:
                        if(8 == count ( $match )) // 文件夹,不带文件名
                            list ( , $this->method, $this->shareid, $this->uk, $this->path,,,$this->folderIndex) = $match;
                        if(5 == count ( $match )) // 文件夹,遍历
                            list ( , $this->method, $this->shareid, $this->uk, $this->path) = $match;
                        if(7 == count ( $match )) // 文件夹,带文件名
                            list ( , $this->method, $this->shareid, $this->uk, $this->path,$this->folderIndex) = $match;
                        $this->folderIndex=$this->folderIndex-1;
                        $this->matchBy=&#39;folder&#39;;
                        // var_dump($match);exit();
                        break;

                    case &#39;file&#39;:
                        list ( , $this->method,$this->shareid, $this->uk) = $match;
                        $this->matchBy=&#39;file&#39;;
                        break;

                    case &#39;picName&#39;:
                        list ( , $this->shareid, $this->uk, $this->path, $c,$u,$q,$this->file) = $match;
                        $this->size=&#39;size=&#39;.$c.&#39;_&#39;.$u.&#39;&quality=&#39;.$q;
                        $this->file=urldecode($this->file);
                        $this->matchBy=&#39;picName&#39;;
                        break;

                    case &#39;fileName&#39;:
                        list ( , ,$this->shareid, $this->uk, $this->path,$this->file) = $match;
                        $this->file=urldecode($this->file);
                        $this->matchBy=&#39;fileName&#39;;
                        break;
                }
                return $this;
            }
        }
        if(!$match) $this->error();//匹配不到任何正确的参数则退出
    }
    private function parseFolder($url){
        $html=file_get_contents($url);
        return json_decode($html,true);
    }
    private function searchFolder(){
        $url = "http://pan.baidu.com/share/list?dir=" . $this->path . "&shareid=" . $this->shareid . "&uk=" . $this->uk . "&";
        $this->json=$this->parseFolder($url);
        if(! is_array($this->json)) $this->error();
    }
    private function getFolderIndex($list){
        foreach ($list as $k=>$v) {
            if(strtolower($v[&#39;server_filename&#39;])==strtolower($this->file) )return $k;
        }
    }
    private function digFolder($path = &#39;&#39;, $shareid = &#39;&#39;, $uk = &#39;&#39;){
        $url = "http://pan.baidu.com/share/list?dir=" . $path . "&shareid=" . $shareid . "&uk=" . $uk . "&";
        $this->json=$this->parseFolder($url);
        if(! is_array($this->json)) $this->error();
        foreach ( $this->json [&#39;list&#39;] as $arr ) {
            if (1 == $arr [&#39;isdir&#39;])
                $this->digFolder ( urlencode ( $arr [&#39;path&#39;] ), $shareid, $uk );
            else
                $this->str .= "<a href=\"" . $arr [&#39;dlink&#39;] . "\">" . $arr [&#39;server_filename&#39;] . "</a><br />";
        }
        return $this->str;
    }
    private function getRealLink(){
        if(!$this->method){//如果没有方法,只有输入真实文件名或输出可调质量图片两种方法
            $this->searchFolder();
            $this->folderIndex=$this->getFolderIndex($this->json[&#39;list&#39;]);
            if($this->size){
                //调节图片输出质量
                $this->realLink=preg_replace("/size=c\d+_u\d+&quality=\d+/", $this->size, $this->json[&#39;list&#39;][$this->folderIndex][&#39;thumbs&#39;][&#39;url3&#39;]);
            }else{
                $this->realLink=$this->json[&#39;list&#39;][$this->folderIndex][&#39;dlink&#39;];
            }
        }elseif (&#39;folder&#39; == $this->method) {
            $this->realLink = $this->digFolder($this->path,$this->shareid ,$this->uk);
        }else{
            if(!$this->path){
                $url = "http://pan.baidu.com/share/link?shareid=" . $this->shareid . "&uk=" . $this->uk;
                $html = file_get_contents ( $url );
                if (0 == preg_match ( $this->trueLinkPreg, $html, $url ))
                    $this->error();
                $_string = array ("replace" => Array ("&amp;", &#39;\\&#39; ), "string" => Array ("&", "" ) );
                // 替换转义字符
                $this->realLink = str_ireplace ( $_string ["replace"], $_string ["string"], $url [1] );
            }elseif ($this->path) {
                $this->searchFolder();
                $this->realLink=$this->json[&#39;list&#39;][$this->folderIndex][&#39;dlink&#39;];
            }
        }
    }
    public function haveFun(){
        if ($this->checkNew ( $this->localVersion, $this->checkFrequency ))
            $this->updateFile ( $this->localVersion );
        $this->parseUrl()->getRealLink();
        if(&#39;show&#39;==$this->method || &#39;folder&#39;==$this->method)
            echo $this->realLink;
        else
            header ( "Location:" . $this->realLink );
    }
    private function error(){
        die(&#39;oops!! 获取失败诶~建议到<a href="https://github.com/tofuliang/baidu-pan-proxy" target="_blank">http://www.php.cn/;/a>了解了解吧~&#39;);
    }
    private function checkNew($localVersion, $checkFrequency) {
        $lastCheck = ( int ) @file_get_contents ( __DIR__ . "/.lastcheck" );
        if (time () - $lastCheck > $checkFrequency) {
            @file_put_contents ( __DIR__ . "/.lastcheck", time () );
            $serverVersion = ( double ) @file_get_contents ( "https://raw.github.com/tofuliang/baidu-pan-proxy/master/version" );
            if ($serverVersion - ( double ) $localVersion > 0)
                return true;
            return false;
        }
        return false;
    }
    public function updateFile($localVersion) {
        $new = ( string ) @file_get_contents ( "https://raw.github.com/tofuliang/baidu-pan-proxy/master/index.php" );
        if (strpos ( $new, &#39;STARTOFFILE&#39; ) && strpos ( $new, &#39;ENDOFFILE&#39; )) {
            copy ( __FILE__, __FILE__ . $localVersion ) && @file_put_contents ( __FILE__, $new ) && exit ( "代理程序已更新到新版" );
        }
    }


}
$link = new BaiduPanProxy($_SERVER ["QUERY_STRING"],$preg);
$link->localVersion = &#39;0.6&#39;;
$link->checkFrequency = 86400; // 每隔多少秒到服务器检测更新,默认是一天
$link->haveFun();
//ENDOFFILE
Nach dem Login kopieren

                               

                   

Verwandte Etiketten:
php
Quelle:php.cn
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Beliebte Empfehlungen
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage