├── README.md ├── cache └── README.md ├── config └── README.md ├── controller ├── AdminController.php ├── ImagesController.php ├── IndexController.php └── UploadController.php ├── index.php ├── init.php ├── install.sh ├── lib ├── Parsedown.php ├── cache.php ├── cache │ ├── filecache_.php │ ├── memcache_.php │ ├── redis_.php │ └── secache_.php ├── fetch.php ├── onedrive.php ├── oneindex.php ├── route.php ├── sqlite.php └── view.php ├── nginx ├── fastcgi_params ├── fcgiwrap-php ├── fcgiwrap.conf └── sites-available │ └── default ├── one.php ├── php.ini └── view ├── admin ├── cache.php ├── images.php ├── install │ ├── install_0.php │ ├── install_1.php │ ├── install_2.php │ ├── install_3.php │ └── layout.php ├── layout.php ├── login.php ├── setpass.php ├── settings.php ├── show.php └── upload.php └── nexmoe ├── 404.php ├── images ├── index.php └── layout.php ├── layout.php ├── list.php ├── password.php └── show ├── audio.php ├── code.php ├── doc.php ├── image.php ├── pdf.php ├── stream.php ├── video.php ├── video2.php └── video5.php /README.md: -------------------------------------------------------------------------------- 1 | # oneindex 2 | OneDrive Directory Index 3 | 4 | ## 功能: 5 | 不用服务器空间,不走服务器流量, 6 | 7 | 直接列onedrive目录,文件直链下载。 8 | 9 | ## 一键安装(Debian 8): 10 | ``` 11 | wget --no-check-certificate -qO- https://github.com/0oVicero0/oneindex/raw/master/install.sh |bash 12 | ``` 13 | ## 添加 Redis 支持(Debian 8): 14 | ``` 15 | # 安装 redis 支持 16 | apt-get install -y redis-server php5-redis 17 | # 重启 fcgiwrap-php 进程 18 | bash /etc/init.d/fcgiwrap-php restart 19 | # 后台选择 redis 模式, 并更新缓存. 20 | ``` 21 | ## 预览地址 22 | [萌咖 存储仓库](https://moeclub.org/onedrive/) 23 | 24 | ## 创意整合 25 | 1.极大简化安装步骤。 26 | 2.一些样式美化修改。 27 | 3.分页模式,加快页面预览速度。创意来自[oneindex-h](https://github.com/hang666/oneindex-h) 28 | 4.可后台自定义网站主标题,副标题。 29 | 5.可后台自定义每页显示项目数量。 30 | 31 | ## 重新安装 32 | 删除 oneindex/config 下的所有文件即可. 33 | 一键安装的地址: /var/www/oneindex/config 34 | 35 | ## change log: 36 | 18-03-29: 更新直链获取机制、缓存机制,避免频繁访问的token失效 37 | 18-03-29: 解决非英文编码问题 38 | 18-03-29: 添加onedrive共享的起始目录 功能 39 | 18-03-29: 添加rewrite的配置文件 40 | 18-03-29: 增加sqlite模式cache支持 41 | 18-03-29: 添加缩略图功能 42 | 18-03-29: 添加404判断 43 | 18-03-31: 添加console 44 | 18-04-13: 修复特殊文件名无法下载问题 45 | 18-04-13: 添加命令行上传功能 46 | 18-04-16: 更新 2.0 beta 47 | 18-04-16: 更新展示界面 48 | 18-04-16: 响应式,支持小屏设备 49 | 18-04-16: 图片在线预览 50 | 18-04-16: 视频在线播放 51 | 18-04-16: 代码在线查看(js、css、html、sh、php、java、md等) 52 | 18-04-16: README.md 支持,解析各目录下(onedirive目录下) README.md 文件,在页面尾部展示。 53 | 18-04-18: 音频在线播放 54 | 18-04-18: HEAD.md 支持,在页面头部展示 55 | 18-04-18: .password 文件夹加密 56 | 18-05-06: 在线视频播放器替换成 Dplayer 57 | 18-05-06: 在线视频播放支持'mp4','webm','avi','mpg', 'mpeg', 'rm', 'rmvb', 'mov', 'wmv', 'mkv', 'asf' 58 | 18-06-01: 支持个人账号 59 | 18-06-01: cli文件夹上传(单线程) 60 | 18-06-01: 管理后台(后台地址:?/admin 默认密码:oneindex) 61 | 18-06-01: 不同后缀展示设置 62 | 18-06-01: 文件直接输出 63 | 18-06-01: 文件上传管理(后台) 64 | 18-06-01: 增加index.html特性 65 | 18-06-01: 图床功能 66 | 67 | ## 需求: 68 | 1、PHP空间,PHP 5.6+ 打开curl支持 69 | 2、onedrive 账号 (个人、企业版或教育版/工作或学校帐户) 70 | 3、oneindex 程序 71 | 72 | ## 安装: 73 | image 74 | 75 | 76 | ## 计划任务   77 | [可选]**推荐配置**,非必需。后台定时刷新缓存,可增加前台访问的速度   78 | ``` 79 | # 每小时刷新一次token 80 | 0 * * * * /具体路径/php /程序具体路径/one.php token:refresh 81 | 82 | # 每十分钟后台刷新一遍缓存 83 | */10 * * * * /具体路径/php /程序具体路径/one.php cache:refresh 84 | ``` 85 | 86 | ## 特殊文件实现功能   87 | ` README.md `、`HEAD.md` 、 `.password`特殊文件使用 88 | 89 | 可以参考[https://github.com/0oVicero0/oneindex/tree/files](https://github.com/0oVicero0/oneindex/tree/files) 90 | 91 | **在文件夹底部添加说明:**   92 | >在onedrive的文件夹中添加` README.md `文件,使用markdown语法。 93 | 94 | **在文件夹头部添加说明:**   95 | >在onedrive的文件夹中添加`HEAD.md` 文件,使用markdown语法。   96 | 97 | **加密文件夹:**   98 | >在onedrive的文件夹中添加`.password`文件,填入密码,密码不能为空。   99 | 100 | **直接输出网页:** 101 | >在onedrive的文件夹中添加`index.html` 文件,程序会直接输出网页而不列目录。 102 | >配合 文件展示设置-直接输出 效果更佳 103 | 104 | ## 命令行功能   105 | 仅能在php cli模式下运行 106 | **清除缓存:**   107 | ``` 108 | php one.php cache:clear 109 | ``` 110 | **刷新缓存:**   111 | ``` 112 | php one.php cache:refresh 113 | ``` 114 | **刷新令牌:**   115 | ``` 116 | php one.php token:refresh 117 | ``` 118 | **上传文件:**   119 | ``` 120 | php one.php upload:file 本地文件 [onedrive文件] 121 | ``` 122 | -------------------------------------------------------------------------------- /cache/README.md: -------------------------------------------------------------------------------- 1 | # Directory Index 2 | -------------------------------------------------------------------------------- /config/README.md: -------------------------------------------------------------------------------- 1 | # Directory Index 2 | -------------------------------------------------------------------------------- /controller/AdminController.php: -------------------------------------------------------------------------------- 1 | 'OneIndex', 6 | 'title_name' => 'Index of /', 7 | 'password' => 'oneindex', 8 | 'style'=>'nexmoe', 9 | 'onedrive_root' =>'', 10 | 'cache_type'=>'filecache', 11 | 'cache_expire_time' => 3600, 12 | 'cache_refresh_time' => 600, 13 | 'page_item' => 23, 14 | 'root_path' => '?', 15 | 'show'=> array ( 16 | 'stream'=>['txt'], 17 | 'image' => ['bmp','jpg','jpeg','png','gif'], 18 | 'video5'=>['mp4','webm','mkv'], 19 | 'video'=>[], 20 | 'video2'=>['avi','mpg', 'mpeg', 'rm', 'rmvb', 'mov', 'wmv', 'asf', 'ts', 'flv'], 21 | 'audio'=>['ogg','mp3','wav'], 22 | 'code'=>['html','htm','php', 'css', 'go','java','js','json','txt','sh','md'], 23 | 'doc'=>['csv','doc','docx','odp','ods','odt','pot','potm','potx','pps','ppsx','ppsxm','ppt','pptm','pptx','rtf','xls','xlsx'] 24 | ), 25 | 'images'=>['home'=>false,'public'=>false, 'exts'=>['jpg','png','gif','bmp']], 26 | ); 27 | 28 | function __construct(){ 29 | } 30 | 31 | function login(){ 32 | if(!empty($_POST['password']) && $_POST['password'] == config('password')){ 33 | setcookie('admin', md5(config('password').config('refresh_token')) ); 34 | return view::direct(get_absolute_path(dirname($_SERVER['SCRIPT_NAME'])).'?/admin/'); 35 | } 36 | return view::load('login')->with('title', '系统管理'); 37 | } 38 | 39 | function logout(){ 40 | setcookie('admin', '' ); 41 | return view::direct(get_absolute_path(dirname($_SERVER['SCRIPT_NAME'])).'?/login'); 42 | } 43 | 44 | function settings(){ 45 | 46 | if($_POST){ 47 | 48 | config('site_name',$_POST['site_name']); 49 | config('title_name',$_POST['title_name']); 50 | config('style',$_POST['style']); 51 | 52 | config('onedrive_root',get_absolute_path($_POST['onedrive_root'])); 53 | 54 | config('onedrive_hide',$_POST['onedrive_hide']); 55 | 56 | config('cache_type',$_POST['cache_type']); 57 | config('cache_expire_time',intval($_POST['cache_expire_time'])); 58 | config('page_item',intval($_POST['page_item'])); 59 | 60 | $_POST['root_path'] = empty($_POST['root_path'])?'?':''; 61 | config('root_path',$_POST['root_path']); 62 | } 63 | $config = config('@base'); 64 | return view::load('settings')->with('config', $config); 65 | } 66 | 67 | function cache(){ 68 | if(!is_null($_POST['clear'])){ 69 | cache::clear(); 70 | $message = "清除缓存成功"; 71 | }elseif ( !is_null($_POST['refresh']) ){ 72 | oneindex::refresh_cache(get_absolute_path(config('onedrive_root'))); 73 | $message = "重建缓存成功"; 74 | } 75 | return view::load('cache')->with('message', $message); 76 | } 77 | 78 | function images(){ 79 | if($_POST){ 80 | $config['home'] = empty($_POST['home'])?false:true; 81 | $config['public'] = empty($_POST['public'])?false:true; 82 | $config['exts'] = explode(" ", $_POST['exts']); 83 | config('images@base',$config); 84 | } 85 | $config = config('images@base'); 86 | return view::load('images')->with('config', $config);; 87 | } 88 | 89 | 90 | function show(){ 91 | if(!empty($_POST) ){ 92 | foreach($_POST as $n=>$ext){ 93 | $show[$n] = explode(' ', $ext); 94 | } 95 | config('show', $show); 96 | } 97 | $names = [ 98 | 'stream'=>'直接输出(<5M),走本服务器流量(stream)', 99 | 'image' =>'图片(image)', 100 | 'video'=>'Dplayer 视频(video)', 101 | 'video2'=>'Dplayer DASH 视频(video2)/个人版账户不支持', 102 | 'video5'=>'html5视频(video5)', 103 | 'audio'=>'音频播放(audio)', 104 | 'code'=>'文本/代码(code)', 105 | 'doc'=>'文档(doc)' 106 | ]; 107 | $show = config('show'); 108 | return view::load('show')->with('names', $names)->with('show', $show); 109 | } 110 | 111 | function setpass(){ 112 | if($_SERVER['REQUEST_METHOD'] == 'POST'){ 113 | if($_POST['old_pass'] == config('password')){ 114 | if($_POST['password'] == $_POST['password2']){ 115 | config('password', $_POST['password']); 116 | $message = "修改成功"; 117 | }else{ 118 | $message = "两次密码不一致,修改失败"; 119 | } 120 | }else{ 121 | $message = "原密码错误,修改失败"; 122 | } 123 | } 124 | return view::load('setpass')->with('message', $message); 125 | } 126 | 127 | function install(){ 128 | if(!empty($_GET['code'])){ 129 | return $this->install_3(); 130 | } 131 | switch ( intval($_GET['step']) ){ 132 | case 1: 133 | return $this->install_1(); 134 | case 2: 135 | return $this->install_2(); 136 | default: 137 | return $this->install_0(); 138 | } 139 | } 140 | 141 | function install_0(){ 142 | $check['php'] = version_compare(PHP_VERSION,'5.5.0','ge'); 143 | $check['curl'] = function_exists('curl_init'); 144 | $check['config'] = is_writable(ROOT.'config/'); 145 | $check['cache'] = is_writable(ROOT.'cache/'); 146 | 147 | return view::load('install/install_0')->with('title','系统安装') 148 | ->with('check', $check); 149 | } 150 | 151 | function install_1(){ 152 | if(!empty($_POST['client_secret']) && !empty($_POST['client_id']) && !empty($_POST['redirect_uri']) ){ 153 | config('@base', self::$default_config); 154 | config('client_secret',$_POST['client_secret']); 155 | config('client_id',$_POST['client_id']); 156 | config('redirect_uri',$_POST['redirect_uri']); 157 | return view::direct('?step=2'); 158 | } 159 | if($_SERVER['HTTP_HOST'] == 'localhost'){ 160 | $redirect_uri = 'http://'.$_SERVER['HTTP_HOST'].get_absolute_path(dirname($_SERVER['PHP_SELF'])); 161 | }else{ 162 | // 调用 https://moeclub.org/onedrive-login 中转 163 | $redirect_uri = 'https://moeclub.org/onedrive-login'; 164 | } 165 | 166 | $oauth_url = 'https://login.microsoftonline.com/common/oauth2/authorize'; 167 | $app_url = "{$oauth_url}?response_type=code&client_id={$client_id}&redirect_uri={$redirect_uri}"; 168 | return view::load('install/install_1')->with('title','系统安装') 169 | ->with('redirect_uri', $redirect_uri) 170 | ->with('app_url', $app_url); 171 | } 172 | 173 | function install_2(){ 174 | return view::load('install/install_2')->with('title','系统安装'); 175 | } 176 | 177 | function install_3(){ 178 | $data = onedrive::authorize($_GET['code']); 179 | if(!empty($data['refresh_token'])){ 180 | config('refresh_token',$data['refresh_token']); 181 | config('@token', $data); 182 | } 183 | return view::load('install/install_3')->with('refresh_token',$data['refresh_token']); 184 | 185 | } 186 | } 187 | -------------------------------------------------------------------------------- /controller/ImagesController.php: -------------------------------------------------------------------------------- 1 | is_image($_FILES["file"]) ){ 15 | $content = file_get_contents( $_FILES["file"]['tmp_name']); 16 | $remotepath = 'images/'.date('Y/m/d/').$this->generateRandomString(10).'/'; 17 | $remotefile = $remotepath.$_FILES["file"]['name']; 18 | $result = onedrive::upload(config('onedrive_root').$remotefile, $content); 19 | 20 | if($result){ 21 | $root = get_absolute_path(dirname($_SERVER['SCRIPT_NAME'])).config('root_path'); 22 | $http_type = ((isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') || (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https')) ? 'https://' : 'http://'; 23 | $url = $_SERVER['HTTP_HOST'].$root.'/'.$remotefile.((config('root_path') == '?')?'&s':'?s'); 24 | $url = $http_type.str_replace('//','/', $url); 25 | view::direct($url); 26 | } 27 | } 28 | return view::load('images/index'); 29 | } 30 | 31 | function is_image($file){ 32 | $config = config('images@base'); 33 | $ext = strtolower(pathinfo($file['name'], PATHINFO_EXTENSION)); 34 | if(!in_array($ext,$config['exts'])){ 35 | return false; 36 | } 37 | if($file['size'] > 10485760 || $file['size'] == 0){ 38 | return false; 39 | } 40 | 41 | return true; 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /controller/IndexController.php: -------------------------------------------------------------------------------- 1 | z_page = config('page_item'); 12 | 13 | //获取路径和文件名 14 | $paths = explode('/', rawurldecode($_GET['path'])); 15 | if(substr($_SERVER['REQUEST_URI'], -1) != '/'){ 16 | $this->name = array_pop($paths); 17 | } 18 | 19 | preg_match_all("(\.page\-([0-9]*)/$)",get_absolute_path(join('/', $paths)),$mat); 20 | if(empty($mat[1][0])){ 21 | $this->page = 1; 22 | } else { 23 | $this->page = $mat[1][0]; 24 | } 25 | 26 | $this->url_path = preg_replace("(\.page\-[0-9]*/$)","",get_absolute_path(join('/', $paths))); 27 | 28 | $this->path = get_absolute_path(config('onedrive_root').$this->url_path); 29 | //获取文件夹下所有元素 30 | $this->items = $this->items($this->path); 31 | } 32 | 33 | 34 | function index(){ 35 | //是否404 36 | $this->is404(); 37 | 38 | $this->is_password(); 39 | 40 | header("Expires:-1"); 41 | header("Cache-Control:no_cache"); 42 | header("Pragma:no-cache"); 43 | 44 | if(!empty($this->name)){//file 45 | return $this->file(); 46 | }else{//dir 47 | return $this->dir(); 48 | } 49 | } 50 | 51 | //判断是否加密 52 | function is_password(){ 53 | if(empty($this->items['.password'])){ 54 | return false; 55 | }else{ 56 | $this->items['.password']['path'] = get_absolute_path($this->path).'.password'; 57 | } 58 | 59 | $password = $this->get_content($this->items['.password']); 60 | list($password) = explode("\n",$password); 61 | $password = trim($password); 62 | unset($this->items['.password']); 63 | if(!empty($password) && strcmp($password, $_COOKIE[md5($this->path)]) === 0){ 64 | return true; 65 | } 66 | 67 | $this->password($password); 68 | 69 | } 70 | 71 | function password($password){ 72 | if(!empty($_POST['password']) && strcmp($password, $_POST['password']) === 0){ 73 | setcookie(md5($this->path), $_POST['password']); 74 | return true; 75 | } 76 | $navs = $this->navs(); 77 | echo view::load('password')->with('navs',$navs); 78 | exit(); 79 | } 80 | 81 | //文件 82 | function file(){ 83 | $item = $this->items[$this->name]; 84 | if ($item['folder']) {//是文件夹 85 | $url = $_SERVER['REQUEST_URI'].'/'; 86 | }elseif(!is_null($_GET['t']) ){//缩略图 87 | $url = $this->thumbnail($item); 88 | }elseif($_SERVER['REQUEST_METHOD'] == 'POST' || !is_null($_GET['s']) ){ 89 | return $this->show($item); 90 | }else{//返回下载链接 91 | $url = $item['downloadUrl']; 92 | } 93 | header('Location: '.$url); 94 | } 95 | 96 | 97 | 98 | //文件夹 99 | function dir(){ 100 | $root = get_absolute_path(dirname($_SERVER['SCRIPT_NAME'])).config('root_path'); 101 | $navs = $this->navs(); 102 | 103 | if($this->items['index.html']){ 104 | $this->items['index.html']['path'] = get_absolute_path($this->path).'index.html'; 105 | $index = $this->get_content($this->items['index.html']); 106 | header('Content-type: text/html'); 107 | echo $index; 108 | exit(); 109 | } 110 | 111 | if($this->items['README.md']){ 112 | $this->items['README.md']['path'] = get_absolute_path($this->path).'README.md'; 113 | $readme = $this->get_content($this->items['README.md']); 114 | $Parsedown = new Parsedown(); 115 | $readme = $Parsedown->text($readme); 116 | //不在列表中展示 117 | unset($this->items['README.md']); 118 | } 119 | 120 | if($this->items['HEAD.md']){ 121 | $this->items['HEAD.md']['path'] = get_absolute_path($this->path).'HEAD.md'; 122 | $head = $this->get_content($this->items['HEAD.md']); 123 | $Parsedown = new Parsedown(); 124 | $head = $Parsedown->text($head); 125 | //不在列表中展示 126 | unset($this->items['HEAD.md']); 127 | } 128 | 129 | $this->totalpage = ceil(count($this->items) / $this->z_page); 130 | 131 | if($this->page*$this->z_page >= count($this->items)) 132 | $this->page = $this->totalpage; 133 | 134 | return view::load('list')->with('title', config('title_name')) 135 | ->with('navs', $navs) 136 | ->with('path',join("/", array_map("rawurlencode", explode("/", $this->url_path)))) 137 | ->with('root', $root) 138 | ->with('items', array_slice($this->items,$this->z_page*($this->page-1),$this->z_page)) 139 | ->with('head',$head) 140 | ->with('readme',$readme) 141 | ->with('page',$this->page) 142 | ->with('totalpage',$this->totalpage); 143 | } 144 | 145 | function show($item){ 146 | $root = get_absolute_path(dirname($_SERVER['SCRIPT_NAME'])).(config('root_path')?'?/':''); 147 | $ext = strtolower(pathinfo($item['name'], PATHINFO_EXTENSION)); 148 | $data['title'] = $item['name']; 149 | $data['navs'] = $this->navs(); 150 | $data['item'] = $item; 151 | $data['ext'] = $ext; 152 | $data['item']['path'] = get_absolute_path($this->path).$this->name; 153 | $http_type = ((isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') || (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https')) ? 'https://' : 'http://'; 154 | $uri = onedrive::urlencode(get_absolute_path($this->url_path.'/'.$this->name)); 155 | $data['url'] = $http_type.$_SERVER['HTTP_HOST'].$root.$uri; 156 | 157 | 158 | $show = config('show'); 159 | foreach($show as $n=>$exts){ 160 | if(in_array($ext,$exts)){ 161 | return view::load('show/'.$n)->with($data); 162 | } 163 | } 164 | 165 | header('Location: '.$item['downloadUrl']); 166 | } 167 | //缩略图 168 | function thumbnail($item){ 169 | if(!empty($_GET['t'])){ 170 | list($width, $height) = explode('|', $_GET['t']); 171 | }else{ 172 | //800 176 96 173 | $width = $height = 800; 174 | } 175 | $item['thumb'] = onedrive::thumbnail($this->path.$this->name); 176 | list($item['thumb'],$tmp) = explode('&width=', $item['thumb']); 177 | $item['thumb'] .= strpos($item['thumb'], '?')?'&':'?'; 178 | return $item['thumb']."width={$width}&height={$height}"; 179 | } 180 | 181 | //文件夹下元素 182 | function items($path, $fetch=false){ 183 | $items = cache::get('dir_'.$this->path, function(){ 184 | return onedrive::dir($this->path); 185 | }, config('cache_expire_time')); 186 | return $items; 187 | } 188 | 189 | function navs(){ 190 | $root = get_absolute_path(dirname($_SERVER['SCRIPT_NAME'])).config('root_path'); 191 | $navs['/'] = get_absolute_path($root.'/'); 192 | foreach(explode('/',$this->url_path) as $v){ 193 | if(empty($v)){ 194 | continue; 195 | } 196 | $navs[rawurldecode($v)] = end($navs).$v.'/'; 197 | } 198 | if(!empty($this->name)){ 199 | $navs[$this->name] = end($navs).urlencode($this->name); 200 | } 201 | 202 | return $navs; 203 | } 204 | 205 | static function get_content($item){ 206 | $content = cache::get('content_'.$item['path'], function() use ($item){ 207 | $resp = fetch::get($item['downloadUrl']); 208 | if($resp->http_code == 200){ 209 | return $resp->content; 210 | } 211 | }, config('cache_expire_time') ); 212 | return $content; 213 | } 214 | 215 | //404 216 | function is404(){ 217 | if(!empty($this->items[$this->name]) || (empty($this->name) && is_array($this->items)) ){ 218 | return false; 219 | } 220 | 221 | http_response_code(404); 222 | view::load('404')->show(); 223 | die(); 224 | } 225 | 226 | function __destruct(){ 227 | if (!function_exists("fastcgi_finish_request")) { 228 | return; 229 | } 230 | } 231 | } 232 | -------------------------------------------------------------------------------- /controller/UploadController.php: -------------------------------------------------------------------------------- 1 | add_task($local, $remotepath); 11 | $message = "文件".$local."已添加到队列"; 12 | }elseif(is_dir($local)){ 13 | $this->scan_dir($local, $remotepath); 14 | $message = "文件夹".$local."已添加到队列"; 15 | }elseif($local == realpath('.')){ 16 | $message = "因为安全原因,程序文件夹根目录不能上传"; 17 | }else{ 18 | $message = "文件不存在"; 19 | } 20 | $request = $this->task_request(); 21 | $request['url'] = substr($request['url'],0,-4).'run'; 22 | fetch::post($request); 23 | }elseif(!empty($_POST['begin_task'])){ 24 | $this->task($_POST['begin_task']); 25 | }elseif(!empty($_POST['delete_task'])){ 26 | unset($_POST['delete_task']); 27 | config('@upload', (array)$uploads); 28 | }elseif(!empty($_POST['empty_uploaded'])){ 29 | config('@uploaded', array()); 30 | } 31 | $uploading = config('@upload'); 32 | $uploaded = array_reverse((array)config('@uploaded')); 33 | return view::load('upload')->with('uploading', $uploading)->with('uploaded', $uploaded)->with('message', $message); 34 | } 35 | 36 | //扫描文件夹,添加到任务队列 37 | private function scan_dir($localpath, $remotepath){ 38 | $files = scandir($localpath); 39 | foreach ($files as $file) { 40 | if ($file == '.' || $file == '..') { 41 | continue; 42 | } 43 | if (is_dir($localpath . '/' . $file)) { 44 | $this->scan_dir($localpath . '/' . $file, $remotepath.$file.'/'); 45 | }else{ 46 | $localfile = realpath($localpath . '/' . $file); 47 | $remotefile = $remotepath.$file; 48 | $this->add_task($localfile, $remotefile); 49 | } 50 | } 51 | } 52 | 53 | private function add_task($localfile, $remotefile){ 54 | $task = array( 55 | 'localfile'=>$localfile, 56 | 'remotepath' => $remotefile, 57 | 'filesize'=>onedrive::_filesize($localfile), 58 | 'upload_type'=>'web', 59 | 'update_time'=>0, 60 | ); 61 | 62 | $uploads = (array)config('@upload'); 63 | if(empty($uploads[$remotefile])){ 64 | $uploads[$remotefile] = $task; 65 | config('@upload', $uploads); 66 | } 67 | } 68 | 69 | //运行队列中的任务 70 | function run(){ 71 | $uploads = (array)config('@upload'); 72 | $time = time(); 73 | $runing = 0; 74 | foreach($uploads as $task){ 75 | if($time < ($task['update_time']+60) AND $task['type']=='web' ){ 76 | $runing = $runing +1; 77 | } 78 | if($runing > 5)break; 79 | } 80 | 81 | foreach($uploads as $remotepath=>$task){ 82 | if($time < ($task['update_time']+60) OR !is_array($task) ){ 83 | continue; 84 | } 85 | $runing = $runing +1; 86 | print $remotepath.PHP_EOL; 87 | fetch::post($this->task_request($remotepath)); 88 | if($runing > 5)break; 89 | } 90 | 91 | if(count($uploads) > 5){ 92 | set_time_limit(100); 93 | sleep(60); 94 | $request = $this->task_request(); 95 | $request['url'] = substr($request['url'],0,-4).'run'; 96 | fetch::get($request); 97 | } 98 | } 99 | 100 | private function task_request($remotepath=''){ 101 | $request['headers'] = "Cookie: admin=".md5(config('password').config('refresh_token')).PHP_EOL; 102 | $request['headers'] .= "Host: ".$_SERVER['HTTP_HOST']; 103 | $request['curl_opt']=[CURLOPT_CONNECTTIMEOUT => 1,CURLOPT_TIMEOUT=>1,CURLOPT_FOLLOWLOCATION=>true]; 104 | $http_type = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') ? 'https://' : 'http://'; 105 | $request['url'] = $http_type.'127.0.0.1'.get_absolute_path(dirname($_SERVER['PHP_SELF'])).'?/admin/upload/task'; 106 | $request['post_data'] = 'remotepath='.urlencode($remotepath); 107 | return $request; 108 | } 109 | 110 | //执行任务 111 | function task($remotepath=null){ 112 | $remotepath = is_null($remotepath)?$_POST['remotepath']:$remotepath; 113 | //file_put_contents('log.txt',$remotepath.PHP_EOL, FILE_APPEND); 114 | $uploads = config('@upload'); 115 | $task = $uploads[$remotepath]; 116 | 117 | if(empty($task)){ 118 | return; 119 | } 120 | if($task['filesize'] < 10485760){ 121 | @onedrive::upload($task['remotepath'], file_get_contents($task['localfile'])); 122 | unset($uploads[$remotepath]); 123 | 124 | config('@upload', (array)$uploads); 125 | config($remotepath.'@uploaded','success'); 126 | }else{ 127 | $uploads[$remotepath]['update_time'] = time(); 128 | config('@upload', (array)$uploads); 129 | $this->upload_large_file($task); 130 | } 131 | } 132 | 133 | function upload_large_file($task){ 134 | 135 | 136 | //创建上传会话 137 | if(empty($task['url'])){ 138 | $data = onedrive::create_upload_session($task['remotepath']); 139 | if(!empty($data['uploadUrl'])){ 140 | $task['url'] = $data['uploadUrl']; 141 | $task['offset'] = 0; 142 | $task['length'] = 327680; 143 | $task['update_time'] = time(); 144 | config($task['remotepath'].'@upload',$task); 145 | }elseif ( $data === false ){ 146 | $uploads = config('@upload'); 147 | unset($uploads[$task['remotepath']]); 148 | config('@upload', $uploads); 149 | config($task['remotepath'].'@uploaded','exists'); 150 | } 151 | }else{ 152 | $begin_time = microtime(true); 153 | set_time_limit(0); 154 | $data = onedrive::upload_session($task['url'], $task['localfile'], $task['offset'], $task['length']); 155 | if(!empty($data['nextExpectedRanges'])){ 156 | //继续上传 157 | $upload_time = microtime(true) - $begin_time; 158 | $task['speed'] = $task['length']/$upload_time; 159 | $task['length'] = intval($task['length']/$upload_time/32768*2)*327680; 160 | $task['length'] = ($task['length']>104857600)?104857600:$task['length']; 161 | list($offset, $filesize) = explode('-',$data['nextExpectedRanges'][0]); 162 | $task['offset'] = intval($offset); 163 | $info['update_time'] = time(); 164 | config($task['remotepath'].'@upload',$task); 165 | }elseif(!empty($data['@content.downloadUrl']) || !empty($data['id'])){ 166 | //上传完成 167 | unset($uploads[$task['remotepath']]); 168 | config('@upload', $uploads); 169 | config($task['remotepath'].'@uploaded','success'); 170 | return; 171 | }else{ 172 | //失败,重新获取信息 173 | echo "re get url"; 174 | $data = onedrive::upload_session_status($task['url']); 175 | if(empty($data)|| $info['length']<100){ 176 | onedrive::delete_upload_session($task['url']); 177 | unset($task['url']); 178 | config($task['remotepath'].'@upload', $task); 179 | }elseif(!empty($data['nextExpectedRanges'])){ 180 | list($offset, $filesize) = explode('-',$data['nextExpectedRanges'][0]); 181 | $task['offset'] = intval($offset); 182 | $task['length'] = $task['length']/1.5; 183 | config($task['remotepath'].'@upload', $task); 184 | } 185 | } 186 | } 187 | $request= $this->task_request($task['remotepath']); 188 | $resp = fetch::post($request); 189 | //var_dump($resp); 190 | } 191 | 192 | 193 | } 194 | -------------------------------------------------------------------------------- /index.php: -------------------------------------------------------------------------------- 1 | DefinitionData = array(); 28 | 29 | # standardize line breaks 30 | $text = str_replace(array("\r\n", "\r"), "\n", $text); 31 | 32 | # remove surrounding line breaks 33 | $text = trim($text, "\n"); 34 | 35 | # split text into lines 36 | $lines = explode("\n", $text); 37 | 38 | # iterate through lines to identify blocks 39 | $markup = $this->lines($lines); 40 | 41 | # trim line breaks 42 | $markup = trim($markup, "\n"); 43 | 44 | return $markup; 45 | } 46 | 47 | # 48 | # Setters 49 | # 50 | 51 | function setBreaksEnabled($breaksEnabled) 52 | { 53 | $this->breaksEnabled = $breaksEnabled; 54 | 55 | return $this; 56 | } 57 | 58 | protected $breaksEnabled; 59 | 60 | function setMarkupEscaped($markupEscaped) 61 | { 62 | $this->markupEscaped = $markupEscaped; 63 | 64 | return $this; 65 | } 66 | 67 | protected $markupEscaped; 68 | 69 | function setUrlsLinked($urlsLinked) 70 | { 71 | $this->urlsLinked = $urlsLinked; 72 | 73 | return $this; 74 | } 75 | 76 | protected $urlsLinked = true; 77 | 78 | function setSafeMode($safeMode) 79 | { 80 | $this->safeMode = (bool) $safeMode; 81 | 82 | return $this; 83 | } 84 | 85 | protected $safeMode; 86 | 87 | protected $safeLinksWhitelist = array( 88 | 'http://', 89 | 'https://', 90 | 'ftp://', 91 | 'ftps://', 92 | 'mailto:', 93 | 'data:image/png;base64,', 94 | 'data:image/gif;base64,', 95 | 'data:image/jpeg;base64,', 96 | 'irc:', 97 | 'ircs:', 98 | 'git:', 99 | 'ssh:', 100 | 'news:', 101 | 'steam:', 102 | ); 103 | 104 | # 105 | # Lines 106 | # 107 | 108 | protected $BlockTypes = array( 109 | '#' => array('Header'), 110 | '*' => array('Rule', 'List'), 111 | '+' => array('List'), 112 | '-' => array('SetextHeader', 'Table', 'Rule', 'List'), 113 | '0' => array('List'), 114 | '1' => array('List'), 115 | '2' => array('List'), 116 | '3' => array('List'), 117 | '4' => array('List'), 118 | '5' => array('List'), 119 | '6' => array('List'), 120 | '7' => array('List'), 121 | '8' => array('List'), 122 | '9' => array('List'), 123 | ':' => array('Table'), 124 | '<' => array('Comment', 'Markup'), 125 | '=' => array('SetextHeader'), 126 | '>' => array('Quote'), 127 | '[' => array('Reference'), 128 | '_' => array('Rule'), 129 | '`' => array('FencedCode'), 130 | '|' => array('Table'), 131 | '~' => array('FencedCode'), 132 | ); 133 | 134 | # ~ 135 | 136 | protected $unmarkedBlockTypes = array( 137 | 'Code', 138 | ); 139 | 140 | # 141 | # Blocks 142 | # 143 | 144 | protected function lines(array $lines) 145 | { 146 | $CurrentBlock = null; 147 | 148 | foreach ($lines as $line) 149 | { 150 | if (chop($line) === '') 151 | { 152 | if (isset($CurrentBlock)) 153 | { 154 | $CurrentBlock['interrupted'] = true; 155 | } 156 | 157 | continue; 158 | } 159 | 160 | if (strpos($line, "\t") !== false) 161 | { 162 | $parts = explode("\t", $line); 163 | 164 | $line = $parts[0]; 165 | 166 | unset($parts[0]); 167 | 168 | foreach ($parts as $part) 169 | { 170 | $shortage = 4 - mb_strlen($line, 'utf-8') % 4; 171 | 172 | $line .= str_repeat(' ', $shortage); 173 | $line .= $part; 174 | } 175 | } 176 | 177 | $indent = 0; 178 | 179 | while (isset($line[$indent]) and $line[$indent] === ' ') 180 | { 181 | $indent ++; 182 | } 183 | 184 | $text = $indent > 0 ? substr($line, $indent) : $line; 185 | 186 | # ~ 187 | 188 | $Line = array('body' => $line, 'indent' => $indent, 'text' => $text); 189 | 190 | # ~ 191 | 192 | if (isset($CurrentBlock['continuable'])) 193 | { 194 | $Block = $this->{'block'.$CurrentBlock['type'].'Continue'}($Line, $CurrentBlock); 195 | 196 | if (isset($Block)) 197 | { 198 | $CurrentBlock = $Block; 199 | 200 | continue; 201 | } 202 | else 203 | { 204 | if ($this->isBlockCompletable($CurrentBlock['type'])) 205 | { 206 | $CurrentBlock = $this->{'block'.$CurrentBlock['type'].'Complete'}($CurrentBlock); 207 | } 208 | } 209 | } 210 | 211 | # ~ 212 | 213 | $marker = $text[0]; 214 | 215 | # ~ 216 | 217 | $blockTypes = $this->unmarkedBlockTypes; 218 | 219 | if (isset($this->BlockTypes[$marker])) 220 | { 221 | foreach ($this->BlockTypes[$marker] as $blockType) 222 | { 223 | $blockTypes []= $blockType; 224 | } 225 | } 226 | 227 | # 228 | # ~ 229 | 230 | foreach ($blockTypes as $blockType) 231 | { 232 | $Block = $this->{'block'.$blockType}($Line, $CurrentBlock); 233 | 234 | if (isset($Block)) 235 | { 236 | $Block['type'] = $blockType; 237 | 238 | if ( ! isset($Block['identified'])) 239 | { 240 | $Blocks []= $CurrentBlock; 241 | 242 | $Block['identified'] = true; 243 | } 244 | 245 | if ($this->isBlockContinuable($blockType)) 246 | { 247 | $Block['continuable'] = true; 248 | } 249 | 250 | $CurrentBlock = $Block; 251 | 252 | continue 2; 253 | } 254 | } 255 | 256 | # ~ 257 | 258 | if (isset($CurrentBlock) and ! isset($CurrentBlock['type']) and ! isset($CurrentBlock['interrupted'])) 259 | { 260 | $CurrentBlock['element']['text'] .= "\n".$text; 261 | } 262 | else 263 | { 264 | $Blocks []= $CurrentBlock; 265 | 266 | $CurrentBlock = $this->paragraph($Line); 267 | 268 | $CurrentBlock['identified'] = true; 269 | } 270 | } 271 | 272 | # ~ 273 | 274 | if (isset($CurrentBlock['continuable']) and $this->isBlockCompletable($CurrentBlock['type'])) 275 | { 276 | $CurrentBlock = $this->{'block'.$CurrentBlock['type'].'Complete'}($CurrentBlock); 277 | } 278 | 279 | # ~ 280 | 281 | $Blocks []= $CurrentBlock; 282 | 283 | unset($Blocks[0]); 284 | 285 | # ~ 286 | 287 | $markup = ''; 288 | 289 | foreach ($Blocks as $Block) 290 | { 291 | if (isset($Block['hidden'])) 292 | { 293 | continue; 294 | } 295 | 296 | $markup .= "\n"; 297 | $markup .= isset($Block['markup']) ? $Block['markup'] : $this->element($Block['element']); 298 | } 299 | 300 | $markup .= "\n"; 301 | 302 | # ~ 303 | 304 | return $markup; 305 | } 306 | 307 | protected function isBlockContinuable($Type) 308 | { 309 | return method_exists($this, 'block'.$Type.'Continue'); 310 | } 311 | 312 | protected function isBlockCompletable($Type) 313 | { 314 | return method_exists($this, 'block'.$Type.'Complete'); 315 | } 316 | 317 | # 318 | # Code 319 | 320 | protected function blockCode($Line, $Block = null) 321 | { 322 | if (isset($Block) and ! isset($Block['type']) and ! isset($Block['interrupted'])) 323 | { 324 | return; 325 | } 326 | 327 | if ($Line['indent'] >= 4) 328 | { 329 | $text = substr($Line['body'], 4); 330 | 331 | $Block = array( 332 | 'element' => array( 333 | 'name' => 'pre', 334 | 'handler' => 'element', 335 | 'text' => array( 336 | 'name' => 'code', 337 | 'text' => $text, 338 | ), 339 | ), 340 | ); 341 | 342 | return $Block; 343 | } 344 | } 345 | 346 | protected function blockCodeContinue($Line, $Block) 347 | { 348 | if ($Line['indent'] >= 4) 349 | { 350 | if (isset($Block['interrupted'])) 351 | { 352 | $Block['element']['text']['text'] .= "\n"; 353 | 354 | unset($Block['interrupted']); 355 | } 356 | 357 | $Block['element']['text']['text'] .= "\n"; 358 | 359 | $text = substr($Line['body'], 4); 360 | 361 | $Block['element']['text']['text'] .= $text; 362 | 363 | return $Block; 364 | } 365 | } 366 | 367 | protected function blockCodeComplete($Block) 368 | { 369 | $text = $Block['element']['text']['text']; 370 | 371 | $Block['element']['text']['text'] = $text; 372 | 373 | return $Block; 374 | } 375 | 376 | # 377 | # Comment 378 | 379 | protected function blockComment($Line) 380 | { 381 | if ($this->markupEscaped or $this->safeMode) 382 | { 383 | return; 384 | } 385 | 386 | if (isset($Line['text'][3]) and $Line['text'][3] === '-' and $Line['text'][2] === '-' and $Line['text'][1] === '!') 387 | { 388 | $Block = array( 389 | 'markup' => $Line['body'], 390 | ); 391 | 392 | if (preg_match('/-->$/', $Line['text'])) 393 | { 394 | $Block['closed'] = true; 395 | } 396 | 397 | return $Block; 398 | } 399 | } 400 | 401 | protected function blockCommentContinue($Line, array $Block) 402 | { 403 | if (isset($Block['closed'])) 404 | { 405 | return; 406 | } 407 | 408 | $Block['markup'] .= "\n" . $Line['body']; 409 | 410 | if (preg_match('/-->$/', $Line['text'])) 411 | { 412 | $Block['closed'] = true; 413 | } 414 | 415 | return $Block; 416 | } 417 | 418 | # 419 | # Fenced Code 420 | 421 | protected function blockFencedCode($Line) 422 | { 423 | if (preg_match('/^['.$Line['text'][0].']{3,}[ ]*([^`]+)?[ ]*$/', $Line['text'], $matches)) 424 | { 425 | $Element = array( 426 | 'name' => 'code', 427 | 'text' => '', 428 | ); 429 | 430 | if (isset($matches[1])) 431 | { 432 | $class = 'language-'.$matches[1]; 433 | 434 | $Element['attributes'] = array( 435 | 'class' => $class, 436 | ); 437 | } 438 | 439 | $Block = array( 440 | 'char' => $Line['text'][0], 441 | 'element' => array( 442 | 'name' => 'pre', 443 | 'handler' => 'element', 444 | 'text' => $Element, 445 | ), 446 | ); 447 | 448 | return $Block; 449 | } 450 | } 451 | 452 | protected function blockFencedCodeContinue($Line, $Block) 453 | { 454 | if (isset($Block['complete'])) 455 | { 456 | return; 457 | } 458 | 459 | if (isset($Block['interrupted'])) 460 | { 461 | $Block['element']['text']['text'] .= "\n"; 462 | 463 | unset($Block['interrupted']); 464 | } 465 | 466 | if (preg_match('/^'.$Block['char'].'{3,}[ ]*$/', $Line['text'])) 467 | { 468 | $Block['element']['text']['text'] = substr($Block['element']['text']['text'], 1); 469 | 470 | $Block['complete'] = true; 471 | 472 | return $Block; 473 | } 474 | 475 | $Block['element']['text']['text'] .= "\n".$Line['body']; 476 | 477 | return $Block; 478 | } 479 | 480 | protected function blockFencedCodeComplete($Block) 481 | { 482 | $text = $Block['element']['text']['text']; 483 | 484 | $Block['element']['text']['text'] = $text; 485 | 486 | return $Block; 487 | } 488 | 489 | # 490 | # Header 491 | 492 | protected function blockHeader($Line) 493 | { 494 | if (isset($Line['text'][1])) 495 | { 496 | $level = 1; 497 | 498 | while (isset($Line['text'][$level]) and $Line['text'][$level] === '#') 499 | { 500 | $level ++; 501 | } 502 | 503 | if ($level > 6) 504 | { 505 | return; 506 | } 507 | 508 | $text = trim($Line['text'], '# '); 509 | 510 | $Block = array( 511 | 'element' => array( 512 | 'name' => 'h' . min(6, $level), 513 | 'text' => $text, 514 | 'handler' => 'line', 515 | ), 516 | ); 517 | 518 | return $Block; 519 | } 520 | } 521 | 522 | # 523 | # List 524 | 525 | protected function blockList($Line) 526 | { 527 | list($name, $pattern) = $Line['text'][0] <= '-' ? array('ul', '[*+-]') : array('ol', '[0-9]+[.]'); 528 | 529 | if (preg_match('/^('.$pattern.'[ ]+)(.*)/', $Line['text'], $matches)) 530 | { 531 | $Block = array( 532 | 'indent' => $Line['indent'], 533 | 'pattern' => $pattern, 534 | 'element' => array( 535 | 'name' => $name, 536 | 'handler' => 'elements', 537 | ), 538 | ); 539 | 540 | if($name === 'ol') 541 | { 542 | $listStart = stristr($matches[0], '.', true); 543 | 544 | if($listStart !== '1') 545 | { 546 | $Block['element']['attributes'] = array('start' => $listStart); 547 | } 548 | } 549 | 550 | $Block['li'] = array( 551 | 'name' => 'li', 552 | 'handler' => 'li', 553 | 'text' => array( 554 | $matches[2], 555 | ), 556 | ); 557 | 558 | $Block['element']['text'] []= & $Block['li']; 559 | 560 | return $Block; 561 | } 562 | } 563 | 564 | protected function blockListContinue($Line, array $Block) 565 | { 566 | if ($Block['indent'] === $Line['indent'] and preg_match('/^'.$Block['pattern'].'(?:[ ]+(.*)|$)/', $Line['text'], $matches)) 567 | { 568 | if (isset($Block['interrupted'])) 569 | { 570 | $Block['li']['text'] []= ''; 571 | 572 | $Block['loose'] = true; 573 | 574 | unset($Block['interrupted']); 575 | } 576 | 577 | unset($Block['li']); 578 | 579 | $text = isset($matches[1]) ? $matches[1] : ''; 580 | 581 | $Block['li'] = array( 582 | 'name' => 'li', 583 | 'handler' => 'li', 584 | 'text' => array( 585 | $text, 586 | ), 587 | ); 588 | 589 | $Block['element']['text'] []= & $Block['li']; 590 | 591 | return $Block; 592 | } 593 | 594 | if ($Line['text'][0] === '[' and $this->blockReference($Line)) 595 | { 596 | return $Block; 597 | } 598 | 599 | if ( ! isset($Block['interrupted'])) 600 | { 601 | $text = preg_replace('/^[ ]{0,4}/', '', $Line['body']); 602 | 603 | $Block['li']['text'] []= $text; 604 | 605 | return $Block; 606 | } 607 | 608 | if ($Line['indent'] > 0) 609 | { 610 | $Block['li']['text'] []= ''; 611 | 612 | $text = preg_replace('/^[ ]{0,4}/', '', $Line['body']); 613 | 614 | $Block['li']['text'] []= $text; 615 | 616 | unset($Block['interrupted']); 617 | 618 | return $Block; 619 | } 620 | } 621 | 622 | protected function blockListComplete(array $Block) 623 | { 624 | if (isset($Block['loose'])) 625 | { 626 | foreach ($Block['element']['text'] as &$li) 627 | { 628 | if (end($li['text']) !== '') 629 | { 630 | $li['text'] []= ''; 631 | } 632 | } 633 | } 634 | 635 | return $Block; 636 | } 637 | 638 | # 639 | # Quote 640 | 641 | protected function blockQuote($Line) 642 | { 643 | if (preg_match('/^>[ ]?(.*)/', $Line['text'], $matches)) 644 | { 645 | $Block = array( 646 | 'element' => array( 647 | 'name' => 'blockquote', 648 | 'handler' => 'lines', 649 | 'text' => (array) $matches[1], 650 | ), 651 | ); 652 | 653 | return $Block; 654 | } 655 | } 656 | 657 | protected function blockQuoteContinue($Line, array $Block) 658 | { 659 | if ($Line['text'][0] === '>' and preg_match('/^>[ ]?(.*)/', $Line['text'], $matches)) 660 | { 661 | if (isset($Block['interrupted'])) 662 | { 663 | $Block['element']['text'] []= ''; 664 | 665 | unset($Block['interrupted']); 666 | } 667 | 668 | $Block['element']['text'] []= $matches[1]; 669 | 670 | return $Block; 671 | } 672 | 673 | if ( ! isset($Block['interrupted'])) 674 | { 675 | $Block['element']['text'] []= $Line['text']; 676 | 677 | return $Block; 678 | } 679 | } 680 | 681 | # 682 | # Rule 683 | 684 | protected function blockRule($Line) 685 | { 686 | if (preg_match('/^(['.$Line['text'][0].'])([ ]*\1){2,}[ ]*$/', $Line['text'])) 687 | { 688 | $Block = array( 689 | 'element' => array( 690 | 'name' => 'hr' 691 | ), 692 | ); 693 | 694 | return $Block; 695 | } 696 | } 697 | 698 | # 699 | # Setext 700 | 701 | protected function blockSetextHeader($Line, array $Block = null) 702 | { 703 | if ( ! isset($Block) or isset($Block['type']) or isset($Block['interrupted'])) 704 | { 705 | return; 706 | } 707 | 708 | if (chop($Line['text'], $Line['text'][0]) === '') 709 | { 710 | $Block['element']['name'] = $Line['text'][0] === '=' ? 'h1' : 'h2'; 711 | 712 | return $Block; 713 | } 714 | } 715 | 716 | # 717 | # Markup 718 | 719 | protected function blockMarkup($Line) 720 | { 721 | if ($this->markupEscaped or $this->safeMode) 722 | { 723 | return; 724 | } 725 | 726 | if (preg_match('/^<(\w[\w-]*)(?:[ ]*'.$this->regexHtmlAttribute.')*[ ]*(\/)?>/', $Line['text'], $matches)) 727 | { 728 | $element = strtolower($matches[1]); 729 | 730 | if (in_array($element, $this->textLevelElements)) 731 | { 732 | return; 733 | } 734 | 735 | $Block = array( 736 | 'name' => $matches[1], 737 | 'depth' => 0, 738 | 'markup' => $Line['text'], 739 | ); 740 | 741 | $length = strlen($matches[0]); 742 | 743 | $remainder = substr($Line['text'], $length); 744 | 745 | if (trim($remainder) === '') 746 | { 747 | if (isset($matches[2]) or in_array($matches[1], $this->voidElements)) 748 | { 749 | $Block['closed'] = true; 750 | 751 | $Block['void'] = true; 752 | } 753 | } 754 | else 755 | { 756 | if (isset($matches[2]) or in_array($matches[1], $this->voidElements)) 757 | { 758 | return; 759 | } 760 | 761 | if (preg_match('/<\/'.$matches[1].'>[ ]*$/i', $remainder)) 762 | { 763 | $Block['closed'] = true; 764 | } 765 | } 766 | 767 | return $Block; 768 | } 769 | } 770 | 771 | protected function blockMarkupContinue($Line, array $Block) 772 | { 773 | if (isset($Block['closed'])) 774 | { 775 | return; 776 | } 777 | 778 | if (preg_match('/^<'.$Block['name'].'(?:[ ]*'.$this->regexHtmlAttribute.')*[ ]*>/i', $Line['text'])) # open 779 | { 780 | $Block['depth'] ++; 781 | } 782 | 783 | if (preg_match('/(.*?)<\/'.$Block['name'].'>[ ]*$/i', $Line['text'], $matches)) # close 784 | { 785 | if ($Block['depth'] > 0) 786 | { 787 | $Block['depth'] --; 788 | } 789 | else 790 | { 791 | $Block['closed'] = true; 792 | } 793 | } 794 | 795 | if (isset($Block['interrupted'])) 796 | { 797 | $Block['markup'] .= "\n"; 798 | 799 | unset($Block['interrupted']); 800 | } 801 | 802 | $Block['markup'] .= "\n".$Line['body']; 803 | 804 | return $Block; 805 | } 806 | 807 | # 808 | # Reference 809 | 810 | protected function blockReference($Line) 811 | { 812 | if (preg_match('/^\[(.+?)\]:[ ]*?(?:[ ]+["\'(](.+)["\')])?[ ]*$/', $Line['text'], $matches)) 813 | { 814 | $id = strtolower($matches[1]); 815 | 816 | $Data = array( 817 | 'url' => $matches[2], 818 | 'title' => null, 819 | ); 820 | 821 | if (isset($matches[3])) 822 | { 823 | $Data['title'] = $matches[3]; 824 | } 825 | 826 | $this->DefinitionData['Reference'][$id] = $Data; 827 | 828 | $Block = array( 829 | 'hidden' => true, 830 | ); 831 | 832 | return $Block; 833 | } 834 | } 835 | 836 | # 837 | # Table 838 | 839 | protected function blockTable($Line, array $Block = null) 840 | { 841 | if ( ! isset($Block) or isset($Block['type']) or isset($Block['interrupted'])) 842 | { 843 | return; 844 | } 845 | 846 | if (strpos($Block['element']['text'], '|') !== false and chop($Line['text'], ' -:|') === '') 847 | { 848 | $alignments = array(); 849 | 850 | $divider = $Line['text']; 851 | 852 | $divider = trim($divider); 853 | $divider = trim($divider, '|'); 854 | 855 | $dividerCells = explode('|', $divider); 856 | 857 | foreach ($dividerCells as $dividerCell) 858 | { 859 | $dividerCell = trim($dividerCell); 860 | 861 | if ($dividerCell === '') 862 | { 863 | continue; 864 | } 865 | 866 | $alignment = null; 867 | 868 | if ($dividerCell[0] === ':') 869 | { 870 | $alignment = 'left'; 871 | } 872 | 873 | if (substr($dividerCell, - 1) === ':') 874 | { 875 | $alignment = $alignment === 'left' ? 'center' : 'right'; 876 | } 877 | 878 | $alignments []= $alignment; 879 | } 880 | 881 | # ~ 882 | 883 | $HeaderElements = array(); 884 | 885 | $header = $Block['element']['text']; 886 | 887 | $header = trim($header); 888 | $header = trim($header, '|'); 889 | 890 | $headerCells = explode('|', $header); 891 | 892 | foreach ($headerCells as $index => $headerCell) 893 | { 894 | $headerCell = trim($headerCell); 895 | 896 | $HeaderElement = array( 897 | 'name' => 'th', 898 | 'text' => $headerCell, 899 | 'handler' => 'line', 900 | ); 901 | 902 | if (isset($alignments[$index])) 903 | { 904 | $alignment = $alignments[$index]; 905 | 906 | $HeaderElement['attributes'] = array( 907 | 'style' => 'text-align: '.$alignment.';', 908 | ); 909 | } 910 | 911 | $HeaderElements []= $HeaderElement; 912 | } 913 | 914 | # ~ 915 | 916 | $Block = array( 917 | 'alignments' => $alignments, 918 | 'identified' => true, 919 | 'element' => array( 920 | 'name' => 'table', 921 | 'handler' => 'elements', 922 | ), 923 | ); 924 | 925 | $Block['element']['text'] []= array( 926 | 'name' => 'thead', 927 | 'handler' => 'elements', 928 | ); 929 | 930 | $Block['element']['text'] []= array( 931 | 'name' => 'tbody', 932 | 'handler' => 'elements', 933 | 'text' => array(), 934 | ); 935 | 936 | $Block['element']['text'][0]['text'] []= array( 937 | 'name' => 'tr', 938 | 'handler' => 'elements', 939 | 'text' => $HeaderElements, 940 | ); 941 | 942 | return $Block; 943 | } 944 | } 945 | 946 | protected function blockTableContinue($Line, array $Block) 947 | { 948 | if (isset($Block['interrupted'])) 949 | { 950 | return; 951 | } 952 | 953 | if ($Line['text'][0] === '|' or strpos($Line['text'], '|')) 954 | { 955 | $Elements = array(); 956 | 957 | $row = $Line['text']; 958 | 959 | $row = trim($row); 960 | $row = trim($row, '|'); 961 | 962 | preg_match_all('/(?:(\\\\[|])|[^|`]|`[^`]+`|`)+/', $row, $matches); 963 | 964 | foreach ($matches[0] as $index => $cell) 965 | { 966 | $cell = trim($cell); 967 | 968 | $Element = array( 969 | 'name' => 'td', 970 | 'handler' => 'line', 971 | 'text' => $cell, 972 | ); 973 | 974 | if (isset($Block['alignments'][$index])) 975 | { 976 | $Element['attributes'] = array( 977 | 'style' => 'text-align: '.$Block['alignments'][$index].';', 978 | ); 979 | } 980 | 981 | $Elements []= $Element; 982 | } 983 | 984 | $Element = array( 985 | 'name' => 'tr', 986 | 'handler' => 'elements', 987 | 'text' => $Elements, 988 | ); 989 | 990 | $Block['element']['text'][1]['text'] []= $Element; 991 | 992 | return $Block; 993 | } 994 | } 995 | 996 | # 997 | # ~ 998 | # 999 | 1000 | protected function paragraph($Line) 1001 | { 1002 | $Block = array( 1003 | 'element' => array( 1004 | 'name' => 'p', 1005 | 'text' => $Line['text'], 1006 | 'handler' => 'line', 1007 | ), 1008 | ); 1009 | 1010 | return $Block; 1011 | } 1012 | 1013 | # 1014 | # Inline Elements 1015 | # 1016 | 1017 | protected $InlineTypes = array( 1018 | '"' => array('SpecialCharacter'), 1019 | '!' => array('Image'), 1020 | '&' => array('SpecialCharacter'), 1021 | '*' => array('Emphasis'), 1022 | ':' => array('Url'), 1023 | '<' => array('UrlTag', 'EmailTag', 'Markup', 'SpecialCharacter'), 1024 | '>' => array('SpecialCharacter'), 1025 | '[' => array('Link'), 1026 | '_' => array('Emphasis'), 1027 | '`' => array('Code'), 1028 | '~' => array('Strikethrough'), 1029 | '\\' => array('EscapeSequence'), 1030 | ); 1031 | 1032 | # ~ 1033 | 1034 | protected $inlineMarkerList = '!"*_&[:<>`~\\'; 1035 | 1036 | # 1037 | # ~ 1038 | # 1039 | 1040 | public function line($text, $nonNestables=array()) 1041 | { 1042 | $markup = ''; 1043 | 1044 | # $excerpt is based on the first occurrence of a marker 1045 | 1046 | while ($excerpt = strpbrk($text, $this->inlineMarkerList)) 1047 | { 1048 | $marker = $excerpt[0]; 1049 | 1050 | $markerPosition = strpos($text, $marker); 1051 | 1052 | $Excerpt = array('text' => $excerpt, 'context' => $text); 1053 | 1054 | foreach ($this->InlineTypes[$marker] as $inlineType) 1055 | { 1056 | # check to see if the current inline type is nestable in the current context 1057 | 1058 | if ( ! empty($nonNestables) and in_array($inlineType, $nonNestables)) 1059 | { 1060 | continue; 1061 | } 1062 | 1063 | $Inline = $this->{'inline'.$inlineType}($Excerpt); 1064 | 1065 | if ( ! isset($Inline)) 1066 | { 1067 | continue; 1068 | } 1069 | 1070 | # makes sure that the inline belongs to "our" marker 1071 | 1072 | if (isset($Inline['position']) and $Inline['position'] > $markerPosition) 1073 | { 1074 | continue; 1075 | } 1076 | 1077 | # sets a default inline position 1078 | 1079 | if ( ! isset($Inline['position'])) 1080 | { 1081 | $Inline['position'] = $markerPosition; 1082 | } 1083 | 1084 | # cause the new element to 'inherit' our non nestables 1085 | 1086 | foreach ($nonNestables as $non_nestable) 1087 | { 1088 | $Inline['element']['nonNestables'][] = $non_nestable; 1089 | } 1090 | 1091 | # the text that comes before the inline 1092 | $unmarkedText = substr($text, 0, $Inline['position']); 1093 | 1094 | # compile the unmarked text 1095 | $markup .= $this->unmarkedText($unmarkedText); 1096 | 1097 | # compile the inline 1098 | $markup .= isset($Inline['markup']) ? $Inline['markup'] : $this->element($Inline['element']); 1099 | 1100 | # remove the examined text 1101 | $text = substr($text, $Inline['position'] + $Inline['extent']); 1102 | 1103 | continue 2; 1104 | } 1105 | 1106 | # the marker does not belong to an inline 1107 | 1108 | $unmarkedText = substr($text, 0, $markerPosition + 1); 1109 | 1110 | $markup .= $this->unmarkedText($unmarkedText); 1111 | 1112 | $text = substr($text, $markerPosition + 1); 1113 | } 1114 | 1115 | $markup .= $this->unmarkedText($text); 1116 | 1117 | return $markup; 1118 | } 1119 | 1120 | # 1121 | # ~ 1122 | # 1123 | 1124 | protected function inlineCode($Excerpt) 1125 | { 1126 | $marker = $Excerpt['text'][0]; 1127 | 1128 | if (preg_match('/^('.$marker.'+)[ ]*(.+?)[ ]*(? strlen($matches[0]), 1135 | 'element' => array( 1136 | 'name' => 'code', 1137 | 'text' => $text, 1138 | ), 1139 | ); 1140 | } 1141 | } 1142 | 1143 | protected function inlineEmailTag($Excerpt) 1144 | { 1145 | if (strpos($Excerpt['text'], '>') !== false and preg_match('/^<((mailto:)?\S+?@\S+?)>/i', $Excerpt['text'], $matches)) 1146 | { 1147 | $url = $matches[1]; 1148 | 1149 | if ( ! isset($matches[2])) 1150 | { 1151 | $url = 'mailto:' . $url; 1152 | } 1153 | 1154 | return array( 1155 | 'extent' => strlen($matches[0]), 1156 | 'element' => array( 1157 | 'name' => 'a', 1158 | 'text' => $matches[1], 1159 | 'attributes' => array( 1160 | 'href' => $url, 1161 | ), 1162 | ), 1163 | ); 1164 | } 1165 | } 1166 | 1167 | protected function inlineEmphasis($Excerpt) 1168 | { 1169 | if ( ! isset($Excerpt['text'][1])) 1170 | { 1171 | return; 1172 | } 1173 | 1174 | $marker = $Excerpt['text'][0]; 1175 | 1176 | if ($Excerpt['text'][1] === $marker and preg_match($this->StrongRegex[$marker], $Excerpt['text'], $matches)) 1177 | { 1178 | $emphasis = 'strong'; 1179 | } 1180 | elseif (preg_match($this->EmRegex[$marker], $Excerpt['text'], $matches)) 1181 | { 1182 | $emphasis = 'em'; 1183 | } 1184 | else 1185 | { 1186 | return; 1187 | } 1188 | 1189 | return array( 1190 | 'extent' => strlen($matches[0]), 1191 | 'element' => array( 1192 | 'name' => $emphasis, 1193 | 'handler' => 'line', 1194 | 'text' => $matches[1], 1195 | ), 1196 | ); 1197 | } 1198 | 1199 | protected function inlineEscapeSequence($Excerpt) 1200 | { 1201 | if (isset($Excerpt['text'][1]) and in_array($Excerpt['text'][1], $this->specialCharacters)) 1202 | { 1203 | return array( 1204 | 'markup' => $Excerpt['text'][1], 1205 | 'extent' => 2, 1206 | ); 1207 | } 1208 | } 1209 | 1210 | protected function inlineImage($Excerpt) 1211 | { 1212 | if ( ! isset($Excerpt['text'][1]) or $Excerpt['text'][1] !== '[') 1213 | { 1214 | return; 1215 | } 1216 | 1217 | $Excerpt['text']= substr($Excerpt['text'], 1); 1218 | 1219 | $Link = $this->inlineLink($Excerpt); 1220 | 1221 | if ($Link === null) 1222 | { 1223 | return; 1224 | } 1225 | 1226 | $Inline = array( 1227 | 'extent' => $Link['extent'] + 1, 1228 | 'element' => array( 1229 | 'name' => 'img', 1230 | 'attributes' => array( 1231 | 'src' => $Link['element']['attributes']['href'], 1232 | 'alt' => $Link['element']['text'], 1233 | ), 1234 | ), 1235 | ); 1236 | 1237 | $Inline['element']['attributes'] += $Link['element']['attributes']; 1238 | 1239 | unset($Inline['element']['attributes']['href']); 1240 | 1241 | return $Inline; 1242 | } 1243 | 1244 | protected function inlineLink($Excerpt) 1245 | { 1246 | $Element = array( 1247 | 'name' => 'a', 1248 | 'handler' => 'line', 1249 | 'nonNestables' => array('Url', 'Link'), 1250 | 'text' => null, 1251 | 'attributes' => array( 1252 | 'href' => null, 1253 | 'title' => null, 1254 | ), 1255 | ); 1256 | 1257 | $extent = 0; 1258 | 1259 | $remainder = $Excerpt['text']; 1260 | 1261 | if (preg_match('/\[((?:[^][]++|(?R))*+)\]/', $remainder, $matches)) 1262 | { 1263 | $Element['text'] = $matches[1]; 1264 | 1265 | $extent += strlen($matches[0]); 1266 | 1267 | $remainder = substr($remainder, $extent); 1268 | } 1269 | else 1270 | { 1271 | return; 1272 | } 1273 | 1274 | if (preg_match('/^[(]\s*+((?:[^ ()]++|[(][^ )]+[)])++)(?:[ ]+("[^"]*"|\'[^\']*\'))?\s*[)]/', $remainder, $matches)) 1275 | { 1276 | $Element['attributes']['href'] = $matches[1]; 1277 | 1278 | if (isset($matches[2])) 1279 | { 1280 | $Element['attributes']['title'] = substr($matches[2], 1, - 1); 1281 | } 1282 | 1283 | $extent += strlen($matches[0]); 1284 | } 1285 | else 1286 | { 1287 | if (preg_match('/^\s*\[(.*?)\]/', $remainder, $matches)) 1288 | { 1289 | $definition = strlen($matches[1]) ? $matches[1] : $Element['text']; 1290 | $definition = strtolower($definition); 1291 | 1292 | $extent += strlen($matches[0]); 1293 | } 1294 | else 1295 | { 1296 | $definition = strtolower($Element['text']); 1297 | } 1298 | 1299 | if ( ! isset($this->DefinitionData['Reference'][$definition])) 1300 | { 1301 | return; 1302 | } 1303 | 1304 | $Definition = $this->DefinitionData['Reference'][$definition]; 1305 | 1306 | $Element['attributes']['href'] = $Definition['url']; 1307 | $Element['attributes']['title'] = $Definition['title']; 1308 | } 1309 | 1310 | return array( 1311 | 'extent' => $extent, 1312 | 'element' => $Element, 1313 | ); 1314 | } 1315 | 1316 | protected function inlineMarkup($Excerpt) 1317 | { 1318 | if ($this->markupEscaped or $this->safeMode or strpos($Excerpt['text'], '>') === false) 1319 | { 1320 | return; 1321 | } 1322 | 1323 | if ($Excerpt['text'][1] === '/' and preg_match('/^<\/\w[\w-]*[ ]*>/s', $Excerpt['text'], $matches)) 1324 | { 1325 | return array( 1326 | 'markup' => $matches[0], 1327 | 'extent' => strlen($matches[0]), 1328 | ); 1329 | } 1330 | 1331 | if ($Excerpt['text'][1] === '!' and preg_match('/^/s', $Excerpt['text'], $matches)) 1332 | { 1333 | return array( 1334 | 'markup' => $matches[0], 1335 | 'extent' => strlen($matches[0]), 1336 | ); 1337 | } 1338 | 1339 | if ($Excerpt['text'][1] !== ' ' and preg_match('/^<\w[\w-]*(?:[ ]*'.$this->regexHtmlAttribute.')*[ ]*\/?>/s', $Excerpt['text'], $matches)) 1340 | { 1341 | return array( 1342 | 'markup' => $matches[0], 1343 | 'extent' => strlen($matches[0]), 1344 | ); 1345 | } 1346 | } 1347 | 1348 | protected function inlineSpecialCharacter($Excerpt) 1349 | { 1350 | if ($Excerpt['text'][0] === '&' and ! preg_match('/^&#?\w+;/', $Excerpt['text'])) 1351 | { 1352 | return array( 1353 | 'markup' => '&', 1354 | 'extent' => 1, 1355 | ); 1356 | } 1357 | 1358 | $SpecialCharacter = array('>' => 'gt', '<' => 'lt', '"' => 'quot'); 1359 | 1360 | if (isset($SpecialCharacter[$Excerpt['text'][0]])) 1361 | { 1362 | return array( 1363 | 'markup' => '&'.$SpecialCharacter[$Excerpt['text'][0]].';', 1364 | 'extent' => 1, 1365 | ); 1366 | } 1367 | } 1368 | 1369 | protected function inlineStrikethrough($Excerpt) 1370 | { 1371 | if ( ! isset($Excerpt['text'][1])) 1372 | { 1373 | return; 1374 | } 1375 | 1376 | if ($Excerpt['text'][1] === '~' and preg_match('/^~~(?=\S)(.+?)(?<=\S)~~/', $Excerpt['text'], $matches)) 1377 | { 1378 | return array( 1379 | 'extent' => strlen($matches[0]), 1380 | 'element' => array( 1381 | 'name' => 'del', 1382 | 'text' => $matches[1], 1383 | 'handler' => 'line', 1384 | ), 1385 | ); 1386 | } 1387 | } 1388 | 1389 | protected function inlineUrl($Excerpt) 1390 | { 1391 | if ($this->urlsLinked !== true or ! isset($Excerpt['text'][2]) or $Excerpt['text'][2] !== '/') 1392 | { 1393 | return; 1394 | } 1395 | 1396 | if (preg_match('/\bhttps?:[\/]{2}[^\s<]+\b\/*/ui', $Excerpt['context'], $matches, PREG_OFFSET_CAPTURE)) 1397 | { 1398 | $url = $matches[0][0]; 1399 | 1400 | $Inline = array( 1401 | 'extent' => strlen($matches[0][0]), 1402 | 'position' => $matches[0][1], 1403 | 'element' => array( 1404 | 'name' => 'a', 1405 | 'text' => $url, 1406 | 'attributes' => array( 1407 | 'href' => $url, 1408 | ), 1409 | ), 1410 | ); 1411 | 1412 | return $Inline; 1413 | } 1414 | } 1415 | 1416 | protected function inlineUrlTag($Excerpt) 1417 | { 1418 | if (strpos($Excerpt['text'], '>') !== false and preg_match('/^<(\w+:\/{2}[^ >]+)>/i', $Excerpt['text'], $matches)) 1419 | { 1420 | $url = $matches[1]; 1421 | 1422 | return array( 1423 | 'extent' => strlen($matches[0]), 1424 | 'element' => array( 1425 | 'name' => 'a', 1426 | 'text' => $url, 1427 | 'attributes' => array( 1428 | 'href' => $url, 1429 | ), 1430 | ), 1431 | ); 1432 | } 1433 | } 1434 | 1435 | # ~ 1436 | 1437 | protected function unmarkedText($text) 1438 | { 1439 | if ($this->breaksEnabled) 1440 | { 1441 | $text = preg_replace('/[ ]*\n/', "
\n", $text); 1442 | } 1443 | else 1444 | { 1445 | $text = preg_replace('/(?:[ ][ ]+|[ ]*\\\\)\n/', "
\n", $text); 1446 | $text = str_replace(" \n", "\n", $text); 1447 | } 1448 | 1449 | return $text; 1450 | } 1451 | 1452 | # 1453 | # Handlers 1454 | # 1455 | 1456 | protected function element(array $Element) 1457 | { 1458 | if ($this->safeMode) 1459 | { 1460 | $Element = $this->sanitiseElement($Element); 1461 | } 1462 | 1463 | $markup = '<'.$Element['name']; 1464 | 1465 | if (isset($Element['attributes'])) 1466 | { 1467 | foreach ($Element['attributes'] as $name => $value) 1468 | { 1469 | if ($value === null) 1470 | { 1471 | continue; 1472 | } 1473 | 1474 | $markup .= ' '.$name.'="'.self::escape($value).'"'; 1475 | } 1476 | } 1477 | 1478 | if (isset($Element['text'])) 1479 | { 1480 | $markup .= '>'; 1481 | 1482 | if (!isset($Element['nonNestables'])) 1483 | { 1484 | $Element['nonNestables'] = array(); 1485 | } 1486 | 1487 | if (isset($Element['handler'])) 1488 | { 1489 | $markup .= $this->{$Element['handler']}($Element['text'], $Element['nonNestables']); 1490 | } 1491 | else 1492 | { 1493 | $markup .= self::escape($Element['text'], true); 1494 | } 1495 | 1496 | $markup .= ''; 1497 | } 1498 | else 1499 | { 1500 | $markup .= ' />'; 1501 | } 1502 | 1503 | return $markup; 1504 | } 1505 | 1506 | protected function elements(array $Elements) 1507 | { 1508 | $markup = ''; 1509 | 1510 | foreach ($Elements as $Element) 1511 | { 1512 | $markup .= "\n" . $this->element($Element); 1513 | } 1514 | 1515 | $markup .= "\n"; 1516 | 1517 | return $markup; 1518 | } 1519 | 1520 | # ~ 1521 | 1522 | protected function li($lines) 1523 | { 1524 | $markup = $this->lines($lines); 1525 | 1526 | $trimmedMarkup = trim($markup); 1527 | 1528 | if ( ! in_array('', $lines) and substr($trimmedMarkup, 0, 3) === '

