Passed
Push — master ( 1bc300...987c24 )
by Michael
01:47
created

InstallWizard::checkAccess()   C

Complexity

Conditions 12
Paths 21

Size

Total Lines 39
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 12
eloc 23
nc 21
nop 0
dl 0
loc 39
rs 5.1612
c 0
b 0
f 0

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 namespace XoopsModules\Moduleinstaller;
2
3
/*
4
 You may not change or alter any portion of this comment or credits
5
 of supporting developers from this source code or any supporting source code
6
 which is considered copyrighted (c) material of the original comment or credit authors.
7
8
 This program is distributed in the hope that it will be useful,
9
 but WITHOUT ANY WARRANTY; without even the implied warranty of
10
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11
*/
12
13
/**
14
 * See the enclosed file license.txt for licensing information.
15
 * If you did not receive this file, get it at http://www.gnu.org/licenses/gpl-2.0.html
16
 *
17
 * @copyright   XOOPS Project (https://xoops.org)
18
 * @license     http://www.gnu.org/licenses/gpl-2.0.html GNU General Public License (GPL)
19
 * @package     installer
20
 * @since       2.3.0
21
 * @author      Haruki Setoyama  <[email protected]>
22
 * @author      Kazumi Ono <[email protected]>
23
 * @author      Skalpa Keo <[email protected]>
24
 * @author      Taiwen Jiang <[email protected]>
25
 * @author      DuGris (aka L. JEN) <[email protected]>
26
 */
