Subversion Repositories oidplus

Rev

Rev 1086 | Rev 1121 | 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.  
  28.         /**
  29.          * @param bool $html
  30.          * @return void
  31.          * @throws OIDplusException
  32.          */
  33.         public function init(bool $html=true) {
  34.                 OIDplus::config()->prepareConfigKey('whois_auth_token',                       'OID-over-WHOIS authentication token to display confidential data', '', OIDplusConfig::PROTECTION_EDITABLE, function($value) {
  35.                         $test_value = preg_replace('@[0-9a-zA-Z]*@', '', $value);
  36.                         if ($test_value != '') {
  37.                                 throw new OIDplusException(_L('Only characters and numbers are allowed as authentication token.'));
  38.                         }
  39.                 });
  40.                 OIDplus::config()->prepareConfigKey('webwhois_output_format_spacer',          'WebWHOIS: Spacer', '2', OIDplusConfig::PROTECTION_EDITABLE, function($value) {
  41.                         if (!is_numeric($value) || ($value < 0)) {
  42.                                 throw new OIDplusException(_L('Please enter a valid value.'));
  43.                         }
  44.                 });
  45.                 OIDplus::config()->prepareConfigKey('webwhois_output_format_max_line_length', 'WebWHOIS: Max line length', '80', OIDplusConfig::PROTECTION_EDITABLE, function($value) {
  46.                         if (!is_numeric($value) || ($value < 0)) {
  47.                                 throw new OIDplusException(_L('Please enter a valid value.'));
  48.                         }
  49.                 });
  50.                 OIDplus::config()->prepareConfigKey('individual_whois_server', 'A WHOIS/OID-IP "hostname:port" that will be presented', '', OIDplusConfig::PROTECTION_EDITABLE, function($value) {
  51.                         if ($value == '') return;
  52.                         $ary = explode(':', $value);
  53.                         if (count($ary) !== 2) {
  54.                                 throw new OIDplusException(_L('Please enter either an empty string or an input in the format "hostname:port".'));
  55.                         }
  56.                         // TODO: verify hostname $ary[0]
  57.                         // TODO: verify port $ary[1]
  58.                 });
  59.         }
  60.  
  61.         /**
  62.          * @return mixed|string
  63.          * @throws OIDplusException
  64.          */
  65.         private function getExampleId() {
  66.                 $firsts = array();
  67.                 $first_ns = null;
  68.                 foreach (OIDplus::getEnabledObjectTypes() as $ot) {
  69.                         $res = OIDplus::db()->query("SELECT id FROM ###objects WHERE parent = ? ORDER BY ".OIDplus::db()->natOrder('id'), array($ot::ns().':'));
  70.                         if ($row = $res->fetch_array()) {
  71.                                 if (is_null($first_ns)) $first_ns = $ot::ns();
  72.                                 $firsts[$ot::ns()] = $row['id'];
  73.                         }
  74.                 }
  75.                 if ((count($firsts) == 0) || is_null($first_ns)) {
  76.                         return 'oid:2.999';
  77.                 } elseif (isset($firsts['oid'])) {
  78.                         return  $firsts['oid'];
  79.                 } else {
  80.                         return  $firsts[$first_ns];
  81.                 }
  82.         }
  83.  
  84.         /**
  85.          * @param string $id
  86.          * @param array $out
  87.          * @param bool $handled
  88.          * @return void
  89.          * @throws OIDplusException
  90.          */
  91.         public function gui(string $id, array &$out, bool &$handled) {
  92.                 if (explode('$',$id)[0] == 'oidplus:whois') {
  93.                         $handled = true;
  94.  
  95.                         $example = $this->getExampleId();
  96.  
  97.                         $whois_server = '';
  98.                         if (OIDplus::config()->getValue('individual_whois_server', '') != '') {
  99.                                 $whois_server = OIDplus::config()->getValue('individual_whois_server', '');
  100.                         }
  101.                         else if (OIDplus::config()->getValue('vts_whois', '') != '') {
  102.                                 // This config setting is set by the "Registration" plugin
  103.                                 $whois_server = OIDplus::config()->getValue('vts_whois', '');
  104.                         }
  105.  
  106.                         $out['title'] = _L('OID Information Protocol (OID-IP) / WHOIS');
  107.                         $out['icon'] = file_exists(__DIR__.'/img/main_icon.png') ? OIDplus::webpath(__DIR__,OIDplus::PATH_RELATIVE).'img/main_icon.png' : '';
  108.  
  109.                         $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>';
  110.  
  111.                         // Use this if webwhois.php matches the currently uploaded Internet Draft:
  112.                         $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>';
  113.                         // Use this if webwhois.php implements something which is not yet uploaded to IETF:
  114.                         //$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>';
  115.                         # ---
  116.                         $out['text'] .= '<h2>'._L('Parameters for new request').'</h2>';
  117.                         $out['text'] .= '<noscript>';
  118.                         $out['text'] .= '<p>'._L('You need to enable JavaScript to use the login area.').'</p>';
  119.                         $out['text'] .= '</noscript>';
  120.                         $out['text'] .= '<div id="oidipArea" style="display:none">';
  121.                         # ---
  122.                         $out['text'] .= _L('Requested object including namespace, e.g. %1','<code>oid:2.999</code>').'<br>';
  123.                         $out['text'] .= '<input type="text" id="whois_query" name="query" value="'.htmlentities($example).'" onkeyup="OIDplusPagePublicWhois.refresh_whois_url_bar()">';
  124.                         $out['text'] .= '&nbsp;<span id="whois_query_invalid" style="display:none"><font color="red"><b>('._L('Invalid').')</b></font></span>';
  125.                         $out['text'] .= '<br><br>';
  126.                         # ---
  127.                         $out['text'] .= _L('Output format').':<br><fieldset id="whois_format">';
  128.                         $out['text'] .= '    <input type="radio" id="text" name="format" value="text" checked onclick="OIDplusPagePublicWhois.refresh_whois_url_bar()">';
  129.                         $out['text'] .= '    <label for="text"><code>$format=text</code> '._L('Text format').'</label><br>';
  130.                         $out['text'] .= '    <input type="radio" id="json" name="format" value="json" onclick="OIDplusPagePublicWhois.refresh_whois_url_bar()">';
  131.                         $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>';
  132.                         $out['text'] .= '    <input type="radio" id="xml" name="format" value="xml" onclick="OIDplusPagePublicWhois.refresh_whois_url_bar()">';
  133.                         $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>';
  134.                         $out['text'] .= '</fieldset><br>';
  135.                         # ---
  136.                         $out['text'] .= _L('Authentication token(s), comma separated (optional)').':<br>';
  137.                         $out['text'] .= '<code>$auth = </code><input type="text" id="whois_auth" name="auth" value="" onkeyup="OIDplusPagePublicWhois.refresh_whois_url_bar()">';
  138.                         $out['text'] .= '&nbsp;<span id="whois_auth_invalid" style="display:none"><font color="red"><b>('._L('Invalid').')</b></font></span>';
  139.                         $out['text'] .= '<br>';
  140.                         # ---
  141.                         $out['text'] .= '<h2>'._L('Access via OID Information Protocol').'</h2>';
  142.                         $out['text'] .= '<p>'._L('The query according to OID Information Protocol is:').'</p>';
  143.                         $out['text'] .= '       <p><pre id="whois_query_bar"></pre></p>';
  144.                         $out['text'] .= '       <p><input type="button" value="'._L('Copy to clipboard').'" onClick="copyToClipboard(whois_query_bar)"></p>';
  145.                         $out['text'] .= '<p>'._L('You can use any WHOIS compatible client to query the information from the WHOIS or OID-IP port.').'</p>';
  146.                         if ($whois_server != '') {
  147.                                 $out['text'] .= '<p>'._L('The hostname and port number is:').'</p>';
  148.                                 $out['text'] .= '<p><pre>'.htmlentities($whois_server).'</pre></p>';
  149.                         }
  150.                         # ---
  151.                         $out['text'] .= '<h2>'._L('Access via web-browser').'</h2>';
  152.                         $out['text'] .= '<p>'._L('The URL for the Web Service is:').'</p>';
  153.                         $out['text'] .= '       <p><pre id="whois_url_bar"></pre></p>';
  154.                         $out['text'] .= '       <p>';
  155.                         $out['text'] .= '       <input type="button" value="'._L('Copy to clipboard').'" onClick="copyToClipboard(whois_url_bar)">';
  156.                         $out['text'] .= '       &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
  157.                         $out['text'] .= '       <input type="button" value="'._L('Open in web-browser').'" onClick="OIDplusPagePublicWhois.openInBrowser()">';
  158.                         $out['text'] .= '       </p>';
  159.                         $out['text'] .= '</div>';
  160.                         # ---
  161.                         $out['text'] .= '<script>';
  162.                         $out['text'] .= '   $("#oidipArea")[0].style.display = "Block";';  // because of NoScript
  163.                         $out['text'] .= '   OIDplusPagePublicWhois.refresh_whois_url_bar();';
  164.                         $out['text'] .= '</script>';
  165.                 }
  166.         }
  167.  
  168.         /**
  169.          * @param array $out
  170.          * @return void
  171.          */
  172.         public function publicSitemap(array &$out) {
  173.                 $out[] = 'oidplus:whois';
  174.         }
  175.  
  176.         /**
  177.          * @param array $json
  178.          * @param string|null $ra_email
  179.          * @param bool $nonjs
  180.          * @param string $req_goto
  181.          * @return bool
  182.          * @throws OIDplusException
  183.          */
  184.         public function tree(array &$json, string $ra_email=null, bool $nonjs=false, string $req_goto=''): bool {
  185.                 if (file_exists(__DIR__.'/img/main_icon16.png')) {
  186.                         $tree_icon = OIDplus::webpath(__DIR__,OIDplus::PATH_RELATIVE).'img/main_icon16.png';
  187.                 } else {
  188.                         $tree_icon = null; // default icon (folder)
  189.                 }
  190.  
  191.                 $json[] = array(
  192.                         'id' => 'oidplus:whois',
  193.                         'icon' => $tree_icon,
  194.                         'text' => _L('OID-IP / WHOIS')
  195.                 );
  196.  
  197.                 return true;
  198.         }
  199.  
  200.         /**
  201.          * @param string $id
  202.          * @return bool
  203.          */
  204.         public function implementsFeature(string $id): bool {
  205.                 if (strtolower($id) == '1.3.6.1.4.1.37476.2.5.2.3.2') return true; // modifyContent
  206.                 return false;
  207.         }
  208.  
  209.         /**
  210.          * Implements interface 1.3.6.1.4.1.37476.2.5.2.3.2
  211.          * @param string $id
  212.          * @param string $title
  213.          * @param string $icon
  214.          * @param string $text
  215.          * @return void
  216.          * @throws OIDplusException
  217.          */
  218.         public function modifyContent(string $id, string &$title, string &$icon, string &$text) {
  219.                 $text .= '<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>';
  220.  
  221.                 $obj = OIDplusObject::parse($id);
  222.                 if ($obj->userHasParentalWriteRights()) {
  223.                         $text .= '<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>';
  224.                         $text .= '<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>';
  225.                 }
  226.  
  227.         }
  228.  
  229.         /**
  230.          * @param string $request
  231.          * @return array|false
  232.          */
  233.         public function tree_search(string $request) {
  234.                 return false;
  235.         }
  236.  
  237.         /**
  238.          * @param $id
  239.          * @return int
  240.          * @throws OIDplusException
  241.          */
  242.         public static function genWhoisAuthToken($id) {
  243.                 return smallhash(OIDplus::baseConfig()->getValue('SERVER_SECRET').'/WHOIS/'.$id);
  244.         }
  245. }
  246.