-
-
Notifications
You must be signed in to change notification settings - Fork 175
/
Copy pathSystem.php
464 lines (402 loc) · 10.9 KB
/
System.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
<?php
namespace Kirby\Cms;
use Throwable;
use Kirby\Data\Json;
use Kirby\Exception\Exception;
use Kirby\Exception\InvalidArgumentException;
use Kirby\Exception\PermissionException;
use Kirby\Http\Remote;
use Kirby\Http\Uri;
use Kirby\Http\Url;
use Kirby\Toolkit\Dir;
use Kirby\Toolkit\F;
use Kirby\Toolkit\Str;
use Kirby\Toolkit\V;
/**
* The System class gathers all information
* about the server, PHP and other environment
* parameters and checks for a valid setup.
*
* This is mostly used by the panel installer
* to check if the panel can be installed at all.
*
* @package Kirby Cms
* @author Bastian Allgeier <[email protected]>
* @link https://getkirby.com
* @copyright Bastian Allgeier GmbH
* @license https://getkirby.com/license
*/
class System
{
/**
* @var App
*/
protected $app;
/**
* @param Kirby\Cms\App $app
*/
public function __construct(App $app)
{
$this->app = $app;
// try to create all folders that could be missing
$this->init();
}
/**
* Improved `var_dump` output
*
* @return array
*/
public function __debuginfo(): array
{
return $this->toArray();
}
/**
* Get an status array of all checks
*
* @return array
*/
public function status(): array
{
return [
'accounts' => $this->accounts(),
'content' => $this->content(),
'curl' => $this->curl(),
'sessions' => $this->sessions(),
'mbstring' => $this->mbstring(),
'media' => $this->media(),
'php' => $this->php(),
'server' => $this->server(),
];
}
/**
* Check for a writable accounts folder
*
* @return boolean
*/
public function accounts(): bool
{
return is_writable($this->app->root('accounts'));
}
/**
* Check for a writable content folder
*
* @return boolean
*/
public function content(): bool
{
return is_writable($this->app->root('content'));
}
/**
* Check for an existing curl extension
*
* @return boolean
*/
public function curl(): bool
{
return extension_loaded('curl');
}
/**
* Returns the app's human-readable
* index URL without scheme
*
* @return string
*/
public function indexUrl(): string
{
$url = $this->app->url('index');
if (Url::isAbsolute($url)) {
$uri = Url::toObject($url);
} else {
// index URL was configured without host, use the current host
$uri = Uri::current([
'path' => $url,
'query' => null
]);
}
return $uri->setScheme(null)->setSlash(false)->toString();
}
/**
* Create the most important folders
* if they don't exist yet
*
* @return void
*/
public function init()
{
/* /site/accounts */
try {
Dir::make($this->app->root('accounts'));
} catch (Throwable $e) {
throw new PermissionException('The accounts directory could not be created');
}
/* /content */
try {
Dir::make($this->app->root('content'));
} catch (Throwable $e) {
throw new PermissionException('The content directory could not be created');
}
try {
Dir::make($this->app->root('media'));
} catch (Throwable $e) {
throw new PermissionException('The media directory could not be created');
}
}
/**
* Check if the panel is installable.
* On a public server the panel.install
* option must be explicitly set to true
* to get the installer up and running.
*
* @return boolean
*/
public function isInstallable(): bool
{
return $this->isLocal() === true || $this->app->option('panel.install', false) === true;
}
/**
* Check if Kirby is already installed
*
* @return boolean
*/
public function isInstalled(): bool
{
return $this->app->users()->count() > 0;
}
/**
* Check if this is a local installation
*
* @return boolean
*/
public function isLocal(): bool
{
$server = $this->app->server();
$host = $server->host();
if ($host === 'localhost') {
return true;
}
if (in_array($server->address(), ['::1', '127.0.0.1', '0.0.0.0']) === true) {
return true;
}
if (Str::endsWith($host, '.dev') === true) {
return true;
}
if (Str::endsWith($host, '.local') === true) {
return true;
}
if (Str::endsWith($host, '.test') === true) {
return true;
}
return false;
}
/**
* Check if all tests pass
*
* @return boolean
*/
public function isOk(): bool
{
return in_array(false, array_values($this->status()), true) === false;
}
/**
* Normalizes the app's index URL for
* licensing purposes
*
* @param string|null $url Input URL, by default the app's index URL
* @return string Normalized URL
*/
protected function licenseUrl(string $url = null): string
{
if ($url === null) {
$url = $this->indexUrl();
}
// remove common "testing" subdomains as well as www.
// to ensure that installations of the same site have
// the same license URL; only for installations at /,
// subdirectory installations are difficult to normalize
if (Str::contains($url, '/') === false) {
if (Str::startsWith($url, 'www.')) {
return substr($url, 4);
}
if (Str::startsWith($url, 'dev.')) {
return substr($url, 4);
}
if (Str::startsWith($url, 'test.')) {
return substr($url, 5);
}
if (Str::startsWith($url, 'staging.')) {
return substr($url, 8);
}
}
return $url;
}
/**
* Loads the license file and returns
* the license information if available
*
* @return string|false
*/
public function license()
{
try {
$license = Json::read($this->app->root('config') . '/.license');
} catch (Throwable $e) {
return false;
}
// check for all required fields for the validation
if (isset(
$license['license'],
$license['order'],
$license['date'],
$license['email'],
$license['domain'],
$license['signature']
) !== true) {
return false;
}
// build the license verification data
$data = [
'license' => $license['license'],
'order' => $license['order'],
'email' => hash('sha256', $license['email'] . 'kwAHMLyLPBnHEskzH9pPbJsBxQhKXZnX'),
'domain' => $license['domain'],
'date' => $license['date']
];
// get the public key
$pubKey = F::read($this->app->root('kirby') . '/kirby.pub');
// verify the license signature
if (openssl_verify(json_encode($data), hex2bin($license['signature']), $pubKey, 'RSA-SHA256') !== 1) {
return false;
}
// verify the URL
if ($this->licenseUrl() !== $this->licenseUrl($license['domain'])) {
return false;
}
return $license['license'];
}
/**
* Check for an existing mbstring extension
*
* @return boolean
*/
public function mbString(): bool
{
return extension_loaded('mbstring');
}
/**
* Check for a writable media folder
*
* @return boolean
*/
public function media(): bool
{
return is_writable($this->app->root('media'));
}
/**
* Check for a valid PHP version
*
* @return boolean
*/
public function php(): bool
{
return version_compare(phpversion(), '7.1.0', '>=');
}
/**
* Validates the license key
* and adds it to the .license file in the config
* folder if possible.
*
* @param string $license
* @param string $email
* @return boolean
*/
public function register(string $license = null, string $email = null): bool
{
if (Str::startsWith($license, 'K3-PRO-') === false) {
throw new InvalidArgumentException([
'key' => 'license.format'
]);
}
if (V::email($email) === false) {
throw new InvalidArgumentException([
'key' => 'license.email'
]);
}
$response = Remote::get('https://licenses.getkirby.com/register', [
'data' => [
'license' => $license,
'email' => $email,
'domain' => $this->indexUrl()
]
]);
if ($response->code() !== 200) {
throw new Exception($response->content());
}
// decode the response
$json = Json::decode($response->content());
// replace the email with the plaintext version
$json['email'] = $email;
// where to store the license file
$file = $this->app->root('config') . '/.license';
// save the license information
Json::write($file, $json);
if ($this->license() === false) {
throw new InvalidArgumentException([
'key' => 'license.verification'
]);
}
return true;
}
/**
* Check for a valid server environment
*
* @return boolean
*/
public function server(): bool
{
$servers = [
'apache',
'caddy',
'litespeed',
'nginx',
'php'
];
$software = $_SERVER['SERVER_SOFTWARE'] ?? null;
return preg_match('!(' . implode('|', $servers) . ')!i', $software) > 0;
}
/**
* Check for a writable sessions folder
*
* @return boolean
*/
public function sessions(): bool
{
return is_writable($this->app->root('sessions'));
}
/**
* Return the status as array
*
* @return array
*/
public function toArray(): array
{
return $this->status();
}
/**
* Upgrade to the new folder separator
*
* @param string $root
* @return void
*/
public static function upgradeContent(string $root)
{
$index = Dir::read($root);
foreach ($index as $dir) {
$oldRoot = $root . '/' . $dir;
$newRoot = preg_replace('!\/([0-9]+)\-!', '/$1_', $oldRoot);
if (is_dir($oldRoot) === true) {
Dir::move($oldRoot, $newRoot);
static::upgradeContent($newRoot);
}
}
}
}