Completed
Push — master ( b7ea8c...bb29b5 )
by Marcus
34:36 queued 30:49
created

Resetpassword::preparePage()   D

Complexity

Conditions 9
Paths 7

Size

Total Lines 43
Code Lines 33

Duplication

Lines 8
Ratio 18.6 %

Importance

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