Subversion Repositories oidplus

Rev

Rev 694 | Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
635 daniel-mar 1
<?php
2
 
3
/*
4
 * OIDplus 2.0
5
 * Copyright 2019 - 2021 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('INSIDE_OIDPLUS')) die();
21
 
22
class OIDplusPagePublicResources extends OIDplusPagePluginPublic {
23
 
24
        private function getMainTitle() {
25
                return _L('Documents and Resources');
26
        }
27
 
28
        public function init($html=true) {
29
                OIDplus::config()->prepareConfigKey('resource_plugin_autoopen_level', 'Resource plugin: How many levels should be open in the treeview when OIDplus is loaded?', '1', OIDplusConfig::PROTECTION_EDITABLE, function($value) {
30
                        if (!is_numeric($value) || ($value < 0)) {
31
                                throw new OIDplusException(_L('Please enter a valid value.'));
32
                        }
33
                });
34
                OIDplus::config()->delete('resource_plugin_title');
35
                OIDplus::config()->delete('resource_plugin_path');
36
                OIDplus::config()->prepareConfigKey('resource_plugin_hide_empty_path','Resource plugin: Hide empty paths? (0=no, 1=yes)', '1', OIDplusConfig::PROTECTION_EDITABLE, function($value) {
37
                        if (!is_numeric($value) || (($value != 0) && ($value != 1))) {
38
                                throw new OIDplusException(_L('Please enter a valid value (0=no, 1=yes).'));
39
                        }
40
                });
41
        }
42
 
43
        private static function getDocumentContent($file) {
44
                $file = self::realname($file);
45
                $file2 = preg_replace('/\.([^.]+)$/', '$'.OIDplus::getCurrentLang().'.\1', $file);
46
                if (file_exists($file2)) $file = $file2;
47
 
48
                $cont = file_get_contents($file);
49
 
50
                list($html, $js, $css) = extractHtmlContents($cont);
51
                $cont = '';
52
                if (!empty($js))  $cont .= "<script>\n$js\n</script>";
53
                if (!empty($css)) $cont .= "<style>\n$css\n</style>";
54
                $cont .= $html;
55
 
56
                return $cont;
57
        }
58
 
59
        private static function getDocumentTitle($file) {
60
                $file = self::realname($file);
61
                $file2 = preg_replace('/\.([^.]+)$/', '$'.OIDplus::getCurrentLang().'.\1', $file);
62
                if (file_exists($file2)) $file = $file2;
63
 
64
                $cont = file_get_contents($file);
65
 
66
                // make sure the program works even if the user provided HTML is not UTF-8
67
                $cont = iconv(mb_detect_encoding($cont, mb_detect_order(), true), 'UTF-8//IGNORE', $cont);
68
                $bom = pack('H*','EFBBBF');
69
                $cont = preg_replace("/^$bom/", '', $cont);
70
 
71
                $m = array();
72
                if (preg_match('@<title>(.+)</title>@ismU', $cont, $m)) return $m[1];
73
                if (preg_match('@<h1>(.+)</h1>@ismU', $cont, $m)) return $m[1];
74
                if (preg_match('@<h2>(.+)</h2>@ismU', $cont, $m)) return $m[1];
75
                if (preg_match('@<h3>(.+)</h3>@ismU', $cont, $m)) return $m[1];
76
                if (preg_match('@<h4>(.+)</h4>@ismU', $cont, $m)) return $m[1];
77
                if (preg_match('@<h5>(.+)</h5>@ismU', $cont, $m)) return $m[1];
78
                if (preg_match('@<h6>(.+)</h6>@ismU', $cont, $m)) return $m[1];
79
                return pathinfo($file, PATHINFO_FILENAME); // filename without extension
80
        }
81
 
82
        protected static function mayAccessResource($source) {
83
                if (OIDplus::authUtils()->isAdminLoggedIn()) return true;
84
 
85
                $candidates = array(
86
                        OIDplus::localpath().'userdata/resources/security.ini',
87
                        OIDplus::localpath().'res/security.ini'
88
                );
89
                foreach ($candidates as $ini_file) {
90
                        if (file_exists($ini_file)) {
91
                                $data = @parse_ini_file($ini_file, true);
92
                                if (isset($data['Security']) && isset($data['Security'][$source])) {
93
                                        $level = $data['Security'][$source];
94
                                        if ($level == 'PUBLIC') {
95
                                                return true;
96
                                        } else if ($level == 'RA') {
97
                                                return
98
                                                        ((OIDplus::authUtils()->raNumLoggedIn() > 0) ||
99
                                                        (OIDplus::authUtils()->isAdminLoggedIn()));
100
                                        } else if ($level == 'ADMIN') {
101
                                                return OIDplus::authUtils()->isAdminLoggedIn();
102
                                        } else {
103
                                                throw new OIDplusException(_L('Unexpected security level in %1 (expect PUBLIC, RA or ADMIN)', $ini_file));
104
                                        }
105
                                }
106
                        }
107
                }
108
                return true;
109
        }
110
 
111
        private static function myglob($reldir, $onlydir=false) {
112
                $out = array();
113
 
114
                $root = OIDplus::localpath().'userdata/resources/';
115
                $res = $onlydir ? glob($root.ltrim($reldir,'/'), GLOB_ONLYDIR) : glob($root.ltrim($reldir,'/'));
116
                foreach ($res as &$x) {
117
                        $x = substr($x, strlen($root));
118
                        if (strpos($x,'$') !== false) continue;
119
                        $out[] = $x;
120
                }
121
 
122
                $root = OIDplus::localpath().'res/';
123
                $res = $onlydir ? glob($root.ltrim($reldir,'/'), GLOB_ONLYDIR) : glob($root.ltrim($reldir,'/'));
124
                foreach ($res as $x) {
125
                        $x = substr($x, strlen($root));
126
                        if (strpos($x,'$') !== false) continue;
127
                        $out[] = $x;
128
                }
129
 
130
                $out = array_unique($out);
131
 
132
                return array_filter($out, function($v, $k) {
133
                        return self::mayAccessResource($v);
134
                }, ARRAY_FILTER_USE_BOTH);
135
        }
136
 
137
        private static function realname($rel) {
138
                $candidate1 = OIDplus::localpath().'userdata/resources/'.$rel;
139
                $candidate2 = OIDplus::localpath().'res/'.$rel;
140
                if (file_exists($candidate1) || is_dir($candidate1)) return $candidate1;
141
                if (file_exists($candidate2) || is_dir($candidate2)) return $candidate2;
142
                return null;
143
        }
144
 
145
        protected static function checkRedirect($source, &$target): bool {
146
                $candidates = array(
147
                        OIDplus::localpath().'userdata/resources/redirect.ini',
148
                        OIDplus::localpath().'res/redirect.ini'
149
                );
150
                foreach ($candidates as $ini_file) {
151
                        if (file_exists($ini_file)) {
152
                                $data = @parse_ini_file($ini_file, true);
153
                                if (isset($data['Redirects']) && isset($data['Redirects'][$source])) {
154
                                        $target = $data['Redirects'][$source];
155
                                        return true;
156
                                }
157
                        }
158
                }
159
                return false;
160
        }
161
 
162
        public function gui($id, &$out, &$handled) {
163
                if (explode('$',$id,2)[0] === 'oidplus:resources') {
164
                        $handled = true;
165
 
166
                        $file = @explode('$',$id)[1];
167
 
168
                        // Security checks
169
 
170
                        if (
171
                                ($file != '') && (
172
                                (strpos($file, chr(0)) !== false) || // Directory traversal (LFI,RFI) helper
173
                                (strpos($file, '../') !== false) || ($file[0] == '/') || ($file[0] == '~') || // <-- Local File Injection (LFI)
174
                                ($file[0] == '.') || (strpos($file, '/.') !== false) ||                       // <-- Calling hidden files e.g. ".htpasswd"
175
                                (strpos($file, '://') !== false)                                              // <-- Remote File Injection (RFI)
176
                           )) {
177
                                if (strpos($file, chr(0)) !== false) {
178
                                        $file = str_replace(chr(0), '[NUL]', $file);
179
                                }
180
                                // This will not be logged anymore, because people could spam the log files otherwise
181
                                //OIDplus::logger()->log("[WARN]A!", "LFI/RFI attack blocked (requested file '$file')");
182
                                $out['title'] = _L('Access denied');
183
                                $out['icon'] = 'img/error_big.png';
184
                                $out['text'] = '<p>'._L('This request is invalid').'</p>';
185
                                return;
186
                        }
187
 
188
                        $out['text'] = '';
189
 
190
                        // Check for permission
191
 
192
                        if ($file != '') {
193
                                if (!self::mayAccessResource($file)) {
194
                                        $out['title'] = _L('Access denied');
195
                                        $out['icon'] = 'img/error_big.png';
196
                                        $out['text'] = '<p>'._L('Authentication error. Please log in.').'</p>';
197
                                        return;
198
                                }
199
                        }
200
 
201
                        // Redirections
202
 
203
                        if ($file != '') {
204
                                $target = null;
205
                                if (self::checkRedirect($file, $target)) {
206
                                        $out['title'] = _L('Please wait...');
207
                                        $out['text'] = '<p>'._L('You are being redirected...').'</p><script>window.location.href = '.js_escape($target).';</script>';
208
                                        return;
209
                                }
210
                        }
211
 
212
                        // First, "Go back to" line
213
 
214
                        if ($file != '') {
215
                                $dir = dirname($file);
216
 
217
                                if ($dir == '.') {
218
                                        if (file_exists(__DIR__.'/treeicon.png')) {
219
                                                $tree_icon = OIDplus::webpath(__DIR__).'treeicon.png';
220
                                        } else {
221
                                                $tree_icon = null; // default icon (folder)
222
                                        }
223
 
224
                                        $ic = empty($tree_icon) ? '' : '<img src="'.$tree_icon.'" alt="">';
225
 
226
                                        $lng_gobackto = _L('Go back to').':';
227
                                        $out['text'] .= '<p><a '.OIDplus::gui()->link('oidplus:resources').'><img src="img/arrow_back.png" width="16" alt="'._L('Go back').'"> '.$lng_gobackto.' '.$ic.' '.htmlentities($this->getMainTitle()).'</a></p>';
228
                                } else {
229
                                        $realdir = self::realname($dir);
230
 
231
                                        $tree_icon = OIDplus::webpath(__DIR__).'show_icon.php?mode=treeicon_folder&lang='.OIDplus::getCurrentLang().'&file='.urlencode($dir);
232
                                        /*
233
                                        $icon_candidate = pathinfo($realdir)['dirname'].'/'.pathinfo($realdir)['filename'].'_tree.png';
234
                                        if (file_exists($icon_candidate)) {
235
                                                $tree_icon = $icon_candidate;
236
                                        } else if (file_exists(__DIR__.'/treeicon_folder.png')) {
237
                                                $tree_icon = OIDplus::webpath(__DIR__).'treeicon_folder.png';
238
                                        } else {
239
                                                $tree_icon = null; // no icon
240
                                        }
241
                                        */
