Subversion Repositories oidplus

Rev

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