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