Subversion Repositories oidplus

Rev

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

  1. <?php
  2.  
  3. /*
  4.  * OIDplus 2.0
  5.  * Copyright 2019 - 2023 Daniel Marschall, ViaThinkSoft
  6.  *
  7.  * Licensed under the Apache License, Version 2.0 (the "License");
  8.  * you may not use this file except in compliance with the License.
  9.  * You may obtain a copy of the License at
  10.  *
  11.  *     http://www.apache.org/licenses/LICENSE-2.0
  12.  *
  13.  * Unless required by applicable law or agreed to in writing, software
  14.  * distributed under the License is distributed on an "AS IS" BASIS,
  15.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16.  * See the License for the specific language governing permissions and
  17.  * limitations under the License.
  18.  */
  19.  
  20. namespace ViaThinkSoft\OIDplus;
  21.  
  22. // phpcs:disable PSR1.Files.SideEffects
  23. \defined('INSIDE_OIDPLUS') or die;
  24. // phpcs:enable PSR1.Files.SideEffects
  25.  
  26. class OIDplusPagePublicWhois extends OIDplusPagePluginPublic
  27.         implements INTF_OID_1_3_6_1_4_1_37476_2_5_2_3_2 /* modifyContent */
  28. {
  29.         /**
  30.          * @param bool $html
  31.          * @return void
  32.          * @throws OIDplusException
  33.          */
  34.         public function init(bool $html=true) {
  35.                 OIDplus::config()->prepareConfigKey('whois_auth_token',                       'OID-over-WHOIS authentication token to display confidential data', '', OIDplusConfig::PROTECTION_EDITABLE, function($value) {
  36.                         $test_value = preg_replace('@[0-9a-zA-Z]*@', '', $value);
  37.                         if ($test_value != '') {
  38.                                 throw new OIDplusException(_L('Only characters and numbers are allowed as authentication token.'));
  39.                         }
  40.                 });
  41.                 OIDplus::config()->prepareConfigKey('webwhois_output_format_spacer',          'WebWHOIS: Spacer', '2', OIDplusConfig::PROTECTION_EDITABLE, function($value) {
  42.                         if (!is_numeric($value) || ($value < 0)) {
  43.                                 throw new OIDplusException(_L('Please enter a valid value.'));
  44.                         }
  45.                 });
  46.                 OIDplus::config()->prepareConfigKey('webwhois_output_format_max_line_length', 'WebWHOIS: Max line length', '80', OIDplusConfig::PROTECTION_EDITABLE, function($value) {
  47.                         if (!is_numeric($value) || ($value < 0)) {
  48.                                 throw new OIDplusException(_L('Please enter a valid value.'));
  49.                         }
  50.                 });
  51.                 OIDplus::config()->prepareConfigKey('individual_whois_server', 'A WHOIS/OID-IP "hostname:port" that will be presented', '', OIDplusConfig::PROTECTION_EDITABLE, function($value) {
  52.                         if ($value == '') return;
  53.                         $ary = explode(':', $value);
  54.                         if (count($ary) !== 2) {
  55.                                 throw new OIDplusException(_L('Please enter either an empty string or an input in the format "hostname:port".'));
  56.                         }
  57.                         // TODO: verify hostname $ary[0]
  58.                         // TODO: verify port $ary[1]
  59.                 });
  60.         }
  61.  
  62.         /**
  63.          * @return mixed|string
  64.          * @throws OIDplusException
  65.          */
  66.         private function getExampleId() {
  67.                 $firsts = array();
  68.                 $first_ns = null;
  69.                 foreach (OIDplus::getEnabledObjectTypes() as $ot) {
  70.                         $res = OIDplus::db()->query("select id FROM ###objects where parent = ?", array($ot::ns().':'));
  71.                         $res->naturalSortByField('id');
  72.                         if ($row = $res->fetch_array()) {
  73.                                 if (is_null($first_ns)) $first_ns = $ot::ns();
  74.                                 $firsts[$ot::ns()] = $row['id'];
  75.                         }
  76.                 }
  77.                 if ((count($firsts) == 0) || is_null($first_ns)) {
  78.                         return 'oid:2.999';
  79.                 } elseif (isset($firsts['oid'])) {
  80.                         return  $firsts['oid'];
  81.                 } else {
  82.                         return  $firsts[$first_ns];
  83.                 }
  84.         }
  85.  
  86.         /**
  87.          * @param string $id
  88.          * @param array $out
  89.          * @param bool $handled
  90.          * @return void
  91.          * @throws OIDplusException
  92.          */
  93.         public function gui(string $id, array &$out, bool &$handled) {
  94.                 if (explode('$',$id)[0] == 'oidplus:whois') {
  95.                         $handled = true;
  96.  
  97.                         $example = $this->getExampleId();
  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.                                 // This config setting is set by the "Registration" plugin
  105.                                 $whois_server = OIDplus::config()->getValue('vts_whois', '');
  106.                         }
  107.  
  108.                         $out['title'] = _L('OID Information Protocol (OID-IP) / WHOIS');
  109.                         $out['icon'] = file_exists(__DIR__.'/img/main_icon.png') ? OIDplus::webpath(__DIR__,OIDplus::PATH_RELATIVE).'img/main_icon.png' : '';
  110.  
  111.                         $out['text']  = '<p>'._L('With the OID Information Protocol (OID-IP), you can query object information in a format that is human-readable and machine-readable.').'</p>';
  112.  
  113.                         // Use this if webwhois.php matches the currently uploaded Internet Draft:
  114.                         $out['text'] .= '<p>'._L('RFC Internet Draft').': <a target="_blank" href="https://datatracker.ietf.org/doc/draft-viathinksoft-oidip/">draft-viathinksoft-oidip-05</a></p>';
  115.                         // Use this if webwhois.php implements something which is not yet uploaded to IETF:
  116.                         //$out['text'] .= '<p>'._L('RFC Internet Draft').': <a href="'.OIDplus::webpath(__DIR__.'/whois/rfc/draft-viathinksoft-oidip-06.txt', true).'" target="_blank">draft-viathinksoft-oidip-06</a></p>';
  117.                         # ---
  118.                         $out['text'] .= '<h2>'._L('Parameters for new request').'</h2>';
  119.                         $out['text'] .= '<noscript>';
  120.                         $out['text'] .= '<p>'._L('You need to enable JavaScript to use the login area.').'</p>';
  121.                         $out['text'] .= '</noscript>';
  122.                         $out['text'] .= '<div id="oidipArea" style="display:none">';
  123.                         # ---
  124.                         $out['text'] .= _L('Requested object including namespace, e.g. %1','<code>oid:2.999</code>').'<br>';
  125.                         $out['text'] .= '<input type="text" id="whois_query" name="query" value="'.htmlentities($example).'" onkeyup="OIDplusPagePublicWhois.refresh_whois_url_bar()">';
  126.                         $out['text'] .= '&nbsp;<span id="whois_query_invalid" style="display:none"><font color="red"><b>('._L('Invalid').')</b></font></span>';
  127.                         $out['text'] .= '<br><br>';
  128.                         # ---
  129.                         $out['text'] .= _L('Output format').':<br><fieldset id="whois_format">';
  130.                         $out['text'] .= '    <input type="radio" id="text" name="format" value="text" checked onclick="OIDplusPagePublicWhois.refresh_whois_url_bar()">';
  131.                         $out['text'] .= '    <label for="text"><code>$format=text</code> '._L('Text format').'</label><br>';
  132.                         $out['text'] .= '    <input type="radio" id="json" name="format" value="json" onclick="OIDplusPagePublicWhois.refresh_whois_url_bar()">';
  133.                         $out['text'] .= '    <label for="json"><code>$format=json</code>  '._L('JSON format').'</label> (<a target="_blank" href="'.OIDplus::webpath(__DIR__,OIDplus::PATH_RELATIVE).'whois/draft-viathinksoft-oidip-05.json">'._L('Schema').'</a>)<br>';
  134.                         $out['text'] .= '    <input type="radio" id="xml" name="format" value="xml" onclick="OIDplusPagePublicWhois.refresh_whois_url_bar()">';
  135.                         $out['text'] .= '    <label for="xml"><code>$format=xml</code>  '._L('XML format').'</label> (<a target="_blank" href="'.OIDplus::webpath(__DIR__,OIDplus::PATH_RELATIVE).'whois/draft-viathinksoft-oidip-05.xsd">'._L('Schema').'</a>)<br>';
  136.                         $out['text'] .= '</fieldset><br>';
  137.                         # ---
  138.                         $out['text'] .= _L('Authentication token(s), comma separated (optional)').':<br>';
  139.                         $out['text'] .= '<code>$auth = </code><input type="text" id="whois_auth" name="auth" value="" onkeyup="OIDplusPagePublicWhois.refresh_whois_url_bar()">';
  140.                         $out['text'] .= '&nbsp;<span id="whois_auth_invalid" style="display:none"><font color="red"><b>('._L('Invalid').')</b></font></span>';
  141.                         $out['text'] .= '<br>';
  142.                         # ---
  143.                         $out['text'] .= '<h2>'._L('Access via OID Information Protocol').'</h2>';
  144.                         $out['text'] .= '<p>'._L('The query according to OID Information Protocol is:').'</p>';
  145.                         $out['text'] .= '       <p><pre id="whois_query_bar"></pre></p>';
  146.                         $out['text'] .= '       <p><input type="button" value="'._L('Copy to clipboard').'" onClick="copyToClipboard(whois_query_bar)"></p>';
  147.                         $out['text'] .= '<p>'._L('You can use any WHOIS compatible client to query the information from the WHOIS or OID-IP port.').'</p>';
  148.                         if ($whois_server != '') {
  149.                                 $out['text'] .= '<p>'._L('The hostname and port number is:').'</p>';
  150.                                 $out['text'] .= '<p><pre>'.htmlentities($whois_server).'</pre></p>';
  151.                         }
  152.                         # ---
  153.                         $out['text'] .= '<h2>'._L('Access via web-browser').'</h2>';
  154.                         $out['text'] .= '<p>'._L('The URL for the Web Service is:').'</p>';
  155.                         $out['text'] .= '       <p><pre id="whois_url_bar"></pre></p>';
  156.                         $out['text'] .= '       <p>';
  157.                         $out['text'] .= '       <input type="button" value="'._L('Copy to clipboard').'" onClick="copyToClipboard(whois_url_bar)">';
  158.                         $out['text'] .= '       &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
  159.                         $out['text'] .= '       <input type="button" value="'._L('Open in web-browser').'" onClick="OIDplusPagePublicWhois.openInBrowser()">';
  160.                         $out['text'] .= '       </p>';
  161.                         $out['text'] .= '</div>';
  162.                         # ---
  163.                         $out['text'] .= '<script>';
  164.                         $out['text'] .= '   $("#oidipArea")[0].style.display = "Block";';  // because of NoScript
  165.                         $out['text'] .= '   OIDplusPagePublicWhois.refresh_whois_url_bar();';
  166.                         $out['text'] .= '</script>';
  167.                 }
  168.         }
  169.  
  170.         /**
  171.          * @param array $out
  172.          * @return void
  173.          */
  174.         public function publicSitemap(array &$out) {
  175.                 $out[] = 'oidplus:whois';
  176.         }
  177.  
  178.         /**
  179.          * @param array $json
  180.          * @param string|null $ra_email
  181.          * @param bool $nonjs
  182.          * @param string $req_goto
  183.          * @return bool
  184.          * @throws OIDplusException
  185.          */
  186.         public function tree(array &$json, string $ra_email=null, bool $nonjs=false, string $req_goto=''): bool {
  187.                 if (file_exists(__DIR__.'/img/main_icon16.png')) {
  188.                         $tree_icon = OIDplus::webpath(__DIR__,OIDplus::PATH_RELATIVE).'img/main_icon16.png';
  189.                 } else {
  190.                         $tree_icon = null; // default icon (folder)
  191.                 }
  192.  
  193.                 $json[] = array(
  194.                         'id' => 'oidplus:whois',
  195.                         'icon' => $tree_icon,
  196.                         'text' => _L('OID-IP / WHOIS')
  197.                 );
  198.  
  199.                 return true;
  200.         }
  201.  
  202.         /**
  203.          * Implements interface INTF_OID_1_3_6_1_4_1_37476_2_5_2_3_2
  204.          * @param string $id
  205.          * @param string $title
  206.          * @param string $icon
  207.          * @param string $text
  208.          * @return void
  209.          * @throws OIDplusException
  210.          */
  211.         public function modifyContent(string $id, string &$title, string &$icon, string &$text) {
  212.                 $payload = '<br><img src="'.OIDplus::webpath(__DIR__,OIDplus::PATH_RELATIVE).'img/page_pictogram.png" height="15" alt=""> <a href="'.OIDplus::webpath(__DIR__,OIDplus::PATH_RELATIVE).'whois/webwhois.php?query='.urlencode($id).'" class="gray_footer_font" target="_blank">'._L('Whois').'</a>';
  213.                 $obj = OIDplusObject::parse($id);
  214.                 if ($obj && $obj->userHasParentalWriteRights()) {
  215.                         $payload .= '<br><span class="gray_footer_font">'._L('OID-WHOIS Auth Token for displaying full object information: %1 (only applies if the this or superior objects are marked confidential)','<b>'.self::genWhoisAuthToken($id).'</b>').'</span>';
  216.                         $payload .= '<br><span class="gray_footer_font">'._L('OID-WHOIS Auth Token for displaying full RA information: %1 (only applies if the RA has set the privacy-flag)','<b>'.self::genWhoisAuthToken('ra:'.$obj->getRaMail()).'</b>').'</span>';
  217.                 }
  218.  
  219.                 $text = str_replace('<!-- MARKER 6 -->', '<!-- MARKER 6 -->'.$payload, $text);
  220.         }
  221.  
  222.         /**
  223.          * @param string $request
  224.          * @return array|false
  225.          */
  226.         public function tree_search(string $request) {
  227.                 return false;
  228.         }
  229.  
  230.         /**
  231.          * @param string $id
  232.          * @return int
  233.          * @throws OIDplusException
  234.          */
  235.         public static function genWhoisAuthToken(string $id): int {
  236.                 return smallhash(OIDplus::authUtils()->makeSecret('d8f44c7c-f4e9-11ed-86ca-3c4a92df8582:'.$id));
  237.         }
  238. }
  239.