') 1529 | { 1530 | $markup = $trimmedMarkup; 1531 | $markup = substr($markup, 3); 1532 | 1533 | $position = strpos($markup, "

"); 1534 | 1535 | $markup = substr_replace($markup, '', $position, 4); 1536 | } 1537 | 1538 | return $markup; 1539 | } 1540 | 1541 | # 1542 | # Deprecated Methods 1543 | # 1544 | 1545 | function parse($text) 1546 | { 1547 | $markup = $this->text($text); 1548 | 1549 | return $markup; 1550 | } 1551 | 1552 | protected function sanitiseElement(array $Element) 1553 | { 1554 | static $goodAttribute = '/^[a-zA-Z0-9][a-zA-Z0-9-_]*+$/'; 1555 | static $safeUrlNameToAtt = array( 1556 | 'a' => 'href', 1557 | 'img' => 'src', 1558 | ); 1559 | 1560 | if (isset($safeUrlNameToAtt[$Element['name']])) 1561 | { 1562 | $Element = $this->filterUnsafeUrlInAttribute($Element, $safeUrlNameToAtt[$Element['name']]); 1563 | } 1564 | 1565 | if ( ! empty($Element['attributes'])) 1566 | { 1567 | foreach ($Element['attributes'] as $att => $val) 1568 | { 1569 | # filter out badly parsed attribute 1570 | if ( ! preg_match($goodAttribute, $att)) 1571 | { 1572 | unset($Element['attributes'][$att]); 1573 | } 1574 | # dump onevent attribute 1575 | elseif (self::striAtStart($att, 'on')) 1576 | { 1577 | unset($Element['attributes'][$att]); 1578 | } 1579 | } 1580 | } 1581 | 1582 | return $Element; 1583 | } 1584 | 1585 | protected function filterUnsafeUrlInAttribute(array $Element, $attribute) 1586 | { 1587 | foreach ($this->safeLinksWhitelist as $scheme) 1588 | { 1589 | if (self::striAtStart($Element['attributes'][$attribute], $scheme)) 1590 | { 1591 | return $Element; 1592 | } 1593 | } 1594 | 1595 | $Element['attributes'][$attribute] = str_replace(':', '%3A', $Element['attributes'][$attribute]); 1596 | 1597 | return $Element; 1598 | } 1599 | 1600 | # 1601 | # Static Methods 1602 | # 1603 | 1604 | protected static function escape($text, $allowQuotes = false) 1605 | { 1606 | return htmlspecialchars($text, $allowQuotes ? ENT_NOQUOTES : ENT_QUOTES, 'UTF-8'); 1607 | } 1608 | 1609 | protected static function striAtStart($string, $needle) 1610 | { 1611 | $len = strlen($needle); 1612 | 1613 | if ($len > strlen($string)) 1614 | { 1615 | return false; 1616 | } 1617 | else 1618 | { 1619 | return strtolower(substr($string, 0, $len)) === strtolower($needle); 1620 | } 1621 | } 1622 | 1623 | static function instance($name = 'default') 1624 | { 1625 | if (isset(self::$instances[$name])) 1626 | { 1627 | return self::$instances[$name]; 1628 | } 1629 | 1630 | $instance = new static(); 1631 | 1632 | self::$instances[$name] = $instance; 1633 | 1634 | return $instance; 1635 | } 1636 | 1637 | private static $instances = array(); 1638 | 1639 | # 1640 | # Fields 1641 | # 1642 | 1643 | protected $DefinitionData; 1644 | 1645 | # 1646 | # Read-Only 1647 | 1648 | protected $specialCharacters = array( 1649 | '\\', '`', '*', '_', '{', '}', '[', ']', '(', ')', '>', '#', '+', '-', '.', '!', '|', 1650 | ); 1651 | 1652 | protected $StrongRegex = array( 1653 | '*' => '/^[*]{2}((?:\\\\\*|[^*]|[*][^*]*[*])+?)[*]{2}(?![*])/s', 1654 | '_' => '/^__((?:\\\\_|[^_]|_[^_]*_)+?)__(?!_)/us', 1655 | ); 1656 | 1657 | protected $EmRegex = array( 1658 | '*' => '/^[*]((?:\\\\\*|[^*]|[*][*][^*]+?[*][*])+?)[*](?![*])/s', 1659 | '_' => '/^_((?:\\\\_|[^_]|__[^_]*__)+?)_(?!_)\b/us', 1660 | ); 1661 | 1662 | protected $regexHtmlAttribute = '[a-zA-Z_:][\w:.-]*(?:\s*=\s*(?:[^"\'=<>`\s]+|"[^"]*"|\'[^\']*\'))?'; 1663 | 1664 | protected $voidElements = array( 1665 | 'area', 'base', 'br', 'col', 'command', 'embed', 'hr', 'img', 'input', 'link', 'meta', 'param', 'source', 1666 | ); 1667 | 1668 | protected $textLevelElements = array( 1669 | 'a', 'br', 'bdo', 'abbr', 'blink', 'nextid', 'acronym', 'basefont', 1670 | 'b', 'em', 'big', 'cite', 'small', 'spacer', 'listing', 1671 | 'i', 'rp', 'del', 'code', 'strike', 'marquee', 1672 | 'q', 'rt', 'ins', 'font', 'strong', 1673 | 's', 'tt', 'kbd', 'mark', 1674 | 'u', 'xm', 'sub', 'nobr', 1675 | 'sup', 'ruby', 1676 | 'var', 'span', 1677 | 'wbr', 'time', 1678 | ); 1679 | } 1680 | -------------------------------------------------------------------------------- /lib/cache.php: -------------------------------------------------------------------------------- 1 | get($key); 29 | if(!is_null($value)){ 30 | return $value; 31 | }elseif(is_callable($default)){ 32 | $value = $default(); 33 | self::set($key, $value, $expire); 34 | return $value; 35 | }elseif(!is_null($default)){ 36 | self::set($key, $default, $expire); 37 | return $default; 38 | } 39 | } 40 | 41 | // 设置缓存 42 | static function set($key, $value, $expire=99999999){ 43 | return self::c()->set($key, $value, $expire); 44 | } 45 | 46 | // 清空缓存 47 | static function clear(){ 48 | return self::c()->clear(); 49 | } 50 | 51 | // 删除缓存 52 | static function del($key){ 53 | return self::set($key, null); 54 | } 55 | 56 | // 判断缓存是否设置 57 | static function has($key){ 58 | if(is_null(self::get($key))){ 59 | return false; 60 | }else{ 61 | return true; 62 | } 63 | } 64 | // 读取并删除缓存 65 | static function pull($key){ 66 | $value = self::get($key); 67 | self::del($key); 68 | return $value; 69 | } 70 | } 71 | -------------------------------------------------------------------------------- /lib/cache/filecache_.php: -------------------------------------------------------------------------------- 1 | cache_path = $cache_path; 10 | } 11 | 12 | function get($key){ 13 | $file = $this->cache_path . md5($key) . '.php'; 14 | $data = @include $file; 15 | if( is_array($data) && $data['expire'] > time() && !is_null($data['data']) ){ 16 | return $data['data']; 17 | }else{ 18 | return null; 19 | } 20 | } 21 | 22 | function set($key, $value=null, $expire=99999999){ 23 | $file = $this->cache_path . md5($key) . '.php'; 24 | $data['expire'] = time() + $expire; 25 | $data['data'] = $value; 26 | return @file_put_contents($file, "cache_path.'*.php')); 31 | } 32 | } -------------------------------------------------------------------------------- /lib/cache/memcache_.php: -------------------------------------------------------------------------------- 1 | m = new Memcache(); 7 | if(empty($config)){ 8 | $config = 'localhost:11211'; 9 | } 10 | list($host, $port) = explode(':', $config, 2); 11 | $this->m->addServer($host, $port); 12 | } 13 | 14 | function get($key){ 15 | $data = $this->m->get($key); 16 | if( is_array($data) && $data['expire'] > time() && !is_null($data['data']) ){ 17 | return $data['data']; 18 | }else{ 19 | return null; 20 | } 21 | } 22 | 23 | function set($key, $value=null, $expire=99999999){ 24 | $data['expire'] = time() + $expire; 25 | $data['data'] = $value; 26 | return $this->m->set($key, $data); 27 | } 28 | 29 | function clear(){ 30 | $this->m->flush(10); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /lib/cache/redis_.php: -------------------------------------------------------------------------------- 1 | redis = new Redis(); 7 | if(empty($config)){ 8 | $config = 'localhost:6379'; 9 | } 10 | list($host, $port) = explode(':', $config, 2); 11 | $this->redis->pconnect($host, $port); 12 | } 13 | 14 | function get($key){ 15 | $gRefreshTime = $this->redis->get("OneIndex_gRefreshTime"); 16 | $key = "OneIndex_$gRefreshTime\_" . $key; 17 | $data = $this->redis->get($key); 18 | return unserialize($data) ?: null; 19 | } 20 | 21 | function set($key, $value=null, $expire=600){ 22 | $gRefreshTime = $this->redis->get("OneIndex_gRefreshTime"); 23 | if (empty($gRefreshTime)) { 24 | $gRefreshTime = time(); 25 | $this->redis->set("OneIndex_gRefreshTime", $gRefreshTime); 26 | } 27 | $key = "OneIndex_$gRefreshTime\_" . $key; 28 | return $this->redis->set($key, serialize($value), (time() + $expire)); 29 | } 30 | 31 | function clear(){ 32 | $this->redis->set("OneIndex_gRefreshTime", $gRefreshTime); 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /lib/cache/secache_.php: -------------------------------------------------------------------------------- 1 | cachefile = $cachefile; 20 | $this->c = new secache(); 21 | $this->c->workat($this->cachefile); 22 | } 23 | 24 | function get($key){ 25 | $this->c->fetch(md5($key),$data); 26 | if( is_array($data) && $data['expire'] > time() && !is_null($data['data']) ){ 27 | return $data['data']; 28 | }else{ 29 | return null; 30 | } 31 | } 32 | 33 | function set($key, $value=null, $expire=99999999){ 34 | $data['expire'] = time() + $expire; 35 | $data['data'] = $value; 36 | return $this->c->store(md5($key),$data); 37 | 38 | } 39 | 40 | function clear(){ 41 | return $this->c->clear(); 42 | } 43 | } 44 | 45 | class secache{ 46 | var $idx_node_size = 40; 47 | var $idx_node_base = 0; 48 | var $data_base_pos = 262588; //40+20+24*16+16*16*16*16*4; 49 | var $schema_item_size = 24; 50 | var $header_padding = 20; //保留空间 放置php标记防止下载 51 | var $info_size = 20; //保留空间 4+16 maxsize|ver 52 | //40起 添加20字节保留区域 53 | var $idx_seq_pos = 40; //id 计数器节点地址 54 | var $dfile_cur_pos = 44; //id 计数器节点地址 55 | var $idx_free_pos = 48; //id 空闲链表入口地址 56 | var $idx_base_pos = 444; //40+20+24*16 57 | var $min_size = 10240; //10M最小值 58 | var $schema_struct = array('size','free','lru_head','lru_tail','hits','miss'); 59 | var $ver = '$Rev$'; 60 | var $name = '系统默认缓存(文件型)'; 61 | function workat($file){ 62 | $this->_file = $file.'.php'; 63 | $this->_bsize_list = array( 64 | 512=>10, 65 | 3<<10=>10, 66 | 8<<10=>10, 67 | 20<<10=>4, 68 | 30<<10=>2, 69 | 50<<10=>2, 70 | 80<<10=>2, 71 | 96<<10=>2, 72 | 128<<10=>2, 73 | 224<<10=>2, 74 | 256<<10=>2, 75 | 512<<10=>1, 76 | 1024<<10=>1, 77 | ); 78 | $this->_node_struct = array( 79 | 'next'=>array(0,'V'), 80 | 'prev'=>array(4,'V'), 81 | 'data'=>array(8,'V'), 82 | 'size'=>array(12,'V'), 83 | 'lru_right'=>array(16,'V'), 84 | 'lru_left'=>array(20,'V'), 85 | 'key'=>array(24,'H*'), 86 | ); 87 | if(!file_exists($this->_file)){ 88 | $this->create(); 89 | }else{ 90 | $this->_rs = fopen($this->_file,'rb+') or $this->trigger_error('Can\'t open the cachefile: '.realpath($this->_file),E_USER_ERROR); 91 | $this->_seek($this->header_padding); 92 | $info = unpack('V1max_size/a*ver',fread($this->_rs,$this->info_size)); 93 | $info['ver'] = trim($info['ver']); 94 | if($info['ver']!=$this->ver){ 95 | $this->_format(true); 96 | }else{ 97 | $this->max_size = $info['max_size']; 98 | } 99 | } 100 | $this->idx_node_base = $this->data_base_pos+$this->max_size; 101 | $this->_block_size_list = array_keys($this->_bsize_list); 102 | sort($this->_block_size_list); 103 | return true; 104 | } 105 | function create(){ 106 | $this->_rs = fopen($this->_file,'wb+') or $this->trigger_error('Can\'t open the cachefile: '.realpath($this->_file),E_USER_ERROR);; 107 | fseek($this->_rs,0); 108 | fputs($this->_rs,'<'.'?php exit()?'.'>'); 109 | return $this->_format(); 110 | } 111 | function _puts($offset,$data){ 112 | if($offset < $this->max_size*1.5){ 113 | $this->_seek($offset); 114 | return fputs($this->_rs,$data); 115 | }else{ 116 | $this->trigger_error('Offset over quota:'.$offset,E_USER_ERROR); 117 | } 118 | } 119 | function _seek($offset){ 120 | return fseek($this->_rs,$offset); 121 | } 122 | function clear(){ 123 | return $this->_format(true); 124 | } 125 | function fetch($key,&$return){ 126 | if($this->lock(false)){ 127 | $locked = true; 128 | } 129 | if($this->search($key,$offset)){ 130 | $info = $this->_get_node($offset); 131 | $schema_id = $this->_get_size_schema_id($info['size']); 132 | if($schema_id===false){ 133 | if($locked) $this->unlock(); 134 | return false; 135 | } 136 | $this->_seek($info['data']); 137 | $data = fread($this->_rs,$info['size']); 138 | $return = unserialize($data); 139 | if($return===false){ 140 | if($locked) $this->unlock(); 141 | return false; 142 | } 143 | if($locked){ 144 | $this->_lru_push($schema_id,$info['offset']); 145 | $this->_set_schema($schema_id,'hits',$this->_get_schema($schema_id,'hits')+1); 146 | return $this->unlock(); 147 | }else{ 148 | return true; 149 | } 150 | }else{ 151 | if($locked) $this->unlock(); 152 | return false; 153 | } 154 | } 155 | /** 156 | * lock 157 | * 如果flock不管用,请继承本类,并重载此方法 158 | * 159 | * @param mixed $is_block 是否阻塞 160 | * @access public 161 | * @return void 162 | */ 163 | function lock($is_block,$whatever=false){ 164 | ignore_user_abort(1); 165 | return flock($this->_rs, $is_block?LOCK_EX:LOCK_EX+LOCK_NB); 166 | } 167 | /** 168 | * unlock 169 | * 如果flock不管用,请继承本类,并重载此方法 170 | * 171 | * @access public 172 | * @return void 173 | */ 174 | function unlock(){ 175 | ignore_user_abort(0); 176 | return flock($this->_rs, LOCK_UN); 177 | } 178 | function delete($key,$pos=false){ 179 | if($pos || $this->search($key,$pos)){ 180 | if($info = $this->_get_node($pos)){ 181 | //删除data区域 182 | if($info['prev']){ 183 | $this->_set_node($info['prev'],'next',$info['next']); 184 | $this->_set_node($info['next'],'prev',$info['prev']); 185 | }else{ //改入口位置 186 | $this->_set_node($info['next'],'prev',0); 187 | $this->_set_node_root($key,$info['next']); 188 | } 189 | $this->_free_dspace($info['size'],$info['data']); 190 | $this->_lru_delete($info); 191 | $this->_free_node($pos); 192 | return $info['prev']; 193 | } 194 | } 195 | return false; 196 | } 197 | function store($key,$value){ 198 | if($this->lock(true)){ 199 | //save data 200 | $data = serialize($value); 201 | $size = strlen($data); 202 | //get list_idx 203 | $has_key = $this->search($key,$list_idx_offset); 204 | $schema_id = $this->_get_size_schema_id($size); 205 | if($schema_id===false){ 206 | $this->unlock(); 207 | return false; 208 | } 209 | if($has_key){ 210 | $hdseq = $list_idx_offset; 211 | $info = $this->_get_node($hdseq); 212 | if($schema_id == $this->_get_size_schema_id($info['size'])){ 213 | $dataoffset = $info['data']; 214 | }else{ 215 | //破掉原有lru 216 | $this->_lru_delete($info); 217 | if(!($dataoffset = $this->_dalloc($schema_id))){ 218 | $this->unlock(); 219 | return false; 220 | } 221 | $this->_free_dspace($info['size'],$info['data']); 222 | $this->_set_node($hdseq,'lru_left',0); 223 | $this->_set_node($hdseq,'lru_right',0); 224 | } 225 | $this->_set_node($hdseq,'size',$size); 226 | $this->_set_node($hdseq,'data',$dataoffset); 227 | }else{ 228 | if(!($dataoffset = $this->_dalloc($schema_id))){ 229 | $this->unlock(); 230 | return false; 231 | } 232 | $hdseq = $this->_alloc_idx(array( 233 | 'next'=>0, 234 | 'prev'=>$list_idx_offset, 235 | 'data'=>$dataoffset, 236 | 'size'=>$size, 237 | 'lru_right'=>0, 238 | 'lru_left'=>0, 239 | 'key'=>$key, 240 | )); 241 | if($list_idx_offset>0){ 242 | $this->_set_node($list_idx_offset,'next',$hdseq); 243 | }else{ 244 | $this->_set_node_root($key,$hdseq); 245 | } 246 | } 247 | if($dataoffset>$this->max_size){ 248 | $this->trigger_error('alloc datasize:'.$dataoffset,E_USER_WARNING); 249 | return false; 250 | } 251 | $this->_puts($dataoffset,$data); 252 | $this->_set_schema($schema_id,'miss',$this->_get_schema($schema_id,'miss')+1); 253 | $this->_lru_push($schema_id,$hdseq); 254 | $this->unlock(); 255 | return true; 256 | }else{ 257 | $this->trigger_error("Couldn't lock the file !",E_USER_WARNING); 258 | return false; 259 | } 260 | } 261 | /** 262 | * search 263 | * 查找指定的key 264 | * 如果找到节点则$pos=节点本身 返回true 265 | * 否则 $pos=树的末端 返回false 266 | * 267 | * @param mixed $key 268 | * @access public 269 | * @return void 270 | */ 271 | function search($key,&$pos){ 272 | return $this->_get_pos_by_key($this->_get_node_root($key),$key,$pos); 273 | } 274 | function _get_size_schema_id($size){ 275 | foreach($this->_block_size_list as $k=>$block_size){ 276 | if($size <= $block_size){ 277 | return $k; 278 | } 279 | } 280 | return false; 281 | } 282 | function _parse_str_size($str_size,$default){ 283 | if(preg_match('/^([0-9]+)\s*([gmk]|)$/i',$str_size,$match)){ 284 | switch(strtolower($match[2])){ 285 | case 'g': 286 | if($match[1]>1){ 287 | $this->trigger_error('Max cache size 1G',E_USER_ERROR); 288 | } 289 | $size = $match[1]<<30; 290 | break; 291 | case 'm': 292 | $size = $match[1]<<20; 293 | break; 294 | case 'k': 295 | $size = $match[1]<<10; 296 | break; 297 | default: 298 | $size = $match[1]; 299 | } 300 | if($size<=0){ 301 | $this->trigger_error('Error cache size '.$this->max_size,E_USER_ERROR); 302 | return false; 303 | }elseif($size<10485760){ 304 | return 10485760; 305 | }else{ 306 | return $size; 307 | } 308 | }else{ 309 | return $default; 310 | } 311 | } 312 | function _format($truncate=false){ 313 | if($this->lock(true,true)){ 314 | if($truncate){ 315 | $this->_seek(0); 316 | ftruncate($this->_rs,$this->idx_node_base); 317 | } 318 | $this->max_size = $this->_parse_str_size(SECACHE_SIZE,15728640); //default:15m 319 | $this->_puts($this->header_padding,pack('V1a*',$this->max_size,$this->ver)); 320 | ksort($this->_bsize_list); 321 | $ds_offset = $this->data_base_pos; 322 | $i=0; 323 | foreach($this->_bsize_list as $size=>$count){ 324 | //将预分配的空间注册到free链表里 325 | $count *= min(3,floor($this->max_size/10485760)); 326 | $next_free_node = 0; 327 | for($j=0;$j<$count;$j++){ 328 | $this->_puts($ds_offset,pack('V',$next_free_node)); 329 | $next_free_node = $ds_offset; 330 | $ds_offset+=intval($size); 331 | } 332 | $code = pack(str_repeat('V1',count($this->schema_struct)),$size,$next_free_node,0,0,0,0); 333 | $this->_puts(60+$i*$this->schema_item_size,$code); 334 | $i++; 335 | } 336 | $this->_set_dcur_pos($ds_offset); 337 | $this->_puts($this->idx_base_pos,str_repeat("\0",262144)); 338 | $this->_puts($this->idx_seq_pos,pack('V',1)); 339 | $this->unlock(); 340 | return true; 341 | }else{ 342 | $this->trigger_error("Couldn't lock the file !",E_USER_ERROR); 343 | return false; 344 | } 345 | } 346 | function _get_node_root($key){ 347 | $this->_seek(hexdec(substr($key,0,4))*4+$this->idx_base_pos); 348 | $a= fread($this->_rs,4); 349 | list(,$offset) = unpack('V',$a); 350 | return $offset; 351 | } 352 | function _set_node_root($key,$value){ 353 | return $this->_puts(hexdec(substr($key,0,4))*4+$this->idx_base_pos,pack('V',$value)); 354 | } 355 | function _set_node($pos,$key,$value){ 356 | if(!$pos){ 357 | return false; 358 | } 359 | if(isset($this->_node_struct[$key])){ 360 | return $this->_puts($pos*$this->idx_node_size+$this->idx_node_base+$this->_node_struct[$key][0],pack($this->_node_struct[$key][1],$value)); 361 | }else{ 362 | return false; 363 | } 364 | } 365 | function _get_pos_by_key($offset,$key,&$pos){ 366 | if(!$offset){ 367 | $pos = 0; 368 | return false; 369 | } 370 | $info = $this->_get_node($offset); 371 | if($info['key']==$key){ 372 | $pos = $info['offset']; 373 | return true; 374 | }elseif($info['next'] && $info['next']!=$offset){ 375 | return $this->_get_pos_by_key($info['next'],$key,$pos); 376 | }else{ 377 | $pos = $offset; 378 | return false; 379 | } 380 | } 381 | function _lru_delete($info){ 382 | if($info['lru_right']){ 383 | $this->_set_node($info['lru_right'],'lru_left',$info['lru_left']); 384 | }else{ 385 | $this->_set_schema($this->_get_size_schema_id($info['size']),'lru_tail',$info['lru_left']); 386 | } 387 | if($info['lru_left']){ 388 | $this->_set_node($info['lru_left'],'lru_right',$info['lru_right']); 389 | }else{ 390 | $this->_set_schema($this->_get_size_schema_id($info['size']),'lru_head',$info['lru_right']); 391 | } 392 | return true; 393 | } 394 | function _lru_push($schema_id,$offset){ 395 | $lru_head = $this->_get_schema($schema_id,'lru_head'); 396 | $lru_tail = $this->_get_schema($schema_id,'lru_tail'); 397 | if((!$offset) || ($lru_head==$offset))return; 398 | $info = $this->_get_node($offset); 399 | $this->_set_node($info['lru_right'],'lru_left',$info['lru_left']); 400 | $this->_set_node($info['lru_left'],'lru_right',$info['lru_right']); 401 | $this->_set_node($offset,'lru_right',$lru_head); 402 | $this->_set_node($offset,'lru_left',0); 403 | $this->_set_node($lru_head,'lru_left',$offset); 404 | $this->_set_schema($schema_id,'lru_head',$offset); 405 | if($lru_tail==0){ 406 | $this->_set_schema($schema_id,'lru_tail',$offset); 407 | }elseif($lru_tail==$offset && $info['lru_left']){ 408 | $this->_set_schema($schema_id,'lru_tail',$info['lru_left']); 409 | } 410 | return true; 411 | } 412 | function _get_node($offset){ 413 | $this->_seek($offset*$this->idx_node_size + $this->idx_node_base); 414 | $info = unpack('V1next/V1prev/V1data/V1size/V1lru_right/V1lru_left/H*key',fread($this->_rs,$this->idx_node_size)); 415 | $info['offset'] = $offset; 416 | return $info; 417 | } 418 | function _lru_pop($schema_id){ 419 | if($node = $this->_get_schema($schema_id,'lru_tail')){ 420 | $info = $this->_get_node($node); 421 | if(!$info['data']){ 422 | return false; 423 | } 424 | $this->delete($info['key'],$info['offset']); 425 | if(!$this->_get_schema($schema_id,'free')){ 426 | $this->trigger_error('pop lru,But nothing free...',E_USER_ERROR); 427 | } 428 | return $info; 429 | }else{ 430 | return false; 431 | } 432 | } 433 | function _dalloc($schema_id,$lru_freed=false){ 434 | if($free = $this->_get_schema($schema_id,'free')){ //如果lru里有链表 435 | $this->_seek($free); 436 | list(,$next) = unpack('V',fread($this->_rs,4)); 437 | $this->_set_schema($schema_id,'free',$next); 438 | return $free; 439 | }elseif($lru_freed){ 440 | $this->trigger_error('Bat lru poped freesize',E_USER_ERROR); 441 | return false; 442 | }else{ 443 | $ds_offset = $this->_get_dcur_pos(); 444 | $size = $this->_get_schema($schema_id,'size'); 445 | if($size+$ds_offset > $this->max_size){ 446 | if($info = $this->_lru_pop($schema_id)){ 447 | return $this->_dalloc($schema_id,$info); 448 | }else{ 449 | $this->trigger_error('Can\'t alloc dataspace',E_USER_ERROR); 450 | return false; 451 | } 452 | }else{ 453 | $this->_set_dcur_pos($ds_offset+$size); 454 | return $ds_offset; 455 | } 456 | } 457 | } 458 | function _get_dcur_pos(){ 459 | $this->_seek($this->dfile_cur_pos); 460 | list(,$ds_offset) = unpack('V',fread($this->_rs,4)); 461 | return $ds_offset; 462 | } 463 | function _set_dcur_pos($pos){ 464 | return $this->_puts($this->dfile_cur_pos,pack('V',$pos)); 465 | } 466 | function _free_dspace($size,$pos){ 467 | if($pos>$this->max_size){ 468 | $this->trigger_error('free dspace over quota:'.$pos,E_USER_ERROR); 469 | return false; 470 | } 471 | $schema_id = $this->_get_size_schema_id($size); 472 | if($free = $this->_get_schema($schema_id,'free')){ 473 | $this->_puts($free,pack('V1',$pos)); 474 | }else{ 475 | $this->_set_schema($schema_id,'free',$pos); 476 | } 477 | $this->_puts($pos,pack('V1',0)); 478 | } 479 | function _dfollow($pos,&$c){ 480 | $c++; 481 | $this->_seek($pos); 482 | list(,$next) = unpack('V1',fread($this->_rs,4)); 483 | if($next){ 484 | return $this->_dfollow($next,$c); 485 | }else{ 486 | return $pos; 487 | } 488 | } 489 | function _free_node($pos){ 490 | $this->_seek($this->idx_free_pos); 491 | list(,$prev_free_node) = unpack('V',fread($this->_rs,4)); 492 | $this->_puts($pos*$this->idx_node_size+$this->idx_node_base,pack('V',$prev_free_node).str_repeat("\0",$this->idx_node_size-4)); 493 | return $this->_puts($this->idx_free_pos,pack('V',$pos)); 494 | } 495 | function _alloc_idx($data){ 496 | $this->_seek($this->idx_free_pos); 497 | list(,$list_pos) = unpack('V',fread($this->_rs,4)); 498 | if($list_pos){ 499 | $this->_seek($list_pos*$this->idx_node_size+$this->idx_node_base); 500 | list(,$prev_free_node) = unpack('V',fread($this->_rs,4)); 501 | $this->_puts($this->idx_free_pos,pack('V',$prev_free_node)); 502 | }else{ 503 | $this->_seek($this->idx_seq_pos); 504 | list(,$list_pos) = unpack('V',fread($this->_rs,4)); 505 | $this->_puts($this->idx_seq_pos,pack('V',$list_pos+1)); 506 | } 507 | return $this->_create_node($list_pos,$data); 508 | } 509 | function _create_node($pos,$data){ 510 | $this->_puts($pos*$this->idx_node_size + $this->idx_node_base 511 | ,pack('V1V1V1V1V1V1H*',$data['next'],$data['prev'],$data['data'],$data['size'],$data['lru_right'],$data['lru_left'],$data['key'])); 512 | return $pos; 513 | } 514 | function _set_schema($schema_id,$key,$value){ 515 | $info = array_flip($this->schema_struct); 516 | return $this->_puts(60+$schema_id*$this->schema_item_size + $info[$key]*4,pack('V',$value)); 517 | } 518 | function _get_schema($id,$key){ 519 | $info = array_flip($this->schema_struct); 520 | $this->_seek(60+$id*$this->schema_item_size); 521 | unpack('V1'.implode('/V1',$this->schema_struct),fread($this->_rs,$this->schema_item_size)); 522 | $this->_seek(60+$id*$this->schema_item_size + $info[$key]*4); 523 | list(,$value) =unpack('V',fread($this->_rs,4)); 524 | return $value; 525 | } 526 | function _all_schemas(){ 527 | $schema = array(); 528 | for($i=0;$i<16;$i++){ 529 | $this->_seek(60+$i*$this->schema_item_size); 530 | $info = unpack('V1'.implode('/V1',$this->schema_struct),fread($this->_rs,$this->schema_item_size)); 531 | if($info['size']){ 532 | $info['id'] = $i; 533 | $schema[$i] = $info; 534 | }else{ 535 | return $schema; 536 | } 537 | } 538 | } 539 | function schemaStatus(){ 540 | $return = array(); 541 | foreach($this->_all_schemas() as $k=>$schemaItem){ 542 | if($schemaItem['free']){ 543 | $this->_dfollow($schemaItem['free'],$schemaItem['freecount']); 544 | } 545 | $return[] = $schemaItem; 546 | } 547 | return $return; 548 | } 549 | function status(&$curBytes,&$totalBytes){ 550 | $totalBytes = $curBytes = 0; 551 | $hits = $miss = 0; 552 | $schemaStatus = $this->schemaStatus(); 553 | $totalBytes = $this->max_size; 554 | $freeBytes = $this->max_size - $this->_get_dcur_pos(); 555 | foreach($schemaStatus as $schema){ 556 | $freeBytes+=$schema['freecount']*$schema['size']; 557 | $miss += $schema['miss']; 558 | $hits += $schema['hits']; 559 | } 560 | $curBytes = $totalBytes-$freeBytes; 561 | $return[] = array('name'=>'缓存命中','value'=>$hits); 562 | $return[] = array('name'=>'缓存未命中','value'=>$miss); 563 | return $return; 564 | } 565 | function trigger_error($errstr,$errno){ 566 | trigger_error($errstr,$errno); 567 | } 568 | } 569 | ?> 570 | -------------------------------------------------------------------------------- /lib/fetch.php: -------------------------------------------------------------------------------- 1 | 1, //true, $head 有请求的返回值 13 | CURLOPT_BINARYTRANSFER => true, //返回原生的Raw输出 14 | CURLOPT_HEADER => true, //启用时会将头文件的信息作为数据流输出。 15 | CURLOPT_FAILONERROR => true, //显示HTTP状态码,默认行为是忽略编号小于等于400的HTTP信息。 16 | CURLOPT_AUTOREFERER => true, //当根据Location:重定向时,自动设置header中的Referer:信息。 17 | CURLOPT_FOLLOWLOCATION => false, //跳转 18 | CURLOPT_CONNECTTIMEOUT => 3, //在发起连接前等待的时间,如果设置为0,则无限等待。 19 | CURLOPT_TIMEOUT => 5, //设置cURL允许执行的最长秒数。 20 | CURLOPT_ENCODING => 'gzip,deflate', 21 | CURLOPT_SSL_VERIFYHOST => false, 22 | CURLOPT_SSL_VERIFYPEER => false, 23 | ); 24 | foreach ($opt as $k => $v) { 25 | self::$curl_opt[$k] = $v; 26 | } 27 | } 28 | 29 | /** 30 | * fetch::get('http://www.google.com/'); 31 | * fetch::post('http://www.google.com/', array('name'=>'foo')); 32 | */ 33 | public static function __callstatic($method, $args) { 34 | if (is_null(self::$curl_opt)) { 35 | self::init(); 36 | } 37 | @list($request, $post_data, $callback) = $args; 38 | if (is_callable($post_data)) { 39 | $callback = $post_data; 40 | $post_data = null; 41 | } 42 | 43 | //single_curl 44 | if (is_string($request) || !empty($request['url'])) { 45 | $request = self::bulid_request($request, $method, $post_data, $callback); 46 | return self::single_curl($request); 47 | } elseif (is_array($request)) { 48 | //rolling_curl 49 | foreach ($request as $k => $r) { 50 | $requests[$k] = self::bulid_request($r, $method, $post_data, $callback); 51 | } 52 | return self::rolling_curl($requests); 53 | } 54 | } 55 | 56 | private static function bulid_request($request, $method = 'GET', $post_data = null, $callback = null) { 57 | //url 58 | if (is_string($request)) { 59 | $request = array('url' => $request); 60 | } 61 | empty($request['method']) && $request['method'] = $method; 62 | empty($request['post_data']) && $request['post_data'] = $post_data; 63 | empty($request['callback']) && $request['callback'] = $callback; 64 | return $request; 65 | } 66 | 67 | private static function bulid_ch(&$request) { 68 | // url 69 | $ch = curl_init($request['url']); 70 | // curl_opt 71 | $curl_opt = empty($request['curl_opt']) ? array() : $request['curl_opt']; 72 | $curl_opt = $curl_opt + (array) self::$curl_opt; 73 | // method 74 | $curl_opt[CURLOPT_CUSTOMREQUEST] = strtoupper($request['method']); 75 | // post_data 76 | if (!empty($request['post_data'])) { 77 | $curl_opt[CURLOPT_POST] = true; 78 | $curl_opt[CURLOPT_POSTFIELDS] = $request['post_data']; 79 | } 80 | // header 81 | $headers = @self::bulid_request_header($request['headers'], $cookies); 82 | $curl_opt[CURLOPT_HTTPHEADER] = $headers; 83 | 84 | // cookies 85 | $request['cookies'] = empty($request['cookies']) ? fetch::$cookies : $request['cookies']; 86 | $cookies = empty($request['cookies']) ? $cookies : self::cookies_arr2str($request['cookies']); 87 | if (!empty($cookies)) { 88 | $curl_opt[CURLOPT_COOKIE] = $cookies; 89 | } 90 | 91 | //proxy 92 | $proxy = empty($request['proxy']) ? self::$proxy : $request['proxy']; 93 | if (!empty($proxy)) { 94 | $curl_opt[CURLOPT_PROXY] = $proxy; 95 | } 96 | 97 | //setopt 98 | curl_setopt_array($ch, $curl_opt); 99 | 100 | $request['curl_opt'] = $curl_opt; 101 | $request['ch'] = $ch; 102 | 103 | return $ch; 104 | } 105 | 106 | private static function response($raw, $ch) { 107 | $response = (object) curl_getinfo($ch); 108 | $response->raw = $raw; 109 | //$raw = fetch::iconv($raw, $response->content_type); 110 | $response->headers = substr($raw, 0, $response->header_size); 111 | $response->cookies = fetch::get_respone_cookies($response->headers); 112 | fetch::$cookies = array_merge((array) fetch::$cookies, $response->cookies); 113 | $response->content = substr($raw, $response->header_size); 114 | return $response; 115 | } 116 | 117 | private static function single_curl($request) { 118 | $ch = self::bulid_ch($request); 119 | $raw = curl_exec($ch); 120 | $response = self::response($raw, $ch); 121 | curl_close($ch); 122 | if (is_callable($request['callback'])) { 123 | call_user_func($request['callback'], $response, $request); 124 | } 125 | return $response; 126 | } 127 | 128 | private static function rolling_curl($requests) { 129 | $master = curl_multi_init(); 130 | $map = array(); 131 | // start the first batch of requests 132 | do { 133 | $k = key($requests); 134 | $request = current($requests); 135 | next($requests); 136 | $ch = self::bulid_ch($request); 137 | curl_multi_add_handle($master, $ch); 138 | $key = (string) $ch; 139 | $map[$key] = array($k, $request['callback']); 140 | } while (count($map) < self::$max_connect && count($map) < count($requests)); 141 | 142 | do { 143 | while (($execrun = curl_multi_exec($master, $running)) == CURLM_CALL_MULTI_PERFORM); 144 | if ($execrun != CURLM_OK) { 145 | break; 146 | } 147 | 148 | // a request was just completed -- find out which one 149 | while ($done = curl_multi_info_read($master)) { 150 | $key = (string) $done['handle']; 151 | 152 | list($k, $callback) = $map[$key]; 153 | 154 | // get the info and content returned on the request 155 | $raw = curl_multi_getcontent($done['handle']); 156 | $response = self::response($raw, $done['handle']); 157 | $responses[$k] = $response; 158 | 159 | // send the return values to the callback function. 160 | if (is_callable($callback)) { 161 | $key = (string) $done['handle']; 162 | unset($map[$key]); 163 | call_user_func($callback, $response, $requests[$k], $k); 164 | } 165 | 166 | // start a new request (it's important to do this before removing the old one) 167 | $k = key($requests); 168 | if (!empty($k)) { 169 | $k = key($requests); 170 | $request = current($requests); 171 | next($requests); 172 | $ch = self::bulid_ch($request); 173 | curl_multi_add_handle($master, $ch); 174 | $key = (string) $ch; 175 | $map[$key] = array($k, $request['callback']); 176 | curl_multi_exec($master, $running); 177 | } 178 | 179 | // remove the curl handle that just completed 180 | curl_multi_remove_handle($master, $done['handle']); 181 | } 182 | 183 | // Block for data in / output; error handling is done by curl_multi_exec 184 | if ($running) { 185 | curl_multi_select($master, 10); 186 | } 187 | 188 | } while ($running); 189 | 190 | return $responses; 191 | } 192 | 193 | private static function bulid_request_header($headers, &$cookies) { 194 | if (is_array($headers)) { 195 | $headers = join(PHP_EOL, $headers); 196 | } 197 | if (is_array(self::$headers)) { 198 | self::$headers = join(PHP_EOL, self::$headers); 199 | } 200 | $headers = self::$headers.PHP_EOL .$headers; 201 | 202 | foreach (explode(PHP_EOL, $headers) as $k => $v) { 203 | @list($k, $v) = explode(':', $v, 2); 204 | if (empty($k) || empty($v)) { 205 | continue; 206 | } 207 | $k = implode('-', array_map('ucfirst', explode('-', $k))); 208 | $tmp[$k] = $v; 209 | } 210 | 211 | foreach ((array) $tmp as $k => $v) { 212 | if ($k == 'Cookie') { 213 | $cookies = $v; 214 | } else { 215 | $return[] = $k . ':' . $v; 216 | } 217 | } 218 | return (array) $return; 219 | } 220 | 221 | public static function iconv(&$raw, $content_type) { 222 | @list($tmp, $charset) = explode('CHARSET=', strtoupper($content_type)); 223 | 224 | if (empty($charset) && stripos($content_type, 'html') > 0) { 225 | preg_match('@\ $v) { 255 | $str .= $k . "=" . $v . "; "; 256 | } 257 | return $str; 258 | } 259 | } -------------------------------------------------------------------------------- /lib/onedrive.php: -------------------------------------------------------------------------------- 1 | content, true); 34 | return $data; 35 | } 36 | 37 | //使用 $refresh_token,获取 $access_token 38 | static function get_token($refresh_token){ 39 | $client_id = self::$client_id; 40 | $client_secret = self::$client_secret; 41 | $redirect_uri = self::$redirect_uri; 42 | 43 | $request['url'] = self::$oauth_url."/token"; 44 | $request['post_data'] = "client_id={$client_id}&redirect_uri={$redirect_uri}&client_secret={$client_secret}&refresh_token={$refresh_token}&grant_type=refresh_token"; 45 | $request['headers']= "Content-Type: application/x-www-form-urlencoded"; 46 | $resp = fetch::post($request); 47 | $data = json_decode($resp->content, true); 48 | return $data; 49 | } 50 | 51 | //获取 $access_token, 带缓存 52 | static function access_token(){ 53 | $token = config('@token'); 54 | if($token['expires_on'] > time()+600){ 55 | return $token['access_token']; 56 | }else{ 57 | $refresh_token = config('refresh_token'); 58 | $token = self::get_token($refresh_token); 59 | if(!empty($token['refresh_token'])){ 60 | $token['expires_on'] = time()+ $token['expires_in']; 61 | config('@token', $token); 62 | return $token['access_token']; 63 | } 64 | } 65 | return ""; 66 | } 67 | 68 | 69 | // 生成一个request,带token 70 | static function request($path="/", $query=""){ 71 | $path = self::urlencode($path); 72 | $path = empty($path)?'/':":/{$path}:/"; 73 | $token = self::access_token(); 74 | $request['headers'] = "Authorization: bearer {$token}".PHP_EOL."Content-Type: application/json".PHP_EOL; 75 | $request['url'] = self::$api_url."/me/drive/root".$path.$query; 76 | return $request; 77 | } 78 | 79 | 80 | //返回目录信息 81 | static function dir($path="/"){ 82 | $request = self::request($path, "children?select=name,size,folder,@microsoft.graph.downloadUrl,lastModifiedDateTime"); 83 | $items = array(); 84 | self::dir_next_page($request, $items); 85 | //不在列表显示的文件夹 86 | $hide_list = explode(PHP_EOL,config('onedrive_hide')); 87 | if(is_array($hide_list) && count($hide_list)>0){ 88 | foreach($hide_list as $hide_dir){ 89 | foreach($items as $key=>$_array){ 90 | if(!empty(trim($hide_dir)) && stristr($key,trim($hide_dir)))unset($items[$key]); 91 | } 92 | } 93 | } 94 | return $items; 95 | } 96 | 97 | //通过分页获取页面所有item 98 | static function dir_next_page($request, &$items, $retry=0){ 99 | $resp = fetch::get($request); 100 | 101 | $data = json_decode($resp->content, true); 102 | if(empty($data) && $retry < 3){ 103 | $retry += 1; 104 | return self::dir_next_page($request, $items, $retry); 105 | } 106 | 107 | foreach((array)$data['value'] as $item){ 108 | //var_dump($item); 109 | $items[$item['name']] = array( 110 | 'name'=>$item['name'], 111 | 'size'=>$item['size'], 112 | 'lastModifiedDateTime'=>strtotime($item['lastModifiedDateTime']), 113 | 'downloadUrl'=>$item['@microsoft.graph.downloadUrl'], 114 | 'folder'=>empty($item['folder'])?false:true 115 | ); 116 | } 117 | 118 | if(!empty($data['@odata.nextLink'])){ 119 | $request = self::request(); 120 | $request['url'] = $data['@odata.nextLink']; 121 | return self::dir_next_page($request, $items); 122 | } 123 | } 124 | 125 | 126 | //static function content($path){ 127 | // $token = self::access_token(); 128 | // fetch::$headers = "Authorization: bearer {$token}"; 129 | // $url = self::$api_url."/me/drive/root:".self::urlencode($path).":/content"; 130 | // $resp = fetch::get($url); 131 | // return $resp->content; 132 | //} 133 | 134 | //文件缩略图链接 135 | static function thumbnail($path,$size='large'){ 136 | $request = self::request($path,"thumbnails/0?select={$size}"); 137 | $resp = fetch::get($request); 138 | $data = json_decode($resp->content, true); 139 | $request = self::request($path,"thumbnails/0?select={$size}"); 140 | return @$data[$size]['url']; 141 | } 142 | 143 | //文件上传函数 144 | static function upload($path,$content){ 145 | $request = self::request($path,"content"); 146 | $request['post_data'] = $content; 147 | $resp = fetch::put($request); 148 | $data = @json_decode($resp->content, true); 149 | return $data; 150 | } 151 | 152 | static function upload_url($path, $url){ 153 | $request = self::request(get_absolute_path(dirname($path)),"children"); 154 | $request['headers'] .= "Prefer: respond-async".PHP_EOL; 155 | $post_data['@microsoft.graph.sourceUrl'] = $url; 156 | $post_data['name'] = pathinfo($path, PATHINFO_BASENAME ); 157 | $post_data['file'] = json_decode("{}"); 158 | $request['post_data'] = json_encode($post_data); 159 | $resp = fetch::post($request); 160 | list($tmp, $location) = explode('ocation:', $resp->headers); 161 | list($location, $tmp) = explode(PHP_EOL, $location); 162 | return trim($location); 163 | } 164 | 165 | static function create_upload_session($path){ 166 | $request = self::request($path, 'createUploadSession'); 167 | $request['post_data'] = '{"item": {"@microsoft.graph.conflictBehavior": "fail"}}'; 168 | $token = self::access_token(); 169 | $resp = fetch::post($request); 170 | $data = json_decode($resp->content, true); 171 | if($resp->http_code == 409){ 172 | return false; 173 | } 174 | return $data; 175 | } 176 | 177 | static function upload_session($url, $file, $offset, $length=10240){ 178 | $token = self::access_token(); 179 | $file_size = self::_filesize($file); 180 | $content_length = (($offset+$length)>$file_size)?($file_size-$offset):$length; 181 | $end = $offset+$content_length-1; 182 | $post_data = self::file_content($file, $offset, $length); 183 | 184 | $request['url'] = $url; 185 | $request['curl_opt']=[CURLOPT_TIMEOUT=>360]; 186 | $request['headers'] = "Authorization: bearer {$token}".PHP_EOL; 187 | $request['headers'] .= "Content-Length: {$content_length}".PHP_EOL; 188 | $request['headers'] .= "Content-Range: bytes {$offset}-{$end}/{$file_size}"; 189 | $request['post_data'] = $post_data; 190 | $resp = fetch::put($request); 191 | $data = json_decode($resp->content, true); 192 | return $data; 193 | } 194 | 195 | static function upload_session_status($url){ 196 | $token = self::access_token(); 197 | fetch::$headers = "Authorization: bearer {$token}".PHP_EOL."Content-Type: application/json".PHP_EOL; 198 | $resp = fetch::get($url); 199 | $data = json_decode($resp->content, true); 200 | return $data; 201 | } 202 | 203 | static function delete_upload_session($url){ 204 | $token = self::access_token(); 205 | fetch::$headers = "Authorization: bearer {$token}".PHP_EOL."Content-Type: application/json".PHP_EOL; 206 | $resp = fetch::delete($url); 207 | $data = json_decode($resp->content, true); 208 | return $data; 209 | } 210 | 211 | static function file_content($file, $offset, $length){ 212 | $handler = fopen($file, "rb") OR die('获取文件内容失败'); 213 | fseek($handler, $offset); 214 | 215 | return fread($handler, $length); 216 | } 217 | 218 | static function human_filesize($size, $precision = 1) { 219 | for($i = 0; ($size / 1024) > 1; $i++, $size /= 1024) {} 220 | return round($size, $precision).(['B','KB','MB','GB','TB','PB','EB','ZB','YB'][$i]); 221 | } 222 | 223 | static function urlencode($path){ 224 | foreach(explode('/', $path) as $k=>$v){ 225 | if(empty(!$v)){ 226 | $paths[] = rawurlencode($v); 227 | } 228 | } 229 | return @join('/',$paths); 230 | } 231 | 232 | static function _filesize($path){ 233 | if (!file_exists($path)) 234 | return false; 235 | $size = filesize($path); 236 | 237 | if (!($file = fopen($path, 'rb'))) 238 | return false; 239 | 240 | if ($size >= 0){//Check if it really is a small file (< 2 GB) 241 | if (fseek($file, 0, SEEK_END) === 0){//It really is a small file 242 | fclose($file); 243 | return $size; 244 | } 245 | } 246 | 247 | //Quickly jump the first 2 GB with fseek. After that fseek is not working on 32 bit php (it uses int internally) 248 | $size = PHP_INT_MAX - 1; 249 | if (fseek($file, PHP_INT_MAX - 1) !== 0){ 250 | fclose($file); 251 | return false; 252 | } 253 | 254 | $length = 1024 * 1024; 255 | while (!feof($file)){//Read the file until end 256 | $read = fread($file, $length); 257 | $size = bcadd($size, $length); 258 | } 259 | $size = bcsub($size, $length); 260 | $size = bcadd($size, strlen($read)); 261 | 262 | fclose($file); 263 | return $size; 264 | } 265 | } 266 | -------------------------------------------------------------------------------- /lib/oneindex.php: -------------------------------------------------------------------------------- 1 | '[^/]+', 10 | '#num' => '[0-9]+', 11 | '#all' => '.*', 12 | ); 13 | 14 | public static function __callstatic($method, $args) { 15 | if (self::$runed) { 16 | return; 17 | } 18 | 19 | if (empty(self::$method)) { 20 | self::init(); 21 | } 22 | 23 | $method = strtoupper($method); 24 | if ($method != self::$method && !in_array($method, ['ANY', 'ERROR', 'ON'])) { 25 | return; 26 | } 27 | $pattern = trim(array_shift($args), '\/'); 28 | $pattern = self::$root . $pattern; 29 | 30 | if (self::uri_match($pattern, self::$uri)) { 31 | if (is_string($args[0]) && strpos($args[0], '@') > 0) { 32 | list($class, $action) = explode('@', $args[0]); 33 | $object = new $class(); 34 | $args[0] = array($object, $action); 35 | } 36 | 37 | $return = call_user_func($args[0]); 38 | if (is_array($return)) { 39 | print json_encode($return); 40 | } else { 41 | print (string) $return; 42 | } 43 | self::$runed = true; 44 | } 45 | } 46 | 47 | public static function init() { 48 | if (!empty(self::$method)) {return;} 49 | self::$uri = self::get_uri(); 50 | self::$method = empty($_POST['_METHOD']) ? $_SERVER['REQUEST_METHOD'] : $_POST['_METHOD']; 51 | if (defined('CONTROLLER_PATH')) { 52 | spl_autoload_register(function ($class) { 53 | $file = CONTROLLER_PATH . $class . '.php'; 54 | if (file_exists($file)) { 55 | include $file; 56 | } 57 | }); 58 | } 59 | } 60 | 61 | public static function auto($controller_path) { 62 | self::init(); 63 | $uri = self::get_uri(); 64 | list($tmp, $controller, $action) = explode('/', $uri); 65 | $controller = empty($controller) ? 'IndexController' : ucfirst($controller) . 'Controller'; 66 | $action = empty($action) ? 'index' : $action; 67 | $file = $controller_path . $controller . '.php'; 68 | if (file_exists($file)) { 69 | include $file; 70 | if (is_callable(array($controller, $action))) { 71 | $obj = new $controller(); 72 | print (string) $obj->$action(); 73 | return; 74 | } 75 | } 76 | } 77 | 78 | public static function group($middleware, $callback){ 79 | self::init(); 80 | if (is_string($middleware) && strpos($middleware, '@') > 0) { 81 | list($class, $action) = explode('@', $middleware); 82 | $object = new $class(); 83 | $result = $object->$action(); 84 | }elseif(is_callable($middleware)){ 85 | $result = $middleware(); 86 | } 87 | 88 | if($result == true && is_callable($callback)){ 89 | return $callback(); 90 | } 91 | } 92 | 93 | public static function resource($name, $controller) { 94 | self::get('/' . $name, $controller . '@index'); 95 | self::get('/' . $name . '/add', $controller . '@add'); 96 | self::post('/' . $name, $controller . '@store'); 97 | self::get('/' . $name . '/{id:#num}', $controller . '@show'); 98 | self::get('/' . $name . '/{id:#num}/edit', $controller . '@edit'); 99 | self::post('/' . $name . '/{id:#num}', $controller . '@update'); 100 | self::get('/' . $name . '/{id:#num}/delete', $controller . '@delete'); 101 | } 102 | 103 | public static function uri_match($pattern, $uri) { 104 | $pattern = ($pattern == '/') ? '/' : rtrim($pattern, '\/'); 105 | 106 | $ps = explode('/', $pattern); 107 | 108 | $searches = array_keys(static::$patterns); 109 | $replaces = array_values(static::$patterns); 110 | 111 | foreach($ps as &$p){ 112 | $p = str_replace($searches, $replaces, $p); 113 | $p = preg_replace("`\{(\w+)\:([^\)]+)\}`", '(?P<$1>$2)', $p); 114 | } 115 | 116 | $pattern = join('/',$ps); 117 | 118 | if (preg_match("`^{$pattern}$`", $uri)) { 119 | preg_match_all("`^{$pattern}$`", $uri, $matches, PREG_PATTERN_ORDER); 120 | foreach ($matches as $key => $value) { 121 | if (!is_int($key)) { 122 | $_GET[$key] = $matches[$key][0]; 123 | } 124 | } 125 | return true; 126 | } 127 | } 128 | 129 | public static function get_uri() { 130 | $file = basename($_SERVER['PHP_SELF']); 131 | $path = dirname($_SERVER['PHP_SELF']); 132 | $req_uri = $_SERVER['REQUEST_URI']; 133 | 134 | if ($path != '/' && strpos($req_uri, $path) === 0) { 135 | $req_uri = substr($req_uri, strlen($path)); 136 | } 137 | 138 | if (strpos($req_uri, '/?/') === 0) { 139 | $req_uri = parse_url($req_uri, PHP_URL_QUERY); 140 | list($req_uri) = explode('&', $req_uri); 141 | unset($_GET[$req_uri]); 142 | } 143 | $uri = parse_url($req_uri, PHP_URL_PATH); 144 | return '/' . trim($uri, '\/'); 145 | } 146 | } -------------------------------------------------------------------------------- /lib/sqlite.php: -------------------------------------------------------------------------------- 1 | db = new PDO('sqlite:' . $filename); 18 | $this->db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 19 | $this->name = $name; 20 | $this->createTable(); 21 | } 22 | 23 | /** 24 | * @param string $key key 25 | * 26 | * @throws InvalidArgumentException 27 | * @return string|null 28 | */ 29 | public function get($key) 30 | { 31 | if (!is_string($key)) { 32 | throw new InvalidArgumentException('Expected string as key'); 33 | } 34 | 35 | $stmt = $this->db->prepare( 36 | 'SELECT value FROM ' . $this->name . ' WHERE key = :key;' 37 | ); 38 | $stmt->bindParam(':key', $key, PDO::PARAM_STR); 39 | $stmt->execute(); 40 | 41 | if ($row = $stmt->fetch(PDO::FETCH_OBJ)) { 42 | return $row->value; 43 | } 44 | 45 | return null; 46 | } 47 | 48 | /** 49 | * @param string $key key 50 | * @param string $value value 51 | * 52 | * @throws InvalidArgumentException 53 | */ 54 | public function set($key, $value) 55 | { 56 | if (!is_string($key)) { 57 | throw new InvalidArgumentException('Expected string as key'); 58 | } 59 | 60 | $queryString = 'REPLACE INTO ' . $this->name . ' VALUES (:key, :value);'; 61 | $stmt = $this->db->prepare($queryString); 62 | $stmt->bindParam(':key', $key, \PDO::PARAM_STR); 63 | $stmt->bindParam(':value', $value, \PDO::PARAM_STR); 64 | $stmt->execute(); 65 | } 66 | 67 | /** 68 | * @param string $key key 69 | * 70 | * @return null 71 | */ 72 | public function delete($key) 73 | { 74 | $stmt = $this->db->prepare( 75 | 'DELETE FROM ' . $this->name . ' WHERE key = :key;' 76 | ); 77 | $stmt->bindParam(':key', $key, \PDO::PARAM_STR); 78 | $stmt->execute(); 79 | } 80 | 81 | /** 82 | * Delete all values from store 83 | * 84 | * @return null 85 | */ 86 | public function deleteAll() 87 | { 88 | $stmt = $this->db->prepare('DELETE FROM ' . $this->name); 89 | $stmt->execute(); 90 | $this->data = array(); 91 | } 92 | 93 | /** 94 | * @return int 95 | */ 96 | public function count() 97 | { 98 | return (int) $this->db->query('SELECT COUNT(*) FROM ' . $this->name)->fetchColumn(); 99 | } 100 | 101 | /** 102 | * Create storage table in database if not exists 103 | * 104 | * @return null 105 | */ 106 | private function createTable() 107 | { 108 | $stmt = 'CREATE TABLE IF NOT EXISTS "' . $this->name . '"'; 109 | $stmt.= '(key TEXT PRIMARY KEY, value TEXT);'; 110 | $this->db->exec($stmt); 111 | } 112 | } 113 | -------------------------------------------------------------------------------- /lib/view.php: -------------------------------------------------------------------------------- 1 | '', 'data' => array()); 5 | static $_pos = null; 6 | static $_section = null; 7 | 8 | static function load($file, $set = null) { 9 | if (is_int(strripos($file, '..'))) { 10 | die("error view file name:$file"); 11 | } 12 | $file = str_replace('.', '/', $file); 13 | 14 | return new view($file, $set); 15 | } 16 | 17 | public function __construct($file, $set = null) { 18 | if (isset(self::$_pos)) { 19 | $this->_view['parent'] = self::$_pos; 20 | $this->with($this->_view['parent']->_view['data']); 21 | } 22 | $this->_view['file'] = $file; 23 | $this->with($set); 24 | } 25 | 26 | public function with($name, $value = NULL) { 27 | if (is_array($name)) { 28 | $this->_view['data'] = array_merge($this->_view['data'], $name); 29 | } elseif (is_string($name)) { 30 | $this->_view['data'][$name] = $value; 31 | } 32 | return $this; 33 | } 34 | 35 | public function __toString() { 36 | self::$_pos = $this; 37 | 38 | $this->_view['file'] = VIEW_PATH . $this->_view['file'] . '.php'; 39 | if(!file_exists($this->_view['file'])){ 40 | self::to404('404'); 41 | } 42 | 43 | extract($this->_view['data']); 44 | 45 | ob_start(); 46 | include $this->_view['file']; 47 | 48 | if (isset($this->_view['layout'])) { 49 | echo $this->_view['layout'] . ob_get_clean(); 50 | } 51 | 52 | 53 | self::$_pos = isset($this->_view['parent']) ? $this->_view['parent'] : NULL; 54 | 55 | $return_str = ob_get_clean(); 56 | 57 | return $return_str; 58 | } 59 | 60 | public function show() { 61 | echo $this; 62 | } 63 | 64 | 65 | static function layout($file) { 66 | ob_start(); 67 | $view_layout = self::load($file); 68 | self::$_pos->_view['layout'] = $view_layout; 69 | } 70 | 71 | static function section($name) { 72 | if (isset(self::$_pos->_view['section'][$name])) { 73 | echo self::$_pos->_view['section'][$name]; 74 | } elseif (isset(self::$_pos->_view['parent']->_view['section'][$name])) { 75 | echo self::$_pos->_view['parent']->_view['section'][$name]; 76 | } 77 | } 78 | 79 | static function begin($name) { 80 | self::$_section = $name; 81 | ob_start(); 82 | } 83 | 84 | static function end() { 85 | self::$_pos->_view['section'][self::$_section] = ob_get_clean(); 86 | self::$_section = null; 87 | } 88 | 89 | static function direct($loction) { 90 | header("Location:$loction"); 91 | exit(); 92 | } 93 | 94 | static function abort() { 95 | header("HTTP/1.1 502 Bad Gateway"); 96 | exit(); 97 | } 98 | 99 | static function to404(){ 100 | header("HTTP/1.1 404 Not Found"); 101 | exit(); 102 | } 103 | } 104 | -------------------------------------------------------------------------------- /nginx/fastcgi_params: -------------------------------------------------------------------------------- 1 | #fastcgi_params 2 | fastcgi_param GATEWAY_INTERFACE CGI/1.1; 3 | fastcgi_param REDIRECT_STATUS 200; 4 | fastcgi_param SERVER_SOFTWARE nginx/$nginx_version; 5 | fastcgi_param QUERY_STRING $query_string; 6 | fastcgi_param REQUEST_METHOD $request_method; 7 | fastcgi_param CONTENT_TYPE $content_type; 8 | fastcgi_param CONTENT_LENGTH $content_length; 9 | fastcgi_param SCRIPT_NAME $fastcgi_script_name; 10 | fastcgi_param REQUEST_URI $request_uri; 11 | fastcgi_param DOCUMENT_URI $document_uri; 12 | fastcgi_param DOCUMENT_ROOT $document_root; 13 | fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; 14 | fastcgi_param SERVER_PROTOCOL $server_protocol; 15 | fastcgi_param HTTPS $https if_not_empty; 16 | fastcgi_param REMOTE_ADDR $remote_addr; 17 | fastcgi_param REMOTE_PORT $remote_port; 18 | fastcgi_param SERVER_ADDR $server_addr; 19 | fastcgi_param SERVER_PORT $server_port; 20 | fastcgi_param SERVER_NAME $server_name; 21 | -------------------------------------------------------------------------------- /nginx/fcgiwrap-php: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | ### BEGIN INIT INFO 3 | # Provides: fcgiwrap-php 4 | # Required-Start: $remote_fs 5 | # Required-Stop: $remote_fs 6 | # Should-Start: 7 | # Should-Stop: 8 | # Default-Start: 2 3 4 5 9 | # Default-Stop: 0 1 6 10 | # Short-Description: FastCGI-PHP wrapper 11 | # Description: Simple server for running CGI applications over FastCGI 12 | ### END INIT INFO 13 | 14 | PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin 15 | 16 | SPAWN_FCGI="/usr/bin/spawn-fcgi" 17 | DAEMON="/usr/bin/php-cgi" 18 | NAME="fcgiwrap-php" 19 | DESC="FastCGI-PHP wrapper" 20 | 21 | PIDFILE="/var/run/$NAME.pids" 22 | 23 | test -x $SPAWN_FCGI || exit 0 24 | test -x $DAEMON || exit 0 25 | 26 | # FCGI_APP Variables 27 | FCGI_CHILDREN="3" 28 | FCGI_SOCKET="/dev/shm/$NAME.socket" 29 | FCGI_USER="www-data" 30 | FCGI_GROUP="www-data" 31 | # Socket owner/group (will default to FCGI_USER/FCGI_GROUP if not defined) 32 | FCGI_SOCKET_OWNER="www-data" 33 | FCGI_SOCKET_GROUP="www-data" 34 | 35 | . /lib/lsb/init-functions 36 | 37 | # Default options, these can be overriden by the information 38 | # at /etc/default/$NAME 39 | DAEMON_OPTS="-C 5" # Additional options given to the server 40 | 41 | DIETIME=10 # Time to wait for the server to die, in seconds 42 | # If this value is set too low you might not 43 | # let some servers to die gracefully and 44 | # 'restart' will not work 45 | QDIETIME=0.5 # The same as DIETIME, but a lot shorter for the 46 | # stop case. 47 | 48 | #STARTTIME=2 # Time to wait for the server to start, in seconds 49 | # If this value is set each time the server is 50 | # started (on start or restart) the script will 51 | # stall to try to determine if it is running 52 | # If it is not set and the server takes time 53 | # to setup a pid file the log message might 54 | # be a false positive (says it did not start 55 | # when it actually did) 56 | 57 | # Include defaults if available 58 | if [ -f /etc/default/$NAME ] ; then 59 | . /etc/default/$NAME 60 | fi 61 | 62 | DAEMONUSER="$FCGI_USER" 63 | 64 | # Check that the user exists (if we set a user) 65 | # Does the user exist? 66 | if [ -n "$DAEMONUSER" ] ; then 67 | if getent passwd | grep -q "^$DAEMONUSER:"; then 68 | # Obtain the uid and gid 69 | DAEMONUID=$(getent passwd |grep "^$DAEMONUSER:" | awk -F : '{print $3}') 70 | DAEMONGID=$(getent passwd |grep "^$DAEMONUSER:" | awk -F : '{print $4}') 71 | else 72 | log_failure_msg "The user $DAEMONUSER, required to run $NAME does not exist." 73 | exit 1 74 | fi 75 | fi 76 | 77 | set -e 78 | 79 | running_pid() { 80 | # Check if a given process pid's cmdline matches a given name 81 | pid=$1 82 | name=$2 83 | [ -z "$pid" ] && return 1 84 | [ ! -d /proc/$pid ] && return 1 85 | cmd="$(cat /proc/$pid/cmdline | tr "\000" "\n"|head -n 1 |cut -d : -f 1)" 86 | # Is this the expected server 87 | [ "$cmd" != "$name" ] && return 1 88 | return 0 89 | } 90 | 91 | running() { 92 | # Check if the process is running looking at /proc 93 | # (works for all users) 94 | # No pidfile, probably no daemon present 95 | [ ! -f "$PIDFILE" ] && return 1 96 | PIDS="$(cat "$PIDFILE")" 97 | for pid in $PIDS; do 98 | if [ -n "$pid" ]; then 99 | running_pid $pid $DAEMON && return 0 || true 100 | fi 101 | done 102 | return 1 103 | } 104 | 105 | start_server() { 106 | ARGS="-P $PIDFILE" 107 | # Adjust NUMBER of processes 108 | if [ -n "$FCGI_CHILDREN" ]; then 109 | ARGS="$ARGS -F '$FCGI_CHILDREN'" 110 | fi 111 | # Adjust SOCKET or PORT and ADDR 112 | if [ -n "$FCGI_SOCKET" ]; then 113 | ARGS="$ARGS -s '$FCGI_SOCKET'" 114 | elif [ -n "$FCGI_PORT" ]; then 115 | if [ -n "$FCGI_ADDR" ]; then 116 | ARGS="$ARGS -a '$FCGI_ADDR'" 117 | fi 118 | ARGS="$ARGS -p '$FCGI_PORT'" 119 | fi 120 | # Adjust user 121 | if [ -n "$FCGI_USER" ]; then 122 | ARGS="$ARGS -u '$FCGI_USER'" 123 | if [ -n "$FCGI_SOCKET" ]; then 124 | if [ -n "$FCGI_SOCKET_OWNER" ]; then 125 | ARGS="$ARGS -U '$FCGI_SOCKET_OWNER'" 126 | else 127 | ARGS="$ARGS -U '$FCGI_USER'" 128 | fi 129 | fi 130 | fi 131 | # Adjust group 132 | if [ -n "$FCGI_GROUP" ]; then 133 | ARGS="$ARGS -g '$FCGI_GROUP'" 134 | if [ -n "$FCGI_SOCKET" ]; then 135 | if [ -n "$FCGI_SOCKET_GROUP" ]; then 136 | ARGS="$ARGS -G '$FCGI_SOCKET_GROUP'" 137 | else 138 | ARGS="$ARGS -G '$FCGI_GROUP'" 139 | fi 140 | fi 141 | fi 142 | eval $(echo $SPAWN_FCGI $ARGS -f $DAEMON $DAEMON_OPTS) > /dev/null 143 | errcode="$?" 144 | return $errcode 145 | } 146 | 147 | stop_server() { 148 | # Force the process to die killing it manually 149 | [ ! -e "$PIDFILE" ] && return 150 | PIDS="$(cat "$PIDFILE")" 151 | for pid in $PIDS; do 152 | if running_pid $pid $DAEMON; then 153 | kill -15 $pid 154 | # Is it really dead? 155 | sleep "$QDIETIME"s 156 | if running_pid $pid $DAEMON; then 157 | kill -9 $pid 158 | sleep "$QDIETIME"s 159 | if running_pid $pid $DAEMON; then 160 | echo "Cannot kill $NAME (pid=$pid)!" 161 | exit 1 162 | fi 163 | fi 164 | fi 165 | done 166 | rm -f "$PIDFILE" 167 | if [ -n "$FCGI_SOCKET" ]; then 168 | rm -f "$FCGI_SOCKET" 169 | fi 170 | } 171 | 172 | case "$1" in 173 | start) 174 | log_daemon_msg "Starting $DESC" "$NAME" 175 | # Check if it's running first 176 | if running ; then 177 | log_progress_msg "apparently already running" 178 | log_end_msg 0 179 | exit 0 180 | fi 181 | if start_server ; then 182 | # NOTE: Some servers might die some time after they start, 183 | # this code will detect this issue if STARTTIME is set 184 | # to a reasonable value 185 | [ -n "$STARTTIME" ] && sleep $STARTTIME # Wait some time 186 | if running ; then 187 | # It's ok, the server started and is running 188 | log_end_msg 0 189 | else 190 | # It is not running after we did start 191 | log_end_msg 1 192 | fi 193 | else 194 | # Either we could not start it 195 | log_end_msg 1 196 | fi 197 | ;; 198 | stop|force-stop) 199 | log_daemon_msg "Stopping $DESC" "$NAME" 200 | if running ; then 201 | # Only stop the server if we see it running 202 | errcode=0 203 | stop_server || errcode=$? 204 | log_end_msg $errcode 205 | else 206 | # If it's not running don't do anything 207 | log_progress_msg "apparently not running" 208 | log_end_msg 0 209 | exit 0 210 | fi 211 | ;; 212 | restart|force-reload) 213 | log_daemon_msg "Restarting $DESC" "$NAME" 214 | errcode=0 215 | stop_server || errcode=$? 216 | # Wait some sensible amount, some server need this 217 | [ -n "$DIETIME" ] && sleep $DIETIME 218 | start_server || errcode=$? 219 | [ -n "$STARTTIME" ] && sleep $STARTTIME 220 | running || errcode=$? 221 | log_end_msg $errcode 222 | ;; 223 | status) 224 | 225 | log_daemon_msg "Checking status of $DESC" "$NAME" 226 | if running ; then 227 | log_progress_msg "running" 228 | log_end_msg 0 229 | else 230 | log_progress_msg "apparently not running" 231 | log_end_msg 1 232 | exit 1 233 | fi 234 | ;; 235 | # Use this if the daemon cannot reload 236 | reload) 237 | log_warning_msg "Reloading $NAME daemon: not implemented, as the daemon" 238 | log_warning_msg "cannot re-read the config file (use restart)." 239 | ;; 240 | *) 241 | N=/etc/init.d/$NAME 242 | echo "Usage: $N {start|stop|force-stop|restart|force-reload|status}" >&2 243 | exit 1 244 | ;; 245 | esac 246 | 247 | exit 0 -------------------------------------------------------------------------------- /nginx/fcgiwrap.conf: -------------------------------------------------------------------------------- 1 | # Include this file on your nginx.conf 2 | # fcgiwrap 3 | 4 | # For .php scripts 5 | location ~* \.php$ { 6 | gzip off; 7 | include /etc/nginx/fastcgi_params; 8 | fastcgi_split_path_info ^(.+\.php)(.*)$; 9 | fastcgi_pass unix:/dev/shm/fcgiwrap-php.socket; 10 | } 11 | 12 | 13 | -------------------------------------------------------------------------------- /nginx/sites-available/default: -------------------------------------------------------------------------------- 1 | server { 2 | listen 80; 3 | #listen 443 ssl default_server; 4 | server_name _; 5 | #ssl_certificate /etc/nginx/cert.pem; 6 | #ssl_certificate_key /etc/nginx/key.pem; 7 | gzip on; 8 | autoindex off; 9 | include /etc/nginx/fcgiwrap.conf; 10 | root /var/www/oneindex; 11 | location / { 12 | index index.php index.html; 13 | include /etc/nginx/fcgiwrap.conf; 14 | try_files $uri $uri/ /?$args; 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /one.php: -------------------------------------------------------------------------------- 1 | $localfile, 95 | // 'remotepath' => $remotepath, 96 | // 'filesize'=>onedrive::_filesize($localfile), 97 | // 'update_time'=>0 98 | // ); 99 | // $uploads = config('@upload'); 100 | // if(empty($uploads[$remotepath])){ 101 | // $uploads[$remotepath] = $task; 102 | // config('@upload', $uploads); 103 | // } 104 | // } 105 | // } 106 | // } 107 | //} 108 | 109 | 110 | static function upload_large_file($localfile, $remotepath){ 111 | fetch::init([CURLOPT_TIMEOUT=>200]); 112 | $upload = config('@upload'); 113 | $info = $upload[$remotepath]; 114 | if(empty($info['url'])){ 115 | print ' 创建上传会话'.PHP_EOL; 116 | $data = onedrive::create_upload_session($remotepath); 117 | if(!empty($data['uploadUrl'])){ 118 | $info['url'] = $data['uploadUrl']; 119 | $info['localfile'] = $localfile; 120 | $info['remotepath'] = $remotepath; 121 | $info['filesize'] = onedrive::_filesize($localfile); 122 | $info['offset'] = 0; 123 | $info['length'] = 327680; 124 | $info['update_time'] = time(); 125 | $upload[$remotepath] = $info; 126 | config('@upload', $upload); 127 | }elseif ( $data === false ){ 128 | print ' 文件已存在!'.PHP_EOL; 129 | return; 130 | } 131 | } 132 | 133 | if(empty($info['url'])){ 134 | print ' 获取会话失败!'.PHP_EOL; 135 | sleep(3); 136 | return self::upload_large_file($localfile, $remotepath); 137 | } 138 | 139 | print ' 上传分块'.onedrive::human_filesize($info['length']).' '; 140 | $begin_time = microtime(true); 141 | $data = onedrive::upload_session($info['url'], $info['localfile'], $info['offset'], $info['length']); 142 | 143 | if(!empty($data['nextExpectedRanges'])){ 144 | $upload_time = microtime(true) - $begin_time; 145 | $info['speed'] = $info['length']/$upload_time; 146 | 147 | print onedrive::human_filesize($info['speed']).'/s'.' '.round(($info['offset']/$info['filesize'])*100).'% '.PHP_EOL; 148 | $info['length'] = intval($info['length']/$upload_time/32768*2)*327680; 149 | $info['length'] = ($info['length']>104857600)?104857600:$info['length']; 150 | 151 | list($offset, $filesize) = explode('-',$data['nextExpectedRanges'][0]); 152 | $info['offset'] = $offset; 153 | $info['update_time'] = time(); 154 | $upload[$remotepath] = $info; 155 | config('@upload', $upload); 156 | }elseif(!empty($data['@content.downloadUrl']) || !empty($data['id'])){ 157 | unset($upload[$remotepath]); 158 | config('@upload', $upload); 159 | print ' 上传完成!'.PHP_EOL; 160 | return; 161 | }else{ 162 | print ' 失败!'.PHP_EOL; 163 | $data = onedrive::upload_session_status($info['url']); 164 | if(empty($data)|| $info['length']<100){ 165 | onedrive::delete_upload_session($info['url']); 166 | unset($upload[$remotepath]); 167 | config('@upload', $upload); 168 | }elseif(!empty($data['nextExpectedRanges'])){ 169 | list($offset, $filesize) = explode('-',$data['nextExpectedRanges'][0]); 170 | $info['offset'] = $offset; 171 | $info['length'] = $info['length']/1.5; 172 | $upload[$remotepath] = $info; 173 | config('@upload', $upload); 174 | } 175 | } 176 | 177 | return self::upload_large_file($localfile, $remotepath); 178 | 179 | } 180 | 181 | 182 | } 183 | 184 | 185 | array_shift($argv); 186 | $action = str_replace(':', '_',array_shift($argv)); 187 | 188 | if(is_callable(['one',$action])){ 189 | @call_user_func_array(['one',$action], $argv); 190 | exit(); 191 | } 192 | ?> 193 | oneindex commands : 194 | cache 195 | cache:clear clear cache 196 | cache:refresh refresh cache 197 | token 198 | token:refresh refresh token 199 | upload 200 | upload:file upload a file to onedrive 201 | upload:folder upload a folder to onedrive 202 | -------------------------------------------------------------------------------- /php.ini: -------------------------------------------------------------------------------- 1 | display_errors = Off -------------------------------------------------------------------------------- /view/admin/cache.php: -------------------------------------------------------------------------------- 1 | 2 | 21 | 22 |
23 | 24 |
25 |

