Subversion Repositories oidplus

Rev

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