-
-
Notifications
You must be signed in to change notification settings - Fork 18
/
utility.php
282 lines (226 loc) · 6.51 KB
/
utility.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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
<?php
/*
* A set of utility helper functions
*/
use Miiverse\Config;
use Miiverse\CurrentSession;
use Miiverse\FileSystem;
use Miiverse\Hashid;
use Miiverse\Helpers\ConsoleAuth;
use Miiverse\Net;
use Miiverse\Router;
use Miiverse\Template;
use Miiverse\Translation;
// Sort of alias for Config::get
function config($value)
{
$split = explode('.', $value);
$key = array_pop($split);
$section = implode('.', $split);
return Config::get($section, $key) ?? Config::get($value) ?? null;
}
// Alias for Router::route
function route($name, $args = null, $full = false) : string
{
return ($full ? full_domain() : '').Router::route($name, $args);
}
// Getting the full domain (+protocol) of the current host, only works for http
function full_domain() : string
{
return 'http'.($_SERVER['HTTPS'] ?? false ? 's' : '').'://'.$_SERVER['HTTP_HOST'];
}
// Checking if a parameter is equal to session_id()
function session_check($param = 'session') : bool
{
return isset($_REQUEST[$param]) && $_REQUEST[$param] === session_id();
}
// Alias for Template::vars and Template::render
function view($name, $vars = []) : string
{
Template::vars($vars);
return Template::render($name);
}
// Get a path
function path($path)
{
return FileSystem::getPath($path);
}
// Convert camel case to snake case
function camel_to_snake($text) : string
{
return ltrim(strtolower(preg_replace('#[A-Z]#', '_$0', $text)), '_');
}
function clean_string($string, $lower = false, $noSpecial = false, $replaceSpecial = '', $underscores = false)
{
// Run common sanitisation function over string
$string = trim($string);
$string = htmlentities($string, ENT_NOQUOTES | ENT_HTML401, 'utf-8');
$string = stripslashes($string);
$string = strip_tags($string);
// If set convert spaces to underscores
if ($underscores) {
$string = str_replace(' ', '_', $string);
}
// If set also make the string lowercase
if ($lower) {
$string = strtolower($string);
}
// If set remove all characters that aren't a-z or 0-9
if ($noSpecial) {
$string = preg_replace('/[^a-z0-9]/', $replaceSpecial, $string);
}
// Return clean string
return $string;
}
// Redirect with turbolinks header
function redirect($url)
{
header("Location: {$url}");
exit;
}
function check_mx_record($email)
{
// Get the domain from the e-mail address
$domain = substr(strstr($email, '@'), 1);
// Check the MX record
$record = checkdnsrr($domain, 'MX');
// Return the record data
return $record;
}
function get_country_code()
{
// Attempt to get country code using PHP's built in geo thing
if (function_exists('geoip_country_code_by_name')) {
try {
$code = geoip_country_code_by_name(Net::ip());
// Check if $code is anything
if ($code) {
return $code;
}
} catch (\Exception $e) {
}
}
// Check if the required header is set and return it
if (isset($_SERVER['HTTP_CF_IPCOUNTRY']) && strlen($_SERVER['HTTP_CF_IPCOUNTRY']) === 2) {
return $_SERVER['HTTP_CF_IPCOUNTRY'];
}
// Return XX as a fallback
return 'XX';
}
function get_country_name($code)
{
switch (strtolower($code)) {
case 'xx':
return 'Unknown';
case 'a1':
return 'Anonymous Proxy';
case 'a2':
return 'Satellite Provider';
default:
return locale_get_display_region("-{$code}", 'en');
}
}
// Count the amount of unique characters in the password string and calculate the entropy
function password_entropy($password)
{
return count(count_chars(utf8_decode($password), 1)) * log(256, 2);
}
function byte_symbol($bytes)
{
// Return nothing if the input was 0
if (!$bytes) {
return '0 B';
}
// Array with byte symbols
$symbols = ['B', 'KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB', 'ZiB', 'YiB'];
// Calculate byte entity
$exp = floor(log($bytes) / log(1024));
// Format the things
$bytes = sprintf('%.2f '.$symbols[$exp], ($bytes / pow(1024, floor($exp))));
// Return the formatted string
return $bytes;
}
// turn this function into a wrapped class!
function send_mail($to, $subject, $body)
{
$transport = Swift_SmtpTransport::newInstance()
->setHost(config('mail.smtp.server'))
->setPort(config('mail.smtp.port'));
if (config('mail.smtp.secure')) {
$transport->setEncryption(config('mail.smtp.secure'));
}
if (config('mail.smtp.auth')) {
$transport->setUsername(config('mail.smtp.username'))
->setPassword(config('mail.smtp.password'));
}
$mailer = Swift_Mailer::newInstance($transport);
$message = Swift_Message::newInstance($subject)
->setFrom([config('mail.smtp.from') => config('mail.smtp.name')])
->setBcc($to)
->setBody($body);
return $mailer->send($message);
}
function rel2abs($rel, $base) : string
{
// return if already absolute URL
if (parse_url($rel, PHP_URL_SCHEME) != '') {
return $rel;
}
// queries and anchors
if ($rel[0] == '#' || $rel[0] == '?') {
return $base.$rel;
}
// parse base URL and convert to local variables:
// $scheme, $host, $path
extract(parse_url($base));
// remove non-directory element from path
$path = preg_replace('#/[^/]*$#', '', $path);
// destroy path if relative url points to root
if ($rel[0] == '/') {
$path = '';
}
// dirty absolute URL
$abs = "$host$path/$rel";
// replace '//' or '/./' or '/foo/../' with '/'
$re = ['#(/\.?/)#', '#/(?!\.\.)[^/]+/\.\./#'];
for ($n = 1; $n > 0; $abs = preg_replace($re, '/', $abs, -1, $n)) {
}
// absolute URL is ready!
return $scheme.'://'.$abs;
}
function checkMaintenance()
{
if (config('general.maintenance')) {
http_response_code(503);
return view('errors/503');
}
}
function auth3DS()
{
ConsoleAuth::check3DS();
}
function authWiiU()
{
ConsoleAuth::checkWiiU();
}
function checkConsoleAuth()
{
CurrentSession::authByConsole(ConsoleAuth::$consoleId);
Template::vars([
'_console' => ConsoleAuth::$paramPack,
'user' => CurrentSession::$user,
'language' => Translation::getIsoLanguage(CurrentSession::$user->language)
]);
}
function hashid($items)
{
return Hashid::encode($items);
}
function dehashid($hash)
{
return Hashid::decode($hash);
}
function __(string $key, array $replace = [])
{
return Translation::get($key, $replace);
}