Subversion Repositories oidplus

Rev

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