XCL Web Application Platform 2.5.0
The XoopsCube Legacy Project
Loading...
Searching...
No Matches
EncryptPassword.class.php
1<?php
2if (!defined('XOOPS_ROOT_PATH')) {
3 exit();
4}
5
6// load password_compat - https://github.com/ircmaxell/password_compat
7if (version_compare(PHP_VERSION, '5.5.0', '<') && (version_compare(PHP_VERSION, '5.3.7', '>=') || defined('PHP53_BCRYPT_Y2_FIXED'))) {
8 include_once dirname(__DIR__, 2) . '/compat/password.php';
9}
10
11class User_EncryptPassword extends XCube_ActionFilter
12{
13 private bool $useNativeHashing = false;
14
15 public function User_EncryptPassword(&$controller)
16 {
17 self::__construct($controller);
18 }
19
20 public function __construct(&$controller)
21 {
22 parent::__construct($controller);
23 $this->useNativeHashing = (function_exists('password_hash') && function_exists('password_needs_rehash'));
24 }
25
26 public function preFilter()
27 {
28 $this->mController->mRoot->mDelegateManager->add('User.EncryptPassword', [$this, 'encryptPassword']);
29 $this->mController->mRoot->mDelegateManager->add('User.PasswordVerify', [$this, 'passwordVerify']);
30 $this->mController->mRoot->mDelegateManager->add('User.PasswordNeedsRehash', [$this, 'needsRehash']);
31 }
32
33 public function encryptPassword(&$password)
34 {
35 $input = $password;
36 if ($this->useNativeHashing) {
37 $password = password_hash($input, PASSWORD_DEFAULT);
38 } else {
39 $password = md5($input);
40 }
41 }
42
43 public function passwordVerify(&$result, $password, $hash)
44 {
45 $result = false;
46 if (32 === strlen($hash)) {
47 $result = md5($password) === $hash;
48 } else if ($this->useNativeHashing) {
49 $result = password_verify($password, $hash);
50 }
51 }
52
53 public function needsRehash(&$needs, $val)
54 {
55 if ($this->useNativeHashing) {
56 $needs = password_needs_rehash($val, PASSWORD_DEFAULT);
57 } else {
58 // md5 length = 32
59 $needs = 32 !== strlen($val);
60 }
61 }
62}
preFilter()
[Abstract] Executes the logic, when the controller executes preFilter().
__construct(&$controller)
Constructor.