27
class  InstallWizard
0 ignored issues
show
Coding Style introduced by
Expected 1 space between class keyword and class name; 2 found
Loading history...
28
{
29
    public $language    = 'english';
30
    public $pages       = [];
31
    public $currentPage = 'langselect';
32
    public $pageIndex   = 0;
33
    public $configs     = [];
34
35
    /**
36
     * @return bool
37
     */
38
    public function xoInit()
39
    {
40
        if (@empty($_SERVER['REQUEST_URI'])) {
41
            $_SERVER['REQUEST_URI'] = $_SERVER['PHP_SELF'];
42
        }
43
44
        // Load the main language file
45
        $this->initLanguage(!empty($_COOKIE['xo_install_lang']) ? $_COOKIE['xo_install_lang'] : 'english');
46
        // Setup pages
47
        require_once __DIR__ . '/../include/page.php';
48
        $this->pages = $pages;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $pages seems to be never defined.
Loading history...
49
50
        // Load default configs
51
        require_once __DIR__ . '/../include/config.php';
52
        $this->configs = $configs;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $configs seems to be never defined.
Loading history...
53
        /*
54
        // Database type
55
        $this->db_types  = $db_types;
56
57
        // setup config site info
58
        $this->conf_names  = $conf_names;
59
60
        // languages config files
61
        $this->language_files = $language_files;
62
63
        // extension_loaded
64
        $this->extensions = $extensions;
65
66
        // Modules to be installed by default
67
        $this->modules = $modules;
68
69
        // xoops_lib, xoops_data directories
70
        $this->xoopsPathDefault = $xoopsPathDefault;
71
72
        // writable xoops_lib, xoops_data directories
73
        $this->dataPath = $dataPath;
74
75
        // Protector default trust_path
76
        $this->trust_path = isset($trust_path) ? $trust_path : false;
77
78
        // Writable files and directories
79
        $this->writable = $writable;
80
        */
81
82
        if (!$this->checkAccess()) {
83
            return false;
84
        }
85
86
        $pagename = preg_replace('~(page_)(.*)~', '$2', basename($_SERVER['PHP_SELF'], '.php'));
87
        $this->setPage($pagename);
88
89
        // Prevent client caching
90
        header('Cache-Control: no-store, no-cache, must-revalidate', false);
91
        header('Pragma: no-cache');
92
93
        return true;
94
    }
95
96
    /**
97
     * @return bool
98
     */
99
    public function checkAccess()
100
    {
101
        if (INSTALL_USER != '' && INSTALL_PASSWORD != '') {
0 ignored issues
show
introduced by
The condition XoopsModules\Moduleinstaller\INSTALL_USER != '' is always false.
Loading history...
102
            if (!isset($_SERVER['PHP_AUTH_USER'])) {
103
                header('WWW-Authenticate: Basic realm="XOOPS Installer"');
104
                header('HTTP/1.0 401 Unauthorized');
105
                echo 'You can not access this XOOPS installer.';
106
107
                return false;
108
            }
109
            if (INSTALL_USER != '' && INSTALL_USER != $_SERVER['PHP_AUTH_USER']) {
110
                header('HTTP/1.0 401 Unauthorized');
111
                echo 'You can not access this XOOPS installer.';
112
113
                return false;
114
            }
115
            if (INSTALL_PASSWORD != $_SERVER['PHP_AUTH_PW']) {
116
                header('HTTP/1.0 401 Unauthorized');
117
                echo 'You can not access this XOOPS installer.';
118
119
                return false;
120
            }
121
        }
122
123
        if (empty($GLOBALS['xoopsOption']['checkadmin'])) {
124
            return true;
125
        }
126
127
        if (empty($GLOBALS['xoopsUser']) && !empty($_COOKIE['xo_install_user'])) {
128
            install_acceptUser($_COOKIE['xo_install_user']);
0 ignored issues
show
Bug introduced by
The function install_acceptUser was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

128
            /** @scrutinizer ignore-call */ 
129
            install_acceptUser($_COOKIE['xo_install_user']);
Loading history...
129
        }
130
        if (empty($GLOBALS['xoopsUser'])) {
131
            redirect_header('../user.php');
0 ignored issues
show
Bug introduced by
The function redirect_header was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

131
            /** @scrutinizer ignore-call */ 
132
            redirect_header('../user.php');
Loading history...
132
        }
133
        if (!$GLOBALS['xoopsUser']->isAdmin()) {
134
            return false;
135
        }
136
137
        return true;
138
    }
139
140
    /**
141
     * @param $file
142
     */
143
    public function loadLangFile($file)
144
    {
145
        if (file_exists("./language/{$this->language}/{$file}.php")) {
146
            require_once __DIR__ . "./language/{$this->language}/{$file}.php";
147
        } else {
148
            require_once "./../language/english/$file.php";
149
        }
150
    }
151
152
    /**
153
     * @param $language
154
     */
155
    public function initLanguage($language)
156
    {
157
        $language = preg_replace("/[^a-z0-9_\-]/i", '', $language);
158
        if (!file_exists("./language/{$language}/install.php")) {
159
            $language = 'english';
160
        }
161
        $this->language = $language;
162
        $this->loadLangFile('install');
163
    }
164
165
    /**
166
     * @param $page
167
     *
168
     * @return bool|mixed
169
     */
170
    public function setPage($page)
171
    {
172
        $pages = array_keys($this->pages);
173
        if ((int)$page && $page >= 0 && $page < count($pages)) {
174
            $this->pageIndex   = $page;
175
            $this->currentPage = $pages[$page];
176
        } elseif (isset($this->pages[$page])) {
177
            $this->currentPage = $page;
178
            $this->pageIndex   = array_search($this->currentPage, $pages);
179
        } else {
180
            return false;
181
        }
182
183
        if ($this->pageIndex > 0 && !isset($_COOKIE['xo_install_lang'])) {
184
            header('Location: index.php');
185
        }
186
187
        return $this->pageIndex;
188
    }
189
190
    /**
191
     * @return string
192
     */
193
    public function baseLocation()
194
    {
195
        $proto = ('on' === @$_SERVER['HTTPS']) ? 'https' : 'http';
196
        $host  = $_SERVER['HTTP_HOST'];
197
        $base  = substr($_SERVER['PHP_SELF'], 0, strrpos($_SERVER['PHP_SELF'], '/'));
198
199
        return $proto . '://' . $host . $base;
200
    }
201
202
    /**
203
     * @param $page
204
     *
205
     * @return string
206
     */
207
    public function pageURI($page)
208
    {
209
        $pages     = array_keys($this->pages);
210
        $pageIndex = $this->pageIndex;
211
        if (!(int)$page{0}) {
212
            if ('+' == $page{0}) {
213
                $pageIndex += substr($page, 1);
214
            } elseif ('-' == $page{0}) {
215
                $pageIndex -= substr($page, 1);
216
            } else {
217
                $pageIndex = (int)array_search($page, $pages);
218
            }
219
        }
220
        if (!isset($pages[$pageIndex])) {
221
            if (defined('XOOPS_URL')) {
222
                return XOOPS_URL;
0 ignored issues
show
Bug introduced by
The constant XoopsModules\Moduleinstaller\XOOPS_URL was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
223
            } else {
224
                return $this->baseLocation();
225
            }
226
        }
227
        $page = $pages[$pageIndex];
228
229
        return $this->baseLocation() . "/page_{$page}.php";
230
    }
231
232
    /**
233
     * @param        $page
234
     * @param int    $status
235
     * @param string $message
236
     */
237
    public function redirectToPage($page, $status = 303, $message = 'See other')
238
    {
239
        $location = $this->pageURI($page);
240
        $proto    = !@empty($_SERVER['SERVER_PROTOCOL']) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.1';
241
        header("{$proto} {$status} {$message}");
242
        //header( "Status: $status $message" );
0 ignored issues
show
Unused Code Comprehensibility introduced by
58% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
243
        header("Location: {$location}");
244
    }
245
246
    /**
247
     * @return string
248
     */
249
    public function createForm()
250
    {
251
        $hidden = '';
252
        $ret    = '';
253
254
        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...
255
            $ret .= '<fieldset><legend>' . $form->getTitle() . "</legend>\n";
256
257
            foreach ($form->getElements() as $ele) {
258
                if (is_object($ele)) {
259
                    if (!$ele->isHidden()) {
260
                        if ('' != ($caption = $ele->getCaption())) {
261
                            $name = $ele->getName();
0 ignored issues
show
Unused Code introduced by
The assignment to $name is dead and can be removed.
Loading history...
262
                            $ret  .= "<label class='xolabel' for='" . $ele->getName() . "'>" . $caption . '</label>';
263
                            if ('' != ($desc = $ele->getDescription())) {
264
                                $ret .= "<div class='xoform-help'>";
265
                                $ret .= $desc;
266
                                $ret .= '</div>';
267
                            }
268
                        }
269
                        $ret .= $ele->render() . "\n";
270
                    } else {
271
                        $hidden .= $ele->render() . "\n";
272
                    }
273
                }
274
            }
275
            $ret .= "</fieldset>\n" . $hidden . "\n" . $form->renderValidationJS(true);
276
        }
277
278
        return $ret;
279
    }
280
}
281