Subversion Repositories oidplus

Rev

Rev 148 | 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
 
67
                        if (!OIDplus::authUtils()::validateAuthKey("oidplus:resources;$file", $auth)) {
68
                                $out['title'] = 'Access denied';
69
                                $out['icon'] = 'img/error_big.png';
70
                                $out['text'] = '<p>Invalid authentification token</p>';
71
                                return $out;
72
                        }
73
 
74
                        $out['text'] = '';
75
 
76
                        if ($file != CFG_RESOURCE_PLUGIN_PATH) {
77
                                $dir = dirname($file).'/';
78
 
79
                                if ($dir == CFG_RESOURCE_PLUGIN_PATH) {
80
                                        if (file_exists(__DIR__.'/treeicon.png')) {
148 daniel-mar 81
                                                $tree_icon = 'plugins/'.basename(dirname(__DIR__)).'/'.basename(__DIR__).'/treeicon.png';
146 daniel-mar 82
                                        } else {
83
                                                $tree_icon = null; // default icon (folder)
84
                                        }
85
 
86
                                        $ic = empty($tree_icon) ? '' : '<img src="'.$tree_icon.'" alt="">';
87
 
88
                                        $out['text'] .= '<p><a '.oidplus_link('oidplus:resources$'.CFG_RESOURCE_PLUGIN_PATH.'$'.OIDplus::authUtils()::makeAuthKey("oidplus:resources;".CFG_RESOURCE_PLUGIN_PATH)).'><img src="img/arrow_back.png" width="16"> Go back to: '.$ic.' '.htmlentities(CFG_RESOURCE_PLUGIN_TITLE).'</a></p>';
89
                                } else {
90
                                        $icon_candidate = pathinfo($dir)['dirname'].'/'.pathinfo($dir)['filename'].'_tree.png';
91
                                        if (file_exists($icon_candidate)) {
92
                                                $tree_icon = $icon_candidate;
93
                                        } else if (file_exists(__DIR__.'/treeicon_folder.png')) {
148 daniel-mar 94
                                                $tree_icon = 'plugins/'.basename(dirname(__DIR__)).'/'.basename(__DIR__).'/treeicon_folder.png';
146 daniel-mar 95
                                        } else {
96
                                                $tree_icon = null; // no icon
97
                                        }
98
 
99
                                        $ic = empty($tree_icon) ? '' : '<img src="'.$tree_icon.'" alt="">';
100
 
101
                                        $out['text'] .= '<p><a '.oidplus_link('oidplus:resources$'.$dir.'$'.OIDplus::authUtils()::makeAuthKey("oidplus:resources;$dir")).'><img src="img/arrow_back.png" width="16"> Go back to: '.$ic.' '.htmlentities(basename($dir)).'</a></p><br>';
102
                                }
103
                        }
104
 
