Subversion Repositories oidplus

Rev

Rev 1206 | 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 OIDplusPageAdminListRAs extends OIDplusPagePluginAdmin {
  27.  
  28.         /**
  29.          * @param bool $html
  30.          * @return void
  31.          */
  32.         public function init(bool $html=true) {
  33.                 // Nothing
  34.         }
  35.  
  36.         /**
  37.          * @return array
  38.          * @throws OIDplusConfigInitializationException
  39.          * @throws OIDplusException
  40.          */
  41.         private function get_ralist(): array {
  42.                 $tmp = array();
  43.                 if (OIDplus::db()->getSlang()->id() == 'mysql') {
  44.                         $res = OIDplus::db()->query("select distinct BINARY(email) as distinct_email from ###ra"); // "binary" because we want to ensure that 'distinct' is case sensitive
  45.                 } else {
  46.                         $res = OIDplus::db()->query("select distinct email as distinct_email from ###ra"); // distinct in PGSQL is always case sensitive
  47.                 }
  48.                 while ($row = $res->fetch_array()) {
  49.                         $tmp[$row['distinct_email']] = 1;
  50.                 }
  51.                 if (OIDplus::db()->getSlang()->id() == 'mysql') {
  52.                         $res = OIDplus::db()->query("select distinct BINARY(ra_email) as distinct_ra_email from ###objects");
  53.                 } else {
  54.                         $res = OIDplus::db()->query("select distinct ra_email as distinct_ra_email from ###objects");
  55.                 }
  56.                 while ($row = $res->fetch_array()) {
  57.                         if (!isset($tmp[$row['distinct_ra_email']])) {
  58.                                 $tmp[$row['distinct_ra_email']] = 0;
  59.                         } else {
  60.                                 $tmp[$row['distinct_ra_email']] = 2;
  61.                         }
  62.                 }
  63.                 ksort($tmp);
  64.  
  65.                 return $tmp;
  66.         }
  67.  
  68.         /**
  69.          * @param string $id
  70.          * @param array $out
  71.          * @param bool $handled
  72.          * @return void
  73.          * @throws OIDplusConfigInitializationException
  74.          * @throws OIDplusException
  75.          */
  76.         public function gui(string $id, array &$out, bool &$handled) {
  77.                 if ($id === 'oidplus:list_ra') {
  78.                         $handled = true;
  79.                         $out['title'] = _L('RA Listing');
  80.                         $out['icon'] = file_exists(__DIR__.'/img/main_icon.png') ? OIDplus::webpath(__DIR__,OIDplus::PATH_RELATIVE).'img/main_icon.png' : '';
  81.  
  82.                         if (!OIDplus::authUtils()->isAdminLoggedIn()) {
  83.                                 throw new OIDplusHtmlException(_L('You need to <a %1>log in</a> as administrator.',OIDplus::gui()->link('oidplus:login$admin')), $out['title'], 401);
  84.                         }
  85.  
  86.                         $out['text'] = '';
  87.  
  88.                         $tmp = $this->get_ralist();
  89.  
  90.                         $raCreatePlugin = OIDplus::getPluginByOid('1.3.6.1.4.1.37476.2.5.2.4.3.130'); // OIDplusPageAdminCreateRa
  91.                         if (!is_null($raCreatePlugin)) {
  92.                                 $out['text'] .= '<p><a '.OIDplus::gui()->link('oidplus:create_ra').'>'._L('Create a new RA manually').'</a></p>';
  93.                         }
  94.  
  95.                         if (count($tmp) == 0) {
  96.                                 $out['text'] .= '<p>'._L('Currently there are no Registration Authorities.').'</p>';
  97.                         }
  98.  
  99.                         foreach ($tmp as $ra_email => $registered) {
  100.                                 if (empty($ra_email)) {
  101.                                         $out['text'] .= '<p><b><a '.OIDplus::gui()->link('oidplus:rainfo$').'>'._L('(Objects with undefined RA)').'</a></b></p>';
  102.                                 } else {
  103.                                         if ($registered == 0) {
  104.                                                 $out['text'] .= '<p><b><a '.OIDplus::gui()->link('oidplus:rainfo$'.str_replace('@','&',$ra_email)).'>'.htmlentities($ra_email).'</a></b> '._L('(has objects, is not registered)').'</p>';
  105.                                         }
  106.                                         if ($registered == 1) {
  107.                                                 $out['text'] .= '<p><b><a '.OIDplus::gui()->link('oidplus:rainfo$'.str_replace('@','&',$ra_email)).'>'.htmlentities($ra_email).'</a></b> '._L('(registered, <font color="red">has no objects</font>)').'</p>';
  108.                                         }
  109.                                         if ($registered == 2) {
  110.                                                 $out['text'] .= '<p><b><a '.OIDplus::gui()->link('oidplus:rainfo$'.str_replace('@','&',$ra_email)).'>'.htmlentities($ra_email).'</a></b></p>';
  111.                                         }
  112.                                 }
  113.                         }
  114.                 }
  115.         }
  116.  
  117.         /**
  118.          * @param array $json
  119.          * @param string|null $ra_email
  120.          * @param bool $nonjs
  121.          * @param string $req_goto
  122.          * @return bool
  123.          * @throws OIDplusConfigInitializationException
  124.          * @throws OIDplusException
  125.          */
  126.         public function tree(array &$json, string $ra_email=null, bool $nonjs=false, string $req_goto=''): bool {
  127.                 if (!OIDplus::authUtils()->isAdminLoggedIn()) return false;
  128.  
  129.                 if (file_exists(__DIR__.'/img/main_icon16.png')) {
  130.                         $tree_icon = OIDplus::webpath(__DIR__,OIDplus::PATH_RELATIVE).'img/main_icon16.png';
  131.                 } else {
  132.                         $tree_icon = null; // default icon (folder)
  133.                 }
  134.  
  135.                 $children = array();
  136.                 $tmp = $this->get_ralist();
  137.                 foreach ($tmp as $ra_email => $registered) {
  138.                         if (empty($ra_email)) {
  139.                                 $children[] = array(
  140.                                         'id' => 'oidplus:rainfo$',
  141.                                         'icon' => $tree_icon,
  142.                                         'text' => _L('(Objects with undefined RA)')
  143.                                 );
  144.                         } else {
  145.                                 if ($registered == 0) {
  146.                                         $children[] = array(
  147.                                                 'id' => 'oidplus:rainfo$'.str_replace('@', '&', $ra_email),
  148.                                                 'icon' => $tree_icon,
  149.                                                 'text' => $ra_email.' <i>'._L('(has objects, is not registered)').'</i>'
  150.                                         );
  151.                                 }
  152.                                 if ($registered == 1) {
  153.                                         $children[] = array(
  154.                                                 'id' => 'oidplus:rainfo$'.$ra_email,
  155.                                                 'icon' => $tree_icon,
  156.                                                 'text' => $ra_email.' <i><font color="red">'._L('(has no objects)').'</font></i>'
  157.                                         );
  158.                                 }
  159.                                 if ($registered == 2) {
  160.                                         $children[] = array(
  161.                                                 'id' => 'oidplus:rainfo$'.$ra_email,
  162.                                                 'icon' => $tree_icon,
  163.                                                 'text' => $ra_email
  164.                                         );
  165.                                 }
  166.                         }
  167.                 }
  168.  
  169.                 $json[] = array(
  170.                         'id' => 'oidplus:list_ra',
  171.                         'icon' => $tree_icon,
  172.                         'text' => _L('List RAs'),
  173.                         'children' => $children
  174.                 );
  175.  
  176.                 return true;
  177.         }
  178.  
  179.         /**
  180.          * @param string $request
  181.          * @return array|false
  182.          */
  183.         public function tree_search(string $request) {
  184.                 // We don't need this, because the list of RAs is loaded without lazy-loading,
  185.                 // so the node does not need to be searched
  186.                 /*
  187.                 if (strpos($request, 'oidplus:rainfo$') === 0) {
  188.                         if (OIDplus::authUtils()->isAdminLoggedIn()) {
  189.                                 return array('oidplus:login', ...dummy..., 'oidplus:list_ra', $request);
  190.                         }
  191.                 }
  192.                 */
  193.                 return false;
  194.         }
  195. }
  196.