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