-
Notifications
You must be signed in to change notification settings - Fork 0
/
library.php
204 lines (165 loc) · 7.47 KB
/
library.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
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
<?php
require __DIR__ . "/library/external/vendor/autoload.php";
class Slack{
private object $Property;
public function __construct(?string $AuthorizationToken){
$APIURL = "https://slack.com/api/";
$cURL = curl_init();
$this->Property = (object)get_defined_vars();
//if(is_null($this->Property->URL))$this->Property->URL = "http://{$this->Property->Host}" . ($HTTPPort == 80 ? null : ":{$this->Property->HTTPPort}") . "";
$this->Property->cURL = curl_init();
}
public function __destruct(){
curl_close($this->Property->cURL);
}
public function SendMessage(string $Message, string $ChannelList, ?string $AuthorizationToken = null){
if(is_null($AuthorizationToken))$AuthorizationToken = $this->Property->AuthorizationToken;
curl_setopt($this->Property->cURL, CURLOPT_URL, "{$this->Property->APIURL}chat.postMessage");
curl_setopt($this->Property->cURL, CURLOPT_RETURNTRANSFER, true); // Return the transfer as a string of the return value of curl_exec() instead of outputting it directly
//curl_setopt($this->Property->cURL, CURLOPT_HEADER, true); // Include the header in the output
curl_setopt($this->Property->cURL, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($this->Property->cURL, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($this->Property->cURL, CURLOPT_FOLLOWLOCATION, true);
foreach(array_filter(explode(",", str_replace(" ", "", trim($ChannelList)))) as $Channel){
curl_setopt($this->Property->cURL, CURLOPT_POSTFIELDS, [
"token" => $AuthorizationToken,
"channel" => $Channel,
"text" => $Message,
]);
$HTTPContent = curl_exec($this->Property->cURL);
$HTTPResponseCode = (int)curl_getinfo($this->Property->cURL, CURLINFO_HTTP_CODE);
}
$Result = (object)[
"Error" => [],
"Slack" => (object)[],
];
if($HTTPResponseCode == 200){
$Result->Slack->Response = json_decode($HTTPContent);
if($Result->Slack->Response->ok){
}
else{ //! Slack API error
$Result->Error[] = (object)["Code" => -99999, "Message" => "API error", ];
}
}
else{ //! HTTP error
$Result->Error[] = (object)["Code" => -99999, "Message" => "Server unreachable", ];
}
return $Result;
}
}
class HostTest{
const HTTP_STATUS_CODE_INFORMATION = [
0 => ["Name" => "Unknown"],
200 => ["Name" => "Ok"],
301 => ["Name" => "Parmanent redirect"],
302 => ["Name" => "Temporary redirect"],
400 => ["Name" => "Bad request"],
401 => ["Name" => "Unauthorized"],
403 => ["Name" => "Forbidden"],
404 => ["Name" => "Not found"],
405 => ["Name" => "Method not allowed"],
500 => ["Name" => "Server error"],
];
private object $Property;
public function __construct(?string $Host = null, ?string $URL = null, ?int $ConnectionTimeout = 5, ?int $HTTPPort = 80){
$cURL = curl_init();
$this->Property = (object)get_defined_vars();
if(is_null($this->Property->URL))$this->Property->URL = "http://{$this->Property->Host}" . ($HTTPPort == 80 ? null : ":{$this->Property->HTTPPort}") . "";
if(is_null($this->Property->ConnectionTimeout))$this->Property->ConnectionTimeout = 5;
if(is_null($this->Property->HTTPPort))$this->Property->HTTPPort = 80;
$this->Property->cURL = curl_init();
}
public function __destruct(){
curl_close($this->Property->cURL);
}
public function HTTP(?string $URL = null, ?string $AcceptableHTTPStatusCode = "200, 301", ?int $ConnectionTimeout = 5){
if(is_null($URL))$URL = $this->Property->URL;
if(is_null($AcceptableHTTPStatusCode))$AcceptableHTTPStatusCode = "200, 301";
if(is_null($ConnectionTimeout))$ConnectionTimeout = $this->Property->ConnectionTimeout;
curl_setopt($this->Property->cURL, CURLOPT_URL, trim($URL));
curl_setopt($this->Property->cURL, CURLOPT_CONNECTTIMEOUT, $ConnectionTimeout); // Seconds to wait for connection; 0 = Indefinitely
curl_setopt($this->Property->cURL, CURLOPT_TIMEOUT, 30); // Maximum seconds to keep connection
curl_setopt($this->Property->cURL, CURLOPT_RETURNTRANSFER, true); // Return the transfer as a string of the return value of curl_exec() instead of outputting it directly
curl_setopt($this->Property->cURL, CURLOPT_HEADER, true); // Include the header in the output
curl_setopt($this->Property->cURL, CURLOPT_NOBODY, true); // Do not receive body
curl_setopt($this->Property->cURL, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($this->Property->cURL, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($this->Property->cURL, CURLOPT_FOLLOWLOCATION, true); // Follow HTTP redirections
$HTTPHeader = (object)[];
foreach(explode(PHP_EOL, curl_exec($this->Property->cURL)) as $RawHTTPHeader){
$RawHTTPHeader = trim($RawHTTPHeader);
if($RawHTTPHeader){
$HTTPHeaderKVPSeparatorPosition = strpos($RawHTTPHeader, ":");
if($HTTPHeaderKVPSeparatorPosition === false){
$HTTPHeader->{$RawHTTPHeader} = null;
}
else{
$HTTPHeaderName = trim(substr($RawHTTPHeader, 0, $HTTPHeaderKVPSeparatorPosition));
$HTTPHeaderValue = trim(substr($RawHTTPHeader, $HTTPHeaderKVPSeparatorPosition + 1));
$HTTPHeader->{$HTTPHeaderName} = $HTTPHeaderValue;
}
}
}
$HTTPStatusCode = (int)curl_getinfo($this->Property->cURL, CURLINFO_HTTP_CODE);
$HTTPStatusCodeInformation = (object)(self::HTTP_STATUS_CODE_INFORMATION[$HTTPStatusCode] ?? ["Name" => "Unknown", ]);
$HostOnline = in_array($HTTPStatusCode, array_filter(explode(",", str_replace(" ", "", trim($AcceptableHTTPStatusCode)))));
if(false)if(!$HostOnline)var_dump([
"HostOnline" => $HostOnline,
"HTTPStatusCode" => $HTTPStatusCode,
"HTTPStatusCodeInformation" => $HTTPStatusCodeInformation,
"HTTPHeader" => $HTTPHeader,
]);
return (object)[
"Online" => $HostOnline,
"Code" => $HTTPStatusCode,
"CodeInformation" => $HTTPStatusCodeInformation,
"Header" => $HTTPHeader,
];
}
}
function SendMail(
string|array $ToAddress,
string $Subject,
string|array $From = null,
?string $Message = null,
?string $BodyStyle = null,
?string $MainStyle = null,
?array $SMTP = null,
?bool $HTML = true,
){
//return false;
if(!is_array($ToAddress))$ToAddress = [$ToAddress];
if(!is_array($From))$From = [$From, null];
if(is_null($HTML))$HTML = true;
$PHPMailer = new \PHPMailer\PHPMailer\PHPMailer();
$PHPMailer->isHTML($HTML);
$PHPMailer->setFrom($From[0], $From[1]); // $PHPMailer->setFrom('[email protected]', 'HostMeow'); // Set sender of the mail
$PHPMailer->Subject = $Subject;
$PHPMailer->Body = $Message;
foreach($ToAddress as $ThisToAddress)foreach(explode(",", str_replace(" ", "", $ThisToAddress)) as $ThisThisToAddress)$PHPMailer->addAddress($ThisThisToAddress);
//$PHPMailer->addAttachment('url', 'filename'); // Name is optional
if(is_array($SMTP)){
$SMTP["Debug"] = (bool)($SMTP["Debug"] ?? null);
$SMTP["Host"] = $SMTP["Host"] ?? "localhost";
$SMTP["User"] = $SMTP["User"] ?? null;
$SMTP["Password"] = $SMTP["Password"] ?? null;
$SMTP["Security"] = strtolower($SMTP["Security"] ?? null);
$SMTP["Port"] = $SMTP["Port"] ?? (
$SMTP["Security"] == "tls" ? 587 : (
$SMTP["Security"] == "ssl" ? 465 :
25
)
);
if($SMTP["Debug"])$PHPMailer->SMTPDebug = 2; // Enable verbose debug output
$PHPMailer->isSMTP(); // Set mailer to use SMTP
$PHPMailer->Host = $SMTP["Host"];
$PHPMailer->SMTPAuth = (bool)$SMTP["User"];
$PHPMailer->Username = $SMTP["User"];
$PHPMailer->Password = $SMTP["Password"];
$PHPMailer->SMTPSecure = $SMTP["Security"];
$PHPMailer->Port = $SMTP["Port"];
}
//print "PHPMailer: Mail sent to:"; var_dump($ToAddress, $SMTP);
return $PHPMailer->send();
}
?>