242
 
243
                                        $ic = empty($tree_icon) ? '' : '<img src="'.$tree_icon.'" alt="">';
244
 
245
                                        $out['text'] .= '<p><a '.OIDplus::gui()->link('oidplus:resources$'.rtrim($dir,'/').'/').'><img src="img/arrow_back.png" width="16" alt="'._L('Go back').'"> '._L('Go back to').': '.$ic.' '.htmlentities(self::getFolderTitle($realdir)).'</a></p><br>';
246
                                }
247
                        }
248
 
249
                        // Then the content
250
 
251
                        $realfile = self::realname($file);
252
                        // $realfile2 = preg_replace('/\.([^.]+)$/', '$'.OIDplus::getCurrentLang().'.\1', $realfile);
253
                        // if (file_exists($realfile2)) $realfile = $realfile2;
254
 
255
                        if (file_exists($realfile) && (!is_dir($realfile))) {
256
                                if ((substr($file,-4,4) == '.url') || (substr($file,-5,5) == '.link')) {
257
                                        $out['title'] = $this->getHyperlinkTitle($realfile);
258
 
259
                                        $out['icon'] = OIDplus::webpath(__DIR__).'show_icon.php?mode=icon_leaf_url_big&lang='.OIDplus::getCurrentLang().'&file='.urlencode($file);
260
                                        /*
261
                                        $icon_candidate = pathinfo($realfile)['dirname'].'/'.pathinfo($realfile)['filename'].'_big.png';
262
                                        if (file_exists($icon_candidate)) {
263
                                                $out['icon'] = $icon_candidate;
264
                                        } else if (file_exists(__DIR__.'/icon_leaf_url_big.png')) {
265
                                                $out['icon'] = OIDplus::webpath(__DIR__).'icon_leaf_url_big.png';
266
                                        } else {
267
                                                $out['icon'] = '';
268
                                        }
269
                                        */
270
 
271
                                        // Should not happen though, due to conditionalselect
272
                                        $out['text'] .= '<a href="'.htmlentities(self::getHyperlinkURL($realfile)).'" target="_blank">'._L('Open in new window').'</a>';
273
                                } else if ((substr($file,-4,4) == '.htm') || (substr($file,-5,5) == '.html')) {
274
                                        $out['title'] = $this->getDocumentTitle($file);
275
 
276
                                        $out['icon'] = OIDplus::webpath(__DIR__).'show_icon.php?mode=icon_leaf_doc_big&lang='.OIDplus::getCurrentLang().'&file='.urlencode($file);
277
                                        /*
278
                                        $icon_candidate = pathinfo($realfile)['dirname'].'/'.pathinfo($realfile)['filename'].'_big.png';
279
                                        if (file_exists($icon_candidate)) {
280
                                                $out['icon'] = $icon_candidate;
281
                                        } else if (file_exists(__DIR__.'/icon_leaf_doc_big.png')) {
282
                                                $out['icon'] = OIDplus::webpath(__DIR__).'icon_leaf_doc_big.png';
283
                                        } else {
284
                                                $out['icon'] = '';
285
                                        }
286
                                        */
287
 
288
                                        $out['text'] .= self::getDocumentContent($file);
289
                                } else {
290
                                        $out['title'] = _L('Unknown file type');
291
                                        $out['icon'] = 'img/error_big.png';
292
                                        $out['text'] = '<p>'._L('The system does not know how to handle this file type.').'</p>';
293
                                        return;
294
                                }
295
                        } else if (is_dir($realfile)) {
296
                                $out['title'] = ($file == '') ? $this->getMainTitle() : self::getFolderTitle($realfile);
297
 
298
                                if ($file == '') {
299
                                        $out['icon'] = file_exists(__DIR__.'/icon_big.png') ? OIDplus::webpath(__DIR__).'icon_big.png' : '';
300
                                } else {
301
                                        $out['icon'] = OIDplus::webpath(__DIR__).'show_icon.php?mode=icon_folder_big&lang='.OIDplus::getCurrentLang().'&file='.urlencode($file);
302
                                        /*
303
                                        $icon_candidate = pathinfo($realfile)['dirname'].'/'.pathinfo($realfile)['filename'].'_big.png';
304
                                        if (file_exists($icon_candidate)) {
305
                                                $out['icon'] = $icon_candidate;
306
                                        } else if (file_exists(__DIR__.'/icon_folder_big.png')) {
307
                                                $out['icon'] = OIDplus::webpath(__DIR__).'icon_folder_big.png';
308
                                        } else {
309
                                                $out['icon'] = null; // no icon
310
                                        }
311
                                        */
312
                                }
313
 
314
                                if (file_exists(__DIR__.'/treeicon.png')) {
315
                                        $tree_icon = OIDplus::webpath(__DIR__).'treeicon.png';
316
                                } else {
317
                                        $tree_icon = null; // default icon (folder)
318
                                }
319
 
320
                                $count = 0;
321
 
322
                                $dirs = self::myglob(rtrim($file,'/').'/'.'*', true);
323
                                natcasesort($dirs);
324
                                foreach ($dirs as $dir) {
325
                                        $realdir = self::realname($dir);
326
                                        $tree_icon = OIDplus::webpath(__DIR__).'show_icon.php?mode=treeicon_folder&lang='.OIDplus::getCurrentLang().'&file='.urlencode($dir);
327
                                        /*
328
                                        $icon_candidate = pathinfo($realdir)['dirname'].'/'.pathinfo($realdir)['filename'].'_tree.png';
329
                                        if (file_exists($icon_candidate)) {
330
                                                $tree_icon = $icon_candidate;
331
                                        } else if (file_exists(__DIR__.'/treeicon_folder.png')) {
332
                                                $tree_icon = OIDplus::webpath(__DIR__).'treeicon_folder.png';
333
                                        } else {
334
                                                $tree_icon = null; // no icon
335
                                        }
336
                                        */
337
 
338
                                        $ic = empty($tree_icon) ? '' : '<img src="'.$tree_icon.'" alt="">';
339
 
340
                                        $out['text'] .= '<p><a '.OIDplus::gui()->link('oidplus:resources$'.rtrim($dir,'/').'/').'>'.$ic.' '.htmlentities(self::getFolderTitle($realdir)).'</a></p>';
341
                                        $count++;
342
                                }
343
 
344
                                $files = array_merge(
345
                                        self::myglob(rtrim($file,'/').'/'.'*.htm'), // TODO: also PHP?
346
                                        self::myglob(rtrim($file,'/').'/'.'*.html'),
347
                                        self::myglob(rtrim($file,'/').'/'.'*.url'),
348
                                        self::myglob(rtrim($file,'/').'/'.'*.link')
349
                                );
350
                                natcasesort($files);
351
                                foreach ($files as $file) {
352
                                        $realfile = self::realname($file);
353
                                        if ((substr($file,-4,4) == '.url') || (substr($file,-5,5) == '.link')) {
354
                                                $tree_icon = OIDplus::webpath(__DIR__).'show_icon.php?mode=treeicon_leaf_url&lang='.OIDplus::getCurrentLang().'&file='.urlencode($file);
355
                                                /*
356
                                                $icon_candidate = pathinfo($realfile)['dirname'].'/'.pathinfo($realfile)['filename'].'_tree.png';
357
                                                if (file_exists($icon_candidate)) {
358
                                                        $tree_icon = $icon_candidate;
359
                                                } else if (file_exists(__DIR__.'/treeicon_leaf_url.png')) {
360
                                                        $tree_icon = OIDplus::webpath(__DIR__).'treeicon_leaf_url.png';
361
                                                } else {
362
                                                        $tree_icon = null; // default icon (folder)
363
                                                }
364
                                                */
365
 
366
                                                $ic = empty($tree_icon) ? '' : '<img src="'.$tree_icon.'" alt="">';
367
 
368
                                                $hyperlink_pic = ' <img src="'.OIDplus::webpath(__DIR__).'hyperlink.png" widht="13" height="13" alt="Hyperlink" style="top:-3px;position:relative">';
369
 
370
                                                $out['text'] .= '<p><a href="'.htmlentities(self::getHyperlinkURL($realfile)).'" target="_blank">'.$ic.' '.htmlentities($this->getHyperlinkTitle($realfile)).' '.$hyperlink_pic.'</a></p>';
371
                                                $count++;
372
                                        } else {
373
                                                $tree_icon = OIDplus::webpath(__DIR__).'show_icon.php?mode=treeicon_leaf_doc&lang='.OIDplus::getCurrentLang().'&file='.urlencode($file);
374
                                                /*
375
                                                $icon_candidate = pathinfo($realfile)['dirname'].'/'.pathinfo($realfile)['filename'].'_tree.png';
376
                                                if (file_exists($icon_candidate)) {
377
                                                        $tree_icon = $icon_candidate;
378
                                                } else if (file_exists(__DIR__.'/treeicon_leaf_doc.png')) {
379
                                                        $tree_icon = OIDplus::webpath(__DIR__).'treeicon_leaf_doc.png';
380
                                                } else {
381
                                                        $tree_icon = null; // default icon (folder)
382
                                                }
383
                                                */
384
 
385
                                                $ic = empty($tree_icon) ? '' : '<img src="'.$tree_icon.'" alt="">';
386
 
387
                                                $out['text'] .= '<p><a '.OIDplus::gui()->link('oidplus:resources$'.$file).'>'.$ic.' '.htmlentities($this->getDocumentTitle($file)).'</a></p>';
388
                                                $count++;
389
                                        }
390
                                }
391
 
392
                                if ($count == 0) {
393
                                        $out['text'] .= '<p>'._L('This folder does not contain any elements').'</p>';
394
                                }
395
                        } else {
396
                                $out['title'] = _L('Not found');
397
                                $out['icon'] = 'img/error_big.png';
398
                                $out['text'] = '<p>'._L('This resource doesn\'t exist anymore.').'</p>';
399
                        }
