Subversion Repositories oidplus

Rev

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