-
-
Notifications
You must be signed in to change notification settings - Fork 375
Expand file tree
/
Copy pathPanicAttack.php
More file actions
158 lines (144 loc) · 4.42 KB
/
Copy pathPanicAttack.php
File metadata and controls
158 lines (144 loc) · 4.42 KB
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
<?php
/**
* SPDX-License-Identifier: MIT
* Copyright (c) 2017-2018 Tobias Reich
* Copyright (c) 2018-2026 LycheeOrg.
*/
/**
* Basic PanicAttack class.
*
* Used only in case of emergency: e.g. vendor not found
*/
class PanicAttack
{
private $title = '';
private $code = 0;
private $message = '';
/**
* Check if all the elements of the array are in a string.
*
* @param string $haystack string to check against
* @param array $needles array of needles to check
*
* @return bool
*/
private function contains(string $haystack, array $needles)
{
foreach ($needles as $needle) {
if (strpos($haystack, $needle) === false) {
return false;
}
}
return true;
}
/**
* Display errors as nice page.
*
* This function is an EXIT.
*/
private function displaySimpleError()
{
$error_display = file_get_contents(__DIR__ . '/../simple_error_template.html');
$replacing = [
'$title' => $this->title,
'$code' => $this->code,
'$message' => $this->message,
];
http_response_code($this->code);
echo strtr($error_display, $replacing);
exit;
}
/**
* Called from ../index.php.
*/
public function root()
{
$this->title = 'ROOT';
$this->code = 403;
$this->message = '<span class="important">This is the root directory and MUST NOT BE PUBLICLY ACCESSIBLE.</span><br>
To access Lychee, go <a href="public/">here</a>.';
$this->displaySimpleError();
}
/**
* Called from bootstrap/initialize.php if apache rewrite is not enabled.
*/
public function apacheRewrite()
{
$this->title = 'mod_rewrite is not enabled';
$this->code = 503;
$this->message = 'You are using apache but <code>mod_rewrite</code> is not enabled.<br>
Please do: <code>a2enmod rewrite</code>';
$this->displaySimpleError();
}
/*
|--------------------------------------------------------------------------
| Catch error where composer is loading properly.
|--------------------------------------------------------------------------
| When composer has not been run yet the ../vendor/autoload.php file does
| not exists. We check if the error message contains 'require' and
| 'vendor/autoload', in such case we display a nice error.
*/
public function composerVendorNotFound()
{
$this->title = 'vendor/autoload.php not found';
$this->code = 503;
$this->message = '<code>../vendor/autoload.php</code> not found.<br>
Please do: <code>composer install --no-dev --prefer-dist</code>';
$this->displaySimpleError();
}
/*
|--------------------------------------------------------------------------
| Catch error on missing access rights.
|--------------------------------------------------------------------------
*/
public function checkAccessRightsViews()
{
$this->title = 'Invalid access rights';
$this->code = 503;
$this->message = '<code>../storage</code> and sub-directories are not writable.<br>
Please set the proper access rights.';
$this->displaySimpleError();
}
/*
|--------------------------------------------------------------------------
| Catch error on missing access rights.
|--------------------------------------------------------------------------
*/
public function checkAccessRightsEnv()
{
$this->title = 'Invalid access rights';
$this->code = 503;
$this->message = '<code>.env</code> is not writable.<br>
Please set the proper access rights.';
$this->displaySimpleError();
}
/*
|--------------------------------------------------------------------------
| Catch error on missing ldap extension.
|--------------------------------------------------------------------------
| When the ldap extension is not loaded, we display a nice error.
| This is a sanity check to prevent the user from seeing a blank page with an error in the top left
*/
public function ldap() {
$this->title = 'Missing PHP Extension';
$this->code = 500;
$this->message = 'The PHP extension "ldap" is required but not loaded. Please install and enable it to run Lychee.';
$this->displaySimpleError();
}
/**
* dispatcher.
*/
public function handle(string $error_message)
{
$handling = [
'composerVendorNotFound' => ['require', 'Failed opening required', 'vendor/autoload.php'],
'checkAccessRightsViews' => ['file_put_contents', 'storage/framework/views', 'Permission denied'],
'checkAccessRightsEnv' => ['file_put_contents', '.env'],
];
foreach ($handling as $fun => $needles) {
if ($this->contains($error_message, $needles)) {
$this->$fun();
}
}
}
}