Subversion Repositories oidplus

Rev

Rev 148 | Go to most recent revision | Blame | Last modification | View Log | RSS feed

  1. <?php
  2.  
  3. /*
  4.  * OIDplus 2.0
  5.  * Copyright 2019 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. if (!defined('IN_OIDPLUS')) die();
  21.  
  22. define('QUERY_REGISTER_V1',         '1.3.6.1.4.1.37476.2.5.2.1.1.1');
  23. define('QUERY_UNREGISTER_V1',       '1.3.6.1.4.1.37476.2.5.2.1.2.1');
  24. define('QUERY_LISTALLSYSTEMIDS_V1', '1.3.6.1.4.1.37476.2.5.2.1.3.1');
  25. define('QUERY_LIVESTATUS_V1',       '1.3.6.1.4.1.37476.2.5.2.1.4.1');
  26.  
  27. class OIDplusPageAdminRegistration extends OIDplusPagePlugin {
  28.         public function type() {
  29.                 return 'admin';
  30.         }
  31.  
  32.         public function priority() {
  33.                 return 120;
  34.         }
  35.  
  36.         public function action(&$handled) {
  37.                 // Nothing
  38.         }
  39.  
  40.         public function cfgSetValue($name, $value) {
  41.                 if ($name == 'reg_privacy') {
  42.                         if (($value != '0') && ($value != '1') && ($value != '2')) {
  43.                                 throw new Exception("Please enter either 0, 1 or 2.");
  44.                         }
  45.                         // Now do a recheck and notify the ViaThinkSoft server
  46.                         OIDplus::config()->setValue('reg_last_ping', 0);
  47.                         $this->sendRegistrationQuery($value);
  48.                 }
  49.         }
  50.  
  51.         public function gui($id, &$out, &$handled) {
  52.                 if ($id === 'oidplus:srv_registration') {
  53.                         $handled = true;
  54.                         $out['title'] = 'System registration settings';
  55.                         $out['icon'] = file_exists(__DIR__.'/icon_big.png') ? 'plugins/'.basename(dirname(__DIR__)).'/'.basename(__DIR__).'/icon_big.png' : '';
  56.  
  57.                         if (!OIDplus::authUtils()::isAdminLoggedIn()) {
  58.                                 $out['icon'] = 'img/error_big.png';
  59.                                 $out['text'] .= '<p>You need to <a '.oidplus_link('oidplus:login').'>log in</a> as administrator.</p>';
  60.                         } else {
  61.                                 $out['text'] = '<p>The registration of your OIDplus installation has various advantages: The public key of your system is published, so that users can check the integrity of your data (e.g. signed OID-over-WHOIS requests). You can optionally also enable the automatic publishing of your public OID information to the repository oid-info.com.</p>'.
  62.                                                '<p><input type="button" onclick="openOidInPanel(\'oidplus:srvreg_status\');" value="Check status of the registration and collected data"></p>';
  63.  
  64.                                 if (!function_exists('openssl_sign')) {
  65.                                         $out['text'] .= '<p><font color="red">Error: OpenSSL plugin is missing in PHP. You cannot (un)register your OIDplus instance.</font></p>';
  66.                                 } else {
  67.                                         $out['text'] .= '<p>You can adjust your privacy level here:</p><p><select name="reg_privacy" id="reg_privacy">';
  68.  
  69.                                         # ---
  70.  
  71.                                         $out['text'] .= '<option value="0"';
  72.                                         if (OIDplus::config()->getValue('reg_privacy') == 0) {
  73.                                                 $out['text'] .= ' selected';
  74.                                         } else {
  75.                                                 $out['text'] .= '';
  76.                                         }
  77.                                         $out['text'] .= '>0 = Register to directory service and automatically publish RA/OID data at oid-info.com</option>';
  78.  
  79.                                         # ---
  80.  
  81.                                         $out['text'] .= '<option value="1"';
  82.                                         if (OIDplus::config()->getValue('reg_privacy') == 1) {
  83.                                                 $out['text'] .= ' selected';
  84.                                         } else {
  85.                                                 $out['text'] .= '';
  86.                                         }
  87.                                         $out['text'] .= '>1 = Only register to directory service</option>';
  88.  
  89.                                         # ---
  90.  
  91.                                         $out['text'] .= '<option value="2"';
  92.                                         if (OIDplus::config()->getValue('reg_privacy') == 2) {
  93.                                                 $out['text'] .= ' selected';
  94.                                         } else {
  95.                                                 $out['text'] .= '';
  96.                                         }
  97.                                         $out['text'] .= '>2 = Hide system</option>';
  98.  
  99.                                         # ---
  100.  
  101.                                         $out['text'] .= '</select> <input type="button" value="Change" onclick="crudActionRegPrivacyUpdate()"></p>';
  102.  
  103.                                         $out['text'] .= '<p>After clicking "change", your OIDplus installation will contact the ViaThinkSoft server to adjust (add or remove information) your privacy setting. This may take a few minutes.</p>';
  104.                                 }
  105.  
  106.                                 $out['text'] .= '<p><i>Privacy information:</i> Please note that removing your system from the directory does not automatically delete information about OIDs which are already published at oid-info.com. To remove already submitted OIDs at oid-info.com, please contact the <a href="mailto:admin@oid-info.com">OID Repository Webmaster</a>.';
  107.                         }
  108.                 }
  109.                 if ($id === 'oidplus:srvreg_status') {
  110.                         $handled = true;
  111.  
  112.                         $query = QUERY_LIVESTATUS_V1;
  113.  
  114.                         $payload = array(
  115.                                 "query" => $query, // we must repeat the query because we want to sign it
  116.                                 "system_id" => OIDplus::system_id(false)
  117.                         );
  118.  
  119.                         $signature = '';
  120.                         openssl_sign(json_encode($payload), $signature, OIDplus::config()->getValue('oidplus_private_key'));
  121.  
  122.                         $data = array(
  123.                                 "payload" => $payload,
  124.                                 "signature" => base64_encode($signature)
  125.                         );
  126.  
  127.                         $ch = curl_init();
  128.                         curl_setopt($ch, CURLOPT_URL, 'https://oidplus.viathinksoft.com/reg2/query.php');
  129.                         curl_setopt($ch, CURLOPT_POST, 1);
  130.                         curl_setopt($ch, CURLOPT_POSTFIELDS, "query=$query&data=".base64_encode(json_encode($data)));
  131.                         curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  132.                         curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
  133.                         curl_setopt($ch, CURLOPT_AUTOREFERER, true);
  134.                         $res = curl_exec($ch);
  135.                         curl_close($ch);
  136.                         // die("RES: $res\n");
  137.                         // if ($res == 'OK') ...
  138.  
  139.                         $out['title'] = 'Registration live status';
  140.                         $out['text']  = '<p><a '.oidplus_link('oidplus:srv_registration').'><img src="img/arrow_back.png" width="16"> Go back to registration settings</a></p>' .
  141.                                         $res;
  142.                 }
  143.         }
  144.  
  145.         public function sendRegistrationQuery($privacy_level=null) {
  146.                 if (is_null($privacy_level)) {
  147.                         $privacy_level = OIDplus::config()->getValue('reg_privacy');
  148.                 }
  149.  
  150.                 $system_url = OIDplus::system_url();
  151.  
  152.                 if ($privacy_level == 2) {
  153.                         // The user wants to unregister
  154.                         // but we only unregister if we are registered. Check this "anonymously" (i.e. without revealing our system ID)
  155.                         if (in_array(OIDplus::system_id(false), explode(';',file_get_contents('https://oidplus.viathinksoft.com/reg2/query.php?query='.QUERY_LISTALLSYSTEMIDS_V1)))) {
  156.                                 $query = QUERY_UNREGISTER_V1;
  157.  
  158.                                 $payload = array(
  159.                                         "query" => $query, // we must repeat the query because we want to sign it
  160.                                         "system_id" => OIDplus::system_id(false)
  161.                                 );
  162.  
  163.                                 $signature = '';
  164.                                 openssl_sign(json_encode($payload), $signature, OIDplus::config()->getValue('oidplus_private_key'));
  165.  
  166.                                 $data = array(
  167.                                         "payload" => $payload,
  168.                                         "signature" => base64_encode($signature)
  169.                                 );
  170.  
  171.                                 $ch = curl_init();
  172.                                 curl_setopt($ch, CURLOPT_URL, 'https://oidplus.viathinksoft.com/reg2/query.php');
  173.                                 curl_setopt($ch, CURLOPT_POST, 1);
  174.                                 curl_setopt($ch, CURLOPT_POSTFIELDS, "query=$query&data=".base64_encode(json_encode($data)));
  175.                                 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  176.                                 curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
  177.                                 curl_setopt($ch, CURLOPT_AUTOREFERER, true);
  178.                                 $res = curl_exec($ch);
  179.                                 curl_close($ch);
  180.                                 // die("RES: $res\n");
  181.                                 // if ($res == 'OK') ...
  182.                         }
  183.                 } else {
  184.                         if ($privacy_level == 0) {
  185.                                 if (class_exists('OIDplusPageAdminOIDInfoExport')) {
  186.                                         ob_start();
  187.                                         OIDplusPageAdminOIDInfoExport::outputXML(false); // no online check, because the query should be short (since the query is done while a visitor waits for the response)
  188.                                         $oidinfo_xml = ob_get_contents();
  189.                                         ob_end_clean();
  190.                                 } else {
  191.                                         $oidinfo_xml = false;
  192.                                 }
  193.                         } else {
  194.                                 $oidinfo_xml = false;
  195.                         }
  196.  
  197.                         $query = QUERY_REGISTER_V1;
  198.  
  199.                         $root_oids = array();
  200.                         foreach (OIDplus::getRegisteredObjectTypes() as $ot) {
  201.                                 if ($ot::ns() == 'oid') {
  202.                                         $res = OIDplus::db()->query("select id from ".OIDPLUS_TABLENAME_PREFIX."objects where " .
  203.                                                                     "parent = 'oid:' " .
  204.                                                                     "order by ".OIDplus::db()->natOrder('id'));
  205.                                         while ($row = OIDplus::db()->fetch_array($res)) {
  206.                                                 $root_oids[] = substr($row['id'],strlen('oid:'));
  207.                                         }
  208.                                 }
  209.                         }
  210.                         $payload = array(
  211.                                 "query" => $query, // we must repeat the query because we want to sign it
  212.                                 "privacy_level" => $privacy_level,
  213.                                 "system_id" => OIDplus::system_id(false),
  214.                                 "public_key" => OIDplus::config()->getValue('oidplus_public_key'),
  215.                                 "system_url" => $system_url,
  216.                                 "hide_system_url" => 0,
  217.                                 "hide_public_key" => 0,
  218.                                 "admin_email" => OIDplus::config()->getValue('admin_email'),
  219.                                 "system_title" => OIDplus::config()->systemTitle(),
  220.                                 "oidinfo_xml" => @base64_encode($oidinfo_xml),
  221.                                 "root_oids" => $root_oids
  222.                         );
  223.  
  224.                         $signature = '';
  225.                         openssl_sign(json_encode($payload), $signature, OIDplus::config()->getValue('oidplus_private_key'));
  226.  
  227.                         $data = array(
  228.                                 "payload" => $payload,
  229.                                 "signature" => base64_encode($signature)
  230.                         );
  231.  
  232.                         $ch = curl_init();
  233.                         curl_setopt($ch, CURLOPT_URL, 'https://oidplus.viathinksoft.com/reg2/query.php');
  234.                         curl_setopt($ch, CURLOPT_POST, 1);
  235.                         curl_setopt($ch, CURLOPT_POSTFIELDS, "query=$query&data=".base64_encode(json_encode($data)));
  236.                         curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  237.                         curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
  238.                         curl_setopt($ch, CURLOPT_AUTOREFERER, true);
  239.                         $res = curl_exec($ch);
  240.                         curl_close($ch);
  241.                         // die("RES: $res\n");
  242.                         // if ($res == 'OK') ...
  243.                 }
  244.  
  245.                 OIDplus::config()->setValue('reg_last_ping', time());
  246.         }
  247.  
  248.         public function init($html=true) {
  249.                 OIDplus::config()->prepareConfigKey('reg_wizard_done', 'Registration wizard done once?', '0', 1, 0);
  250.                 OIDplus::config()->prepareConfigKey('reg_privacy', '2=Hide your system, 1=Register your system to the ViaThinkSoft directory and oid-info.com, 0=Publish your system to ViaThinkSoft directory and all public contents (RA/OID) to oid-info.com', '0', 0, 1);
  251.                 OIDplus::config()->prepareConfigKey('reg_ping_interval', 'Registration ping interval (in seconds)', '3600', 0, 0);
  252.                 OIDplus::config()->prepareConfigKey('reg_last_ping', 'Last ping to ViaThinkSoft directory services', '0', 1, 0);
  253.  
  254.                 // REGISTRATION_HIDE_SYSTEM is an undocumented constant that can be put in the config.inc.php files of a test system accessing the same database as the productive system that is registered.
  255.                 // This avoids that the URL of the productive system is overridden with the test system URL (since they use the same database, they also have the same system ID)
  256.                 if (function_exists('openssl_sign') && !defined('REGISTRATION_HIDE_SYSTEM')) {
  257.                         // Show registration wizard once
  258.  
  259.                         if ($html && (OIDplus::config()->getValue('reg_wizard_done') != '1')) {
  260.                                 if (basename($_SERVER['SCRIPT_NAME']) != 'registration.php') {
  261.                                         if ($system_url = OIDplus::system_url()) {
  262.                                                 header('Location:'.$system_url.'plugins/'.basename(dirname(__DIR__)).'/'.basename(__DIR__).'/registration.php');
  263.                                         } else {
  264.                                                 header('Location:plugins/'.basename(dirname(__DIR__)).'/'.basename(__DIR__).'/registration.php');
  265.                                         }
  266.                                         die();
  267.                                 }
  268.                         }
  269.  
  270.                         // Is it time to register / renew directory entry?
  271.  
  272.                         if (OIDplus::config()->getValue('reg_wizard_done') == '1') {
  273.                                 $privacy_level = OIDplus::config()->getValue('reg_privacy');
  274.  
  275.                                 if ((time()-OIDplus::config()->getValue('reg_last_ping') >= OIDplus::config()->getValue('reg_ping_interval'))) {
  276.                                         $this->sendRegistrationQuery();
  277.                                 }
  278.                         }
  279.                 }
  280.         }
  281.  
  282.         public function tree(&$json, $ra_email=null, $nonjs=false, $req_goto='') {
  283.                 if (file_exists(__DIR__.'/treeicon.png')) {
  284.                         $tree_icon = 'plugins/'.basename(dirname(__DIR__)).'/'.basename(__DIR__).'/treeicon.png';
  285.                 } else {
  286.                         $tree_icon = null; // default icon (folder)
  287.                 }
  288.  
  289.                 $json[] = array(
  290.                         'id' => 'oidplus:srv_registration',
  291.                         'icon' => $tree_icon,
  292.                         'text' => 'Registration'
  293.                 );
  294.  
  295.                 return true;
  296.         }
  297.  
  298.         public function tree_search($request) {
  299.                 return false;
  300.         }
  301. }
  302.  
  303. OIDplus::registerPagePlugin(new OIDplusPageAdminRegistration());
  304.