Subversion Repositories oidplus

Rev

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