Passed
Push — cirework ( 7349fb...97b46e )
by Richard
04:26
created

protector_precheck()   F

Complexity

Conditions 26
Paths 12103

Size

Total Lines 99
Code Lines 48

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 26
eloc 48
nc 12103
nop 0
dl 0
loc 99
rs 2
c 0
b 0
f 0

How to fix   Long Method    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
2
/*
3
 You may not change or alter any portion of this comment or credits
4
 of supporting developers from this source code or any supporting source code
5
 which is considered copyrighted (c) material of the original comment or credit authors.
6
7
 This program is distributed in the hope that it will be useful,
8
 but WITHOUT ANY WARRANTY; without even the implied warranty of
9
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10
*/
11
12
/**
13
 * Protector
14
 *
15
 * @copyright       XOOPS Project (http://xoops.org)
16
 * @license         GNU GPL 2 or later (http://www.gnu.org/licenses/gpl-2.0.html)
17
 * @package         protector
18
 * @author          trabis <[email protected]>
19
 * @version         $Id$
20
 */
21
22
/**
23
 * @return null|boolean
24
 */
25
function protector_precheck()
26
{
27
    // check the access is from install/index.php
28
    if (defined('_INSTALL_CHARSET') && !is_writable(\XoopsBaseConfig::get('root-path') . '/mainfile.php')) {
29
        die('To use installer, remove protector\'s lines from mainfile.php first.');
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
30
    }
31
32
    // Protector class
33
    require_once dirname(__DIR__) . '/class/protector.php';
34
35
    // Protector object
36
    $protector = Protector::getInstance();
37
    $conf = $protector->getConf();
38
39
    // bandwidth limitation
40
    if (@$conf['bwlimit_count'] >= 10) {
41
        $bwexpire = $protector->get_bwlimit();
42
        if ($bwexpire > time()) {
43
            header('HTTP/1.0 503 Service unavailable');
44
            $protector->call_filter('precommon_bwlimit', 'This site is very crowed now. try later.');
45
        }
46
    }
47
48
    // bad_ips
49
    $bad_ips = $protector->get_bad_ips(true);
50
    $bad_ip_match = $protector->ip_match($bad_ips);
51
    if ($bad_ip_match) {
52
        $protector->call_filter('precommon_badip', 'You are registered as BAD_IP by Protector.');
53
    }
54
55
    // global enabled or disabled
56
    if (!empty($conf['global_disabled'])) {
57
        return true;
58
    }
59
60
    // reliable ips
61
    $reliable_ips = @unserialize(@$conf['reliable_ips']);
62
    if (!is_array($reliable_ips)) {
63
        // for the environment of (buggy core version && magic_quotes_gpc)
64
        $reliable_ips = @unserialize(stripslashes(@$conf['reliable_ips']));
65
        if (!is_array($reliable_ips)) {
66
            $reliable_ips = array();
67
        }
68
    }
69
    $is_reliable = false;
70
    foreach ($reliable_ips as $reliable_ip) {
71
        if (!empty($reliable_ip) && preg_match('/' . $reliable_ip . '/', $_SERVER['REMOTE_ADDR'])) {
72
            $is_reliable = true;
73
        }
74
    }
75
76
    // "Big Umbrella" subset version
77
    if (!empty($conf['enable_bigumbrella'])) {
78
        @define('PROTECTOR_ENABLED_ANTI_XSS', 1);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition for define(). This can introduce security issues, and is generally not recommended. ( Ignorable by Annotation )

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

78
        /** @scrutinizer ignore-unhandled */ @define('PROTECTOR_ENABLED_ANTI_XSS', 1);

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
79
        $protector->bigumbrella_init();
80
    }
81
82
    // force intval variables whose name is *id
83
    if (!empty($conf['id_forceintval'])) {
84
        $protector->intval_allrequestsendid();
85
    }
86
87
    // eliminate '..' from requests looks like file specifications
88
    if (!$is_reliable && !empty($conf['file_dotdot'])) {
89
        $protector->eliminate_dotdot();
90
    }
91
92
    // Check uploaded files
93
    if (!$is_reliable && !empty($_FILES) && !empty($conf['die_badext']) && !defined('PROTECTOR_SKIP_FILESCHECKER') && !$protector->check_uploaded_files()) {
94
        $protector->output_log($protector->last_error_type);
95
        $protector->purge();
96
    }
97
98
    // Variables contamination
99
    if (!$protector->check_contami_systemglobals()) {
100
        if (@$conf['contami_action'] & 4) {
101
            if (@$conf['contami_action'] & 8) {
102
                $protector->_should_be_banned = true;
103
            } else {
104
                $protector->_should_be_banned_time0 = true;
105
            }
106
            $_GET = $_POST = array();
107
        }
108
109
        $protector->output_log($protector->last_error_type);
110
        if (@$conf['contami_action'] & 2) {
111
            $protector->purge();
112
        }
113
    }
114
115
    // prepare for DoS
116
    //if( ! $protector->check_dos_attack_prepare() ) {
117
    //  $protector->output_log( $protector->last_error_type , 0 , true ) ;
118
    //}
119
120
    if (!empty($conf['disable_features'])) {
121
        $protector->disable_features();
122
    }
123
    return true;
124
}
125