Subversion Repositories oidplus

Rev

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