Subversion Repositories oidplus

Rev

Rev 1282 | 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 OIDplusPagePublicForgotPassword extends OIDplusPagePluginPublic {
  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 == 'forgot_password') {
  37.                         _CheckParamExists($params, 'email');
  38.                         $email = $params['email'];
  39.  
  40.                         if (!OIDplus::mailUtils()->validMailAddress($email)) {
  41.                                 throw new OIDplusException(_L('Invalid email address'));
  42.                         }
  43.  
  44.                         OIDplus::getActiveCaptchaPlugin()->captchaVerify($params, 'captcha');
  45.  
  46.                         OIDplus::logger()->log("V2:[WARN]RA(%1)", "A new password for '%1' was requested (forgot password)", $email);
  47.  
  48.                         $activate_url = OIDplus::webpath(null,OIDplus::PATH_ABSOLUTE_CANONICAL) . '?goto='.urlencode('oidplus:reset_password$'.$email.'$'.OIDplus::authUtils()->makeAuthKey(['93a16dbe-f4fb-11ed-b67e-3c4a92df8582',$email]));
  49.  
  50.                         $message = $this->getForgotPasswordText($params['email']);
  51.                         $message = str_replace('{{ACTIVATE_URL}}', $activate_url, $message);
  52.  
  53.                         OIDplus::mailUtils()->sendMail($email, OIDplus::config()->getValue('system_title').' - Password reset request', $message);
  54.  
  55.                         return array("status" => 0);
  56.  
  57.                 } else if ($actionID == 'reset_password') {
  58.  
  59.                         _CheckParamExists($params, 'password1');
  60.                         _CheckParamExists($params, 'password2');
  61.                         _CheckParamExists($params, 'email');
  62.                         _CheckParamExists($params, 'auth');
  63.  
  64.                         $password1 = $params['password1'];
  65.                         $password2 = $params['password2'];
  66.                         $email = $params['email'];
  67.                         $auth = $params['auth'];
  68.  
  69.                         if (!OIDplus::authUtils()->validateAuthKey(['93a16dbe-f4fb-11ed-b67e-3c4a92df8582',$email], $auth, OIDplus::config()->getValue('max_ra_pwd_reset_time',-1))) {
  70.                                 throw new OIDplusException(_L('Invalid or expired authentication key'));
  71.                         }
  72.  
  73.                         if ($password1 !== $password2) {
  74.                                 throw new OIDplusException(_L('Passwords do not match'));
  75.                         }
  76.  
  77.                         if (strlen($password1) < OIDplus::config()->getValue('ra_min_password_length')) {
  78.                                 $minlen = OIDplus::config()->getValue('ra_min_password_length');
  79.                                 throw new OIDplusException(_L('Password is too short. Need at least %1 characters',$minlen));
  80.                         }
  81.  
  82.                         OIDplus::logger()->log("V2:[INFO]RA(%1)", "RA '%1' has reset his password (forgot passwort)", $email);
  83.  
  84.                         $ra = new OIDplusRA($email);
  85.                         $ra->change_password($password1);
  86.  
  87.                         return array("status" => 0);
  88.                 } else {
  89.                         return parent::action($actionID, $params);
  90.                 }
  91.         }
  92.  
  93.         /**
  94.          * @param bool $html
  95.          * @return void
  96.          * @throws OIDplusException
  97.          */
  98.         public function init(bool $html=true) {
  99.                 OIDplus::config()->prepareConfigKey('max_ra_pwd_reset_time', 'Max RA password reset time in seconds (0 = infinite)', '0', OIDplusConfig::PROTECTION_EDITABLE, function($value) {
  100.                         if (!is_numeric($value) || ($value < 0)) {
  101.                                 throw new OIDplusException(_L('Please enter a valid value.'));
  102.                         }
  103.                 });
  104.         }
  105.  
  106.         /**
  107.          * @param string $id
  108.          * @param array $out
  109.          * @param bool $handled
  110.          * @return void
  111.          * @throws OIDplusException
  112.          */
  113.         public function gui(string $id, array &$out, bool &$handled) {
  114.                 if (explode('$',$id)[0] == 'oidplus:forgot_password') {
  115.                         $handled = true;
  116.  
  117.                         $out['title'] = _L('Forgot password');
  118.                         $out['icon'] = OIDplus::webpath(__DIR__,OIDplus::PATH_RELATIVE).'img/forgot_password_icon.png';
  119.  
  120.                         try {
  121.                                 $out['text'] .= '<p>'._L('Please enter the email address of your account, and information about the password reset will be sent to you.').'</p>
  122.                                   <form id="forgotPasswordForm" action="javascript:void(0);" onsubmit="return OIDplusPagePublicForgotPassword.forgotPasswordFormOnSubmit();">
  123.                                     '._L('E-Mail').': <input type="text" id="email" value=""/><br><br>
  124.                                     '.OIDplus::getActiveCaptchaPlugin()->captchaGenerate().'
  125.                                     <br>
  126.                                     <input type="submit" value="'._L('Send recovery information').'">
  127.                                   </form>';
  128.  
  129.                         } catch (\Exception $e) {
  130.  
  131.                                 $htmlmsg = $e instanceof OIDplusException ? $e->getHtmlMessage() : htmlentities($e->getMessage());
  132.                                 throw new OIDplusHtmlException(_L('Error: %1',$htmlmsg), $out['title']);
  133.  
  134.                         }
  135.                 } else if (explode('$',$id)[0] == 'oidplus:reset_password') {
  136.                         $handled = true;
  137.  
  138.                         $email = explode('$',$id)[1];
  139.                         $auth = explode('$',$id)[2];
  140.  
  141.                         $out['title'] = _L('Reset password');
  142.                         $out['icon'] = OIDplus::webpath(__DIR__,OIDplus::PATH_RELATIVE).'img/reset_password_icon.png';
  143.  
  144.                         if (!OIDplus::authUtils()->validateAuthKey(['93a16dbe-f4fb-11ed-b67e-3c4a92df8582',$email], $auth, OIDplus::config()->getValue('max_ra_pwd_reset_time',-1))) {
  145.                                 throw new OIDplusException(_L('Invalid authorization. Is the URL OK?'), $out['title']);
  146.                         } else {
  147.                                 $out['text'] = '<p>'._L('E-Mail-Address: %1','<b>'.$email.'</b>').'</p>
  148.  
  149.                                   <form id="resetPasswordForm" action="javascript:void(0);" onsubmit="return OIDplusPagePublicForgotPassword.resetPasswordFormOnSubmit();">
  150.                                     <input type="hidden" id="email" value="'.htmlentities($email).'"/>
  151.                                     <input type="hidden" id="auth" value="'.htmlentities($auth).'"/>
  152.                                     <div><label class="padding_label">'._L('New password').':</label><input type="password" id="password1" value=""/></div>
  153.                                     <div><label class="padding_label">'._L('Repeat').':</label><input type="password" id="password2" value=""/></div>
  154.                                     <br><input type="submit" value="'._L('Change password').'">
  155.                                   </form>';
  156.                         }
  157.                 }
  158.         }
  159.  
  160.         /**
  161.          * @param array $out
  162.          * @return void
  163.          */
  164.         public function publicSitemap(array &$out) {
  165.                 $out[] = 'oidplus:forgot_password';
  166.         }
  167.  
  168.         /**
  169.          * @param array $json
  170.          * @param string|null $ra_email
  171.          * @param bool $nonjs
  172.          * @param string $req_goto
  173.          * @return bool
  174.          */
  175.         public function tree(array &$json, string $ra_email=null, bool $nonjs=false, string $req_goto=''): bool {
  176.                 return false;
  177.         }
  178.  
  179.         /**
  180.          * @param string $email
  181.          * @return string
  182.          * @throws OIDplusException
  183.          */
  184.         private function getForgotPasswordText(string $email): string {
  185.                 $res = OIDplus::db()->query("select * from ###ra where email = ?", array($email));
  186.                 if (!$res->any()) {
  187.                         throw new OIDplusException(_L('This RA does not exist.'));
  188.                 }
  189.  
  190.                 $message = file_get_contents(__DIR__ . '/forgot_password.tpl');
  191.  
  192.                 // Resolve stuff
  193.                 // Note: {{ACTIVATE_URL}} will be resolved in ajax.php
  194.  
  195.                 $message = str_replace('{{SYSTEM_URL}}', OIDplus::webpath(null,OIDplus::PATH_ABSOLUTE_CANONICAL), $message);
  196.  
  197.                 return str_replace('{{ADMIN_EMAIL}}', OIDplus::config()->getValue('admin_email'), $message);
  198.         }
  199.  
  200.         /**
  201.          * @param string $request
  202.          * @return array|false
  203.          */
  204.         public function tree_search(string $request) {
  205.                 return false;
  206.         }
  207. }
  208.