Issues (2473)

Branch: master

Security Analysis    no vulnerabilities found

This project does not seem to handle request data directly as such no vulnerable execution paths were found.

  Cross-Site Scripting
Cross-Site Scripting enables an attacker to inject code into the response of a web-request that is viewed by other users. It can for example be used to bypass access controls, or even to take over other users' accounts.
  File Exposure
File Exposure allows an attacker to gain access to local files that he should not be able to access. These files can for example include database credentials, or other configuration files.
  File Manipulation
File Manipulation enables an attacker to write custom data to files. This potentially leads to injection of arbitrary code on the server.
  Object Injection
Object Injection enables an attacker to inject an object into PHP code, and can lead to arbitrary code execution, file exposure, or file manipulation attacks.
  Code Injection
Code Injection enables an attacker to execute arbitrary code on the server.
  Response Splitting
Response Splitting can be used to send arbitrary responses.
  File Inclusion
File Inclusion enables an attacker to inject custom files into PHP's file loading mechanism, either explicitly passed to include, or for example via PHP's auto-loading mechanism.
  Command Injection
Command Injection enables an attacker to inject a shell command that is execute with the privileges of the web-server. This can be used to expose sensitive data, or gain access of your server.
  SQL Injection
SQL Injection enables an attacker to execute arbitrary SQL code on your database server gaining access to user data, or manipulating user data.
  XPath Injection
XPath Injection enables an attacker to modify the parts of XML document that are read. If that XML document is for example used for authentication, this can lead to further vulnerabilities similar to SQL Injection.
  LDAP Injection
LDAP Injection enables an attacker to inject LDAP statements potentially granting permission to run unauthorized queries, or modify content inside the LDAP tree.
  Header Injection
  Other Vulnerability
This category comprises other attack vectors such as manipulating the PHP runtime, loading custom extensions, freezing the runtime, or similar.
  Regex Injection
Regex Injection enables an attacker to execute arbitrary code in your PHP process.
  XML Injection
XML Injection enables an attacker to read files on your local filesystem including configuration files, or can be abused to freeze your web-server process.
  Variable Injection
Variable Injection enables an attacker to overwrite program variables with custom data, and can lead to further vulnerabilities.
Unfortunately, the security analysis is currently not available for your project. If you are a non-commercial open-source project, please contact support to gain access.