400
                }
401
        }
402
 
403
        private function tree_rec(&$children, $rootdir=null, $depth=0) {
404
                if (is_null($rootdir)) $rootdir = '';
405
                if ($depth > 100) return false; // something is wrong!
406
 
407
                $dirs = self::myglob($rootdir.'*'.'/', true);
408
                natcasesort($dirs);
409
                foreach ($dirs as $dir) {
410
                        $tmp = array();
411
 
412
                        $this->tree_rec($tmp, $dir, $depth+1);
413
 
414
                        $realdir = self::realname($dir);
415
 
416
                        $tree_icon = OIDplus::webpath(__DIR__).'show_icon.php?mode=treeicon_folder&lang='.OIDplus::getCurrentLang().'&file='.urlencode($dir);
417
                        /*
418
                        $icon_candidate = pathinfo($realdir)['dirname'].'/'.pathinfo($realdir)['filename'].'_tree.png';
419
                        if (file_exists($icon_candidate)) {
420
                                $tree_icon = $icon_candidate;
421
                        } else if (file_exists(__DIR__.'/treeicon_folder.png')) {
422
                                $tree_icon = OIDplus::webpath(__DIR__).'treeicon_folder.png';
423
                        } else {
424
                                $tree_icon = null; // default icon (folder)
425
                        }
426
                        */
427
 
428
                        $children[] = array(
429
                                'id' => 'oidplus:resources$'.$dir,
430
                                'icon' => $tree_icon,
431
                                'text' => self::getFolderTitle($realdir),
432
                                'children' => $tmp,
433
                                'state' => array("opened" => $depth <= OIDplus::config()->getValue('resource_plugin_autoopen_level', 1)-1)
434
                        );
435
                }
436
 
437
                $files = array_merge(
438
                        self::myglob($rootdir.'*.htm'), // TODO: Also PHP?
439
                        self::myglob($rootdir.'*.html'),
440
                        self::myglob($rootdir.'*.url'),
441
                        self::myglob($rootdir.'*.link')
442
                );
443
                natcasesort($files);
444
                foreach ($files as $file) {
445
                        $realfile = self::realname($file);
446
                        if ((substr($file,-4,4) == '.url') || (substr($file,-5,5) == '.link')) {
447
                                $tree_icon = OIDplus::webpath(__DIR__).'show_icon.php?mode=treeicon_leaf_url&lang='.OIDplus::getCurrentLang().'&file='.urlencode($file);
448
                                /*
449
                                $icon_candidate = pathinfo($realfile)['dirname'].'/'.pathinfo($realfile)['filename'].'_tree.png';
450
                                if (file_exists($icon_candidate)) {
451
                                        $tree_icon = $icon_candidate;
452
                                } else if (file_exists(__DIR__.'/treeicon_leaf_url.png')) {
453
                                        $tree_icon = OIDplus::webpath(__DIR__).'treeicon_leaf_url.png';
454
                                } else {
455
                                        $tree_icon = null; // default icon (folder)
456
                                }
457
                                */
458
 
459
                                $hyperlink_pic = ' <img src="'.OIDplus::webpath(__DIR__).'hyperlink.png" widht="13" height="13" alt="Hyperlink" style="top:-3px;position:relative">';
460
 
461
                                $children[] = array(
462
                                        'id' => 'oidplus:resources$'.$file,
463
                                        'conditionalselect' => 'window.open('.js_escape(self::getHyperlinkURL($realfile)).'); false;',
464
                                        'icon' => $tree_icon,
465
                                        'text' => $this->getHyperlinkTitle($realfile).' '.$hyperlink_pic,
466
                                        'state' => array("opened" => $depth <= OIDplus::config()->getValue('resource_plugin_autoopen_level', 1)-1),
467
                                        'a_attr' => array(
468
                                                'href' => self::getHyperlinkURL($realfile),
469
                                                'target' => '_blank'
470
                                        )
471
                                );
472
                        } else {
473
                                $tree_icon = OIDplus::webpath(__DIR__).'show_icon.php?mode=treeicon_leaf_doc&lang='.OIDplus::getCurrentLang().'&file='.urlencode($file);
474
                                /*
475
                                $icon_candidate = pathinfo($realfile)['dirname'].'/'.pathinfo($realfile)['filename'].'_tree.png';
476
                                if (file_exists($icon_candidate)) {
477
                                        $tree_icon = $icon_candidate;
478
                                } else if (file_exists(__DIR__.'/treeicon_leaf_doc.png')) {
479
                                        $tree_icon = OIDplus::webpath(__DIR__).'treeicon_leaf_doc.png';
480
                                } else {
481
                                        $tree_icon = null; // default icon (folder)
482
                                }
483
                                */
484
                                $children[] = array(
485
                                        'id' => 'oidplus:resources$'.$file,
486
                                        'icon' => $tree_icon,
487
                                        'text' => $this->getDocumentTitle($file),
488
                                        'state' => array("opened" => $depth <= OIDplus::config()->getValue('resource_plugin_autoopen_level', 1)-1)
489
                                );
490
                        }
491
                }
492
        }
