Subversion Repositories oidplus

Rev

Rev 353 | 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 gui($id, &$out, &$handled) {
  28.                 if ($id === 'oidplus:srv_registration') {
  29.                         $handled = true;
  30.                         $out['title'] = _L('System registration settings');
  31.                         $out['icon'] = file_exists(__DIR__.'/icon_big.png') ? OIDplus::webpath(__DIR__).'icon_big.png' : '';
  32.  
  33.                         if (!OIDplus::authUtils()::isAdminLoggedIn()) {
  34.                                 $out['icon'] = 'img/error_big.png';
  35.                                 $out['text'] = '<p>'._L('You need to <a %1>log in</a> as administrator.',OIDplus::gui()->link('oidplus:login')).'</p>';
  36.                                 return;
  37.                         }
  38.  
  39.                         $out['text'] = file_get_contents(__DIR__ . '/info.tpl');
  40.  
  41.                         if (!OIDplus::getPkiStatus()) {
  42.                                 $out['text'] .= '<p><font color="red">'._L('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>';
  43.                         } else {
  44.                                 $out['text'] .= '<p><input type="button" onclick="openOidInPanel(\'oidplus:srvreg_status\');" value="'._L('Check status of the registration and collected data').'"></p>';
  45.  
  46.                                 if (OIDplus::baseConfig()->getValue('REGISTRATION_HIDE_SYSTEM', false)) {
  47.                                         $out['text'] .= '<p><font color="red"><b>'._L('Attention!').'</b> '._L('<code>REGISTRATION_HIDE_SYSTEM</code> is set in the local configuration file! Therefore, this system will not register itself, despite of the settings below.').'</font></p>';
  48.                                 }
  49.  
  50.                                 $out['text'] .= '<p>'._L('You can adjust your privacy level here').':</p><p><select name="reg_privacy" id="reg_privacy">';
  51.  
  52.                                 # ---
  53.  
  54.                                 $out['text'] .= '<option value="0"';
  55.                                 if (OIDplus::config()->getValue('reg_privacy') == 0) {
  56.                                         $out['text'] .= ' selected';
  57.                                 } else {
  58.                                         $out['text'] .= '';
  59.                                 }
  60.                                 $out['text'] .= '>'._L('0 = Register to directory service and automatically publish RA/OID data at oid-info.com').'</option>';
  61.  
  62.                                 # ---
  63.  
  64.                                 $out['text'] .= '<option value="1"';
  65.                                 if (OIDplus::config()->getValue('reg_privacy') == 1) {
  66.                                         $out['text'] .= ' selected';
  67.                                 } else {
  68.                                         $out['text'] .= '';
  69.                                 }
  70.                                 $out['text'] .= '>'._L('1 = Only register to directory service').'</option>';
  71.  
  72.                                 # ---
  73.  
  74.                                 $out['text'] .= '<option value="2"';
  75.                                 if (OIDplus::config()->getValue('reg_privacy') == 2) {
  76.                                         $out['text'] .= ' selected';
  77.                                 } else {
  78.                                         $out['text'] .= '';
  79.                                 }
  80.                                 $out['text'] .= '>'._L('2 = Hide system').'</option>';
  81.  
  82.                                 # ---
  83.  
  84.                                 $out['text'] .= '</select> <input type="button" value="'._L('Change').'" onclick="crudActionRegPrivacyUpdate()"></p>';
  85.  
  86.                                 $out['text'] .= '<p>'._L('After clicking "change", your OIDplus system will contact the ViaThinkSoft server to adjust (add or remove information) your privacy setting. This may take a few minutes.').'</p>';
  87.  
  88.                                 $out['text'] .= '<p>'._L('<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>.').'</p>';
  89.                         }
  90.                 }
  91.                 if ($id === 'oidplus:srvreg_status') {
  92.                         $handled = true;
  93.  
  94.                         $query = self::QUERY_LIVESTATUS_V1;
  95.  
  96.                         $payload = array(
  97.                                 "query" => $query, // we must repeat the query because we want to sign it
  98.                                 "system_id" => OIDplus::getSystemId(false)
  99.                         );
  100.  
  101.                         $signature = '';
  102.                         if (!@openssl_sign(json_encode($payload), $signature, OIDplus::config()->getValue('oidplus_private_key'))) {
  103.                                 throw new OIDplusException(_L('Signature failed'));
  104.                         }
  105.  
  106.                         $data = array(
  107.                                 "payload" => $payload,
  108.                                 "signature" => base64_encode($signature)
  109.                         );
  110.  
  111.                         $ch = curl_init();
  112.                         curl_setopt($ch, CURLOPT_URL, 'https://oidplus.viathinksoft.com/reg2/query.php');
  113.                         curl_setopt($ch, CURLOPT_POST, 1);
  114.                         curl_setopt($ch, CURLOPT_POSTFIELDS, "query=$query&data=".base64_encode(json_encode($data)));
  115.                         curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  116.                         curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
  117.                         curl_setopt($ch, CURLOPT_AUTOREFERER, true);
  118.                         if (!($res = @curl_exec($ch))) {
  119.                                 throw new OIDplusException(_L('Communication with ViaThinkSoft server failed: %1',curl_error($ch)));
  120.                         }
  121.                         curl_close($ch);
  122.                         // die("RES: $res\n");
  123.                         // if ($res == 'OK') ...
  124.  
  125.                         $out['title'] = _L('Registration live status');
  126.                         $out['text']  = '<p><a '.OIDplus::gui()->link('oidplus:srv_registration').'><img src="img/arrow_back.png" width="16"> '._L('Go back to registration settings').'</a></p>' .
  127.                                         $res;
  128.                 }
  129.         }
  130.  
  131.         public function sendRegistrationQuery($privacy_level=null) {
  132.                 if (is_null($privacy_level)) {
  133.                         $privacy_level = OIDplus::config()->getValue('reg_privacy');
  134.                 }
  135.  
  136.                 $system_url = OIDplus::getSystemUrl();
  137.  
  138.                 // It is very important that we set the ping time NOW, because ViaThinkSoft might contact us during the ping,
  139.                 // and this would cause an endless loop!
  140.                 OIDplus::config()->setValue('reg_last_ping', time());
  141.  
  142.                 if (!OIDplus::getPkiStatus()) return false;
  143.  
  144.                 if ($privacy_level == 2) {
  145.                         // The user wants to unregister
  146.                         // but we only unregister if we are registered. Check this "anonymously" (i.e. without revealing our system ID)
  147.                         if (in_array(OIDplus::getSystemId(false), explode(';',file_get_contents('https://oidplus.viathinksoft.com/reg2/query.php?query='.self::QUERY_LISTALLSYSTEMIDS_V1)))) {
  148.                                 $query = self::QUERY_UNREGISTER_V1;
  149.  
  150.                                 $payload = array(
  151.                                         "query" => $query, // we must repeat the query because we want to sign it
  152.                                         "system_id" => OIDplus::getSystemId(false)
  153.                                 );
  154.  
  155.                                 $signature = '';
  156.                                 if (!@openssl_sign(json_encode($payload), $signature, OIDplus::config()->getValue('oidplus_private_key'))) {
  157.                                         return false; // throw new OIDplusException(_L('Signature failed'));
  158.                                 }
  159.  
  160.                                 $data = array(
  161.                                         "payload" => $payload,
  162.                                         "signature" => base64_encode($signature)
  163.                                 );
  164.  
  165.                                 $ch = curl_init();
  166.                                 curl_setopt($ch, CURLOPT_URL, 'https://oidplus.viathinksoft.com/reg2/query.php');
  167.                                 curl_setopt($ch, CURLOPT_POST, 1);
  168.                                 curl_setopt($ch, CURLOPT_POSTFIELDS, "query=$query&data=".base64_encode(json_encode($data)));
  169.                                 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  170.                                 curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
  171.                                 curl_setopt($ch, CURLOPT_AUTOREFERER, true);
  172.                                 if (!($res = @curl_exec($ch))) {
  173.                                         return false; // throw new OIDplusException(_L('Communication with ViaThinkSoft server failed: %1',curl_error($ch)));
  174.                                 }
  175.                                 curl_close($ch);
  176.                                 // die("RES: $res\n");
  177.                                 // if ($res == 'OK') ...
  178.                         }
  179.                 } else {
  180.                         if ($privacy_level == 0) {
  181.                                 if (class_exists('OIDplusPageAdminOIDInfoExport')) {
  182.                                         ob_start();
  183.                                         OIDplusPageAdminOIDInfoExport::outputXML(false); // no online check, because the query should be short (since the query is done while a visitor waits for the response)
  184.                                         $oidinfo_xml = ob_get_contents();
  185.                                         ob_end_clean();
  186.                                 } else {
  187.                                         $oidinfo_xml = false;
  188.                                 }
  189.                         } else {
  190.                                 $oidinfo_xml = false;
  191.                         }
  192.  
  193.                         $query = self::QUERY_REGISTER_V1;
  194.  
  195.                         $root_oids = array();
  196.                         foreach (OIDplus::getEnabledObjectTypes() as $ot) {
  197.                                 if ($ot::ns() == 'oid') {
  198.                                         $res = OIDplus::db()->query("select id from ###objects where " .
  199.                                                                     "parent = 'oid:' " .
  200.                                                                     "order by ".OIDplus::db()->natOrder('id'));
  201.                                         while ($row = $res->fetch_array()) {
  202.                                                 $root_oids[] = substr($row['id'],strlen('oid:'));
  203.                                         }
  204.                                 }
  205.                         }
  206.                         $payload = array(
  207.                                 "query" => $query, // we must repeat the query because we want to sign it
  208.                                 "privacy_level" => $privacy_level,
  209.                                 "system_id" => OIDplus::getSystemId(false),
  210.                                 "public_key" => OIDplus::config()->getValue('oidplus_public_key'),
  211.                                 "system_url" => $system_url,
  212.                                 "hide_system_url" => 0,
  213.                                 "hide_public_key" => 0,
  214.                                 "admin_email" => OIDplus::config()->getValue('admin_email'),
  215.                                 "system_title" => OIDplus::config()->getValue('system_title'),
  216.                                 "oidinfo_xml" => @base64_encode($oidinfo_xml),
  217.                                 "root_oids" => $root_oids,
  218.                                 "system_version" => OIDplus::getVersion(),
  219.                                 "system_install_type" => OIDplus::getInstallType()
  220.                         );
  221.  
  222.                         $signature = '';
  223.                         if (!@openssl_sign(json_encode($payload), $signature, OIDplus::config()->getValue('oidplus_private_key'))) {
  224.                                         return false; // throw new OIDplusException(_L('Signature failed'));
  225.                         }
  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.                         if (!($res = @curl_exec($ch))) {
  240.                                 return false; // throw new OIDplusException(_L('Communication with ViaThinkSoft server failed: %1',curl_error($ch)));
  241.                         }
  242.                         curl_close($ch);
  243.  
  244.                         if ($res === 'HASH_CONFLICT') {
  245.                                 OIDplus::logger()->log("[WARN]A!", "Removing SystemID and key pair because there is a hash conflict with another OIDplus system!");
  246.  
  247.                                 // Delete the system ID since we have a conflict with the 31-bit hash!
  248.                                 OIDplus::config()->setValue('oidplus_private_key', '');
  249.                                 OIDplus::config()->setValue('oidplus_public_key', '');
  250.  
  251.                                 // Try to generate a new system ID
  252.                                 OIDplus::getPkiStatus(true);
  253.  
  254.                                 // Enforce a new registration attempt at the next run
  255.                                 // We will not try again here, because that might lead to an endless loop if the VTS server would always return 'HASH_CONFLCIT'
  256.                                 OIDplus::config()->setValue('reg_last_ping', 0);
  257.                         }
  258.  
  259.                         // die("RES: $res\n");
  260.                         // if ($res == 'OK') ...
  261.                 }
  262.         }
  263.  
  264.         public function init($html=true) {
  265.                 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) {
  266.                         if (($value != '0') && ($value != '1') && ($value != '2')) {
  267.                                 throw new OIDplusException(_L('Please enter either 0, 1 or 2.'));
  268.                         }
  269.                         // Now do a recheck and notify the ViaThinkSoft server
  270.                         if (($value == 2) || !OIDplus::baseConfig()->getValue('REGISTRATION_HIDE_SYSTEM', false)) {
  271.                                 OIDplus::config()->setValue('reg_last_ping', 0);
  272.                                 $this->sendRegistrationQuery($value);
  273.                         }
  274.                 });
  275.                 OIDplus::config()->prepareConfigKey('reg_ping_interval', 'Registration ping interval (in seconds)', '3600', OIDplusConfig::PROTECTION_HIDDEN, function($value) {
  276.  
  277.                 });
  278.                 OIDplus::config()->prepareConfigKey('reg_last_ping', 'Last ping to ViaThinkSoft directory services', '0', OIDplusConfig::PROTECTION_HIDDEN, function($value) {
  279.  
  280.                 });
  281.  
  282.                 // Is it time to register / renew the directory entry?
  283.                 // Note: REGISTRATION_HIDE_SYSTEM is an undocumented constant that can be put in the userdata/baseconfig/config.inc.php files of a test system accessing the same database as the productive system that is registered.
  284.                 // 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)
  285.  
  286.                 if (!OIDplus::baseConfig()->getValue('REGISTRATION_HIDE_SYSTEM', false)) {
  287.                         $privacy_level = OIDplus::config()->getValue('reg_privacy');
  288.  
  289.                         if (php_sapi_name() !== 'cli') { // don't register when called from CLI, otherwise the oidinfo XML can't convert relative links into absolute links
  290.                                 if ((time()-OIDplus::config()->getValue('reg_last_ping') >= OIDplus::config()->getValue('reg_ping_interval'))) {
  291.                                         $this->sendRegistrationQuery();
  292.                                 }
  293.                         }
  294.                 }
  295.         }
  296.  
  297.         public function tree(&$json, $ra_email=null, $nonjs=false, $req_goto='') {
  298.                 if (!OIDplus::authUtils()::isAdminLoggedIn()) return false;
  299.  
  300.                 if (file_exists(__DIR__.'/treeicon.png')) {
  301.                         $tree_icon = OIDplus::webpath(__DIR__).'treeicon.png';
  302.                 } else {
  303.                         $tree_icon = null; // default icon (folder)
  304.                 }
  305.  
  306.                 $json[] = array(
  307.                         'id' => 'oidplus:srv_registration',
  308.                         'icon' => $tree_icon,
  309.                         'text' => _L('System registration')
  310.                 );
  311.  
  312.                 return true;
  313.         }
  314.  
  315.         public function tree_search($request) {
  316.                 return false;
  317.         }
  318.  
  319.         public function implementsFeature($id) {
  320.                 if (strtolower($id) == '1.3.6.1.4.1.37476.2.5.2.3.1') return true; // oobeEntry
  321.                 return false;
  322.         }
  323.  
  324.         public function oobeEntry($step, $do_edits, &$errors_happened)/*: void*/ {
  325.                 // Interface 1.3.6.1.4.1.37476.2.5.2.3.1
  326.  
  327.                 echo '<p><u>'._L('Step %1: System registration and automatic publishing (optional)',$step).'</u></p>';
  328.  
  329.                 echo file_get_contents(__DIR__ . '/info.tpl');
  330.  
  331.                 if (!function_exists('curl_exec')) {
  332.                         echo '<p><font color="red">';
  333.                         echo _L('Note: The "CURL" PHP extension is not installed at your system. Please enable the PHP extension <code>php_curl</code>.');
  334.                         echo _L('Therefore, you <b>cannot</b> register your OIDplus instance now.');
  335.                         echo '</font></p>';
  336.                         return;
  337.                 }
  338.  
  339.                 $testurl = 'https://www.google.com/';
  340.                 $ch = curl_init();
  341.                 curl_setopt($ch, CURLOPT_URL, $testurl);
  342.                 curl_setopt($ch, CURLOPT_HEADER, TRUE);
  343.                 curl_setopt($ch, CURLOPT_NOBODY, TRUE);
  344.                 curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
  345.                 curl_exec($ch);
  346.                 $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  347.                 curl_close($ch);
  348.                 if (!$httpCode) {
  349.                         echo '<p><font color="red">';
  350.                         echo _L('Note: The "CURL" PHP extension cannot access HTTPS webpages. Therefore, you cannot use this feature. Please download <a href="https://curl.haxx.se/ca/cacert.pem">cacert.pem</a>, place it somewhere and then adjust the setting <code>curl.cainfo</code> in PHP.ini.');
  351.                         echo _L('Therefore, you <b>cannot</b> register your OIDplus instance now.');
  352.                         echo '</font></p>';
  353.                         return;
  354.                 }
  355.  
  356.                 $pki_status = OIDplus::getPkiStatus();
  357.  
  358.                 if (!$pki_status) {
  359.                         echo '<p><font color="red">';
  360.                         echo _L('Note: Your system could not generate a private/public key pair. (OpenSSL is probably missing on your system).');
  361.                         echo _L('Therefore, you <b>cannot</b> register your OIDplus instance now.');
  362.                         echo '</font></p>';
  363.                         return;
  364.                 }
  365.  
  366.                 echo '<p>'._L('Privacy level').':</p><select name="reg_privacy" id="reg_privacy">';
  367.  
  368.                 # ---
  369.  
  370.                 echo '<option value="0"';
  371.                 if (isset($_REQUEST['sent'])) {
  372.                         if (isset($_REQUEST['reg_privacy']) && ($_REQUEST['reg_privacy'] == 0)) echo ' selected';
  373.                 } else {
  374.                         if ((OIDplus::config()->getValue('reg_privacy') == 0) || !OIDplus::config()->getValue('reg_wizard_done')) {
  375.                                 echo ' selected';
  376.                         } else {
  377.                                 echo '';
  378.                         }
  379.                 }
  380.                 echo '>'._L('0 = Register to directory service and automatically publish RA/OID data at oid-info.com').'</option>';
  381.  
  382.                 # ---
  383.  
  384.                 echo '<option value="1"';
  385.                 if (isset($_REQUEST['sent'])) {
  386.                         if (isset($_REQUEST['reg_privacy']) && ($_REQUEST['reg_privacy'] == 1)) echo ' selected';
  387.                 } else {
  388.                         if ((OIDplus::config()->getValue('reg_privacy') == 1)) {
  389.                                 echo ' selected';
  390.                         } else {
  391.                                 echo '';
  392.                         }
  393.                 }
  394.                 echo '>'._L('1 = Only register to directory service').'</option>';
  395.  
  396.                 # ---
  397.  
  398.                 echo '<option value="2"';
  399.                 if (isset($_REQUEST['sent'])) {
  400.                         if (isset($_REQUEST['reg_privacy']) && ($_REQUEST['reg_privacy'] == 2)) echo ' selected';
  401.                 } else {
  402.                         if ((OIDplus::config()->getValue('reg_privacy') == 2)) {
  403.                                 echo ' selected';
  404.                         } else {
  405.                                 echo '';
  406.                         }
  407.                 }
  408.                 echo '>'._L('2 = Hide system').'</option>';
  409.  
  410.                 # ---
  411.  
  412.                 echo '</select>';
  413.  
  414.                 $msg = '';
  415.                 if ($do_edits) {
  416.                         try {
  417.                                 OIDplus::config()->setValue('reg_privacy', $_REQUEST['reg_privacy']);
  418.                         } catch (Exception $e) {
  419.                                 $msg = $e->getMessage();
  420.                                 $errors_happened = true;
  421.                         }
  422.                 }
  423.                 echo ' <font color="red"><b>'.$msg.'</b></font>';
  424.  
  425.                 echo '<p>'._L('<i>Privacy information:</i> This setting can always be changed in the administrator login / control panel.').'<br>';
  426.                 echo _L('<a %1>Click here</a> for more information about privacy related topics.','href="../../../res/OIDplus/privacy_documentation.html" target="_blank"');
  427.                 echo '</p>';
  428.         }
  429.  
  430. }