页面缓存 清除所有页面缓存

26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 | 37 |
38 | 39 |
40 |
41 | 42 |
43 | 47 |
48 |


49 |
50 |


51 |
52 |
53 | 54 |
55 |

crontab定时刷新缓存 能极大提高系统访问性能

56 |

57 | 添加以下命令到crontab
58 | */10 * * * * 59 |

60 |
61 | 62 |
63 | 66 | -------------------------------------------------------------------------------- /view/admin/images.php: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 |
5 | 6 |
7 |

图床 OneImages

8 |
9 |
10 |
11 |

作为网站首页

12 | 13 | 17 |
18 |
19 |

允许游客上传图片

20 | 21 | 25 |
26 |
27 |

允许上传文件类型

28 | 29 |
30 |
31 | 34 |
35 |
36 |
37 | -------------------------------------------------------------------------------- /view/admin/install/install_0.php: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 |
6 |
7 |

系统安装 环境检测

8 |
9 | 10 |
11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 |
#环境需求当前环境
1PHP > 5.5
2curl 支持
3config/ 目录可读可写
4cache/ 目录可读可写
58 |
59 |

60 | 61 | 62 | 下一步 63 | 64 | 65 | 66 |
67 | 68 | -------------------------------------------------------------------------------- /view/admin/install/install_1.php: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 |
10 |

