Subversion Repositories oidplus

Rev

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