initial_sanity_check()   F
last analyzed

Complexity

Conditions 48
Paths > 20000

Size

Total Lines 195
Code Lines 115

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 48
eloc 115
c 1
b 0
f 0
nc 754974725
nop 0
dl 0
loc 195
rs 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
    function make_self_url() {
4
        $proto = is_server_https() ? 'https' : 'http';
5
6
        return $proto.'://'.$_SERVER["HTTP_HOST"].$_SERVER["REQUEST_URI"];
7
    }
8
9
    function make_self_url_path() {
10
        $proto = is_server_https() ? 'https' : 'http';
11
        $url_path = $proto.'://'.$_SERVER["HTTP_HOST"].parse_url($_SERVER["REQUEST_URI"], PHP_URL_PATH);
12
13
        return $url_path;
14
    }
15
16
    function check_mysql_tables() {
17
        $pdo = Db::pdo();
18
19
        $sth = $pdo->prepare("SELECT engine, table_name FROM information_schema.tables WHERE
20
				table_schema = ? AND table_name LIKE 'ttrss_%' AND engine != 'InnoDB'");
21
        $sth->execute([DB_NAME]);
0 ignored issues
show
Bug introduced by
The constant DB_NAME was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
22
23
        $bad_tables = [];
24
25
        while ($line = $sth->fetch()) {
26
            array_push($bad_tables, $line);
27
        }
28
29
        return $bad_tables;
30
    }
31
32
    function initial_sanity_check() {
33
34
        $errors = array();
35
36
        if (!file_exists("config.php")) {
37
            array_push($errors, "Configuration file not found. Looks like you forgot to copy config.php-dist to config.php and edit it.");
38
        } else {
39
40
            require_once "sanity_config.php";
41
42
            if (file_exists("install") && !file_exists("config.php")) {
43
                array_push($errors, "Please copy config.php-dist to config.php or run the installer in install/");
44
            }
45
46
            if (strpos(PLUGINS, "auth_") === false) {
0 ignored issues
show
Bug introduced by
The constant PLUGINS was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
47
                array_push($errors, "Please enable at least one authentication module via PLUGINS constant in config.php");
48
            }
49
50
            if (function_exists('posix_getuid') && posix_getuid() == 0) {
51
                array_push($errors, "Please don't run this script as root.");
52
            }
53
54
            if (version_compare(PHP_VERSION, '5.6.0', '<')) {
55
                array_push($errors, "PHP version 5.6.0 or newer required. You're using ".PHP_VERSION.".");
56
            }
57
58
            if (!class_exists("UConverter")) {
59
                array_push($errors, "PHP UConverter class is missing, it's provided by the Internationalization (intl) module.");
60
            }
61
62
            if (CONFIG_VERSION != EXPECTED_CONFIG_VERSION) {
0 ignored issues
show
Bug introduced by
The constant CONFIG_VERSION was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
63
                array_push($errors, "Configuration file (config.php) has incorrect version. Update it with new options from config.php-dist and set CONFIG_VERSION to the correct value.");
64
            }
65
66
            if (!is_writable(CACHE_DIR."/images")) {
0 ignored issues
show
Bug introduced by
The constant CACHE_DIR was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
67
                array_push($errors, "Image cache is not writable (chmod -R 777 ".CACHE_DIR."/images)");
68
            }
69
70
            if (!is_writable(CACHE_DIR."/upload")) {
71
                array_push($errors, "Upload cache is not writable (chmod -R 777 ".CACHE_DIR."/upload)");
72
            }
73
74
            if (!is_writable(CACHE_DIR."/export")) {
75
                array_push($errors, "Data export cache is not writable (chmod -R 777 ".CACHE_DIR."/export)");
76
            }
77
78
            if (GENERATED_CONFIG_CHECK != EXPECTED_CONFIG_VERSION) {
0 ignored issues
show
introduced by
The condition GENERATED_CONFIG_CHECK != EXPECTED_CONFIG_VERSION is always false.
Loading history...
79
                array_push($errors,
80
                    "Configuration option checker sanity_config.php is outdated, please recreate it using ./utils/regen_config_checks.sh");
81
            }
82
83
            foreach ($required_defines as $d) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $required_defines seems to be never defined.
Loading history...
84
                if (!defined($d)) {
85
                    array_push($errors,
86
                        "Required configuration file parameter $d is not defined in config.php. You might need to copy it from config.php-dist.");
87
                }
88
            }
89
90
            if (SINGLE_USER_MODE && class_exists("PDO")) {
0 ignored issues
show
Bug introduced by
The constant SINGLE_USER_MODE was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
91
                $pdo = DB::pdo();
92
93
                $res = $pdo->query("SELECT id FROM ttrss_users WHERE id = 1");
94
95
                if (!$res->fetch()) {
96
                    array_push($errors, "SINGLE_USER_MODE is enabled in config.php but default admin account is not found.");
97
                }
98
            }
99
100
            $ref_self_url_path = make_self_url_path();
101
            $ref_self_url_path = preg_replace("/\w+\.php$/", "", $ref_self_url_path);
102
103
            if (SELF_URL_PATH == "http://example.org/tt-rss/") {
0 ignored issues
show
Bug introduced by
The constant SELF_URL_PATH was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
104
                array_push($errors,
105
                        "Please set SELF_URL_PATH to the correct value for your server (possible value: <b>$ref_self_url_path</b>)");
106
            }
107
108
            if (isset($_SERVER["HTTP_HOST"]) &&
109
                (!defined('_SKIP_SELF_URL_PATH_CHECKS') || !_SKIP_SELF_URL_PATH_CHECKS) &&
0 ignored issues
show
Bug introduced by
The constant _SKIP_SELF_URL_PATH_CHECKS was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
110
                SELF_URL_PATH != $ref_self_url_path && SELF_URL_PATH != mb_substr($ref_self_url_path, 0, mb_strlen($ref_self_url_path) - 1)) {
111
                array_push($errors,
112
                    "Please set SELF_URL_PATH to the correct value detected for your server: <b>$ref_self_url_path</b>");
113
            }
114
115
            if (!is_writable(ICONS_DIR)) {
0 ignored issues
show
Bug introduced by
The constant ICONS_DIR was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
116
                array_push($errors, "ICONS_DIR defined in config.php is not writable (chmod -R 777 ".ICONS_DIR.").\n");
117
            }
118
119
            if (!is_writable(LOCK_DIRECTORY)) {
0 ignored issues
show
Bug introduced by
The constant LOCK_DIRECTORY was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
120
                array_push($errors, "LOCK_DIRECTORY defined in config.php is not writable (chmod -R 777 ".LOCK_DIRECTORY.").\n");
121
            }
122
123
            if (!function_exists("curl_init") && !ini_get("allow_url_fopen")) {
124
                array_push($errors, "PHP configuration option allow_url_fopen is disabled, and CURL functions are not present. Either enable allow_url_fopen or install PHP extension for CURL.");
125
            }
126
127
            if (!function_exists("json_encode")) {
128
                array_push($errors, "PHP support for JSON is required, but was not found.");
129
            }
130
131
            if (DB_TYPE == "mysql" && !function_exists("mysqli_connect")) {
0 ignored issues
show
Bug introduced by
The constant DB_TYPE was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
132
                array_push($errors, "PHP support for MySQL is required for configured DB_TYPE in config.php.");
133
            }
134
135
            if (DB_TYPE == "pgsql" && !function_exists("pg_connect")) {
136
                array_push($errors, "PHP support for PostgreSQL is required for configured DB_TYPE in config.php");
137
            }
138
139
            if (!class_exists("PDO")) {
140
                array_push($errors, "PHP support for PDO is required but was not found.");
141
            }
142
143
            if (!function_exists("mb_strlen")) {
144
                array_push($errors, "PHP support for mbstring functions is required but was not found.");
145
            }
146
147
            if (!function_exists("hash")) {
148
                array_push($errors, "PHP support for hash() function is required but was not found.");
149
            }
150
151
            if (ini_get("safe_mode")) {
152
                array_push($errors, "PHP safe mode setting is obsolete and not supported by tt-rss.");
153
            }
154
155
            if (!function_exists("mime_content_type")) {
156
                array_push($errors, "PHP function mime_content_type() is missing, try enabling fileinfo module.");
157
            }
158
159
            if (!class_exists("DOMDocument")) {
160
                array_push($errors, "PHP support for DOMDocument is required, but was not found.");
161
            }
162
163
            if (DB_TYPE == "mysql") {
164
                $bad_tables = check_mysql_tables();
165
166
                if (count($bad_tables) > 0) {
167
                    $bad_tables_fmt = [];
168
169
                    foreach ($bad_tables as $bt) {
170
                        array_push($bad_tables_fmt, sprintf("%s (%s)", $bt['table_name'], $bt['engine']));
171
                    }
172
173
                    $msg = "<p>The following tables use an unsupported MySQL engine: <b>".
174
                        implode(", ", $bad_tables_fmt)."</b>.</p>";
175
176
                    $msg .= "<p>The only supported engine on MySQL is InnoDB. MyISAM lacks functionality to run
177
						tt-rss.
178
						Please backup your data (via OPML) and re-import the schema before continuing.</p>
179
						<p><b>WARNING: importing the schema would mean LOSS OF ALL YOUR DATA.</b></p>";
180
181
182
                    array_push($errors, $msg);
183
                }
184
            }
185
        }
186
187
        if (count($errors) > 0 && $_SERVER['REQUEST_URI']) { ?>
188
			<!DOCTYPE html>
189
			<html>
190
			<head>
191
			<title>Startup failed</title>
192
				<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
193
				<link rel="stylesheet" type="text/css" href="css/default.css">
194
			</head>
195
		<body class='sanity_failed claro ttrss_utility'>
196
			<div class="content">
197
198
			<h1>Startup failed</h1>
199
200
			<p>Tiny Tiny RSS was unable to start properly. This usually means a misconfiguration or an incomplete upgrade. Please fix
201
			errors indicated by the following messages:</p>
202
203
			<?php foreach ($errors as $error) { echo format_error($error); } ?>
0 ignored issues
show
Deprecated Code introduced by
The function format_error() has been deprecated: Use twig function errorMessage ( Ignorable by Annotation )

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

203
			<?php foreach ($errors as $error) { echo /** @scrutinizer ignore-deprecated */ format_error($error); } ?>

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
Bug introduced by
Are you sure the usage of format_error($error) is correct as it seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
Unused Code introduced by
The call to format_error() has too many arguments starting with $error. ( Ignorable by Annotation )

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

203
			<?php foreach ($errors as $error) { echo /** @scrutinizer ignore-call */ format_error($error); } ?>

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
204
205
			<p>You might want to check tt-rss <a href="http://tt-rss.org/wiki">wiki</a> or the
206
				<a href="http://tt-rss.org/forum">forums</a> for more information. Please search the forums before creating new topic
207
				for your question.</p>
208
209
		</div>
210
		</body>
211
		</html>
212
213
		<?php
214
            die;
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...
215
        } else if (count($errors) > 0) {
216
            echo "Tiny Tiny RSS was unable to start properly. This usually means a misconfiguration or an incomplete upgrade.\n";
217
            echo "Please fix errors indicated by the following messages:\n\n";
218
219
            foreach ($errors as $error) {
220
                echo " * $error\n";
221
            }
222
223
            echo "\nYou might want to check tt-rss wiki or the forums for more information.\n";
224
            echo "Please search the forums before creating new topic for your question.\n";
225
226
            exit(-1);
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...
227
        }
228
    }
229
230
    initial_sanity_check();
231
232
?>
233