Completed
Push — master ( 195cac...2c4aac )
by Nazar
05:02
created

web.php ➔ install_process()   C

Complexity

Conditions 11
Paths 63

Size

Total Lines 64
Code Lines 44

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 29
CRAP Score 29.2496

Importance

Changes 0
Metric Value
cc 11
eloc 44
nc 63
nop 1
dl 0
loc 64
ccs 29
cts 62
cp 0.4677
crap 29.2496
rs 6.0563
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
 * @package    CleverStyle Framework
4
 * @subpackage Installer
5
 * @author     Nazar Mokrynskyi <[email protected]>
6
 * @copyright  Copyright (c) 2016-2017, Nazar Mokrynskyi
7
 * @license    MIT License, see license.txt
8
 */
9
namespace cs;
10
use
11
	h,
12
	PharException;
13
14 1
$phar_path = __DIR__;
15 1
if (strpos(__DIR__, 'phar://') !== 0) {
16 1
	foreach (debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS) as $step) {
17 1
		if (preg_match('#^phar://.+/web.php$#', $step['file'])) {
18 1
			$phar_path = dirname($step['file']);
19 1
			break;
20
		}
21
	}
22
}
23
24 1
date_default_timezone_set('UTC');
25 1
require_once __DIR__.'/Installer.php';
26
27
/**
28
 * @param string $phar_path
29
 *
30
 * @return string
31
 */
32
function install_form ($phar_path) {
33 1
	$timezones = get_timezones_list();
34 1
	return h::{'form[method=post]'}(
35 1
		h::nav(
36 1
			h::{'radio[name=mode]'}(
37
				[
38 1
					'value'   => ['1', '0'],
39 1
					'in'      => [h::span('Regular user'), h::span('Expert')],
40
					'onclick' => <<<JS
41 1
var items = document.querySelectorAll('.expert'), i; for (i = 0; i < items.length; i++) items[i].style.display = this.value == '0' ? 'table-row' : '';
42
JS
43
				]
44
			)
45
		).
46 1
		h::table(
47 1
			h::{'tr td'}(
48 1
				'Site name:',
49 1
				h::{'input[name=site_name]'}()
50
			).
51 1
			h::{'tr.expert td'}(
52 1
				'Database driver:',
53 1
				h::{'select[name=db_driver][size=3][selected=MySQLi]'}(
54 1
					file_get_json("$phar_path/db_drivers.json")
55
				)
56
			).
57 1
			h::{'tr.expert td'}(
58 1
				'Database host:',
59 1
				h::{'input[name=db_host][value=localhost]'}(
60
					[
61 1
						'placeholder' => 'Relative or absolute path to DB for SQLite'
62
					]
63
				)
64
			).
65 1
			h::{'tr td'}(
66 1
				'Database name:',
67 1
				h::{'input[name=db_name]'}()
68
			).
69 1
			h::{'tr td'}(
70 1
				'Database user:',
71 1
				h::{'input[name=db_user]'}()
72
			).
73 1
			h::{'tr td'}(
74 1
				'Database user password:',
75 1
				h::{'input[type=password][name=db_password]'}()
76
			).
77 1
			h::{'tr.expert td'}(
78 1
				'Database tables prefix:',
79 1
				h::{'input[name=db_prefix]'}(
80
					[
81 1
						'value' => substr(md5(random_bytes(1000)), 0, 5).'_'
82
					]
83
				)
84
			).
85 1
			h::{'tr td'}(
86 1
				'Timezone:',
87 1
				h::{'select[name=timezone][size=7][selected=UTC]'}(
88
					[
89 1
						'in'    => array_keys($timezones),
90 1
						'value' => array_values($timezones)
91
					]
92
				)
93
			).
94 1
			h::{'tr td'}(
95 1
				'Language:',
96 1
				h::{'select[name=language][size=3][selected=English]'}(
97 1
					file_get_json("$phar_path/languages.json")
98
				)
99
			).
100 1
			h::{'tr td'}(
101 1
				'Email of administrator:',
102 1
				h::{'input[type=email][name=admin_email]'}()
103
			).
104 1
			h::{'tr td'}(
105 1
				'Administrator password:',
106 1
				h::{'input[type=password][name=admin_password]'}()
107
			)
108
		).
109 1
		h::{'button.license'}(
110 1
			'License',
111
			[
112 1
				'onclick' => "window.open('license.txt', 'license', 'location=no')"
113
			]
114
		).
115 1
		h::{'button[type=submit]'}(
116 1
			'Install'
117
		)
118
	);
119
}
120
121
/**
122
 * @param string $phar_path
123
 *
124
 * @return string
125
 */
