Subversion Repositories oidplus

Rev

Rev 213 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
146 daniel-mar 1
<?php
2
 
3
/*
4
 * OIDplus 2.0
5
 * Copyright 2019 Daniel Marschall, ViaThinkSoft
6
 *
7
 * Licensed under the Apache License, Version 2.0 (the "License");
8
 * you may not use this file except in compliance with the License.
9
 * You may obtain a copy of the License at
10
 *
11
 *     http://www.apache.org/licenses/LICENSE-2.0
12
 *
13
 * Unless required by applicable law or agreed to in writing, software
14
 * distributed under the License is distributed on an "AS IS" BASIS,
15
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
 * See the License for the specific language governing permissions and
17
 * limitations under the License.
18
 */
19
 
20
if (!defined('IN_OIDPLUS')) die();
21
 
22
define('CFG_RESOURCE_PLUGIN_AUTOOPEN_LEVEL', 1);
23
define('CFG_RESOURCE_PLUGIN_TITLE', 'Documents and resources');
24
define('CFG_RESOURCE_PLUGIN_PATH', 'res/');
25
define('CFG_RESOURCE_PLUGIN_HIDE_EMPTY_PATH', true);
26
 
27
class OIDplusPagePublicResources extends OIDplusPagePlugin {
28
        public function type() {
29
                return 'public';
30
        }
31
 
32
        public function priority() {
33
                return 500;
34
        }
35
 
36
        public function action(&$handled) {
37
                // Nothing
38
        }
39
 
40
        public function init($html=true) {
41
                // Nothing
42
        }
43
 
44
        public function cfgSetValue($name, $value) {
45
                // Nothing
46
        }
47
 
48
        private static function getDocumentTitle($file) {
49
                $cont = file_get_contents($file);
50
                if (preg_match('@<title>(.+)</title>@ismU', $cont, $m)) return $m[1];
51
                if (preg_match('@<h1>(.+)</h1>@ismU', $cont, $m)) return $m[1];
52
                if (preg_match('@<h2>(.+)</h2>@ismU', $cont, $m)) return $m[1];
53
                if (preg_match('@<h3>(.+)</h3>@ismU', $cont, $m)) return $m[1];
54
                if (preg_match('@<h4>(.+)</h4>@ismU', $cont, $m)) return $m[1];
55
                if (preg_match('@<h5>(.+)</h5>@ismU', $cont, $m)) return $m[1];
56
                if (preg_match('@<h6>(.+)</h6>@ismU', $cont, $m)) return $m[1];
180 daniel-mar 57
                return pathinfo($file, PATHINFO_FILENAME); // filename without extension
146 daniel-mar 58
        }
59
 
60
        public function gui($id, &$out, &$handled) {
61
                if (explode('$',$id)[0] === 'oidplus:resources') {
62
                        $handled = true;
63
 
64
                        $file = @explode('$',$id)[1];
65
                        $auth = @explode('$',$id)[2];
66
 
213 daniel-mar 67
                        if (!OIDplus::authUtils()::validateAuthKey("resources;$file", $auth)) {
146 daniel-mar 68
                                $out['title'] = 'Access denied';
69
                                $out['icon'] = 'img/error_big.png';
213 daniel-mar 70
                                $out['text'] = '<p>Invalid authentication token</p>';
146 daniel-mar 71
                                return $out;
72
                        }
73
 
214 daniel-mar 74
                        if (strpos($file, CFG_RESOURCE_PLUGIN_PATH) !== 0) {
75
                                $out['title'] = 'Access denied';
76
                                $out['icon'] = 'img/error_big.png';
77
                                $out['text'] = '<p>Security breach A</p>';
78
                                return $out;
79
                        }
80
 
81
                        if (strpos($file, '..') !== false) {
82
                                $out['title'] = 'Access denied';
83
                                $out['icon'] = 'img/error_big.png';
84
                                $out['text'] = '<p>Security breach A</p>';
85
                                return $out;
86
                        }
87
 
146 daniel-mar 88
                        $out['text'] = '';
89
 
90
                        if ($file != CFG_RESOURCE_PLUGIN_PATH) {
91
                                $dir = dirname($file).'/';
92
 
93
                                if ($dir == CFG_RESOURCE_PLUGIN_PATH) {
94
                                        if (file_exists(__DIR__.'/treeicon.png')) {
148 daniel-mar 95
                                                $tree_icon = 'plugins/'.basename(dirname(__DIR__)).'/'.basename(__DIR__).'/treeicon.png';
146 daniel-mar 96
                                        } else {
97
                                                $tree_icon = null; // default icon (folder)
98
                                        }
99
 
100
                                        $ic = empty($tree_icon) ? '' : '<img src="'.$tree_icon.'" alt="">';
101
 
213 daniel-mar 102
                                        $out['text'] .= '<p><a '.oidplus_link('oidplus:resources$'.CFG_RESOURCE_PLUGIN_PATH.'$'.OIDplus::authUtils()::makeAuthKey("resources;".CFG_RESOURCE_PLUGIN_PATH)).'><img src="img/arrow_back.png" width="16"> Go back to: '.$ic.' '.htmlentities(CFG_RESOURCE_PLUGIN_TITLE).'</a></p>';
146 daniel-mar 103
                                } else {
104
                                        $icon_candidate = pathinfo($dir)['dirname'].'/'.pathinfo($dir)['filename'].'_tree.png';
105
                                        if (file_exists($icon_candidate)) {
106
                                                $tree_icon = $icon_candidate;
107
                                        } else if (file_exists(__DIR__.'/treeicon_folder.png')) {
148 daniel-mar 108
                                                $tree_icon = 'plugins/'.basename(dirname(__DIR__)).'/'.basename(__DIR__).'/treeicon_folder.png';
146 daniel-mar 109
                                        } else {
110
                                                $tree_icon = null; // no icon
111
                                        }
112
 
113
                                        $ic = empty($tree_icon) ? '' : '<img src="'.$tree_icon.'" alt="">';
114
 
213 daniel-mar 115
                                        $out['text'] .= '<p><a '.oidplus_link('oidplus:resources$'.$dir.'$'.OIDplus::authUtils()::makeAuthKey("resources;$dir")).'><img src="img/arrow_back.png" width="16"> Go back to: '.$ic.' '.htmlentities(basename($dir)).'</a></p><br>';
146 daniel-mar 116
                                }
117
                        }
118
 
119
                        if (file_exists($file) && (!is_dir($file))) {
120
                                if (substr($file,-4,4) == '.url') {
121
                                        $out['title'] = $this->getHyperlinkTitle($file);
122
 
123
                                        $icon_candidate = pathinfo($file)['dirname'].'/'.pathinfo($file)['filename'].'_big.png';
124
                                        if (file_exists($icon_candidate)) {
125
                                                $out['icon'] = $icon_candidate;
126
                                        } else if (file_exists(__DIR__.'/icon_leaf_url_big.png')) {
148 daniel-mar 127
                                                $out['icon'] = 'plugins/'.basename(dirname(__DIR__)).'/'.basename(__DIR__).'/icon_leaf_url_big.png';
146 daniel-mar 128
                                        } else {
129
                                                $out['icon'] = '';
130
                                        }
131
 
132
                                        // Should not happen though, due to conditionalselect
133
                                        $out['text'] .= '<a href="'.htmlentities(self::getHyperlinkURL($file)).'" target="_blank">Open in new window</a>';
213 daniel-mar 134
                                } else if ((substr($file,-4,4) == '.htm') || (substr($file,-5,5) == '.html')) {
146 daniel-mar 135
                                        $out['title'] = $this->getDocumentTitle($file);
136
 
137
                                        $icon_candidate = pathinfo($file)['dirname'].'/'.pathinfo($file)['filename'].'_big.png';
138
                                        if (file_exists($icon_candidate)) {
139
                                                $out['icon'] = $icon_candidate;
140
                                        } else if (file_exists(__DIR__.'/icon_leaf_doc_big.png')) {
148 daniel-mar 141
                                                $out['icon'] = 'plugins/'.basename(dirname(__DIR__)).'/'.basename(__DIR__).'/icon_leaf_doc_big.png';
146 daniel-mar 142
                                        } else {
143
                                                $out['icon'] = '';
144
                                        }
145
 
146
                                        $cont = file_get_contents($file);
147
                                        $cont = preg_replace('@^(.+)<body@isU', '', $cont);
148
                                        $cont = preg_replace('@</body>.+$@isU', '', $cont);
149
                                        $cont = preg_replace('@<title>.+</title>@isU', '', $cont);
150
                                        $cont = preg_replace('@<h1>.+</h1>@isU', '', $cont, 1);
151
 
152
                                        $out['text'] .= $cont;
213 daniel-mar 153
                                } else {
154
                                        $out['title'] = 'Unknown file type';
155
                                        $out['icon'] = 'img/error_big.png';
156
                                        $out['text'] = '<p>The system does not know how to handle this file type.</p>';
157
                                        return $out;
146 daniel-mar 158
                                }
159
                        } else if (is_dir($file)) {
160
                                $out['title'] = ($file == CFG_RESOURCE_PLUGIN_PATH) ? CFG_RESOURCE_PLUGIN_TITLE : basename($file);
161
 
162
                                if ($file == CFG_RESOURCE_PLUGIN_PATH) {
148 daniel-mar 163
                                        $out['icon'] = file_exists(__DIR__.'/icon_big.png') ? 'plugins/'.basename(dirname(__DIR__)).'/'.basename(__DIR__).'/icon_big.png' : '';
146 daniel-mar 164
                                } else {
165
                                        $icon_candidate = pathinfo($file)['dirname'].'/'.pathinfo($file)['filename'].'_big.png';
166
                                        if (file_exists($icon_candidate)) {
167
                                                $out['icon'] = $icon_candidate;
168
                                        } else if (file_exists(__DIR__.'/icon_folder_big.png')) {
148 daniel-mar 169
                                                $out['icon'] = 'plugins/'.basename(dirname(__DIR__)).'/'.basename(__DIR__).'/icon_folder_big.png';
146 daniel-mar 170
                                        } else {
171
                                                $out['icon'] = null; // no icon
172
                                        }
173
                                }
174
 
175
                                if (file_exists(__DIR__.'/treeicon.png')) {
148 daniel-mar 176
                                        $tree_icon = 'plugins/'.basename(dirname(__DIR__)).'/'.basename(__DIR__).'/treeicon.png';
146 daniel-mar 177
                                } else {
178
                                        $tree_icon = null; // default icon (folder)
179
                                }
180
 
181
                                $count = 0;
182
 
183
                                $dirs = glob($file.'*'.'/', GLOB_ONLYDIR);
184
                                natcasesort($dirs);
185
                                foreach ($dirs as $dir) {
186
                                        $icon_candidate = pathinfo($dir)['dirname'].'/'.pathinfo($dir)['filename'].'_tree.png';
187
                                        if (file_exists($icon_candidate)) {
188
                                                $tree_icon = $icon_candidate;
189
                                        } else if (file_exists(__DIR__.'/treeicon_folder.png')) {
148 daniel-mar 190
                                                $tree_icon = 'plugins/'.basename(dirname(__DIR__)).'/'.basename(__DIR__).'/treeicon_folder.png';
146 daniel-mar 191
                                        } else {
192
                                                $tree_icon = null; // no icon
193
                                        }
194
 
195
                                        $ic = empty($tree_icon) ? '' : '<img src="'.$tree_icon.'" alt="">';
196
 
213 daniel-mar 197
                                        $out['text'] .= '<p><a '.oidplus_link('oidplus:resources$'.$dir.'$'.OIDplus::authUtils()::makeAuthKey("resources;$dir")).'>'.$ic.' '.htmlentities(basename($dir)).'</a></p>';
146 daniel-mar 198
                                        $count++;
199
                                }
200
 
201
                                $files = array_merge(
202
                                        glob($file.'/'.'*.htm*'), // TODO: also PHP?
203
                                        glob($file.'/'.'*.url')
204
                                );
205
                                natcasesort($files);
206
                                foreach ($files as $file) {
207
                                        if (substr($file,-4,4) == '.url') {
208
                                                $icon_candidate = pathinfo($file)['dirname'].'/'.pathinfo($file)['filename'].'_tree.png';
209
                                                if (file_exists($icon_candidate)) {
210
                                                        $tree_icon = $icon_candidate;
211
                                                } else if (file_exists(__DIR__.'/treeicon_leaf_url.png')) {
148 daniel-mar 212
                                                        $tree_icon = 'plugins/'.basename(dirname(__DIR__)).'/'.basename(__DIR__).'/treeicon_leaf_url.png';
146 daniel-mar 213
                                                } else {
214
                                                        $tree_icon = null; // default icon (folder)
215
                                                }
216
                                                $ic = empty($tree_icon) ? '' : '<img src="'.$tree_icon.'" alt="">';
217
 
148 daniel-mar 218
                                                $hyperlink_pic = ' <img src="plugins/'.basename(dirname(__DIR__)).'/'.basename(__DIR__).'/hyperlink.png" widht="13" height="13" alt="Hyperlink" style="top:-3px;position:relative">';
146 daniel-mar 219
 
220
                                                $out['text'] .= '<p><a href="'.htmlentities(self::getHyperlinkURL($file)).'" target="_blank">'.$ic.' '.htmlentities($this->getHyperlinkTitle($file)).' '.$hyperlink_pic.'</a></p>';
221
                                                $count++;
222
                                        } else {
223
                                                $icon_candidate = pathinfo($file)['dirname'].'/'.pathinfo($file)['filename'].'_tree.png';
224
                                                if (file_exists($icon_candidate)) {
225
                                                        $tree_icon = $icon_candidate;
226
                                                } else if (file_exists(__DIR__.'/treeicon_leaf_doc.png')) {
148 daniel-mar 227
                                                        $tree_icon = 'plugins/'.basename(dirname(__DIR__)).'/'.basename(__DIR__).'/treeicon_leaf_doc.png';
146 daniel-mar 228
                                                } else {
229
                                                        $tree_icon = null; // default icon (folder)
230
                                                }
231
                                                $ic = empty($tree_icon) ? '' : '<img src="'.$tree_icon.'" alt="">';
232
 
213 daniel-mar 233
                                                $out['text'] .= '<p><a '.oidplus_link('oidplus:resources$'.$file.'$'.OIDplus::authUtils()::makeAuthKey("resources;$file")).'>'.$ic.' '.htmlentities($this->getDocumentTitle($file)).'</a></p>';
146 daniel-mar 234
                                                $count++;
235
                                        }
236
                                }
237
 
238
                                if ($count == 0) {
239
                                        $out['text'] .= '<p>This folder does not contain any elements</p>';
240
                                }
241
                        } else {
242
                                $out['title'] = 'Not found';
243
                                $out['icon'] = 'img/error_big.png';
244
                                $out['text'] = '<p>This resource doesn\'t exist anymore.</p>';
245
                                return $out;
246
                        }
247
                }
248
        }
249
 
250
        private function tree_rec(&$children, $rootdir=CFG_RESOURCE_PLUGIN_PATH, $depth=0) {
251
                if ($depth > 100) return false; // something is wrong!
252
 
253
                $dirs = glob($rootdir.'*'.'/', GLOB_ONLYDIR);
254
                natcasesort($dirs);
255
                foreach ($dirs as $dir) {
256
                        $tmp = array();
257
                        $this->tree_rec($tmp, $dir, $depth+1);
258
 
259
                        $icon_candidate = pathinfo($dir)['dirname'].'/'.pathinfo($dir)['filename'].'_tree.png';
260
                        if (file_exists($icon_candidate)) {
261
                                $tree_icon = $icon_candidate;
262
                        } else if (file_exists(__DIR__.'/treeicon_folder.png')) {
148 daniel-mar 263
                                $tree_icon = 'plugins/'.basename(dirname(__DIR__)).'/'.basename(__DIR__).'/treeicon_folder.png';
146 daniel-mar 264
                        } else {
265
                                $tree_icon = null; // default icon (folder)
266
                        }
267
 
268
                        $children[] = array(
213 daniel-mar 269
                                'id' => 'oidplus:resources$'.$dir.'$'.OIDplus::authUtils()::makeAuthKey("resources;$dir"),
146 daniel-mar 270
                                'icon' => $tree_icon,
271
                                'text' => basename($dir),
272
                                'children' => $tmp,
273
                                'state' => array("opened" => $depth <= CFG_RESOURCE_PLUGIN_AUTOOPEN_LEVEL-1)
274
                        );
275
                }
276
 
277
                $files = array_merge(
278
                        glob($rootdir.'*.htm*'), // TODO: Also PHP?
279
                        glob($rootdir.'*.url')
280
                );
281
                natcasesort($files);
282
                foreach ($files as $file) {
283
                        if (substr($file,-4,4) == '.url') {
284
 
285
                                $icon_candidate = pathinfo($file)['dirname'].'/'.pathinfo($file)['filename'].'_tree.png';
286
                                if (file_exists($icon_candidate)) {
287
                                        $tree_icon = $icon_candidate;
288
                                } else if (file_exists(__DIR__.'/treeicon_leaf_url.png')) {
148 daniel-mar 289
                                        $tree_icon = 'plugins/'.basename(dirname(__DIR__)).'/'.basename(__DIR__).'/treeicon_leaf_url.png';
146 daniel-mar 290
                                } else {
291
                                        $tree_icon = null; // default icon (folder)
292
                                }
293
 
148 daniel-mar 294
                                $hyperlink_pic = ' <img src="plugins/'.basename(dirname(__DIR__)).'/'.basename(__DIR__).'/hyperlink.png" widht="13" height="13" alt="Hyperlink" style="top:-3px;position:relative">';
146 daniel-mar 295
 
296
                                $children[] = array(
213 daniel-mar 297
                                        'id' => 'oidplus:resources$'.$file.'$'.OIDplus::authUtils()::makeAuthKey("resources;$file"),
146 daniel-mar 298
                                        'conditionalselect' => 'window.open('.js_escape(self::getHyperlinkURL($file)).'); false;',
299
                                        'icon' => $tree_icon,
300
                                        'text' => $this->getHyperlinkTitle($file).' '.$hyperlink_pic,
301
                                        'state' => array("opened" => $depth <= CFG_RESOURCE_PLUGIN_AUTOOPEN_LEVEL-1)
302
                                );
303
 
304
                        } else {
305
                                $icon_candidate = pathinfo($file)['dirname'].'/'.pathinfo($file)['filename'].'_tree.png';
306
                                if (file_exists($icon_candidate)) {
307
                                        $tree_icon = $icon_candidate;
308
                                } else if (file_exists(__DIR__.'/treeicon_leaf_doc.png')) {
148 daniel-mar 309
                                        $tree_icon = 'plugins/'.basename(dirname(__DIR__)).'/'.basename(__DIR__).'/treeicon_leaf_doc.png';
146 daniel-mar 310
                                } else {
311
                                        $tree_icon = null; // default icon (folder)
312
                                }
313
                                $children[] = array(
213 daniel-mar 314
                                        'id' => 'oidplus:resources$'.$file.'$'.OIDplus::authUtils()::makeAuthKey("resources;$file"),
146 daniel-mar 315
                                        'icon' => $tree_icon,
316
                                        'text' => $this->getDocumentTitle($file),
317
                                        'state' => array("opened" => $depth <= CFG_RESOURCE_PLUGIN_AUTOOPEN_LEVEL-1)
318
                                );
319
                        }
320
                }
321
        }
322
 
323
        public function tree(&$json, $ra_email=null, $nonjs=false, $req_goto='') {
324
                $children = array();
325
 
326
                $this->tree_rec($children, CFG_RESOURCE_PLUGIN_PATH);
327
 
328
                if (!CFG_RESOURCE_PLUGIN_HIDE_EMPTY_PATH || (count($children) > 0)) {
329
                        if (file_exists(__DIR__.'/treeicon.png')) {
148 daniel-mar 330
                                $tree_icon = 'plugins/'.basename(dirname(__DIR__)).'/'.basename(__DIR__).'/treeicon.png';
146 daniel-mar 331
                        } else {
332
                                $tree_icon = null; // default icon (folder)
333
                        }
334
 
335
                        $json[] = array(
213 daniel-mar 336
                                'id' => 'oidplus:resources$'.CFG_RESOURCE_PLUGIN_PATH.'$'.OIDplus::authUtils()::makeAuthKey("resources;".CFG_RESOURCE_PLUGIN_PATH),
146 daniel-mar 337
                                'icon' => $tree_icon,
338
                                'state' => array("opened" => true),
339
                                'text' => CFG_RESOURCE_PLUGIN_TITLE,
340
                                'children' => $children
341
                        );
342
                }
343
 
344
                return true;
345
        }
346
 
347
        public function tree_search($request) {
348
                return false;
349
        }
350
 
351
        private static function getHyperlinkTitle($file) {
352
                return preg_replace('/\\.[^.\\s]{3,4}$/', '', basename($file));
353
        }
354
 
355
        private static function getHyperlinkURL($file) {
356
                /*
357
                [{000214A0-0000-0000-C000-000000000046}]
358
                Prop3=19,2
359
                [InternetShortcut]
360
                URL=http://www.example.com/
361
                IDList=
362
                */
363
                $cont = file_get_contents($file);
364
                if (!preg_match('@URL=(.+)\n@ismU', $cont, $m)) return null;
365
                return trim($m[1]);
366
        }
367
}
368
 
369
OIDplus::registerPagePlugin(new OIDplusPagePublicResources());