105
                        if (file_exists($file) && (!is_dir($file))) {
106
                                if (substr($file,-4,4) == '.url') {
107
                                        $out['title'] = $this->getHyperlinkTitle($file);
108
 
109
                                        $icon_candidate = pathinfo($file)['dirname'].'/'.pathinfo($file)['filename'].'_big.png';
110
                                        if (file_exists($icon_candidate)) {
111
                                                $out['icon'] = $icon_candidate;
112
                                        } else if (file_exists(__DIR__.'/icon_leaf_url_big.png')) {
148 daniel-mar 113
                                                $out['icon'] = 'plugins/'.basename(dirname(__DIR__)).'/'.basename(__DIR__).'/icon_leaf_url_big.png';
146 daniel-mar 114
                                        } else {
115
                                                $out['icon'] = '';
116
                                        }
117
 
118
                                        // Should not happen though, due to conditionalselect
119
                                        $out['text'] .= '<a href="'.htmlentities(self::getHyperlinkURL($file)).'" target="_blank">Open in new window</a>';
120
                                } else {
121
                                        $out['title'] = $this->getDocumentTitle($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_doc_big.png')) {
148 daniel-mar 127
                                                $out['icon'] = 'plugins/'.basename(dirname(__DIR__)).'/'.basename(__DIR__).'/icon_leaf_doc_big.png';
146 daniel-mar 128
                                        } else {
129
                                                $out['icon'] = '';
130
                                        }
131
 
132
                                        $cont = file_get_contents($file);
133
                                        $cont = preg_replace('@^(.+)<body@isU', '', $cont);
134
                                        $cont = preg_replace('@</body>.+$@isU', '', $cont);
135
                                        $cont = preg_replace('@<title>.+</title>@isU', '', $cont);
136
                                        $cont = preg_replace('@<h1>.+</h1>@isU', '', $cont, 1);
137
 
138
                                        $out['text'] .= $cont;
139
                                }
140
                        } else if (is_dir($file)) {
141
                                $out['title'] = ($file == CFG_RESOURCE_PLUGIN_PATH) ? CFG_RESOURCE_PLUGIN_TITLE : basename($file);
142
 
143
                                if ($file == CFG_RESOURCE_PLUGIN_PATH) {
148 daniel-mar 144
                                        $out['icon'] = file_exists(__DIR__.'/icon_big.png') ? 'plugins/'.basename(dirname(__DIR__)).'/'.basename(__DIR__).'/icon_big.png' : '';
146 daniel-mar 145
                                } else {
146
                                        $icon_candidate = pathinfo($file)['dirname'].'/'.pathinfo($file)['filename'].'_big.png';
147
                                        if (file_exists($icon_candidate)) {
148
                                                $out['icon'] = $icon_candidate;
149
                                        } else if (file_exists(__DIR__.'/icon_folder_big.png')) {
148 daniel-mar 150
                                                $out['icon'] = 'plugins/'.basename(dirname(__DIR__)).'/'.basename(__DIR__).'/icon_folder_big.png';
146 daniel-mar 151
                                        } else {
152
                                                $out['icon'] = null; // no icon
153
                                        }
154
                                }
155
 
156
                                if (file_exists(__DIR__.'/treeicon.png')) {
148 daniel-mar 157
                                        $tree_icon = 'plugins/'.basename(dirname(__DIR__)).'/'.basename(__DIR__).'/treeicon.png';
146 daniel-mar 158
                                } else {
159
                                        $tree_icon = null; // default icon (folder)
160
                                }
161
 
162
                                $count = 0;
163
 
164
                                $dirs = glob($file.'*'.'/', GLOB_ONLYDIR);
165
                                natcasesort($dirs);
166
                                foreach ($dirs as $dir) {
167
                                        $icon_candidate = pathinfo($dir)['dirname'].'/'.pathinfo($dir)['filename'].'_tree.png';
168
                                        if (file_exists($icon_candidate)) {
169
                                                $tree_icon = $icon_candidate;
170
                                        } else if (file_exists(__DIR__.'/treeicon_folder.png')) {
148 daniel-mar 171
                                                $tree_icon = 'plugins/'.basename(dirname(__DIR__)).'/'.basename(__DIR__).'/treeicon_folder.png';
146 daniel-mar 172
                                        } else {
173
                                                $tree_icon = null; // no icon
174
                                        }
175
 
176
                                        $ic = empty($tree_icon) ? '' : '<img src="'.$tree_icon.'" alt="">';
177
 
178
                                        $out['text'] .= '<p><a '.oidplus_link('oidplus:resources$'.$dir.'$'.OIDplus::authUtils()::makeAuthKey("oidplus:resources;$dir")).'>'.$ic.' '.htmlentities(basename($dir)).'</a></p>';
179
                                        $count++;
180
                                }
181
 
182
                                $files = array_merge(
183
                                        glob($file.'/'.'*.htm*'), // TODO: also PHP?
184
                                        glob($file.'/'.'*.url')
185
                                );
186
                                natcasesort($files);
187
                                foreach ($files as $file) {
188
                                        if (substr($file,-4,4) == '.url') {
189
                                                $icon_candidate = pathinfo($file)['dirname'].'/'.pathinfo($file)['filename'].'_tree.png';
190
                                                if (file_exists($icon_candidate)) {
191
                                                        $tree_icon = $icon_candidate;
192
                                                } else if (file_exists(__DIR__.'/treeicon_leaf_url.png')) {
148 daniel-mar 193
                                                        $tree_icon = 'plugins/'.basename(dirname(__DIR__)).'/'.basename(__DIR__).'/treeicon_leaf_url.png';
146 daniel-mar 194
                                                } else {
195
                                                        $tree_icon = null; // default icon (folder)
196
                                                }
197
                                                $ic = empty($tree_icon) ? '' : '<img src="'.$tree_icon.'" alt="">';
198
 
148 daniel-mar 199
                                                $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 200
 
201
                                                $out['text'] .= '<p><a href="'.htmlentities(self::getHyperlinkURL($file)).'" target="_blank">'.$ic.' '.htmlentities($this->getHyperlinkTitle($file)).' '.$hyperlink_pic.'</a></p>';
202
                                                $count++;
203
                                        } else {
204
                                                $icon_candidate = pathinfo($file)['dirname'].'/'.pathinfo($file)['filename'].'_tree.png';
205
                                                if (file_exists($icon_candidate)) {
206
                                                        $tree_icon = $icon_candidate;
207
                                                } else if (file_exists(__DIR__.'/treeicon_leaf_doc.png')) {
148 daniel-mar 208
                                                        $tree_icon = 'plugins/'.basename(dirname(__DIR__)).'/'.basename(__DIR__).'/treeicon_leaf_doc.png';
146 daniel-mar 209
                                                } else {
210
                                                        $tree_icon = null; // default icon (folder)
211
                                                }
212
                                                $ic = empty($tree_icon) ? '' : '<img src="'.$tree_icon.'" alt="">';
213
 
214
                                                $out['text'] .= '<p><a '.oidplus_link('oidplus:resources$'.$file.'$'.OIDplus::authUtils()::makeAuthKey("oidplus:resources;$file")).'>'.$ic.' '.htmlentities($this->getDocumentTitle($file)).'</a></p>';
215
                                                $count++;
216
                                        }
217
                                }
218
 
219
                                if ($count == 0) {
220
                                        $out['text'] .= '<p>This folder does not contain any elements</p>';
221
                                }
222
                        } else {
223
                                $out['title'] = 'Not found';
224
                                $out['icon'] = 'img/error_big.png';
225
                                $out['text'] = '<p>This resource doesn\'t exist anymore.</p>';
226
                                return $out;
227
                        }
