Subversion Repositories oidplus

Rev

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