1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* NRC Recommendation Platform API Library |
5
|
|
|
* Copyright (c) 2017 National Research Council Canada |
6
|
|
|
* |
7
|
|
|
* Author: Luc Belliveau <[email protected]> |
8
|
|
|
* |
9
|
|
|
*/ |
10
|
|
|
header('Content-Type: application/json'); |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Verify that the API request has the appropriate X-Custom-Authorization |
14
|
|
|
* header, and make sure the script has all require privileges to run. |
15
|
|
|
* |
16
|
|
|
* Responds with a 403 if authorization is missing or invalid. |
17
|
|
|
* |
18
|
|
|
*/ |
19
|
|
|
function mm_api_secure() { |
|
|
|
|
20
|
|
|
|
21
|
|
|
if ( |
22
|
|
|
!isset($_SERVER['HTTP_X_CUSTOM_AUTHORIZATION']) |
23
|
|
|
|| (!openssl_public_decrypt(base64_decode($_SERVER['HTTP_X_CUSTOM_AUTHORIZATION']), $decrypted, PUBLIC_KEY)) |
24
|
|
|
|| ($decrypted !== '-- NRC -- LPSS -- GCTools -- Sig -- dsaj9843uj80w7IJHYS&UHSJY(*IOIJHY*') |
25
|
|
|
) { |
26
|
|
|
header('HTTP/1.0 403 Forbidden'); |
27
|
|
|
exit; |
|
|
|
|
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
# Ensure API has full access |
31
|
|
|
session_destroy(); |
32
|
|
|
elgg_set_ignore_access(true); |
33
|
|
|
|
34
|
|
|
# Increase the script timeout value |
35
|
|
|
set_time_limit(14400); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Generator that finds the GUIDs of the entities defined by the search critera. |
40
|
|
|
* The first value yielded is the total number of guids found. |
41
|
|
|
* |
42
|
|
|
* @param str $type Desired entity type. (object, user, export) |
43
|
|
|
* @param str $subtype Desired subtype. (mission) |
44
|
|
|
* @param int $guid GUID of single object, for single entity fetch. |
45
|
|
|
* @param int $since: Fetch entities that have been modified since the specified time |
|
|
|
|
46
|
|
|
* @param int $before: Fetch entities that have been modified before the specified time |
|
|
|
|
47
|
|
|
* @param int $limit: Fetch at most X entities. |
|
|
|
|
48
|
|
|
* @param int $resume: Resume starting at the specified GUID. |
|
|
|
|
49
|
|
|
* @param bool $sort: If true sorts entities based on time created. |
|
|
|
|
50
|
|
|
* @param str $omit: Comma separated list of GUIDs to omit. |
|
|
|
|
51
|
|
|
* |
52
|
|
|
* @return Generator[] Guid iterable |
53
|
|
|
*/ |
54
|
|
|
function mm_api_get_entity_guids($type, $subtype = false, $guid = null, |
55
|
|
|
$since = null, $before = null, $limit = null, $resume = null, $sort = false, |
56
|
|
|
$omit = null) { |
57
|
|
|
$where = ['a.enabled = "yes"']; |
58
|
|
|
if ($type !== 'export') { |
59
|
|
|
$where[] = 'a.type = "' . mysql_escape_string($type) . '"'; |
60
|
|
|
} |
61
|
|
|
if ($subtype !== false) { |
62
|
|
|
$subtype_id = get_data("select id from ".elgg_get_config('dbprefix')."entity_subtypes where subtype = '$subtype'")[0]->id; |
63
|
|
|
$where[] = 'a.subtype = ' . $subtype_id; |
64
|
|
|
} else $subtype_id = 0; |
65
|
|
|
|
66
|
|
|
if (!is_null($guid) && is_numeric($guid)) { |
67
|
|
|
$where[] = 'a.guid = ' . mysql_escape_string(intval($guid)); |
68
|
|
|
} |
69
|
|
|
if (is_numeric($since)) { |
70
|
|
|
$where[] = 'a.time_updated > ' . mysql_escape_string($since); |
71
|
|
|
} |
72
|
|
|
if (is_numeric($before)) { |
73
|
|
|
$where[] = 'a.time_updated < ' . mysql_escape_string($before); |
74
|
|
|
} |
75
|
|
|
if (!is_null($omit)) { |
76
|
|
|
$omitGuids = explode(',', $omit); |
77
|
|
|
if (count($omitGuids) > 0) { |
78
|
|
|
$ogs = []; |
79
|
|
|
foreach ($omitGuids as $og) { |
80
|
|
|
$ogs[] = mysql_escape_string(intval($og)); |
81
|
|
|
} |
82
|
|
|
$where[] = 'a.guid NOT IN ('.implode(',', $ogs).')'; |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
$where_sql = ''; |
87
|
|
|
if (count($where) > 0) { |
88
|
|
|
$where_sql = 'WHERE ' . implode(' AND ', $where); |
89
|
|
|
} |
90
|
|
|
if ($sort) { |
91
|
|
|
$sort_sql = 'ORDER BY a.time_updated ASC'; |
92
|
|
|
} |
93
|
|
|
try { |
94
|
|
|
$sql = ' |
95
|
|
|
SELECT |
96
|
|
|
a.guid |
97
|
|
|
FROM |
98
|
|
|
'.elgg_get_config('dbprefix').'entities a |
99
|
|
|
' . $where_sql . ' |
100
|
|
|
' . $sort_sql; |
|
|
|
|
101
|
|
|
$result = mysql_query($sql, _elgg_services()->db->getLink('read')); |
102
|
|
|
yield mysql_num_rows($result); |
103
|
|
|
$emit = !is_numeric($resume); |
104
|
|
|
$max = (is_numeric($limit) && ($limit > 0)) ? $limit : false; |
105
|
|
|
$count = 0; |
106
|
|
|
while ($row = mysql_fetch_object($result)) { |
107
|
|
|
if ($emit) { |
108
|
|
|
$count += 1; |
109
|
|
|
yield $row->guid; |
110
|
|
|
if (($max !== false) && ($count >= $max)) break; |
111
|
|
|
} |
112
|
|
|
if (!$emit) { |
113
|
|
|
if ($row->guid == $resume) { |
114
|
|
|
$emit = true; |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
} catch (Exception $e) { |
119
|
|
|
// |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Iterate over list of GUIDs and yields the complete object as a row. |
125
|
|
|
* |
126
|
|
|
* @param mixed $entity_guids Array of entity GUIDs |
127
|
|
|
*/ |
128
|
|
|
function mm_api_entity_export($entity_guids) { |
129
|
|
|
function createInvalidEntityObject($guid) { |
130
|
|
|
$ret = new \stdClass; |
131
|
|
|
$ret->guid = $guid; |
132
|
|
|
$ret->__error__ = 'Incomplete Entity Error'; |
133
|
|
|
return $ret; |
134
|
|
|
} |
135
|
|
|
function exportEntity($entity_or_guid) { |
136
|
|
|
// List of fields not to include in any export |
137
|
|
|
$omit = array('password', 'password_hash', 'salt'); |
138
|
|
|
if (is_object($entity_or_guid)) { |
139
|
|
|
$entity = $entity_or_guid; |
140
|
|
|
} else { |
141
|
|
|
$entity = get_entity($entity_or_guid); |
142
|
|
|
if (!is_object($entity)) { |
143
|
|
|
$invalidObj = createInvalidEntityObject($entity_or_guid); |
144
|
|
|
return [$invalidObj, $invalidObj]; |
145
|
|
|
} |
146
|
|
|
} |
147
|
|
|
$ret = new \stdClass; |
148
|
|
|
$exportable_values = $entity->getExportableValues(); |
149
|
|
|
foreach ($exportable_values as $v) { |
150
|
|
|
$ret->$v = $entity->$v; |
151
|
|
|
} |
152
|
|
|
foreach ($entity as $field=>$value) { |
153
|
|
|
if (!in_array($field, $exportable_values) && !in_array($field, $omit)) { |
154
|
|
|
$ret->$field = $value; |
155
|
|
|
} |
156
|
|
|
} |
157
|
|
|
$ret->url = $entity->getURL(); |
158
|
|
|
$ret->subtype_name = $entity->getSubtype(); |
159
|
|
|
return [$entity, $ret]; |
160
|
|
|
} |
161
|
|
|
function dismount($object) { |
162
|
|
|
$reflectionClass = new ReflectionClass(get_class($object)); |
163
|
|
|
$array = array(); |
164
|
|
|
foreach ($reflectionClass->getProperties() as $property) { |
165
|
|
|
$property->setAccessible(true); |
166
|
|
|
$array[$property->getName()] = $property->getValue($object); |
167
|
|
|
$property->setAccessible(false); |
168
|
|
|
} |
169
|
|
|
return $array; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
while ($entity_guids->valid()) { |
173
|
|
|
yield ','; |
174
|
|
|
|
175
|
|
|
$uguid = $entity_guids->current(); |
176
|
|
|
$objectData = exportEntity($uguid); |
177
|
|
|
$object = $objectData[0]; |
178
|
|
|
$data = $objectData[1]; |
179
|
|
|
|
180
|
|
|
$options = array( |
181
|
|
|
'guid' => $object->guid, |
182
|
|
|
'limit' => 0 |
183
|
|
|
); |
184
|
|
|
|
185
|
|
|
$metadata = elgg_get_metadata($options); |
186
|
|
|
$annotations = elgg_get_annotations($options); |
187
|
|
|
|
188
|
|
|
if ($metadata) { |
189
|
|
|
foreach ($metadata as $v) { |
190
|
|
|
$prop = $v->name; |
191
|
|
|
if (!isset($data->$prop)) $data->$prop = $object->$prop; |
192
|
|
|
} |
193
|
|
|
} |
194
|
|
|
if ($annotations) { |
195
|
|
|
foreach ($annotations as $v) { |
196
|
|
|
if (!isset($data->annotations[$v->name])) { |
197
|
|
|
$data->annotations[$v->name] = []; |
198
|
|
|
}; |
199
|
|
|
$data->annotations[$v->name][] = dismount($v); |
200
|
|
|
} |
201
|
|
|
} |
202
|
|
|
yield '{'.substr(json_encode($data), 1, -1) . ',"relationships":['; |
203
|
|
|
|
204
|
|
|
$relstart = false; |
205
|
|
|
$reltable = elgg_get_config('dbprefix') . 'entity_relationships'; |
206
|
|
|
$relationships = mysql_unbuffered_query( |
207
|
|
|
"SELECT * from $reltable where guid_one = {$object->guid}", |
208
|
|
|
_elgg_services()->db->getLink('read') |
209
|
|
|
); |
210
|
|
View Code Duplication |
while ($v = mysql_fetch_object($relationships)) { |
211
|
|
|
yield (($relstart) ? ',' : '') . json_encode(array( |
212
|
|
|
'direction' => 'OUT', |
213
|
|
|
'time_created' => $v->time_created, |
214
|
|
|
'id' => $v->id, |
215
|
|
|
'relationship' => $v->relationship, |
216
|
|
|
'entity' => $v->guid_two, |
217
|
|
|
)); |
218
|
|
|
$relstart = true; |
219
|
|
|
} |
220
|
|
|
mysql_free_result($relationships); |
221
|
|
|
|
222
|
|
|
$relationships2 = mysql_unbuffered_query( |
223
|
|
|
"SELECT * from $reltable where guid_two = {$object->guid}", |
224
|
|
|
_elgg_services()->db->getLink('read') |
225
|
|
|
); |
226
|
|
View Code Duplication |
while ($v = mysql_fetch_object($relationships2)) { |
227
|
|
|
yield (($relstart) ? ',' : '') . json_encode(array( |
228
|
|
|
'direction' => 'IN', |
229
|
|
|
'time_created' => $v->time_created, |
230
|
|
|
'id' => $v->id, |
231
|
|
|
'relationship' => $v->relationship, |
232
|
|
|
'entity' => $v->guid_one, |
233
|
|
|
)); |
234
|
|
|
$relstart = true; |
235
|
|
|
} |
236
|
|
|
mysql_free_result($relationships2); |
237
|
|
|
yield ']}'; |
238
|
|
|
$entity_guids->next(); |
239
|
|
|
} |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
// Public key of server authorized to make requests against this API. |
243
|
|
|
define('PUBLIC_KEY', "-----BEGIN PUBLIC KEY----- |
244
|
|
|
MIICIjANBgkqhkiG9w0BAQEFAAOCAg8AMIICCgKCAgEAmYj9ceaqHi7UmUmhE8e/ |
245
|
|
|
eU/02ZEJeLD8HN7Ku+VN8IB1dIwoSibvoxWZv5bfKnVajkGvud88TMNw3NwqO1jP |
246
|
|
|
b2XiXs/1VvJkqHC/KYkd82iDUOdiDxvXtl8ZxVRA3m4WjtTIB8eJCZitc75fNrzl |
247
|
|
|
fshoP0XQfbNQBBvfP7IBvPIhNuRPgmRMcDdzisqM+c2mAAzQQ04AZ11olhTZzYW0 |
248
|
|
|
HEx6vExkdNBXy/Q0pWas5Zvxe4eTONi7ls14GMKzMeecDnlbQh6P/dCf9ZGF06eM |
249
|
|
|
biMSsnUiYeGsCgtAm9voq0omuVaDY6BDtlsJ50UyMnS5cCIkQrA1Vlt6g8MNt3jh |
250
|
|
|
yXX8L0SxORCBiLGobFnxMSqvuxZkHjp7Jq/k4S3JK2mYxWlJHzcOB8yioI99ErqU |
251
|
|
|
IO+2bqljuNe9v95bh3wu82UjhpU+gmbL5TMqR3mVGGH6mW2WJaRkujQL9hK/efde |
252
|
|
|
V5T4oSM85QajxodYF4nsnhVjmQLzyDxQcVTyj6yQk+cwr68guOMkh389G29Kxgoi |
253
|
|
|
otz1VvR5vYO5/KOFRDkELA8XLEIWtKmwYXTwmwzjX36GdeQpDny3JGJMlBPP7xVd |
254
|
|
|
cBCzK/zh7Ize/pWhN5KSAhJ/a0jByClU0VtMD5d8da6dClWkO6k+Mg9nynSsIAOr |
255
|
|
|
ALJ7RZP/EF2k6WwUtdrGluUCAwEAAQ== |
256
|
|
|
-----END PUBLIC KEY----- |
257
|
|
|
"); |
258
|
|
|
|
259
|
|
|
?> |
|
|
|
|
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: