Completed
Push — master ( fab357...0ba320 )
by Nazar
07:50
created

web.php ➔ install_form()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 88
Code Lines 55

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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