-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdetached.php
More file actions
177 lines (139 loc) · 5.33 KB
/
detached.php
File metadata and controls
177 lines (139 loc) · 5.33 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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
<?php
/**
* Detached helper not dependent on any Laravel functions, new PHP versions, classes and namespaces.
*/
class DetachedHelper
{
const MINIMUM_PHP_VERSION = '8.2.3';
const MINIMUM_RECOMMENDED_PHP_MEMORY_LIMIT = 128;
const MAINTENANCE_FILE_PATH = __DIR__ . '/storage/framework/maintenance.php';
const DOWN_FILE_PATH = __DIR__ . '/storage/framework/down';
const INSTALLED_FILE = '.installed';
const INSTALL_ROUTE_PREFIX = 'install';
const BASE_PATH = __DIR__;
protected static $iniAllData;
public static function isUsingMinimumPhpVersion()
{
return version_compare(phpversion(), static::MINIMUM_PHP_VERSION, '>=');
}
// NOTE: Perhaps from v1.1.2 this is no longer needed? The updater is catching the \Throwable exception
// in previous versions, only "finally" was used, in this case, on fatal error, will disable maintenance mode
public static function shouldDisableMaintenance()
{
if (! file_exists(static::MAINTENANCE_FILE_PATH)) {
return false;
}
// Used to fix the updater for users performing update from version lower than 1.1.0
// The updater throws an error and the app is stuck in maintenance mode
// The code will disable maintenance mode after 3 minutes and a page will be shown to finalize the update
$started = filectime(static::MAINTENANCE_FILE_PATH);
if (! $started) {
return false;
}
// We will check if the file creation date is older then 3 minutes
// It should not take more then 3 minutes to perform an update
if ((time() - $started) / 60 >= 3 && file_exists(static::DOWN_FILE_PATH)) {
return strpos(
file_get_contents(static::DOWN_FILE_PATH),
'Application update is in progress'
) !== false;
}
return false;
}
public static function disableMaintenance()
{
unlink(static::MAINTENANCE_FILE_PATH);
unlink(static::DOWN_FILE_PATH);
}
public static function memoryLimitIsTooLow()
{
$limit = static::getMemoryLimitInMegabytes();
if ($limit === -1) {
return false;
}
return $limit < static::MINIMUM_RECOMMENDED_PHP_MEMORY_LIMIT;
}
public static function getMemoryLimitInMegabytes()
{
$limit = static::memoryLimitToBytes(ini_get('memory_limit'));
if ($limit === -1) {
return $limit;
}
return $limit / (1024 * 1024);
}
/** Use only if autoloader included */
public static function requiresInstallation()
{
return ! file_exists(implode(DIRECTORY_SEPARATOR, [
static::BASE_PATH,
'storage',
static::INSTALLED_FILE,
]));
}
/** Use only if autoloader included */
public static function isInstalling()
{
return strpos($_SERVER['REQUEST_URI'], static::INSTALL_ROUTE_PREFIX) !== false;
}
public static function memoryLimitToBytes($memoryLimit)
{
if ('-1' === $memoryLimit) {
return -1;
}
$memoryLimit = strtolower($memoryLimit);
$max = strtolower(ltrim($memoryLimit, '+'));
if (str_starts_with($max, '0x')) {
$max = \intval($max, 16);
} elseif (str_starts_with($max, '0')) {
$max = \intval($max, 8);
} else {
$max = (int) $max;
}
switch (substr($memoryLimit, -1)) {
case 't': $max *= 1024;
// no break
case 'g': $max *= 1024;
// no break
case 'm': $max *= 1024;
// no break
case 'k': $max *= 1024;
}
return $max;
}
public static function raiseMemoryLimit($limit)
{
// Return early if the limit cannot be changed.
if (static::isIniValueChangeable('memory_limit') === false) {
return false;
}
$currentLimitInBytes = static::memoryLimitToBytes(ini_get('memory_limit'));
if ($currentLimitInBytes === -1) {
return false;
}
$limitInBytes = static::memoryLimitToBytes($limit);
if ($limitInBytes === -1 || $limitInBytes > $currentLimitInBytes) {
if (ini_set('memory_limit', $limit) !== false) {
return $limit;
}
return false;
}
return false;
}
public static function isIniValueChangeable($setting)
{
if (! isset(static::$iniAllData)) {
static::$iniAllData = false;
// Sometimes `ini_get_all()` is disabled via the `disable_functions` option for "security purposes".
if (function_exists('ini_get_all')) {
static::$iniAllData = ini_get_all();
}
}
if (isset(static::$iniAllData[$setting]['access']) &&
(INI_ALL === (static::$iniAllData[$setting]['access'] & 7) ||
INI_USER === (static::$iniAllData[$setting]['access'] & 7))) {
return true;
}
// If we were unable to retrieve the details, fail gracefully to assume it's changeable.
return (bool) (! is_array(static::$iniAllData));
}
}