Subversion Repositories oidplus

Rev

Rev 1050 | 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 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\OIDplus;
  13. use ViaThinkSoft\OIDplus\OIDplusObject;
  14. use ViaThinkSoft\OIDplus\OIDplusPagePluginPublic;
  15.  
  16. // phpcs:disable PSR1.Files.SideEffects
  17. \defined('INSIDE_OIDPLUS') or die;
  18. // phpcs:enable PSR1.Files.SideEffects
  19.  
  20. class OIDplusPagePublicAltIds extends OIDplusPagePluginPublic {
  21.  
  22.         public function action($actionID, $params) {
  23.  
  24.         }
  25.  
  26.         public function gui($id, &$out, &$handled) {
  27.  
  28.         }
  29.  
  30.         public function publicSitemap(&$out) {
  31.  
  32.         }
  33.  
  34.         public function tree(&$json, $ra_email=null, $nonjs=false, $req_goto='') {
  35.                 return false;
  36.         }
  37.  
  38.         private function cache_id() {
  39.                 static $cache_id = null;
  40.                 if (!is_null($cache_id)) return $cache_id;
  41.                 $cache_id  =  'Create='.OIDplus::db()->getScalar("select max(created) as ts from ###objects where created is not null;");
  42.                 $cache_id .= '/Update='.OIDplus::db()->getScalar("select max(updated) as ts from ###objects where updated is not null;");
  43.                 $cache_id .= '/Count='.OIDplus::db()->getScalar("select count(id) as cnt from ###objects;");
  44.                 $plugin_versions = array();
  45.                 foreach (OIDplus::getObjectTypePluginsEnabled() as $otp) {
  46.                         $plugin_versions[] = '/'.$otp->getManifest()->getOid().'='.$otp->getManifest()->getVersion();
  47.                 }
  48.                 sort($plugin_versions);
  49.                 $cache_id .= implode('',$plugin_versions);
  50.                 return $cache_id;
  51.         }
  52.  
  53.         public function readAll($noCache = false) {
  54.                 static $local_cache = null;
  55.  
  56.                 $cache_file = OIDplus::localpath().'/userdata/cache/frdl_alt_id.ser';
  57.                 if ($noCache === false) {
  58.                         // Local cache (to save time for multiple calls during the same HTTP request)
  59.                         if (!is_null($local_cache)) return $local_cache;
  60.  
  61.                         // File cache (to save time between HTTP requests)
  62.                         if (file_exists($cache_file)) {
  63.                                 $cache_data = unserialize(file_get_contents($cache_file));
  64.                                 $cache_id = $cache_data[0];
  65.                                 if ($cache_id == $this->cache_id()) {
  66.                                         return $cache_data[1];
  67.                                 }
  68.                         }
  69.                 }
  70.  
  71.                 $alt_ids = array();
  72.                 $rev_lookup = array();
  73.  
  74.                 $res = OIDplus::db()->query("select id from ###objects");
  75.                 while ($row = $res->fetch_array()) {
  76.                         $obj = OIDplusObject::parse($row['id']);
  77.                         if (!$obj) continue; // e.g. if plugin is disabled
  78.                         $ary = $obj->getAltIds();
  79.                         foreach ($ary as $a) {
  80.                                 $origin = $obj->nodeId(true);
  81.                                 $alternative = $a->getNamespace() . ':' . $a->getId();
  82.  
  83.                                 if (!isset($alt_ids[$origin])) $alt_ids[$origin] = array();
  84.                                 $alt_ids[$origin][] = $alternative;
  85.  
  86.                                 if (!isset($rev_lookup[$alternative])) $rev_lookup[$alternative] = array();
  87.                                 $rev_lookup[$alternative][] = $origin;
  88.                         }
  89.                 }
  90.  
  91.                 $data = array($alt_ids, $rev_lookup);
  92.  
  93.                 // File cache (to save time between HTTP requests)
  94.                 $cache_data = array($this->cache_id(), $data);
  95.                 @file_put_contents($cache_file, serialize($cache_data));
  96.  
  97.                 // Local cache (to save time for multiple calls during the same HTTP request)
  98.                 $local_cache = $data;
  99.  
  100.                 return $data;
  101.         }
  102.  
  103.         public function getAlternativesForQuery($id/* 1.3.6.1.4.1.37476.2.5.2.3.7 signature takes just 1 param!? , $noCache = false*/) {
  104.  
  105.                 static $caches = array();
  106.  
  107.                 if(/*$noCache === false && */isset($caches[$id]) ){
  108.                         return $caches[$id];
  109.                 }
  110.  
  111.                 if (strpos($id,':') !== false) {
  112.                         list($ns, $altIdRaw) = explode(':', $id, 2);
  113.                         if($ns === 'weid'){
  114.                                 $id='oid:'.\Frdl\Weid\WeidOidConverter::weid2oid($id);
  115.                         }
  116.                 }
  117.  
  118.                 list($alt_ids, $rev_lookup) = $this->readAll(false);
  119.  
  120.                 $res = [
  121.                         $id,
  122.                 ];
  123.                 if(isset($rev_lookup[$id])){
  124.                         $res = array_merge($res, $rev_lookup[$id]);
  125.                 }
  126.                 foreach($alt_ids as $original => $altIds){
  127.                         if($id === $original || in_array($id, $altIds) ){
  128.                                  $res = array_merge($res, $altIds);
  129.                                  $res = array_merge($res, [$original]);
  130.                         }
  131.                 }
  132.  
  133.                 $weid = false;
  134.                 foreach($res as $alt){
  135.                         if (strpos($alt,':') !== false) {
  136.                                 list($ns, $altIdRaw) = explode(':', $alt, 2);
  137.                                 if($ns === 'oid'){
  138.                                         $weid=\Frdl\Weid\WeidOidConverter::oid2weid($altIdRaw);
  139.                                         break;
  140.                                 }
  141.                         }
  142.                 }
  143.  
  144.                 if ($weid !== false) {
  145.                         $res[]=$weid;
  146.                 }
  147.                 $res = array_unique($res);
  148.  
  149.                 $caches[$id] = $res;
  150.  
  151.                 return $res;
  152.         }
  153.  
  154.         public function implementsFeature($id) {
  155.                 if (strtolower($id) == '1.3.6.1.4.1.37476.2.5.2.3.4') return true; // whois*Attributes
  156.                 if (strtolower($id) == '1.3.6.1.4.1.37476.2.5.2.3.7') return true; // getAlternativesForQuery
  157.                 return false;
  158.         }
  159.  
  160.         public function tree_search($request) {
  161.                 return false;
  162.         }
  163.  
  164.         public function getCanonical($id){
  165.                 foreach($this->getAlternativesForQuery($id) as $alt){
  166.                         if (strpos($alt,':') !== false) {
  167.                                 list($ns, $altIdRaw) = explode(':', $alt, 2);
  168.                                 if($ns === 'oid'){
  169.                                         return $alt;
  170.                                 }
  171.                         }
  172.                 }
  173.  
  174.                 return false;
  175.         }
  176.  
  177.         public function whoisObjectAttributes($id, &$out) {
  178.                 // Interface 1.3.6.1.4.1.37476.2.5.2.3.4
  179.  
  180.                 $xmlns = 'oidplus-frdlweb-altids-plugin';
  181.                 $xmlschema = 'urn:oid:1.3.6.1.4.1.37553.8.1.8.8.53354196964.641310544.1714020422';
  182.                 $xmlschemauri = OIDplus::webpath(__DIR__.'/altids.xsd',OIDplus::PATH_ABSOLUTE);
  183.  
  184.                 $handleShown = false;
  185.                 $canonicalShown = false;
  186.  
  187.                 foreach($this->getAlternativesForQuery($id) as $alt) {
  188.  
  189.                         if (strpos($alt,':') === false) continue;
  190.  
  191.                         list($ns, $altIdRaw) = explode(':', $alt, 2);
  192.  
  193.                         if (($canonicalShown === false) && ($ns === 'oid')) {
  194.                                 $canonicalShown=true;
  195.  
  196.                                 $out[] = [
  197.                                         'xmlns' => $xmlns,
  198.                                         'xmlschema' => $xmlschema,
  199.                                         'xmlschemauri' => $xmlschemauri,
  200.                                         'name' => 'canonical-identifier',
  201.                                         'value' => $ns.':'.$altIdRaw,
  202.                                 ];
  203.  
  204.                         }
  205.  
  206.                         if (($handleShown === false) && ($alt === $id)) {
  207.                                 $handleShown=true;
  208.  
  209.                                 $out[] = [
  210.                                         'xmlns' => $xmlns,
  211.                                         'xmlschema' => $xmlschema,
  212.                                         'xmlschemauri' => $xmlschemauri,
  213.                                         'name' => 'handle-identifier',
  214.                                         'value' => $alt,
  215.                                 ];
  216.  
  217.                         }
  218.  
  219.                         $out[] = [
  220.                                 'xmlns' => $xmlns,
  221.                                 'xmlschema' => $xmlschema,
  222.                                 'xmlschemauri' => $xmlschemauri,
  223.                                 'name' => 'alternate-identifier',
  224.                                 'value' => $ns.':'.$altIdRaw,
  225.                         ];
  226.  
  227.                 }
  228.         }
  229.  
  230.         public function whoisRaAttributes($email, &$out) {
  231.                 // Interface 1.3.6.1.4.1.37476.2.5.2.3.4
  232.         }
  233.  
  234.  }
  235.