Subversion Repositories oidplus

Rev

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