Subversion Repositories oidplus

Rev

Rev 475 | Go to most recent revision | Blame | Last modification | View Log | RSS feed

  1. <?php
  2.  
  3. /*
  4.  * OIDplus 2.0
  5.  * Copyright 2020 Daniel Marschall, ViaThinkSoft
  6.  *
  7.  * Licensed under the Apache License, Version 2.0 (the "License");
  8.  * you may not use this file except in compliance with the License.
  9.  * You may obtain a copy of the License at
  10.  *
  11.  *     http://www.apache.org/licenses/LICENSE-2.0
  12.  *
  13.  * Unless required by applicable law or agreed to in writing, software
  14.  * distributed under the License is distributed on an "AS IS" BASIS,
  15.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16.  * See the License for the specific language governing permissions and
  17.  * limitations under the License.
  18.  */
  19.  
  20. class OIDplusPagePublicResources extends OIDplusPagePluginPublic {
  21.  
  22.         private function getMainTitle() {
  23.                 return _L('Documents and Resources');
  24.         }
  25.  
  26.         public function init($html=true) {
  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) {
  28.                         if (!is_numeric($value) || ($value < 0)) {
  29.                                 throw new OIDplusException(_L('Please enter a valid value.'));
  30.                         }
  31.                 });
  32.                 OIDplus::config()->deleteConfigKey('resource_plugin_title');
  33.                 OIDplus::config()->deleteConfigKey('resource_plugin_path');
  34.                 OIDplus::config()->prepareConfigKey('resource_plugin_hide_empty_path','Resource plugin: Hide empty paths? (0=no, 1=yes)', '1', OIDplusConfig::PROTECTION_EDITABLE, function($value) {
  35.                         if (!is_numeric($value) || (($value != 0) && ($value != 1))) {
  36.                                 throw new OIDplusException(_L('Please enter a valid value (0=no, 1=yes).'));
  37.                         }
  38.                 });
  39.         }
  40.  
  41.         private static function getDocumentContent($file) {
  42.                 $file = rtrim(OIDplus::basePath(),'/').'/'.self::realname($file);
  43.                 $file2 = preg_replace('/\.([^.]+)$/', '$'.OIDplus::getCurrentLang().'.\1', $file);
  44.                 if (file_exists($file2)) $file = $file2;
  45.  
  46.                 $cont = file_get_contents($file);
  47.  
  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;
  53.  
  54.                 return $cont;
  55.         }
  56.  
  57.         private static function getDocumentTitle($file) {
  58.                 $file = rtrim(OIDplus::basePath(),'/').'/'.self::realname($file);
  59.                 $file2 = preg_replace('/\.([^.]+)$/', '$'.OIDplus::getCurrentLang().'.\1', $file);
  60.                 if (file_exists($file2)) $file = $file2;
  61.  
  62.                 $cont = file_get_contents($file);
  63.  
  64.                 // make sure the program works even if the user provided HTML is not UTF-8
  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.  
  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::basePath().'/userdata/resources/security.ini',
  85.                         OIDplus::basePath().'/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('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::basePath().'/userdata/resources/';
  113.                 $res = $onlydir ? glob($root.ltrim($reldir,'/'), GLOB_ONLYDIR) : glob($root.ltrim($reldir,'/'));
  114.                 foreach ($res as &$x) {
  115.                         $x = substr($x, strlen($root));
  116.                         if (strpos($x,'$') !== false) continue;
  117.                         $out[] = $x;
  118.                 }
  119.  
  120.                 $root = OIDplus::basePath().'/res/';
  121.                 $res = $onlydir ? glob($root.ltrim($reldir,'/'), GLOB_ONLYDIR) : glob($root.ltrim($reldir,'/'));
  122.                 foreach ($res as $x) {
  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::basePath().'/userdata/resources/'.$rel;
  137.                 $candidate2 = OIDplus::basePath().'/res/'.$rel;
  138.                 if (file_exists($candidate1) || is_dir($candidate1)) return "userdata/resources/$rel";
  139.                 if (file_exists($candidate2) || is_dir($candidate2)) return "res/$rel";
  140.         }
  141.        
  142.         protected static function checkRedirect($source, &$target): bool {
  143.                 $candidates = array(
  144.                         OIDplus::basePath().'/userdata/resources/redirect.ini',
  145.                         OIDplus::basePath().'/res/redirect.ini'
  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.         }
  158.  
  159.         public function gui($id, &$out, &$handled) {
  160.                 if (explode('$',$id,2)[0] === 'oidplus:resources') {
  161.                         $handled = true;
  162.  
  163.                         $file = @explode('$',$id)[1];
  164.  
  165.                         // Security checks
  166.  
  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.                                 }
  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')");
  179.                                 $out['title'] = _L('Access denied');
  180.                                 $out['icon'] = 'img/error_big.png';
  181.                                 $out['text'] = '<p>'._L('This request is invalid').'</p>';
  182.                                 return;
  183.                         }
  184.                        
  185.                         $out['text'] = '';
  186.                        
  187.                         // Redirections
  188.                        
  189.                         if ($file != '') {
  190.                                 $target = null;
  191.                                 if (self::checkRedirect($file, $target)) {
  192.                                         $out['title'] = _L('Please wait...');
  193.                                         $out['text'] = '<p>'._L('You are being redirected...').'</p><script>window.location.href = '.js_escape($target).';</script>';
  194.                                         return;
  195.                                 }
  196.                         }
  197.  
  198.                         // Check for permission
  199.  
  200.                         if ($file != '') {
  201.                                 if (!self::mayAccessResource($file)) {
  202.                                         $out['title'] = _L('Access denied');
  203.                                         $out['icon'] = 'img/error_big.png';
  204.                                         $out['text'] = '<p>'._L('Authentication error. Please log in.').'</p>';
  205.                                         return;
  206.                                 }
  207.                         }
  208.                        
  209.                         // First, "Go back to" line
  210.  
  211.                         if ($file != '') {
  212.                                 $dir = dirname($file);
  213.  
  214.                                 if ($dir == '.') {
  215.                                         if (file_exists(__DIR__.'/treeicon.png')) {
  216.                                                 $tree_icon = OIDplus::webpath(__DIR__).'treeicon.png';
  217.                                         } else {
  218.                                                 $tree_icon = null; // default icon (folder)
  219.                                         }
  220.  
  221.                                         $ic = empty($tree_icon) ? '' : '<img src="'.$tree_icon.'" alt="">';
  222.  
  223.                                         $lng_gobackto = _L('Go back to').':';
  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>';
  225.                                 } else {
  226.                                         $realdir = self::realname($dir);
  227.  
  228.                                         $tree_icon = OIDplus::webpath(__DIR__).'show_icon.php?mode=treeicon_folder&lang='.OIDplus::getCurrentLang().'&file='.urlencode($dir);
  229.                                         /*
  230.                                         $icon_candidate = pathinfo($realdir)['dirname'].'/'.pathinfo($realdir)['filename'].'_tree.png';
  231.                                         if (file_exists($icon_candidate)) {
  232.                                                 $tree_icon = $icon_candidate;
  233.                                         } else if (file_exists(__DIR__.'/treeicon_folder.png')) {
  234.                                                 $tree_icon = OIDplus::webpath(__DIR__).'treeicon_folder.png';
  235.                                         } else {
  236.                                                 $tree_icon = null; // no icon
  237.                                         }
  238.                                         */
  239.  
  240.                                         $ic = empty($tree_icon) ? '' : '<img src="'.$tree_icon.'" alt="">';
  241.  
  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>';
  243.                                 }
  244.                         }
  245.  
  246.                         // Then the content
  247.  
  248.                         $realfile = self::realname($file);
  249.                         // $realfile2 = preg_replace('/\.([^.]+)$/', '$'.OIDplus::getCurrentLang().'.\1', $realfile);
  250.                         // if (file_exists($realfile2)) $realfile = $realfile2;
  251.                        
  252.                         if (file_exists($realfile) && (!is_dir($realfile))) {
  253.                                 if ((substr($file,-4,4) == '.url') || (substr($file,-5,5) == '.link')) {
  254.                                         $out['title'] = $this->getHyperlinkTitle($realfile);
  255.  
  256.                                         $out['icon'] = OIDplus::webpath(__DIR__).'show_icon.php?mode=icon_leaf_url_big&lang='.OIDplus::getCurrentLang().'&file='.urlencode($file);
  257.                                         /*
  258.                                         $icon_candidate = pathinfo($realfile)['dirname'].'/'.pathinfo($realfile)['filename'].'_big.png';
  259.                                         if (file_exists($icon_candidate)) {
  260.                                                 $out['icon'] = $icon_candidate;
  261.                                         } else if (file_exists(__DIR__.'/icon_leaf_url_big.png')) {
  262.                                                 $out['icon'] = OIDplus::webpath(__DIR__).'icon_leaf_url_big.png';
  263.                                         } else {
  264.                                                 $out['icon'] = '';
  265.                                         }
  266.                                         */
  267.  
  268.                                         // Should not happen though, due to conditionalselect
  269.                                         $out['text'] .= '<a href="'.htmlentities(self::getHyperlinkURL($realfile)).'" target="_blank">'._L('Open in new window').'</a>';
  270.                                 } else if ((substr($file,-4,4) == '.htm') || (substr($file,-5,5) == '.html')) {
  271.                                         $out['title'] = $this->getDocumentTitle($file);
  272.  
  273.                                         $out['icon'] = OIDplus::webpath(__DIR__).'show_icon.php?mode=icon_leaf_doc_big&lang='.OIDplus::getCurrentLang().'&file='.urlencode($file);
  274.                                         /*
  275.                                         $icon_candidate = pathinfo($realfile)['dirname'].'/'.pathinfo($realfile)['filename'].'_big.png';
  276.                                         if (file_exists($icon_candidate)) {
  277.                                                 $out['icon'] = $icon_candidate;
  278.                                         } else if (file_exists(__DIR__.'/icon_leaf_doc_big.png')) {
  279.                                                 $out['icon'] = OIDplus::webpath(__DIR__).'icon_leaf_doc_big.png';
  280.                                         } else {
  281.                                                 $out['icon'] = '';
  282.                                         }
  283.                                         */
  284.  
  285.                                         $out['text'] .= self::getDocumentContent($file);
  286.                                 } else {
  287.                                         $out['title'] = _L('Unknown file type');
  288.                                         $out['icon'] = 'img/error_big.png';
  289.                                         $out['text'] = '<p>'._L('The system does not know how to handle this file type.').'</p>';
  290.                                         return;
  291.                                 }
  292.                         } else if (is_dir($realfile)) {
  293.                                 $out['title'] = ($file == '') ? $this->getMainTitle() : self::getFolderTitle($realfile);
  294.  
  295.                                 if ($file == '') {
  296.                                         $out['icon'] = file_exists(__DIR__.'/icon_big.png') ? OIDplus::webpath(__DIR__).'icon_big.png' : '';
  297.                                 } else {
  298.                                         $out['icon'] = OIDplus::webpath(__DIR__).'show_icon.php?mode=icon_folder_big&lang='.OIDplus::getCurrentLang().'&file='.urlencode($file);
  299.                                         /*
  300.                                         $icon_candidate = pathinfo($realfile)['dirname'].'/'.pathinfo($realfile)['filename'].'_big.png';
  301.                                         if (file_exists($icon_candidate)) {
  302.                                                 $out['icon'] = $icon_candidate;
  303.                                         } else if (file_exists(__DIR__.'/icon_folder_big.png')) {
  304.                                                 $out['icon'] = OIDplus::webpath(__DIR__).'icon_folder_big.png';
  305.                                         } else {
  306.                                                 $out['icon'] = null; // no icon
  307.                                         }
  308.                                         */
  309.                                 }
  310.  
  311.                                 if (file_exists(__DIR__.'/treeicon.png')) {
  312.                                         $tree_icon = OIDplus::webpath(__DIR__).'treeicon.png';
  313.                                 } else {
  314.                                         $tree_icon = null; // default icon (folder)
  315.                                 }
  316.  
  317.                                 $count = 0;
  318.  
  319.                                 $dirs = self::myglob(rtrim($file,'/').'/'.'*', true);
  320.                                 natcasesort($dirs);
  321.                                 foreach ($dirs as $dir) {
  322.                                         $realdir = self::realname($dir);
  323.                                         $tree_icon = OIDplus::webpath(__DIR__).'show_icon.php?mode=treeicon_folder&lang='.OIDplus::getCurrentLang().'&file='.urlencode($dir);
  324.                                         /*
  325.                                         $icon_candidate = pathinfo($realdir)['dirname'].'/'.pathinfo($realdir)['filename'].'_tree.png';
  326.                                         if (file_exists($icon_candidate)) {
  327.                                                 $tree_icon = $icon_candidate;
  328.                                         } else if (file_exists(__DIR__.'/treeicon_folder.png')) {
  329.                                                 $tree_icon = OIDplus::webpath(__DIR__).'treeicon_folder.png';
  330.                                         } else {
  331.                                                 $tree_icon = null; // no icon
  332.                                         }
  333.                                         */
  334.  
  335.                                         $ic = empty($tree_icon) ? '' : '<img src="'.$tree_icon.'" alt="">';
  336.  
  337.                                         $out['text'] .= '<p><a '.OIDplus::gui()->link('oidplus:resources$'.rtrim($dir,'/').'/').'>'.$ic.' '.htmlentities(self::getFolderTitle($realdir)).'</a></p>';
  338.                                         $count++;
  339.                                 }
  340.  
  341.                                 $files = array_merge(
  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')
  346.                                 );
  347.                                 natcasesort($files);
  348.                                 foreach ($files as $file) {
  349.                                         $realfile = self::realname($file);
  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);
  352.                                                 /*
  353.                                                 $icon_candidate = pathinfo($realfile)['dirname'].'/'.pathinfo($realfile)['filename'].'_tree.png';
  354.                                                 if (file_exists($icon_candidate)) {
  355.                                                         $tree_icon = $icon_candidate;
  356.                                                 } else if (file_exists(__DIR__.'/treeicon_leaf_url.png')) {
  357.                                                         $tree_icon = OIDplus::webpath(__DIR__).'treeicon_leaf_url.png';
  358.                                                 } else {
  359.                                                         $tree_icon = null; // default icon (folder)
  360.                                                 }
  361.                                                 */
  362.  
  363.                                                 $ic = empty($tree_icon) ? '' : '<img src="'.$tree_icon.'" alt="">';
  364.  
  365.                                                 $hyperlink_pic = ' <img src="'.OIDplus::webpath(__DIR__).'hyperlink.png" widht="13" height="13" alt="Hyperlink" style="top:-3px;position:relative">';
  366.  
  367.                                                 $out['text'] .= '<p><a href="'.htmlentities(self::getHyperlinkURL($realfile)).'" target="_blank">'.$ic.' '.htmlentities($this->getHyperlinkTitle($realfile)).' '.$hyperlink_pic.'</a></p>';
  368.                                                 $count++;
  369.                                         } else {
  370.                                                 $tree_icon = OIDplus::webpath(__DIR__).'show_icon.php?mode=treeicon_leaf_doc&lang='.OIDplus::getCurrentLang().'&file='.urlencode($file);
  371.                                                 /*
  372.                                                 $icon_candidate = pathinfo($realfile)['dirname'].'/'.pathinfo($realfile)['filename'].'_tree.png';
  373.                                                 if (file_exists($icon_candidate)) {
  374.                                                         $tree_icon = $icon_candidate;
  375.                                                 } else if (file_exists(__DIR__.'/treeicon_leaf_doc.png')) {
  376.                                                         $tree_icon = OIDplus::webpath(__DIR__).'treeicon_leaf_doc.png';
  377.                                                 } else {
  378.                                                         $tree_icon = null; // default icon (folder)
  379.                                                 }
  380.                                                 */
  381.  
  382.                                                 $ic = empty($tree_icon) ? '' : '<img src="'.$tree_icon.'" alt="">';
  383.  
  384.                                                 $out['text'] .= '<p><a '.OIDplus::gui()->link('oidplus:resources$'.$file).'>'.$ic.' '.htmlentities($this->getDocumentTitle($file)).'</a></p>';
  385.                                                 $count++;
  386.                                         }
  387.                                 }
  388.  
  389.                                 if ($count == 0) {
  390.                                         $out['text'] .= '<p>'._L('This folder does not contain any elements').'</p>';
  391.                                 }
  392.                         } else {
  393.                                 $out['title'] = _L('Not found');
  394.                                 $out['icon'] = 'img/error_big.png';
  395.                                 $out['text'] = '<p>'._L('This resource doesn\'t exist anymore.').'</p>';
  396.                         }
  397.                 }
  398.         }
  399.  
  400.         private function tree_rec(&$children, $rootdir=null, $depth=0) {
  401.                 if (is_null($rootdir)) $rootdir = '';
  402.                 if ($depth > 100) return false; // something is wrong!
  403.  
  404.                 $dirs = self::myglob($rootdir.'*'.'/', true);
  405.                 natcasesort($dirs);
  406.                 foreach ($dirs as $dir) {
  407.                         $tmp = array();
  408.  
  409.                         $this->tree_rec($tmp, $dir, $depth+1);
  410.  
  411.                         $realdir = self::realname($dir);
  412.  
  413.                         $tree_icon = OIDplus::webpath(__DIR__).'show_icon.php?mode=treeicon_folder&lang='.OIDplus::getCurrentLang().'&file='.urlencode($dir);
  414.                         /*
  415.                         $icon_candidate = pathinfo($realdir)['dirname'].'/'.pathinfo($realdir)['filename'].'_tree.png';
  416.                         if (file_exists($icon_candidate)) {
  417.                                 $tree_icon = $icon_candidate;
  418.                         } else if (file_exists(__DIR__.'/treeicon_folder.png')) {
  419.                                 $tree_icon = OIDplus::webpath(__DIR__).'treeicon_folder.png';
  420.                         } else {
  421.                                 $tree_icon = null; // default icon (folder)
  422.                         }
  423.                         */
  424.  
  425.                         $children[] = array(
  426.                                 'id' => 'oidplus:resources$'.$dir,
  427.                                 'icon' => $tree_icon,
  428.                                 'text' => self::getFolderTitle($realdir),
  429.                                 'children' => $tmp,
  430.                                 'state' => array("opened" => $depth <= OIDplus::config()->getValue('resource_plugin_autoopen_level', 1)-1)
  431.                         );
  432.                 }
  433.  
  434.                 $files = array_merge(
  435.                         self::myglob($rootdir.'*.htm'), // TODO: Also PHP?
  436.                         self::myglob($rootdir.'*.html'),
  437.                         self::myglob($rootdir.'*.url'),
  438.                         self::myglob($rootdir.'*.link')
  439.                 );
  440.                 natcasesort($files);
  441.                 foreach ($files as $file) {
  442.                         $realfile = self::realname($file);
  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);
  445.                                 /*
  446.                                 $icon_candidate = pathinfo($realfile)['dirname'].'/'.pathinfo($realfile)['filename'].'_tree.png';
  447.                                 if (file_exists($icon_candidate)) {
  448.                                         $tree_icon = $icon_candidate;
  449.                                 } else if (file_exists(__DIR__.'/treeicon_leaf_url.png')) {
  450.                                         $tree_icon = OIDplus::webpath(__DIR__).'treeicon_leaf_url.png';
  451.                                 } else {
  452.                                         $tree_icon = null; // default icon (folder)
  453.                                 }
  454.                                 */
  455.  
  456.                                 $hyperlink_pic = ' <img src="'.OIDplus::webpath(__DIR__).'hyperlink.png" widht="13" height="13" alt="Hyperlink" style="top:-3px;position:relative">';
  457.  
  458.                                 $children[] = array(
  459.                                         'id' => 'oidplus:resources$'.$file,
  460.                                         'conditionalselect' => 'window.open('.js_escape(self::getHyperlinkURL($realfile)).'); false;',
  461.                                         'icon' => $tree_icon,
  462.                                         'text' => $this->getHyperlinkTitle($realfile).' '.$hyperlink_pic,
  463.                                         'state' => array("opened" => $depth <= OIDplus::config()->getValue('resource_plugin_autoopen_level', 1)-1)
  464.                                 );
  465.                         } else {
  466.                                 $tree_icon = OIDplus::webpath(__DIR__).'show_icon.php?mode=treeicon_leaf_doc&lang='.OIDplus::getCurrentLang().'&file='.urlencode($file);
  467.                                 /*
  468.                                 $icon_candidate = pathinfo($realfile)['dirname'].'/'.pathinfo($realfile)['filename'].'_tree.png';
  469.                                 if (file_exists($icon_candidate)) {
  470.                                         $tree_icon = $icon_candidate;
  471.                                 } else if (file_exists(__DIR__.'/treeicon_leaf_doc.png')) {
  472.                                         $tree_icon = OIDplus::webpath(__DIR__).'treeicon_leaf_doc.png';
  473.                                 } else {
  474.                                         $tree_icon = null; // default icon (folder)
  475.                                 }
  476.                                 */
  477.                                 $children[] = array(
  478.                                         'id' => 'oidplus:resources$'.$file,
  479.                                         'icon' => $tree_icon,
  480.                                         'text' => $this->getDocumentTitle($file),
  481.                                         'state' => array("opened" => $depth <= OIDplus::config()->getValue('resource_plugin_autoopen_level', 1)-1)
  482.                                 );
  483.                         }
  484.                 }
  485.         }
  486.  
  487.         private function publicSitemap_rec($json, &$out) {
  488.                 foreach ($json as $x) {
  489.                         if (isset($x['id']) && $x['id']) {
  490.                                 $out[] = $x['id'];
  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.  
  504.         public function tree(&$json, $ra_email=null, $nonjs=false, $req_goto='') {
  505.                 $children = array();
  506.  
  507.                 $this->tree_rec($children, '/');
  508.  
  509.                 if (!OIDplus::config()->getValue('resource_plugin_hide_empty_path', true) || (count($children) > 0)) {
  510.                         if (file_exists(__DIR__.'/treeicon.png')) {
  511.                                 $tree_icon = OIDplus::webpath(__DIR__).'treeicon.png';
  512.                         } else {
  513.                                 $tree_icon = null; // default icon (folder)
  514.                         }
  515.  
  516.                         $json[] = array(
  517.                                 'id' => 'oidplus:resources',
  518.                                 'icon' => $tree_icon,
  519.                                 'state' => array("opened" => true),
  520.                                 'text' => $this->getMainTitle(),
  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) {
  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.                 }
  559.         }
  560.  
  561.         private static function getHyperlinkURL($file) {
  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.  
  607.         }
  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.         }
  622. }
  623.