mod/messages/start.php (5 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
/**
3
* Elgg internal messages plugin
4
* This plugin lets users send messages to each other.
5
*
6
* @package ElggMessages
7
*/
8
9
10
elgg_register_event_handler('init', 'system', 'messages_init');
11
12
function messages_init() {
13
14
	// register a library of helper functions
15
	elgg_register_library('elgg:messages', elgg_get_plugins_path() . 'messages/lib/messages.php');
16
17
	// add page menu items
18
	if (elgg_is_logged_in()) {
19
		elgg_register_menu_item('page', array(
20
			'name' => 'messages:inbox',
21
			'text' => elgg_echo('messages:inbox'),
22
			'href' => "messages/inbox/" . elgg_get_logged_in_user_entity()->username,
23
			'context' => 'messages',
24
		));
25
26
		elgg_register_menu_item('page', array(
27
			'name' => 'messages:notifications',
28
			'text' => elgg_echo('messages:notifications'),
29
			'href' => "messages/notifications/" . elgg_get_logged_in_user_entity()->username,
30
			'context' => 'messages',
31
		));
32
33
		elgg_register_menu_item('page', array(
34
			'name' => 'messages:sentmessages',
35
			'text' => elgg_echo('messages:sentmessages'),
36
			'href' => "messages/sent/" . elgg_get_logged_in_user_entity()->username,
37
			'context' => 'messages',
38
		));
39
	}
40
41
	elgg_register_event_handler('pagesetup', 'system', 'messages_notifier');
42
43
	// Extend system CSS with our own styles, which are defined in the messages/css view
44
	elgg_extend_view('css/elgg', 'messages/css');
45
	elgg_extend_view('js/elgg', 'messages/js');
46
47
	// Register a page handler, so we can have nice URLs
48
	elgg_register_page_handler('messages', 'messages_page_handler');
49
50
	// Register a URL handler
51
	elgg_register_plugin_hook_handler('entity:url', 'object', 'messages_set_url');
52
53
	// Extend avatar hover menu
54
	elgg_register_plugin_hook_handler('register', 'menu:user_hover', 'messages_user_hover_menu');
55
56
	// delete messages sent by a user when user is deleted
57
	elgg_register_event_handler('delete', 'user', 'messages_purge');
58
59
	// ecml
60
	elgg_register_plugin_hook_handler('get_views', 'ecml', 'messages_ecml_views_hook');
61
62
	// permission overrides
63
	elgg_register_plugin_hook_handler('permissions_check:metadata', 'object', 'messages_can_edit_metadata');
64
	elgg_register_plugin_hook_handler('permissions_check', 'object', 'messages_can_edit');
65
	elgg_register_plugin_hook_handler('container_permissions_check', 'object', 'messages_can_edit_container');
66
67
	// Register actions
68
	$action_path = elgg_get_plugins_path() . 'messages/actions/messages';
69
	elgg_register_action("messages/send", "$action_path/send.php");
70
	elgg_register_action("messages/delete", "$action_path/delete.php");
71
	elgg_register_action("messages/process", "$action_path/process.php");
72
}
73
74
/**
75
 * Messages page handler
76
 *
77
 * @param array $page Array of URL components for routing
78
 * @return bool
79
 */
80 View Code Duplication
function messages_page_handler($page) {
81
82
	$current_user = elgg_get_logged_in_user_entity();
83
	if (!$current_user) {
84
		register_error(elgg_echo('noaccess'));
85
		elgg_get_session()->set('last_forward_from', current_page_url());
86
		forward('');
87
	}
88
89
	elgg_load_library('elgg:messages');
90
91
	elgg_push_breadcrumb(elgg_echo('messages'), 'messages/inbox/' . $current_user->username);
92
93
	if (!isset($page[0])) {
94
		$page[0] = 'inbox';
95
	}
96
97
	// Support the old inbox url /messages/<username>, but only if it matches the logged in user.
98
	// Otherwise having a username like "read" on the system could confuse this function.
99
	if ($current_user->username === $page[0]) {
100
		$page[1] = $page[0];
101
		$page[0] = 'inbox';
102
	}
103
104
	if (!isset($page[1])) {
105
		$page[1] = $current_user->username;
106
	}
107
108
	$base_dir = elgg_get_plugins_path() . 'messages/pages/messages';
109
110
	switch ($page[0]) {
111
		case 'inbox':
112
			set_input('username', $page[1]);
113
			include("$base_dir/inbox.php");
114
			break;
115
		case 'notifications':
116
			set_input('username', $page[1]);
117
			include("$base_dir/notifications.php");
118
			break;
119
		case 'sent':
120
			set_input('username', $page[1]);
121
			include("$base_dir/sent.php");
122
			break;
123
		case 'read':
124
			set_input('guid', $page[1]);
125
			include("$base_dir/read.php");
126
			break;
127
		case 'compose':
128
		case 'add':
129
			include("$base_dir/send.php");
130
			break;
131
		default:
132
			return false;
133
	}
134
	return true;
135
}
136
137
/**
138
 * Display notification of new messages in topbar
139
 */
140
function messages_notifier() {
141
	if (elgg_is_logged_in()) {
142
		$text = elgg_view_icon("mail");
143
		$tooltip = elgg_echo("messages");
144
145
		// get unread messages
146
		$num_messages = (int)messages_count_unread();
147
		if ($num_messages != 0) {
148
			$text .= "<span class=\"messages-new\">$num_messages<div class='hidden wb-invisible'>". elgg_echo("messages:unreadmessages" ) ."</div></span>";
149
			$tooltip .= " (" . elgg_echo("messages:unreadcount", array($num_messages)) . ")";
150
		}
151
152
		elgg_register_menu_item('topbar', array(
153
			'name' => 'messages',
154
			'href' => 'messages/inbox/' . elgg_get_logged_in_user_entity()->username,
155
			'text' => $text,
156
			'priority' => 600,
157
			'title' => $tooltip,
158
		));
159
	}
160
}
161
162
/**
163
 * Override the canEditMetadata function to return true for messages
164
 *
165
 */
166 View Code Duplication
function messages_can_edit_metadata($hook_name, $entity_type, $return_value, $parameters) {
167
168
	global $messagesendflag;
169
170
	if ($messagesendflag == 1) {
171
		$entity = $parameters['entity'];
172
		if ($entity->getSubtype() == "messages") {
173
			return true;
174
		}
175
	}
176
177
	return $return_value;
178
}
179
180
/**
181
 * Override the canEdit function to return true for messages within a particular context.
182
 *
183
 */
184 View Code Duplication
function messages_can_edit($hook_name, $entity_type, $return_value, $parameters) {
185
186
	global $messagesendflag;
187
188
	if ($messagesendflag == 1) {
189
		$entity = $parameters['entity'];
190
		if ($entity->getSubtype() == "messages") {
191
			return true;
192
		}
193
	}
194
195
	return $return_value;
196
}
197
198
/**
199
 * Prevent messages from generating a notification
200
 */
201
function messages_notification_msg($hook_name, $entity_type, $return_value, $params) {
202
203
	if ($params['entity'] instanceof ElggEntity) {
204
		if ($params['entity']->getSubtype() == 'messages') {
205
			return false;
206
		}
207
	}
208
}
209
210
/**
211
 * Override the canEdit function to return true for messages within a particular context.
212
 *
213
 */
214
function messages_can_edit_container($hook_name, $entity_type, $return_value, $parameters) {
215
216
	global $messagesendflag;
217
218
	if ($messagesendflag == 1) {
219
		return true;
220
	}
221
222
	return $return_value;
223
}
224
225
/**
226
 * Send an internal message
227
 *
228
 * @param string $subject           The subject line of the message
229
 * @param string $body              The body of the mesage
230
 * @param int    $recipient_guid    The GUID of the user to send to
231
 * @param int    $sender_guid       Optionally, the GUID of the user to send from
232
 * @param int    $original_msg_guid The GUID of the message to reply from (default: none)
233
 * @param bool   $notify            Send a notification (default: true)
234
 * @param bool   $add_to_sent       If true (default), will add a message to the sender's 'sent' tray
235
 * @return bool
236
 */
237
function messages_send($subject, $body, $recipient_guid, $sender_guid = 0, $original_msg_guid = 0, $notify = true, $add_to_sent = true) {
238
239
	// @todo remove globals
240
	global $messagesendflag;
241
	$messagesendflag = 1;
242
243
	// @todo remove globals
244
	global $messages_pm;
245
	if ($notify) {
246
		$messages_pm = 1;
247
	} else {
248
		$messages_pm = 0;
249
	}
250
251
	// If $sender_guid == 0, set to current user
252
	if ($sender_guid == 0) {
253
		$sender_guid = (int) elgg_get_logged_in_user_guid();
254
	}
255
256
	// cyu - fatal error (inserts line breaks, upon saving into database)
257
	$body = utf8_encode($body);
258
259
	// Initialise 2 new ElggObject
260
	$message_to = new ElggObject();
261
	$message_sent = new ElggObject();
262
263
	$message_to->subtype = "messages";
264
	$message_sent->subtype = "messages";
265
266
	$message_to->owner_guid = $recipient_guid;
267
	$message_to->container_guid = $recipient_guid;
268
	$message_sent->owner_guid = $sender_guid;
269
	$message_sent->container_guid = $sender_guid;
270
271
	$message_to->access_id = ACCESS_PUBLIC;
272
	$message_sent->access_id = ACCESS_PUBLIC;
273
274
	$message_to->title = $subject;
275
	$message_to->description = $body;
276
277
	$message_sent->title = $subject;
278
	$message_sent->description = $body;
279
280
	$message_to->toId = $recipient_guid; // the user receiving the message
281
	$message_to->fromId = $sender_guid; // the user receiving the message
282
	$message_to->readYet = 0; // this is a toggle between 0 / 1 (1 = read)
283
	$message_to->hiddenFrom = 0; // this is used when a user deletes a message in their sentbox, it is a flag
284
	$message_to->hiddenTo = 0; // this is used when a user deletes a message in their inbox
285
286
	$message_sent->toId = $recipient_guid; // the user receiving the message
287
	$message_sent->fromId = $sender_guid; // the user receiving the message
288
	$message_sent->readYet = 0; // this is a toggle between 0 / 1 (1 = read)
289
	$message_sent->hiddenFrom = 0; // this is used when a user deletes a message in their sentbox, it is a flag
290
	$message_sent->hiddenTo = 0; // this is used when a user deletes a message in their inbox
291
292
	$message_to->msg = 1;
293
	$message_sent->msg = 1;
294
295
	// Save the copy of the message that goes to the recipient
296
	$success = $message_to->save();
297
298
	// Save the copy of the message that goes to the sender
299
	if ($add_to_sent) {
300
		$message_sent->save();
301
	}
302
303
	$message_to->access_id = ACCESS_PRIVATE;
304
	$message_to->save();
305
306
	if ($add_to_sent) {
307
		$message_sent->access_id = ACCESS_PRIVATE;
308
		$message_sent->save();
309
	}
310
311
	// if the new message is a reply then create a relationship link between the new message
312
	// and the message it is in reply to
313
	if ($original_msg_guid && $success) {
314
		add_entity_relationship($message_sent->guid, "reply", $original_msg_guid);
315
	}
316
317
	//$message_contents = strip_tags($body);
318
	if (($recipient_guid != elgg_get_logged_in_user_guid()) && $notify) {
319
		$recipient = get_user($recipient_guid);
320
		$sender = get_user($sender_guid);
321
322
		$subject = elgg_echo('messages:email:subject', array(), $recipient->language);
323
		$body = elgg_echo('messages:email:body', array(
324
				$sender->name,
325
				$message_contents,
0 ignored issues
show
The variable $message_contents 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...
326
				elgg_get_site_url() . "messages/inbox/" . $recipient->username,
327
				$sender->name,
328
				elgg_get_site_url() . "messages/compose?send_to=" . $sender_guid
329
			),
330
			$recipient->language
331
		);
332
333
		notify_user($recipient_guid, $sender_guid, $subject, $body);
334
	}
335
336
	$messagesendflag = 0;
337
	return $success;
338
}
339
340
/**
341
 * Message URL override
342
 *
343
 * @param string $hook
344
 * @param string $type
345
 * @param string $url
346
 * @param array  $params
347
 * @return string
348
 */
349
function messages_set_url($hook, $type, $url, $params) {
350
	$entity = $params['entity'];
351
	if (elgg_instanceof($entity, 'object', 'messages')) {
352
		return 'messages/read/' . $entity->getGUID();
353
	}
354
}
355
356
function count_unread_messages() {
357
	elgg_deprecated_notice('Your theme is using count_unread_messages which has been deprecated for messages_count_unread()', 1.8);
358
	return messages_count_unread();
359
}
360
361
/**
362
 * Returns the unread messages in a user's inbox
363
 *
364
 * @param int  $user_guid GUID of user whose inbox we're counting (0 for logged in user)
365
 * @param int  $limit     Number of unread messages to return (default from settings)
366
 * @param int  $offset    Start at a defined offset (for listings)
367
 * @param bool $count     Switch between entities array or count mode
368
 *
369
 * @return array, int (if $count = true)
0 ignored issues
show
The doc-type array, could not be parsed: Expected "|" or "end of type", but got "," at position 5. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
370
 * @since 1.9
371
 */
372
function messages_get_unread($user_guid = 0, $limit = null, $offset = 0, $count = false) {
373
	if (!$user_guid) {
374
		$user_guid = elgg_get_logged_in_user_guid();
375
	}
376
	$db_prefix = elgg_get_config('dbprefix');
377
378
	// denormalize the md to speed things up.
379
	// seriously, 10 joins if you don't.
380
	$strings = array('toId', $user_guid, 'readYet', 0, 'msg', 1);
381
	$map = array();
382
	foreach ($strings as $string) {
383
		$id = elgg_get_metastring_id($string);
384
		$map[$string] = $id;
385
	}
386
387
	if ($limit === null) {
388
		$limit = elgg_get_config('default_limit');
389
	}
390
391
	$options = array(
392
		// original options before denormalizing
393
		// 'metadata_name_value_pairs' => array(
394
		// 'toId' => elgg_get_logged_in_user_guid(),
395
		// 'readYet' => 0,
396
		// 'msg' => 1
397
		// ),
398
		'joins' => array(
399
			"JOIN {$db_prefix}metadata msg_toId on e.guid = msg_toId.entity_guid",
400
			"JOIN {$db_prefix}metadata msg_readYet on e.guid = msg_readYet.entity_guid",
401
			"JOIN {$db_prefix}metadata msg_msg on e.guid = msg_msg.entity_guid",
402
		),
403
		'wheres' => array(
404
			"msg_toId.name_id='{$map['toId']}' AND msg_toId.value_id='{$map[$user_guid]}'",
405
			"msg_readYet.name_id='{$map['readYet']}' AND msg_readYet.value_id='{$map[0]}'",
406
			"msg_msg.name_id='{$map['msg']}' AND msg_msg.value_id='{$map[1]}'",
407
		),
408
		'owner_guid' => $user_guid,
409
		'limit' => $limit,
410
		'offset' => $offset,
411
		'count' => $count,
412
		'distinct' => false,
413
	);
414
415
	return elgg_get_entities_from_metadata($options);
416
}
417
418
419
/**
420
 * Returns the unread messages from other users in a user's inbox
421
 *
422
 * @param int  $user_guid GUID of user whose inbox we're counting (0 for logged in user)
423
 * @param int  $limit     Number of unread messages to return (default from settings)
424
 * @param int  $offset    Start at a defined offset (for listings)
425
 * @param bool $count     Switch between entities array or count mode
426
 *
427
 * @return array, int (if $count = true)
0 ignored issues
show
The doc-type array, could not be parsed: Expected "|" or "end of type", but got "," at position 5. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
428
 * @since 1.9
429
 */
430 View Code Duplication
function messages_get_unread_inbox($user_guid = 0, $limit = null, $offset = 0, $count = false) {
431
	if (!$user_guid) {
432
		$user_guid = elgg_get_logged_in_user_guid();
433
	}
434
	$db_prefix = elgg_get_config('dbprefix');
435
436
	// denormalize the md to speed things up.
437
	// seriously, 10 joins if you don't.
438
	$strings = array('toId', $user_guid, 'readYet', 0, 'msg', 1, 'fromId');
439
	$map = array();
440
	foreach ($strings as $string) {
441
		$id = elgg_get_metastring_id($string);
442
		$map[$string] = $id;
443
	}
444
445
	if ($limit === null) {
446
		$limit = elgg_get_config('default_limit');
447
	}
448
449
	$options = array(
450
		// original options before denormalizing
451
		// 'metadata_name_value_pairs' => array(
452
		// 'toId' => elgg_get_logged_in_user_guid(),
453
		// 'readYet' => 0,
454
		// 'msg' => 1
455
		// ),
456
		'joins' => array(
457
			"JOIN {$db_prefix}metadata msg_toId on e.guid = msg_toId.entity_guid",
458
			"JOIN {$db_prefix}metadata msg_readYet on e.guid = msg_readYet.entity_guid",
459
			"JOIN {$db_prefix}metadata msg_msg on e.guid = msg_msg.entity_guid",
460
			"LEFT JOIN {$db_prefix}metadata msg_fromId on e.guid = msg_fromId.entity_guid",
461
			"LEFT JOIN {$db_prefix}metastrings msvfrom ON msg_fromId.value_id = msvfrom.id",
462
			"LEFT JOIN {$db_prefix}entities efrom ON msvfrom.string = efrom.guid",
463
		),
464
		'wheres' => array(
465
			"msg_toId.name_id='{$map['toId']}' AND msg_toId.value_id='{$map[$user_guid]}'",
466
			"msg_fromId.name_id='{$map['fromId']}' AND efrom.type = 'user'",
467
			"msg_readYet.name_id='{$map['readYet']}' AND msg_readYet.value_id='{$map[0]}'",
468
			"msg_msg.name_id='{$map['msg']}' AND msg_msg.value_id='{$map[1]}'",
469
		),
470
		'owner_guid' => $user_guid,
471
		'limit' => $limit,
472
		'offset' => $offset,
473
		'count' => $count,
474
		'distinct' => false,
475
	);
476
477
	return elgg_get_entities_from_metadata($options);
478
}
479
480
481
/**
482
 * Returns the unread notification messages in a user's inbox
483
 *
484
 * @param int  $user_guid GUID of user whose inbox we're counting (0 for logged in user)
485
 * @param int  $limit     Number of unread messages to return (default from settings)
486
 * @param int  $offset    Start at a defined offset (for listings)
487
 * @param bool $count     Switch between entities array or count mode
488
 *
489
 * @return array, int (if $count = true)
0 ignored issues
show
The doc-type array, could not be parsed: Expected "|" or "end of type", but got "," at position 5. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
490
 * @since 1.9
491
 */
492 View Code Duplication
function messages_get_unread_notifications($user_guid = 0, $limit = null, $offset = 0, $count = false) {
493
	if (!$user_guid) {
494
		$user_guid = elgg_get_logged_in_user_guid();
495
	}
496
	$db_prefix = elgg_get_config('dbprefix');
497
498
	// denormalize the md to speed things up.
499
	// seriously, 10 joins if you don't.
500
	$strings = array('toId', $user_guid, 'readYet', 0, 'msg', 1, 'fromId');
501
	$map = array();
502
	foreach ($strings as $string) {
503
		$id = elgg_get_metastring_id($string);
504
		$map[$string] = $id;
505
	}
506
507
	if ($limit === null) {
508
		$limit = elgg_get_config('default_limit');
509
	}
510
511
	$options = array(
512
		// original options before denormalizing
513
		// 'metadata_name_value_pairs' => array(
514
		// 'toId' => elgg_get_logged_in_user_guid(),
515
		// 'readYet' => 0,
516
		// 'msg' => 1
517
		// ),
518
		'joins' => array(
519
			"JOIN {$db_prefix}metadata msg_toId on e.guid = msg_toId.entity_guid",
520
			"JOIN {$db_prefix}metadata msg_readYet on e.guid = msg_readYet.entity_guid",
521
			"JOIN {$db_prefix}metadata msg_msg on e.guid = msg_msg.entity_guid",
522
			"LEFT JOIN {$db_prefix}metadata msg_fromId on e.guid = msg_fromId.entity_guid",
523
			"LEFT JOIN {$db_prefix}metastrings msvfrom ON msg_fromId.value_id = msvfrom.id",
524
			"LEFT JOIN {$db_prefix}entities efrom ON msvfrom.string = efrom.guid",
525
		),
526
		'wheres' => array(
527
			"msg_toId.name_id='{$map['toId']}' AND msg_toId.value_id='{$map[$user_guid]}'",
528
			"msg_fromId.name_id='{$map['fromId']}' AND efrom.type <> 'user'",
529
			"msg_readYet.name_id='{$map['readYet']}' AND msg_readYet.value_id='{$map[0]}'",
530
			"msg_msg.name_id='{$map['msg']}' AND msg_msg.value_id='{$map[1]}'",
531
		),
532
		'owner_guid' => $user_guid,
533
		'limit' => $limit,
534
		'offset' => $offset,
535
		'count' => $count,
536
		'distinct' => false,
537
	);
538
539
	return elgg_get_entities_from_metadata($options);
540
}
541
542
/**
543
 * Count the unread messages in a user's inbox
544
 *
545
 * @param int $user_guid GUID of user whose inbox we're counting (0 for logged in user)
546
 *
547
 * @return int
548
 */
549
function messages_count_unread($user_guid = 0) {
550
	return messages_get_unread($user_guid, 10, 0, true);
551
}
552
553
/**
554
 * Count the unread messages in a user's inbox
555
 *
556
 * @param int $user_guid GUID of user whose inbox we're counting (0 for logged in user)
557
 *
558
 * @return int
559
 */
560
function messages_count_unread_inbox($user_guid = 0) {
561
	return messages_get_unread_inbox($user_guid, 10, 0, true);
562
}
563
564
/**
565
 * Count the unread messages in a user's inbox
566
 *
567
 * @param int $user_guid GUID of user whose inbox we're counting (0 for logged in user)
568
 *
569
 * @return int
570
 */
571
function messages_count_unread_notifications($user_guid = 0) {
572
	return messages_get_unread_notifications($user_guid, 10, 0, true);
573
}
574
575
/**
576
 * Add to the user hover menu
577
 */
578
function messages_user_hover_menu($hook, $type, $return, $params) {
579
	$user = $params['entity'];
580
581 View Code Duplication
	if (elgg_is_logged_in() && elgg_get_logged_in_user_guid() != $user->guid) {
582
		$url = "messages/compose?send_to={$user->guid}";
583
		$item = new ElggMenuItem('send', elgg_echo('messages:sendmessage'), $url);
584
		$item->setSection('action');
585
		$return[] = $item;
586
	}
587
588
	return $return;
589
}
590
591
/**
592
 * Delete messages from a user who is being deleted
593
 *
594
 * @param string   $event Event name
595
 * @param string   $type  Event type
596
 * @param ElggUser $user  User being deleted
597
 */
598
function messages_purge($event, $type, $user) {
599
600
	if (!$user->getGUID()) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $user->getGUID() of type integer|null is loosely compared to false; this is ambiguous if the integer can be zero. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
601
		return;
602
	}
603
604
	// make sure we delete them all
605
	$entity_disable_override = access_get_show_hidden_status();
606
	access_show_hidden_entities(true);
607
	$ia = elgg_set_ignore_access(true);
608
609
	$options = array(
610
		'type' => 'object',
611
		'subtype' => 'messages',
612
		'metadata_name' => 'fromId',
613
		'metadata_value' => $user->getGUID(),
614
		'limit' => 0,
615
	);
616
	$batch = new ElggBatch('elgg_get_entities_from_metadata', $options);
617
	foreach ($batch as $e) {
618
		$e->delete();
619
	}
620
621
	elgg_set_ignore_access($ia);
622
	access_show_hidden_entities($entity_disable_override);
623
}
624
625
/**
626
 * Register messages with ECML.
627
 *
628
 * @param string $hook
629
 * @param string $entity_type
630
 * @param string $return_value
631
 * @param array  $params
632
 *
633
 * @return array
634
 */
635
function messages_ecml_views_hook($hook, $entity_type, $return_value, $params) {
636
	$return_value['messages/messages'] = elgg_echo('messages');
637
638
	return $return_value;
639
}
640