Subversion Repositories oidplus

Rev

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