Subversion Repositories oidplus

Rev

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