-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathRes.php
70 lines (67 loc) · 1.93 KB
/
Res.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
<?php
namespace core;
use core\Env;
use core\Helper;
use core\Render;
/**
* Response helpers.
*/
class Res
{
/**
* Proplery set HTTP-header code.
* https://dev.twitter.com/overview/api/response-codes
*/
public static function error($http = 400)
{
$v = Env::protocol();
if ($http === 400) {
header("$v 400 Bad request");
} elseif ($http === 500) {
header("$v 500 Internal Server Error");
} elseif ($http === 401) {
header("$v 401 Unauthorized");
} elseif ($http === 403) {
header("$v 403 Banned");
} elseif ($http === 404) {
header("$v 404 Page Not Found");
} elseif ($http === 503) {
header("$v 503 Too Many Requests");
} else {
user_error(sprintf('TODO: HTTP-statusCode(%d) not implemented (or not numeric)', $http));
}
}
public static function json($msg)
{
header("Content-Type: application/json");
echo json_encode($msg);
}
/** HTTP Redirect */
public static function redirect($relative)
{
$base = "https://" . Env::host();
$allowed = Helper::config("general")["baseurl"];
if (is_array($allowed)) {
// Check if whitelisted for multi-domain site
if (! in_array($base, $allowed)) {
user_error("CRIT: Invalid domain given by user=$base");
}
} else {
// Just force as 1 domain
$base = $allowed;
}
header(sprintf("Location: %s/%s", $base, $relative), true, 303);
}
/** HTTP redirect to external domain */
public static function redirect_external($url, $allowHTTP=false)
{
$parts = parse_url($url);
if ($parts === false || !isset($parts["scheme"])) {
user_error("Mailformed URL: $url");
}
if (!$allowHTTP && $parts["scheme"] !== "https") {
user_error("Redirect without https: $url");
}
header(sprintf("Location: %s", $url), true, 303);
}
}