Completed
Pull Request — develop (#527)
by Agel_Nash
07:45
created

functions.php ➔ get_installmode()   B

Complexity

Conditions 9
Paths 13

Size

Total Lines 56
Code Lines 41

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 9
eloc 41
nc 13
nop 0
dl 0
loc 56
rs 7.1584
c 0
b 0
f 0

How to fix   Long Method   

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
if( ! function_exists('install_sessionCheck')) {
3
    function install_sessionCheck()
4
    {
5
        global $_lang;
6
7
        // session loop-back tester
8
        if (!isset($_GET['action']) || $_GET['action'] !== 'mode') {
9
            if (!isset($_SESSION['test']) || $_SESSION['test'] != 1) {
10
                echo '
11
<html>
12
<head>
13
	<title>Install Problem</title>
14
	<style type="text/css">
15
		*{margin:0;padding:0}
16
		body{margin:150px;background:#eee;}
17
		.install{padding:10px;border:3px solid #ffc565;background:#ffddb4;margin:0 auto;text-align:center;}
18
		p{ margin:20px 0; }
19
		a{margin-top:30px;padding:5px;}
20
	</style>
21
</head>
22
<body>
23
	<div class="install">
24
		<p>' . $_lang["session_problem"] . '</p>
25
		<p><a href="./">' . $_lang["session_problem_try_again"] . '</a></p>
26
	</div>
27
</body>
28
</html>';
29
                exit;
30
            }
31
        }
32
    }
33
}
34
35
if( ! function_exists('parse')) {
36
    function parse($src, $ph, $left = '[+', $right = '+]')
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
37
    {
38
        foreach ($ph as $k => $v) {
39
            $k = $left . $k . $right;
40
            $src = str_replace($k, $v, $src);
41
        }
42
43
        return $src;
44
    }
45
}
46
47
if( ! function_exists('ph')) {
48
    function ph()
49
    {
50
        global $_lang, $moduleName, $moduleVersion, $modx_textdir, $modx_release_date;
51
52
        if (isset($_SESSION['installmode'])) {
53
            $installmode = $_SESSION['installmode'];
54
        } else {
55
            $installmode = get_installmode();
56
        }
57
58
        $ph['pagetitle'] = $_lang['modx_install'];
0 ignored issues
show
Coding Style Comprehensibility introduced by
$ph was never initialized. Although not strictly required by PHP, it is generally a good practice to add $ph = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
59
        $ph['textdir'] = $modx_textdir ? ' id="rtl"' : '';
60
        $ph['help_link'] = $installmode == 0 ? $_lang['help_link_new'] : $_lang['help_link_upd'];
61
        $ph['version'] = $moduleVersion;
62
        $ph['release_date'] = ($modx_textdir ? '&rlm;' : '') . $modx_release_date;
63
        $ph['footer1'] = $_lang['modx_footer1'];
64
        $ph['footer2'] = $_lang['modx_footer2'];
65
        $ph['current_year'] = date('Y');
66
67
        return $ph;
68
    }
69
}
70
71
if( ! function_exists('get_installmode')) {
72
    function get_installmode()
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
73
    {
74
        global $base_path, $database_server, $database_user, $database_password, $dbase, $table_prefix;
75
76
        $conf_path = "{$base_path}manager/includes/config.inc.php";
77
        if (!is_file($conf_path)) {
78
            $installmode = 0;
79
        } elseif (isset($_POST['installmode'])) {
80
            $installmode = $_POST['installmode'];
81
        } else {
82
            include_once("{$base_path}manager/includes/config.inc.php");
83
84
            if (!isset($dbase) || empty($dbase)) {
85
                $installmode = 0;
86
            } else {
87
                $conn = mysqli_connect($database_server, $database_user, $database_password);
88
                if ($conn) {
89
                    $_SESSION['database_server'] = $database_server;
90
                    $_SESSION['database_user'] = $database_user;
91
                    $_SESSION['database_password'] = $database_password;
92
93
                    $dbase = trim($dbase, '`');
94
                    $rs = mysqli_select_db($conn, $dbase);
95
                } else {
96
                    $rs = false;
97
                }
98
99
                if ($rs) {
100
                    $_SESSION['dbase'] = $dbase;
101
                    $_SESSION['table_prefix'] = $table_prefix;
102
                    $_SESSION['database_collation'] = 'utf8_general_ci';
103
                    $_SESSION['database_connection_method'] = 'SET CHARACTER SET';
104
105
                    $tbl_system_settings = "`{$dbase}`.`{$table_prefix}system_settings`";
106
                    $rs = mysqli_query($conn,
107
                        "SELECT setting_value FROM {$tbl_system_settings} WHERE setting_name='settings_version'");
108
                    if ($rs) {
109
                        $row = mysqli_fetch_assoc($rs);
110
                        $settings_version = $row['setting_value'];
111
                    } else {
112
                        $settings_version = '';
113
                    }
114
115
                    if (empty($settings_version)) {
116
                        $installmode = 0;
117
                    } else {
118
                        $installmode = 1;
119
                    }
120
                } else {
121
                    $installmode = 1;
122
                }
123
            }
124
        }
125
126
        return $installmode;
127
    }
128
}
129
130
if( ! function_exists('getLangs')) {
131
    /**
132
     * @param $install_language
133
     * @return string
134
     */
135
    function getLangs($install_language)
136
    {
137
        if ($install_language !== "english" && is_file(sprintf("../%s/includes/lang/%s.inc.php", MGR_DIR, $install_language))) {
138
            $manager_language = $install_language;
139
        } else {
140
            $manager_language = "english";
141
        }
142
143
        $langs = array();
144
        if ($handle = opendir("../" . MGR_DIR . "/includes/lang")) {
145
            while (false !== ($file = readdir($handle))) {
146
                if (strpos($file, '.inc.') !== false) {
147
                    $langs[] = $file;
148
                }
149
            }
150
            closedir($handle);
151
        }
152
        sort($langs);
153
154
        $_ = array();
155
        foreach ($langs as $language) {
156
            $abrv_language = explode('.', $language);
157
            $selected = (strtolower($abrv_language[0]) == strtolower($manager_language)) ? ' selected' : '';
158
            $_[] = sprintf('<option value="%s" %s>%s</option>', $abrv_language[0], $selected,
159
                ucwords($abrv_language[0]));
160
        }
161
162
        return implode("\n", $_);
163
    }
164
}
165