Rev 1062 | Rev 1084 | 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 | |||
1050 | daniel-mar | 20 | namespace ViaThinkSoft\OIDplus; |
635 | daniel-mar | 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>"; |
||
821 | daniel-mar | 54 | $cont .= stripHtmlComments($html); |
635 | daniel-mar | 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 | |||
1062 | daniel-mar | 164 | $tmp = explode('$',$id); |
165 | $file = isset($tmp[1]) ? $tmp[1] : ''; |
||
166 | unset($tmp); |
||
635 | daniel-mar | 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'); |
||
800 | daniel-mar | 183 | $out['icon'] = 'img/error.png'; |
635 | daniel-mar | 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'); |
||
800 | daniel-mar | 195 | $out['icon'] = 'img/error.png'; |
635 | daniel-mar | 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 == '.') { |
||
800 | daniel-mar | 218 | if (file_exists(__DIR__.'/img/main_icon16.png')) { |
801 | daniel-mar | 219 | $tree_icon = OIDplus::webpath(__DIR__,OIDplus::PATH_RELATIVE).'img/main_icon16.png'; |
635 | daniel-mar | 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 | |||
801 | daniel-mar | 231 | $tree_icon = OIDplus::webpath(__DIR__,OIDplus::PATH_RELATIVE).'show_icon.php?mode=folder_icon16&lang='.OIDplus::getCurrentLang().'&file='.urlencode($dir); |
635 | daniel-mar | 232 | /* |
233 | $icon_candidate = pathinfo($realdir)['dirname'].'/'.pathinfo($realdir)['filename'].'_tree.png'; |
||
234 | if (file_exists($icon_candidate)) { |
||
235 | $tree_icon = $icon_candidate; |
||
800 | daniel-mar | 236 | } else if (file_exists(__DIR__.'/img/folder_icon16.png')) { |
801 | daniel-mar | 237 | $tree_icon = OIDplus::webpath(__DIR__,OIDplus::PATH_RELATIVE).'img/folder_icon16.png'; |
635 | daniel-mar | 238 | } else { |
239 | $tree_icon = null; // no icon |
||
240 | } |
||
241 | */ |
||
242 | |||
705 | daniel-mar | 243 | $ic = /*empty($tree_icon) ? '' : */'<img src="'.$tree_icon.'" alt="">'; |
635 | daniel-mar | 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 | |||
801 | daniel-mar | 259 | $out['icon'] = OIDplus::webpath(__DIR__,OIDplus::PATH_RELATIVE).'show_icon.php?mode=leaf_url_icon&lang='.OIDplus::getCurrentLang().'&file='.urlencode($file); |
635 | daniel-mar | 260 | /* |
261 | $icon_candidate = pathinfo($realfile)['dirname'].'/'.pathinfo($realfile)['filename'].'_big.png'; |
||
262 | if (file_exists($icon_candidate)) { |
||
263 | $out['icon'] = $icon_candidate; |
||
800 | daniel-mar | 264 | } else if (file_exists(__DIR__.'/img/leaf_url_icon.png')) { |
801 | daniel-mar | 265 | $out['icon'] = OIDplus::webpath(__DIR__,OIDplus::PATH_RELATIVE).'img/leaf_url_icon.png'; |
635 | daniel-mar | 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 | |||
801 | daniel-mar | 276 | $out['icon'] = OIDplus::webpath(__DIR__,OIDplus::PATH_RELATIVE).'show_icon.php?mode=leaf_doc_icon&lang='.OIDplus::getCurrentLang().'&file='.urlencode($file); |
635 | daniel-mar | 277 | /* |
278 | $icon_candidate = pathinfo($realfile)['dirname'].'/'.pathinfo($realfile)['filename'].'_big.png'; |
||
279 | if (file_exists($icon_candidate)) { |
||
280 | $out['icon'] = $icon_candidate; |
||
800 | daniel-mar | 281 | } else if (file_exists(__DIR__.'/img/leaf_doc_icon.png')) { |
801 | daniel-mar | 282 | $out['icon'] = OIDplus::webpath(__DIR__,OIDplus::PATH_RELATIVE).'img/leaf_doc_icon.png'; |
635 | daniel-mar | 283 | } else { |
284 | $out['icon'] = ''; |
||
285 | } |
||
286 | */ |
||
287 | |||
288 | $out['text'] .= self::getDocumentContent($file); |
||
289 | } else { |
||
290 | $out['title'] = _L('Unknown file type'); |
||
800 | daniel-mar | 291 | $out['icon'] = 'img/error.png'; |
635 | daniel-mar | 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 == '') { |
||
801 | daniel-mar | 299 | $out['icon'] = file_exists(__DIR__.'/img/main_icon.png') ? OIDplus::webpath(__DIR__,OIDplus::PATH_RELATIVE).'img/main_icon.png' : ''; |
635 | daniel-mar | 300 | } else { |
801 | daniel-mar | 301 | $out['icon'] = OIDplus::webpath(__DIR__,OIDplus::PATH_RELATIVE).'show_icon.php?mode=folder_icon&lang='.OIDplus::getCurrentLang().'&file='.urlencode($file); |
635 | daniel-mar | 302 | /* |
303 | $icon_candidate = pathinfo($realfile)['dirname'].'/'.pathinfo($realfile)['filename'].'_big.png'; |
||
304 | if (file_exists($icon_candidate)) { |
||
305 | $out['icon'] = $icon_candidate; |
||
800 | daniel-mar | 306 | } else if (file_exists(__DIR__.'/img/folder_icon.png')) { |
801 | daniel-mar | 307 | $out['icon'] = OIDplus::webpath(__DIR__,OIDplus::PATH_RELATIVE).'img/folder_icon.png'; |
635 | daniel-mar | 308 | } else { |
309 | $out['icon'] = null; // no icon |
||
310 | } |
||
311 | */ |
||
312 | } |
||
313 | |||
800 | daniel-mar | 314 | if (file_exists(__DIR__.'/img/main_icon16.png')) { |
801 | daniel-mar | 315 | $tree_icon = OIDplus::webpath(__DIR__,OIDplus::PATH_RELATIVE).'img/main_icon16.png'; |
635 | daniel-mar | 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); |
||
801 | daniel-mar | 326 | $tree_icon = OIDplus::webpath(__DIR__,OIDplus::PATH_RELATIVE).'show_icon.php?mode=folder_icon16&lang='.OIDplus::getCurrentLang().'&file='.urlencode($dir); |
635 | daniel-mar | 327 | /* |
328 | $icon_candidate = pathinfo($realdir)['dirname'].'/'.pathinfo($realdir)['filename'].'_tree.png'; |
||
329 | if (file_exists($icon_candidate)) { |
||
330 | $tree_icon = $icon_candidate; |
||
800 | daniel-mar | 331 | } else if (file_exists(__DIR__.'/img/folder_icon16.png')) { |
801 | daniel-mar | 332 | $tree_icon = OIDplus::webpath(__DIR__,OIDplus::PATH_RELATIVE).'img/folder_icon16.png'; |
635 | daniel-mar | 333 | } else { |
334 | $tree_icon = null; // no icon |
||
335 | } |
||
336 | */ |
||
337 | |||
705 | daniel-mar | 338 | $ic = /*empty($tree_icon) ? '' : */'<img src="'.$tree_icon.'" alt="">'; |
635 | daniel-mar | 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')) { |
||
801 | daniel-mar | 354 | $tree_icon = OIDplus::webpath(__DIR__,OIDplus::PATH_RELATIVE).'show_icon.php?mode=leaf_url_icon16&lang='.OIDplus::getCurrentLang().'&file='.urlencode($file); |
635 | daniel-mar | 355 | /* |
356 | $icon_candidate = pathinfo($realfile)['dirname'].'/'.pathinfo($realfile)['filename'].'_tree.png'; |
||
357 | if (file_exists($icon_candidate)) { |
||
358 | $tree_icon = $icon_candidate; |
||
800 | daniel-mar | 359 | } else if (file_exists(__DIR__.'/img/leaf_url_icon16.png')) { |
801 | daniel-mar | 360 | $tree_icon = OIDplus::webpath(__DIR__,OIDplus::PATH_RELATIVE).'img/leaf_url_icon16.png'; |
635 | daniel-mar | 361 | } else { |
362 | $tree_icon = null; // default icon (folder) |
||
363 | } |
||
364 | */ |
||
365 | |||
705 | daniel-mar | 366 | $ic = /*empty($tree_icon) ? '' : */'<img src="'.$tree_icon.'" alt="">'; |
635 | daniel-mar | 367 | |
694 | daniel-mar | 368 | $out['text'] .= '<p><a href="'.htmlentities(self::getHyperlinkURL($realfile)).'" target="_blank">'.$ic.' '.htmlentities($this->getHyperlinkTitle($realfile)).'</a></p>'; |
635 | daniel-mar | 369 | $count++; |
370 | } else { |
||
801 | daniel-mar | 371 | $tree_icon = OIDplus::webpath(__DIR__,OIDplus::PATH_RELATIVE).'show_icon.php?mode=leaf_doc_icon16&lang='.OIDplus::getCurrentLang().'&file='.urlencode($file); |
635 | daniel-mar | 372 | /* |
373 | $icon_candidate = pathinfo($realfile)['dirname'].'/'.pathinfo($realfile)['filename'].'_tree.png'; |
||
374 | if (file_exists($icon_candidate)) { |
||
375 | $tree_icon = $icon_candidate; |
||
800 | daniel-mar | 376 | } else if (file_exists(__DIR__.'/img/leaf_doc_icon16.png')) { |
801 | daniel-mar | 377 | $tree_icon = OIDplus::webpath(__DIR__,OIDplus::PATH_RELATIVE).'img/leaf_doc_icon16.png'; |
635 | daniel-mar | 378 | } else { |
379 | $tree_icon = null; // default icon (folder) |
||
380 | } |
||
381 | */ |
||
382 | |||
705 | daniel-mar | 383 | $ic = /*empty($tree_icon) ? '' : */'<img src="'.$tree_icon.'" alt="">'; |
635 | daniel-mar | 384 | |
385 | $out['text'] .= '<p><a '.OIDplus::gui()->link('oidplus:resources$'.$file).'>'.$ic.' '.htmlentities($this->getDocumentTitle($file)).'</a></p>'; |
||
386 | $count++; |
||
387 | } |
||
388 | } |
||
389 | |||
390 | if ($count == 0) { |
||
391 | $out['text'] .= '<p>'._L('This folder does not contain any elements').'</p>'; |
||
392 | } |
||
393 | } else { |
||
394 | $out['title'] = _L('Not found'); |
||
800 | daniel-mar | 395 | $out['icon'] = 'img/error.png'; |
635 | daniel-mar | 396 | $out['text'] = '<p>'._L('This resource doesn\'t exist anymore.').'</p>'; |
397 | } |
||
398 | } |
||
399 | } |
||
400 | |||
401 | private function tree_rec(&$children, $rootdir=null, $depth=0) { |
||
402 | if (is_null($rootdir)) $rootdir = ''; |
||
403 | if ($depth > 100) return false; // something is wrong! |
||
404 | |||
405 | $dirs = self::myglob($rootdir.'*'.'/', true); |
||
406 | natcasesort($dirs); |
||
407 | foreach ($dirs as $dir) { |
||
408 | $tmp = array(); |
||
409 | |||
410 | $this->tree_rec($tmp, $dir, $depth+1); |
||
411 | |||
412 | $realdir = self::realname($dir); |
||
413 | |||
801 | daniel-mar | 414 | $tree_icon = OIDplus::webpath(__DIR__,OIDplus::PATH_RELATIVE).'show_icon.php?mode=folder_icon16&lang='.OIDplus::getCurrentLang().'&file='.urlencode($dir); |
635 | daniel-mar | 415 | /* |
416 | $icon_candidate = pathinfo($realdir)['dirname'].'/'.pathinfo($realdir)['filename'].'_tree.png'; |
||
417 | if (file_exists($icon_candidate)) { |
||
418 | $tree_icon = $icon_candidate; |
||
800 | daniel-mar | 419 | } else if (file_exists(__DIR__.'/img/folder_icon16.png')) { |
801 | daniel-mar | 420 | $tree_icon = OIDplus::webpath(__DIR__,OIDplus::PATH_RELATIVE).'img/folder_icon16.png'; |
635 | daniel-mar | 421 | } else { |
422 | $tree_icon = null; // default icon (folder) |
||
423 | } |
||
424 | */ |
||
425 | |||
426 | $children[] = array( |
||
427 | 'id' => 'oidplus:resources$'.$dir, |
||
428 | 'icon' => $tree_icon, |
||
429 | 'text' => self::getFolderTitle($realdir), |
||
430 | 'children' => $tmp, |
||
431 | 'state' => array("opened" => $depth <= OIDplus::config()->getValue('resource_plugin_autoopen_level', 1)-1) |
||
432 | ); |
||
433 | } |
||
434 | |||
435 | $files = array_merge( |
||
436 | self::myglob($rootdir.'*.htm'), // TODO: Also PHP? |
||
437 | self::myglob($rootdir.'*.html'), |
||
438 | self::myglob($rootdir.'*.url'), |
||
439 | self::myglob($rootdir.'*.link') |
||
440 | ); |
||
441 | natcasesort($files); |
||
442 | foreach ($files as $file) { |
||
443 | $realfile = self::realname($file); |
||
444 | if ((substr($file,-4,4) == '.url') || (substr($file,-5,5) == '.link')) { |
||
801 | daniel-mar | 445 | $tree_icon = OIDplus::webpath(__DIR__,OIDplus::PATH_RELATIVE).'show_icon.php?mode=leaf_url_icon16&lang='.OIDplus::getCurrentLang().'&file='.urlencode($file); |
635 | daniel-mar | 446 | /* |
447 | $icon_candidate = pathinfo($realfile)['dirname'].'/'.pathinfo($realfile)['filename'].'_tree.png'; |
||
448 | if (file_exists($icon_candidate)) { |
||
449 | $tree_icon = $icon_candidate; |
||
800 | daniel-mar | 450 | } else if (file_exists(__DIR__.'/img/leaf_url_icon16.png')) { |
801 | daniel-mar | 451 | $tree_icon = OIDplus::webpath(__DIR__,OIDplus::PATH_RELATIVE).'img/leaf_url_icon16.png'; |
635 | daniel-mar | 452 | } else { |
453 | $tree_icon = null; // default icon (folder) |
||
454 | } |
||
455 | */ |
||
456 | |||
457 | $children[] = array( |
||
458 | 'id' => 'oidplus:resources$'.$file, |
||
459 | 'conditionalselect' => 'window.open('.js_escape(self::getHyperlinkURL($realfile)).'); false;', |
||
460 | 'icon' => $tree_icon, |
||
694 | daniel-mar | 461 | 'text' => $this->getHyperlinkTitle($realfile), |
635 | daniel-mar | 462 | 'state' => array("opened" => $depth <= OIDplus::config()->getValue('resource_plugin_autoopen_level', 1)-1), |
463 | 'a_attr' => array( |
||
464 | 'href' => self::getHyperlinkURL($realfile), |
||
465 | 'target' => '_blank' |
||
466 | ) |
||
467 | ); |
||
468 | } else { |
||
801 | daniel-mar | 469 | $tree_icon = OIDplus::webpath(__DIR__,OIDplus::PATH_RELATIVE).'show_icon.php?mode=leaf_doc_icon16&lang='.OIDplus::getCurrentLang().'&file='.urlencode($file); |
635 | daniel-mar | 470 | /* |
471 | $icon_candidate = pathinfo($realfile)['dirname'].'/'.pathinfo($realfile)['filename'].'_tree.png'; |
||
472 | if (file_exists($icon_candidate)) { |
||
473 | $tree_icon = $icon_candidate; |
||
800 | daniel-mar | 474 | } else if (file_exists(__DIR__.'/img/leaf_doc_icon16.png')) { |
801 | daniel-mar | 475 | $tree_icon = OIDplus::webpath(__DIR__,OIDplus::PATH_RELATIVE).'img/leaf_doc_icon16.png'; |
635 | daniel-mar | 476 | } else { |
477 | $tree_icon = null; // default icon (folder) |
||
478 | } |
||
479 | */ |
||
480 | $children[] = array( |
||
481 | 'id' => 'oidplus:resources$'.$file, |
||
482 | 'icon' => $tree_icon, |
||
483 | 'text' => $this->getDocumentTitle($file), |
||
484 | 'state' => array("opened" => $depth <= OIDplus::config()->getValue('resource_plugin_autoopen_level', 1)-1) |
||
485 | ); |
||
486 | } |
||
487 | } |
||
488 | } |
||
489 | |||
490 | private function publicSitemap_rec($json, &$out) { |
||
491 | foreach ($json as $x) { |
||
492 | if (isset($x['id']) && $x['id']) { |
||
493 | $out[] = $x['id']; |
||
494 | } |
||
495 | if (isset($x['children'])) { |
||
496 | $this->publicSitemap_rec($x['children'], $out); |
||
497 | } |
||
498 | } |
||
499 | } |
||
500 | |||
501 | public function publicSitemap(&$out) { |
||
502 | $json = array(); |
||
503 | $this->tree($json, null/*RA EMail*/, false/*HTML tree algorithm*/, true/*display all*/); |
||
504 | $this->publicSitemap_rec($json, $out); |
||
505 | } |
||
506 | |||
507 | public function tree(&$json, $ra_email=null, $nonjs=false, $req_goto='') { |
||
508 | $children = array(); |
||
509 | |||
510 | $this->tree_rec($children, '/'); |
||
511 | |||
512 | if (!OIDplus::config()->getValue('resource_plugin_hide_empty_path', true) || (count($children) > 0)) { |
||
800 | daniel-mar | 513 | if (file_exists(__DIR__.'/img/main_icon16.png')) { |
801 | daniel-mar | 514 | $tree_icon = OIDplus::webpath(__DIR__,OIDplus::PATH_RELATIVE).'img/main_icon16.png'; |
635 | daniel-mar | 515 | } else { |
516 | $tree_icon = null; // default icon (folder) |
||
517 | } |
||
518 | |||
519 | $json[] = array( |
||
520 | 'id' => 'oidplus:resources', |
||
521 | 'icon' => $tree_icon, |
||
522 | 'state' => array("opened" => true), |
||
523 | 'text' => $this->getMainTitle(), |
||
524 | 'children' => $children |
||
525 | ); |
||
526 | } |
||
527 | |||
528 | return true; |
||
529 | } |
||
530 | |||
531 | public function tree_search($request) { |
||
532 | return false; |
||
533 | } |
||
534 | |||
535 | private static function getHyperlinkTitle($file) { |
||
536 | $file2 = preg_replace('/\.([^.]+)$/', '$'.OIDplus::getCurrentLang().'.\1', $file); |
||
537 | if (file_exists($file2)) $file = $file2; |
||
538 | |||
539 | if (substr($file,-4,4) == '.url') { |
||
540 | return preg_replace('/\\.[^.\\s]{3,4}$/', '', basename($file)); |
||
541 | } else if (substr($file,-5,5) == '.link') { |
||
542 | /* |
||
543 | [Link] |
||
544 | Title=Report a bug |
||
1083 | daniel-mar | 545 | URL=https://github.com/danielmarschall/oidplus/issues |
635 | daniel-mar | 546 | */ |
547 | |||
548 | $data = @parse_ini_file($file, true); |
||
549 | if (!$data) { |
||
550 | throw new OIDplusException(_L('File %1 has an invalid INI format!',$file)); |
||
551 | } |
||
552 | if (!isset($data['Link'])) { |
||
553 | throw new OIDplusException(_L('Could not find "%1" section at %2','Link',$file)); |
||
554 | } |
||
555 | if (!isset($data['Link']['Title'])) { |
||
556 | throw new OIDplusException(_L('"%1" is missing in %2','Title',$file)); |
||
557 | } |
||
558 | return $data['Link']['Title']; |
||
559 | } else { |
||
560 | throw new OIDplusException(_L('Unexpected file extension for file %1',$file)); |
||
561 | } |
||
562 | } |
||
563 | |||
564 | private static function getHyperlinkURL($file) { |
||
565 | $file2 = preg_replace('/\.([^.]+)$/', '$'.OIDplus::getCurrentLang().'.\1', $file); |
||
566 | if (file_exists($file2)) $file = $file2; |
||
567 | |||
568 | if (substr($file,-4,4) == '.url') { |
||
569 | /* |
||
570 | [{000214A0-0000-0000-C000-000000000046}] |
||
571 | Prop3=19,2 |
||
572 | [InternetShortcut] |
||
573 | URL=http://www.example.com/ |
||
574 | IDList= |
||
575 | */ |
||
576 | |||
577 | $data = @parse_ini_file($file, true); |
||
578 | if (!$data) { |
||
579 | throw new OIDplusException(_L('File %1 has an invalid INI format!',$file)); |
||
580 | } |
||
581 | if (!isset($data['InternetShortcut'])) { |
||
582 | throw new OIDplusException(_L('Could not find "%1" section at %2','InternetShortcut',$file)); |
||
583 | } |
||
584 | if (!isset($data['InternetShortcut']['URL'])) { |
||
585 | throw new OIDplusException(_L('"%1" is missing in %2','URL',$file)); |
||
586 | } |
||
587 | return $data['InternetShortcut']['URL']; |
||
588 | } else if (substr($file,-5,5) == '.link') { |
||
589 | /* |
||
590 | [Link] |
||
591 | Title=Report a bug |
||
1083 | daniel-mar | 592 | URL=https://github.com/danielmarschall/oidplus/issues |
635 | daniel-mar | 593 | */ |
594 | |||
595 | $data = @parse_ini_file($file, true); |
||
596 | if (!$data) { |
||
597 | throw new OIDplusException(_L('File %1 has an invalid INI format!',$file)); |
||
598 | } |
||
599 | if (!isset($data['Link'])) { |
||
600 | throw new OIDplusException(_L('Could not find "%1" section at %2','Link',$file)); |
||
601 | } |
||
602 | if (!isset($data['Link']['URL'])) { |
||
603 | throw new OIDplusException(_L('"%1" is missing in %2','URL',$file)); |
||
604 | } |
||
605 | return $data['Link']['URL']; |
||
606 | } else { |
||
607 | throw new OIDplusException(_L('Unexpected file extension for file %1',$file)); |
||
608 | } |
||
609 | |||
610 | } |
||
611 | |||
612 | private static function getFolderTitle($dir) { |
||
613 | $data = @parse_ini_file("$dir/folder\$".OIDplus::getCurrentLang().".ini", true); |
||
614 | if ($data && isset($data['Folder']) && isset($data['Folder']['Title'])) { |
||
615 | return $data['Folder']['Title']; |
||
616 | } |
||
617 | |||
618 | $data = @parse_ini_file("$dir/folder.ini", true); |
||
619 | if ($data && isset($data['Folder']) && isset($data['Folder']['Title'])) { |
||
620 | return $data['Folder']['Title']; |
||
621 | } |
||
622 | |||
623 | return basename($dir); |
||
624 | } |
||
625 | } |