Login   A
last analyzed

Complexity

Total Complexity 26

Size/Duplication

Total Lines 103
Duplicated Lines 7.77 %

Coupling/Cohesion

Components 1
Dependencies 9

Importance

Changes 0
Metric Value
wmc 26
lcom 1
cbo 9
dl 8
loc 103
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
B preparePage() 8 36 11
F getLogin() 0 57 15

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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\Toolbox\Tools;
24
25
/**
26
 * Class Login
27
 * @package HaaseIT\HCSF\Controller\Customer
28
 */
29
class Login extends Base
30
{
31
    /**
32
     *
33
     */
34
    public function preparePage()
35
    {
36
        $this->P = new \HaaseIT\HCSF\CorePage($this->serviceManager);
37
        $this->P->cb_pagetype = 'content';
38
39
        /** @var \HaaseIT\Toolbox\Textcat $textcats */
40
        $textcats = $this->serviceManager->get('textcats');
41
42
        if (filter_input(INPUT_POST, 'sAction') !== 'login') {
43
            $this->P->cb_customcontenttemplate = 'customer/login';
44
        } else {
45
            $mLogin = $this->getLogin();
46
            if (isset($mLogin['status']) && $mLogin['status'] === 'success') {
47
                $this->P->oPayload->cl_html = $textcats->T('login_success').'<br>';
48
                $this->helper->redirectToPage('/_misc/userhome.html?login=true');
49 View Code Duplication
            } elseif (isset($mLogin['status']) && $mLogin['status'] === 'tosnotaccepted') {
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...
50
                $this->P->oPayload->cl_html = $textcats->T('login_fail_tosnotaccepted').'<br>';
51
                $this->P->cb_customcontenttemplate = 'customer/login';
52
            } elseif (isset($mLogin['status']) && $mLogin['status'] === 'emailnotverified') {
53
                $this->P->oPayload->cl_html = $textcats->T('login_fail_emailnotverified').'<br><br>';
54
                $this->P->oPayload->cl_html .= '<a href="/_misc/resendverificationmail.html?email='
55
                   .$mLogin['data']['cust_email'].'">'.$textcats->T('login_fail_emailnotverifiedresend').'</a>';
56
                $this->P->cb_customcontenttemplate = 'customer/login';
57 View Code Duplication
            } elseif (isset($mLogin['status']) && $mLogin['status'] === 'accountinactive') {
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...
58
                $this->P->oPayload->cl_html = $textcats->T('login_fail_accountinactive').'<br>';
59
                $this->P->cb_customcontenttemplate = 'customer/login';
60
            } else {
61
                $this->P->oPayload->cl_html = $textcats->T('login_fail');
62
                $this->P->cb_customcontenttemplate = 'customer/login';
63
            }
64
        }
65
66
        if ($this->config->getCore('enable_module_shop')) {
67
            $this->helperShop->refreshCartItems($this->serviceManager);
68
        }
69
    }
70
71
    /**
72
     * @return array|bool
73
     */
74
    private function getLogin()
75
    {
76
        $bTryEmail = false;
77
        if ('cust_no' !== 'cust_email') {
78
            $bTryEmail = true;
79
        }
80
81
        $sEmail = filter_var(trim(Tools::getFormfield('user')), FILTER_SANITIZE_EMAIL);
82
        $sUser = filter_var(trim(Tools::getFormfield('user')), FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_LOW);
83
84
        $sql = 'SELECT cust_no, cust_email, cust_password, cust_active, cust_emailverified, cust_tosaccepted'
85
           .' FROM customer WHERE ';
86
        if ($bTryEmail) {
87
            $sql .= '(';
88
        }
89
        $sql .= 'cust_no = :user';
90
        if ($bTryEmail) {
91
            $sql .= ' OR cust_email = :email) ';
92
        }
93
        $sql .= ' AND ';
94
        if ($bTryEmail) {
95
            $sql .= '(';
96
        }
97
        $sql .= 'cust_no != \'\'';
98
99
        if ($bTryEmail) {
100
            $sql .= ' OR cust_email != \'\')';
101
        }
102
103
        /** @var \PDOStatement $hResult */
104
        $hResult = $this->serviceManager->get('db')->prepare($sql);
105
        $hResult->bindValue(':user', $sUser, \PDO::PARAM_STR);
106
        if ($bTryEmail) {
107
            $hResult->bindValue(':email', $sEmail, \PDO::PARAM_STR);
108
        }
109
        $hResult->execute();
110
111
        $iRows = $hResult->rowCount();
112
        if($iRows == 1) {
113
            $aRow = $hResult->fetch();
114
115
            if (password_verify(filter_input(INPUT_POST, 'password'), $aRow['cust_password'])) {
116
                if ($aRow['cust_active'] === 'y' && $aRow['cust_emailverified'] === 'y' && $aRow['cust_tosaccepted'] === 'y') {
117
                    $_SESSION['user'] = $aRow;
118
                    return ['status' => 'success'];
119
                } elseif ($aRow['cust_tosaccepted'] === 'n') {
120
                    return ['status' => 'tosnotaccepted'];
121
                } elseif ($aRow['cust_emailverified'] === 'n') {
122
                    return ['status' => 'emailnotverified', 'data' => $aRow,];
123
                } elseif ($aRow['cust_active'] === 'n') {
124
                    return ['status' => 'accountinactive',];
125
                }
126
            }
127
        }
128
129
        return false;
130
    }
131
}
132