forked from gausie/osm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlibrary.php
More file actions
283 lines (254 loc) · 6.86 KB
/
library.php
File metadata and controls
283 lines (254 loc) · 6.86 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
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
283
<?php
class OSM {
const CACHE_NONE = 0x00;
const CACHE_NONPERSISTANT = 0x01;
const CACHE_PERSISTANTEXCLUSIVE = 0x02;
const CACHE_PERSISTANT = 0x03;
const BADGETYPE_CHALLENGE = "challenge";
const BADGETYPE_STAGED = "staged";
const BADGETYPE_ACTIVITY = "activity";
/**
* API ID, from Ed
*
* @var int
*/
private $apiid = -1;
/**
* API Token, from Ed
*
* @var string
*/
private $token = 'XXX';
/**
* The user ID, obtained through authorize()
*
* @var int
*/
private $userid = null;
/**
* The user secret, obtained through authorize()
*
* @var string
*/
private $secret = null;
/**
* The absolute URL which all URLs are relative to.
*
* @var string
*/
private $base = 'https://www.onlinescoutmanager.co.uk/';
/**
* Non-persistent cache
*
* @var string[]
*/
private $cache = array();
/**
* Construct object
*
* @param string $apiid API ID, from Ed
* @param string $token API Token, from Ed
*/
public function __construct($apiid, $token) {
$this->apiid = $apiid;
$this->token = $token;
$this->loadAuthorization();
}
/**
* Load authorization which has been saved in the $_SESSION
*
* @return boolean
*/
private function loadAuthorization() {
if (isset($_SESSION['osm_userid'])) {
$this->userid = $_SESSION['osm_userid'];
$this->secret = $_SESSION['osm_secret'];
return true;
}
return false;
}
/**
* Check if the API is currently authorized
*
* @return boolean
*/
public function isAuthorized() {
if ($this->userid === null) {
return false;
}
return true;
}
/**
* Authorize the API with the username and password provided
*
* @param string $email Email address of user to authorize
* @param string $password Password of the user to authorize
*
* @return boolean;
*/
public function authorize($email, $password) {
$parts['password'] = $password;
$parts['email'] = $email;
$json = $this->perform_query('users.php?action=authorise', $parts);
if (!isset($json->secret)) {
return false;
}
$this->secret = $json->secret;
$this->userid = $json->userid;
if (session_id() != "") {
$_SESSION['osm_userid'] = $this->userid;
$_SESSION['osm_secret'] = $this->secret;
}
$this->destroyPersistantCache();
return true;
}
/**
* Perform a query against the API endpoint
*
* @param string $url The URL to query, relative to the base URL
* @param string[] $parts The URL parts, encoded as an associative array
* @param int $cachetype The type of caching to use
*
* @return string[];
*/
private function perform_query($url, $parts=array(), $cachetype=0) {
$parts['token'] = $this->token;
$parts['apiid'] = $this->apiid;
$parts['userid'] = $this->userid;
$parts['secret'] = $this->secret;
$data = http_build_query($parts);
if ($cache = $this->getCache($url, $data, $cachetype)) {
return $cache;
}
$curl_handle = curl_init();
curl_setopt($curl_handle, CURLOPT_URL, $this->base.$url);
curl_setopt($curl_handle, CURLOPT_POSTFIELDS, $data);
curl_setopt($curl_handle, CURLOPT_POST, 1);
curl_setopt($curl_handle, CURLOPT_CONNECTTIMEOUT, 2);
curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, true);
$msg = curl_exec($curl_handle);
if($msg === false){
print "error: " . curl_error($curl_handle);
}
$out = json_decode($msg);
$this->setCache($url, $data, $cachetype, $cachetype, $out);
return $out;
}
/**
* Implement caching using a datahash devised from query
*
* @param string $url URL of data to cache
* @param string $data Params passed to API, after HTTP encoding.
* @param int $type The type of caching to use
*
* @return string
*/
private function getCache($url, $data, $type) {
$datahash = sha1($url."?".$data);
if ($type & self::CACHE_NONPERSISTANT && isset($this->cache[$datahash])) {
return $this->cache[sha1($url."?".$data)];
}
if ($type & self::CACHE_PERSISTANTEXCLUSIVE && isset($_SESSION['osm_cache']) && isset($_SESSION['osm_cache'][$datahash])) {
return $_SESSION['osm_cache'][$datahash];
}
return null;
}
/**
* Cache the value for future queries
*
* @param string $url URL of data to cache
* @param string $data Params passed to API, after HTTP encoding.
* @param int $type The type of caching to use CACHE_*
* @param string[] $return The returned value of the query after JSON decoding
*
* @return null
*/
private function setCache($url, $data, $type, $return) {
$datahash = sha1($url."?".$data);
if ($type & self::CACHE_NONPERSISTANT) {
$this->cache[sha1($url."?".$data)] = $return;
}
if ($type & self::CACHE_PERSISTANTEXCLUSIVE) {
if (!isset($_SESSION['osm_cache'])) {
$_SESSION['osm_cache'] = array();
}
$_SESSION['osm_cache'][$datahash] = $return;
}
}
/**
* Destroys the session cache
*
* @return null
*/
public function destroyPersistantCache() {
if (isset($_SESSION['osm_cache'])) {
$_SESSION['osm_cache'] = array();
}
}
/**
* Get the programme terms
*
* @return Object
*/
public function getTerms() {
return $this->perform_query('api.php?action=getTerms');
}
/**
* Get the current term for a given section
*
* @param string $sectionid The section ID returned by getTerms()
*
* @return Object
*/
function getThisTerm($sectionid) {
$section = $this->getTerms()->{$sectionid};
foreach($section as $term) {
if(strtotime($term->startdate) <= strtotime('now') && strtotime($term->enddate) >= strtotime('now')) {
return $term;
}
}
}
/**
* Get badges available
*
* @param string $type Type, from the BADGETYPE_*
*
* @return Object
*/
public function getBadges($type=null) {
$this->perform_query('challenges.php?action=getBadgeDetails§ion=scouts&badgeType=challenge', $type==null? array(): array("badgeType"=>$type)); //badgeType = challenge/staged/activity
}
/**
* Get events
*
* @param int $sectionid The section ID returned by getTerms()
*
* @return object
*/
public function getEvents($sectionid) {
$events = $this->perform_query('events.php?action=getEvents§ionid='.$sectionid);
return $events;
}
/**
* Get a list of patrols
*
* @param string $sectionid The section ID returned by getTerms()
*
* @return Object
*/
public function getPatrols($sectionid) {
$patrols = $this->perform_query('users.php?action=getPatrols§ionid='. $sectionid);
return $patrols->patrols;
}
/**
* List of records of the kids present in term $termid
*
* @param string $sectionid The section ID returned by getTerms()
* @param string $termid The term ID returned by getTerms()
*
* @return Object
*/
public function getKidsByTermID($sectionid, $termid) {
return $this->perform_query('challenges.php?termid='.$termid.'&type=challenge§ion=scouts&c=community§ionid='. $sectionid, array());
}
}