493
 
494
        private function publicSitemap_rec($json, &$out) {
495
                foreach ($json as $x) {
496
                        if (isset($x['id']) && $x['id']) {
497
                                $out[] = $x['id'];
498
                        }
499
                        if (isset($x['children'])) {
500
                                $this->publicSitemap_rec($x['children'], $out);
501
                        }
502
                }
503
        }
504
 
505
        public function publicSitemap(&$out) {
506
                $json = array();
507
                $this->tree($json, null/*RA EMail*/, false/*HTML tree algorithm*/, true/*display all*/);
508
                $this->publicSitemap_rec($json, $out);
509
        }
510
 
511
        public function tree(&$json, $ra_email=null, $nonjs=false, $req_goto='') {
512
                $children = array();
513
 
514
                $this->tree_rec($children, '/');
515
 
516
                if (!OIDplus::config()->getValue('resource_plugin_hide_empty_path', true) || (count($children) > 0)) {
517
                        if (file_exists(__DIR__.'/treeicon.png')) {
518
                                $tree_icon = OIDplus::webpath(__DIR__).'treeicon.png';
519
                        } else {
520
                                $tree_icon = null; // default icon (folder)
521
                        }
522
 
523
                        $json[] = array(
524
                                'id' => 'oidplus:resources',
525
                                'icon' => $tree_icon,
526
                                'state' => array("opened" => true),
527
                                'text' => $this->getMainTitle(),
528
                                'children' => $children
529
                        );
530
                }
531
 
532
                return true;
533
        }
