Subversion Repositories oidplus

Rev

Rev 277 | 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. class OIDplusPageAdminRegistration extends OIDplusPagePluginAdmin {
  21.  
  22.         /*private*/ const QUERY_REGISTER_V1 =         '1.3.6.1.4.1.37476.2.5.2.1.1.1';
  23.         /*private*/ const QUERY_UNREGISTER_V1 =       '1.3.6.1.4.1.37476.2.5.2.1.2.1';
  24.         /*private*/ const QUERY_LISTALLSYSTEMIDS_V1 = '1.3.6.1.4.1.37476.2.5.2.1.3.1';
  25.         /*private*/ const QUERY_LIVESTATUS_V1 =       '1.3.6.1.4.1.37476.2.5.2.1.4.1';
  26.  
  27.         public function action(&$handled) {
  28.                 // Nothing
  29.         }
  30.  
  31.         public function gui($id, &$out, &$handled) {
  32.                 if ($id === 'oidplus:srv_registration') {
  33.                         $handled = true;
  34.                         $out['title'] = 'System registration settings';
  35.                         $out['icon'] = file_exists(__DIR__.'/icon_big.png') ? OIDplus::webpath(__DIR__).'icon_big.png' : '';
  36.  
  37.                         if (!OIDplus::authUtils()::isAdminLoggedIn()) {
  38.                                 $out['icon'] = 'img/error_big.png';
  39.                                 $out['text'] = '<p>You need to <a '.OIDplus::gui()->link('oidplus:login').'>log in</a> as administrator.</p>';
  40.                         } else {
  41.                                 $out['text'] = file_get_contents(__DIR__ . '/info.tpl');
  42.  
  43.                                 if (!OIDplus::getPkiStatus()) {
  44.                                         $out['text'] .= '<p><font color="red">Error: Your system could not generate a private/public key pair. (OpenSSL is probably missing on your system). Therefore, you cannot register/unregister your OIDplus instance.</font></p>';
  45.                                 } else {
  46.                                         $out['text'] .= '<p><input type="button" onclick="openOidInPanel(\'oidplus:srvreg_status\');" value="Check status of the registration and collected data"></p>';
  47.  
  48.                                         if (OIDplus::baseConfig()->getValue('REGISTRATION_HIDE_SYSTEM', false)) {
  49.                                                 $out['text'] .= '<p><font color="red"><b>Attention!</b> <code>REGISTRATION_HIDE_SYSTEM</code> is set in the local configuration file! Therefore, this system will not register itself, despire the settings below.</font></p>';
  50.                                         }
  51.  
  52.                                         $out['text'] .= '<p>You can adjust your privacy level here:</p><p><select name="reg_privacy" id="reg_privacy">';
  53.  
  54.                                         # ---
  55.  
  56.                                         $out['text'] .= '<option value="0"';
  57.                                         if (OIDplus::config()->getValue('reg_privacy') == 0) {
  58.                                                 $out['text'] .= ' selected';
  59.                                         } else {
  60.                                                 $out['text'] .= '';
  61.                                         }
  62.                                         $out['text'] .= '>0 = Register to directory service and automatically publish RA/OID data at oid-info.com</option>';
  63.  
  64.                                         # ---
  65.  
  66.                                         $out['text'] .= '<option value="1"';
  67.                                         if (OIDplus::config()->getValue('reg_privacy') == 1) {
  68.                                                 $out['text'] .= ' selected';
  69.                                         } else {
  70.                                                 $out['text'] .= '';
  71.                                         }
  72.                                         $out['text'] .= '>1 = Only register to directory service</option>';
  73.  
  74.                                         # ---
  75.  
  76.                                         $out['text'] .= '<option value="2"';
  77.                                         if (OIDplus::config()->getValue('reg_privacy') == 2) {
  78.                                                 $out['text'] .= ' selected';
  79.                                         } else {
  80.                                                 $out['text'] .= '';
  81.                                         }
  82.                                         $out['text'] .= '>2 = Hide system</option>';
  83.  
  84.                                         # ---
  85.  
  86.                                         $out['text'] .= '</select> <input type="button" value="Change" onclick="crudActionRegPrivacyUpdate()"></p>';
  87.  
  88.                                         $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>';
  89.  
  90.                                         $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>.';
  91.                                 }
  92.                         }
  93.                 }
  94.                 if ($id === 'oidplus:srvreg_status') {
  95.                         $handled = true;
  96.  
  97.                         $query = self::QUERY_LIVESTATUS_V1;
  98.  
  99.                         $payload = array(
  100.                                 "query" => $query, // we must repeat the query because we want to sign it
  101.                                 "system_id" => OIDplus::getSystemId(false)
  102.                         );
  103.  
  104.                         $signature = '';
  105.                         if (!@openssl_sign(json_encode($payload), $signature, OIDplus::config()->getValue('oidplus_private_key'))) {
  106.                                 throw new OIDplusException("Signature failed");
  107.                         }
  108.  
  109.                         $data = array(
  110.                                 "payload" => $payload,
  111.                                 "signature" => base64_encode($signature)
  112.                         );
  113.  
  114.                         $ch = curl_init();
  115.                         curl_setopt($ch, CURLOPT_URL, 'https://oidplus.viathinksoft.com/reg2/query.php');
  116.                         curl_setopt($ch, CURLOPT_POST, 1);
  117.                         curl_setopt($ch, CURLOPT_POSTFIELDS, "query=$query&data=".base64_encode(json_encode($data)));
  118.                         curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  119.                         curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
  120.                         curl_setopt($ch, CURLOPT_AUTOREFERER, true);
  121.                         if (!($res = @curl_exec($ch))) {
  122.                                 throw new OIDplusException("Communication with ViaThinkSoft server failed");
  123.                         }
  124.                         curl_close($ch);
  125.                         // die("RES: $res\n");
  126.                         // if ($res == 'OK') ...
  127.  
  128.                         $out['title'] = 'Registration live status';
  129.                         $out['text']  = '<p><a '.OIDplus::gui()->link('oidplus:srv_registration').'><img src="img/arrow_back.png" width="16"> Go back to registration settings</a></p>' .
  130.                                         $res;
  131.                 }
  132.         }
  133.  
  134.         public function sendRegistrationQuery($privacy_level=null) {
  135.                 if (is_null($privacy_level)) {
  136.                         $privacy_level = OIDplus::config()->getValue('reg_privacy');
  137.                 }
  138.  
  139.                 $system_url = OIDplus::getSystemUrl();
  140.  
  141.                 // It is very important that we set the ping time NOW, because ViaThinkSoft might contact us during the ping,
  142.                 // and this would cause an endless loop!
  143.                 OIDplus::config()->setValue('reg_last_ping', time());
  144.  
  145.                 if (!OIDplus::getPkiStatus()) return false;
  146.  
  147.                 if ($privacy_level == 2) {
  148.                         // The user wants to unregister
  149.                         // but we only unregister if we are registered. Check this "anonymously" (i.e. without revealing our system ID)
  150.                         if (in_array(OIDplus::getSystemId(false), explode(';',file_get_contents('https://oidplus.viathinksoft.com/reg2/query.php?query='.self::QUERY_LISTALLSYSTEMIDS_V1)))) {
  151.                                 $query = self::QUERY_UNREGISTER_V1;
  152.  
  153.                                 $payload = array(
  154.                                         "query" => $query, // we must repeat the query because we want to sign it
  155.                                         "system_id" => OIDplus::getSystemId(false)
  156.                                 );
  157.  
  158.                                 $signature = '';
  159.                                 if (!@openssl_sign(json_encode($payload), $signature, OIDplus::config()->getValue('oidplus_private_key'))) {
  160.                                         return false; // throw new OIDplusException("Signature failed");
  161.                                 }
  162.  
  163.                                 $data = array(
  164.                                         "payload" => $payload,
  165.                                         "signature" => base64_encode($signature)
  166.                                 );
  167.  
  168.                                 $ch = curl_init();
  169.                                 curl_setopt($ch, CURLOPT_URL, 'https://oidplus.viathinksoft.com/reg2/query.php');
  170.                                 curl_setopt($ch, CURLOPT_POST, 1);
  171.                                 curl_setopt($ch, CURLOPT_POSTFIELDS, "query=$query&data=".base64_encode(json_encode($data)));
  172.                                 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  173.                                 curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
  174.                                 curl_setopt($ch, CURLOPT_AUTOREFERER, true);
  175.                                 if (!($res = @curl_exec($ch))) {
  176.                                         return false; // throw new OIDplusException("Communication with ViaThinkSoft server failed");
  177.                                 }
  178.                                 curl_close($ch);
  179.                                 // die("RES: $res\n");
  180.                                 // if ($res == 'OK') ...
  181.                         }
  182.                 } else {
  183.                         if ($privacy_level == 0) {
  184.                                 if (class_exists('OIDplusPageAdminOIDInfoExport')) {
  185.                                         ob_start();
  186.                                         OIDplusPageAdminOIDInfoExport::outputXML(false); // no online check, because the query should be short (since the query is done while a visitor waits for the response)
  187.                                         $oidinfo_xml = ob_get_contents();
  188.                                         ob_end_clean();
  189.                                 } else {
  190.                                         $oidinfo_xml = false;
  191.                                 }
  192.                         } else {
  193.                                 $oidinfo_xml = false;
  194.                         }
  195.  
  196.                         $query = self::QUERY_REGISTER_V1;
  197.  
  198.                         $root_oids = array();
  199.                         foreach (OIDplus::getEnabledObjectTypes() as $ot) {
  200.                                 if ($ot::ns() == 'oid') {
  201.                                         $res = OIDplus::db()->query("select id from ###objects where " .
  202.                                                                     "parent = 'oid:' " .
  203.                                                                     "order by ".OIDplus::db()->natOrder('id'));
  204.                                         while ($row = $res->fetch_array()) {
  205.                                                 $root_oids[] = substr($row['id'],strlen('oid:'));
  206.                                         }
  207.                                 }
  208.                         }
  209.                         $payload = array(
  210.                                 "query" => $query, // we must repeat the query because we want to sign it
  211.                                 "privacy_level" => $privacy_level,
  212.                                 "system_id" => OIDplus::getSystemId(false),
  213.                                 "public_key" => OIDplus::config()->getValue('oidplus_public_key'),
  214.                                 "system_url" => $system_url,
  215.                                 "hide_system_url" => 0,
  216.                                 "hide_public_key" => 0,
  217.                                 "admin_email" => OIDplus::config()->getValue('admin_email'),
  218.                                 "system_title" => OIDplus::config()->getValue('system_title'),
  219.                                 "oidinfo_xml" => @base64_encode($oidinfo_xml),
  220.                                 "root_oids" => $root_oids,
  221.                                 "system_version" => OIDplus::getVersion(),
  222.                                 "system_install_type" => OIDplus::getInstallType()
  223.                         );
  224.  
  225.                         $signature = '';
  226.                         if (!@openssl_sign(json_encode($payload), $signature, OIDplus::config()->getValue('oidplus_private_key'))) {
  227.                                         return false; // throw new OIDplusException("Signature failed");
  228.                         }
  229.  
  230.                         $data = array(
  231.                                 "payload" => $payload,
  232.                                 "signature" => base64_encode($signature)
  233.                         );
  234.  
  235.                         $ch = curl_init();
  236.                         curl_setopt($ch, CURLOPT_URL, 'https://oidplus.viathinksoft.com/reg2/query.php');
  237.                         curl_setopt($ch, CURLOPT_POST, 1);
  238.                         curl_setopt($ch, CURLOPT_POSTFIELDS, "query=$query&data=".base64_encode(json_encode($data)));
  239.                         curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  240.                         curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
  241.                         curl_setopt($ch, CURLOPT_AUTOREFERER, true);
  242.                         if (!($res = @curl_exec($ch))) {
  243.                                 return false; // throw new OIDplusException("Communication with ViaThinkSoft server failed");
  244.                         }
  245.                         curl_close($ch);
  246.  
  247.                         if ($res === 'HASH_CONFLICT') {
  248.                                 OIDplus::logger()->log("A!", "Removing SystemID and key pair because there is a hash conflict with another OIDplus system!");
  249.  
  250.                                 // Delete the system ID since we have a conflict with the 31-bit hash!
  251.                                 OIDplus::config()->setValue('oidplus_private_key', '');
  252.                                 OIDplus::config()->setValue('oidplus_public_key', '');
  253.  
  254.                                 // Try to generate a new system ID
  255.                                 OIDplus::getPkiStatus(true);
  256.  
  257.                                 // Enforce a new registration attempt at the next run
  258.                                 // We will not try again here, because that might lead to an endless loop if the VTS server would always return 'HASH_CONFLCIT'
  259.                                 OIDplus::config()->setValue('reg_last_ping', 0);
  260.                         }
  261.  
  262.                         // die("RES: $res\n");
  263.                         // if ($res == 'OK') ...
  264.                 }
  265.         }
  266.  
  267.         public function init($html=true) {
  268.                 OIDplus::config()->prepareConfigKey('reg_wizard_done', 'Registration wizard done once?', '0', OIDplusConfig::PROTECTION_HIDDEN, function($value) {
  269.  
  270.                 });
  271.                 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', OIDplusConfig::PROTECTION_EDITABLE, function($value) {
  272.                         if (($value != '0') && ($value != '1') && ($value != '2')) {
  273.                                 throw new OIDplusException("Please enter either 0, 1 or 2.");
  274.                         }
  275.                         // Now do a recheck and notify the ViaThinkSoft server
  276.                         OIDplus::config()->setValue('reg_last_ping', 0);
  277.                         $this->sendRegistrationQuery($value);
  278.                 });
  279.                 OIDplus::config()->prepareConfigKey('reg_ping_interval', 'Registration ping interval (in seconds)', '3600', OIDplusConfig::PROTECTION_HIDDEN, function($value) {
  280.  
  281.                 });
  282.                 OIDplus::config()->prepareConfigKey('reg_last_ping', 'Last ping to ViaThinkSoft directory services', '0', OIDplusConfig::PROTECTION_HIDDEN, function($value) {
  283.  
  284.                 });
  285.  
  286.                 $oobe_done = OIDplus::config()->getValue('reg_wizard_done') == '1';
  287.  
  288.                 if (!$oobe_done) {
  289.                         // Show registration/configuration wizard once
  290.                         if ($html) {
  291.                                 if (basename($_SERVER['SCRIPT_NAME']) != 'oobe.php') {
  292.                                         header('Location:'.OIDplus::webpath(__DIR__).'oobe.php');
  293.                                         die('Redirecting to registration wizard...');
  294.                                 }
  295.                         }
  296.                 } else {
  297.                         // Is it time to register / renew the directory entry?
  298.                         // Note: 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.
  299.                         // This avoids that the URL of a productive system is overridden with the URL of a cloned test system (since they use the same database, they also have the same system ID)
  300.  
  301.                         if (!OIDplus::baseConfig()->getValue('REGISTRATION_HIDE_SYSTEM', false)) {
  302.                                 $privacy_level = OIDplus::config()->getValue('reg_privacy');
  303.  
  304.                                 if (php_sapi_name() !== 'cli') { // don't register when called from CLI, otherweise the oidinfo XML can't convert relative links into absolute links
  305.                                         if ((time()-OIDplus::config()->getValue('reg_last_ping') >= OIDplus::config()->getValue('reg_ping_interval'))) {
  306.                                                 $this->sendRegistrationQuery();
  307.                                         }
  308.                                 }
  309.                         }
  310.                 }
  311.         }
  312.  
  313.         public function tree(&$json, $ra_email=null, $nonjs=false, $req_goto='') {
  314.                 if (file_exists(__DIR__.'/treeicon.png')) {
  315.                         $tree_icon = OIDplus::webpath(__DIR__).'treeicon.png';
  316.                 } else {
  317.                         $tree_icon = null; // default icon (folder)
  318.                 }
  319.  
  320.                 $json[] = array(
  321.                         'id' => 'oidplus:srv_registration',
  322.                         'icon' => $tree_icon,
  323.                         'text' => 'System registration'
  324.                 );
  325.  
  326.                 return true;
  327.         }
  328.  
  329.         public function tree_search($request) {
  330.                 return false;
  331.         }
  332. }
  333.