Completed
Push — master ( a19c47...e3bee1 )
by Nazar
04:21
created

web.php ➔ install_process()   C

Complexity

Conditions 11
Paths 51

Size

Total Lines 76
Code Lines 53

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 11
eloc 53
c 1
b 0
f 1
nc 51
nop 0
dl 0
loc 76
rs 5.4429

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) 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
 * @return string
116
 */
117
function install_process () {
118
	if (isset($_POST['site_url'])) {
119
		$url = $_POST['site_url'];
120
	} else {
121
		$https  = @$_SERVER['HTTPS'] ? $_SERVER['HTTPS'] !== 'off' : (
122
			@$_SERVER['REQUEST_SCHEME'] === 'https' ||
123
			@$_SERVER['HTTP_X_FORWARDED_PROTO'] === 'https'
124
		);
125
		$scheme = $https ? 'https' : 'http';
126
		$host   = explode(':', $_SERVER['HTTP_HOST'])[0];
127
		$path   = explode('?', $_SERVER['REQUEST_URI'])[0] ?: '/';
128
		$url    = "$scheme://$host$path";
129
		$url    = implode('/', array_slice(explode('/', $url), 0, -2)); //Remove 2 last items
130
	}
131
	try {
132
		Installer::install(
133
			__DIR__.'/..',
134
			getcwd(),
135
			$_POST['site_name'],
136
			$url,
137
			$_POST['timezone'],
138
			$_POST['db_host'],
139
			$_POST['db_engine'],
140
			$_POST['db_name'],
141
			$_POST['db_user'],
142
			$_POST['db_password'],
143
			$_POST['db_prefix'],
144
			$_POST['db_charset'],
145
			$_POST['language'],
146
			$_POST['admin_email'],
147
			$_POST['admin_password'],
148
			!isset($_POST['mode']) || $_POST['mode'] ? 1 : 0
149
		);
150
	} catch (\Exception $e) {
151
		return $e->getMessage();
152
	}
153
	$admin_login = strstr($_POST['admin_email'], '@', true);
154
	$warning     = false;
155
	// Removing of installer file
156
	$installer = getcwd().'/'.basename(__DIR__.'/..');
157
	if (!is_writable($installer) || !unlink($installer)) {
158
		$warning = "Please, remove installer file $installer for security!\n";
159
	}
160
	return
161
		h::h3(
162
			'Congratulations! CleverStyle CMS has been installed successfully!'
163
		).
164
		h::{'table tr| td'}(
165
			[
166
				'Your sign in information:',
167
				[
168
					'colspan' => 2
169
				]
170
			],
171
			[
172
				'Login:',
173
				$admin_login
174
			],
175
			[
176
				'Password:',
177
				$_POST['admin_password']
178
			]
179
		).
180
		h::p(
181
			$warning,
182
			[
183
				'style' => 'color: red;'
184
			]
185
		).
186
		h::button(
187
			'Go to website',
188
			[
189
				'onclick' => "location.href = '/';"
190
			]
191
		);
192
}
193
194
if (count(explode('/', $_SERVER['REQUEST_URI'])) > 3) {
195
	echo 'Installation into subdirectory is not supported!';
196
	return;
197
}
198
199
header('Content-Type: text/html; charset=utf-8');
200
header('Connection: close');
201
$version = file_get_json(__DIR__.'/../meta.json')['version'];
202
echo
203
	"<!doctype html>\n".
204
	h::title("CleverStyle CMS $version Installation").
205
	h::meta(
206
		[
207
			'charset' => 'utf-8'
208
		]
209
	).
210
	h::style(file_get_contents(__DIR__.'/../install/style.css')).
211
	h::header(
212
		h::img(
213
			[
214
				'src' => 'data:image/png;charset=utf-8;base64,'.base64_encode(file_get_contents(__DIR__.'/../install/logo.png'))
215
			]
216
		).
217
		h::h1("CleverStyle CMS $version Installation")
218
	).
219
	h::section(
220
		isset($_POST['site_name']) ? install_process() : install_form()
221
	).
222
	h::footer(
223
		'Copyright (c) 2011-2016, Nazar Mokrynskyi'
224
	);
225