Subversion Repositories oidplus

Rev

Rev 1267 | Rev 1283 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

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