24 | 25 | 26 |
├── .dockerignore
├── .htaccess
├── Dockerfile
├── README.md
├── cache
└── README.md
├── config
└── README.md
├── controller
├── AdminController.php
├── ImagesController.php
├── IndexController.php
└── UploadController.php
├── default.conf
├── index.php
├── init.php
├── lib
├── Parsedown.php
├── cache.php
├── cache
│ ├── filecache_.php
│ ├── memcache_.php
│ └── secache_.php
├── fetch.php
├── onedrive.php
├── oneindex.php
├── route.php
├── sqlite.php
└── view.php
├── 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
├── classic
├── 404.php
├── layout.php
└── list.php
├── material
├── 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
└── 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
/.dockerignore:
--------------------------------------------------------------------------------
1 | .git
2 | Dockerfile
--------------------------------------------------------------------------------
/.htaccess:
--------------------------------------------------------------------------------
1 | RewriteEngine On
2 |
3 | RewriteCond %{REQUEST_FILENAME} !-f
4 | RewriteCond %{REQUEST_FILENAME} !-d
5 |
6 | RewriteRule ^(.*) index.php?/$1 [L]
7 |
--------------------------------------------------------------------------------
/Dockerfile:
--------------------------------------------------------------------------------
1 | FROM php:fpm-alpine
2 | WORKDIR /var/www/html
3 | COPY / /var/www/html/
4 | RUN apk add --no-cache nginx \
5 | && mkdir /run/nginx \
6 | && chown -R www-data:www-data cache/ config/ \
7 | && mv default.conf /etc/nginx/conf.d \
8 | && mv php.ini /usr/local/etc/php
9 |
10 | EXPOSE 80
11 | # Persistent config file and cache
12 | VOLUME [ "/var/www/html/config", "/var/www/html/cache" ]
13 |
14 | CMD php-fpm & \
15 | nginx -g "daemon off;"
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # oneindex修改版增加功能
2 | 1.看图模式
3 |
4 | 2.目录分页
5 |
6 | ## 仅修改了nexmoe主题,使用时请更换
7 |
8 | # oneindex
9 | Onedrive Directory Index
10 |
11 | ## 功能:
12 | 不用服务器空间,不走服务器流量,
13 |
14 | 直接列onedrive目录,文件直链下载。
15 |
16 | ## demo
17 | [https://xn.tn](https://xn.tn)
18 |
19 | ## change log:
20 | 18-03-29: 更新直链获取机制、缓存机制,避免频繁访问的token失效
21 | 18-03-29: 解决非英文编码问题
22 | 18-03-29: 添加onedrive共享的起始目录 功能
23 | 18-03-29: 添加rewrite的配置文件
24 | 18-03-29: 增加sqlite模式cache支持
25 | 18-03-29: 添加缩略图功能
26 | 18-03-29: 添加404判断
27 | 18-03-31: 添加console
28 | 18-04-13: 修复特殊文件名无法下载问题
29 | 18-04-13: 添加命令行上传功能
30 | 18-04-16: 更新 2.0 beta
31 | 18-04-16: 更新展示界面
32 | 18-04-16: 响应式,支持小屏设备
33 | 18-04-16: 图片在线预览
34 | 18-04-16: 视频在线播放
35 | 18-04-16: 代码在线查看(js、css、html、sh、php、java、md等)
36 | 18-04-16: README.md 支持,解析各目录下(onedirive目录下) README.md 文件,在页面尾部展示。
37 | 18-04-18: 音频在线播放
38 | 18-04-18: HEAD.md 支持,在页面头部展示
39 | 18-04-18: .password 文件夹加密
40 | 18-05-06: 在线视频播放器替换成 Dplayer
41 | 18-05-06: 在线视频播放支持'mp4','webm','avi','mpg', 'mpeg', 'rm', 'rmvb', 'mov', 'wmv', 'mkv', 'asf'
42 | 18-06-01: 支持个人账号
43 | 18-06-01: cli文件夹上传(单线程)
44 | 18-06-01: 管理后台(后台地址:?/admin 默认密码:oneindex)
45 | 18-06-01: 不同后缀展示设置
46 | 18-06-01: 文件直接输出
47 | 18-06-01: 文件上传管理(后台)
48 | 18-06-01: 增加index.html特性
49 | 18-06-01: 图床功能
50 |
51 | ## 需求:
52 | 1、PHP空间,PHP 5.6+ 打开curl支持
53 | 2、onedrive 账号 (个人、企业版或教育版/工作或学校帐户)
54 | 3、oneindex 程序
55 |
56 | ## 安装:
57 |
58 |
59 |
60 | ## docker 安装运行:
61 |
62 | 从docker仓库获取镜像:
63 | ```sh
64 | docker pull yinaoxiong/oneindex
65 | ```
66 |
67 | 或者从源码构建镜像:
68 |
69 | ```shell
70 | git clone https://github.com/donwa/oneindex.git
71 | cd oneindex/
72 | docker build -t your-image-name .
73 | ```
74 |
75 | 运行:
76 |
77 | ```shell
78 | docker run -d -p {open port}:80 --name {container name} --restart=always {image name}
79 | ```
80 |
81 | 停止删除容器:
82 |
83 | ```shell
84 | docker stop {container name}
85 | docker rm -v {container name}
86 | ```
87 |
88 |
89 |
90 | ## 计划任务
91 | [可选]**推荐配置**,非必需。后台定时刷新缓存,可增加前台访问的速度
92 | ```
93 | # 每小时刷新一次token
94 | 0 * * * * /具体路径/php /程序具体路径/one.php token:refresh
95 |
96 | # 每十分钟后台刷新一遍缓存
97 | */10 * * * * /具体路径/php /程序具体路径/one.php cache:refresh
98 | ```
99 |
100 | ## 特殊文件实现功能
101 | ` README.md `、`HEAD.md` 、 `.password`特殊文件使用
102 |
103 | 可以参考[https://github.com/donwa/oneindex/tree/files](https://github.com/donwa/oneindex/tree/files)
104 |
105 | **在文件夹底部添加说明:**
106 | >在onedrive的文件夹中添加` README.md `文件,使用markdown语法。
107 |
108 | **在文件夹头部添加说明:**
109 | >在onedrive的文件夹中添加`HEAD.md` 文件,使用markdown语法。
110 |
111 | **加密文件夹:**
112 | >在onedrive的文件夹中添加`.password`文件,填入密码,密码不能为空。
113 |
114 | **直接输出网页:**
115 | >在onedrive的文件夹中添加`index.html` 文件,程序会直接输出网页而不列目录。
116 | >配合 文件展示设置-直接输出 效果更佳
117 |
118 | ## 命令行功能
119 | 仅能在php cli模式下运行
120 | **清除缓存:**
121 | ```
122 | php one.php cache:clear
123 | ```
124 | **刷新缓存:**
125 | ```
126 | php one.php cache:refresh
127 | ```
128 | **刷新令牌:**
129 | ```
130 | php one.php token:refresh
131 | ```
132 | **上传文件:**
133 | ```
134 | php one.php upload:file 本地文件 [onedrive文件]
135 | ```
136 |
137 |
138 | **上传文件夹:**
139 | ```
140 | php one.php upload:folder 本地文件夹 [onedrive文件夹]
141 | ```
142 |
143 | 例如:
144 | ```
145 | //上传demo.zip 到onedrive 根目录
146 | php one.php upload:file demo.zip
147 |
148 | //上传demo.zip 到onedrive /test/目录
149 | php one.php upload:file demo.zip /test/
150 |
151 | //上传demo.zip 到onedrive /test/目录并命名为 d.zip
152 | php one.php upload:file demo.zip /test/d.zip
153 |
154 | //上传up/ 到onedrive /test/
155 | php one.php upload:file up/ /test/
156 | ```
157 |
--------------------------------------------------------------------------------
/cache/README.md:
--------------------------------------------------------------------------------
1 | # oneindex
2 | Onedrive Directory Index
3 |
--------------------------------------------------------------------------------
/config/README.md:
--------------------------------------------------------------------------------
1 | # oneindex
2 | Onedrive Directory Index
3 |
--------------------------------------------------------------------------------
/controller/AdminController.php:
--------------------------------------------------------------------------------
1 | 'OneIndex',
6 | 'password' => 'oneindex',
7 | 'style'=>'material',
8 | 'onedrive_root' =>'',
9 | 'cache_type'=>'secache',
10 | 'cache_expire_time' => 3600,
11 | 'cache_refresh_time' => 600,
12 | 'root_path' => '?',
13 | 'show'=> array (
14 | 'stream'=>['txt'],
15 | 'image' => ['bmp','jpg','jpeg','png','gif'],
16 | 'video5'=>['mp4','webm','mkv'],
17 | 'video'=>[],
18 | 'video2'=>['avi','mpg', 'mpeg', 'rm', 'rmvb', 'mov', 'wmv', 'asf', 'ts', 'flv'],
19 | 'audio'=>['ogg','mp3','wav'],
20 | 'code'=>['html','htm','php', 'css', 'go','java','js','json','txt','sh','md'],
21 | 'doc'=>['csv','doc','docx','odp','ods','odt','pot','potm','potx','pps','ppsx','ppsxm','ppt','pptm','pptx','rtf','xls','xlsx']
22 | ),
23 | 'images'=>['home'=>false,'public'=>false, 'exts'=>['jpg','png','gif','bmp']]
24 | );
25 |
26 | function __construct(){
27 | }
28 |
29 | function login(){
30 | if(!empty($_POST['password']) && $_POST['password'] == config('password')){
31 | setcookie('admin', md5(config('password').config('refresh_token')) );
32 | return view::direct(get_absolute_path(dirname($_SERVER['SCRIPT_NAME'])).'?/admin/');
33 | }
34 | return view::load('login')->with('title', '系统管理');
35 | }
36 |
37 | function logout(){
38 | setcookie('admin', '' );
39 | return view::direct(get_absolute_path(dirname($_SERVER['SCRIPT_NAME'])).'?/login');
40 | }
41 |
42 | function settings(){
43 |
44 | if($_POST){
45 |
46 | config('site_name',$_POST['site_name']);
47 | config('style',$_POST['style']);
48 |
49 | config('onedrive_root',get_absolute_path($_POST['onedrive_root']));
50 |
51 | config('cache_type',$_POST['cache_type']);
52 | config('cache_expire_time',intval($_POST['cache_expire_time']));
53 |
54 | $_POST['root_path'] = empty($_POST['root_path'])?'?':'';
55 | config('root_path',$_POST['root_path']);
56 | }
57 | $config = config('@base');
58 | return view::load('settings')->with('config', $config);
59 | }
60 |
61 | function cache(){
62 | if(!is_null($_POST['clear'])){
63 | cache::clear();
64 | $message = "清除缓存成功";
65 | }elseif ( !is_null($_POST['refresh']) ){
66 | oneindex::refresh_cache(get_absolute_path(config('onedrive_root')));
67 | $message = "重建缓存成功";
68 | }
69 | return view::load('cache')->with('message', $message);
70 | }
71 |
72 | function images(){
73 | if($_POST){
74 | $config['home'] = empty($_POST['home'])?false:true;
75 | $config['public'] = empty($_POST['public'])?false:true;
76 | $config['exts'] = explode(" ", $_POST['exts']);
77 | config('images@base',$config);
78 | }
79 | $config = config('images@base');
80 | return view::load('images')->with('config', $config);;
81 | }
82 |
83 |
84 | function show(){
85 | if(!empty($_POST) ){
86 | foreach($_POST as $n=>$ext){
87 | $show[$n] = explode(' ', $ext);
88 | }
89 | config('show', $show);
90 | }
91 | $names = [
92 | 'stream'=>'直接输出(<5M),走本服务器流量(stream)',
93 | 'image' =>'图片(image)',
94 | 'video'=>'Dplayer 视频(video)',
95 | 'video2'=>'Dplayer DASH 视频(video2)/个人版账户不支持',
96 | 'video5'=>'html5视频(video5)',
97 | 'audio'=>'音频播放(audio)',
98 | 'code'=>'文本/代码(code)',
99 | 'doc'=>'文档(doc)'
100 | ];
101 | $show = config('show');
102 | return view::load('show')->with('names', $names)->with('show', $show);
103 | }
104 |
105 | function setpass(){
106 | if($_SERVER['REQUEST_METHOD'] == 'POST'){
107 | if($_POST['old_pass'] == config('password')){
108 | if($_POST['password'] == $_POST['password2']){
109 | config('password', $_POST['password']);
110 | $message = "修改成功";
111 | }else{
112 | $message = "两次密码不一致,修改失败";
113 | }
114 | }else{
115 | $message = "原密码错误,修改失败";
116 | }
117 | }
118 | return view::load('setpass')->with('message', $message);
119 | }
120 |
121 | function install(){
122 | if(!empty($_GET['code'])){
123 | return $this->install_3();
124 | }
125 | switch ( intval($_GET['step']) ){
126 | case 1:
127 | return $this->install_1();
128 | case 2:
129 | return $this->install_2();
130 | default:
131 | return $this->install_0();
132 | }
133 | }
134 |
135 | function install_0(){
136 | $check['php'] = version_compare(PHP_VERSION,'5.5.0','ge');
137 | $check['curl'] = function_exists('curl_init');
138 | $check['config'] = is_writable(ROOT.'config/');
139 | $check['cache'] = is_writable(ROOT.'cache/');
140 |
141 | return view::load('install/install_0')->with('title','系统安装')
142 | ->with('check', $check);
143 | }
144 |
145 | function install_1(){
146 | if(!empty($_POST['client_secret']) && !empty($_POST['client_id']) && !empty($_POST['redirect_uri']) ){
147 | config('@base', self::$default_config);
148 | config('client_secret',$_POST['client_secret']);
149 | config('client_id',$_POST['client_id']);
150 | config('redirect_uri',$_POST['redirect_uri']);
151 | return view::direct('?step=2');
152 | }
153 | if($_SERVER['HTTP_HOST'] == 'localhost'){
154 | $redirect_uri = 'http://'.$_SERVER['HTTP_HOST'].get_absolute_path(dirname($_SERVER['PHP_SELF']));
155 | }else{
156 | // 非https,调用ju.tn中转
157 | $redirect_uri = 'https://ju.tn/';
158 | }
159 |
160 | $ru = "https://developer.microsoft.com/en-us/graph/quick-start?appID=_appId_&appName=_appName_&redirectUrl={$redirect_uri}&platform=option-php";
161 | $deepLink = "/quickstart/graphIO?publicClientSupport=false&appName=oneindex&redirectUrl={$redirect_uri}&allowImplicitFlow=false&ru=".urlencode($ru);
162 | $app_url = "https://apps.dev.microsoft.com/?deepLink=".urlencode($deepLink);
163 | return view::load('install/install_1')->with('title','系统安装')
164 | ->with('redirect_uri', $redirect_uri)
165 | ->with('app_url', $app_url);
166 | }
167 |
168 | function install_2(){
169 | return view::load('install/install_2')->with('title','系统安装');
170 | }
171 |
172 | function install_3(){
173 | $data = onedrive::authorize($_GET['code']);
174 | if(!empty($data['refresh_token'])){
175 | config('refresh_token',$data['refresh_token']);
176 | config('@token', $data);
177 | }
178 | return view::load('install/install_3')->with('refresh_token',$data['refresh_token']);
179 |
180 | }
181 | }
182 |
--------------------------------------------------------------------------------
/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 = 50;
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 | $this->url_path = preg_replace("(\.page\-[0-9]*/$)","",get_absolute_path(join('/', $paths)));
26 |
27 | $this->path = get_absolute_path(config('onedrive_root').$this->url_path);
28 | //获取文件夹下所有元素
29 | $this->items = $this->items($this->path);
30 | }
31 |
32 |
33 | function index(){
34 | //是否404
35 | $this->is404();
36 |
37 | $this->is_password();
38 |
39 | header("Expires:-1");
40 | header("Cache-Control:no_cache");
41 | header("Pragma:no-cache");
42 |
43 | $fName = $this->name;
44 |
45 | if(!empty($fName)){//file
46 | return $this->file();
47 | }else{//dir
48 | return $this->dir();
49 | }
50 | }
51 |
52 | //判断是否加密
53 | function is_password(){
54 | if(empty($this->items['.password'])){
55 | return false;
56 | }else{
57 | $this->items['.password']['path'] = get_absolute_path($this->path).'.password';
58 | }
59 |
60 | $password = $this->get_content($this->items['.password']);
61 | list($password) = explode("\n",$password);
62 | $password = trim($password);
63 | unset($this->items['.password']);
64 | if(!empty($password) && $password == $_COOKIE[md5($this->path)]){
65 | return true;
66 | }
67 |
68 | $this->password($password);
69 |
70 | }
71 |
72 | function password($password){
73 | if(!empty($_POST['password']) && $password == $_POST['password']){
74 | setcookie(md5($this->path), $_POST['password']);
75 | return true;
76 | }
77 | $navs = $this->navs();
78 | echo view::load('password')->with('navs',$navs);
79 | exit();
80 | }
81 |
82 | //文件
83 | function file(){
84 | $item = $this->items[$this->name];
85 | if ($item['folder']) {//是文件夹
86 | $url = $_SERVER['REQUEST_URI'].'/';
87 | }elseif(!is_null($_GET['t']) ){//缩略图
88 | $url = $this->thumbnail($item);
89 | }elseif($_SERVER['REQUEST_METHOD'] == 'POST' || !is_null($_GET['s']) ){
90 | return $this->show($item);
91 | }else{//返回下载链接
92 | $url = $item['downloadUrl'];
93 | }
94 | header('Location: '.$url);
95 | }
96 |
97 |
98 |
99 | //文件夹
100 | function dir(){
101 | $root = get_absolute_path(dirname($_SERVER['SCRIPT_NAME'])).config('root_path');
102 | $navs = $this->navs();
103 |
104 | if($this->items['index.html']){
105 | $this->items['index.html']['path'] = get_absolute_path($this->path).'index.html';
106 | $index = $this->get_content($this->items['index.html']);
107 | header('Content-type: text/html');
108 | echo $index;
109 | exit();
110 | }
111 |
112 | if($this->items['README.md']){
113 | $this->items['README.md']['path'] = get_absolute_path($this->path).'README.md';
114 | $readme = $this->get_content($this->items['README.md']);
115 | $Parsedown = new Parsedown();
116 | $readme = $Parsedown->text($readme);
117 | //不在列表中展示
118 | unset($this->items['README.md']);
119 | }
120 |
121 | if($this->items['HEAD.md']){
122 | $this->items['HEAD.md']['path'] = get_absolute_path($this->path).'HEAD.md';
123 | $head = $this->get_content($this->items['HEAD.md']);
124 | $Parsedown = new Parsedown();
125 | $head = $Parsedown->text($head);
126 | //不在列表中展示
127 | unset($this->items['HEAD.md']);
128 | }
129 |
130 | $this->zongye = ceil(count($this->items) / $this->z_page);
131 |
132 | if($this->page*$this->z_page >= count($this->items))
133 | $this->page = $this->zongye;
134 |
135 |
136 | return view::load('list')->with('title', 'index of '. urldecode($this->url_path))
137 | ->with('navs', $navs)
138 | ->with('path',join("/", array_map("rawurlencode", explode("/", $this->url_path))) )
139 | ->with('fullpath',$this->url_path)
140 | ->with('root', $root)
141 | ->with('items', array_slice($this->items,$this->z_page*($this->page-1),$this->z_page))
142 | ->with('head',$head)
143 | ->with('readme',$readme)
144 | ->with('page',$this->page)
145 | ->with('zongye',$this->zongye);
146 | }
147 |
148 | function show($item){
149 | $root = get_absolute_path(dirname($_SERVER['SCRIPT_NAME'])).(config('root_path')?'?/':'');
150 | $ext = strtolower(pathinfo($item['name'], PATHINFO_EXTENSION));
151 | $data['title'] = $item['name'];
152 | $data['navs'] = $this->navs();
153 | $data['item'] = $item;
154 | $data['ext'] = $ext;
155 | $data['item']['path'] = get_absolute_path($this->path).$this->name;
156 | $http_type = ((isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') || (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https')) ? 'https://' : 'http://';
157 | $uri = onedrive::urlencode(get_absolute_path($this->url_path.'/'.$this->name));
158 | $data['url'] = $http_type.$_SERVER['HTTP_HOST'].$root.$uri;
159 |
160 |
161 | $show = config('show');
162 | foreach($show as $n=>$exts){
163 | if(in_array($ext,$exts)){
164 | return view::load('show/'.$n)->with($data);
165 | }
166 | }
167 |
168 | header('Location: '.$item['downloadUrl']);
169 | }
170 | //缩略图
171 | function thumbnail($item){
172 | if(!empty($_GET['t'])){
173 | list($width, $height) = explode('|', $_GET['t']);
174 | }else{
175 | //800 176 96
176 | $width = $height = 800;
177 | }
178 | $item['thumb'] = onedrive::thumbnail($this->path.$this->name);
179 | list($item['thumb'],$tmp) = explode('&width=', $item['thumb']);
180 | $item['thumb'] .= strpos($item['thumb'], '?')?'&':'?';
181 | return $item['thumb']."width={$width}&height={$height}";
182 | }
183 |
184 | //文件夹下元素
185 | function items($path, $fetch=false){
186 | $items = cache::get('dir_'.$this->path, function(){
187 | return onedrive::dir($this->path);
188 | }, config('cache_expire_time'));
189 | return $items;
190 | }
191 |
192 | function navs(){
193 | $root = get_absolute_path(dirname($_SERVER['SCRIPT_NAME'])).config('root_path');
194 | $navs['/'] = get_absolute_path($root.'/');
195 | foreach(explode('/',$this->url_path) as $v){
196 | if(empty($v)){
197 | continue;
198 | }
199 | $navs[rawurldecode($v)] = end($navs).$v.'/';
200 | }
201 | if(!empty($this->name)){
202 | $navs[$this->name] = end($navs).urlencode($this->name);
203 | }
204 |
205 | return $navs;
206 | }
207 |
208 | static function get_content($item){
209 | $content = cache::get('content_'.$item['path'], function() use ($item){
210 | $resp = fetch::get($item['downloadUrl']);
211 | if($resp->http_code == 200){
212 | return $resp->content;
213 | }
214 | }, config('cache_expire_time') );
215 | return $content;
216 | }
217 |
218 | //时候404
219 | function is404(){
220 | if(!empty($this->items[$this->name]) || (empty($this->name) && is_array($this->items)) ){
221 | return false;
222 | }
223 |
224 | http_response_code(404);
225 | view::load('404')->show();
226 | die();
227 | }
228 |
229 | function __destruct(){
230 | if (!function_exists("fastcgi_finish_request")) {
231 | return;
232 | }
233 | }
234 | }
235 |
--------------------------------------------------------------------------------
/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 |
--------------------------------------------------------------------------------
/default.conf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/hang666/oneindex-h/41dcb792522ae275d11b0d2f678fffd7878f0d28/default.conf
--------------------------------------------------------------------------------
/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('/^\[(.+?)\]:[ ]*(\S+?)>?(?:[ ]+["\'(](.+)["\')])?[ ]*$/', $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 .= ''.$Element['name'].'>';
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 | } -------------------------------------------------------------------------------- /lib/cache/secache_.php: -------------------------------------------------------------------------------- 1 | cachefile = $cachefile; 18 | $this->c = new secache(); 19 | $this->c->workat($this->cachefile); 20 | } 21 | function get($key){ 22 | $this->c->fetch(md5($key),$data); 23 | if( is_array($data) && $data['expire'] > time() && !is_null($data['data']) ){ 24 | return $data['data']; 25 | }else{ 26 | return null; 27 | } 28 | } 29 | function set($key, $value=null, $expire=99999999){ 30 | $data['expire'] = time() + $expire; 31 | $data['data'] = $value; 32 | return $this->c->store(md5($key),$data); 33 | 34 | } 35 | function clear(){ 36 | return $this->c->clear(); 37 | } 38 | } 39 | class secache{ 40 | var $idx_node_size = 40; 41 | var $idx_node_base = 0; 42 | var $data_base_pos = 262588; //40+20+24*16+16*16*16*16*4; 43 | var $schema_item_size = 24; 44 | var $header_padding = 20; //保留空间 放置php标记防止下载 45 | var $info_size = 20; //保留空间 4+16 maxsize|ver 46 | //40起 添加20字节保留区域 47 | var $idx_seq_pos = 40; //id 计数器节点地址 48 | var $dfile_cur_pos = 44; //id 计数器节点地址 49 | var $idx_free_pos = 48; //id 空闲链表入口地址 50 | var $idx_base_pos = 444; //40+20+24*16 51 | var $min_size = 10240; //10M最小值 52 | var $schema_struct = array('size','free','lru_head','lru_tail','hits','miss'); 53 | var $ver = '$Rev$'; 54 | var $name = '系统默认缓存(文件型)'; 55 | function workat($file){ 56 | $this->_file = $file.'.php'; 57 | $this->_bsize_list = array( 58 | 512=>10, 59 | 3<<10=>10, 60 | 8<<10=>10, 61 | 20<<10=>4, 62 | 30<<10=>2, 63 | 50<<10=>2, 64 | 80<<10=>2, 65 | 96<<10=>2, 66 | 128<<10=>2, 67 | 224<<10=>2, 68 | 256<<10=>2, 69 | 512<<10=>1, 70 | 1024<<10=>1, 71 | ); 72 | $this->_node_struct = array( 73 | 'next'=>array(0,'V'), 74 | 'prev'=>array(4,'V'), 75 | 'data'=>array(8,'V'), 76 | 'size'=>array(12,'V'), 77 | 'lru_right'=>array(16,'V'), 78 | 'lru_left'=>array(20,'V'), 79 | 'key'=>array(24,'H*'), 80 | ); 81 | if(!file_exists($this->_file)){ 82 | $this->create(); 83 | }else{ 84 | $this->_rs = fopen($this->_file,'rb+') or $this->trigger_error('Can\'t open the cachefile: '.realpath($this->_file),E_USER_ERROR); 85 | $this->_seek($this->header_padding); 86 | $info = unpack('V1max_size/a*ver',fread($this->_rs,$this->info_size)); 87 | $info['ver'] = trim($info['ver']); 88 | if($info['ver']!=$this->ver){ 89 | $this->_format(true); 90 | }else{ 91 | $this->max_size = $info['max_size']; 92 | } 93 | } 94 | $this->idx_node_base = $this->data_base_pos+$this->max_size; 95 | $this->_block_size_list = array_keys($this->_bsize_list); 96 | sort($this->_block_size_list); 97 | return true; 98 | } 99 | function create(){ 100 | $this->_rs = fopen($this->_file,'wb+') or $this->trigger_error('Can\'t open the cachefile: '.realpath($this->_file),E_USER_ERROR);; 101 | fseek($this->_rs,0); 102 | fputs($this->_rs,'<'.'?php exit()?'.'>'); 103 | return $this->_format(); 104 | } 105 | function _puts($offset,$data){ 106 | if($offset < $this->max_size*1.5){ 107 | $this->_seek($offset); 108 | return fputs($this->_rs,$data); 109 | }else{ 110 | $this->trigger_error('Offset over quota:'.$offset,E_USER_ERROR); 111 | } 112 | } 113 | function _seek($offset){ 114 | return fseek($this->_rs,$offset); 115 | } 116 | function clear(){ 117 | return $this->_format(true); 118 | } 119 | function fetch($key,&$return){ 120 | if($this->lock(false)){ 121 | $locked = true; 122 | } 123 | if($this->search($key,$offset)){ 124 | $info = $this->_get_node($offset); 125 | $schema_id = $this->_get_size_schema_id($info['size']); 126 | if($schema_id===false){ 127 | if($locked) $this->unlock(); 128 | return false; 129 | } 130 | $this->_seek($info['data']); 131 | $data = fread($this->_rs,$info['size']); 132 | $return = unserialize($data); 133 | if($return===false){ 134 | if($locked) $this->unlock(); 135 | return false; 136 | } 137 | if($locked){ 138 | $this->_lru_push($schema_id,$info['offset']); 139 | $this->_set_schema($schema_id,'hits',$this->_get_schema($schema_id,'hits')+1); 140 | return $this->unlock(); 141 | }else{ 142 | return true; 143 | } 144 | }else{ 145 | if($locked) $this->unlock(); 146 | return false; 147 | } 148 | } 149 | /** 150 | * lock 151 | * 如果flock不管用,请继承本类,并重载此方法 152 | * 153 | * @param mixed $is_block 是否阻塞 154 | * @access public 155 | * @return void 156 | */ 157 | function lock($is_block,$whatever=false){ 158 | ignore_user_abort(1); 159 | return flock($this->_rs, $is_block?LOCK_EX:LOCK_EX+LOCK_NB); 160 | } 161 | /** 162 | * unlock 163 | * 如果flock不管用,请继承本类,并重载此方法 164 | * 165 | * @access public 166 | * @return void 167 | */ 168 | function unlock(){ 169 | ignore_user_abort(0); 170 | return flock($this->_rs, LOCK_UN); 171 | } 172 | function delete($key,$pos=false){ 173 | if($pos || $this->search($key,$pos)){ 174 | if($info = $this->_get_node($pos)){ 175 | //删除data区域 176 | if($info['prev']){ 177 | $this->_set_node($info['prev'],'next',$info['next']); 178 | $this->_set_node($info['next'],'prev',$info['prev']); 179 | }else{ //改入口位置 180 | $this->_set_node($info['next'],'prev',0); 181 | $this->_set_node_root($key,$info['next']); 182 | } 183 | $this->_free_dspace($info['size'],$info['data']); 184 | $this->_lru_delete($info); 185 | $this->_free_node($pos); 186 | return $info['prev']; 187 | } 188 | } 189 | return false; 190 | } 191 | function store($key,$value){ 192 | if($this->lock(true)){ 193 | //save data 194 | $data = serialize($value); 195 | $size = strlen($data); 196 | //get list_idx 197 | $has_key = $this->search($key,$list_idx_offset); 198 | $schema_id = $this->_get_size_schema_id($size); 199 | if($schema_id===false){ 200 | $this->unlock(); 201 | return false; 202 | } 203 | if($has_key){ 204 | $hdseq = $list_idx_offset; 205 | $info = $this->_get_node($hdseq); 206 | if($schema_id == $this->_get_size_schema_id($info['size'])){ 207 | $dataoffset = $info['data']; 208 | }else{ 209 | //破掉原有lru 210 | $this->_lru_delete($info); 211 | if(!($dataoffset = $this->_dalloc($schema_id))){ 212 | $this->unlock(); 213 | return false; 214 | } 215 | $this->_free_dspace($info['size'],$info['data']); 216 | $this->_set_node($hdseq,'lru_left',0); 217 | $this->_set_node($hdseq,'lru_right',0); 218 | } 219 | $this->_set_node($hdseq,'size',$size); 220 | $this->_set_node($hdseq,'data',$dataoffset); 221 | }else{ 222 | if(!($dataoffset = $this->_dalloc($schema_id))){ 223 | $this->unlock(); 224 | return false; 225 | } 226 | $hdseq = $this->_alloc_idx(array( 227 | 'next'=>0, 228 | 'prev'=>$list_idx_offset, 229 | 'data'=>$dataoffset, 230 | 'size'=>$size, 231 | 'lru_right'=>0, 232 | 'lru_left'=>0, 233 | 'key'=>$key, 234 | )); 235 | if($list_idx_offset>0){ 236 | $this->_set_node($list_idx_offset,'next',$hdseq); 237 | }else{ 238 | $this->_set_node_root($key,$hdseq); 239 | } 240 | } 241 | if($dataoffset>$this->max_size){ 242 | $this->trigger_error('alloc datasize:'.$dataoffset,E_USER_WARNING); 243 | return false; 244 | } 245 | $this->_puts($dataoffset,$data); 246 | $this->_set_schema($schema_id,'miss',$this->_get_schema($schema_id,'miss')+1); 247 | $this->_lru_push($schema_id,$hdseq); 248 | $this->unlock(); 249 | return true; 250 | }else{ 251 | $this->trigger_error("Couldn't lock the file !",E_USER_WARNING); 252 | return false; 253 | } 254 | } 255 | /** 256 | * search 257 | * 查找指定的key 258 | * 如果找到节点则$pos=节点本身 返回true 259 | * 否则 $pos=树的末端 返回false 260 | * 261 | * @param mixed $key 262 | * @access public 263 | * @return void 264 | */ 265 | function search($key,&$pos){ 266 | return $this->_get_pos_by_key($this->_get_node_root($key),$key,$pos); 267 | } 268 | function _get_size_schema_id($size){ 269 | foreach($this->_block_size_list as $k=>$block_size){ 270 | if($size <= $block_size){ 271 | return $k; 272 | } 273 | } 274 | return false; 275 | } 276 | function _parse_str_size($str_size,$default){ 277 | if(preg_match('/^([0-9]+)\s*([gmk]|)$/i',$str_size,$match)){ 278 | switch(strtolower($match[2])){ 279 | case 'g': 280 | if($match[1]>1){ 281 | $this->trigger_error('Max cache size 1G',E_USER_ERROR); 282 | } 283 | $size = $match[1]<<30; 284 | break; 285 | case 'm': 286 | $size = $match[1]<<20; 287 | break; 288 | case 'k': 289 | $size = $match[1]<<10; 290 | break; 291 | default: 292 | $size = $match[1]; 293 | } 294 | if($size<=0){ 295 | $this->trigger_error('Error cache size '.$this->max_size,E_USER_ERROR); 296 | return false; 297 | }elseif($size<10485760){ 298 | return 10485760; 299 | }else{ 300 | return $size; 301 | } 302 | }else{ 303 | return $default; 304 | } 305 | } 306 | function _format($truncate=false){ 307 | if($this->lock(true,true)){ 308 | if($truncate){ 309 | $this->_seek(0); 310 | ftruncate($this->_rs,$this->idx_node_base); 311 | } 312 | $this->max_size = $this->_parse_str_size(SECACHE_SIZE,15728640); //default:15m 313 | $this->_puts($this->header_padding,pack('V1a*',$this->max_size,$this->ver)); 314 | ksort($this->_bsize_list); 315 | $ds_offset = $this->data_base_pos; 316 | $i=0; 317 | foreach($this->_bsize_list as $size=>$count){ 318 | //将预分配的空间注册到free链表里 319 | $count *= min(3,floor($this->max_size/10485760)); 320 | $next_free_node = 0; 321 | for($j=0;$j<$count;$j++){ 322 | $this->_puts($ds_offset,pack('V',$next_free_node)); 323 | $next_free_node = $ds_offset; 324 | $ds_offset+=intval($size); 325 | } 326 | $code = pack(str_repeat('V1',count($this->schema_struct)),$size,$next_free_node,0,0,0,0); 327 | $this->_puts(60+$i*$this->schema_item_size,$code); 328 | $i++; 329 | } 330 | $this->_set_dcur_pos($ds_offset); 331 | $this->_puts($this->idx_base_pos,str_repeat("\0",262144)); 332 | $this->_puts($this->idx_seq_pos,pack('V',1)); 333 | $this->unlock(); 334 | return true; 335 | }else{ 336 | $this->trigger_error("Couldn't lock the file !",E_USER_ERROR); 337 | return false; 338 | } 339 | } 340 | function _get_node_root($key){ 341 | $this->_seek(hexdec(substr($key,0,4))*4+$this->idx_base_pos); 342 | $a= fread($this->_rs,4); 343 | list(,$offset) = unpack('V',$a); 344 | return $offset; 345 | } 346 | function _set_node_root($key,$value){ 347 | return $this->_puts(hexdec(substr($key,0,4))*4+$this->idx_base_pos,pack('V',$value)); 348 | } 349 | function _set_node($pos,$key,$value){ 350 | if(!$pos){ 351 | return false; 352 | } 353 | if(isset($this->_node_struct[$key])){ 354 | return $this->_puts($pos*$this->idx_node_size+$this->idx_node_base+$this->_node_struct[$key][0],pack($this->_node_struct[$key][1],$value)); 355 | }else{ 356 | return false; 357 | } 358 | } 359 | function _get_pos_by_key($offset,$key,&$pos){ 360 | if(!$offset){ 361 | $pos = 0; 362 | return false; 363 | } 364 | $info = $this->_get_node($offset); 365 | if($info['key']==$key){ 366 | $pos = $info['offset']; 367 | return true; 368 | }elseif($info['next'] && $info['next']!=$offset){ 369 | return $this->_get_pos_by_key($info['next'],$key,$pos); 370 | }else{ 371 | $pos = $offset; 372 | return false; 373 | } 374 | } 375 | function _lru_delete($info){ 376 | if($info['lru_right']){ 377 | $this->_set_node($info['lru_right'],'lru_left',$info['lru_left']); 378 | }else{ 379 | $this->_set_schema($this->_get_size_schema_id($info['size']),'lru_tail',$info['lru_left']); 380 | } 381 | if($info['lru_left']){ 382 | $this->_set_node($info['lru_left'],'lru_right',$info['lru_right']); 383 | }else{ 384 | $this->_set_schema($this->_get_size_schema_id($info['size']),'lru_head',$info['lru_right']); 385 | } 386 | return true; 387 | } 388 | function _lru_push($schema_id,$offset){ 389 | $lru_head = $this->_get_schema($schema_id,'lru_head'); 390 | $lru_tail = $this->_get_schema($schema_id,'lru_tail'); 391 | if((!$offset) || ($lru_head==$offset))return; 392 | $info = $this->_get_node($offset); 393 | $this->_set_node($info['lru_right'],'lru_left',$info['lru_left']); 394 | $this->_set_node($info['lru_left'],'lru_right',$info['lru_right']); 395 | $this->_set_node($offset,'lru_right',$lru_head); 396 | $this->_set_node($offset,'lru_left',0); 397 | $this->_set_node($lru_head,'lru_left',$offset); 398 | $this->_set_schema($schema_id,'lru_head',$offset); 399 | if($lru_tail==0){ 400 | $this->_set_schema($schema_id,'lru_tail',$offset); 401 | }elseif($lru_tail==$offset && $info['lru_left']){ 402 | $this->_set_schema($schema_id,'lru_tail',$info['lru_left']); 403 | } 404 | return true; 405 | } 406 | function _get_node($offset){ 407 | $this->_seek($offset*$this->idx_node_size + $this->idx_node_base); 408 | $info = unpack('V1next/V1prev/V1data/V1size/V1lru_right/V1lru_left/H*key',fread($this->_rs,$this->idx_node_size)); 409 | $info['offset'] = $offset; 410 | return $info; 411 | } 412 | function _lru_pop($schema_id){ 413 | if($node = $this->_get_schema($schema_id,'lru_tail')){ 414 | $info = $this->_get_node($node); 415 | if(!$info['data']){ 416 | return false; 417 | } 418 | $this->delete($info['key'],$info['offset']); 419 | if(!$this->_get_schema($schema_id,'free')){ 420 | $this->trigger_error('pop lru,But nothing free...',E_USER_ERROR); 421 | } 422 | return $info; 423 | }else{ 424 | return false; 425 | } 426 | } 427 | function _dalloc($schema_id,$lru_freed=false){ 428 | if($free = $this->_get_schema($schema_id,'free')){ //如果lru里有链表 429 | $this->_seek($free); 430 | list(,$next) = unpack('V',fread($this->_rs,4)); 431 | $this->_set_schema($schema_id,'free',$next); 432 | return $free; 433 | }elseif($lru_freed){ 434 | $this->trigger_error('Bat lru poped freesize',E_USER_ERROR); 435 | return false; 436 | }else{ 437 | $ds_offset = $this->_get_dcur_pos(); 438 | $size = $this->_get_schema($schema_id,'size'); 439 | if($size+$ds_offset > $this->max_size){ 440 | if($info = $this->_lru_pop($schema_id)){ 441 | return $this->_dalloc($schema_id,$info); 442 | }else{ 443 | $this->trigger_error('Can\'t alloc dataspace',E_USER_ERROR); 444 | return false; 445 | } 446 | }else{ 447 | $this->_set_dcur_pos($ds_offset+$size); 448 | return $ds_offset; 449 | } 450 | } 451 | } 452 | function _get_dcur_pos(){ 453 | $this->_seek($this->dfile_cur_pos); 454 | list(,$ds_offset) = unpack('V',fread($this->_rs,4)); 455 | return $ds_offset; 456 | } 457 | function _set_dcur_pos($pos){ 458 | return $this->_puts($this->dfile_cur_pos,pack('V',$pos)); 459 | } 460 | function _free_dspace($size,$pos){ 461 | if($pos>$this->max_size){ 462 | $this->trigger_error('free dspace over quota:'.$pos,E_USER_ERROR); 463 | return false; 464 | } 465 | $schema_id = $this->_get_size_schema_id($size); 466 | if($free = $this->_get_schema($schema_id,'free')){ 467 | $this->_puts($free,pack('V1',$pos)); 468 | }else{ 469 | $this->_set_schema($schema_id,'free',$pos); 470 | } 471 | $this->_puts($pos,pack('V1',0)); 472 | } 473 | function _dfollow($pos,&$c){ 474 | $c++; 475 | $this->_seek($pos); 476 | list(,$next) = unpack('V1',fread($this->_rs,4)); 477 | if($next){ 478 | return $this->_dfollow($next,$c); 479 | }else{ 480 | return $pos; 481 | } 482 | } 483 | function _free_node($pos){ 484 | $this->_seek($this->idx_free_pos); 485 | list(,$prev_free_node) = unpack('V',fread($this->_rs,4)); 486 | $this->_puts($pos*$this->idx_node_size+$this->idx_node_base,pack('V',$prev_free_node).str_repeat("\0",$this->idx_node_size-4)); 487 | return $this->_puts($this->idx_free_pos,pack('V',$pos)); 488 | } 489 | function _alloc_idx($data){ 490 | $this->_seek($this->idx_free_pos); 491 | list(,$list_pos) = unpack('V',fread($this->_rs,4)); 492 | if($list_pos){ 493 | $this->_seek($list_pos*$this->idx_node_size+$this->idx_node_base); 494 | list(,$prev_free_node) = unpack('V',fread($this->_rs,4)); 495 | $this->_puts($this->idx_free_pos,pack('V',$prev_free_node)); 496 | }else{ 497 | $this->_seek($this->idx_seq_pos); 498 | list(,$list_pos) = unpack('V',fread($this->_rs,4)); 499 | $this->_puts($this->idx_seq_pos,pack('V',$list_pos+1)); 500 | } 501 | return $this->_create_node($list_pos,$data); 502 | } 503 | function _create_node($pos,$data){ 504 | $this->_puts($pos*$this->idx_node_size + $this->idx_node_base 505 | ,pack('V1V1V1V1V1V1H*',$data['next'],$data['prev'],$data['data'],$data['size'],$data['lru_right'],$data['lru_left'],$data['key'])); 506 | return $pos; 507 | } 508 | function _set_schema($schema_id,$key,$value){ 509 | $info = array_flip($this->schema_struct); 510 | return $this->_puts(60+$schema_id*$this->schema_item_size + $info[$key]*4,pack('V',$value)); 511 | } 512 | function _get_schema($id,$key){ 513 | $info = array_flip($this->schema_struct); 514 | $this->_seek(60+$id*$this->schema_item_size); 515 | unpack('V1'.implode('/V1',$this->schema_struct),fread($this->_rs,$this->schema_item_size)); 516 | $this->_seek(60+$id*$this->schema_item_size + $info[$key]*4); 517 | list(,$value) =unpack('V',fread($this->_rs,4)); 518 | return $value; 519 | } 520 | function _all_schemas(){ 521 | $schema = array(); 522 | for($i=0;$i<16;$i++){ 523 | $this->_seek(60+$i*$this->schema_item_size); 524 | $info = unpack('V1'.implode('/V1',$this->schema_struct),fread($this->_rs,$this->schema_item_size)); 525 | if($info['size']){ 526 | $info['id'] = $i; 527 | $schema[$i] = $info; 528 | }else{ 529 | return $schema; 530 | } 531 | } 532 | } 533 | function schemaStatus(){ 534 | $return = array(); 535 | foreach($this->_all_schemas() as $k=>$schemaItem){ 536 | if($schemaItem['free']){ 537 | $this->_dfollow($schemaItem['free'],$schemaItem['freecount']); 538 | } 539 | $return[] = $schemaItem; 540 | } 541 | return $return; 542 | } 543 | function status(&$curBytes,&$totalBytes){ 544 | $totalBytes = $curBytes = 0; 545 | $hits = $miss = 0; 546 | $schemaStatus = $this->schemaStatus(); 547 | $totalBytes = $this->max_size; 548 | $freeBytes = $this->max_size - $this->_get_dcur_pos(); 549 | foreach($schemaStatus as $schema){ 550 | $freeBytes+=$schema['freecount']*$schema['size']; 551 | $miss += $schema['miss']; 552 | $hits += $schema['hits']; 553 | } 554 | $curBytes = $totalBytes-$freeBytes; 555 | $return[] = array('name'=>'缓存命中','value'=>$hits); 556 | $return[] = array('name'=>'缓存未命中','value'=>$miss); 557 | return $return; 558 | } 559 | function trigger_error($errstr,$errno){ 560 | trigger_error($errstr,$errno); 561 | } 562 | } 563 | ?> 564 | -------------------------------------------------------------------------------- /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('@\
57 | 添加以下命令到crontab
58 | */10 * * * *
59 |
# | 15 |环境需求 | 16 |当前环境 | 17 ||
---|---|---|---|
1 | 22 |PHP > 5.5 | 23 | 24 | | 25 | 26 | | 27 | 28 |
2 | 31 |curl 支持 | 32 | 33 | | 34 | 35 | | 36 | 37 |
3 | 40 |config/ 目录可读可写 | 41 | 42 | | 43 | 44 | | 45 | 46 |
4 | 49 |cache/ 目录可读可写 | 50 | 51 | | 52 | 53 | | 54 | 55 |
client_id
和client_secret
,
13 | 获取应用ID和机密(分两个页面显示,请注意保存)
14 | 远程路径 | 44 |上传速度 | 45 |进度 | 46 |状态 | 47 |操作 | 48 |
---|
远程路径 | 100 |状态 | 101 |
---|---|
107 | | 108 | 109 | |
Name | Size | Date Created | Date Modified |
---|---|---|---|
17 | .. 18 | | 19 |20 | | 21 | | |
/ | 27 |28 | | 29 | | |
33 | | 34 | | 35 | |