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 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("RA($old_email)!+RA($new_email)!+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("RA($old_email)!+RA($new_email)!", "Requested email 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("RA($old_email)!", "Changed email address from '$old_email' to '$new_email'");
  131.                         OIDplus::logger()->log("RA($new_email)!", "RA '$old_email' has changed its email address to '$new_email'");
  132.  
  133.                         $message = file_get_contents(__DIR__ . '/email_change_confirmation.tpl');
  134.                         $message = str_replace('{{SYSTEM_URL}}', OIDplus::getSystemUrl(), $message);
  135.                         $message = str_replace('{{SYSTEM_TITLE}}', OIDplus::config()->getValue('system_title'), $message);
  136.                         $message = str_replace('{{ADMIN_EMAIL}}', OIDplus::config()->getValue('admin_email'), $message);
  137.                         $message = str_replace('{{OLD_EMAIL}}', $old_email, $message);
  138.                         $message = str_replace('{{NEW_EMAIL}}', $new_email, $message);
  139.                         OIDplus::mailUtils()->sendMail($old_email, OIDplus::config()->getValue('system_title').' - eMail address changed', $message);
  140.  
  141.                         echo json_encode(array("status" => 0));
  142.                 }
  143.         }
  144.  
  145.         public function init($html=true) {
  146.                 OIDplus::config()->prepareConfigKey('max_ra_email_change_time', 'Max RA email change time in seconds (0 = infinite)', '0', OIDplusConfig::PROTECTION_EDITABLE, function($value) {
  147.                         if (!is_numeric($value) || ($value < 0)) {
  148.                                 throw new OIDplusException("Please enter a valid value.");
  149.                         }
  150.                 });
  151.                 OIDplus::config()->prepareConfigKey('allow_ra_email_change', 'Allow that RAs change their email address (0/1)', '1', OIDplusConfig::PROTECTION_EDITABLE, function($value) {
  152.                         if (($value != '0') && ($value != '1')) {
  153.                                 throw new OIDplusException("Please enter either 0 or 1.");
  154.                         }
  155.                 });
  156.         }
  157.  
  158.         public function gui($id, &$out, &$handled) {
  159.                 if (explode('$',$id)[0] == 'oidplus:change_ra_email') {
  160.                         $handled = true;
  161.  
  162.                         $ra_email = explode('$',$id)[1];
  163.  
  164.                         $out['title'] = 'Change RA email';
  165.                         $out['icon'] = file_exists(__DIR__.'/icon_big.png') ? OIDplus::webpath(__DIR__).'icon_big.png' : '';
  166.  
  167.                         if (!OIDplus::authUtils()::isRaLoggedIn($ra_email) && !OIDplus::authUtils()::isAdminLoggedIn()) {
  168.                                 $out['icon'] = 'img/error_big.png';
  169.                                 $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>';
  170.                                 return;
  171.                         }
  172.  
  173.                         $res = OIDplus::db()->query("select * from ###ra where email = ?", array($ra_email));
  174.                         if ($res->num_rows() == 0) {
  175.                                 $out['icon'] = 'img/error_big.png';
  176.                                 $out['text'] = 'RA <b>'.htmlentities($ra_email).'</b> does not exist';
  177.                                 return;
  178.                         }
  179.                        
  180.                         if (!OIDplus::config()->getValue('allow_ra_email_change') && !OIDplus::authUtils()::isAdminLoggedIn()) {
  181.                                 $out['icon'] = 'img/error_big.png';
  182.                                 $out['text'] = '<p>This functionality has been disabled by the administrator.</p>';
  183.                                 return;
  184.                         }
  185.                        
  186.                         if (OIDplus::authUtils()::isAdminLoggedIn()) {
  187.                                 $out['text'] .= '<form id="changeRaEmailForm" onsubmit="return changeRaEmailFormOnSubmit(true);">';
  188.                                 $out['text'] .= '<input type="hidden" id="old_email" value="'.htmlentities($ra_email).'"/><br>';
  189.                                 $out['text'] .= '<div><label class="padding_label">Old address:</label><b>'.htmlentities($ra_email).'</b></div>';
  190.                                 $out['text'] .= '<div><label class="padding_label">New address:</label><input type="text" id="new_email" value=""/></div>';
  191.                                 $out['text'] .= '<br><input type="submit" value="Change password"> (admin does not require email verification)</form>';
  192.                         } else {
  193.                                 $out['text'] .= '<form id="changeRaEmailForm" onsubmit="return changeRaEmailFormOnSubmit(false);">';
  194.                                 $out['text'] .= '<input type="hidden" id="old_email" value="'.htmlentities($ra_email).'"/><br>';
  195.                                 $out['text'] .= '<div><label class="padding_label">Old address:</label><b>'.htmlentities($ra_email).'</b></div>';
  196.                                 $out['text'] .= '<div><label class="padding_label">New address:</label><input type="text" id="new_email" value=""/></div>';
  197.                                 $out['text'] .= '<br><input type="submit" value="Send new activation email"></form>';
  198.                         }
  199.                 } else if (explode('$',$id)[0] == 'oidplus:activate_new_ra_email') {
  200.                         $handled = true;
  201.  
  202.                         $old_email = explode('$',$id)[1];
  203.                         $new_email = explode('$',$id)[2];
  204.                         $timestamp = explode('$',$id)[3];
  205.                         $auth = explode('$',$id)[4];
  206.  
  207.                         if (!OIDplus::config()->getValue('allow_ra_email_change') && !OIDplus::authUtils()::isAdminLoggedIn()) {
  208.                                 $out['icon'] = 'img/error_big.png';
  209.                                 $out['text'] = '<p>This functionality has been disabled by the administrator.</p>';
  210.                                 return;
  211.                         }
  212.  
  213.                         $out['title'] = 'Perform email address change';
  214.                         $out['icon'] = file_exists(__DIR__.'/icon_big.png') ? OIDplus::webpath(__DIR__).'icon_big.png' : '';
  215.  
  216.                         $res = OIDplus::db()->query("select * from ###ra where email = ?", array($old_email));
  217.                         if ($res->num_rows() == 0) {
  218.                                 $out['icon'] = 'img/error_big.png';
  219.                                 $out['text'] = 'eMail address does not exist anymore. It was probably already changed.';
  220.                         } else {
  221.                                 $res = OIDplus::db()->query("select * from ###ra where email = ?", array($new_email));
  222.                                 if ($res->num_rows() > 0) {
  223.                                         $out['icon'] = 'img/error_big.png';
  224.                                         $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.';
  225.                                 } else {
  226.                                         if (!OIDplus::authUtils()::validateAuthKey('activate_new_ra_email;'.$old_email.';'.$new_email.';'.$timestamp, $auth)) {
  227.                                                 $out['icon'] = 'img/error_big.png';
  228.                                                 $out['text'] = 'Invalid authorization. Is the URL OK?';
  229.                                         } else {
  230.                                                 $out['text'] = '<p>Old eMail-Address: <b>'.$old_email.'</b></p>
  231.                                                 <p>New eMail-Address: <b>'.$new_email.'</b></p>
  232.  
  233.                                                  <form id="activateNewRaEmailForm" onsubmit="return activateNewRaEmailFormOnSubmit();">
  234.                                             <input type="hidden" id="old_email" value="'.htmlentities($old_email).'"/>
  235.                                             <input type="hidden" id="new_email" value="'.htmlentities($new_email).'"/>
  236.                                             <input type="hidden" id="timestamp" value="'.htmlentities($timestamp).'"/>
  237.                                             <input type="hidden" id="auth" value="'.htmlentities($auth).'"/>
  238.  
  239.                                             <div><label class="padding_label">Please verify your password:</label><input type="password" id="password" value=""/></div>
  240.                                             <br><input type="submit" value="Change email address">
  241.                                           </form>';
  242.                                         }
  243.                                 }
  244.                         }
  245.                 }
  246.         }
  247.  
  248.         public function tree(&$json, $ra_email=null, $nonjs=false, $req_goto='') {
  249.                 if (!$ra_email) return false;
  250.                 if (!OIDplus::authUtils()::isRaLoggedIn($ra_email) && !OIDplus::authUtils()::isAdminLoggedIn()) return false;
  251.                
  252.                 if (file_exists(__DIR__.'/treeicon.png')) {
  253.                         $tree_icon = OIDplus::webpath(__DIR__).'treeicon.png';
  254.                 } else {
  255.                         $tree_icon = null; // default icon (folder)
  256.                 }
  257.  
  258.                 $json[] = array(
  259.                         'id' => 'oidplus:change_ra_email$'.$ra_email,
  260.                         'icon' => $tree_icon,
  261.                         'text' => 'Change email address'
  262.                 );
  263.  
  264.                 return true;
  265.         }
  266.  
  267.         public function tree_search($request) {
  268.                 return false;
  269.         }
  270. }
  271.