534
 
535
        public function tree_search($request) {
536
                return false;
537
        }
538
 
539
        private static function getHyperlinkTitle($file) {
540
                $file2 = preg_replace('/\.([^.]+)$/', '$'.OIDplus::getCurrentLang().'.\1', $file);
541
                if (file_exists($file2)) $file = $file2;
542
 
543
                if (substr($file,-4,4) == '.url') {
544
                        return preg_replace('/\\.[^.\\s]{3,4}$/', '', basename($file));
545
                } else if (substr($file,-5,5) == '.link') {
546
                        /*
547
                        [Link]
548
                        Title=Report a bug
549
                        URL=https://www.viathinksoft.com/thinkbug/thinkbug.php?id=97
550
                        */
551
 
552
                        $data = @parse_ini_file($file, true);
553
                        if (!$data) {
554
                                throw new OIDplusException(_L('File %1 has an invalid INI format!',$file));
555
                        }
556
                        if (!isset($data['Link'])) {
557
                                throw new OIDplusException(_L('Could not find "%1" section at %2','Link',$file));
558
                        }
559
                        if (!isset($data['Link']['Title'])) {
560
                                throw new OIDplusException(_L('"%1" is missing in %2','Title',$file));
561
                        }
562
                        return $data['Link']['Title'];
563
                } else {
564
                        throw new OIDplusException(_L('Unexpected file extension for file %1',$file));
565
                }
566
        }
