-
Notifications
You must be signed in to change notification settings - Fork 106
/
api.php
68 lines (58 loc) · 1.82 KB
/
api.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
<?php
namespace Controller;
abstract class Api extends \Controller
{
protected $_userId;
public function __construct()
{
$f3 = \Base::instance();
$f3->set("ONERROR", function (\Base $f3) {
if (!headers_sent()) {
header("Content-type: application/json");
}
$out = array(
"status" => $f3->get("ERROR.code"),
"error" => $f3->get("ERROR.text")
);
if ($f3->get("DEBUG") >= 2) {
$out["trace"] = strip_tags($f3->get("ERROR.trace"));
}
echo json_encode($out);
});
$this->_userId = $this->_requireAuth();
}
/**
* Require an API key. Sends an HTTP 401 if one is not supplied.
* @return int|bool
*/
protected function _requireAuth()
{
$f3 = \Base::instance();
$user = new \Model\User();
// Use the logged in user if there is one
if ($f3->get("user.api_key")) {
$key = $f3->get("user.api_key");
} else {
$key = false;
}
// Check all supported key methods
if (!empty($_GET["key"])) {
$key = $_GET["key"];
} elseif ($f3->get("HEADERS.X-Redmine-API-Key")) {
$key = $f3->get("HEADERS.X-Redmine-API-Key");
} elseif ($f3->get("HEADERS.X-API-Key")) {
$key = $f3->get("HEADERS.X-API-Key");
} elseif ($f3->get("HEADERS.X-Api-Key")) {
$key = $f3->get("HEADERS.X-Api-Key");
}
$user->load(array("api_key = ?", $key));
if ($key && $user->id && $user->api_key) {
$f3->set("user", $user->cast());
$f3->set("user_obj", $user);
return $user->id;
} else {
$f3->error(401);
return false;
}
}
}