Base::preparePage()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 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
108
        $adminusers = $this->config->getSecret('admin_users');
109
        if ($this->requireAdminAuthAdminHome && (empty($adminusers) || !count($adminusers))) {
110
            return true;
111
        } elseif (count($adminusers)) {
112
            $user = filter_var($_SERVER['PHP_AUTH_USER'], FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_LOW);
113
            $pass = filter_var($_SERVER['PHP_AUTH_PW'], FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_LOW);
114
115
            if (!empty($_SERVER['REDIRECT_HTTP_AUTHORIZATION'])) { // fix for php cgi mode
116
                //die($_SERVER['REDIRECT_HTTP_AUTHORIZATION']);
117
                list($user, $pass) = explode(':' , base64_decode(substr(filter_var($_SERVER['REDIRECT_HTTP_AUTHORIZATION'], FILTER_SANITIZE_STRING), 6)));
118
            }
119
120
            if (!empty($user) && !empty($pass)) {
121
                $validated = !empty(
122
                    $adminusers[$user])
123
                    && password_verify($pass, $adminusers[$user]
124
                    );
125
            } else {
126
                $validated = false;
127
            }
128
129 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...
130
                header('WWW-Authenticate: Basic realm="'.$this->config->getSecret('admin_authrealm').'"');
131
                header('HTTP/1.0 401 Unauthorized');
132
                $this->helper->terminateScript('Not authorized');
133
            }
134
135 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...
136
//            die('foo');
137
            header('WWW-Authenticate: Basic realm="'.$this->config->getSecret('admin_authrealm').'"');
138
            header('HTTP/1.0 401 Unauthorized');
139
            $this->helper->terminateScript('Not authorized');
140
        }
141
    }
142
}
143