Completed
Push — preprod ( 503287 )
by Ilia
29:11
created

wire.php ➔ get_wirepostsbycolleagues()   B

Complexity

Conditions 6
Paths 16

Size

Total Lines 29
Code Lines 19

Duplication

Lines 29
Ratio 100 %

Importance

Changes 0
Metric Value
cc 6
eloc 19
nc 16
nop 4
dl 29
loc 29
rs 8.439
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
		"image" => array('type' =>'string', 'required' => false, 'default' => ''),
75
		"lang" => array('type' => 'string', 'required' => false, 'default' => "en")
76
	),
77
	'Posts a new wire post based on user id',
78
	'POST',
79
	true,
80
	false
81
);
82
83
elgg_ws_expose_function(
84
	"reply.wire",
85
	"reply_wire",
86
	array(
87
		"user" => array('type' => 'string', 'required' => true),
88
		"message" => array('type' => 'string', 'required' => true),
89
		"guid" => array('type' => 'int', 'required' => false, 'default' => 0),
90
		"lang" => array('type' => 'string', 'required' => false, 'default' => "en")
91
	),
92
	'Submits a reply to a wire post based on user id and wire post id',
93
	'POST',
94
	true,
95
	false
96
);
97
98
elgg_ws_expose_function(
99
	"edit.wire",
100
	"edit_wire",
101
	array(
102
		"user" => array('type' => 'string', 'required' => true),
103
		"message" => array('type' => 'string', 'required' => true),
104
		"guid" => array('type' => 'int', 'required' => false, 'default' => 0),
105
		"lang" => array('type' => 'string', 'required' => false, 'default' => "en")
106
	),
107
	'Edits a wire post based on user id and wire post id',
108
	'POST',
109
	true,
110
	false
111
);
112
113
function wires_foreach($wire_posts, $user_entity)
114
{
115
	foreach ($wire_posts as $wire_post) {
116
		$wire_post_obj = get_entity($wire_post->guid);
117
		$reshare = $wire_post_obj->getEntitiesFromRelationship(array("relationship" => "reshare", "limit" => 1))[0];
118
		$wire_attachements = elgg_get_entities_from_relationship(array(
119
			'relationship' => 'is_attachment',
120
			'relationship_guid' => $wire_post->guid,
121
			'inverse_relationship' => true,
122
			'limit' => 1
123
		));
124
125
		if ($wire_attachements){
126
			$wire_post->attachment->guid = $wire_attachements[0]->getGUID();
127
			$wire_post->attachment->name = $wire_attachements[0]->original_filename;
128
		}
129
130
		$url = "";
131
		if (!empty($reshare)) {
132
			$url = $reshare->getURL();
133
		}
134
135
		$text = "";
136
		if (!empty($reshare->title)) {
137
			$text = $reshare->title;
138
		} elseif (!empty($reshare->name)) {
139
			$text = $reshare->name;
140
		} elseif (!empty($reshare->description)) {
141
			$text = elgg_get_excerpt($reshare->description, 140);
142
		}
143
144
		$wire_post->shareURL = $url;
145
		$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...
146
147
		$likes = elgg_get_annotations(array(
148
			'guid' => $wire_post->guid,
149
			'annotation_name' => 'likes'
150
		));
151
		$wire_post->likes = count($likes);
152
153
		$liked = elgg_get_annotations(array(
154
			'guid' => $wire_post->guid,
155
			'annotation_owner_guid' => $user_entity->guid,
156
			'annotation_name' => 'likes'
157
		));
158
		$wire_post->liked = count($liked) > 0;
159
160
		$replied = elgg_get_entities_from_metadata(array(
161
			"metadata_name" => "wire_thread",
162
			"metadata_value" => $wire_post->wire_thread,
163
			"type" => "object",
164
			"subtype" => "thewire",
165
			"owner_guid" => $user_entity->guid
166
		));
167
		$wire_post->replied = count($replied) > 0;
168
169
		$wire_post->userDetails = get_user_block($wire_post->owner_guid, $lang);
170
		$wire_post->description = wire_filter($wire_post->description);
171
	}
172
173
	return $wire_posts;
174
}
175
176
function get_wirepost($user, $guid, $thread, $lang)
177
{
178
	$user_entity = is_numeric($user) ? get_user($user) : (strpos($user, '@') !== false ? get_user_by_email($user)[0] : get_user_by_username($user));
179
	if (!$user_entity) {
180
		return "User was not found. Please try a different GUID, username, or email address";
181
	}
182
	if (!$user_entity instanceof ElggUser) {
183
		return "Invalid user. Please try a different GUID, username, or email address";
184
	}
185
186
187
	$entity = get_entity($guid);
188
	if (!$entity) {
189
		return "Wire was not found. Please try a different GUID";
190
	}
191
	if (!$entity instanceof ElggWire) {
192
		return "Invalid wire. Please try a different GUID";
193
	}
194
195
	if (!elgg_is_logged_in()) {
196
		login($user_entity);
197
	}
198
199
	$thread_id = $entity->wire_thread;
200
201
	if ($thread) {
202
		$all_wire_posts = elgg_list_entities_from_metadata(array(
203
			"metadata_name" => "wire_thread",
204
			"metadata_value" => $thread_id,
205
			"type" => "object",
206
			"subtype" => "thewire",
207
			"limit" => 0,
208
			"preload_owners" => true
209
		));
210
		$wire_posts = json_decode($all_wire_posts);
211
212
		$wire_posts = wires_foreach($wire_posts, $user_entity);
213
214 View Code Duplication
	} else {
215
		$wire_posts = elgg_list_entities(array(
216
			"type" => "object",
217
			"subtype" => "thewire",
218
			"guid" => $guid
219
		));
220
221
		$wire_post = json_decode($wire_posts)[0];
222
223
		$wire_posts = wires_foreach($wire_posts, $user_entity);
224
	}
225
226
	return $wire_posts;
227
}
228
229
function get_wireposts($user, $limit, $offset, $filters, $lang)
230
{
231
	$user_entity = is_numeric($user) ? get_user($user) : (strpos($user, '@') !== false ? get_user_by_email($user)[0] : get_user_by_username($user));
232
	if (!$user_entity) {
233
		return "User was not found. Please try a different GUID, username, or email address";
234
	}
235
	if (!$user_entity instanceof ElggUser) {
236
		return "Invalid user. Please try a different GUID, username, or email address";
237
	}
238
239
	if (!elgg_is_logged_in()) {
240
		login($user_entity);
241
	}
242
243
	$filter_data = json_decode($filters);
244
	if (!empty($filter_data)) {
245
		$params = array(
246
			'type' => 'object',
247
			'subtype' => 'thewire',
248
			'limit' => $limit,
249
			'offset' => $offset
250
		);
251
252
		if ($filter_data->mine) {
253
			$all_wire_posts = elgg_list_entities_from_relationship($params);
254
		} else {
255
			$all_wire_posts = elgg_list_entities_from_metadata($params);
256
		}
257
	} else {
258
		$all_wire_posts = elgg_list_entities(array(
259
			'type' => 'object',
260
			'subtype' => 'thewire',
261
			'limit' => $limit,
262
			'offset' => $offset
263
		));
264
	}
265
266
	$wire_posts = json_decode($all_wire_posts);
267
268
	$wire_posts = wires_foreach($wire_posts, $user_entity);
269
270
	return $wire_posts;
271
}
272
273 View Code Duplication
function get_wirepostsbycolleagues($user, $limit, $offset, $lang)
274
{
275
	$user_entity = is_numeric($user) ? get_user($user) : (strpos($user, '@') !== false ? get_user_by_email($user)[0] : get_user_by_username($user));
276
	if (!$user_entity) {
277
		return "User was not found. Please try a different GUID, username, or email address";
278
	}
279
	if (!$user_entity instanceof ElggUser) {
280
		return "Invalid user. Please try a different GUID, username, or email address";
281
	}
282
283
	if (!elgg_is_logged_in()) {
284
		login($user_entity);
285
	}
286
287
	$all_wire_posts = elgg_list_entities_from_relationship(array(
288
		'type' => 'object',
289
		'subtype' => 'thewire',
290
		'relationship' => 'friend',
291
		'relationship_guid' => $user_entity->guid,
292
		'relationship_join_on' => 'container_guid',
293
		'limit' => $limit,
294
		'offset' => $offset
295
	));
296
	$wire_posts = json_decode($all_wire_posts);
297
298
	$wire_posts = wires_foreach($wire_posts, $user_entity);
299
300
	return $wire_posts;
301
}
302
303
function get_wirepostsbyuser($profileemail, $user, $limit, $offset, $lang)
304
{
305
	$user_entity = is_numeric($profileemail) ? get_user($profileemail) : (strpos($profileemail, '@') !== false ? get_user_by_email($profileemail)[0] : get_user_by_username($profileemail));
306
	if (!$user_entity) {
307
		return "User was not found. Please try a different GUID, username, or email address";
308
	}
309
	if (!$user_entity instanceof ElggUser) {
310
		return "Invalid user. Please try a different GUID, username, or email address";
311
	}
312
313
	$viewer = is_numeric($user) ? get_user($user) : (strpos($user, '@') !== false ? get_user_by_email($user)[0] : get_user_by_username($user));
314
	if (!$viewer) {
315
		return "Viewer user was not found. Please try a different GUID, username, or email address";
316
	}
317
	if (!$viewer instanceof ElggUser) {
318
		return "Invalid viewer user. Please try a different GUID, username, or email address";
319
	}
320
321
	if (!elgg_is_logged_in()) {
322
		login($user_entity);
323
	}
324
325
	$all_wire_posts = elgg_list_entities(array(
326
		'type' => 'object',
327
		'subtype' => 'thewire',
328
		'owner_guid' => $user_entity->guid,
329
		'limit' => $limit,
330
		'offset' => $offset
331
	));
332
	$wire_posts = json_decode($all_wire_posts);
333
334
	$wire_posts = wires_foreach($wire_posts, $user_entity);
335
336
	return $wire_posts;
337
}
338
339
function post_wire($user, $message, $image, $lang)
340
{
341
	$user_entity = is_numeric($user) ? get_user($user) : (strpos($user, '@') !== false ? get_user_by_email($user)[0] : get_user_by_username($user));
342
 	if (!$user_entity) {
343
 		return "User was not found. Please try a different GUID, username, or email address";
344
 	}
345
 	if (!$user_entity instanceof ElggUser) {
346
 		return "Invalid user. Please try a different GUID, username, or email address";
347
 	}
348
349
 	if (trim($message) == "") {
350
 		return elgg_echo("thewire:blank");
351
 	}
352
353
 	if (!elgg_is_logged_in()) {
354
 		login($user_entity);
355
 	}
356
357
 	$new_wire = thewire_save_post($message, $user_entity->guid, ACCESS_PUBLIC, 0);
358
 	if (!$new_wire) {
359
 		return elgg_echo("thewire:notsaved");
360
 	}
361
362
	if ($image != "") {
363
		$image_data = base64_decode($image);
364
365
		$file_obj = new TheWireImage();
366
		$file_obj->setFilename('thewire_image/' . rand().".jpg");
367
		$file_obj->setMimeType("image/jpeg");
368
		$file_obj->original_filename = "Image_from_Mobile_API.jpg";
369
		$file_obj->simpletype = file_get_simple_type("image");
370
		$file_obj->access_id = ACCESS_PUBLIC;
371
372
		$file_obj->open("write");
373
		$file_obj->write($image_data);
374
		$file_obj->close();
375
376
		if ($file_obj->save()) {
377
			$file_obj->addRelationship($new_wire, 'is_attachment');
0 ignored issues
show
Bug introduced by
It seems like $new_wire defined by thewire_save_post($messa...guid, ACCESS_PUBLIC, 0) on line 357 can also be of type boolean; however, ElggEntity::addRelationship() does only seem to accept integer, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
378
379
		} else {
380
			return elgg_echo('thewire_image:could_not_save_image');
381
		}
382
	}
383
384
 	return elgg_echo("thewire:posted");
385
}
386
387
function reply_wire($user, $message, $guid, $lang)
388
{
389
	$user_entity = is_numeric($user) ? get_user($user) : (strpos($user, '@') !== false ? get_user_by_email($user)[0] : get_user_by_username($user));
390
	if (!$user_entity) {
391
		return "User was not found. Please try a different GUID, username, or email address";
392
	}
393
	if (!$user_entity instanceof ElggUser) {
394
		return "Invalid user. Please try a different GUID, username, or email address";
395
	}
396
397
	if (trim($message) == "") {
398
		return elgg_echo("thewire:blank");
399
	}
400
401
	if (!elgg_is_logged_in()) {
402
		login($user_entity);
403
	}
404
405
	$new_wire = thewire_save_post($message, $user_entity->guid, ACCESS_PUBLIC, $guid);
406
	if (!$new_wire) {
407
		return elgg_echo("thewire:notsaved");
408
	}
409
410
	return elgg_echo("thewire:posted");
411
}
412
413
function edit_wire($user, $message, $guid, $lang)
414
{
415
	$user_entity = is_numeric($user) ? get_user($user) : (strpos($user, '@') !== false ? get_user_by_email($user)[0] : get_user_by_username($user));
416
	if (!$user_entity) {
417
		return "User was not found. Please try a different GUID, username, or email address";
418
	}
419
	if (!$user_entity instanceof ElggUser) {
420
		return "Invalid user. Please try a different GUID, username, or email address";
421
	}
422
423
	$entity = get_entity($guid);
424
	if (!$entity) {
425
		return "Wire was not found. Please try a different GUID";
426
	}
427
	if (!$entity instanceof ElggWire) {
428
		return "Invalid wire. Please try a different GUID";
429
	}
430
431
	if (trim($message) == "") {
432
		return elgg_echo("thewire:blank");
433
	}
434
435
	if (!elgg_is_logged_in()) {
436
		login($user_entity);
437
	}
438
439
	$message = htmlspecialchars($message, ENT_NOQUOTES, 'UTF-8');
440
441
	$result = elgg_echo("thewire:notsaved");
442
	if ($entity->canEdit()) {
443
		$entity->description = $message;
444
		if ($entity->save()) {
445
			$result = elgg_echo("thewire:posted");
446
		}
447
	}
448
449
	return $result;
450
}
451