Subversion Repositories oidplus

Rev

Rev 241 | 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 OIDplusPageRaChangeEMail extends OIDplusPagePlugin {
  23.         public function type() {
  24.                 return 'ra';
  25.         }
  26.  
  27.         public static function getPluginInformation() {
  28.                 $out = array();
  29.                 $out['name'] = 'Change E-Mail';
  30.                 $out['author'] = 'ViaThinkSoft';
  31.                 $out['version'] = null;
  32.                 $out['descriptionHTML'] = null;
  33.                 return $out;
  34.         }
  35.  
  36.         public function priority() {
  37.                 return 102;
  38.         }
  39.  
  40.         public function action(&$handled) {
  41.                 if (isset($_POST["action"]) && ($_POST["action"] == "change_ra_email")) {
  42.                         $handled = true;
  43.  
  44.                         if (!OIDplus::config()->getValue('allow_ra_email_change') && !OIDplus::authUtils()::isAdminLoggedIn()) {
  45.                                 throw new OIDplusException('This functionality has been disabled by the administrator.');
  46.                         }
  47.  
  48.                         $old_email = $_POST['old_email'];
  49.                         $new_email = $_POST['new_email'];
  50.  
  51.                         if (!OIDplus::authUtils()::isRaLoggedIn($old_email) && !OIDplus::authUtils()::isAdminLoggedIn()) {
  52.                                 throw new OIDplusException('Authentification error. Please log in as the RA to update its email address.');
  53.                         }
  54.  
  55.                         if (!OIDplus::mailUtils()->validMailAddress($new_email)) {
  56.                                 throw new OIDplusException('eMail address is invalid.');
  57.                         }
  58.  
  59.                         $res = OIDplus::db()->query("select * from ".OIDPLUS_TABLENAME_PREFIX."ra where email = ?", array($old_email));
  60.                         if ($res->num_rows() == 0) {
  61.                                 throw new OIDplusException('eMail address does not exist anymore. It was probably already changed.');
  62.                         }
  63.  
  64.                         $res = OIDplus::db()->query("select * from ".OIDPLUS_TABLENAME_PREFIX."ra where email = ?", array($new_email));
  65.                         if ($res->num_rows() > 0) {
  66.                                 throw new OIDplusException('eMail address is already used by another RA. To merge accounts, please contact the superior RA of your objects and request an owner change of your objects.');
  67.                         }
  68.  
  69.                         if (OIDplus::authUtils()::isAdminLoggedIn()) {
  70.                                 OIDplus::logger()->log("RA($old_email)!+RA($new_email)!+A!", "Admin changed email address '$old_email' to '$new_email'");
  71.  
  72.                                 $ra_was_logged_in = OIDplus::authUtils()::isRaLoggedIn($old_email);
  73.  
  74.                                 $ra = new OIDplusRA($old_email);
  75.                                 $ra->change_email($new_email);
  76.  
  77.                                 OIDplus::db()->query("update ".OIDPLUS_TABLENAME_PREFIX."objects set ra_email = ? where ra_email = ?", array($new_email, $old_email));
  78.  
  79.                                 if ($ra_was_logged_in) {
  80.                                         OIDplus::authUtils()->raLogout($old_email);
  81.                                         OIDplus::authUtils()->raLogin($new_email);
  82.                                 }
  83.  
  84.                                 echo json_encode(array("status" => 0));
  85.                         } else {
  86.                                 OIDplus::logger()->log("RA($old_email)!+RA($new_email)!", "Requested email change from '$old_email' to '$new_email'");
  87.  
  88.                                 $timestamp = time();
  89.                                 $activate_url = OIDplus::getSystemUrl() . '?goto='.urlencode('oidplus:activate_new_ra_email$'.$old_email.'$'.$new_email.'$'.$timestamp.'$'.OIDplus::authUtils()::makeAuthKey('activate_new_ra_email;'.$old_email.';'.$new_email.';'.$timestamp));
  90.  
  91.                                 $message = file_get_contents(__DIR__ . '/change_request_email.tpl');
  92.                                 $message = str_replace('{{SYSTEM_URL}}', OIDplus::getSystemUrl(), $message);
  93.                                 $message = str_replace('{{SYSTEM_TITLE}}', OIDplus::config()->systemTitle(), $message);
  94.                                 $message = str_replace('{{ADMIN_EMAIL}}', OIDplus::config()->getValue('admin_email'), $message);
  95.                                 $message = str_replace('{{OLD_EMAIL}}', $old_email, $message);
  96.                                 $message = str_replace('{{NEW_EMAIL}}', $new_email, $message);
  97.                                 $message = str_replace('{{ACTIVATE_URL}}', $activate_url, $message);
  98.                                 OIDplus::mailUtils()->sendMail($new_email, OIDplus::config()->systemTitle().' - Change email request', $message);
  99.  
  100.                                 echo json_encode(array("status" => 0));
  101.                         }
  102.                 }
  103.  
  104.                 if (isset($_POST["action"]) && ($_POST["action"] == "activate_new_ra_email")) {
  105.                         $handled = true;
  106.  
  107.                         if (!OIDplus::config()->getValue('allow_ra_email_change')) {
  108.                                 throw new OIDplusException('This functionality has been disabled by the administrator.');
  109.                         }
  110.  
  111.                         $old_email = $_POST['old_email'];
  112.                         $new_email = $_POST['new_email'];
  113.                         $password = $_POST['password'];
  114.  
  115.                         $auth = $_POST['auth'];
  116.                         $timestamp = $_POST['timestamp'];
  117.  
  118.                         if (!OIDplus::authUtils()::validateAuthKey('activate_new_ra_email;'.$old_email.';'.$new_email.';'.$timestamp, $auth)) {
  119.                                 throw new OIDplusException('Invalid auth key');
  120.                         }
  121.  
  122.                         if ((OIDplus::config()->getValue('max_ra_email_change_time') > 0) && (time()-$timestamp > OIDplus::config()->maxEmailChangeTime())) {
  123.                                 throw new OIDplusException('Activation link expired!');
  124.                         }
  125.  
  126.                         $res = OIDplus::db()->query("select * from ".OIDPLUS_TABLENAME_PREFIX."ra where email = ?", array($old_email));
  127.                         if ($res->num_rows() == 0) {
  128.                                 throw new OIDplusException('eMail address does not exist anymore. It was probably already changed.');
  129.                         }
  130.  
  131.                         $res = OIDplus::db()->query("select * from ".OIDPLUS_TABLENAME_PREFIX."ra where email = ?", array($new_email));
  132.                         if ($res->num_rows() > 0) {
  133.                                 throw new OIDplusException('eMail address is already used by another RA. To merge accounts, please contact the superior RA of your objects and request an owner change of your objects.');
  134.                         }
  135.  
  136.                         $ra = new OIDplusRA($old_email);
  137.                         if (!$ra->checkPassword($password)) {
  138.                                 throw new OIDplusException('Wrong password');
  139.                         }
  140.  
  141.                         $ra->change_email($new_email);
  142.  
  143.                         OIDplus::db()->query("update ".OIDPLUS_TABLENAME_PREFIX."objects set ra_email = ? where ra_email = ?", array($new_email, $old_email));
  144.  
  145.                         OIDplus::authUtils()->raLogout($old_email);
  146.                         OIDplus::authUtils()->raLogin($new_email);
  147.  
  148.                         OIDplus::logger()->log("RA($old_email)!", "Changed email address from '$old_email' to '$new_email'");
  149.                         OIDplus::logger()->log("RA($new_email)!", "RA '$old_email' has changed its email address to '$new_email'");
  150.  
  151.                         $message = file_get_contents(__DIR__ . '/email_change_confirmation.tpl');
  152.                         $message = str_replace('{{SYSTEM_URL}}', OIDplus::getSystemUrl(), $message);
  153.                         $message = str_replace('{{SYSTEM_TITLE}}', OIDplus::config()->systemTitle(), $message);
  154.                         $message = str_replace('{{ADMIN_EMAIL}}', OIDplus::config()->getValue('admin_email'), $message);
  155.                         $message = str_replace('{{OLD_EMAIL}}', $old_email, $message);
  156.                         $message = str_replace('{{NEW_EMAIL}}', $new_email, $message);
  157.                         OIDplus::mailUtils()->sendMail($old_email, OIDplus::config()->systemTitle().' - eMail address changed', $message);
  158.  
  159.                         echo json_encode(array("status" => 0));
  160.                 }
  161.         }
  162.  
  163.         public function init($html=true) {
  164.                 OIDplus::config()->prepareConfigKey('max_ra_email_change_time', 'Max RA email change time in seconds (0 = infinite)', '0', 0, 1);
  165.                 OIDplus::config()->prepareConfigKey('allow_ra_email_change', 'Allow that RAs change their email address (0/1)', '1', 0, 1);
  166.         }
  167.  
  168.         public function cfgSetValue($name, $value) {
  169.                 if ($name == 'allow_ra_email_change') {
  170.                         if (($value != '0') && ($value != '1')) {
  171.                                 throw new OIDplusException("Please enter either 0 or 1.");
  172.                         }
  173.                 }
  174.                 if (($name == 'max_ra_invite_time') || ($name == 'max_ra_pwd_reset_time') || ($name == 'max_ra_email_change_time')) {
  175.                         if (!is_numeric($value) || ($value < 0)) {
  176.                                 throw new OIDplusException("Please enter a valid value.");
  177.                         }
  178.                 }
  179.         }
  180.  
  181.         public function gui($id, &$out, &$handled) {
  182.                 if (explode('$',$id)[0] == 'oidplus:change_ra_email') {
  183.                         $handled = true;
  184.                         $out['title'] = 'Change RA email';
  185.                         $out['icon'] = file_exists(__DIR__.'/icon_big.png') ? OIDplus::webpath(__DIR__).'icon_big.png' : '';
  186.  
  187.                         $ra_email = explode('$',$id)[1];
  188.  
  189.                         $res = OIDplus::db()->query("select * from ".OIDPLUS_TABLENAME_PREFIX."ra where email = ?", array($ra_email));
  190.                         if ($res->num_rows() == 0) {
  191.                                 $out['icon'] = 'img/error_big.png';
  192.                                 $out['text'] = 'RA <b>'.htmlentities($ra_email).'</b> does not exist';
  193.                         } else if (!OIDplus::config()->getValue('allow_ra_email_change') && !OIDplus::authUtils()::isAdminLoggedIn()) {
  194.                                 $out['icon'] = 'img/error_big.png';
  195.                                 $out['text'] = '<p>This functionality has been disabled by the administrator.</p>';
  196.                         } else if (!OIDplus::authUtils()::isRaLoggedIn($ra_email) && !OIDplus::authUtils()::isAdminLoggedIn()) {
  197.                                 $out['icon'] = 'img/error_big.png';
  198.                                 $out['text'] = '<p>You need to <a '.OIDplus::gui()->link('oidplus:login').'>log in</a> as the requested RA <b>'.htmlentities($ra_email).'</b> or as admin.</p>';
  199.                         } else {
  200.                                 if (OIDplus::authUtils()::isAdminLoggedIn()) {
  201.                                         $out['text'] .= '<form id="changeRaEmailForm" onsubmit="return changeRaEmailFormOnSubmit(true);">';
  202.                                         $out['text'] .= '<input type="hidden" id="old_email" value="'.htmlentities($ra_email).'"/><br>';
  203.                                         $out['text'] .= '<div><label class="padding_label">Old address:</label><b>'.htmlentities($ra_email).'</b></div>';
  204.                                         $out['text'] .= '<div><label class="padding_label">New address:</label><input type="text" id="new_email" value=""/></div>';
  205.                                         $out['text'] .= '<br><input type="submit" value="Change password"> (admin does not require email verification)</form>';
  206.                                 } else {
  207.                                         $out['text'] .= '<form id="changeRaEmailForm" onsubmit="return changeRaEmailFormOnSubmit(false);">';
  208.                                         $out['text'] .= '<input type="hidden" id="old_email" value="'.htmlentities($ra_email).'"/><br>';
  209.                                         $out['text'] .= '<div><label class="padding_label">Old address:</label><b>'.htmlentities($ra_email).'</b></div>';
  210.                                         $out['text'] .= '<div><label class="padding_label">New address:</label><input type="text" id="new_email" value=""/></div>';
  211.                                         $out['text'] .= '<br><input type="submit" value="Send new activation email"></form>';
  212.                                 }
  213.                         }
  214.                 } else if (explode('$',$id)[0] == 'oidplus:activate_new_ra_email') {
  215.                         $handled = true;
  216.  
  217.                         $old_email = explode('$',$id)[1];
  218.                         $new_email = explode('$',$id)[2];
  219.                         $timestamp = explode('$',$id)[3];
  220.                         $auth = explode('$',$id)[4];
  221.  
  222.                         if (!OIDplus::config()->getValue('allow_ra_email_change') && !OIDplus::authUtils()::isAdminLoggedIn()) {
  223.                                 $out['icon'] = 'img/error_big.png';
  224.                                 $out['text'] = '<p>This functionality has been disabled by the administrator.</p>';
  225.                         } else {
  226.                                 $out['title'] = 'Perform email address change';
  227.                                 $out['icon'] = file_exists(__DIR__.'/icon_big.png') ? OIDplus::webpath(__DIR__).'icon_big.png' : '';
  228.  
  229.                                 $res = OIDplus::db()->query("select * from ".OIDPLUS_TABLENAME_PREFIX."ra where email = ?", array($old_email));
  230.                                 if ($res->num_rows() == 0) {
  231.                                         $out['icon'] = 'img/error_big.png';
  232.                                         $out['text'] = 'eMail address does not exist anymore. It was probably already changed.';
  233.                                 } else {
  234.                                         $res = OIDplus::db()->query("select * from ".OIDPLUS_TABLENAME_PREFIX."ra where email = ?", array($new_email));
  235.                                         if ($res->num_rows() > 0) {
  236.                                                 $out['icon'] = 'img/error_big.png';
  237.                                                 $out['text'] = 'eMail address is already used by another RA. To merge accounts, please contact the superior RA of your objects and request an owner change of your objects.';
  238.                                         } else {
  239.                                                 if (!OIDplus::authUtils()::validateAuthKey('activate_new_ra_email;'.$old_email.';'.$new_email.';'.$timestamp, $auth)) {
  240.                                                         $out['icon'] = 'img/error_big.png';
  241.                                                         $out['text'] = 'Invalid authorization. Is the URL OK?';
  242.                                                 } else {
  243.                                                         $out['text'] = '<p>Old eMail-Address: <b>'.$old_email.'</b></p>
  244.                                                         <p>New eMail-Address: <b>'.$new_email.'</b></p>
  245.  
  246.                                                          <form id="activateNewRaEmailForm" onsubmit="return activateNewRaEmailFormOnSubmit();">
  247.                                                     <input type="hidden" id="old_email" value="'.htmlentities($old_email).'"/>
  248.                                                     <input type="hidden" id="new_email" value="'.htmlentities($new_email).'"/>
  249.                                                     <input type="hidden" id="timestamp" value="'.htmlentities($timestamp).'"/>
  250.                                                     <input type="hidden" id="auth" value="'.htmlentities($auth).'"/>
  251.  
  252.                                                     <div><label class="padding_label">Please verify your password:</label><input type="password" id="password" value=""/></div>
  253.                                                     <br><input type="submit" value="Change email address">
  254.                                                   </form>';
  255.                                                 }
  256.                                         }
  257.                                 }
  258.                         }
  259.                 }
  260.         }
  261.  
  262.         public function tree(&$json, $ra_email=null, $nonjs=false, $req_goto='') {
  263.                 if (file_exists(__DIR__.'/treeicon.png')) {
  264.                         $tree_icon = OIDplus::webpath(__DIR__).'treeicon.png';
  265.                 } else {
  266.                         $tree_icon = null; // default icon (folder)
  267.                 }
  268.  
  269.                 $json[] = array(
  270.                         'id' => 'oidplus:change_ra_email$'.$ra_email,
  271.                         'icon' => $tree_icon,
  272.                         'text' => 'Change email address'
  273.                 );
  274.  
  275.                 return true;
  276.         }
  277.  
  278.         public function tree_search($request) {
  279.                 return false;
  280.         }
  281. }
  282.