Subversion Repositories oidplus

Rev

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