-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAssert.php
More file actions
86 lines (74 loc) · 2.15 KB
/
Assert.php
File metadata and controls
86 lines (74 loc) · 2.15 KB
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
<?php
require_once dirname(__FILE__) . "/modules/HttpException/HttpException.php";
class Assert
{
public static function RequestMethod()
{
$request_method = strtoupper($_SERVER['REQUEST_METHOD']);
$methods = func_get_args();
$methods = array_map('strtoupper', $methods);
if (!in_array($request_method, $methods)) {
throw new HttpException(405, 'Unsupported request method: ' . $request_method, array("Allow" => implode(", ", $methods)));
}
}
const REQUEST_METHOD_GET = 'GET';
const REQUEST_METHOD_POST = 'POST';
const REQUEST_METHOD_DELETE = 'DELETE';
const REQUEST_METHOD_PUT = 'PUT';
public static function GetParameters()
{
return self::parameters(func_get_args(), $_GET);
}
public static function PostParameters()
{
return self::parameters(func_get_args(), $_POST);
}
private static function parameters($sets, $arr)
{
$satisfied = true; # init here in case the foreach has 0 iterations
foreach ($sets AS $set)
{
$satisfied = true; # reset on each iteration
if (!is_array($set))
$set = array($set);
foreach ($set as $param)
{
if (empty($arr[$param]))
{
$satisfied = false;
break;
}
}
if ($satisfied)
break;
}
if (!$satisfied)
throw new HttpException(400);
}
public static function ContentType($mime) {
if (!isset($_SERVER['CONTENT_TYPE']) || $_SERVER['CONTENT_TYPE'] != $mime) {
throw new HttpException(415);
}
}
public static function ContentLengthSpecified() {
if (!isset($_SERVER['CONTENT_LENGTH'])) {
throw new HttpException(411);
}
}
public static function HTTPS() {
if (empty($_SERVER['HTTPS']) && $_SERVER['SERVER_ADDR'] != '127.0.0.1') {
throw new HttpException(403, 'Must use HTTPS for authenticated API!');
}
}
public static function credentials($realm = '') {
if (empty($_SERVER['PHP_AUTH_USER']) || empty($_SERVER['PHP_AUTH_PW'])) {
throw new HttpException(401, NULL, array('WWW-Authenticate' => 'Basic realm="' . $realm . '"'));
}
}
public static function dbMinRows($db, $msg = NULL, $code = 404, $count = 1) {
if ((is_a($db, 'mysqli_result') ? $db->num_rows : $db->affected_rows) < $count) {
throw new HttpException($code, $msg);
}
}
}
?>