567
 
568
        private static function getHyperlinkURL($file) {
569
                $file2 = preg_replace('/\.([^.]+)$/', '$'.OIDplus::getCurrentLang().'.\1', $file);
570
                if (file_exists($file2)) $file = $file2;
571
 
572
                if (substr($file,-4,4) == '.url') {
573
                        /*
574
                        [{000214A0-0000-0000-C000-000000000046}]
575
                        Prop3=19,2
576
                        [InternetShortcut]
577
                        URL=http://www.example.com/
578
                        IDList=
579
                        */
580
 
581
                        $data = @parse_ini_file($file, true);
582
                        if (!$data) {
583
                                throw new OIDplusException(_L('File %1 has an invalid INI format!',$file));
584
                        }
585
                        if (!isset($data['InternetShortcut'])) {
586
                                throw new OIDplusException(_L('Could not find "%1" section at %2','InternetShortcut',$file));
587
                        }
588
                        if (!isset($data['InternetShortcut']['URL'])) {
589
                                throw new OIDplusException(_L('"%1" is missing in %2','URL',$file));
590
                        }
591
                        return $data['InternetShortcut']['URL'];
592
                } else if (substr($file,-5,5) == '.link') {
593
                        /*
594
                        [Link]
595
                        Title=Report a bug
596
                        URL=https://www.viathinksoft.com/thinkbug/thinkbug.php?id=97
597
                        */
598
 
599
                        $data = @parse_ini_file($file, true);
600
                        if (!$data) {
601
                                throw new OIDplusException(_L('File %1 has an invalid INI format!',$file));
602
                        }
603
                        if (!isset($data['Link'])) {
604
                                throw new OIDplusException(_L('Could not find "%1" section at %2','Link',$file));
605
                        }
606
                        if (!isset($data['Link']['URL'])) {
607
                                throw new OIDplusException(_L('"%1" is missing in %2','URL',$file));
608
                        }
609
                        return $data['Link']['URL'];
610
                } else {
611
                        throw new OIDplusException(_L('Unexpected file extension for file %1',$file));
612
                }
613
 
614
        }
615
 
616
        private static function getFolderTitle($dir) {
617
                $data = @parse_ini_file("$dir/folder\$".OIDplus::getCurrentLang().".ini", true);
618
                if ($data && isset($data['Folder']) && isset($data['Folder']['Title'])) {
619
                        return $data['Folder']['Title'];
620
                }
621
 
622
                $data = @parse_ini_file("$dir/folder.ini", true);
623
                if ($data && isset($data['Folder']) && isset($data['Folder']['Title'])) {
624
                        return $data['Folder']['Title'];
625
                }
626
 
627
                return basename($dir);
628
        }
629
}