国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發(fā) > PHP > 正文

百度網(wǎng)盤的外鏈代理PHP源碼分享

2024-05-04 23:18:56
字體:
供稿:網(wǎng)友
將百度網(wǎng)盤的URL轉(zhuǎn)換成“靜態(tài)”鏈接或者輸出真實下載鏈接;
支持單個文件;
支持文件夾特定文件(同過文件索引或者文件名匹配);
支持文件夾中圖片自定義尺寸,質(zhì)量輸出;
支持遍歷文件夾,輸出所有下載鏈接。

<?php
// STARTOFFILE

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

// 文件夾的正則
$folder = '///(get|show|folder)//(/d+)//(/d+)//(.+)(?://(/d+)[//]((?:.+)/.(?:.+?))|//(/d+?)[//]{0,1}|//)/Uis';

// 單文件的正則
$file = '///(get|show)//(/d+)//(/d+?)(?:[//]((?:.+)/.(?:.+?))|[//]{0,1})/Uis';

//文件夾指定文件名的正則
$fileName = '///(file)//(/d+)//(/d+)//(.+)//(.+?)/Uis';

//文件夾指定文件名的正則(擴展:為圖片是增加尺寸,質(zhì)量等參數(shù))
$picName = '///pic//(/d+)(?#shareid)//(/d+)(?#uk)//(.+)(?#path)//(c/d+?)(u/d+?)q(/d+?)(?#圖片參數(shù))//(.+?)(?#文件名)/Uis';

$preg=array('url'=>$url,'folder'=>$folder,'file'=>$file,'picName'=>$picName,'fileName'=>$fileName);

class BaiduPanProxy{
    private $uk;
    private $shareid;
    private $path; //文件夾路徑
    private $file; //指定輸出為圖片時的文件名
    private $size; //指定輸出為圖片時的尺寸
    private $method; //輸出方法
    private $folderIndex; //文件夾的文件索引號
    private $folder;//文件夾的json內(nèi)容
    private $url='';
    private $preg=array();
    // public $trueLinkPreg='|(http:[////]{2,6}www/.baidupcs/.com[////]{1,3}file[////]{1,3}.*)///"|U'; //已失效
    public $trueLinkPreg='|(http:[////]{2,6}d/.pcs/.baidu/.com[////]{1,3}file[////]{1,3}.*)///"|U';

    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 'url':
                        list ( , $this->shareid, $this->uk) = $match;
                        $this->method='get';//此情況下默認是get,不能自定義
                        $this->matchBy='url';
                        break;

                    case 'folder':
                        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='folder';
                        // var_dump($match);exit();
                        break;

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

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

                    case 'fileName':
                        list ( , ,$this->shareid, $this->uk, $this->path,$this->file) = $match;
                        $this->file=urldecode($this->file);
                        $this->matchBy='fileName';
                        break;
                }
                return $this;
            }
        }
        if(!$match) $this->error();//匹配不到任何正確的參數(shù)則退出
    }
    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['server_filename'])==strtolower($this->file) )return $k;
        }
    }
    private function digFolder($path = '', $shareid = '', $uk = ''){
        $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 ['list'] as $arr ) {
            if (1 == $arr ['isdir'])
                $this->digFolder ( urlencode ( $arr ['path'] ), $shareid, $uk );
            else
                $this->str .= "<a href=/"" . $arr ['dlink'] . "/">" . $arr ['server_filename'] . "</a><br />";
        }
        return $this->str;
    }
    private function getRealLink(){
        if(!$this->method){//如果沒有方法,只有輸入真實文件名或輸出可調(diào)質(zhì)量圖片兩種方法
            $this->searchFolder();
            $this->folderIndex=$this->getFolderIndex($this->json['list']);
            if($this->size){
                //調(diào)節(jié)圖片輸出質(zhì)量
                $this->realLink=preg_replace("/size=c/d+_u/d+&quality=/d+/", $this->size, $this->json['list'][$this->folderIndex]['thumbs']['url3']);
            }else{
                $this->realLink=$this->json['list'][$this->folderIndex]['dlink'];
            }
        }elseif ('folder' == $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;", '//' ), "string" => Array ("&", "" ) );
                // 替換轉(zhuǎn)義字符
                $this->realLink = str_ireplace ( $_string ["replace"], $_string ["string"], $url [1] );
            }elseif ($this->path) {
                $this->searchFolder();
                $this->realLink=$this->json['list'][$this->folderIndex]['dlink'];
            }
        }
    }
    public function haveFun(){
        if ($this->checkNew ( $this->localVersion, $this->checkFrequency ))
            $this->updateFile ( $this->localVersion );
        $this->parseUrl()->getRealLink();
        if('show'==$this->method || 'folder'==$this->method)
            echo $this->realLink;
        else
            header ( "Location:" . $this->realLink );
    }
    private function error(){
        die('oops!! 獲取失敗誒~建議到<a href="https://github.com/tofuliang/baidu-pan-proxy" target="_blank">https://github.com/tofuliang/baidu-pan-proxy</a>了解了解吧~');
    }
    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, 'STARTOFFILE' ) && strpos ( $new, 'ENDOFFILE' )) {
            copy ( __FILE__, __FILE__ . $localVersion ) && @file_put_contents ( __FILE__, $new ) && exit ( "代理程序已更新到新版" );
        }
    }


}
$link = new BaiduPanProxy($_SERVER ["QUERY_STRING"],$preg);
$link->localVersion = '0.6';
$link->checkFrequency = 86400; // 每隔多少秒到服務(wù)器檢測更新,默認是一天
$link->haveFun();
//ENDOFFILE
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 沙坪坝区| 荔波县| 定州市| 天津市| 台州市| 买车| 逊克县| 塔河县| 大姚县| 北川| 儋州市| 永胜县| 甘谷县| 黑水县| 乌什县| 天津市| 甘孜县| 西峡县| 绍兴县| 贡山| 永顺县| 抚州市| 合作市| 德令哈市| 江口县| 定襄县| 苍山县| 南郑县| 卓尼县| 策勒县| 炉霍县| 百色市| 四会市| 洛扎县| 温州市| 隆尧县| 遂昌县| 赣榆县| 重庆市| 石棉县| 福海县|