系统安装 设置应用ID和Secret

11 |
12 | 13 |
14 |

15 | 填入client_idclient_secret 16 |

17 |
18 | 19 |
20 |
21 | https 22 | 23 | 24 | 25 |
应用机密不能为空
26 |
27 |
28 |
29 | 30 | 31 | 32 | 33 |
应用 ID不能为空
34 |
35 |
36 | 37 |
38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 |
重定向URL不能为空
46 |
47 |
48 | 上一步 49 | 50 |
51 | 52 | 53 |
54 | 55 | -------------------------------------------------------------------------------- /view/admin/install/install_2.php: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 |
6 |
7 |

系统安装 绑定账号

8 |
9 | 10 | 11 | 上一步 12 | 绑定账号 13 | 14 | 15 | 16 | 17 |
18 | 19 | -------------------------------------------------------------------------------- /view/admin/install/install_3.php: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 |
6 |
7 |

系统安装 完成安装

8 |
9 | 10 | 11 |

程序安装成功!

12 | 初始密码: 13 |

14 | 管理后台 15 | 访问网站 16 | 17 |

程序安装失败!

18 |

19 | 重新绑定 20 | 21 | 22 | 23 | 24 |
25 | 26 | -------------------------------------------------------------------------------- /view/admin/install/layout.php: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | OneIndex <?php e($title);?> 7 | 8 | 9 | 65 | 66 | 67 |
68 |
69 | OneIndex 70 | $l):?> 71 | chevron_right 72 | 73 | 74 | 75 |
76 |
77 | 78 |
79 | 80 |
81 | 82 | -------------------------------------------------------------------------------- /view/admin/layout.php: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 系统管理 7 | 8 | 9 | 10 | 11 | 12 |
13 |
14 | menu 15 | 首页 16 |
17 | 登出 18 |
19 |
20 | 21 |
22 | 59 |
60 | 61 | 62 | 63 |
64 | 65 |
66 | 69 | 70 | 71 | -------------------------------------------------------------------------------- /view/admin/login.php: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 |
6 |
7 |

