Subversion Repositories oidplus

Rev

Rev 1375 | Rev 1449 | 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 2022 - 2023 Daniel Marschall, ViaThinkSoft / Till Wehowski, Frdlweb
  6.  *
  7.  * Licensed under the MIT License.
  8.  */
  9.  
  10. namespace Frdlweb\OIDplus;
  11.  
  12. use ViaThinkSoft\OIDplus\INTF_OID_1_3_6_1_4_1_37476_2_5_2_3_4;
  13. use ViaThinkSoft\OIDplus\INTF_OID_1_3_6_1_4_1_37476_2_5_2_3_7;
  14. use ViaThinkSoft\OIDplus\OIDplus;
  15. use ViaThinkSoft\OIDplus\OIDplusObject;
  16. use ViaThinkSoft\OIDplus\OIDplusPagePluginPublic;
  17.  
  18. // phpcs:disable PSR1.Files.SideEffects
  19. \defined('INSIDE_OIDPLUS') or die;
  20. // phpcs:enable PSR1.Files.SideEffects
  21.  
  22. class OIDplusPagePublicAltIds extends OIDplusPagePluginPublic
  23.         implements INTF_OID_1_3_6_1_4_1_37476_2_5_2_3_4, /* whois*Attributes */
  24.                    INTF_OID_1_3_6_1_4_1_37476_2_5_2_3_7  /* getAlternativesForQuery */
  25. {
  26.  
  27.         /**
  28.          * @param string $actionID
  29.          * @param array $params
  30.          * @return array
  31.          * @throws \ViaThinkSoft\OIDplus\OIDplusException
  32.          */
  33.         public function action(string $actionID, array $params): array {
  34.                 return parent::action($actionID, $params);
  35.         }
  36.  
  37.         /**
  38.          * @param string $id
  39.          * @param array $out
  40.          * @param bool $handled
  41.          * @return void
  42.          */
  43.         public function gui(string $id, array &$out, bool &$handled) {
  44.  
  45.         }
  46.  
  47.         /**
  48.          * @param array $out
  49.          * @return void
  50.          */
  51.         public function publicSitemap(array &$out) {
  52.  
  53.         }
  54.  
  55.         /**
  56.          * @param array $json
  57.          * @param string|null $ra_email
  58.          * @param bool $nonjs
  59.          * @param string $req_goto
  60.          * @return bool
  61.          */
  62.         public function tree(array &$json, string $ra_email=null, bool $nonjs=false, string $req_goto=''): bool {
  63.                 return false;
  64.         }
  65.  
  66.         /**
  67.          * @return string|null
  68.          * @throws \ViaThinkSoft\OIDplus\OIDplusException
  69.          */
  70.         private function cache_id() {
  71.                 static $cache_id = null;
  72.                 if (!is_null($cache_id)) return $cache_id;
  73.                 $cache_id  =  'Create='.OIDplus::db()->getScalar("select max(created) as ts from ###objects where created is not null;");
  74.                 $cache_id .= '/Update='.OIDplus::db()->getScalar("select max(updated) as ts from ###objects where updated is not null;");
  75.                 $cache_id .= '/Count='.OIDplus::db()->getScalar("select count(id) as cnt from ###objects;");
  76.                 $plugin_versions = array();
  77.                 foreach (OIDplus::getObjectTypePluginsEnabled() as $otp) {
  78.                         $plugin_versions[] = '/'.$otp->getManifest()->getOid().'='.$otp->getManifest()->getVersion();
  79.                 }
  80.                 sort($plugin_versions);
  81.                 $cache_id .= implode('',$plugin_versions);
  82.                 return $cache_id;
  83.         }
  84.  
  85.         /**
  86.          * @param bool $noCache
  87.          * @return array[]|mixed|null
  88.          * @throws \ViaThinkSoft\OIDplus\OIDplusException
  89.          */
  90.         public function readAll(bool $noCache = false) {
  91.                 static $local_cache = null;
  92.  
  93.                 $cache_file = OIDplus::localpath().'/userdata/cache/frdl_alt_id.ser';
  94.                 if ($noCache === false) {
  95.                         // Local cache (to save time for multiple calls during the same HTTP request)
  96.                         if (!is_null($local_cache)) return $local_cache;
  97.  
  98.                         // File cache (to save time between HTTP requests)
  99.                         if (file_exists($cache_file)) {
  100.                                 $cache_data = unserialize(file_get_contents($cache_file));
  101.                                 $cache_id = $cache_data[0];
  102.                                 if ($cache_id == $this->cache_id()) {
  103.                                         return $cache_data[1];
  104.                                 }
  105.                         }
  106.                 }
  107.  
  108.                 $alt_ids = array();
  109.                 $rev_lookup = array();
  110.  
  111.                 $res = OIDplus::db()->query("select id from ###objects ".
  112.                                             "where parent <> 'oid:1.3.6.1.4.1.37476.1.2.3.1'");  // TODO FIXME! readAll() is TOOOOO slow if a system has more than 50.000 OIDs!!! DEADLOCK!!!
  113.                 while ($row = $res->fetch_array()) {
  114.                         $obj = OIDplusObject::parse($row['id']);
  115.                         if (!$obj) continue; // e.g. if plugin is disabled
  116.                         $ary = $obj->getAltIds();
  117.                         foreach ($ary as $a) {
  118.                                 $origin = $obj->nodeId(true);
  119.                                 $alternative = $a->getNamespace() . ':' . $a->getId();
  120.  
  121.                                 if (!isset($alt_ids[$origin])) $alt_ids[$origin] = array();
  122.                                 $alt_ids[$origin][] = $alternative;
  123.  
  124.                                 if (!isset($rev_lookup[$alternative])) $rev_lookup[$alternative] = array();
  125.                                 $rev_lookup[$alternative][] = $origin;
  126.                         }
  127.                 }
  128.  
  129.                 $data = array($alt_ids, $rev_lookup);
  130.  
  131.                 // File cache (to save time between HTTP requests)
  132.                 $cache_data = array($this->cache_id(), $data);
  133.                 @file_put_contents($cache_file, serialize($cache_data));
  134.  
  135.                 // Local cache (to save time for multiple calls during the same HTTP request)
  136.                 $local_cache = $data;
  137.  
  138.                 return $data;
  139.         }
  140.  
  141.         /**
  142.          * @param string $id
  143.          * @return string[]
  144.          * @throws \ViaThinkSoft\OIDplus\OIDplusException
  145.          */
  146.         public function getAlternativesForQuery(string $id/* INTF_OID_1_3_6_1_4_1_37476_2_5_2_3_7 signature takes just 1 param!? , $noCache = false*/): array {
  147.  
  148.                 static $caches = array();
  149.  
  150.                 if(/*$noCache === false && */isset($caches[$id]) ){
  151.                         return $caches[$id];
  152.                 }
  153.  
  154.                 if (strpos($id,':') !== false) {
  155.                         list($ns, $altIdRaw) = explode(':', $id, 2);
  156.                         if($ns === 'weid'){
  157.                                 $id='oid:'.\Frdl\Weid\WeidOidConverter::weid2oid($id);
  158.                         }
  159.                 }
  160.  
  161.                 list($alt_ids, $rev_lookup) = $this->readAll(false);
  162.  
  163.                 $res = [
  164.                         $id,
  165.                 ];
  166.                 if(isset($rev_lookup[$id])){
  167.                         $res = array_merge($res, $rev_lookup[$id]);
  168.                 }
  169.                 foreach($alt_ids as $original => $altIds){
  170.                         if($id === $original || in_array($id, $altIds) ){
  171.                                  $res = array_merge($res, $altIds);
  172.                                  $res = array_merge($res, [$original]);
  173.                         }
  174.                 }
  175.  
  176.                 $weid = false;
  177.                 foreach($res as $alt){
  178.                         if (strpos($alt,':') !== false) {
  179.                                 list($ns, $altIdRaw) = explode(':', $alt, 2);
  180.                                 if($ns === 'oid'){
  181.                                         $weid=\Frdl\Weid\WeidOidConverter::oid2weid($altIdRaw);
  182.                                         break;
  183.                                 }
  184.                         }
  185.                 }
  186.  
  187.                 if ($weid !== false) {
  188.                         $res[]=$weid;
  189.                 }
  190.                 $res = array_unique($res);
  191.  
  192.                 $caches[$id] = $res;
  193.  
  194.                 return $res;
  195.         }
  196.  
  197.         /**
  198.          * @param string $request
  199.          * @return array|false
  200.          */
  201.         public function tree_search(string $request) {
  202.                 return false;
  203.         }
  204.  
  205.         /**
  206.          * @param string $id
  207.          * @return false|mixed|string
  208.          * @throws \ViaThinkSoft\OIDplus\OIDplusException
  209.          */
  210.         public function getCanonical(string $id){
  211.                 foreach($this->getAlternativesForQuery($id) as $alt){
  212.                         if (strpos($alt,':') !== false) {
  213.                                 list($ns, $altIdRaw) = explode(':', $alt, 2);
  214.                                 if($ns === 'oid'){
  215.                                         return $alt;
  216.                                 }
  217.                         }
  218.                 }
  219.  
  220.                 return false;
  221.         }
  222.  
  223.         /**
  224.          * Implements interface INTF_OID_1_3_6_1_4_1_37476_2_5_2_3_4
  225.          * @param string $id
  226.          * @param array $out
  227.          * @return void
  228.          * @throws \ViaThinkSoft\OIDplus\OIDplusException
  229.          */
  230.         public function whoisObjectAttributes(string $id, array &$out) {
  231.                 $xmlns = 'oidplus-frdlweb-altids-plugin';
  232.                 $xmlschema = 'urn:oid:1.3.6.1.4.1.37553.8.1.8.8.53354196964.641310544.1714020422';
  233.                 $xmlschemauri = OIDplus::webpath(__DIR__.'/altids.xsd',OIDplus::PATH_ABSOLUTE_CANONICAL);
  234.  
  235.                 $handleShown = false;
  236.                 $canonicalShown = false;
  237.  
  238.                 $out1 = array();
  239.                 $out2 = array();
  240.  
  241.                 $tmp = $this->getAlternativesForQuery($id);
  242.                 sort($tmp); // DM 26.03.2023 : Added sorting (intended to sort "alternate-identifier")
  243.  
  244.                 foreach($tmp as $alt) {
  245.                         if (strpos($alt,':') === false) continue;
  246.  
  247.                         list($ns, $altIdRaw) = explode(':', $alt, 2);
  248.  
  249.                         if (($canonicalShown === false) && ($ns === 'oid')) {
  250.                                 $canonicalShown=true;
  251.  
  252.                                 $out1[] = [
  253.                                         'xmlns' => $xmlns,
  254.                                         'xmlschema' => $xmlschema,
  255.                                         'xmlschemauri' => $xmlschemauri,
  256.                                         'name' => 'canonical-identifier',
  257.                                         'value' => $ns.':'.$altIdRaw,
  258.                                 ];
  259.  
  260.                         }
  261.  
  262.                         if (($handleShown === false) && ($alt === $id)) {
  263.                                 $handleShown=true;
  264.  
  265.                                 $out1[] = [
  266.                                         'xmlns' => $xmlns,
  267.                                         'xmlschema' => $xmlschema,
  268.                                         'xmlschemauri' => $xmlschemauri,
  269.                                         'name' => 'handle-identifier',
  270.                                         'value' => $alt,
  271.                                 ];
  272.  
  273.                         }
  274.  
  275.                         if ($alt !== $id) { // DM 26.03.2023 : Added condition that alternate must not be the id itself
  276.                                 $out2[] = [
  277.                                         'xmlns' => $xmlns,
  278.                                         'xmlschema' => $xmlschema,
  279.                                         'xmlschemauri' => $xmlschemauri,
  280.                                         'name' => 'alternate-identifier',
  281.                                         'value' => $ns.':'.$altIdRaw,
  282.                                 ];
  283.                         }
  284.  
  285.                 }
  286.  
  287.                 // DM 26.03.2023 : Added this
  288.                 $out = array_merge($out, $out1); // handle-identifier and canonical-identifier
  289.                 $out = array_merge($out, $out2); // alternate-identifier
  290.  
  291.         }
  292.  
  293.         /**
  294.          * Implements interface INTF_OID_1_3_6_1_4_1_37476_2_5_2_3_4
  295.          * @param string $email
  296.          * @param array $out
  297.          * @return void
  298.          */
  299.         public function whoisRaAttributes(string $email, array &$out) {
  300.  
  301.         }
  302.  
  303.  }
  304.