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 OIDplusPageRaInvite extends OIDplusPagePluginRa {
  21.  
  22.         public function action(&$handled) {
  23.                 if (isset($_POST["action"]) && ($_POST["action"] == "invite_ra")) {
  24.                         $handled = true;
  25.                         $email = $_POST['email'];
  26.  
  27.                         if (!OIDplus::mailUtils()->validMailAddress($email)) {
  28.                                 throw new OIDplusException('Invalid email address');
  29.                         }
  30.  
  31.                         if (OIDplus::baseConfig()->getValue('RECAPTCHA_ENABLED', false)) {
  32.                                 $secret=OIDplus::baseConfig()->getValue('RECAPTCHA_PRIVATE', '');
  33.                                 $response=$_POST["captcha"];
  34.                                 $verify=file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret={$secret}&response={$response}");
  35.                                 $captcha_success=json_decode($verify);
  36.                                 if ($captcha_success->success==false) {
  37.                                         throw new OIDplusException('Captcha wrong');
  38.                                 }
  39.                         }
  40.  
  41.                         $this->inviteSecurityCheck($email);
  42.                         // TODO: should we also log who has invited?
  43.                         OIDplus::logger()->log("RA($email)!", "RA '$email' has been invited");
  44.  
  45.                         $timestamp = time();
  46.                         $activate_url = OIDplus::getSystemUrl() . '?goto='.urlencode('oidplus:activate_ra$'.$email.'$'.$timestamp.'$'.OIDplus::authUtils()::makeAuthKey('activate_ra;'.$email.';'.$timestamp));
  47.  
  48.                         $message = $this->getInvitationText($email);
  49.                         $message = str_replace('{{ACTIVATE_URL}}', $activate_url, $message);
  50.  
  51.                         OIDplus::mailUtils()->sendMail($email, OIDplus::config()->getValue('system_title').' - Invitation', $message, OIDplus::config()->getValue('global_cc'));
  52.  
  53.                         echo json_encode(array("status" => 0));
  54.                 }
  55.  
  56.                 if (isset($_POST["action"]) && ($_POST["action"] == "activate_ra")) {
  57.                         $handled = true;
  58.  
  59.                         $password1 = $_POST['password1'];
  60.                         $password2 = $_POST['password2'];
  61.                         $email = $_POST['email'];
  62.                         $auth = $_POST['auth'];
  63.                         $timestamp = $_POST['timestamp'];
  64.  
  65.                         if (!OIDplus::authUtils()::validateAuthKey('activate_ra;'.$email.';'.$timestamp, $auth)) {
  66.                                 throw new OIDplusException('Invalid auth key');
  67.                         }
  68.  
  69.                         if ((OIDplus::config()->getValue('max_ra_invite_time') > 0) && (time()-$timestamp > OIDplus::config()->getValue('max_ra_invite_time'))) {
  70.                                 throw new OIDplusException('Invitation expired!');
  71.                         }
  72.  
  73.                         if ($password1 !== $password2) {
  74.                                 throw new OIDplusException('Passwords are not equal');
  75.                         }
  76.  
  77.                         if (strlen($password1) < OIDplus::config()->getValue('ra_min_password_length')) {
  78.                                 throw new OIDplusException('Password is too short. Minimum password length: '.OIDplus::config()->getValue('ra_min_password_length'));
  79.                         }
  80.  
  81.                         OIDplus::logger()->log("RA($email)!", "RA '$email' has been registered due to invitation");
  82.  
  83.                         $ra = new OIDplusRA($email);
  84.                         $ra->register_ra($password1);
  85.  
  86.                         echo json_encode(array("status" => 0));
  87.                 }
  88.         }
  89.  
  90.         public function init($html=true) {
  91.                 OIDplus::config()->prepareConfigKey('max_ra_invite_time', 'Max RA invite time in seconds (0 = infinite)', '0', OIDplusConfig::PROTECTION_EDITABLE, function($value) {
  92.                         if (!is_numeric($value) || ($value < 0)) {
  93.                                 throw new OIDplusException("Please enter a valid value.");
  94.                         }
  95.                 });
  96.                 OIDplus::config()->prepareConfigKey('ra_invitation_enabled', 'May RAs be invited?', '1', OIDplusConfig::PROTECTION_EDITABLE, function($value) {
  97.                         if (($value != 0) && ($value != 1)) {
  98.                                 throw new OIDplusException("Please enter a valid value: 0 or 1.");
  99.                         }
  100.                 });
  101.         }
  102.  
  103.         public function gui($id, &$out, &$handled) {
  104.                 if (explode('$',$id)[0] == 'oidplus:invite_ra') {
  105.                         $handled = true;
  106.  
  107.                         $email = explode('$',$id)[1];
  108.                         $origin = explode('$',$id)[2];
  109.  
  110.                         $out['title'] = 'Invite a Registration Authority';
  111.  
  112.                         if (!OIDplus::config()->getValue('ra_invitation_enabled')) {
  113.                                 $out['icon'] = 'img/error_big.png';
  114.                                 $out['text'] = '<p>Invitations are disabled by the administrator.</p>';
  115.                                 return;
  116.                         }
  117.  
  118.                         $out['icon'] = OIDplus::webpath(__DIR__).'invite_ra_big.png';
  119.  
  120.                         try {
  121.                                 $this->inviteSecurityCheck($email);
  122.                                 $cont = $this->getInvitationText($email);
  123.  
  124.                                 $out['text'] .= '<p>You have chosen to invite <b>'.$email.'</b> as an Registration Authority. If you click "Send", the following email will be sent to '.$email.':</p><p><i>'.nl2br(htmlentities($cont)).'</i></p>
  125.                                   <form id="inviteForm" onsubmit="return inviteFormOnSubmit();">
  126.                                     <input type="hidden" id="email" value="'.htmlentities($email).'"/>
  127.                                     <input type="hidden" id="origin" value="'.htmlentities($origin).'"/>'.
  128.                                  (OIDplus::baseConfig()->getValue('RECAPTCHA_ENABLED', false) ?
  129.                                  '<script> grecaptcha.render(document.getElementById("g-recaptcha"), { "sitekey" : "'.OIDplus::baseConfig()->getValue('RECAPTCHA_PUBLIC', '').'" }); </script>'.
  130.                                  '<div id="g-recaptcha" class="g-recaptcha" data-sitekey="'.OIDplus::baseConfig()->getValue('RECAPTCHA_PUBLIC', '').'"></div>' : '').
  131.                                 ' <br>
  132.                                     <input type="submit" value="Send invitation">
  133.                                   </form>';
  134.  
  135.                         } catch (Exception $e) {
  136.  
  137.                                 $out['icon'] = 'img/error_big.png';
  138.                                 $out['text'] = "Error: ".$e->getMessage();
  139.  
  140.                         }
  141.                 } else if (explode('$',$id)[0] == 'oidplus:activate_ra') {
  142.                         $handled = true;
  143.  
  144.                         $email = explode('$',$id)[1];
  145.                         $timestamp = explode('$',$id)[2];
  146.                         $auth = explode('$',$id)[3];
  147.  
  148.                         $out['title'] = 'Register as Registration Authority';
  149.  
  150.                         if (!OIDplus::config()->getValue('ra_invitation_enabled')) {
  151.                                 $out['icon'] = 'img/error_big.png';
  152.                                 $out['text'] = '<p>Invitations are disabled by the administrator.</p>';
  153.                                 return;
  154.                         }
  155.  
  156.                         $out['icon'] = OIDplus::webpath(__DIR__).'activate_ra_big.png';
  157.  
  158.                         $res = OIDplus::db()->query("select * from ###ra where email = ?", array($email));
  159.                         if ($res->num_rows() > 0) {
  160.                                 $out['text'] = 'This RA is already registered and does not need to be invited.';
  161.                         } else {
  162.                                 if (!OIDplus::authUtils()::validateAuthKey('activate_ra;'.$email.';'.$timestamp, $auth)) {
  163.                                         $out['icon'] = 'img/error_big.png';
  164.                                         $out['text'] = 'Invalid authorization. Is the URL OK?';
  165.                                 } else {
  166.                                         // TODO: like in the FreeOID plugin, we could ask here at least for a name for the RA
  167.                                         $out['text'] = '<p>E-Mail-Adress: <b>'.$email.'</b></p>
  168.  
  169.                                           <form id="activateRaForm" onsubmit="return activateRaFormOnSubmit();">
  170.                                             <input type="hidden" id="email" value="'.htmlentities($email).'"/>
  171.                                             <input type="hidden" id="timestamp" value="'.htmlentities($timestamp).'"/>
  172.                                             <input type="hidden" id="auth" value="'.htmlentities($auth).'"/>
  173.                                             <div><label class="padding_label">New password:</label><input type="password" id="password1" value=""/></div>
  174.                                             <div><label class="padding_label">Repeat:</label><input type="password" id="password2" value=""/></div>
  175.                                             <br><input type="submit" value="Register">
  176.                                           </form>';
  177.                                 }
  178.                         }
  179.                 }
  180.         }
  181.  
  182.         public function tree(&$json, $ra_email=null, $nonjs=false, $req_goto='') {
  183.                 //if (!$ra_email) return false;
  184.                 //if (!OIDplus::authUtils()::isRaLoggedIn($ra_email) && !OIDplus::authUtils()::isAdminLoggedIn()) return false;
  185.  
  186.                 return false;
  187.         }
  188.  
  189.         private function inviteSecurityCheck($email) {
  190.                 $res = OIDplus::db()->query("select * from ###ra where email = ?", array($email));
  191.                 if ($res->num_rows() > 0) {
  192.                         throw new OIDplusException("This RA is already registered and does not need to be invited.");
  193.                 }
  194.  
  195.                 if (!OIDplus::authUtils()::isAdminLoggedIn()) {
  196.                         // Check if the RA may invite the user (i.e. the they are the parent of an OID of that person)
  197.                         $ok = false;
  198.                         $res = OIDplus::db()->query("select parent from ###objects where ra_email = ?", array($email));
  199.                         while ($row = $res->fetch_array()) {
  200.                                 $objParent = OIDplusObject::parse($row['parent']);
  201.                                 if (is_null($objParent)) throw new OIDplusException("Type of ".$row['parent']." unknown");
  202.                                 if ($objParent->userHasWriteRights()) {
  203.                                         $ok = true;
  204.                                 }
  205.                         }
  206.                         if (!$ok) {
  207.                                 throw new OIDplusException('You may not invite this RA. Maybe you need to log in again.');
  208.                         }
  209.                 }
  210.         }
  211.  
  212.         private function getInvitationText($email) {
  213.                 $list_of_oids = array();
  214.                 $res = OIDplus::db()->query("select id from ###objects where ra_email = ?", array($email));
  215.                 while ($row = $res->fetch_array()) {
  216.                         $list_of_oids[] = $row['id'];
  217.                 }
  218.  
  219.                 $message = file_get_contents(__DIR__ . '/invite_msg.tpl');
  220.  
  221.                 // Resolve stuff
  222.                 $message = str_replace('{{SYSTEM_URL}}', OIDplus::getSystemUrl(), $message);
  223.                 $message = str_replace('{{OID_LIST}}', implode("\n", $list_of_oids), $message);
  224.                 $message = str_replace('{{ADMIN_EMAIL}}', OIDplus::config()->getValue('admin_email'), $message);
  225.                 $message = str_replace('{{PARTY}}', OIDplus::authUtils()::isAdminLoggedIn() ? 'the system administrator' : 'a superior Registration Authority', $message);
  226.  
  227.                 // {{ACTIVATE_URL}} will be resolved in ajax.php
  228.  
  229.                 return $message;
  230.         }
  231.  
  232.         public function tree_search($request) {
  233.                 return false;
  234.         }
  235. }
  236.