系统管理

8 |
9 |
10 | https 11 | 12 | 13 |
14 |
15 | 19 |
20 |
21 | 22 |
23 | 24 | -------------------------------------------------------------------------------- /view/admin/setpass.php: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 |
5 | 6 |
7 |

修改密码 后台密码修改(与文件夹密码无关)

8 |
9 |
10 |
11 |
12 |

旧密码

13 | 14 |
15 | 16 |
17 |

新密码

18 | 19 |
20 | 21 |
22 |

重复新密码

23 | 24 |
25 | 26 | 29 |
30 |
31 | -------------------------------------------------------------------------------- /view/admin/settings.php: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 |
5 | 6 |
7 |

基本设置 设置基本参数

8 |
9 |
10 |
11 |

网站名称

12 | 13 |
14 | 15 |
16 |

副标题

17 | 18 |
19 | 20 |
21 |

网站风格

22 | 35 |
36 | 37 |
38 |

项目数量

39 | 40 |
41 | 42 |
43 |

起始目录 (空为根目录)

44 | 45 |
46 | 47 | 48 |
49 |

需要隐藏的目录 (每行一项,通配识别,清空缓存后生效)

50 | 51 |
52 | 53 |
54 |

缓存类型

55 | 62 |
63 | 64 |
65 |

