Subversion Repositories oidplus

Rev

Rev 1130 | Rev 1135 | 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.                 while ($row = $res->fetch_array()) {
  113.                         $obj = OIDplusObject::parse($row['id']);
  114.                         if (!$obj) continue; // e.g. if plugin is disabled
  115.                         $ary = $obj->getAltIds();
  116.                         foreach ($ary as $a) {
  117.                                 $origin = $obj->nodeId(true);
  118.                                 $alternative = $a->getNamespace() . ':' . $a->getId();
  119.  
  120.                                 if (!isset($alt_ids[$origin])) $alt_ids[$origin] = array();
  121.                                 $alt_ids[$origin][] = $alternative;
  122.  
  123.                                 if (!isset($rev_lookup[$alternative])) $rev_lookup[$alternative] = array();
  124.                                 $rev_lookup[$alternative][] = $origin;
  125.                         }
  126.                 }
  127.  
  128.                 $data = array($alt_ids, $rev_lookup);
  129.  
  130.                 // File cache (to save time between HTTP requests)
  131.                 $cache_data = array($this->cache_id(), $data);
  132.                 @file_put_contents($cache_file, serialize($cache_data));
  133.  
  134.                 // Local cache (to save time for multiple calls during the same HTTP request)
  135.                 $local_cache = $data;
  136.  
  137.                 return $data;
  138.         }
  139.  
  140.         /**
  141.          * @param string $id
  142.          * @return string[]
  143.          * @throws \ViaThinkSoft\OIDplus\OIDplusException
  144.          */
  145.         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 {
  146.  
  147.                 static $caches = array();
  148.  
  149.                 if(/*$noCache === false && */isset($caches[$id]) ){
  150.                         return $caches[$id];
  151.                 }
  152.  
  153.                 if (strpos($id,':') !== false) {
  154.                         list($ns, $altIdRaw) = explode(':', $id, 2);
  155.                         if($ns === 'weid'){
  156.                                 $id='oid:'.\Frdl\Weid\WeidOidConverter::weid2oid($id);
  157.                         }
  158.                 }
  159.  
  160.                 list($alt_ids, $rev_lookup) = $this->readAll(false);
  161.  
  162.                 $res = [
  163.                         $id,
  164.                 ];
  165.                 if(isset($rev_lookup[$id])){
  166.                         $res = array_merge($res, $rev_lookup[$id]);
  167.                 }
  168.                 foreach($alt_ids as $original => $altIds){
  169.                         if($id === $original || in_array($id, $altIds) ){
  170.                                  $res = array_merge($res, $altIds);
  171.                                  $res = array_merge($res, [$original]);
  172.                         }
  173.                 }
  174.  
  175.                 $weid = false;
  176.                 foreach($res as $alt){
  177.                         if (strpos($alt,':') !== false) {
  178.                                 list($ns, $altIdRaw) = explode(':', $alt, 2);
  179.                                 if($ns === 'oid'){
  180.                                         $weid=\Frdl\Weid\WeidOidConverter::oid2weid($altIdRaw);
  181.                                         break;
  182.                                 }
  183.                         }
  184.                 }
  185.  
  186.                 if ($weid !== false) {
  187.                         $res[]=$weid;
  188.                 }
  189.                 $res = array_unique($res);
  190.  
  191.                 $caches[$id] = $res;
  192.  
  193.                 return $res;
  194.         }
  195.  
  196.         /**
  197.          * @param string $request
  198.          * @return array|false
  199.          */
  200.         public function tree_search(string $request) {
  201.                 return false;
  202.         }
  203.  
  204.         /**
  205.          * @param string $id
  206.          * @return false|mixed|string
  207.          * @throws \ViaThinkSoft\OIDplus\OIDplusException
  208.          */
  209.         public function getCanonical(string $id){
  210.                 foreach($this->getAlternativesForQuery($id) as $alt){
  211.                         if (strpos($alt,':') !== false) {
  212.                                 list($ns, $altIdRaw) = explode(':', $alt, 2);
  213.                                 if($ns === 'oid'){
  214.                                         return $alt;
  215.                                 }
  216.                         }
  217.                 }
  218.  
  219.                 return false;
  220.         }
  221.  
  222.         /**
  223.          * Implements interface INTF_OID_1_3_6_1_4_1_37476_2_5_2_3_4
  224.          * @param string $id
  225.          * @param array $out
  226.          * @return void
  227.          * @throws \ViaThinkSoft\OIDplus\OIDplusException
  228.          */
  229.         public function whoisObjectAttributes(string $id, array &$out) {
  230.                 $xmlns = 'oidplus-frdlweb-altids-plugin';
  231.                 $xmlschema = 'urn:oid:1.3.6.1.4.1.37553.8.1.8.8.53354196964.641310544.1714020422';
  232.                 $xmlschemauri = OIDplus::webpath(__DIR__.'/altids.xsd',OIDplus::PATH_ABSOLUTE);
  233.  
  234.                 $handleShown = false;
  235.                 $canonicalShown = false;
  236.  
  237.                 foreach($this->getAlternativesForQuery($id) as $alt) {
  238.  
  239.                         if (strpos($alt,':') === false) continue;
  240.  
  241.                         list($ns, $altIdRaw) = explode(':', $alt, 2);
  242.  
  243.                         if (($canonicalShown === false) && ($ns === 'oid')) {
  244.                                 $canonicalShown=true;
  245.  
  246.                                 $out[] = [
  247.                                         'xmlns' => $xmlns,
  248.                                         'xmlschema' => $xmlschema,
  249.                                         'xmlschemauri' => $xmlschemauri,
  250.                                         'name' => 'canonical-identifier',
  251.                                         'value' => $ns.':'.$altIdRaw,
  252.                                 ];
  253.  
  254.                         }
  255.  
  256.                         if (($handleShown === false) && ($alt === $id)) {
  257.                                 $handleShown=true;
  258.  
  259.                                 $out[] = [
  260.                                         'xmlns' => $xmlns,
  261.                                         'xmlschema' => $xmlschema,
  262.                                         'xmlschemauri' => $xmlschemauri,
  263.                                         'name' => 'handle-identifier',
  264.                                         'value' => $alt,
  265.                                 ];
  266.  
  267.                         }
  268.  
  269.                         $out[] = [
  270.                                 'xmlns' => $xmlns,
  271.                                 'xmlschema' => $xmlschema,
  272.                                 'xmlschemauri' => $xmlschemauri,
  273.                                 'name' => 'alternate-identifier',
  274.                                 'value' => $ns.':'.$altIdRaw,
  275.                         ];
  276.  
  277.                 }
  278.         }
  279.  
  280.         /**
  281.          * Implements interface INTF_OID_1_3_6_1_4_1_37476_2_5_2_3_4
  282.          * @param string $email
  283.          * @param array $out
  284.          * @return void
  285.          */
  286.         public function whoisRaAttributes(string $email, array &$out) {
  287.  
  288.         }
  289.  
  290.  }
  291.