diff --git a/README.md b/README.md index 5923a948..d12c58bf 100644 --- a/README.md +++ b/README.md @@ -28,22 +28,19 @@ define('Password', '请在这里填写密码啦!ヾ(≧▽≦*)o'); - [PNL 下载方式](https://www.lanzous.com/u/pnl "PNL 下载方式") ## New Changes -- 当前版本:`1.2.2` -- 更新日期:2020-8-14 +- 当前版本:`1.2.3` +- 更新日期:2020-8-15 - 以下修改由 [LC](https://github.com/lc6464 "LC") 完成 - - 配置、函数与程序分离(`后端`) - - 修复 POST 方法访问 `?download` 参数不齐全出错的问题 - - 修复未配置或配置了普通用户的 `BDUSS` 和 `STOKEN` 时显示空链接的问题 - - 样式、JavaScript 与页面分离(`前端`) - - 优化数据传输 - - 优化打开文件夹的表现(原来直接提示不可用,现在跳转到百度网盘官方的分享页面) - - 使用函数减少重复工作的代码量 - - 增加注释 - - 优化前端代码 + - 此版本可能不稳定,谨慎升级! - 优化后端逻辑和效率 - - 优化用户体验 - - 优化发生错误时的用户体验 + - 优化文件加载 + - 将一些可能会丢失的远程文件复制到本地 + - 减少访问百度 API 的次数 + - 优化代码 + - 正在支持文件夹中(仍在实现中) + - 此版本可能不稳定,谨慎升级! ## 坑或不确定 - `static/functions.js` - - 36 行 \ No newline at end of file + - 36 行 +- 此版本可能不稳定,谨慎升级! \ No newline at end of file diff --git a/config.php b/config.php index 27efa0b8..c3410b63 100644 --- a/config.php +++ b/config.php @@ -1,6 +1,6 @@ * @link https://imwcr.cn/ @@ -22,7 +22,7 @@ * @link https://lcwebsite.cn/ * @link https://space.bilibili.com/52618445 */ -if (!defined('init')){ http_response_code(403); header('Content-Type: text/plain; charset=utf-8'); header('Refresh: 3;url=./'); die('想啥呢?直接访问这个文件?'); } // 直接访问处理程序 +if (!defined('init')){ http_response_code(403); header('Content-Type: text/plain; charset=utf-8'); header('Refresh: 3;url=./'); die("想啥呢?\r\n直接访问这个文件?"); } // 直接访问处理程序 define('BDUSS', ''); // 你的 SVIP BDUSS define('STOKEN', ''); // 你的 SVIP STOKEN diff --git a/functions.php b/functions.php index 90b8704d..648c3f63 100644 --- a/functions.php +++ b/functions.php @@ -1,12 +1,12 @@ * @link https://imwcr.cn/ @@ -16,63 +16,59 @@ * @link https://lcwebsite.cn/ * @link https://space.bilibili.com/52618445 */ -if (!defined('init')){ http_response_code(403); header('Content-Type: text/plain; charset=utf-8'); header('Refresh: 3;url=./'); die('想啥呢?直接访问这个文件?'); } // 直接访问处理程序 +if (!defined('init')){ http_response_code(403); header('Content-Type: text/plain; charset=utf-8'); header('Refresh: 3;url=./'); die("想啥呢?\r\n直接访问这个文件?"); } // 直接访问处理程序 // main -function post($url, $data, array $headerArray) { - $curl = curl_init(); - curl_setopt($curl, CURLOPT_URL, $url); - curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE); // 忽略ssl - curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE); - curl_setopt($curl, CURLOPT_POST, 1); - curl_setopt($curl, CURLOPT_POSTFIELDS, $data); - curl_setopt($curl, CURLOPT_HTTPHEADER, $headerArray); - curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); - $output = curl_exec($curl); - curl_close($curl); - return $output; +function setCurl(&$ch, array $header) { // 批处理 curl + $a = curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // 忽略证书 + $b = curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); // 不检查证书与域名是否匹配(2为检查) + $c = curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // 以字符串返回结果而非输出 + $d = curl_setopt($ch, CURLOPT_HTTPHEADER, $header); // 请求头 + return($a&&$b&&$c&&$d); } -function get($url, array $headerArray) { - $ch = curl_init(); - curl_setopt($ch, CURLOPT_URL, $url); - curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); // 忽略ssl - curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); - curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); - curl_setopt($ch, CURLOPT_HTTPHEADER, $headerArray); - $output = curl_exec($ch); +function post(string $url, $data, array $header) { // POST 发送数据 + $ch = curl_init($url); + setCurl($ch, $header); + curl_setopt($ch, CURLOPT_POST, true); // POST 方法 + curl_setopt($ch, CURLOPT_POSTFIELDS, $data); // POST 的数据 + $result = curl_exec($ch); curl_close($ch); - return $output; + return $result; } -function head($url, array $headerArray) { // curl 获取响应头 - $ch = curl_init(); - curl_setopt($ch, CURLOPT_URL, $url); - curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); // 忽略ssl - curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); - curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); // TRUE 将 curl_exec() 获取的信息以字符串返回,而不是直接输出 - curl_setopt($ch, CURLOPT_HEADER, true); // 返回 response header - curl_setopt($ch, CURLOPT_NOBODY, true); // 为了节省带宽及时间,只需要响应头 - curl_setopt($ch, CURLOPT_HTTPHEADER, $headerArray); +function get(string $url, array $header) { // GET 请求数据 + $ch = curl_init($url); + setCurl($ch, $header); + $result = curl_exec($ch); + curl_close($ch); + return $result; +} +function head(string $url, array $header) { // 获取响应头 + $ch = curl_init($url); + setCurl($ch, $header); + curl_setopt($ch, CURLOPT_HEADER, true); // 返回响应头 + curl_setopt($ch, CURLOPT_NOBODY, true); // 只要响应头 curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false); $response = curl_exec($ch); $header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE); // 获得响应头大小 - $header = substr($response, 0, $header_size); // 根据头大小获取头信息 + $result = substr($response, 0, $header_size); // 根据头大小获取头信息 curl_close($ch); - return $header; + return $result; } -function getSubstr($str, $leftStr, $rightStr) { +function getSubstr(string $str, string $leftStr, string $rightStr) { $left = strpos($str, $leftStr); // echo '左边:'.$left; $right = strpos($str, $rightStr, $left); // echo '
右边:'.$right; if ($left < 0 or $right < $left) return ''; - return substr($str, $left + strlen($leftStr), $right - $left - strlen($leftStr)); + $left += strlen($leftStr); + return substr($str, $left, $right - $left); } -function formatSize($b, $times = 0) { // 格式化size显示 - if ($b > 1024) { - $temp = $b / 1024; - return formatSize($temp, $times + 1); // 递归处理 +function formatSize(float $size, int $times = 0) { // 格式化size显示 + if ($size > 1024) { + $size /= 1024; + return formatSize($size, $times + 1); // 递归处理 } else { switch ($times) { case '0': - $unit = 'B'; break; + $unit = ($size == 1) ? 'Byte' : 'Bytes'; break; case '1': $unit = 'KB'; break; case '2': @@ -90,76 +86,62 @@ function formatSize($b, $times = 0) { // 格式化size显示 default: $unit = '单位未知'; } - return sprintf('%.2f', $b) . $unit; + return sprintf('%.3f', $size) . $unit; } } -function CheckPassword() { // 检查密码 +function CheckPassword() { // 校验密码 if (IsCheckPassword) { - if ((!isset($_POST["password"])) || $_POST["password"] != Password) die('
+ if ((!isset($_POST["password"])) || $_POST["password"] !== Password) die('
'); else echo ''; } } - // 解析分享链接 -function verifyPwd($surl_1, $pwd) { // 验证提取码 +function verifyPwd(string $surl_1, string $pwd) { // 验证提取码 $url = 'https://pan.baidu.com/share/verify?channel=chunlei&clienttype=0&web=1&app_id=250528&surl=' . $surl_1; $data = "pwd=$pwd"; - $headerArray = array("User-Agent: netdisk", "Referer: https://pan.baidu.com/disk/home"); - $json1 = post($url, $data, $headerArray); - $json1 = json_decode($json1, true); // -12 提取码错误 - if ($json1["errno"] == 0) return $json1["randsk"]; + $header = array("User-Agent: netdisk", "Referer: https://pan.baidu.com/disk/home"); + $result = json_decode(post($url, $data, $header), true); // -12 提取码错误 + if ($result["errno"] === 0) return $result["randsk"]; else return 1; } -function getSign($surl, $randsk) { - if ($randsk == 1) return 1; +function getSign(string $surl, $randsk) { + if ($randsk === 1) return 1; $url = 'https://pan.baidu.com/s/1' . $surl; - $headerArray = array( + $header = array( "User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.514.1919.810 Safari/537.36", "Cookie: BDUSS=" . BDUSS . ";STOKEN=" . STOKEN . ";BDCLND=" . $randsk . ";" ); - $json2 = get($url, $headerArray); - $re = '/yunData.setData\(({.+)\);/'; - $re = '/yunData.setData\(\{(.*)?\}\);/'; - if (preg_match($re, $json2, $matches)) { - $json2 = $matches[0]; - $json2 = substr($json2, 16, -2); - $json2 = json_decode($json2, true); - return $json2; - } else return 1; + if (preg_match('/yunData.setData\((\{.*?\})\);/', get($url, $header), $matches)) return json_decode($matches[1], true); + else return 1; } -function getFileList($shareid, $uk, $randsk) { - $url = 'https://pan.baidu.com/share/list?app_id=250528&channel=chunlei&clienttype=0&desc=0&num=100&order=name&page=1&root=1&shareid=' . $shareid . '&showempty=0&uk=' . $uk . '&web=1'; - $headerArray = array( +function getFileList(string $shareid, string $uk, string $randsk) { + $url = 'https://pan.baidu.com/share/list?app_id=250528&channel=chunlei&clienttype=0&desc=0&num=100&order=name&page=1&root=1&shareid='.$shareid.'&showempty=0&uk='.$uk.'&web=1'; + $header = array( "User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.514.1919.810 Safari/537.36", "Cookie: BDUSS=" . BDUSS . ";STOKEN=" . STOKEN . ";BDCLND=" . $randsk . ";", "Referer: https://pan.baidu.com/disk/home" ); - $json3 = get($url, $headerArray); - $json3 = json_decode($json3, true); - return $json3; + return json_decode(get($url, $header), true); } -function fileInfo($filename, $size, $md5, $server_ctime) { // 输出 HTML 字符串 - return '

文件名:' . $filename . '

文件大小:' . formatSize($size) . '

文件MD5:' . $md5 . '

上传时间:' . date("Y年m月d日 H:i:s", $server_ctime) . '

'; +function FileList($sign) { + if ($sign === 1) return 1; + return $sign['file_list']; } - -// 获取下载链接 -function getDlink($fs_id, $timestamp, $sign, $randsk, $share_id, $uk) { - $postdata = "encrypt=0"; - $postdata .= "&extra=" . urlencode("{\"sekey\":\"" . urldecode($randsk) . "\"}"); //被这个转义坑惨了QAQ - $postdata .= "&fid_list=[$fs_id]"; - $postdata .= "&primaryid=$share_id"; - $postdata .= "&uk=$uk"; - $postdata .= "&product=share&type=nolimit"; +function FileInfo(string $filename, float $size, string $md5, int $server_ctime) { // 输出 HTML 字符串 + return '

文件名:' . $filename . '

文件大小:' . formatSize($size) . '

文件MD5:' . $md5 + . '

上传时间:' . date("Y年m月d日 H:i:s", $server_ctime) . '

'; +} +function getDlink($fs_id, $timestamp, $sign, $randsk, $share_id, $uk) { // 获取下载链接 $url = 'https://pan.baidu.com/api/sharedownload?app_id=250528&channel=chunlei&clienttype=12&sign=' . $sign . '×tamp=' . $timestamp . '&web=1'; - $headerArray = array( + $data = "encrypt=0" . "&extra=" . urlencode('{"sekey":"' . urldecode($randsk) . '"}') . "&fid_list=[$fs_id]" . "&primaryid=$share_id" . "&uk=$uk" . "&product=share&type=nolimit"; + $header = array( "User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.514.1919.810 Safari/537.36", "Cookie: BDUSS=" . BDUSS . ";STOKEN=" . STOKEN . ";BDCLND=" . $randsk . ";", "Referer: https://pan.baidu.com/disk/home" ); - $res3 = post($url, $postdata, $headerArray); - $res3 = json_decode($res3, true); + return json_decode(post($url, $data, $header), true); //没有 referer 就 112 ,然后没有 sekey 参数就 118 -20??? // 参数 类型 描述 // list json array 文件信息列表 @@ -175,5 +157,4 @@ function getDlink($fs_id, $timestamp, $sign, $randsk, $share_id, $uk) { // list[0]["height"] int 图片高度 // list[0]["width"] int 图片宽度 // list[0]["date_taken"] int 图片拍摄时间 - return $res3; } \ No newline at end of file diff --git a/index.php b/index.php index d7c150c9..95471285 100644 --- a/index.php +++ b/index.php @@ -1,16 +1,16 @@ * @link https://imwcr.cn/ @@ -26,9 +26,7 @@ require('config.php'); require('functions.php'); } else { http_response_code(503); header('Content-Type: text/plain; charset=utf-8'); header('Refresh: 7;url=https://github.com/lc6464/baiduwp-php'); - die('缺少相关配置和定义文件!无法正常运行程序! -请重新 Clone 项目并配置! -将在七秒内跳转到 GitHub 储存库!'); + die("缺少相关配置和定义文件!无法正常运行程序!\r\n请重新 Clone 项目并配置!\r\n将在七秒内跳转到 GitHub 储存库!"); } // 通用响应头 header('Content-Type: text/html; charset=utf-8'); @@ -43,7 +41,7 @@ PanDownload 复刻版 - + @@ -58,7 +56,7 @@
'; // 链接失效 - else if ($filejson["errno"] != 0) echo '
'; // 鬼知道发生了啥,比如说 -7 - else { // 终于正常了 - $filecontent = '
"; - } - } else echo '
'; + } else echo '
'; + } } elseif (isset($_GET["download"])) { // 解析下载地址页面 if ($_SERVER['REQUEST_METHOD'] == 'POST') { if (isset($_POST["fs_id"]) && isset($_POST["time"]) && isset($_POST["sign"]) && isset($_POST["randsk"]) && isset($_POST["share_id"]) && isset($_POST["uk"])) { @@ -169,28 +172,24 @@ // 1. 使用 dlink 下载文件 2. dlink 有效期为8小时 3. 必需要设置 User-Agent 字段 4. dlink 存在 HTTP 302 跳转 if ($realLink == "") echo '
'; // 未配置 SVIP 账号 +

未配置或配置了普通账号的均会导致失败!必须要 SVIP 账号!

' . FileInfo($filename, $size, $md5, $server_ctime) . ''; // 未配置 SVIP 账号 else echo '
'; // 成功! } else echo '
'; // 未知错误 +
获取下载链接失败

未知错误!

'; // 未知错误 } else echo '
'; // 参数不齐 +
参数有误

POST 传参出现问题!请不要自行构建表单提交!

'; // 参数不齐 } else echo '
'; // 方法错误 +
方法错误

请不要直接访问此页面或使用 GET 方式访问!

'; // 方法错误 } else { // 首页 ?>
百度网盘分享链接在线解析
-
- -
-
- -
+
+
'; } // 密码 ?> diff --git a/resource/UserAgentSwitcher.crx b/resource/UserAgentSwitcher.crx new file mode 100644 index 00000000..ba38eea0 Binary files /dev/null and b/resource/UserAgentSwitcher.crx differ diff --git a/resource/bg.png b/resource/bg.png new file mode 100644 index 00000000..340193b5 Binary files /dev/null and b/resource/bg.png differ diff --git a/resource/favicon.ico b/resource/favicon.ico new file mode 100644 index 00000000..1e80e143 Binary files /dev/null and b/resource/favicon.ico differ diff --git a/resource/logo.png b/resource/logo.png new file mode 100644 index 00000000..4618b2f4 Binary files /dev/null and b/resource/logo.png differ diff --git a/static/functions.js b/static/functions.js index c2f1b606..cee200b1 100644 --- a/static/functions.js +++ b/static/functions.js @@ -47,5 +47,5 @@ function getIconClass(filename) { } function ToSharePage(surl) { sweetAlert('即将跳转','暂不支持文件夹下载!\r\n即将跳转到百度网盘官方的分享页面!','info'); - return setTimeout(u => { open('https://pan.baidu.com/s/' + u); }, 1750, surl); + return setTimeout(open, 1750, 'https://pan.baidu.com/s/' + surl); } \ No newline at end of file diff --git a/static/index.css b/static/index.css index 7417fc43..b3fa317b 100644 --- a/static/index.css +++ b/static/index.css @@ -1,4 +1,4 @@ -body { background: url("https://pandownload.com/img/baiduwp/bg.png"); } +body { background: url("../resource/bg.png"); } .logo-img { width: 1.1em; position: relative; top: -3px; } .form-inline input { width: 500px; } .input-card { position: relative; top: 7.0em; }