Issues (1358)

install/web.php (3 issues)

Labels
Severity
1
<?php
2
/**
3
 * @package    CleverStyle Framework
4
 * @subpackage Installer
5
 * @author     Nazar Mokrynskyi <[email protected]>
6
 * @license    0BSD
7
 */
8
namespace cs;
9
use
10
	h,
11
	PharException;
12
13 1
$phar_path = __DIR__;
14 1
if (strpos(__DIR__, 'phar://') !== 0) {
15 1
	foreach (debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS) as $step) {
16 1
		if (preg_match('#^phar://.+/web.php$#', $step['file'])) {
17 1
			$phar_path = dirname($step['file']);
18 1
			break;
19
		}
20
	}
21
}
22
23 1
date_default_timezone_set('UTC');
24 1
require_once __DIR__.'/Installer.php';
25
26
/**
27
 * @param string $phar_path
28
 *
29
 * @return string
30
 */
31
function install_form ($phar_path) {
32 1
	$timezones = get_timezones_list();
33 1
	return h::{'form[method=post]'}(
34 1
		h::nav(
0 ignored issues
show
The method nav() does not exist on h. Since you implemented __callStatic, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

34
		h::/** @scrutinizer ignore-call */ 
35
     nav(
Loading history...
35 1
			h::{'radio[name=mode]'}(
36
				[
37 1
					'value'   => ['1', '0'],
38 1
					'in'      => [h::span('Regular user'), h::span('Expert')],
0 ignored issues
show
The method span() does not exist on h. Since you implemented __callStatic, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

38
					'in'      => [h::/** @scrutinizer ignore-call */ span('Regular user'), h::span('Expert')],
Loading history...
39
					'onclick' => <<<JS
40 1
var items = document.querySelectorAll('.expert'), i; for (i = 0; i < items.length; i++) items[i].style.display = this.value == '0' ? 'table-row' : '';
41
JS
42
				]
43
			)
44
		).
45 1
		h::table(
0 ignored issues
show
The method table() does not exist on h. Since you implemented __callStatic, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

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