Subversion Repositories oidplus

Rev

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

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