Subversion Repositories oidplus

Rev

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