-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
discord.php
125 lines (105 loc) · 2.92 KB
/
discord.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
<?php
register_shutdown_function("discordCheckForFatal");
set_error_handler("discordErrorHandler");
/**
* Check for fatal errors and send them to discord.
*
* @return void
*/
function discordCheckForFatal()
{
$error = error_get_last();
if (isset($error['type']) && $error['type'] == E_ERROR) {
discord(
"**PHP Fatal error**: <@918438083861573692> " .
"```\r\n{$error['message']}\r\n```\r\n" .
"in `{$error['file']}` on line `{$error['line']}`"
);
}
}
/**
* Log PHP Errors to discord
*
* @param int $errorNumber PHP Error number
* @param string $errorString PHP Error string
* @param string $errorFile PHP file
* @param int $errorLine line number
* @param null $context context (if any)
*
* @return void
*/
function discordErrorHandler(
$errorNumber,
$errorString,
$errorFile,
$errorLine,
$context = null
) {
if (preg_match("/DOMDocument/", $errorString)) {
return;
}
/// @ sign temporary disabled error reporting
/// < PHP 8.0.0
if (error_reporting() == 0) {
return;
}
$errorType = "Unknown";
switch ($errorNumber) {
case E_ERROR:
$errorType = "Error";
break;
case E_WARNING:
$errorType = "Warning";
break;
case E_PARSE:
$errorType = "Parse error";
break;
case E_NOTICE:
$errorType = "Notice";
break;
default:
$errorType = "Unknown";
break;
}
discord("**PHP {$errorType}**: <@&918438083861573692> ```\r\n{$errorString}\r\n```\r\nin `{$errorFile}` on line `{$errorLine}`");
}
/**
* Send a message to discord.
*
* @param string $message Message to send to discord
* @param string $toChannel Channel to send to
*
* @return void
*/
function discord($message)
{
global $settings;
// Default (Reporting channel)
$webhookurl = $settings['discord_webhook'];
if (PHP_OS === 'Darwin') {
// Do not tag.
$message = preg_replace("/@&/", "\@\&", $message);
$message = "_Test Message:_ " . $message;
}
$json_data = json_encode(
[
// Username
"username" => "Aurora Editor API",
// Avatar URL.
"avatar_url" => "https://avatars.githubusercontent.com/u/106490518?s=200&v=4",
// Message
"content" => $message
],
JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE
);
$ch = curl_init($webhookurl);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type: application/json'));
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $json_data);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($ch);
echo $response;
curl_close($ch);
}