Passed
Push — MobileAPI-Wires ( 6b9fc1 )
by
unknown
17:50
created

wire.php ➔ get_wireposts()   C

Complexity

Conditions 8
Paths 32

Size

Total Lines 43
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 8
eloc 28
nc 32
nop 5
dl 0
loc 43
rs 5.3846
c 0
b 0
f 0
1
<?php
2
/*
3
 * Exposes API endpoints for Wire entities
4
 */
5
6
elgg_ws_expose_function(
7
	"get.wirepost",
8
	"get_wirepost",
9
	array(
10
		"user" => array('type' => 'string', 'required' => true),
11
		"guid" => array('type' => 'int', 'required' => true),
12
		"thread" => array('type' => 'int', 'required' => false, 'default' => 0),
13
		"lang" => array('type' => 'string', 'required' => false, 'default' => "en")
14
	),
15
	'Retrieves a wire post & all replies based on user id and wire post id',
16
	'POST',
17
	true,
18
	false
19
);
20
21
elgg_ws_expose_function(
22
	"get.wireposts",
23
	"get_wireposts",
24
	array(
25
		"user" => array('type' => 'string', 'required' => true),
26
		"limit" => array('type' => 'int', 'required' => false, 'default' => 10),
27
		"offset" => array('type' => 'int', 'required' => false, 'default' => 0),
28
		"filters" => array('type' => 'string', 'required' => false, 'default' => ""),
29
		"lang" => array('type' => 'string', 'required' => false, 'default' => "en")
30
	),
31
	'Retrieves a user\'s wire posts based on user id',
32
	'POST',
33
	true,
34
	false
35
);
36
37
elgg_ws_expose_function(
38
	"get.wirepostsbycolleagues",
39
	"get_wirepostsbycolleagues",
40
	array(
41
		"user" => array('type' => 'string', 'required' => true),
42
		"limit" => array('type' => 'int', 'required' => false, 'default' => 10),
43
		"offset" => array('type' => 'int', 'required' => false, 'default' => 0),
44
		"lang" => array('type' => 'string', 'required' => false, 'default' => "en")
45
	),
46
	'Retrieves a user\'s colleague\'s wire posts based on user id',
47
	'POST',
48
	true,
49
	false
50
);
51
52
elgg_ws_expose_function(
53
	"get.wirepostsbyuser",
54
	"get_wirepostsbyuser",
55
	array(
56
		"profileemail" => array('type' => 'string', 'required' => true),
57
		"user" => array('type' => 'string', 'required' => true),
58
		"limit" => array('type' => 'int', 'required' => false, 'default' => 10),
59
		"offset" => array('type' => 'int', 'required' => false, 'default' => 0),
60
		"lang" => array('type' => 'string', 'required' => false, 'default' => "en")
61
	),
62
	'Retrieves a user\'s wire posts based on user id',
63
	'POST',
64
	true,
65
	false
66
);
67
68
elgg_ws_expose_function(
69
	"post.wire",
70
	"post_wire",
71
	array(
72
		"user" => array('type' => 'string', 'required' => true),
73
		"message" => array('type' => 'string', 'required' => true),
74
		"lang" => array('type' => 'string', 'required' => false, 'default' => "en")
75
	),
76
	'Posts a new wire post based on user id',
77
	'POST',
78
	true,
79
	false
80
);
81
82
elgg_ws_expose_function(
83
	"reply.wire",
84
	"reply_wire",
85
	array(
86
		"user" => array('type' => 'string', 'required' => true),
87
		"message" => array('type' => 'string', 'required' => true),
88
		"guid" => array('type' => 'int', 'required' => false, 'default' => 0),
89
		"lang" => array('type' => 'string', 'required' => false, 'default' => "en")
90
	),
91
	'Submits a reply to a wire post based on user id and wire post id',
92
	'POST',
93
	true,
94
	false
95
);
96
97
elgg_ws_expose_function(
98
	"edit.wire",
99
	"edit_wire",
100
	array(
101
		"user" => array('type' => 'string', 'required' => true),
102
		"message" => array('type' => 'string', 'required' => true),
103
		"guid" => array('type' => 'int', 'required' => false, 'default' => 0),
104
		"lang" => array('type' => 'string', 'required' => false, 'default' => "en")
105
	),
106
	'Edits a wire post based on user id and wire post id',
107
	'POST',
108
	true,
109
	false
110
);
111
112
function wires_foreach($wire_posts, $user_entity)
113
{
114
	foreach ($wire_posts as $wire_post) {
115
		$wire_post_obj = get_entity($wire_post->guid);
116
		$reshare = $wire_post_obj->getEntitiesFromRelationship(array("relationship" => "reshare", "limit" => 1))[0];
117
		$wire_attachements = elgg_get_entities_from_relationship(array(
118
			'relationship' => 'is_attachment',
119
			'relationship_guid' => $wire_post->guid,
120
			'inverse_relationship' => true,
121
			'limit' => 1
122
		));
123
124
		if ($wire_attachements){
125
			$wire_post->attachment->guid = $wire_attachements[0]->getGUID();
126
			$wire_post->attachment->name = $wire_attachements[0]->original_filename;
127
		}
128
129
		$url = "";
130
		if (!empty($reshare)) {
131
			$url = $reshare->getURL();
132
		}
133
134
		$text = "";
135
		if (!empty($reshare->title)) {
136
			$text = $reshare->title;
137
		} elseif (!empty($reshare->name)) {
138
			$text = $reshare->name;
139
		} elseif (!empty($reshare->description)) {
140
			$text = elgg_get_excerpt($reshare->description, 140);
141
		}
142
143
		$wire_post->shareURL = $url;
144
		$wire_post->shareText = gc_explode_translation($text, $lang);
0 ignored issues
show
Bug introduced by
The variable $lang does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
145
146
		$likes = elgg_get_annotations(array(
147
			'guid' => $wire_post->guid,
148
			'annotation_name' => 'likes'
149
		));
150
		$wire_post->likes = count($likes);
151
152
		$liked = elgg_get_annotations(array(
153
			'guid' => $wire_post->guid,
154
			'annotation_owner_guid' => $user_entity->guid,
155
			'annotation_name' => 'likes'
156
		));
157
		$wire_post->liked = count($liked) > 0;
158
159
		$replied = elgg_get_entities_from_metadata(array(
160
			"metadata_name" => "wire_thread",
161
			"metadata_value" => $wire_post->wire_thread,
162
			"type" => "object",
163
			"subtype" => "thewire",
164
			"owner_guid" => $user_entity->guid
165
		));
166
		$wire_post->replied = count($replied) > 0;
167
168
		$wire_post->userDetails = get_user_block($wire_post->owner_guid, $lang);
169
		$wire_post->description = wire_filter($wire_post->description);
170
	}
171
172
	return $wire_posts;
173
}
174
175
function get_wirepost($user, $guid, $thread, $lang)
176
{
177
	$user_entity = is_numeric($user) ? get_user($user) : (strpos($user, '@') !== false ? get_user_by_email($user)[0] : get_user_by_username($user));
178
	if (!$user_entity) {
179
		return "User was not found. Please try a different GUID, username, or email address";
180
	}
181
	if (!$user_entity instanceof ElggUser) {
182
		return "Invalid user. Please try a different GUID, username, or email address";
183
	}
184
185
186
	$entity = get_entity($guid);
187
	if (!$entity) {
188
		return "Wire was not found. Please try a different GUID";
189
	}
190
	if (!$entity instanceof ElggWire) {
191
		return "Invalid wire. Please try a different GUID";
192
	}
193
194
	if (!elgg_is_logged_in()) {
195
		login($user_entity);
196
	}
197
198
	$thread_id = $entity->wire_thread;
199
200
	if ($thread) {
201
		$all_wire_posts = elgg_list_entities_from_metadata(array(
202
			"metadata_name" => "wire_thread",
203
			"metadata_value" => $thread_id,
204
			"type" => "object",
205
			"subtype" => "thewire",
206
			"limit" => 0,
207
			"preload_owners" => true
208
		));
209
		$wire_posts = json_decode($all_wire_posts);
210
211
		$wire_posts = wires_foreach($wire_posts, $user_entity);
212
213
	} else {
214
		$wire_posts = elgg_list_entities(array(
215
			"type" => "object",
216
			"subtype" => "thewire",
217
			"guid" => $guid
218
		));
219
220
		$wire_post = json_decode($wire_posts)[0];
221
222
		$wire_posts = wires_foreach($wire_posts, $user_entity);
223
	}
224
225
	return $wire_posts;
226
}
227
228
function get_wireposts($user, $limit, $offset, $filters, $lang)
229
{
230
	$user_entity = is_numeric($user) ? get_user($user) : (strpos($user, '@') !== false ? get_user_by_email($user)[0] : get_user_by_username($user));
231
	if (!$user_entity) {
232
		return "User was not found. Please try a different GUID, username, or email address";
233
	}
234
	if (!$user_entity instanceof ElggUser) {
235
		return "Invalid user. Please try a different GUID, username, or email address";
236
	}
237
238
	if (!elgg_is_logged_in()) {
239
		login($user_entity);
240
	}
241
242
	$filter_data = json_decode($filters);
243
	if (!empty($filter_data)) {
244
		$params = array(
245
			'type' => 'object',
246
			'subtype' => 'thewire',
247
			'limit' => $limit,
248
			'offset' => $offset
249
		);
250
251
		if ($filter_data->mine) {
252
			$all_wire_posts = elgg_list_entities_from_relationship($params);
253
		} else {
254
			$all_wire_posts = elgg_list_entities_from_metadata($params);
255
		}
256
	} else {
257
		$all_wire_posts = elgg_list_entities(array(
258
			'type' => 'object',
259
			'subtype' => 'thewire',
260
			'limit' => $limit,
261
			'offset' => $offset
262
		));
263
	}
264
265
	$wire_posts = json_decode($all_wire_posts);
266
267
	$wire_posts = wires_foreach($wire_posts, $user_entity);
268
269
	return $wire_posts;
270
}
271
272
function get_wirepostsbycolleagues($user, $limit, $offset, $lang)
273
{
274
	$user_entity = is_numeric($user) ? get_user($user) : (strpos($user, '@') !== false ? get_user_by_email($user)[0] : get_user_by_username($user));
275
	if (!$user_entity) {
276
		return "User was not found. Please try a different GUID, username, or email address";
277
	}
278
	if (!$user_entity instanceof ElggUser) {
279
		return "Invalid user. Please try a different GUID, username, or email address";
280
	}
281
282
	if (!elgg_is_logged_in()) {
283
		login($user_entity);
284
	}
285
286
	$all_wire_posts = elgg_list_entities_from_relationship(array(
287
		'type' => 'object',
288
		'subtype' => 'thewire',
289
		'relationship' => 'friend',
290
		'relationship_guid' => $user_entity->guid,
291
		'relationship_join_on' => 'container_guid',
292
		'limit' => $limit,
293
		'offset' => $offset
294
	));
295
	$wire_posts = json_decode($all_wire_posts);
296
297
	$wire_posts = wires_foreach($wire_posts, $user_entity);
298
299
	return $wire_posts;
300
}
301
302
function get_wirepostsbyuser($profileemail, $user, $limit, $offset, $lang)
303
{
304
	$user_entity = is_numeric($profileemail) ? get_user($profileemail) : (strpos($profileemail, '@') !== false ? get_user_by_email($profileemail)[0] : get_user_by_username($profileemail));
305
	if (!$user_entity) {
306
		return "User was not found. Please try a different GUID, username, or email address";
307
	}
308
	if (!$user_entity instanceof ElggUser) {
309
		return "Invalid user. Please try a different GUID, username, or email address";
310
	}
311
312
	$viewer = is_numeric($user) ? get_user($user) : (strpos($user, '@') !== false ? get_user_by_email($user)[0] : get_user_by_username($user));
313
	if (!$viewer) {
314
		return "Viewer user was not found. Please try a different GUID, username, or email address";
315
	}
316
	if (!$viewer instanceof ElggUser) {
317
		return "Invalid viewer user. Please try a different GUID, username, or email address";
318
	}
319
320
	if (!elgg_is_logged_in()) {
321
		login($user_entity);
322
	}
323
324
	$all_wire_posts = elgg_list_entities(array(
325
		'type' => 'object',
326
		'subtype' => 'thewire',
327
		'owner_guid' => $user_entity->guid,
328
		'limit' => $limit,
329
		'offset' => $offset
330
	));
331
	$wire_posts = json_decode($all_wire_posts);
332
333
	$wire_posts = wires_foreach($wire_posts, $user_entity);
334
335
	return $wire_posts;
336
}
337
338 View Code Duplication
function post_wire($user, $message, $lang)
339
{
340
	$user_entity = is_numeric($user) ? get_user($user) : (strpos($user, '@') !== false ? get_user_by_email($user)[0] : get_user_by_username($user));
341
	if (!$user_entity) {
342
		return "User was not found. Please try a different GUID, username, or email address";
343
	}
344
	if (!$user_entity instanceof ElggUser) {
345
		return "Invalid user. Please try a different GUID, username, or email address";
346
	}
347
348
	if (trim($message) == "") {
349
		return elgg_echo("thewire:blank");
350
	}
351
352
	if (!elgg_is_logged_in()) {
353
		login($user_entity);
354
	}
355
356
	$new_wire = thewire_save_post($message, $user_entity->guid, ACCESS_PUBLIC, 0);
357
	if (!$new_wire) {
358
		return elgg_echo("thewire:notsaved");
359
	}
360
361
	return elgg_echo("thewire:posted");
362
}
363
364 View Code Duplication
function reply_wire($user, $message, $guid, $lang)
365
{
366
	$user_entity = is_numeric($user) ? get_user($user) : (strpos($user, '@') !== false ? get_user_by_email($user)[0] : get_user_by_username($user));
367
	if (!$user_entity) {
368
		return "User was not found. Please try a different GUID, username, or email address";
369
	}
370
	if (!$user_entity instanceof ElggUser) {
371
		return "Invalid user. Please try a different GUID, username, or email address";
372
	}
373
374
	if (trim($message) == "") {
375
		return elgg_echo("thewire:blank");
376
	}
377
378
	if (!elgg_is_logged_in()) {
379
		login($user_entity);
380
	}
381
382
	$new_wire = thewire_save_post($message, $user_entity->guid, ACCESS_PUBLIC, $guid);
383
	if (!$new_wire) {
384
		return elgg_echo("thewire:notsaved");
385
	}
386
387
	return elgg_echo("thewire:posted");
388
}
389
390
function edit_wire($user, $message, $guid, $lang)
391
{
392
	$user_entity = is_numeric($user) ? get_user($user) : (strpos($user, '@') !== false ? get_user_by_email($user)[0] : get_user_by_username($user));
393
	if (!$user_entity) {
394
		return "User was not found. Please try a different GUID, username, or email address";
395
	}
396
	if (!$user_entity instanceof ElggUser) {
397
		return "Invalid user. Please try a different GUID, username, or email address";
398
	}
399
400
	$entity = get_entity($guid);
401
	if (!$entity) {
402
		return "Wire was not found. Please try a different GUID";
403
	}
404
	if (!$entity instanceof ElggWire) {
405
		return "Invalid wire. Please try a different GUID";
406
	}
407
408
	if (trim($message) == "") {
409
		return elgg_echo("thewire:blank");
410
	}
411
412
	if (!elgg_is_logged_in()) {
413
		login($user_entity);
414
	}
415
416
	$message = htmlspecialchars($message, ENT_NOQUOTES, 'UTF-8');
417
418
	$result = elgg_echo("thewire:notsaved");
419
	if ($entity->canEdit()) {
420
		$entity->description = $message;
421
		if ($entity->save()) {
422
			$result = elgg_echo("thewire:posted");
423
		}
424
	}
425
426
	return $result;
427
}
428