126
function install_process ($phar_path) {
127 1
	if (isset($_POST['site_url'])) {
128
		$url = $_POST['site_url'];
129
	} else {
130 1
		$https  = @$_SERVER['HTTPS'] ? $_SERVER['HTTPS'] !== 'off' : (
131 1
			@$_SERVER['REQUEST_SCHEME'] === 'https' ||
132 1
			@$_SERVER['HTTP_X_FORWARDED_PROTO'] === 'https'
133
		);
134 1
		$scheme = $https ? 'https' : 'http';
135 1
		$host   = explode(':', $_SERVER['HTTP_HOST'])[0];
136 1
		$url    = "$scheme://$host";
137
	}
138
	try {
139 1
		Installer::install(
140
			$phar_path,
141
			getcwd(),
142 1
			$_POST['site_name'],
143
			$url,
144 1
			$_POST['timezone'],
145 1
			$_POST['db_host'],
146 1
			$_POST['db_driver'],
147 1
			$_POST['db_name'],
148 1
			$_POST['db_user'],
149 1
			$_POST['db_password'],
150 1
			$_POST['db_prefix'],
151 1
			$_POST['language'],
152 1
			$_POST['admin_email'],
153 1
			$_POST['admin_password'],
154 1
			$_POST['mode'] ? 1 : 0
155
		);
156
	} catch (\Exception $e) {
157
		return $e->getMessage();
158
	}
159 1
	$admin_login = strstr($_POST['admin_email'], '@', true);
160 1
	$warning     = false;
161
	// Removing of installer file
162 1
	$installer       = substr($phar_path, strlen('phar://'));
163 1
	$unlink_function = $phar_path == __DIR__ ? 'unlink' : ['Phar', 'unlinkArchive'];
164
	try {
165 1
		if (!is_writable($installer) || !$unlink_function($installer)) {
166 1
			throw new PharException;
167
		}
168
	} catch (PharException $e) {
169
		$warning = "Please, remove installer file $installer for security!\n";
170
	}
171
	return <<<HTML
172
<h3>Congratulations! CleverStyle Framework has been installed successfully!</h3>
173
<table>
174
	<tr>
175
		<td colspan="2">Your sign in information:</td>
176
	</tr>
177
	<tr>
178
		<td>Login:</td>
179 1
		<td><pre>$admin_login</pre></td>
180
	</tr>
181
	<tr>
182
		<td>Password:</td>
183 1
		<td><pre>$_POST[admin_password]</pre></td>
184
	</tr>
185 1
	<p style="color: red">$warning</p>
186
	<button onclick="location.href = '/';">Go to website</button>
187
</table>
188
HTML;
189
}
190
191 1
if (count(explode('/', $_SERVER['REQUEST_URI'])) > 3) {
192
	echo 'Installation into subdirectory is not supported!';
193
	return;
194
}
195
196 1
header('Content-Type: text/html; charset=utf-8');
197 1
header('Connection: close');
198
199 1
$fs = json_decode(file_get_contents("$phar_path/fs.json"), true);
200 1
require_once "$phar_path/fs/".$fs['core/thirdparty/upf.php'];
201 1
require_once "$phar_path/fs/".$fs['core/functions.php'];
202 1
require_once "$phar_path/fs/".$fs['core/thirdparty/nazarpc/BananaHTML.php'];
203 1
require_once "$phar_path/fs/".$fs['core/classes/h/Base.php'];
204 1
require_once "$phar_path/fs/".$fs['core/classes/h.php'];
205
206 1
$version = file_get_json("$phar_path/meta.json")['version'];
207
?>
208 1
<!doctype html>
209
<title>CleverStyle Framework <?=$version?> Installation</title>
210
<meta charset="utf-8">
211
<style><?=file_get_contents(__DIR__.'/style.css')?></style>
212
<header>
213
	<?=file_get_contents("$phar_path/logo.svg")?>
214 1
	<h1>Installation</h1>
215
</header>
216
<section><?=isset($_POST['site_name']) ? install_process($phar_path) : install_form($phar_path)?></section>
217
<footer>Copyright (c) 2011-2017, Nazar Mokrynskyi</footer>
218