Completed
Push — master ( 15c9ac...32d63a )
by Marcus
02:17
created

Resetpassword::handlePasswordReset()   B

Complexity

Conditions 6
Paths 9

Size

Total Lines 29
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 29
rs 8.439
cc 6
eloc 21
nc 9
nop 2
1
<?php
2
3
/*
4
    HCSF - A multilingual CMS and Shopsystem
5
    Copyright (C) 2014  Marcus Haase - [email protected]
6
7
    This program is free software: you can redistribute it and/or modify
8
    it under the terms of the GNU General Public License as published by
9
    the Free Software Foundation, either version 3 of the License, or
10
    (at your option) any later version.
11
12
    This program is distributed in the hope that it will be useful,
13
    but WITHOUT ANY WARRANTY; without even the implied warranty of
14
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
    GNU General Public License for more details.
16
17
    You should have received a copy of the GNU General Public License
18
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
19
 */
20
21
namespace HaaseIT\HCSF\Controller\Customer;
22
23
use HaaseIT\HCSF\HelperConfig;
24
use Zend\ServiceManager\ServiceManager;
25
26
/**
27
 * Class Resetpassword
28
 * @package HaaseIT\HCSF\Controller\Customer
29
 */
30
class Resetpassword extends Base
31
{
32
    /**
33
     * @var \HaaseIT\Toolbox\Textcat
34
     */
35
    private $textcats;
36
37
    /**
38
     * @var \PDO
39
     */
40
    private $db;
41
42
    /**
43
     * Resetpassword constructor.
44
     * @param ServiceManager $serviceManager
45
     */
46
    public function __construct(ServiceManager $serviceManager)
47
    {
48
        parent::__construct($serviceManager);
49
        $this->textcats = $serviceManager->get('textcats');
50
        $this->db = $serviceManager->get('db');
51
    }
52
53
    /**
54
     *
55
     */
56
    public function preparePage()
57
    {
58
        $this->P = new \HaaseIT\HCSF\CorePage($this->serviceManager);
59
        $this->P->cb_pagetype = 'content';
60
61
        if (\HaaseIT\HCSF\Customer\Helper::getUserData()) {
62
            $this->P->oPayload->cl_html = $this->textcats->T('denied_default');
63
        } else {
64
            $getemail = filter_input(INPUT_GET, 'email', FILTER_SANITIZE_EMAIL);
65
            $getkey = filter_input(INPUT_GET, 'key', FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_LOW);
66
            if (empty($getkey) || empty($getemail) || !filter_input(INPUT_GET, 'email', FILTER_VALIDATE_EMAIL)) {
67
                $this->P->oPayload->cl_html = $this->textcats->T('denied_default');
68
            } else {
69
                $sql = 'SELECT * FROM customer WHERE cust_email = :email AND cust_pwresetcode = :pwresetcode AND cust_pwresetcode != \'\'';
70
71
                $hResult = $this->db->prepare($sql);
72
                $hResult->bindValue(':email', $getemail, \PDO::PARAM_STR);
73
                $hResult->bindValue(':pwresetcode', $getkey, \PDO::PARAM_STR);
74
                $hResult->execute();
75
                if ($hResult->rowCount() !== 1) {
76
                    $this->P->oPayload->cl_html = $this->textcats->T('denied_default');
77
                } else {
78
                    $aErr = [];
79
                    $aResult = $hResult->fetch();
80
                    $iTimestamp = time();
81
                    if ($aResult['cust_pwresettimestamp'] < $iTimestamp - strtotime('1 Day', 0)) {
82
                        $this->P->oPayload->cl_html = $this->textcats->T('pwreset_error_expired');
83
                    } else {
84
                        $this->P->cb_customcontenttemplate = 'customer/resetpassword';
85
                        $this->P->cb_customdata['pwreset']['minpwlength'] = HelperConfig::$customer['minimum_length_password'];
86 View Code Duplication
                        if (filter_input(INPUT_POST, 'doSend') === 'yes') {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
87
                            $aErr = $this->handlePasswordReset($aErr, $aResult['cust_id']);
88
                            if (count($aErr) === 0) {
89
                                $this->P->cb_customdata['pwreset']['showsuccessmessage'] = true;
90
                            } else {
91
                                $this->P->cb_customdata['pwreset']['errors'] = $aErr;
92
                            }
93
                        }
94
                    }
95
                }
96
            }
97
        }
98
    }
99
100
    /**
101
     * @param $aErr
102
     * @param $iID
103
     * @return array
104
     */
105
    private function handlePasswordReset($aErr, $iID) {
106
        $postpwd = filter_input(INPUT_POST, 'pwd');
107
        if (!empty($postpwd)) {
108
            if (strlen($postpwd) < HelperConfig::$customer['minimum_length_password']) {
109
                $aErr[] = 'pwlength';
110
            }
111
            if ($postpwd !== filter_input(INPUT_POST, 'pwdc')) {
112
                $aErr[] = 'pwmatch';
113
            }
114
            if (count($aErr) == 0) {
115
                $sEnc = password_hash($postpwd, PASSWORD_DEFAULT);
116
                $aData = [
117
                    'cust_password' => $sEnc,
118
                    'cust_pwresetcode' => '',
119
                    'cust_id' => $iID,
120
                ];
121
                $sql = \HaaseIT\Toolbox\DBTools::buildPSUpdateQuery($aData, 'customer', 'cust_id');
122
                $hResult = $this->db->prepare($sql);
123
                foreach ($aData as $sKey => $sValue) {
124
                    $hResult->bindValue(':'.$sKey, $sValue);
125
                }
126
                $hResult->execute();
127
            }
128
        } else {
129
            $aErr[] = 'nopw';
130
        }
131
132
        return $aErr;
133
    }
134
}
135