Resetpassword::preparePage()   B
last analyzed

Complexity

Conditions 9
Paths 7

Size

Total Lines 43

Duplication

Lines 8
Ratio 18.6 %

Importance

Changes 0
Metric Value
dl 8
loc 43
rs 7.6764
c 0
b 0
f 0
cc 9
nc 7
nop 0
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 Zend\ServiceManager\ServiceManager;
24
25
/**
26
 * Class Resetpassword
27
 * @package HaaseIT\HCSF\Controller\Customer
28
 */
29
class Resetpassword extends Base
30
{
31
    /**
32
     * @var \HaaseIT\Toolbox\Textcat
33
     */
34
    private $textcats;
35
36
    /**
37
     * @var \PDO
38
     */
39
    private $db;
40
41
    /**
42
     * Resetpassword constructor.
43
     * @param ServiceManager $serviceManager
44
     */
45
    public function __construct(ServiceManager $serviceManager)
46
    {
47
        parent::__construct($serviceManager);
48
        $this->textcats = $serviceManager->get('textcats');
49
        $this->db = $serviceManager->get('db');
50
    }
51
52
    /**
53
     *
54
     */
55
    public function preparePage()
56
    {
57
        $this->P = new \HaaseIT\HCSF\CorePage($this->serviceManager);
58
        $this->P->cb_pagetype = 'content';
59
60
        if ($this->helperCustomer->getUserData()) {
61
            $this->P->oPayload->cl_html = $this->textcats->T('denied_default');
62
        } else {
63
            $getemail = filter_input(INPUT_GET, 'email', FILTER_SANITIZE_EMAIL);
64
            $getkey = filter_input(INPUT_GET, 'key', FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_LOW);
65
            if (empty($getkey) || empty($getemail) || !filter_input(INPUT_GET, 'email', FILTER_VALIDATE_EMAIL)) {
66
                $this->P->oPayload->cl_html = $this->textcats->T('denied_default');
67
            } else {
68
                $sql = 'SELECT * FROM customer WHERE cust_email = :email AND cust_pwresetcode = :pwresetcode AND cust_pwresetcode != \'\'';
69
70
                $hResult = $this->db->prepare($sql);
71
                $hResult->bindValue(':email', $getemail, \PDO::PARAM_STR);
72
                $hResult->bindValue(':pwresetcode', $getkey, \PDO::PARAM_STR);
73
                $hResult->execute();
74
                if ($hResult->rowCount() !== 1) {
75
                    $this->P->oPayload->cl_html = $this->textcats->T('denied_default');
76
                } else {
77
                    $aErr = [];
78
                    $aResult = $hResult->fetch();
79
                    $iTimestamp = time();
80
                    if ($aResult['cust_pwresettimestamp'] < $iTimestamp - strtotime('1 Day', 0)) {
81
                        $this->P->oPayload->cl_html = $this->textcats->T('pwreset_error_expired');
82
                    } else {
83
                        $this->P->cb_customcontenttemplate = 'customer/resetpassword';
84
                        $this->P->cb_customdata['pwreset']['minpwlength'] = $this->config->getCustomer('minimum_length_password');
85 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...
86
                            $aErr = $this->handlePasswordReset($aErr, $aResult['cust_id']);
87
                            if (count($aErr) === 0) {
88
                                $this->P->cb_customdata['pwreset']['showsuccessmessage'] = true;
89
                            } else {
90
                                $this->P->cb_customdata['pwreset']['errors'] = $aErr;
91
                            }
92
                        }
93
                    }
94
                }
95
            }
96
        }
97
    }
98
99
    /**
100
     * @param $aErr
101
     * @param $iID
102
     * @return array
103
     */
104
    private function handlePasswordReset($aErr, $iID) {
105
        $postpwd = filter_input(INPUT_POST, 'pwd');
106
        if (!empty($postpwd)) {
107
            if (strlen($postpwd) < $this->config->getCustomer('minimum_length_password')) {
108
                $aErr[] = 'pwlength';
109
            }
110
            if ($postpwd !== filter_input(INPUT_POST, 'pwdc')) {
111
                $aErr[] = 'pwmatch';
112
            }
113
            if (count($aErr) == 0) {
114
                $sEnc = password_hash($postpwd, PASSWORD_DEFAULT);
115
                $aData = [
116
                    'cust_password' => $sEnc,
117
                    'cust_pwresetcode' => '',
118
                    'cust_id' => $iID,
119
                ];
120
                $sql = \HaaseIT\Toolbox\DBTools::buildPSUpdateQuery($aData, 'customer', 'cust_id');
121
                $hResult = $this->db->prepare($sql);
122
                foreach ($aData as $sKey => $sValue) {
123
                    $hResult->bindValue(':'.$sKey, $sValue);
124
                }
125
                $hResult->execute();
126
            }
127
        } else {
128
            $aErr[] = 'nopw';
129
        }
130
131
        return $aErr;
132
    }
133
}
134