-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.php
executable file
·106 lines (84 loc) · 3.26 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
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
<?php
require_once('./rest.php');
require_once('./functions.php');
class MobileAPI extends RestService {
public function getSession($params) {
return $this->encodeOutput(get_userinfo());
}
public function postSession($params) {
$userinfo = login($params['username'], $params['password']);
if ($userinfo === false)
return $this->unauthorized();
$this->status(201); // 201 Created
return $this->encodeOutput($userinfo);
}
public function deleteSession($params) {
return $this->encodeOutput(logout());
}
public function getForum($params) {
$id = isset($params['id']) ? intval($params['id']) : -1;
if (!isset($params['mode'])) $params['mode'] = 'all';
$foruminfo = fetch_forum($id);
if ($id != -1) {
if (!$foruminfo)
return $this->notFound();
if (!can_view_forum($foruminfo))
return $this->notAllowed();
}
if ($params['mode'] != 'all')
$foruminfo = array();
if (in_array($params['mode'], array('subforums', 'all')))
$foruminfo['subforums'] = fetch_subforum_list($id);
if (in_array($params['mode'], array('threads', 'all'))) {
$perpage = isset($params['perpage']) ? intval($params['perpage']) : 10;
$page = isset($params['page']) ? intval($params['page']) : 1;
$foruminfo['threads'] = fetch_threads($id, $perpage, $page);
}
return $this->encodeOutput($foruminfo);
}
public function getThread($params) {
$id = isset($params['id']) ? intval($params['id']) : -1;
if (!isset($params['mode'])) $params['mode'] = 'all';
$threadinfo = fetch_thread($id);
if (!$threadinfo)
return $this->notFound();
if (!can_view_thread($threadinfo))
return $this->notAllowed();
if ($params['mode'] != 'all')
$threadinfo = array();
if (in_array($params['mode'], array('posts', 'all'))) {
$perpage = isset($params['perpage']) ? intval($params['perpage']) : 10;
$page = isset($params['page']) ? intval($params['page']) : 1;
$threadinfo['posts'] = fetch_posts($id, $perpage, $page);
}
return $this->encodeOutput($threadinfo);
}
public function postThreadReply($params) {
return $this->notImplemented();
}
public function postThread($params) {
return $this->notImplemented();
}
private function encodeOutput($data) {
shutdown();
$this->contentType('json');
return json_encode($data);
// Something like this:
//if ($this->request->accepts('json')) {
// return json_encode($data);
//} else {
// return xml_encode($data);
//}
}
}
$dispatcher = new RestDispatcher(new MobileAPI());
$dispatcher->addRoute('GET', '/session', 'getSession');
$dispatcher->addRoute('POST', '/session', 'postSession');
$dispatcher->addRoute('DELETE', '/session', 'deleteSession');
$dispatcher->addRoute('GET', '/forum', 'getForum');
$dispatcher->addRoute('GET', '/forum/:id', 'getForum');
$dispatcher->addRoute('GET', '/thread/:id', 'getThread');
$dispatcher->addRoute('POST', '/thread/:id/reply', 'postThreadReply');
$dispatcher->addRoute('POST', '/thread', 'postThread');
$dispatcher->dispatch();
?>