Subversion Repositories oidplus

Rev

Rev 496 | Go to most recent revision | Blame | Last modification | View Log | RSS feed

  1. <?php
  2.  
  3. /*
  4.  * OIDplus 2.0
  5.  * Copyright 2019 - 2021 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('INSIDE_OIDPLUS')) die();
  21.  
  22. class OIDplusPageRaChangeEMail extends OIDplusPagePluginRa {
  23.  
  24.         public function action($actionID, $params) {
  25.                 if ($actionID == 'change_ra_email') {
  26.                         if (!OIDplus::config()->getValue('allow_ra_email_change') && !OIDplus::authUtils()::isAdminLoggedIn()) {
  27.                                 throw new OIDplusException(_L('This functionality has been disabled by the administrator.'));
  28.                         }
  29.  
  30.                         $old_email = $params['old_email'];
  31.                         $new_email = $params['new_email'];
  32.  
  33.                         $ra = new OIDplusRA($old_email);
  34.                         if ($ra->isPasswordLess() && !OIDplus::authUtils()::isAdminLoggedIn()) {
  35.                                 throw new OIDplusException(_L('E-Mail-Address cannot be changed because this user does not have a password'));
  36.                         }
  37.  
  38.                         if (!OIDplus::authUtils()::isRaLoggedIn($old_email) && !OIDplus::authUtils()::isAdminLoggedIn()) {
  39.                                 throw new OIDplusException(_L('Authentication error. Please log in as admin, or as the RA to update its email address.'));
  40.                         }
  41.  
  42.                         if (!OIDplus::mailUtils()->validMailAddress($new_email)) {
  43.                                 throw new OIDplusException(_L('eMail address is invalid.'));
  44.                         }
  45.  
  46.                         $res = OIDplus::db()->query("select * from ###ra where email = ?", array($old_email));
  47.                         if ($res->num_rows() == 0) {
  48.                                 throw new OIDplusException(_L('eMail address does not exist anymore. It was probably already changed.'));
  49.                         }
  50.  
  51.                         $res = OIDplus::db()->query("select * from ###ra where email = ?", array($new_email));
  52.                         if ($res->num_rows() > 0) {
  53.                                 throw new OIDplusException(_L('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.'));
  54.                         }
  55.  
  56.                         if (OIDplus::authUtils()::isAdminLoggedIn()) {
  57.                                 $ra_was_logged_in = OIDplus::authUtils()::isRaLoggedIn($old_email);
  58.  
  59.                                 $ra = new OIDplusRA($old_email);
  60.  
  61.                                 // Change RA email
  62.                                 $ra->change_email($new_email);
  63.                                 OIDplus::logger()->log("[WARN]RA($old_email)!+[INFO]RA($new_email)!+[OK]A!", "Admin changed email address '$old_email' to '$new_email'");
  64.  
  65.                                 // Change objects
  66.                                 $res = OIDplus::db()->query("select id from ###objects where ra_email = ?", array($old_email));
  67.                                 while ($row = $res->fetch_array()) {
  68.                                         OIDplus::logger()->log("[INFO]OID(".$row['id'].")+SUPOID(".$row['id'].")", "Admin changed email address of RA '$old_email' (owner of ".$row['id'].") to '$new_email'");
  69.                                 }
  70.                                 OIDplus::db()->query("update ###objects set ra_email = ? where ra_email = ?", array($new_email, $old_email));
  71.  
  72.                                 // Re-login
  73.                                 if ($ra_was_logged_in) {
  74.                                         OIDplus::authUtils()->raLogout($old_email);
  75.                                         OIDplus::authUtils()->raLogin($new_email);
  76.                                 }
  77.  
  78.                                 return array("status" => 0);
  79.                         } else {
  80.                                 OIDplus::logger()->log("[INFO]RA($old_email)!+RA($new_email)!", "Requested email address change from '$old_email' to '$new_email'");
  81.  
  82.                                 $timestamp = time();
  83.                                 $activate_url = OIDplus::webpath(null,false) . '?goto='.urlencode('oidplus:activate_new_ra_email$'.$old_email.'$'.$new_email.'$'.$timestamp.'$'.OIDplus::authUtils()::makeAuthKey('activate_new_ra_email;'.$old_email.';'.$new_email.';'.$timestamp));
  84.  
  85.                                 $message = file_get_contents(__DIR__ . '/change_request_email.tpl');
  86.                                 $message = str_replace('{{SYSTEM_URL}}', OIDplus::webpath(null,false), $message);
  87.                                 $message = str_replace('{{SYSTEM_TITLE}}', OIDplus::config()->getValue('system_title'), $message);
  88.                                 $message = str_replace('{{ADMIN_EMAIL}}', OIDplus::config()->getValue('admin_email'), $message);
  89.                                 $message = str_replace('{{OLD_EMAIL}}', $old_email, $message);
  90.                                 $message = str_replace('{{NEW_EMAIL}}', $new_email, $message);
  91.                                 $message = str_replace('{{ACTIVATE_URL}}', $activate_url, $message);
  92.                                 OIDplus::mailUtils()->sendMail($new_email, OIDplus::config()->getValue('system_title').' - Change email request', $message);
  93.  
  94.                                 return array("status" => 0);
  95.                         }
  96.                 }
  97.  
  98.                 else if ($actionID == 'activate_new_ra_email') {
  99.                         if (!OIDplus::config()->getValue('allow_ra_email_change')) {
  100.                                 throw new OIDplusException(_L('This functionality has been disabled by the administrator.'));
  101.                         }
  102.  
  103.                         $old_email = $params['old_email'];
  104.                         $new_email = $params['new_email'];
  105.                         $password = $params['password'];
  106.  
  107.                         $auth = $params['auth'];
  108.                         $timestamp = $params['timestamp'];
  109.  
  110.                         $ra = new OIDplusRA($old_email);
  111.                         if ($ra->isPasswordLess() && !OIDplus::authUtils()::isAdminLoggedIn()) {
  112.                                 throw new OIDplusException(_L('E-Mail-Address cannot be changed because this user does not have a password'));
  113.                         }
  114.  
  115.                         if (!OIDplus::authUtils()::validateAuthKey('activate_new_ra_email;'.$old_email.';'.$new_email.';'.$timestamp, $auth)) {
  116.                                 throw new OIDplusException(_L('Invalid auth key'));
  117.                         }
  118.  
  119.                         if ((OIDplus::config()->getValue('max_ra_email_change_time') > 0) && (time()-$timestamp > OIDplus::config()->maxEmailChangeTime())) {
  120.                                 throw new OIDplusException(_L('Activation link expired!'));
  121.                         }
  122.  
  123.                         $res = OIDplus::db()->query("select * from ###ra where email = ?", array($old_email));
  124.                         if ($res->num_rows() == 0) {
  125.                                 throw new OIDplusException(_L('eMail address does not exist anymore. It was probably already changed.'));
  126.                         }
  127.  
  128.                         $res = OIDplus::db()->query("select * from ###ra where email = ?", array($new_email));
  129.                         if ($res->num_rows() > 0) {
  130.                                 throw new OIDplusException(_L('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.'));
  131.                         }
  132.  
  133.                         $ra = new OIDplusRA($old_email);
  134.                         if (!$ra->isPasswordLess()) {
  135.                                 if (!$ra->checkPassword($password)) {
  136.                                         throw new OIDplusException(_L('Wrong password'));
  137.                                 }
  138.                         }
  139.  
  140.                         // Change address of RA
  141.                         $ra->change_email($new_email);
  142.                         OIDplus::logger()->log("[OK]RA($new_email)!+RA($old_email)!", "RA '$old_email' has changed their email address to '$new_email'");
  143.  
  144.                         // Change objects
  145.                         $res = OIDplus::db()->query("select id from ###objects where ra_email = ?", array($old_email));
  146.                         while ($row = $res->fetch_array()) {
  147.                                 OIDplus::logger()->log("[INFO]OID(".$row['id'].")+SUPOID(".$row['id'].")", "RA '$old_email' (owner of ".$row['id'].") has changed their email address to '$new_email'");
  148.                         }
  149.                         OIDplus::db()->query("update ###objects set ra_email = ? where ra_email = ?", array($new_email, $old_email));
  150.  
  151.                         // Re-login
  152.                         OIDplus::authUtils()->raLogout($old_email);
  153.                         OIDplus::authUtils()->raLogin($new_email);
  154.  
  155.                         // Send email
  156.                         $message = file_get_contents(__DIR__ . '/email_change_confirmation.tpl');
  157.                         $message = str_replace('{{SYSTEM_URL}}', OIDplus::webpath(null,false), $message);
  158.                         $message = str_replace('{{SYSTEM_TITLE}}', OIDplus::config()->getValue('system_title'), $message);
  159.                         $message = str_replace('{{ADMIN_EMAIL}}', OIDplus::config()->getValue('admin_email'), $message);
  160.                         $message = str_replace('{{OLD_EMAIL}}', $old_email, $message);
  161.                         $message = str_replace('{{NEW_EMAIL}}', $new_email, $message);
  162.                         OIDplus::mailUtils()->sendMail($old_email, OIDplus::config()->getValue('system_title').' - eMail address changed', $message);
  163.  
  164.                         return array("status" => 0);
  165.                 } else {
  166.                         throw new OIDplusException(_L('Unknown action ID'));
  167.                 }
  168.         }
  169.  
  170.         public function init($html=true) {
  171.                 OIDplus::config()->prepareConfigKey('max_ra_email_change_time', 'Max RA email change time in seconds (0 = infinite)', '0', OIDplusConfig::PROTECTION_EDITABLE, function($value) {
  172.                         if (!is_numeric($value) || ($value < 0)) {
  173.                                 throw new OIDplusException(_L('Please enter a valid value.'));
  174.                         }
  175.                 });
  176.                 OIDplus::config()->prepareConfigKey('allow_ra_email_change', 'Allow that RAs change their email address (0/1)', '1', OIDplusConfig::PROTECTION_EDITABLE, function($value) {
  177.                         if (($value != '0') && ($value != '1')) {
  178.                                 throw new OIDplusException(_L('Please enter a valid value (0=no, 1=yes).'));
  179.                         }
  180.                 });
  181.         }
  182.  
  183.         public function gui($id, &$out, &$handled) {
  184.                 if (explode('$',$id)[0] == 'oidplus:change_ra_email') {
  185.                         $handled = true;
  186.  
  187.                         $ra_email = explode('$',$id)[1];
  188.  
  189.                         $out['title'] = _L('Change RA email');
  190.                         $out['icon'] = file_exists(__DIR__.'/icon_big.png') ? OIDplus::webpath(__DIR__).'icon_big.png' : '';
  191.  
  192.                         if (!OIDplus::authUtils()::isRaLoggedIn($ra_email) && !OIDplus::authUtils()::isAdminLoggedIn()) {
  193.                                 $out['icon'] = 'img/error_big.png';
  194.                                 $out['text'] = '<p>'._L('You need to <a %1>log in</a> as the requested RA %2 or as admin.',OIDplus::gui()->link('oidplus:login'),'<b>'.htmlentities($ra_email).'</b>').'</p>';
  195.                                 return;
  196.                         }
  197.  
  198.                         $res = OIDplus::db()->query("select * from ###ra where email = ?", array($ra_email));
  199.                         if ($res->num_rows() == 0) {
  200.                                 $out['icon'] = 'img/error_big.png';
  201.                                 $out['text'] = _L('RA "%1" does not exist','<b>'.htmlentities($ra_email).'</b>');
  202.                                 return;
  203.                         }
  204.  
  205.                         if (!OIDplus::config()->getValue('allow_ra_email_change') && !OIDplus::authUtils()::isAdminLoggedIn()) {
  206.                                 $out['icon'] = 'img/error_big.png';
  207.                                 $out['text'] = '<p>'._L('This functionality has been disabled by the administrator.').'</p>';
  208.                                 return;
  209.                         }
  210.  
  211.                         if (OIDplus::authUtils()::isAdminLoggedIn()) {
  212.                                 $ra = new OIDplusRA($ra_email);
  213.                                 if ($ra->isPasswordLess()) {
  214.                                         $out['text'] .= '<p>'._L('Attention: This user does not have a password because they log in using LDAP or Google OAuth etc.').'</p>';
  215.                                         $out['text'] .= '<p>'._L('If you change the email address, the user cannot log in anymore, because the LDAP/OAuth plugin identifies the user via email address, not OpenID.').'</p>';
  216.                                         $out['text'] .= '<p>'._L('If you want to change the email address of the user, please <a %1>define a password</a> for them, so that they can use the regular login method using their new email address.', OIDplus::gui()->link('oidplus:change_ra_password$'.$ra_email)).'</p>';
  217.                                 }
  218.  
  219.                                 $out['text'] .= '<form id="changeRaEmailForm" action="javascript:void(0);" action="javascript:void(0);" onsubmit="return changeRaEmailFormOnSubmit(true);">';
  220.                                 $out['text'] .= '<input type="hidden" id="old_email" value="'.htmlentities($ra_email).'"/><br>';
  221.                                 $out['text'] .= '<div><label class="padding_label">'._L('Old address').':</label><b>'.htmlentities($ra_email).'</b></div>';
  222.                                 $out['text'] .= '<div><label class="padding_label">'._L('New address').':</label><input type="text" id="new_email" value=""/></div>';
  223.                                 $out['text'] .= '<br><input type="submit" value="'._L('Change password').'"> '._L('(admin does not require email verification)').'</form>';
  224.                         } else {
  225.                                 $ra = new OIDplusRA($ra_email);
  226.                                 if ($ra->isPasswordLess()) {
  227.                                         $out['icon'] = 'img/error_big.png';
  228.                                         $out['text'] .= '<p>'._L('Attention: You are logged in without password (via LDAP or Google OAuth etc.).').'</p>';
  229.                                         $out['text'] .= '<p>'._L('Therefore, you cannot change your email address, otherwise you would love access to your account!').'</p>';
  230.                                         $out['text'] .= '<p>'._L('If you want to change your email address, then please <a %1>setup a password</a> first, and then use the regular login method to log in using your new email address.', OIDplus::gui()->link('oidplus:change_ra_password$'.$ra_email)).'</p>';
  231.                                         return;
  232.                                 }
  233.  
  234.                                 $out['text'] .= '<form id="changeRaEmailForm" action="javascript:void(0);" action="javascript:void(0);" onsubmit="return changeRaEmailFormOnSubmit(false);">';
  235.                                 $out['text'] .= '<input type="hidden" id="old_email" value="'.htmlentities($ra_email).'"/><br>';
  236.                                 $out['text'] .= '<div><label class="padding_label">'._L('Old address').':</label><b>'.htmlentities($ra_email).'</b></div>';
  237.                                 $out['text'] .= '<div><label class="padding_label">'._L('New address').':</label><input type="text" id="new_email" value=""/></div>';
  238.                                 $out['text'] .= '<br><input type="submit" value="'._L('Send new activation email').'"></form>';
  239.                         }
  240.                 } else if (explode('$',$id)[0] == 'oidplus:activate_new_ra_email') {
  241.                         $handled = true;
  242.  
  243.                         $old_email = explode('$',$id)[1];
  244.                         $new_email = explode('$',$id)[2];
  245.                         $timestamp = explode('$',$id)[3];
  246.                         $auth = explode('$',$id)[4];
  247.  
  248.                         $out['title'] = _L('Perform email address change');
  249.                         $out['icon'] = file_exists(__DIR__.'/icon_big.png') ? OIDplus::webpath(__DIR__).'icon_big.png' : '';
  250.  
  251.                         if (!OIDplus::config()->getValue('allow_ra_email_change') && !OIDplus::authUtils()::isAdminLoggedIn()) {
  252.                                 $out['icon'] = 'img/error_big.png';
  253.                                 $out['text'] = '<p>'._L('This functionality has been disabled by the administrator.').'</p>';
  254.                                 return;
  255.                         }
  256.  
  257.                         $ra = new OIDplusRA($old_email);
  258.                         if ($ra->isPasswordLess() && !OIDplus::authUtils()::isAdminLoggedIn()) {
  259.                                 $out['icon'] = 'img/error_big.png';
  260.                                 $out['text'] = '<p>'._L('E-Mail-Address cannot be changed because this user does not have a password').'</p>';
  261.                                 return;
  262.                         }
  263.  
  264.                         $res = OIDplus::db()->query("select * from ###ra where email = ?", array($old_email));
  265.                         if ($res->num_rows() == 0) {
  266.                                 $out['icon'] = 'img/error_big.png';
  267.                                 $out['text'] = _L('eMail address does not exist anymore. It was probably already changed.');
  268.                         } else {
  269.                                 $res = OIDplus::db()->query("select * from ###ra where email = ?", array($new_email));
  270.                                 if ($res->num_rows() > 0) {
  271.                                         $out['icon'] = 'img/error_big.png';
  272.                                         $out['text'] = _L('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.');
  273.                                 } else {
  274.                                         if (!OIDplus::authUtils()::validateAuthKey('activate_new_ra_email;'.$old_email.';'.$new_email.';'.$timestamp, $auth)) {
  275.                                                 $out['icon'] = 'img/error_big.png';
  276.                                                 $out['text'] = _L('Invalid authorization. Is the URL OK?');
  277.                                         } else {
  278.                                                 $out['text'] = '<p>'._L('Old eMail-Address').': <b>'.$old_email.'</b></p>
  279.                                                 <p>'._L('New eMail-Address').': <b>'.$new_email.'</b></p>
  280.  
  281.                                                  <form id="activateNewRaEmailForm" action="javascript:void(0);" onsubmit="return activateNewRaEmailFormOnSubmit();">
  282.                                             <input type="hidden" id="old_email" value="'.htmlentities($old_email).'"/>
  283.                                             <input type="hidden" id="new_email" value="'.htmlentities($new_email).'"/>
  284.                                             <input type="hidden" id="timestamp" value="'.htmlentities($timestamp).'"/>
  285.                                             <input type="hidden" id="auth" value="'.htmlentities($auth).'"/>
  286.  
  287.                                             <div><label class="padding_label">'._L('Please verify your password').':</label><input type="password" id="password" value=""/></div>
  288.                                             <br><input type="submit" value="'._L('Change email address').'">
  289.                                           </form>';
  290.                                         }
  291.                                 }
  292.                         }
  293.                 }
  294.         }
  295.  
  296.         public function tree(&$json, $ra_email=null, $nonjs=false, $req_goto='') {
  297.                 if (!$ra_email) return false;
  298.                 if (!OIDplus::authUtils()::isRaLoggedIn($ra_email) && !OIDplus::authUtils()::isAdminLoggedIn()) return false;
  299.  
  300.                 if (file_exists(__DIR__.'/treeicon.png')) {
  301.                         $tree_icon = OIDplus::webpath(__DIR__).'treeicon.png';
  302.                 } else {
  303.                         $tree_icon = null; // default icon (folder)
  304.                 }
  305.  
  306.                 $json[] = array(
  307.                         'id' => 'oidplus:change_ra_email$'.$ra_email,
  308.                         'icon' => $tree_icon,
  309.                         'text' => _L('Change email address')
  310.                 );
  311.  
  312.                 return true;
  313.         }
  314.  
  315.         public function tree_search($request) {
  316.                 return false;
  317.         }
  318. }
  319.