缓存过期时间(秒)

66 | 67 |
68 | 69 |
70 |

去掉/?/ (需配合伪静态使用!!)

71 | 72 | 76 |
77 | 78 | 79 | 80 | 81 | 84 |
85 |
86 | -------------------------------------------------------------------------------- /view/admin/show.php: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 |
5 | 6 |
7 |

文件展示设置 根据不同后缀进行展示。无设置后缀,直连下载

8 |
9 |
10 | $ext):?> 11 |
12 |

13 | 14 |
15 | 16 | 17 | 20 |
21 |
22 | -------------------------------------------------------------------------------- /view/admin/upload.php: -------------------------------------------------------------------------------- 1 | 2 | 3 |
4 | 5 |
6 |

上传管理 文件上传添加和管理

7 |
8 | 9 |
10 |
11 |

12 |
13 |
14 |
15 |
16 | 17 | 18 |
19 |
20 |
21 |
22 | 23 | 24 |
25 |
26 |
27 | 31 |
32 |
33 |
34 |
35 |
36 |
上传进度
37 |
38 | 39 |
40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | $task ):?> 53 | 54 | 55 | 56 | 57 | 58 | 61 | 64 | ($task['update_time']+60)):?> 65 | 66 | 69 | 70 | 71 | 74 | 75 | 76 | 77 | 78 | 79 |
远程路径上传速度进度状态操作
59 | 等待上传中 60 | 62 | 63 | 已暂停 67 | 68 | 上传中 72 | 73 |
80 |
81 | 82 |
83 |
84 |
85 |
已上传 86 | 87 | 88 | 89 | 90 | 91 |
92 |
93 |
94 | 95 |
96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | $status ):?> 105 | 106 | 107 | 108 | 109 | 110 |
远程路径状态
111 |
112 | 113 |
114 | 117 | -------------------------------------------------------------------------------- /view/nexmoe/404.php: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 |
404
5 | -------------------------------------------------------------------------------- /view/nexmoe/images/index.php: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 |
6 | 7 |
8 |
OneImages
9 | 10 |
11 | 12 | 13 | 14 |
15 |
16 |
17 | 18 |
19 |
20 | 21 |
22 | 23 |
24 | 25 |
26 | 27 | -------------------------------------------------------------------------------- /view/nexmoe/images/layout.php: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | <?php e($title.' - '.config('site_name'));?> 7 | 8 | 9 | 10 | 11 | 12 |
13 |
14 | 15 | $l):?> 16 | chevron_right 17 | 18 | 19 | 20 |
21 |
22 | 23 |
24 | 25 |
26 | 27 | -------------------------------------------------------------------------------- /view/nexmoe/layout.php: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | <?php e($title.' - '.config('site_name'));?> 7 | 8 | 11 | 12 | 13 | 14 | 15 |
16 |
17 |
18 | 19 | $l):?> 20 | chevron_right 21 | 22 | 23 | 24 |
25 |
26 | 27 |
28 | 29 | -------------------------------------------------------------------------------- /view/nexmoe/list.php: -------------------------------------------------------------------------------- 1 | 2 | 12 | 27 | 28 | 29 | 30 |
31 | 32 |
33 | 34 |
35 | 36 | 70 | 71 |
72 |
73 | 139 |
140 |
141 | 142 |
143 |
144 | face 145 | README.md 146 |
147 | 148 |
149 | 150 |
151 | 275 | format_list_bulleted 276 | 277 | -------------------------------------------------------------------------------- /view/nexmoe/password.php: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 |
6 |
7 |