228
                }
229
        }
230
 
231
        private function tree_rec(&$children, $rootdir=CFG_RESOURCE_PLUGIN_PATH, $depth=0) {
232
                if ($depth > 100) return false; // something is wrong!
233
 
234
                $dirs = glob($rootdir.'*'.'/', GLOB_ONLYDIR);
235
                natcasesort($dirs);
236
                foreach ($dirs as $dir) {
237
                        $tmp = array();
238
                        $this->tree_rec($tmp, $dir, $depth+1);
239
 
240
                        $icon_candidate = pathinfo($dir)['dirname'].'/'.pathinfo($dir)['filename'].'_tree.png';
241
                        if (file_exists($icon_candidate)) {
242
                                $tree_icon = $icon_candidate;
243
                        } else if (file_exists(__DIR__.'/treeicon_folder.png')) {
148 daniel-mar 244
                                $tree_icon = 'plugins/'.basename(dirname(__DIR__)).'/'.basename(__DIR__).'/treeicon_folder.png';
146 daniel-mar 245
                        } else {
246
                                $tree_icon = null; // default icon (folder)
247
                        }
248
 
249
                        $children[] = array(
250
                                'id' => 'oidplus:resources$'.$dir.'$'.OIDplus::authUtils()::makeAuthKey("oidplus:resources;$dir"),
251
                                'icon' => $tree_icon,
252
                                'text' => basename($dir),
253
                                'children' => $tmp,
254
                                'state' => array("opened" => $depth <= CFG_RESOURCE_PLUGIN_AUTOOPEN_LEVEL-1)
255
                        );
256
                }
257
 
258
                $files = array_merge(
259
                        glob($rootdir.'*.htm*'), // TODO: Also PHP?
260
                        glob($rootdir.'*.url')
261
                );
262
                natcasesort($files);
263
                foreach ($files as $file) {
264
                        if (substr($file,-4,4) == '.url') {
265
 
266
                                $icon_candidate = pathinfo($file)['dirname'].'/'.pathinfo($file)['filename'].'_tree.png';
267
                                if (file_exists($icon_candidate)) {
268
                                        $tree_icon = $icon_candidate;
269
                                } else if (file_exists(__DIR__.'/treeicon_leaf_url.png')) {
148 daniel-mar 270
                                        $tree_icon = 'plugins/'.basename(dirname(__DIR__)).'/'.basename(__DIR__).'/treeicon_leaf_url.png';
146 daniel-mar 271
                                } else {
272
                                        $tree_icon = null; // default icon (folder)
273
                                }
274
 
148 daniel-mar 275
                                $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 276
 
277
                                $children[] = array(
278
                                        'id' => 'oidplus:resources$'.$file.'$'.OIDplus::authUtils()::makeAuthKey("oidplus:resources;$file"),
279
                                        'conditionalselect' => 'window.open('.js_escape(self::getHyperlinkURL($file)).'); false;',
280
                                        'icon' => $tree_icon,
281
                                        'text' => $this->getHyperlinkTitle($file).' '.$hyperlink_pic,
282
                                        'state' => array("opened" => $depth <= CFG_RESOURCE_PLUGIN_AUTOOPEN_LEVEL-1)
283
                                );
284
 
285
                        } else {
286
                                $icon_candidate = pathinfo($file)['dirname'].'/'.pathinfo($file)['filename'].'_tree.png';
287
                                if (file_exists($icon_candidate)) {
288
                                        $tree_icon = $icon_candidate;
289
                                } else if (file_exists(__DIR__.'/treeicon_leaf_doc.png')) {
148 daniel-mar 290
                                        $tree_icon = 'plugins/'.basename(dirname(__DIR__)).'/'.basename(__DIR__).'/treeicon_leaf_doc.png';
146 daniel-mar 291
                                } else {
292
                                        $tree_icon = null; // default icon (folder)
293
                                }
294
                                $children[] = array(
295
                                        'id' => 'oidplus:resources$'.$file.'$'.OIDplus::authUtils()::makeAuthKey("oidplus:resources;$file"),
296
                                        'icon' => $tree_icon,
297
                                        'text' => $this->getDocumentTitle($file),
298
                                        'state' => array("opened" => $depth <= CFG_RESOURCE_PLUGIN_AUTOOPEN_LEVEL-1)
299
                                );
300
                        }
301
                }
302
        }
