Completed
Push — master ( dbe2de...ece09d )
by Marcus
01:54
created

Base::preparePage()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 1
nc 1
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;
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)) {
111
            $user = filter_input(INPUT_SERVER, 'PHP_AUTH_USER');
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 View Code Duplication
            if (!$validated) {
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...
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 View Code Duplication
        } else {
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...
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