Subversion Repositories oidplus

Rev

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