forked from danog/MadelineProto
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathException.php
145 lines (140 loc) · 6.2 KB
/
Exception.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
<?php
declare(strict_types=1);
/**
* Exception module.
*
* This file is part of MadelineProto.
* MadelineProto is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
* MadelineProto is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU Affero General Public License for more details.
* You should have received a copy of the GNU General Public License along with MadelineProto.
* If not, see <http://www.gnu.org/licenses/>.
*
* @author Daniil Gentili <[email protected]>
* @copyright 2016-2023 Daniil Gentili <[email protected]>
* @license https://opensource.org/licenses/AGPL-3.0 AGPLv3
* @link https://docs.madelineproto.xyz MadelineProto documentation
*/
namespace danog\MadelineProto;
use const DIRECTORY_SEPARATOR;
use const PHP_EOL;
use const PHP_MAJOR_VERSION;
use const PHP_MINOR_VERSION;
use const PHP_SAPI;
/**
* Basic exception.
*/
class Exception extends \Exception
{
use TL\PrettyException;
public function __toString(): string
{
return $this->file === 'MadelineProto' ? $this->message : '\\danog\\MadelineProto\\Exception'.($this->message !== '' ? ': ' : '').$this->message.' in '.$this->file.':'.$this->line.PHP_EOL.Magic::$revision.PHP_EOL.'TL Trace:'.PHP_EOL.$this->getTLTrace();
}
public function __construct($message = null, $code = 0, ?\Throwable $previous = null, $file = null, $line = null)
{
$this->prettifyTL();
if ($file !== null) {
$this->file = $file;
}
if ($line !== null) {
$this->line = $line;
}
parent::__construct($message, $code, $previous);
if (!str_contains($message, 'socket_accept')
&& $message !== 'Client backtrace'
&& !\in_array(basename($this->file), ['PKCS8.php', 'PSS.php'], true)
) {
Logger::log($message.' in '.basename($this->file).':'.$this->line, Logger::FATAL_ERROR);
}
}
/**
* Complain about missing extensions.
*
* @param string $extensionName Extension name
*/
public static function extension(string $extensionName): self
{
if ($extensionName === 'libtgvoip') {
$additional = sprintf(Lang::$current_lang['extensionRequiredInstallWithCustomInstructions'], 'https://voip.madelineproto.xyz');
} elseif ($extensionName === 'prime') {
$additional = sprintf(Lang::$current_lang['extensionRequiredInstallWithCustomInstructions'], 'https://prime.madelineproto.xyz');
} else {
$additional = sprintf(Lang::$current_lang['extensionRequiredInstallWithApt'], 'php'.PHP_MAJOR_VERSION.'.'.PHP_MINOR_VERSION.'-'.$extensionName);
}
$message = sprintf(Lang::$current_lang['extensionRequired'], $extensionName, $additional);
if (PHP_SAPI !== 'cli' && PHP_SAPI !== 'phpdbg') {
echo htmlentities($message).'<br>';
}
$file = 'MadelineProto';
$line = 1;
return new self($message, 0, null, $file, $line);
}
/**
* @internal
*
* Error handler
*/
public static function exceptionErrorHandler($errno = 0, $errstr = null, $errfile = null, $errline = null): bool
{
$errfileReplaced = preg_replace('/phabel-transpiler\d+\./', '', $errfile ?? '');
// If error is suppressed with @, don't throw an exception
if (error_reporting() === 0
|| strpos($errstr, 'headers already sent')
|| str_contains($errstr, 'Creation of dynamic property')
|| str_contains($errstr, 'Legacy nullable type detected')
|| str_contains($errstr, '$tdMethods is deprecated')
|| $errfileReplaced && (
str_contains($errfileReplaced, DIRECTORY_SEPARATOR.'amphp'.DIRECTORY_SEPARATOR)
|| str_contains($errfileReplaced, DIRECTORY_SEPARATOR.'league'.DIRECTORY_SEPARATOR)
|| str_contains($errfileReplaced, DIRECTORY_SEPARATOR.'phpseclib'.DIRECTORY_SEPARATOR)
)
) {
return false;
}
throw new self($errstr, $errno, null, $errfile, $errline);
}
/**
* @internal
*
* ExceptionErrorHandler.
*/
public static function exceptionHandler(\Throwable $exception): void
{
$print = static function (string $s): void {
Logger::log($s, Logger::FATAL_ERROR);
if (headers_sent()) {
return;
}
http_response_code(500);
if (PHP_SAPI === 'cli' || PHP_SAPI === 'phpdbg') {
echo($s.PHP_EOL);
} else {
echo(str_replace("\n", "<br>", htmlentities($s)).PHP_EOL);
}
};
if (str_contains($exception->getMessage(), 'Fiber stack protect failed')
|| str_contains($exception->getMessage(), 'Fiber stack allocate failed')
) {
$maps = "?";
try {
$maps = '~'.substr_count(file_get_contents('/proc/self/maps'), "\n");
$pid = getmypid();
$maps = '~'.substr_count(file_get_contents("/proc/$pid/maps"), "\n");
} catch (\Throwable) {
}
$print(Lang::$current_lang['manualAdminActionRequired']);
$print(Lang::$current_lang['manualAdminActionRequired']);
$print(Lang::$current_lang['manualAdminActionRequired']);
$print(sprintf(Lang::$current_lang['mmapErrorPart1'], $maps));
$print(sprintf(Lang::$current_lang['mmapErrorPart2'], 'echo 262144 | sudo tee /proc/sys/vm/max_map_count'));
$print(sprintf(Lang::$current_lang['mmapErrorPart3'], 'echo vm.max_map_count=262144 | sudo tee /etc/sysctl.d/40-madelineproto.conf'));
$print(Lang::$current_lang['mmapErrorPart4']);
$print(Lang::$current_lang['manualAdminActionRequired']);
$print(Lang::$current_lang['manualAdminActionRequired']);
$print(Lang::$current_lang['manualAdminActionRequired']);
}
$print((string) $exception);
die(1);
}
}