1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* NRC Recommendation Platform API Library |
4
|
|
|
* Copyright (c) 2017 National Research Council Canada |
5
|
|
|
* |
6
|
|
|
* Author: Luc Belliveau <[email protected]> |
7
|
|
|
* |
8
|
|
|
*/ |
9
|
|
|
header('Content-Type: application/json'); |
10
|
|
|
|
11
|
|
|
global $subtypes; |
12
|
|
|
|
13
|
|
|
class FakeGUIDEntity { |
14
|
|
|
public $guid; |
15
|
|
|
public function __construct($guid) { |
16
|
|
|
$this->guid = $guid; |
17
|
|
|
} |
18
|
|
|
} |
19
|
|
|
|
20
|
|
|
class FakeEntity extends \ElggEntity { |
21
|
|
|
private $entity; |
22
|
|
|
public function __construct($entity) { |
23
|
|
|
$this->entity = $entity; |
24
|
|
|
} |
25
|
|
|
public function __get($name) { |
26
|
|
|
return $this->entity->$name; |
27
|
|
|
} |
28
|
|
|
public function getType() { |
29
|
|
|
return $this->entity->type; |
30
|
|
|
} |
31
|
|
|
public function getSubtype() { |
32
|
|
|
global $subtypes; |
33
|
|
|
return $subtypes["i_{$this->entity->subtype}"]; |
34
|
|
|
} |
35
|
|
|
public function getOwnerEntity() { |
36
|
|
|
if ($this->entity->owner_guid > 0) { |
37
|
|
|
return new FakeGUIDEntity($this->entity->owner_guid); |
|
|
|
|
38
|
|
|
} else return false; |
|
|
|
|
39
|
|
|
} |
40
|
|
|
public function getDisplayName() { |
41
|
|
|
return $this->entity->name; |
42
|
|
|
} |
43
|
|
|
public function getContainerEntity() { |
44
|
|
|
if ($this->entity->container_guid > 0) { |
45
|
|
|
return new FakeGUIDEntity($this->entity->container_guid); |
|
|
|
|
46
|
|
|
} else return false; |
|
|
|
|
47
|
|
|
} |
48
|
|
|
public function setDisplayName($displayName) {} |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Verify that the API request has the appropriate X-Custom-Authorization |
53
|
|
|
* header, and make sure the script has all require privileges to run. |
54
|
|
|
* |
55
|
|
|
* Responds with a 403 if authorization is missing or invalid. |
56
|
|
|
* |
57
|
|
|
*/ |
58
|
|
|
function mm_api_secure() { |
59
|
|
|
|
60
|
|
|
if ( |
61
|
|
|
!isset($_SERVER['HTTP_X_CUSTOM_AUTHORIZATION']) |
62
|
|
|
|| (!openssl_public_decrypt(base64_decode($_SERVER['HTTP_X_CUSTOM_AUTHORIZATION']), $decrypted, PUBLIC_KEY)) |
63
|
|
|
|| ($decrypted !== '-- NRC -- LPSS -- GCTools -- Sig -- dsaj9843uj80w7IJHYS&UHSJY(*IOIJHY*') |
64
|
|
|
) { |
65
|
|
|
header('HTTP/1.0 403 Forbidden'); |
66
|
|
|
exit; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
# Ensure API has full access |
70
|
|
|
session_destroy(); |
71
|
|
|
elgg_set_ignore_access(true); |
72
|
|
|
|
73
|
|
|
# Increase the script timeout value |
74
|
|
|
set_time_limit(14400); |
75
|
|
|
} |
76
|
|
|
global $CONFIG; |
77
|
|
|
function getURL($entity) { |
78
|
|
|
global $CONFIG; |
79
|
|
|
global $subtypes; |
80
|
|
|
$type = $entity->type; |
81
|
|
|
$subtype = $subtypes["i_{$entity->subtype}"]; |
82
|
|
|
|
83
|
|
|
$url = ''; |
84
|
|
|
|
85
|
|
|
if (isset($CONFIG->entity_url_handler[$type][$subtype])) { |
86
|
|
|
$function = $CONFIG->entity_url_handler[$type][$subtype]; |
87
|
|
|
if (is_callable($function)) { |
88
|
|
|
$url = call_user_func($function, $entity); |
89
|
|
|
} |
90
|
|
|
} elseif (isset($CONFIG->entity_url_handler[$type]['all'])) { |
91
|
|
|
$function = $CONFIG->entity_url_handler[$type]['all']; |
92
|
|
|
if (is_callable($function)) { |
93
|
|
|
$url = call_user_func($function, $entity); |
94
|
|
|
} |
95
|
|
View Code Duplication |
} elseif (isset($CONFIG->entity_url_handler['all']['all'])) { |
96
|
|
|
$function = $CONFIG->entity_url_handler['all']['all']; |
97
|
|
|
if (is_callable($function)) { |
98
|
|
|
$url = call_user_func($function, $entity); |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
if ($url) { |
103
|
|
|
$url = elgg_normalize_url($url); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
$params = array('entity' => new FakeEntity($entity)); |
107
|
|
|
$url = _elgg_services()->hooks->trigger('entity:url', $type, $params, $url); |
108
|
|
|
|
109
|
|
|
return elgg_normalize_url($url); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Stream the requested entities as efficently as possible using JSON. |
115
|
|
|
* |
116
|
|
|
* @param str $type Desired entity type. (object, user, export) |
117
|
|
|
* @param str $subtype Desired subtype. (mission) |
118
|
|
|
* @param int $guid GUID of single object, for single entity fetch. |
119
|
|
|
* @param int $since: Fetch entities that have been modified since the specified time |
|
|
|
|
120
|
|
|
* @param int $before: Fetch entities that have been modified before the specified time |
|
|
|
|
121
|
|
|
* @param int $limit: Fetch at most X entities. |
|
|
|
|
122
|
|
|
* @param int $resume: Resume starting at the specified GUID. |
|
|
|
|
123
|
|
|
* @param bool $sort: If true sorts entities based on time created. |
|
|
|
|
124
|
|
|
* @param str $omit: Comma separated list of GUIDs to omit. |
|
|
|
|
125
|
|
|
* |
126
|
|
|
* @return Generator[] JSON formatted text stream |
127
|
|
|
*/ |
128
|
|
|
function mm_api_export_entities($type, $subtype = false, $guid = null, |
129
|
|
|
$since = null, $before = null, $limit = null, $resume = null, $sort = false, |
130
|
|
|
$omit = null, $countRows = false) { |
131
|
|
|
_elgg_services()->db->establishLink('api_exporter'); |
132
|
|
|
$dbprefix = elgg_get_config('dbprefix'); |
133
|
|
|
$dblink = _elgg_services()->db->getLink('read'); |
134
|
|
|
function dismount($object) { |
135
|
|
|
$reflectionClass = new ReflectionClass(get_class($object)); |
136
|
|
|
$array = array(); |
137
|
|
|
foreach ($reflectionClass->getProperties() as $property) { |
138
|
|
|
$property->setAccessible(true); |
139
|
|
|
$array[$property->getName()] = $property->getValue($object); |
140
|
|
|
$property->setAccessible(false); |
141
|
|
|
} |
142
|
|
|
return $array; |
143
|
|
|
} |
144
|
|
|
global $in_array; |
145
|
|
|
$in_array = false; |
146
|
|
|
function outVal($val, $start_with_comma=true) { |
147
|
|
|
global $in_array; |
148
|
|
|
$value = ($val->meta_type === 'integer') ? |
149
|
|
|
json_encode(intval($val->meta_value)) : json_encode($val->meta_value); |
150
|
|
|
if (($in_array !== false) && ($in_array == $val->meta_name)) { |
151
|
|
|
return (($start_with_comma) ? ',' : '').$value; |
152
|
|
|
} |
153
|
|
|
$ret = ''; |
154
|
|
|
if (($in_array !== false) && (($in_array != $val->meta_name) || ($val->arr == 0))) { |
155
|
|
|
$ret .= ']'; |
156
|
|
|
$in_array = false; |
157
|
|
|
} |
158
|
|
|
if ($start_with_comma) $ret .= ','; |
159
|
|
|
$ret .= '"' . $val->meta_name . '":'; |
160
|
|
|
if ($val->arr != 0) { |
161
|
|
|
$ret .= '['; |
162
|
|
|
$in_array = $val->meta_name; |
163
|
|
|
} |
164
|
|
|
$ret .= $value; |
165
|
|
|
return $ret; |
166
|
|
|
} |
167
|
|
|
function finalizeEntity($uguid) { |
168
|
|
|
global $in_array; |
169
|
|
|
$dbprefix = elgg_get_config('dbprefix'); |
170
|
|
|
$dblink = _elgg_services()->db->getLink('read'); |
171
|
|
|
|
172
|
|
|
if ($uguid > 0) { |
173
|
|
|
if ($in_array) { |
174
|
|
|
yield ']'; |
175
|
|
|
$in_array = false; |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
$options = array('guid' => $uguid, 'limit' => 0); |
179
|
|
|
$annotations = elgg_get_annotations($options); |
180
|
|
|
|
181
|
|
|
if ($annotations) { |
182
|
|
|
$data = [ 'annotations' => []]; |
183
|
|
|
foreach ($annotations as $v) { |
184
|
|
|
if (!isset($data['annotations'][$v->name])) { |
185
|
|
|
$data['annotations'][$v->name] = []; |
186
|
|
|
}; |
187
|
|
|
$data['annotations'][$v->name][] = dismount($v); |
188
|
|
|
} |
189
|
|
|
yield ','.substr(json_encode($data), 1, -1); |
190
|
|
|
} |
191
|
|
|
yield ',"relationships":['; |
192
|
|
|
|
193
|
|
|
$relstart = false; |
194
|
|
|
$reltable = "{$dbprefix}entity_relationships"; |
195
|
|
|
$relationships = mysql_unbuffered_query( |
196
|
|
|
"SELECT * from $reltable where guid_one = " . |
197
|
|
|
mysql_escape_string($uguid) . |
198
|
|
|
' OR guid_two = ' . mysql_escape_string($uguid), |
199
|
|
|
$dblink |
200
|
|
|
); |
201
|
|
|
while ($v = mysql_fetch_object($relationships)) { |
202
|
|
|
yield (($relstart) ? ',' : '') . json_encode(array( |
203
|
|
|
'direction' => ($v->guid_one == $uguid) ? 'OUT' : 'IN', |
204
|
|
|
'time_created' => $v->time_created, |
205
|
|
|
'id' => $v->id, |
206
|
|
|
'relationship' => $v->relationship, |
207
|
|
|
'entity' => ($v->guid_one == $uguid) ? $v->guid_two : $v->guid_one, |
208
|
|
|
)); |
209
|
|
|
$relstart = true; |
210
|
|
|
} |
211
|
|
|
mysql_free_result($relationships); |
212
|
|
|
yield ']}'; |
213
|
|
|
} |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
// Get all subtypes |
217
|
|
|
global $subtypes; |
218
|
|
|
$subtypes = []; |
219
|
|
|
$subtype_results = mysql_unbuffered_query( |
220
|
|
|
"select id, subtype from {$dbprefix}entity_subtypes", |
221
|
|
|
$dblink |
222
|
|
|
); |
223
|
|
|
while ($row = mysql_fetch_object($subtype_results)) { |
224
|
|
|
$subtypes["i_{$row->id}"] = $row->subtype; |
225
|
|
|
$subtypes["s_{$row->subtype}"] = $row->id; |
226
|
|
|
} |
227
|
|
|
mysql_free_result($subtype_results); |
228
|
|
|
|
229
|
|
|
$where = ['a.enabled = "yes"']; |
230
|
|
|
if ($type !== 'export') { |
231
|
|
|
$where[] = 'a.type = "' . mysql_escape_string($type) . '"'; |
232
|
|
|
} |
233
|
|
|
if ($subtype !== false) { |
234
|
|
|
$where[] = 'a.subtype = ' . (($subtypes["s_$subtype"]) ? $subtypes["s_$subtype"] : -1); |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
if (!is_null($guid) && is_numeric($guid)) { |
238
|
|
|
$where[] = 'a.guid = ' . mysql_escape_string(intval($guid)); |
239
|
|
|
} |
240
|
|
|
if (is_numeric($since)) { |
241
|
|
|
$where[] = 'a.time_updated > ' . mysql_escape_string($since); |
242
|
|
|
} |
243
|
|
|
if (is_numeric($before)) { |
244
|
|
|
$where[] = 'a.time_updated < ' . mysql_escape_string($before); |
245
|
|
|
} |
246
|
|
|
if (!is_null($omit)) { |
247
|
|
|
$omitGuids = explode(',', $omit); |
248
|
|
|
if (count($omitGuids) > 0) { |
249
|
|
|
$ogs = []; |
250
|
|
|
foreach ($omitGuids as $og) { |
251
|
|
|
$ogs[] = mysql_escape_string(intval($og)); |
252
|
|
|
} |
253
|
|
|
$where[] = 'a.guid NOT IN ('.implode(',', $ogs).')'; |
254
|
|
|
} |
255
|
|
|
} |
256
|
|
|
|
257
|
|
|
$where_sql = ''; |
258
|
|
|
if (count($where) > 0) { |
259
|
|
|
$where_sql = 'WHERE ' . implode(' AND ', $where); |
260
|
|
|
} |
261
|
|
|
if ($sort) { |
262
|
|
|
$sort_sql = 'ORDER BY a.time_updated ASC'; |
263
|
|
|
} |
264
|
|
|
try { |
265
|
|
|
$sql = " |
266
|
|
|
SELECT |
267
|
|
|
a.guid, |
268
|
|
|
a.type, |
269
|
|
|
a.subtype, |
270
|
|
|
a.owner_guid, |
271
|
|
|
a.site_guid, |
272
|
|
|
a.container_guid, |
273
|
|
|
a.time_created, |
274
|
|
|
a.time_updated, |
275
|
|
|
a.access_id, |
276
|
|
|
a.enabled, |
277
|
|
|
a.last_action, |
278
|
|
|
o.title, |
279
|
|
|
o.description, |
280
|
|
|
u.name, |
281
|
|
|
u.username, |
282
|
|
|
u.admin, |
283
|
|
|
u.banned, |
284
|
|
|
u.language, |
285
|
|
|
u.last_action AS user_last_action, |
286
|
|
|
u.prev_last_action, |
287
|
|
|
u.last_login, |
288
|
|
|
u.prev_last_login, |
289
|
|
|
g.name as group_name, |
290
|
|
|
g.description as group_description, |
291
|
|
|
( |
292
|
|
|
SELECT |
293
|
|
|
COUNT(name_id) |
294
|
|
|
FROM |
295
|
|
|
elggmetadata |
296
|
|
|
WHERE |
297
|
|
|
entity_guid = a.guid |
298
|
|
|
AND name_id = b.name_id |
299
|
|
|
) > 1 as arr, |
300
|
|
|
b.value_type as meta_type, |
301
|
|
|
c.string as meta_name, |
302
|
|
|
d.string as meta_value |
303
|
|
|
FROM |
304
|
|
|
{$dbprefix}entities a |
305
|
|
|
LEFT JOIN {$dbprefix}objects_entity o on o.guid = a.guid |
306
|
|
|
LEFT JOIN {$dbprefix}users_entity u ON u.guid = a.guid |
307
|
|
|
LEFT JOIN {$dbprefix}groups_entity g ON g.guid = a.guid |
308
|
|
|
LEFT JOIN {$dbprefix}metadata b ON b.entity_guid = a.guid |
309
|
|
|
LEFT JOIN {$dbprefix}metastrings c ON c.id = b.name_id |
310
|
|
|
LEFT JOIN {$dbprefix}metastrings d ON d.id = b.value_id |
311
|
|
|
$where_sql |
312
|
|
|
$sort_sql"; |
|
|
|
|
313
|
|
|
|
314
|
|
|
if ($countRows) { |
315
|
|
|
yield get_data( |
316
|
|
|
"select count(guid) c from {$dbprefix}entities a $where_sql" |
317
|
|
|
)[0]->c; |
318
|
|
|
} else yield 0; |
319
|
|
|
|
320
|
|
|
$entity_data = mysql_unbuffered_query( |
321
|
|
|
$sql, |
322
|
|
|
_elgg_services()->db->getLink('api_exporter') |
323
|
|
|
); |
324
|
|
|
$emit = !is_numeric($resume); |
325
|
|
|
$max = (is_numeric($limit) && ($limit > 0)) ? $limit : false; |
326
|
|
|
$count = 0; |
327
|
|
|
$currentGuid = -1; |
328
|
|
|
$uguid = -1; |
329
|
|
|
$euguid = -1; |
330
|
|
|
while ($row = mysql_fetch_object($entity_data)) { |
331
|
|
|
if ($emit) { |
332
|
|
|
if ($currentGuid != $row->guid) { |
333
|
|
|
$fin = finalizeEntity($currentGuid); |
334
|
|
|
foreach ($fin as $fs) yield $fs; |
335
|
|
|
$currentGuid = -1; |
336
|
|
|
$count += 1; |
337
|
|
|
if (($max !== false) && ($count > $max)) break; |
338
|
|
|
yield ','; |
339
|
|
|
yield '{'; |
340
|
|
|
$currentGuid = $row->guid; |
341
|
|
|
$euguid = mysql_escape_string(intval($row->guid)); |
342
|
|
|
$uguid = $row->guid; |
343
|
|
|
|
344
|
|
|
yield '"guid":' . $row->guid . ',' . |
345
|
|
|
'"type":"' . $row->type . '",' . |
346
|
|
|
'"subtype":' . $row->subtype . ',' . |
347
|
|
|
'"subtype_name":' . json_encode($subtypes["i_{$row->subtype}"]) . ',' . |
348
|
|
|
'"time_created":' . $row->time_created . ',' . |
349
|
|
|
'"url":' . json_encode(getURL($row)) . ',' . |
350
|
|
|
'"access_id":' . $row->access_id . ',' . |
351
|
|
|
'"time_updated":' . $row->time_updated . ',' . |
352
|
|
|
'"owner_guid":' . $row->owner_guid . ',' . |
353
|
|
|
'"container_guid":' . $row->container_guid . ',' . |
354
|
|
|
'"enabled":"' . $row->enabled . '",' . |
355
|
|
|
'"site_guid":' . $row->site_guid; |
356
|
|
|
if (!is_null($row->title)) { |
357
|
|
|
yield ',"title":' . json_encode($row->title) . ',' . |
358
|
|
|
'"description":' . json_encode($row->description); |
359
|
|
|
} |
360
|
|
|
if (!is_null($row->group_name)) { |
361
|
|
|
yield ',"name":' . json_encode($row->group_name) . ',' . |
362
|
|
|
'"description":' . json_encode($row->group_description); |
363
|
|
|
} |
364
|
|
|
if (!is_null($row->name)) { |
365
|
|
|
yield ',"name":' . json_encode($row->name) . ',' . |
366
|
|
|
'"username":' . json_encode($row->username) . ',' . |
367
|
|
|
'"language":"' . $row->language . '",' . |
368
|
|
|
'"admin":"' . $row->admin . '",' . |
369
|
|
|
'"banned":"' . $row->banned . '",' . |
370
|
|
|
'"last_action":' . $row->user_last_action . ',' . |
371
|
|
|
'"prev_last_action":' . $row->prev_last_action . ',' . |
372
|
|
|
'"last_login":' . $row->last_login . ',' . |
373
|
|
|
'"prev_last_login":' . $row->prev_last_login; |
374
|
|
|
} else { |
375
|
|
|
yield ',"last_action":' . $row->last_action; |
376
|
|
|
} |
377
|
|
|
} |
378
|
|
|
if (!is_null($row->meta_name)) yield outVal($row); |
379
|
|
|
} |
380
|
|
|
if (!$emit) { |
381
|
|
|
if ($row->guid == $resume) { |
382
|
|
|
$emit = true; |
383
|
|
|
} |
384
|
|
|
} |
385
|
|
|
} |
386
|
|
|
if ($currentGuid > 0) { |
387
|
|
|
$fin = finalizeEntity($currentGuid); |
388
|
|
|
foreach ($fin as $fs) yield $fs; |
389
|
|
|
} |
390
|
|
|
} catch (Exception $e) { |
391
|
|
|
yield ',"error": ' . json_encode($e); |
392
|
|
|
} |
393
|
|
|
} |
394
|
|
|
|
395
|
|
|
// Public key of server authorized to make requests against this API. |
396
|
|
|
define('PUBLIC_KEY', "-----BEGIN PUBLIC KEY----- |
397
|
|
|
MIICIjANBgkqhkiG9w0BAQEFAAOCAg8AMIICCgKCAgEAmYj9ceaqHi7UmUmhE8e/ |
398
|
|
|
eU/02ZEJeLD8HN7Ku+VN8IB1dIwoSibvoxWZv5bfKnVajkGvud88TMNw3NwqO1jP |
399
|
|
|
b2XiXs/1VvJkqHC/KYkd82iDUOdiDxvXtl8ZxVRA3m4WjtTIB8eJCZitc75fNrzl |
400
|
|
|
fshoP0XQfbNQBBvfP7IBvPIhNuRPgmRMcDdzisqM+c2mAAzQQ04AZ11olhTZzYW0 |
401
|
|
|
HEx6vExkdNBXy/Q0pWas5Zvxe4eTONi7ls14GMKzMeecDnlbQh6P/dCf9ZGF06eM |
402
|
|
|
biMSsnUiYeGsCgtAm9voq0omuVaDY6BDtlsJ50UyMnS5cCIkQrA1Vlt6g8MNt3jh |
403
|
|
|
yXX8L0SxORCBiLGobFnxMSqvuxZkHjp7Jq/k4S3JK2mYxWlJHzcOB8yioI99ErqU |
404
|
|
|
IO+2bqljuNe9v95bh3wu82UjhpU+gmbL5TMqR3mVGGH6mW2WJaRkujQL9hK/efde |
405
|
|
|
V5T4oSM85QajxodYF4nsnhVjmQLzyDxQcVTyj6yQk+cwr68guOMkh389G29Kxgoi |
406
|
|
|
otz1VvR5vYO5/KOFRDkELA8XLEIWtKmwYXTwmwzjX36GdeQpDny3JGJMlBPP7xVd |
407
|
|
|
cBCzK/zh7Ize/pWhN5KSAhJ/a0jByClU0VtMD5d8da6dClWkO6k+Mg9nynSsIAOr |
408
|
|
|
ALJ7RZP/EF2k6WwUtdrGluUCAwEAAQ== |
409
|
|
|
-----END PUBLIC KEY----- |
410
|
|
|
"); |
411
|
|
|
|
412
|
|
|
?> |
|
|
|
|
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_function
expects aPost
object, and outputs the author of the post. The base classPost
returns a simple string and outputting a simple string will work just fine. However, the child classBlogPost
which is a sub-type ofPost
instead decided to return anobject
, and is therefore violating the SOLID principles. If aBlogPost
were passed tomy_function
, PHP would not complain, but ultimately fail when executing thestrtoupper
call in its body.