-
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathindex.php
105 lines (96 loc) · 2.65 KB
/
index.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
<?php
use Kirby\Demo\Demo;
use Kirby\Demo\Instance;
use Kirby\Demo\Instances;
use Kirby\Http\Response;
$demo = $instance = null;
if (class_exists(Demo::class) === true) {
$kirby = kirby();
$demo = new Demo();
$instance = $demo->instances()->current();
// ensure we have a valid instance object
if (is_a($instance, Instance::class) !== true) {
http_response_code(500);
require __DIR__ . '/etc/fail_unexpected.php';
die();
}
// if the current visitor is the first visitor,
// remember them using a session
$sessions = glob($kirby->roots()->sessions() . '/*.sess');
$noSessions = is_array($sessions) === true && empty($sessions) === true;
if ($noSessions === true) {
$kirby->session()->set('demo.creator', true);
}
// ensure that the request came from the correct visitor;
// check by IP address but fall back to the session if the IP address has changed
if (
$instance->ipHash() !== Instances::ipHash() &&
$kirby->session()->get('demo.creator') !== true
) {
http_response_code(403);
require __DIR__ . '/etc/fail_ip.php';
die();
}
}
Kirby::plugin('getkirby/demo', [
'areas' => [
'login' => function ($kirby) {
return [
'views' => [
'login' => [
'action' => function () use ($kirby) {
$system = $kirby->system();
$status = $kirby->auth()->status();
return [
'component' => 'k-login-view',
'props' => [
'methods' => array_keys($system->loginMethods()),
'pending' => [
'email' => $status->email(),
'challenge' => $status->challenge()
],
'value' => [
'email' => 'demo@getkirby.com',
'password' => 'demodemo'
]
],
];
}
],
]
];
}
],
'siteMethods' => [
'demoExpiry' => function (bool $max = false) use ($instance) {
if ($instance) {
return $max ? $instance->expiryMax() : $instance->expiry();
} else {
return time() + rand(600, 18000);
}
},
'demoExpiryHuman' => function (bool $max = false) use ($instance) {
if ($instance) {
return $max ? $instance->expiryMaxHuman() : $instance->expiryHuman();
} else {
return 'in ' . rand(2, 100) . ' quadrillion minutes';
}
}
],
'routes' => [
[
'pattern' => '/delete-demo',
'method' => 'POST',
'action' => function () use ($demo, $instance) {
if ($instance) {
// prepare the response before the Kirby files are deleted as well...
$response = $demo->config()->statusResponse($demo, 'status', 'deleted');
$instance->delete();
die($response);
} else {
return new Response('Error: Could not fetch instance object', 'text/plain', 500);
}
}
]
]
]);