303
 
304
        public function tree(&$json, $ra_email=null, $nonjs=false, $req_goto='') {
305
                $children = array();
306
 
307
                $this->tree_rec($children, CFG_RESOURCE_PLUGIN_PATH);
308
 
309
                if (!CFG_RESOURCE_PLUGIN_HIDE_EMPTY_PATH || (count($children) > 0)) {
310
                        if (file_exists(__DIR__.'/treeicon.png')) {
148 daniel-mar 311
                                $tree_icon = 'plugins/'.basename(dirname(__DIR__)).'/'.basename(__DIR__).'/treeicon.png';
146 daniel-mar 312
                        } else {
313
                                $tree_icon = null; // default icon (folder)
314
                        }
315
 
316
                        $json[] = array(
317
                                'id' => 'oidplus:resources$'.CFG_RESOURCE_PLUGIN_PATH.'$'.OIDplus::authUtils()::makeAuthKey("oidplus:resources;".CFG_RESOURCE_PLUGIN_PATH),
318
                                'icon' => $tree_icon,
319
                                'state' => array("opened" => true),
320
                                'text' => CFG_RESOURCE_PLUGIN_TITLE,
321
                                'children' => $children
322
                        );
323
                }
324
 
325
                return true;
326
        }
327
 
328
        public function tree_search($request) {
329
                return false;
330
        }
331
 
332
        private static function getHyperlinkTitle($file) {
333
                return preg_replace('/\\.[^.\\s]{3,4}$/', '', basename($file));
334
        }
335
 
336
        private static function getHyperlinkURL($file) {
337
                /*
338
                [{000214A0-0000-0000-C000-000000000046}]
339
                Prop3=19,2
340
                [InternetShortcut]
341
                URL=http://www.example.com/
342
                IDList=
343
                */
344
                $cont = file_get_contents($file);
345
                if (!preg_match('@URL=(.+)\n@ismU', $cont, $m)) return null;
346
                return trim($m[1]);
347
        }
348
}
349
 
350
OIDplus::registerPagePlugin(new OIDplusPagePublicResources());