输入密码进行查看

8 |
9 |
10 | https 11 | 12 | 13 |
14 |
15 | 19 |
20 |
21 | 22 |
23 | 24 | -------------------------------------------------------------------------------- /view/nexmoe/show/audio.php: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 |
5 |
6 | 7 | 9 | 10 |
11 | 12 |
13 | 17 | 18 |
19 | 31 |
32 |
33 | file_download 34 | -------------------------------------------------------------------------------- /view/nexmoe/show/code.php: -------------------------------------------------------------------------------- 1 | 2 | 22 | 23 | 28 |
29 |
30 | 31 |
32 | 33 |
34 | 37 | 38 |
39 | 51 | 52 |
53 |
54 | file_download 55 | 56 | 57 | 58 | 72 | -------------------------------------------------------------------------------- /view/nexmoe/show/doc.php: -------------------------------------------------------------------------------- 1 | 6 | -------------------------------------------------------------------------------- /view/nexmoe/show/image.php: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 |
7 |
8 | 9 |
10 |
11 | 16 | 17 |
18 | 30 |
31 |
32 | 33 | file_download 34 | 35 | -------------------------------------------------------------------------------- /view/nexmoe/show/pdf.php: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 13 | file_download 14 | 30 | -------------------------------------------------------------------------------- /view/nexmoe/show/stream.php: -------------------------------------------------------------------------------- 1 | 'application/octet-stream', 5 | 'ppt'=>'application/vnd.ms-powerpoint', 6 | 'xls'=>'application/vnd.ms-excel', 7 | 'doc'=>'application/msword', 8 | 'exe'=>'application/octet-stream', 9 | 'rar'=>'application/octet-stream', 10 | 'js'=>"javascrīpt/js", 11 | 'css'=>"text/css", 12 | 'hqx'=>"application/mac-binhex40", 13 | 'bin'=>"application/octet-stream", 14 | 'oda'=>"application/oda", 15 | 'pdf'=>"application/pdf", 16 | 'ai'=>"application/postsrcipt", 17 | 'eps'=>"application/postsrcipt", 18 | 'es'=>"application/postsrcipt", 19 | 'rtf'=>"application/rtf", 20 | 'mif'=>"application/x-mif", 21 | 'csh'=>"application/x-csh", 22 | 'dvi'=>"application/x-dvi", 23 | 'hdf'=>"application/x-hdf", 24 | 'nc'=>"application/x-netcdf", 25 | 'cdf'=>"application/x-netcdf", 26 | 'latex'=>"application/x-latex", 27 | 'ts'=>"application/x-troll-ts", 28 | 'src'=>"application/x-wais-source", 29 | 'zip'=>"application/zip", 30 | 'bcpio'=>"application/x-bcpio", 31 | 'cpio'=>"application/x-cpio", 32 | 'gtar'=>"application/x-gtar", 33 | 'shar'=>"application/x-shar", 34 | 'sv4cpio'=>"application/x-sv4cpio", 35 | 'sv4crc'=>"application/x-sv4crc", 36 | 'tar'=>"application/x-tar", 37 | 'ustar'=>"application/x-ustar", 38 | 'man'=>"application/x-troff-man", 39 | 'sh'=>"application/x-sh", 40 | 'tcl'=>"application/x-tcl", 41 | 'tex'=>"application/x-tex", 42 | 'texi'=>"application/x-texinfo", 43 | 'texinfo'=>"application/x-texinfo", 44 | 't'=>"application/x-troff", 45 | 'tr'=>"application/x-troff", 46 | 'roff'=>"application/x-troff", 47 | 'shar'=>"application/x-shar", 48 | 'me'=>"application/x-troll-me", 49 | 'ts'=>"application/x-troll-ts", 50 | 'gif'=>"image/gif", 51 | 'jpeg'=>"image/pjpeg", 52 | 'jpg'=>"image/pjpeg", 53 | 'jpe'=>"image/pjpeg", 54 | 'ras'=>"image/x-cmu-raster", 55 | 'pbm'=>"image/x-portable-bitmap", 56 | 'ppm'=>"image/x-portable-pixmap", 57 | 'xbm'=>"image/x-xbitmap", 58 | 'xwd'=>"image/x-xwindowdump", 59 | 'ief'=>"image/ief", 60 | 'tif'=>"image/tiff", 61 | 'tiff'=>"image/tiff", 62 | 'pnm'=>"image/x-portable-anymap", 63 | 'pgm'=>"image/x-portable-graymap", 64 | 'rgb'=>"image/x-rgb", 65 | 'xpm'=>"image/x-xpixmap", 66 | 'txt'=>"text/plain", 67 | 'c'=>"text/plain", 68 | 'cc'=>"text/plain", 69 | 'h'=>"text/plain", 70 | 'html'=>"text/html", 71 | 'htm'=>"text/html", 72 | 'htl'=>"text/html", 73 | 'txt'=>"text/html", 74 | 'php'=>"text/html", 75 | 'rtx'=>"text/richtext", 76 | 'etx'=>"text/x-setext", 77 | 'tsv'=>"text/tab-separated-values", 78 | 'mpeg'=>"video/mpeg", 79 | 'mpg'=>"video/mpeg", 80 | 'mpe'=>"video/mpeg", 81 | 'avi'=>"video/x-msvideo", 82 | 'qt'=>"video/quicktime", 83 | 'mov'=>"video/quicktime", 84 | 'moov'=>"video/quicktime", 85 | 'movie'=>"video/x-sgi-movie", 86 | 'au'=>"audio/basic", 87 | 'snd'=>"audio/basic", 88 | 'wav'=>"audio/x-wav", 89 | 'aif'=>"audio/x-aiff", 90 | 'aiff'=>"audio/x-aiff", 91 | 'aifc'=>"audio/x-aiff", 92 | 'swf'=>"application/x-shockwave-flash", 93 | 'myz'=>"application/myz" 94 | ]; 95 | //大于 5M 跳转为直连下载 96 | if($item['size'] > 5242880){ 97 | header('Location: '.$item['downloadUrl']);exit(); 98 | } 99 | $type = empty($types[$ext])?"application/octet-stream":$types[$ext]; 100 | $content = IndexController::get_content($item); 101 | 102 | header('Content-type: '.$type); 103 | echo $content; 104 | exit(); 105 | ?> -------------------------------------------------------------------------------- /view/nexmoe/show/video.php: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 7 | 8 |
9 |
10 |
11 | 12 | 13 |
14 | 18 | 19 |
20 | 32 |
33 |
34 | 45 | file_download 46 | -------------------------------------------------------------------------------- /view/nexmoe/show/video2.php: -------------------------------------------------------------------------------- 1 | 2 | 3 | 11 | 12 | 13 | 14 | 15 | 16 |
17 |
18 |
19 | 20 |
21 | 25 | 26 |
27 | 39 |
40 |
41 | 52 | file_download 53 | -------------------------------------------------------------------------------- /view/nexmoe/show/video5.php: -------------------------------------------------------------------------------- 1 | 2 | 3 | 6 | 7 | 8 |
9 |
10 | 13 | 14 |
15 | 19 | 20 |
21 | 33 |
34 |
35 | file_download 36 | --------------------------------------------------------------------------------