Subversion Repositories oidplus

Rev

Rev 499 | Rev 541 | Go to most recent revision | Blame | Compare with Previous | 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 OIDplusMenuUtils {
  23.  
  24.         public static function nonjs_menu() {
  25.                 $json = array();
  26.  
  27.                 $static_node_id = isset($_REQUEST['goto']) ? $_REQUEST['goto'] : 'oidplus:system';
  28.  
  29.                 foreach (OIDplus::getPagePlugins() as $plugin) {
  30.                         // Note: The system (OIDplusMenuUtils) does only show the menu of
  31.                         //       publicPage plugins. Menu entries for RAs and Admins are
  32.                         //       handled by the tree() function of the plugin publicPages/090_login
  33.                         if (is_subclass_of($plugin, OIDplusPagePluginPublic::class)) {
  34.                                 $plugin->tree($json, null, true, $static_node_id);
  35.                         }
  36.                 }
  37.  
  38.                 foreach ($json as $x) {
  39.                         if ($static_node_id == $x['id']) echo '<b>';
  40.                         if (isset($x['indent'])) echo str_repeat('&nbsp;', $x['indent']*5);
  41.                         $cur_lang = OIDplus::getCurrentLang();
  42.                         if ($cur_lang != OIDplus::DEFAULT_LANGUAGE) {
  43.                                 echo '<a href="?lang='.$cur_lang.'&amp;goto='.urlencode($x['id']).'">';
  44.                         } else {
  45.                                 echo '<a href="?goto='.urlencode($x['id']).'">';
  46.                         }
  47.                         if (!empty($x['icon'])) echo '<img src="'.$x['icon'].'" alt=""> ';
  48.                         echo htmlentities($x['id']).' | '.htmlentities($x['text']).'</a><br>';
  49.                         if ($static_node_id == $x['id']) echo '</b>';
  50.                 }
  51.  
  52.         }
  53.  
  54.         // req_id comes from jsTree via AJAX
  55.         // req_goto comes from the user (GET argument)
  56.         public static function json_tree($req_id, $req_goto) {
  57.                 $json = array();
  58.  
  59.                 if (!isset($req_id) || ($req_id == '#')) {
  60.                         foreach (OIDplus::getPagePlugins() as $plugin) {
  61.                                 // Note: The system (OIDplusMenuUtils) does only show the menu of
  62.                                 //       publicPage plugins. Menu entries for RAs and Admins are
  63.                                 //       handled by the tree() function of the plugin publicPages/090_login
  64.                                 if (is_subclass_of($plugin, OIDplusPagePluginPublic::class)) {
  65.                                         $plugin->tree($json, null, false, $req_goto);
  66.                                 }
  67.                         }
  68.                 } else {
  69.                         $json = self::tree_populate($req_id);
  70.                 }
  71.  
  72.                 return $json;
  73.         }
  74.  
  75.         public static function tree_populate($parent, $goto_path=null) {
  76.                 $children = array();
  77.  
  78.                 $parentObj = OIDplusObject::parse($parent);
  79.  
  80.                 @list($namespace, $oid) = explode(':', $parent, 2);
  81.                 if ($namespace == 'oid') $oid = substr($oid, 1); // Remove leading dot
  82.  
  83.                 if (is_array($goto_path)) array_shift($goto_path);
  84.  
  85.                 $confidential_oids = array();
  86.  
  87.                 $res = OIDplus::db()->query("select id from ###objects where confidential = ?", array(true));
  88.                 while ($row = $res->fetch_array()) {
  89.                         $confidential_oids[] = $row['id'];
  90.                 }
  91.  
  92.                 $res = OIDplus::db()->query("select * from ###objects where parent = ? order by ".OIDplus::db()->natOrder('id'), array($parent));
  93.                 while ($row = $res->fetch_array()) {
  94.                         $obj = OIDplusObject::parse($row['id']);
  95.  
  96.                         if (!$obj->userHasReadRights()) continue;
  97.  
  98.                         $child = array();
  99.                         $child['id'] = $row['id'];
  100.  
  101.                         // Determine display name (relative OID)
  102.                         $child['text'] = $obj->jsTreeNodeName($parentObj);
  103.                         $child['text'] .= empty($row['title']) ? /*' -- <i>'.htmlentities('Title missing').'</i>'*/ '' : ' -- <b>' . htmlentities($row['title']) . '</b>';
  104.  
  105.                         $is_confidential = false;
  106.                         foreach ($confidential_oids as $test) {
  107.                                 $is_confidential |= ($row['id'] === $test) || (strpos($row['id'],$test.'.') === 0);
  108.                         }
  109.                         if ($is_confidential) {
  110.                                 $child['text'] = '<font color="gray"><i>'.$child['text'].'</i></font>';
  111.                         }
  112.  
  113.                         // Determine icon
  114.                         $child['icon'] = $obj->getIcon($row);
  115.  
  116.                         // Check if there are more sub OIDs
  117.                         if ($goto_path === true) {
  118.                                 $child['children'] = self::tree_populate($row['id'], $goto_path);
  119.                                 $child['state'] = array("opened" => true);
  120.                         } else if (!is_null($goto_path) && (count($goto_path) > 0) && ($goto_path[0] === $row['id'])) {
  121.                                 $child['children'] = self::tree_populate($row['id'], $goto_path);
  122.                                 $child['state'] = array("opened" => true);
  123.                         } else {
  124.                                 $obj_children = $obj->getChildren();
  125.  
  126.                                 // Variant 1: Fast, but does not check for hidden OIDs
  127.                                 //$child_count = count($obj_children);
  128.  
  129.                                 // variant 2
  130.                                 $child_count = 0;
  131.                                 foreach ($obj_children as $obj_test) {
  132.                                         if (!$obj_test->userHasReadRights()) continue;
  133.                                         $child_count++;
  134.                                 }
  135.  
  136.                                 $child['children'] = $child_count > 0;
  137.                         }
  138.  
  139.                         $children[] = $child;
  140.                 }
  141.  
  142.                 return $children;
  143.         }
  144. }
  145.