Subversion Repositories oidplus

Rev

Rev 1116 | Rev 1130 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

  1. <?php
  2.  
  3. /*
  4.  * OIDplus 2.0 RDAP
  5.  * Copyright 2019 - 2023 Daniel Marschall, ViaThinkSoft
  6.  * Authors               Daniel Marschall, ViaThinkSoft
  7.  *                       Till Wehowski, Frdlweb
  8.  *
  9.  * Licensed under the Apache License, Version 2.0 (the "License");
  10.  * you may not use this file except in compliance with the License.
  11.  * You may obtain a copy of the License at
  12.  *
  13.  *     http://www.apache.org/licenses/LICENSE-2.0
  14.  *
  15.  * Unless required by applicable law or agreed to in writing, software
  16.  * distributed under the License is distributed on an "AS IS" BASIS,
  17.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  18.  * See the License for the specific language governing permissions and
  19.  * limitations under the License.
  20.  */
  21.  
  22. namespace Frdlweb\OIDplus;
  23.  
  24. use ViaThinkSoft\OIDplus\OIDplus;
  25. use ViaThinkSoft\OIDplus\OIDplusObject;
  26. use ViaThinkSoft\OIDplus\OIDplusOIDIP;
  27. use ViaThinkSoft\OIDplus\OIDplusPagePublicObjects;
  28.  
  29. // phpcs:disable PSR1.Files.SideEffects
  30. \defined('INSIDE_OIDPLUS') or die;
  31. // phpcs:enable PSR1.Files.SideEffects
  32.  
  33. class OIDplusRDAP {
  34.  
  35.         protected $rdapBaseUri;
  36.         protected $useCache;
  37.         protected $rdapCacheDir;
  38.         protected $rdapCacheExpires;
  39.  
  40.         /**
  41.          * @throws \ViaThinkSoft\OIDplus\OIDplusException
  42.          */
  43.         public function __construct() {
  44.                 $this->rdapBaseUri = OIDplus::baseConfig()->getValue('RDAP_BASE_URI', OIDplus::webpath() );
  45.                 $this->useCache = OIDplus::baseConfig()->getValue('RDAP_CACHE_ENABLED', false );
  46.                 $this->rdapCacheDir = OIDplus::baseConfig()->getValue('RDAP_CACHE_DIRECTORY', OIDplus::localpath().'userdata/cache/' );
  47.                 $this->rdapCacheExpires = OIDplus::baseConfig()->getValue('RDAP_CACHE_EXPIRES', 60 * 3 );
  48.         }
  49.  
  50.         /**
  51.          * @param string $query
  52.          * @return array
  53.          * @throws \ViaThinkSoft\OIDplus\OIDplusException
  54.          */
  55.         public function rdapQuery(string $query) {
  56.                 $query = str_replace('oid:.', 'oid:', $query);
  57.                 $n = explode(':', $query);
  58.                 if(2>count($n)){
  59.                  array_unshift($n, 'oid');
  60.                  $query = 'oid:'.$query;
  61.                 }
  62.                 $ns = $n[0];
  63.  
  64.                 if(true === $this->useCache){
  65.                         $cacheFile = $this->rdapCacheDir. 'rdap_'
  66.                         .sha1(\get_current_user()
  67.                                   . $this->rdapBaseUri.__FILE__.$query
  68.                                   .OIDplus::baseConfig()->getValue('SERVER_SECRET', sha1(__FILE__.\get_current_user()) )
  69.                                  )
  70.                         .'.'
  71.                         .strlen( $this->rdapBaseUri.$query )
  72.                         .'.ser'
  73.                         ;
  74.  
  75.                         $tmp = $this->rdap_read_cache($cacheFile, $this->rdapCacheExpires);
  76.                         if ($tmp) return $tmp;
  77.                 }else{
  78.                         $cacheFile = false;
  79.                 }
  80.  
  81.                 $out = [];
  82.  
  83.                 $obj = OIDplusObject::findFitting($query);
  84.  
  85.                 if(!$obj){
  86.                         // If object was not found, try if it is an alternative identifier of another object
  87.                         $alts = OIDplusPagePublicObjects::getAlternativesForQuery($query);
  88.                         foreach ($alts as $alt) {
  89.                                 if ($obj = OIDplusObject::findFitting($alt)) {
  90.                                         $query = $obj->nodeId();
  91.                                         break;
  92.                                 }
  93.                         }
  94.  
  95.                         // Still nothing found?
  96.                         if(!$obj){
  97.                                 $out['error'] = 'Not found';
  98.                                 if(true === $this->useCache){
  99.                                         $this->rdap_write_cache($out, $cacheFile);
  100.                                 }
  101.                                 return $this->rdap_out($out);
  102.                         }
  103.                 } else {
  104.                         $query = $obj->nodeId();
  105.                 }
  106.  
  107.                 $whois_server = '';
  108.                 if (OIDplus::config()->getValue('individual_whois_server', '') != '') {
  109.                         $whois_server = OIDplus::config()->getValue('individual_whois_server', '');
  110.                 }
  111.                 else if (OIDplus::config()->getValue('vts_whois', '') != '') {
  112.                         $whois_server = OIDplus::config()->getValue('vts_whois', '');
  113.                 }
  114.                 if (!empty($whois_server)) {
  115.                         list($whois_host, $whois_port) = explode(':',"$whois_server:43");
  116.                         if ($whois_port === '43') $out['port43'] = $whois_host;
  117.                 }
  118.  
  119.                 $parentHandle=$obj->one_up();
  120.  
  121.                 $out['name'] = $obj->nodeId(true);
  122.                 $out['objectClassName'] = $ns;
  123.                 $out['handle'] = $ns.':'.$n[1];
  124.                 $out['parentHandle'] =   (null !== $parentHandle && is_callable([$parentHandle, 'nodeId']) )
  125.                                          ? $parentHandle->nodeId(true)
  126.                                          : null;
  127.  
  128.                 $out['rdapConformance'] = [
  129.                         "rdap_level_0", //https://datatracker.ietf.org/doc/html/rfc9083
  130.                 ];
  131.                 $out['links'] = [
  132.                         [
  133.                                 "href"=> 'https://'.$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'],
  134.                                 "type"=> "application/rdap+json",
  135.                                 "title"=> sprintf("Information about the %s %s", $ns, $n[1]),
  136.                                 "value"=> $this->rdapBaseUri.$ns.'/'.$n[1],
  137.                                 "rel"=> "self"
  138.                         ],
  139.                         [
  140.                                 "href"=> OIDplus::webpath()."?goto=".urlencode($query),
  141.                                 "type"=> "text/html",
  142.                                 "title"=> sprintf("Information about the %s %s in the online repository", $ns, $n[1]),
  143.                                 "value"=> OIDplus::webpath()."?goto=".urlencode($query),
  144.                                 "rel"=> "alternate"
  145.                         ]
  146.                 ];
  147.                 $out['remarks'] = [
  148.                         [
  149.                                 "title"=>"Availability",
  150.                                 "description"=> [
  151.                                         sprintf("The %s %s is known.", strtoupper($ns), $n[1]),
  152.                                 ],
  153.                                 "links"=> []
  154.                         ],
  155.                         [
  156.                                 "title"=>"Description",
  157.                                 "description"=> [
  158.                                         ($obj->isConfidential()) ? 'REDACTED FOR PRIVACY' : $obj->getDescription(),
  159.                                 ],
  160.                                 "links"=> [
  161.                                         [
  162.                                                 "href"=> OIDplus::webpath()."?goto=".urlencode($query),
  163.                                                 "type"=> "text/html",
  164.                                                 "title"=> sprintf("Information about the %s %s in the online repository", $ns, $n[1]),
  165.                                                 "value"=> OIDplus::webpath()."?goto=".urlencode($query),
  166.                                                 "rel"=> "alternate"
  167.                                         ]
  168.                                 ]
  169.                         ],
  170.  
  171.                 ];
  172.  
  173.                 if (!is_null(OIDplus::getPluginByOid("1.3.6.1.4.1.37476.2.5.2.4.1.100"))) { // OIDplusPagePublicWhois
  174.                         $oidIPUrl = OIDplus::webpath().'plugins/viathinksoft/publicPages/100_whois/whois/webwhois.php?query='.urlencode($query);
  175.  
  176.                         $oidip_generator = new OIDplusOIDIP();
  177.  
  178.                         list($oidIP, $dummy_content_type) = $oidip_generator->oidipQuery($query);
  179.  
  180.                         $out['remarks'][] = [
  181.                                 "title" => "OID-IP Result",
  182.                                 "description" => $oidIP,
  183.                                 "links" => [
  184.                                                 [
  185.                                                         "href"=> $oidIPUrl,
  186.                                                         "type"=> "text/plain",
  187.                                                         "title"=> sprintf("OIDIP Result for the %s %s (Plaintext)", $ns, $n[1]),
  188.                                                         "value"=> $oidIPUrl,
  189.                                                         "rel"=> "alternate"
  190.                                                 ],
  191.                                                 [
  192.                                                         "href"=> "$oidIPUrl\$format=json",
  193.                                                         "type"=> "application/json",
  194.                                                         "title"=> sprintf("OIDIP Result for the %s %s (JSON)", $ns, $n[1]),
  195.                                                         "value"=> "$oidIPUrl\$format=json",
  196.                                                         "rel"=> "alternate"
  197.                                                 ],
  198.                                                 [
  199.                                                         "href"=> "$oidIPUrl\$format=xml",
  200.                                                         "type"=> "application/xml",
  201.                                                         "title"=> sprintf("OIDIP Result for the %s %s (XML)", $ns, $n[1]),
  202.                                                         "value"=> "$oidIPUrl\$format=xml",
  203.                                                         "rel"=> "alternate"
  204.                                                 ]
  205.                                         ]
  206.                                 ];
  207.  
  208.                         list($oidIPJSON, $dummy_content_type) = $oidip_generator->oidipQuery("$query\$format=json");
  209.                         $out['oidplus_oidip'] = json_decode($oidIPJSON);
  210.                 }
  211.  
  212.                 $out['notices']=[
  213.                          [
  214.                                 "title" => "Authentication Policy",
  215.                                 "description" =>
  216.                                 [
  217.                                         "Access to sensitive data for users with proper credentials."
  218.                                 ],
  219.                                 "links" =>
  220.                                 [
  221.                                         [
  222.                                                 "value" => $this->rdapBaseUri."help",
  223.                                                 "rel" => "alternate",
  224.                                                 "type" => "text/html",
  225.                                                 "href" => OIDplus::webpath()."?goto=oidplus%3Aresources%24OIDplus%2Fprivacy_documentation.html"
  226.                                         ]
  227.                                 ]
  228.                         ]
  229.                 ];
  230.  
  231.                 if($obj->isConfidential()){
  232.                         $out['remarks'][1]['type'] = "result set truncated due to authorization";
  233.                 }
  234.  
  235.                 $out['statuses']=[
  236.                         'active',
  237.                 ];
  238.  
  239.  
  240.                 if(true === $this->useCache){
  241.                         $this->rdap_write_cache($out, $cacheFile);
  242.                 }
  243.                 return $this->rdap_out($out);
  244.         }
  245.  
  246.         /**
  247.          * @param $out
  248.          * @param $cacheFile
  249.          * @return void
  250.          */
  251.         protected function rdap_write_cache($out, $cacheFile){
  252.                 if (!is_string($cacheFile)) return;
  253.                 @file_put_contents($cacheFile, serialize($out));
  254.         }
  255.  
  256.         /**
  257.          * @param $cacheFile
  258.          * @param $rdapCacheExpires
  259.          * @return array|null
  260.          */
  261.         protected function rdap_read_cache($cacheFile, $rdapCacheExpires){
  262.                 if (is_string($cacheFile) && file_exists($cacheFile) && filemtime($cacheFile) >= time() - $rdapCacheExpires) {
  263.                         $out = unserialize(file_get_contents($cacheFile));
  264.                         if(is_array($out) || is_object($out)){
  265.                                 return $this->rdap_out($out);
  266.                         }
  267.                 }
  268.                 return null;
  269.         }
  270.  
  271.         /**
  272.          * @param $out
  273.          * @return array
  274.          */
  275.         protected function rdap_out($out){
  276.                 $out_content = json_encode($out);
  277.                 $out_type = 'application/rdap+json';
  278.                 return array($out_content, $out_type);
  279.         }
  280.  
  281. }
  282.