Completed
Push — master ( a8898a...5c0dbc )
by Nazar
04:15
created

web.php ➔ install_form()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 90
Code Lines 57

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 57
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 90
ccs 0
cts 89
cp 0
crap 2
rs 8.5454

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
1 ignored issue
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 32 and the first side effect is on line 14.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
/**
3
 * @package    CleverStyle Framework
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
	Phar;
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
	$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' =>
41
						"var items = document.getElementsByClassName('expert');"
42
						."for (var i = 0; i < items.length; i++) {"
1 ignored issue
show
Coding Style Comprehensibility introduced by
The string literal for (var i = 0; i < items.length; i++) { does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
43
						."items.item(i).style.display = this.value == '0' ? 'table-row' : '';"
44
						."}"
1 ignored issue
show
Coding Style Comprehensibility introduced by
The string literal } does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
45
				]
46
			)
47
		).
48
		h::table(
49
			h::{'tr td'}(
50
				'Site name:',
51
				h::{'input[name=site_name]'}()
52
			).
53
			h::{'tr.expert td'}(
54
				'Database engine:',
55
				h::{'select[name=db_engine][size=3][selected=MySQLi]'}(
56
					file_get_json("$phar_path/db_engines.json")
57
				)
58
			).
59
			h::{'tr.expert td'}(
60
				'Database host:',
61
				h::{'input[name=db_host][value=localhost]'}(
62
					[
63
						'placeholder' => 'Relative or absolute path to DB for SQLite'
64
					]
65
				)
66
			).
67
			h::{'tr td'}(
68
				'Database name:',
69
				h::{'input[name=db_name]'}()
70
			).
71
			h::{'tr td'}(
72
				'Database user:',
73
				h::{'input[name=db_user]'}()
74
			).
75
			h::{'tr td'}(
76
				'Database user password:',
77
				h::{'input[type=password][name=db_password]'}()
78
			).
79
			h::{'tr.expert td'}(
80
				'Database tables prefix:',
81
				h::{'input[name=db_prefix]'}(
82
					[
83
						'value' => substr(md5(random_bytes(1000)), 0, 5).'_'
84
					]
85
				)
86
			).
87
			h::{'tr td'}(
88
				'Timezone:',
89
				h::{'select[name=timezone][size=7][selected=UTC]'}(
90
					[
91
						'in'    => array_keys($timezones),
92
						'value' => array_values($timezones)
93
					]
94
				)
95
			).
96
			h::{'tr td'}(
97
				'Language:',
98
				h::{'select[name=language][size=3][selected=English]'}(
99
					file_get_json("$phar_path/languages.json")
100
				)
101
			).
102
			h::{'tr td'}(
103
				'Email of administrator:',
104
				h::{'input[type=email][name=admin_email]'}()
105
			).
106
			h::{'tr td'}(
107
				'Administrator password:',
108
				h::{'input[type=password][name=admin_password]'}()
109
			)
110
		).
111
		h::{'button.license'}(
112
			'License',
113
			[
114
				'onclick' => "window.open('license.txt', 'license', 'location=no')"
115
			]
116
		).
117
		h::{'button[type=submit]'}(
118
			'Install'
119
		)
120
	);
121
}
122
123
/**
124
 * @param string $phar_path
125
 *
126
 * @return string
127
 */
128
function install_process ($phar_path) {
129 1
	if (isset($_POST['site_url'])) {
130
		$url = $_POST['site_url'];
131
	} else {
132 1
		$https  = @$_SERVER['HTTPS'] ? $_SERVER['HTTPS'] !== 'off' : (
133 1
			@$_SERVER['REQUEST_SCHEME'] === 'https' ||
134 1
			@$_SERVER['HTTP_X_FORWARDED_PROTO'] === 'https'
135
		);
136 1
		$scheme = $https ? 'https' : 'http';
137 1
		$host   = explode(':', $_SERVER['HTTP_HOST'])[0];
138 1
		$path   = explode('?', $_SERVER['REQUEST_URI'])[0] ?: '/';
139 1
		$url    = "$scheme://$host$path";
140 1
		$url    = implode('/', array_slice(explode('/', $url), 0, -2)); //Remove 2 last items
141
	}
142
	try {
143 1
		Installer::install(
144
			$phar_path,
145
			getcwd(),
146 1
			$_POST['site_name'],
147
			$url,
148 1
			$_POST['timezone'],
149 1
			$_POST['db_host'],
150 1
			$_POST['db_engine'],
151 1
			$_POST['db_name'],
152 1
			$_POST['db_user'],
153 1
			$_POST['db_password'],
154 1
			$_POST['db_prefix'],
155 1
			$_POST['language'],
156 1
			$_POST['admin_email'],
157 1
			$_POST['admin_password'],
158 1
			$_POST['mode'] ? 1 : 0
159
		);
160
	} catch (\Exception $e) {
161
		return $e->getMessage();
162
	}
163 1
	$admin_login = strstr($_POST['admin_email'], '@', true);
164 1
	$warning     = false;
165
	// Removing of installer file
166 1
	$installer = substr($phar_path, strlen('phar://'));
167 1
	if (!is_writable($installer) || !Phar::unlinkArchive($installer)) {
168
		$warning = "Please, remove installer file $installer for security!\n";
169
	}
170
	return <<<HTML
171
<h3>Congratulations! CleverStyle Framework has been installed successfully!</h3>
172
<table>
173
	<tr>
174
		<td colspan="2">Your sign in information:</td>
175
	</tr>
176
	<tr>
177
		<td>Login:</td>
178 1
		<td><pre>$admin_login</pre></td>
179
	</tr>
180
	<tr>
181
		<td>Password:</td>
182 1
		<td><pre>$_POST[admin_password]</pre></td>
183
	</tr>
184 1
	<p style="color: red">$warning</p>
185
	<button onclick="location.href = '/';">Go to website</button>
186 1
</table>
187
HTML;
188
}
189
190 1
if (count(explode('/', $_SERVER['REQUEST_URI'])) > 3) {
191
	echo 'Installation into subdirectory is not supported!';
192
	return;
193
}
194
195 1
header('Content-Type: text/html; charset=utf-8');
196 1
header('Connection: close');
197
198 1
$fs = json_decode(file_get_contents("$phar_path/fs.json"), true);
199 1
require_once "$phar_path/fs/".$fs['core/thirdparty/upf.php'];
200 1
require_once "$phar_path/fs/".$fs['core/functions.php'];
201 1
require_once "$phar_path/fs/".$fs['core/thirdparty/nazarpc/BananaHTML.php'];
202 1
require_once "$phar_path/fs/".$fs['core/classes/h/Base.php'];
203 1
require_once "$phar_path/fs/".$fs['core/classes/h.php'];
204
205 1
$version = file_get_json("$phar_path/meta.json")['version'];
206
?>
207
<!doctype html>
208
<title>CleverStyle Framework <?=$version?> Installation</title>
209
<meta charset="utf-8">
210
<style><?=file_get_contents(__DIR__.'/style.css')?></style>
211
<header>
212
	<?=file_get_contents("$phar_path/logo.svg")?>
213
	<h1>Installation</h1>
214
</header>
215
<section><?=isset($_POST['site_name']) ? install_process($phar_path) : install_form($phar_path)?></section>
216
<footer>Copyright (c) 2011-2016, Nazar Mokrynskyi</footer>
217