Subversion Repositories oidplus

Rev

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