Completed
Push — master ( fc0340...72ef30 )
by Nazar
03:52
created

functions.php ➔ install_process()   D

Complexity

Conditions 13
Paths 153

Size

Total Lines 81
Code Lines 57

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 13
eloc 57
c 2
b 0
f 0
nc 153
nop 1
dl 0
loc 81
rs 4.6762

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
 * @package    CleverStyle CMS
4
 * @subpackage Installer
5
 * @author     Nazar Mokrynskyi <[email protected]>
6
 * @copyright  Copyright (c) 2011-2016, Nazar Mokrynskyi
7
 * @license    MIT License, see license.txt
8
 */
9
namespace cs;
10
use
11
	h;
12
13
function install_form () {
14
	$timezones = get_timezones_list();
15
	return h::{'form[method=post]'}(
16
		h::nav(
17
			h::{'radio[name=mode]'}(
18
				[
19
					'value'   => ['1', '0'],
20
					'in'      => [h::span('Regular user'), h::span('Expert')],
21
					'onclick' =>
22
						"var items = document.getElementsByClassName('expert');"
23
						."for (var i = 0; i < items.length; i++) {"
24
						."items.item(i).style.display = this.value == '0' ? 'table-row' : '';"
25
						."}"
26
				]
27
			)
28
		).
29
		h::table(
30
			h::{'tr td'}(
31
				'Site name:',
32
				h::{'input[name=site_name]'}()
33
			).
34
			h::{'tr.expert td'}(
35
				'Database engine:',
36
				h::{'select[name=db_engine][size=3][selected=MySQLi]'}(
37
					file_get_json(DIR.'/db_engines.json')
38
				)
39
			).
40
			h::{'tr.expert td'}(
41
				'Database host:',
42
				h::{'input[name=db_host][value=localhost]'}(
43
					[
44
						'placeholder' => 'Relative or absolute path to DB for SQLite'
45
					]
46
				)
47
			).
48
			h::{'tr td'}(
49
				'Database name:',
50
				h::{'input[name=db_name]'}()
51
			).
52
			h::{'tr td'}(
53
				'Database user:',
54
				h::{'input[name=db_user]'}()
55
			).
56
			h::{'tr td'}(
57
				'Database user password:',
58
				h::{'input[type=password][name=db_password]'}()
59
			).
60
			h::{'tr.expert td'}(
61
				'Database tables prefix:',
62
				h::{'input[name=db_prefix]'}(
63
					[
64
						'value' => substr(md5(random_bytes(1000)), 0, 5).'_'
65
					]
66
				)
67
			).
68
			h::{'tr.expert td'}(
69
				'Database charset:',
70
				h::{'input[name=db_charset][value=utf8mb4]'}()
71
			).
72
			h::{'tr td'}(
73
				'Timezone:',
74
				h::{'select[name=timezone][size=7][selected=UTC]'}(
75
					[
76
						'in'    => array_keys($timezones),
77
						'value' => array_values($timezones)
78
					]
79
				)
80
			).
81
			h::{'tr td'}(
82
				'Language:',
83
				h::{'select[name=language][size=3][selected=English]'}(
84
					file_get_json(DIR.'/languages.json')
85
				)
86
			).
87
			h::{'tr td'}(
88
				'Email of administrator:',
89
				h::{'input[type=email][name=admin_email]'}()
90
			).
91
			h::{'tr td'}(
92
				'Administrator password:',
93
				h::{'input[type=password][name=admin_password]'}()
94
			)
95
		).
96
		h::{'button.readme'}(
97
			'Readme',
98
			[
99
				'onclick' => "window.open('readme.html', 'readme', 'location=no')"
100
			]
101
		).
102
		h::{'button.license'}(
103
			'License',
104
			[
105
				'onclick' => "window.open('license.txt', 'license', 'location=no')"
106
			]
107
		).
108
		h::{'button[type=submit]'}(
109
			'Install'
110
		)
111
	);
112
}
113
114
/**
115
 * @param array|null $argv
116
 *
117
 * @return string
118
 */
119
function install_process ($argv = null) {
120
	if (isset($_POST['site_url'])) {
121
		$url = $_POST['site_url'];
122
	} else {
123
		$https  = @$_SERVER['HTTPS'] ? $_SERVER['HTTPS'] !== 'off' : (
124
			@$_SERVER['REQUEST_SCHEME'] === 'https' ||
125
			@$_SERVER['HTTP_X_FORWARDED_PROTO'] === 'https'
126
		);
127
		$scheme = $https ? 'https' : 'http';
128
		$host   = explode(':', $_SERVER['HTTP_HOST'])[0];
129
		$path   = explode('?', $_SERVER['REQUEST_URI'])[0] ?: '/';
130
		$url    = "$scheme://$host$path";
131
		$url    = implode('/', array_slice(explode('/', $url), 0, -2)); //Remove 2 last items
132
	}
133
	try {
134
		Installer::install(
135
			DIR,
136
			ROOT,
137
			$_POST['site_name'],
138
			$url,
139
			$_POST['timezone'],
140
			$_POST['db_host'],
141
			$_POST['db_engine'],
142
			$_POST['db_name'],
143
			$_POST['db_user'],
144
			$_POST['db_password'],
145
			$_POST['db_prefix'],
146
			$_POST['db_charset'],
147
			$_POST['language'],
148
			$_POST['admin_email'],
149
			$_POST['admin_password'],
150
			!isset($_POST['mode']) || $_POST['mode'] ? 1 : 0
151
		);
152
	} catch (\Exception $e) {
153
		return $e;
154
	}
155
	$admin_login = strstr($_POST['admin_email'], '@', true);
156
	$warning     = false;
157
	// Removing of installer file
158
	$cli       = PHP_SAPI == 'cli';
159
	$installer = $cli ? ROOT."/$argv[0]" : ROOT.'/'.pathinfo(DIR, PATHINFO_BASENAME);
160
	if (!is_writable($installer) || !unlink($installer)) {
161
		$warning = "Please, remove installer file $installer for security!\n";
162
	}
163
	if ($cli) {
164
		return "Congratulations! CleverStyle CMS has been installed successfully!\n$warning\nLogin: $admin_login\nPassword: $_POST[admin_password]";
165
	} else {
166
		return
167
			h::h3(
168
				'Congratulations! CleverStyle CMS has been installed successfully!'
169
			).
170
			h::{'table tr| td'}(
171
				[
172
					'Your sign in information:',
173
					[
174
						'colspan' => 2
175
					]
176
				],
177
				[
178
					'Login:',
179
					$admin_login
180
				],
181
				[
182
					'Password:',
183
					$_POST['admin_password']
184
				]
185
			).
186
			h::p(
187
				$warning,
188
				[
189
					'style' => 'color: red;'
190
				]
191
			).
192
			h::button(
193
				'Go to website',
194
				[
195
					'onclick' => "location.href = '/';"
196
				]
197
			);
198
	}
199
}
200