diff --git a/lib/DirScanner.php b/lib/DirScanner.php new file mode 100644 index 0000000..6793687 --- /dev/null +++ b/lib/DirScanner.php @@ -0,0 +1,183 @@ + '目录名', + 'filename' => '文件名', + 'realpath' => '完整路径', + 'path' => '相对网址', + 'extension' => '文件后缀', + 'fstat' => '资源状态', //同php方法fstat: https://www.php.net/manual/en/function.fstat.php + 'content' => 'MD文件内容', + 'shortcut' => 'URL快捷方式', + + 'description' => '描述', + 'keywords' => '关键词', + 'snapshot' => '快照图片', + ]; + private $scanResults = []; //目录扫描结果 + + + protected $supportFileExtensions = [ //支持的文件类型 + 'txt', //纯文本 + 'md', //纯文本 + 'url', //快捷方式 + 'jpg', //图片 + 'png', //图片 + 'gif', //图片 + 'ico', //图标 + 'mp4', //视频 + 'ts', //视频 + 'm3u8', //视频 + ]; + protected $maxReadFilesize = [ //默认每种文件读取内容最大大小 + 'txt' => 100*1024, //纯文本 + 'md' => 5*1024*1024, //纯文本 + 'url' => 20*1024, //快捷方式 + 'jpg' => 500*1024, //图片 + 'png' => 500*1024, //图片 + 'gif' => 500*1024, //图片 + 'ico' => 50*1024, //图标 + 'mp4' => 100*1024*1024, //视频 + 'ts' => 10*1024*1024, //视频 + 'm3u8' => 10*1024*1024, //视频 + ]; + protected $securedFileExtensions = [ //开启Nginx防盗链的文件类型 + 'jpg', //图片 + 'png', //图片 + 'gif', //图片 + 'ico', //图标 + 'mp4', //视频 + 'ts', //视频 + 'm3u8', //视频 + ]; + + public $scanTimeCost = 0; //上一次目录扫描耗时,单位:微秒 + + + //解析描述文件内容 + private function parseDescriptionFiles() { + } + + //解析快捷方式文件内容 + private function parseShortCuts() { + } + + //根据文件路径生成唯一编号 + private function getId($realpath) { + return !empty($realpath) ? md5($realpath) : ''; + } + + + //根据文件生成防盗链网址 + //参考:https://nginx.org/en/docs/http/ngx_http_secure_link_module.html#secure_link + //防盗链参数名:md5, expires + protected function getSecureLink($path) { + $expires = time() + $this->nginxSecureTimeout; + $originStr = str_replace([ + '{secure_link_expires}', + '{uri}', + '{remote_addr}', + '{secret}', + ], [ + $expires, + $path, + $this->userIp, + $this->nginxSecret, + ], $this->nginxSecureLinkMd5Pattern); + + $md5 = base64_encode( md5($originStr, true) ); + $md5 = strtr($md5, '+/', '-_'); + $md5 = str_replace('=', '', $md5); + + return "{$path}?md5={$md5}&expires={$expires}"; + } + + //根据文件生成相对路径 + protected function getFilePath() { + } + + + //设置Nginx防盗链开启或关闭,以及密钥、加密方式、超时时长 + public function setNginxSecure($secureOn, $secret = '', $userIp = '', $pattern = '', $timeout = 0) { + $status = false; + if (is_string($secureOn) && strtolower($secureOn) == 'on') { + $status = true; + }else if ((bool)$secureOn == true) { + $status = true; + } + $this->nginxSecureOn = $status; + + if (!empty($secret) && is_string($secret)) { + $this->nginxSecret = $secret; + } + + if (!empty($userIp) && is_string($userIp)) { + $this->userIp = $userIp; + } + + if (!empty($pattern) && is_string($pattern)) { + $this->nginxSecureLinkMd5Pattern = $pattern; + } + + if ((int)$timeout > 0) { + $this->nginxSecureTimeout = (int)$timeout; + } + } + + //设置Nginx防盗链密钥 + public function setNginxSecret($secret) { + if (!empty($secret) && is_string($secret)) { + $this->nginxSecret = $secret; + } + } + + //设置Nginx防盗链密钥 + public function setUserIp($userIp) { + if (!empty($userIp) && is_string($userIp)) { + $this->userIp = $userIp; + } + } + + //设置Nginx防盗链MD5加密方式 + /** + * Nginx防盗链MD5加密方式参考下面网址中的示例, + * 将Nginx的变量替换$符号为英文大括号; + * + * 示例: + * ``` + * {secure_link_expires}{uri}{remote_addr} {secret} + * ``` + * Nginx文档参考:http://nginx.org/en/docs/http/ngx_http_secure_link_module.html#secure_link_md5 + */ + public function setNginxSecureLinkMd5Pattern($pattern) { + if (!empty($pattern) && is_string($pattern)) { + $this->nginxSecureLinkMd5Pattern = $pattern; + } + } + + //设置Nginx防盗链超时时长,单位:秒 + public function setNginxSecureTimeout($timeout) { + if ((int)$timeout > 0) { + $this->nginxSecureTimeout = (int)$timeout; + } + } + + //扫描目录获取目录和文件列表,支持指定目录扫描深度(目录级数) + public function scan($dir, $levels = 3) { + } + +} diff --git a/test/DirScannerTest.php b/test/DirScannerTest.php new file mode 100644 index 0000000..2accff2 --- /dev/null +++ b/test/DirScannerTest.php @@ -0,0 +1,38 @@ +setNginxSecret($secret); + $this->setUserIp($userIp); + + if (!empty($pattern)) { + $this->setNginxSecureLinkMd5Pattern($pattern); + } + + echo "\n"; + echo "==function secureLink==\n"; + echo "secret: {$secret}\n"; + echo "user ip: {$userIp}\n"; + $url = $this->getSecureLink($path); + echo "secure link: {$url}\n"; + echo "\n"; + } + +} + + +//--调用测试方法-- +$scanner = new DirScannerTest(); +$scanner->secureLinkTest('/default/');