1
|
|
|
<?php |
|
|
|
|
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
|
|
|
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
|
|
|
$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++) {" |
|
|
|
|
43
|
|
|
."items.item(i).style.display = this.value == '0' ? 'table-row' : '';" |
44
|
|
|
."}" |
|
|
|
|
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 |
|
$unlink_function = $phar_path == __DIR__ ? 'unlink' : ['Phar', 'unlinkArchive']; |
168
|
|
|
try { |
169
|
1 |
|
if (!is_writable($installer) || !$unlink_function($installer)) { |
170
|
1 |
|
throw new PharException; |
171
|
|
|
} |
172
|
|
|
} catch (PharException $e) { |
173
|
|
|
$warning = "Please, remove installer file $installer for security!\n"; |
174
|
|
|
} |
175
|
|
|
return <<<HTML |
176
|
|
|
<h3>Congratulations! CleverStyle Framework has been installed successfully!</h3> |
177
|
|
|
<table> |
178
|
|
|
<tr> |
179
|
|
|
<td colspan="2">Your sign in information:</td> |
180
|
|
|
</tr> |
181
|
|
|
<tr> |
182
|
|
|
<td>Login:</td> |
183
|
1 |
|
<td><pre>$admin_login</pre></td> |
184
|
|
|
</tr> |
185
|
|
|
<tr> |
186
|
|
|
<td>Password:</td> |
187
|
1 |
|
<td><pre>$_POST[admin_password]</pre></td> |
188
|
|
|
</tr> |
189
|
1 |
|
<p style="color: red">$warning</p> |
190
|
|
|
<button onclick="location.href = '/';">Go to website</button> |
191
|
1 |
|
</table> |
192
|
|
|
HTML; |
193
|
|
|
} |
194
|
|
|
|
195
|
1 |
|
if (count(explode('/', $_SERVER['REQUEST_URI'])) > 3) { |
196
|
|
|
echo 'Installation into subdirectory is not supported!'; |
197
|
|
|
return; |
198
|
|
|
} |
199
|
|
|
|
200
|
1 |
|
header('Content-Type: text/html; charset=utf-8'); |
201
|
1 |
|
header('Connection: close'); |
202
|
|
|
|
203
|
1 |
|
$fs = json_decode(file_get_contents("$phar_path/fs.json"), true); |
204
|
1 |
|
require_once "$phar_path/fs/".$fs['core/thirdparty/upf.php']; |
205
|
1 |
|
require_once "$phar_path/fs/".$fs['core/functions.php']; |
206
|
1 |
|
require_once "$phar_path/fs/".$fs['core/thirdparty/nazarpc/BananaHTML.php']; |
207
|
1 |
|
require_once "$phar_path/fs/".$fs['core/classes/h/Base.php']; |
208
|
1 |
|
require_once "$phar_path/fs/".$fs['core/classes/h.php']; |
209
|
|
|
|
210
|
1 |
|
$version = file_get_json("$phar_path/meta.json")['version']; |
211
|
|
|
?> |
212
|
|
|
<!doctype html> |
213
|
|
|
<title>CleverStyle Framework <?=$version?> Installation</title> |
214
|
|
|
<meta charset="utf-8"> |
215
|
|
|
<style><?=file_get_contents(__DIR__.'/style.css')?></style> |
216
|
|
|
<header> |
217
|
|
|
<?=file_get_contents("$phar_path/logo.svg")?> |
218
|
|
|
<h1>Installation</h1> |
219
|
|
|
</header> |
220
|
|
|
<section><?=isset($_POST['site_name']) ? install_process($phar_path) : install_form($phar_path)?></section> |
221
|
|
|
<footer>Copyright (c) 2011-2016, Nazar Mokrynskyi</footer> |
222
|
|
|
|
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.