Passed
Pull Request — master (#1500)
by Michael
09:16
created

InstallWizard::checkAccess()   C

Complexity

Conditions 12
Paths 21

Size

Total Lines 39
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 12
eloc 23
c 1
b 0
f 0
nc 21
nop 0
dl 0
loc 39
rs 6.9666

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php declare(strict_types=1);
2
3
namespace XoopsModules\Moduleinstaller;
4
5
/*
6
 You may not change or alter any portion of this comment or credits
7
 of supporting developers from this source code or any supporting source code
8
 which is considered copyrighted (c) material of the original comment or credit authors.
9
10
 This program is distributed in the hope that it will be useful,
11
 but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13
*/
14
15
/**
16
 * See the enclosed file license.txt for licensing information.
17
 * If you did not receive this file, get it at https://www.gnu.org/licenses/gpl-2.0.html
18
 *
19
 * @copyright   XOOPS Project (https://xoops.org)
20
 * @license     https://www.gnu.org/licenses/gpl-2.0.html GNU General Public License (GPL)
21
 * @since       2.3.0
22
 * @author      Haruki Setoyama  <[email protected]>
23
 * @author      Kazumi Ono <[email protected]>
24
 * @author      Skalpa Keo <[email protected]>
25
 * @author      Taiwen Jiang <[email protected]>
26
 * @author      DuGris (aka L. JEN) <[email protected]>
27
 */
28
class InstallWizard
29
{
30
    public $language    = 'english';
31
    public $pages       = [];
32
    public $currentPage = 'langselect';
33
    public $pageIndex   = 0;
34
    public $configs     = [];
35
36
    /**
37
     * @return bool
38
     */
39
    public function xoInit()
40
    {
41
        if (@empty($_SERVER['REQUEST_URI'])) {
42
            $_SERVER['REQUEST_URI'] = $_SERVER['SCRIPT_NAME'];
43
        }
44
45
        // Load the main language file
46
        $this->initLanguage(!empty($_COOKIE['xo_install_lang']) ? $_COOKIE['xo_install_lang'] : 'english');
47
        // Setup pages
48
        require_once \dirname(__DIR__) . '/include/page.php';
49
        $this->pages = $pages;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $pages seems to be never defined.
Loading history...
50
51
        // Load default configs
52
53
        $this->configs = [];
54
        /*
55
        // Database type
56
        $this->db_types  = $db_types;
57
58
        // setup config site info
59
        $this->conf_names  = $conf_names;
60
61
        // languages config files
62
        $this->language_files = $language_files;
63
64
        // extension_loaded
65
        $this->extensions = $extensions;
66
67
        // Modules to be installed by default
68
        $this->modules = $modules;
69
70
        // xoops_lib, xoops_data directories
71
        $this->xoopsPathDefault = $xoopsPathDefault;
72
73
        // writable xoops_lib, xoops_data directories
74
        $this->dataPath = $dataPath;
75
76
        // Protector default trust_path
77
        $this->trust_path = isset($trust_path) ? $trust_path : false;
78
79
        // Writable files and directories
80
        $this->writable = $writable;
81
        */
82
83
        if (!$this->checkAccess()) {
84
            return false;
85
        }
86
87
        $pagename = \preg_replace('~(page_)(.*)~', '$2', \basename((string) $_SERVER['SCRIPT_NAME'], '.php'));
88
        $this->setPage($pagename);
89
90
        // Prevent client caching
91
        \header('Cache-Control: no-store, no-cache, must-revalidate', false);
92
        \header('Pragma: no-cache');
93
94
        return true;
95
    }
96
97
    /**
98
     * @return bool
99
     */
100
    public function checkAccess()
101
    {
102
        if (INSTALL_USER != '' && INSTALL_PASSWORD != '') {
0 ignored issues
show
introduced by
The condition XoopsModules\Moduleinstaller\INSTALL_USER != '' is always false.
Loading history...
103
            if (!isset($_SERVER['PHP_AUTH_USER'])) {
104
                \header('WWW-Authenticate: Basic realm="XOOPS Installer"');
105
                \header('HTTP/1.0 401 Unauthorized');
106
                echo 'You can not access this XOOPS installer.';
107
108
                return false;
109
            }
110
            if (INSTALL_USER != '' && INSTALL_USER != $_SERVER['PHP_AUTH_USER']) {
111
                \header('HTTP/1.0 401 Unauthorized');
112
                echo 'You can not access this XOOPS installer.';
113
114
                return false;
115
            }
116
            if (INSTALL_PASSWORD != $_SERVER['PHP_AUTH_PW']) {
117
                \header('HTTP/1.0 401 Unauthorized');
118
                echo 'You can not access this XOOPS installer.';
119
120
                return false;
121
            }
122
        }
123
124
        if (empty($GLOBALS['xoopsOption']['checkadmin'])) {
125
            return true;
126
        }
127
128
        if (empty($GLOBALS['xoopsUser']) && !empty($_COOKIE['xo_install_user'])) {
129
            \install_acceptUser($_COOKIE['xo_install_user']);
130
        }
131
        if (empty($GLOBALS['xoopsUser'])) {
132
            \redirect_header('../user.php');
133
        }
134
        if (!$GLOBALS['xoopsUser']->isAdmin()) {
135
            return false;
136
        }
137
138
        return true;
139
    }
140
141
    /**
142
     * @param $file
143
     */
144
    public function loadLangFile($file): void
145
    {
146
        \xoops_loadLanguage($file, 'moduleinstaller');
147
    }
148
149
    /**
150
     * @param $language
151
     */
152
    public function initLanguage($language): void
153
    {
154
        $language = \preg_replace('/[^a-z0-9_\-]/i', '', (string) $language);
155
        if (!\file_exists("./language/{$language}/install.php")) {
156
            $language = 'english';
157
        }
158
        $this->language = $language;
159
        $this->loadLangFile('install');
160
    }
161
162
    /**
163
     * @param $page
164
     *
165
     * @return false|int|string
166
     */
167
    public function setPage($page)
168
    {
169
        $pages = \array_keys($this->pages);
170
        if ((int)$page && $page >= 0 && $page < \count($pages)) {
171
            $this->pageIndex   = $page;
172
            $this->currentPage = $pages[$page];
173
        } elseif (isset($this->pages[$page])) {
174
            $this->currentPage = $page;
175
            $this->pageIndex   = \array_search($this->currentPage, $pages, true);
176
        } else {
177
            return false;
178
        }
179
180
        if ($this->pageIndex > 0 && !isset($_COOKIE['xo_install_lang'])) {
181
            \header('Location: index.php');
182
        }
183
184
        return $this->pageIndex;
185
    }
186
187
    /**
188
     * @return string
189
     */
190
    public function baseLocation()
191
    {
192
        $proto = ('on' === @$_SERVER['HTTPS']) ? 'https' : 'http';
193
        $host  = $_SERVER['HTTP_HOST'];
194
        $base  = mb_substr((string) $_SERVER['SCRIPT_NAME'], 0, mb_strrpos((string) $_SERVER['SCRIPT_NAME'], '/'));
195
196
        return $proto . '://' . $host . $base;
197
    }
198
199
    /**
200
     * @param $page
201
     *
202
     * @return string
203
     */
204
    public function pageURI($page)
205
    {
206
        $pages     = \array_keys($this->pages);
207
        $pageIndex = $this->pageIndex;
208
        if (!(int)$page[0]) {
209
            if ('+' == $page[0]) {
210
                $pageIndex += mb_substr((string) $page, 1);
211
            } elseif ('-' == $page[0]) {
212
                $pageIndex -= mb_substr((string) $page, 1);
213
            } else {
214
                $pageIndex = (int)\array_search($page, $pages, true);
215
            }
216
        }
217
        if (!isset($pages[$pageIndex])) {
218
            if (\defined('XOOPS_URL')) {
219
                return XOOPS_URL;
220
            }
221
222
            return $this->baseLocation();
223
        }
224
        $page = $pages[$pageIndex];
225
226
        return $this->baseLocation() . "/page_{$page}.php";
227
    }
228
229
    /**
230
     * @param        $page
231
     * @param int    $status
232
     * @param string $message
233
     */
234
    public function redirectToPage($page, $status = 303, $message = 'See other'): void
235
    {
236
        $location = $this->pageURI($page);
237
        $proto    = !@empty($_SERVER['SERVER_PROTOCOL']) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.1';
238
        \header("{$proto} {$status} {$message}");
239
        //header( "Status: $status $message" );
240
        \header("Location: {$location}");
241
    }
242
243
    /**
244
     * @return string
245
     */
246
    public function createForm()
247
    {
248
        $hidden = '';
249
        $ret    = '';
250
251
        foreach ($this->form as $form) {
0 ignored issues
show
Bug Best Practice introduced by
The property form does not exist on XoopsModules\Moduleinstaller\InstallWizard. Did you maybe forget to declare it?
Loading history...
252
            $ret .= '<fieldset><legend>' . $form->getTitle() . "</legend>\n";
253
254
            foreach ($form->getElements() as $ele) {
255
                if (\is_object($ele)) {
256
                    if (!$ele->isHidden()) {
257
                        if ('' != ($caption = $ele->getCaption())) {
258
                            $name = $ele->getName();
0 ignored issues
show
Unused Code introduced by
The assignment to $name is dead and can be removed.
Loading history...
259
                            $ret  .= "<label class='xolabel' for='" . $ele->getName() . "'>" . $caption . '</label>';
260
                            if ('' != ($desc = $ele->getDescription())) {
261
                                $ret .= "<div class='xoform-help'>";
262
                                $ret .= $desc;
263
                                $ret .= '</div>';
264
                            }
265
                        }
266
                        $ret .= $ele->render() . "\n";
267
                    } else {
268
                        $hidden .= $ele->render() . "\n";
269
                    }
270
                }
271
            }
272
            $ret .= "</fieldset>\n" . $hidden . "\n" . $form->renderValidationJS(true);
273
        }
274
275
        return $ret;
276
    }
277
}
278