Completed
Push — master ( 60b053...61ce86 )
by Marcus
02:08
created

Base   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 117
Duplicated Lines 8.55 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 20
c 1
b 0
f 1
lcom 1
cbo 2
dl 10
loc 117
rs 10

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;
22
23
24
use Zend\ServiceManager\ServiceManager;
25
26
class Base
27
{
28
    /**
29
     * @var \HaaseIT\HCSF\Page
30
     */
31
    protected $P;
32
33
    /**
34
     * @var ServiceManager
35
     */
36
    protected $serviceManager;
37
38
    /**
39
     * @var bool
40
     */
41
    protected $requireAdminAuth = false;
42
43
    /**
44
     * @var bool
45
     */
46
    protected $requireAdminAuthAdminHome = false;
47
48
    /**
49
     * @var bool
50
     */
51
    protected $requireModuleCustomer = false;
52
53
    /**
54
     * @var bool
55
     */
56
    protected $requireModuleShop = false;
57
58
    /**
59
     * @var \HaaseIT\HCSF\HelperConfig
60
     */
61
    protected $config;
62
63
    /**
64
     * @var \HaaseIT\HCSF\Helper
65
     */
66
    protected $helper;
67
68
    /**
69
     * Base constructor.
70
     * @param ServiceManager $serviceManager
71
     */
72
    public function __construct(ServiceManager $serviceManager)
73
    {
74
        $this->serviceManager = $serviceManager;
75
        $this->config = $serviceManager->get('config');
76
        $this->helper = $serviceManager->get('helper');
77
    }
78
79
    /**
80
     * @return \HaaseIT\HCSF\Page
81
     * @throws \Exception
82
     */
83
    public function getPage()
84
    {
85
        if ($this->requireAdminAuth) {
86
            $this->requireAdminAuth();
87
        }
88
        if ($this->requireModuleCustomer && !$this->config->getCore('enable_module_customer')) {
89
            throw new \Exception(404);
90
        }
91
        if ($this->requireModuleShop && !$this->config->getCore('enable_module_shop')) {
92
            throw new \Exception(404);
93
        }
94
        $this->preparePage();
95
        return $this->P;
96
    }
97
98
    public function preparePage()
99
    {
100
101
    }
102
103
    /**
104
     * @return bool
105
     */
106
    private function requireAdminAuth() {
107
        $adminusers = $this->config->getSecret('admin_users');
108
        if ($this->requireAdminAuthAdminHome && (empty($adminusers) || !count($adminusers))) {
109
            return true;
110
        } elseif (count($adminusers) {
0 ignored issues
show
Bug introduced by
Avoid IF statements that are always true or false
Loading history...
111
            $user = filter_input(INPUT_SERVER, 'PHP_AUTH_USER');
0 ignored issues
show
Bug introduced by
This code did not parse for me. Apparently, there is an error somewhere around this line:

Syntax error, unexpected ';'
Loading history...
112
            $pass = filter_input(INPUT_SERVER, 'PHP_AUTH_PW');
113
            if (filter_input(INPUT_SERVER, 'REDIRECT_HTTP_AUTHORIZATION') !== null) { // fix for php cgi mode
114
                list($user, $pass) = explode(':' , base64_decode(substr(filter_input(INPUT_SERVER, 'REDIRECT_HTTP_AUTHORIZATION'), 6)));
115
            }
116
117
            if (!empty($user) && !empty($pass)) {
118
                $validated = !empty(
119
                    $adminusers[$user])
120
                    && password_verify($pass, $adminusers[$user]
121
                );
122
            } else {
123
                $validated = false;
124
            }
125
126
            if (!$validated) {
127
                header('WWW-Authenticate: Basic realm="'.$this->config->getSecret('admin_authrealm').'"');
128
                header('HTTP/1.0 401 Unauthorized');
129
                $this->helper->terminateScript('Not authorized');
130
            }
131
132
        } else {
133
            header('WWW-Authenticate: Basic realm="'.$this->config->getSecret('admin_authrealm').'"');
134
            header('HTTP/1.0 401 Unauthorized');
135
            $this->helper->terminateScript('Not authorized');
136
        }
137
    }
138
}
139