Elgg  Version 3.0
notification.php
Go to the documentation of this file.
1 <?php
61 function elgg_register_notification_event($object_type, $object_subtype, array $actions = []) {
62  _elgg_services()->notifications->registerEvent($object_type, $object_subtype, $actions);
63 }
64 
73 function elgg_unregister_notification_event($object_type, $object_subtype) {
74  return _elgg_services()->notifications->unregisterEvent($object_type, $object_subtype);
75 }
76 
90  _elgg_services()->notifications->registerMethod($name);
91 }
92 
106  return _elgg_services()->notifications->getMethods();
107 }
108 
118  return _elgg_services()->notifications->unregisterMethod($name);
119 }
120 
131  $methods = _elgg_services()->notifications->getMethods();
132  $db = _elgg_services()->db;
133  $subs = new \Elgg\Notifications\SubscriptionsService($db, $methods);
134  return $subs->addSubscription($user_guid, $method, $target_guid);
135 }
136 
147  $methods = _elgg_services()->notifications->getMethods();
148  $db = _elgg_services()->db;
149  $subs = new \Elgg\Notifications\SubscriptionsService($db, $methods);
150  return $subs->removeSubscription($user_guid, $method, $target_guid);
151 }
152 
168  $methods = _elgg_services()->notifications->getMethods();
169  $db = _elgg_services()->db;
170  $subs = new \Elgg\Notifications\SubscriptionsService($db, $methods);
171  return $subs->getSubscriptionsForContainer($container_guid);
172 }
173 
190  _elgg_services()->notifications->enqueueEvent($action, $type, $object);
191 }
192 
201  // calculate when we should stop
202  // @todo make configurable?
203  $stop_time = time() + 45;
204  _elgg_services()->notifications->processQueue($stop_time);
205 }
206 
218 
219  if ($result === true) {
220  // assume someone else already sent the message
221  return;
222  }
223 
224  $message = $params['notification'];
225  if (!$message instanceof \Elgg\Notifications\Notification) {
226  return false;
227  }
228 
229  $sender = $message->getSender();
230  $recipient = $message->getRecipient();
231 
232  if (!$sender) {
233  return false;
234  }
235 
236  if (!$recipient || !$recipient->email) {
237  return false;
238  }
239 
241  'from' => $sender,
242  'to' => $recipient,
243  'subject' => $message->subject,
244  'body' => $message->body,
245  'params' => $message->params,
246  ]);
247 
248  return _elgg_services()->emails->send($email);
249 }
250 
264 
265  if (!$email instanceof \Elgg\Email) {
266  return;
267  }
268 
269  $hostname = parse_url(elgg_get_site_url(), PHP_URL_HOST);
270  $url_path = parse_url(elgg_get_site_url(), PHP_URL_PATH);
271 
272  $mt = microtime(true);
273 
274  $email->addHeader('Message-ID', "{$url_path}.default.{$mt}@{$hostname}");
275 
276  return $email;
277 }
278 
291 
292  if (!$email instanceof \Elgg\Email) {
293  return;
294  }
295 
296  $notificationParams = $email->getParams();
297 
298  $notification = elgg_extract('notification', $notificationParams);
299  if (!$notification instanceof \Elgg\Notifications\Notification) {
300  return;
301  }
302 
303  $object = elgg_extract('object', $notification->params);
304  if (!$object instanceof \ElggEntity) {
305  return;
306  }
307 
308  $event = elgg_extract('event', $notification->params);
309  if (!$event instanceof \Elgg\Notifications\NotificationEvent) {
310  return;
311  }
312 
313  $hostname = parse_url(elgg_get_site_url(), PHP_URL_HOST);
314  $urlPath = parse_url(elgg_get_site_url(), PHP_URL_PATH);
315 
316  if ($event->getAction() === 'create') {
317  // create event happens once per entity and we need to guarantee message id uniqueness
318  // and at the same time have thread message id that we don't need to store
319  $messageId = "{$urlPath}.entity.{$object->guid}@{$hostname}";
320  } else {
321  $mt = microtime(true);
322  $messageId = "{$urlPath}.entity.{$object->guid}.$mt@{$hostname}";
323  }
324 
325  $email->addHeader("Message-ID", $messageId);
326 
327  // let's just thread comments by default
328  $container = $object->getContainerEntity();
329  if ($container instanceof \ElggEntity && $object instanceof \ElggComment) {
330  $threadMessageId = "<{$urlPath}.entity.{$container->guid}@{$hostname}>";
331  $email->addHeader('In-Reply-To', $threadMessageId);
332  $email->addHeader('References', $threadMessageId);
333  }
334 
335  return $email;
336 }
337 
346  elgg_register_plugin_hook_handler('cron', 'minute', '_elgg_notifications_cron', 100);
347  elgg_register_event_handler('all', 'all', '_elgg_enqueue_notification_event', 700);
348 
349  // add email notifications
351  elgg_register_plugin_hook_handler('send', 'notification:email', '_elgg_send_email_notification');
352  elgg_register_plugin_hook_handler('prepare', 'system:email', '_elgg_notifications_smtp_default_message_id_header', 1);
353  elgg_register_plugin_hook_handler('prepare', 'system:email', '_elgg_notifications_smtp_thread_headers');
354 
355  // add ability to set personal notification method
356  elgg_extend_view('forms/usersettings/save', 'core/settings/account/notifications');
357  elgg_register_plugin_hook_handler('usersettings:save', 'user', '_elgg_save_notification_user_settings');
358 }
359 
375 function _elgg_notify_user($to, $from, $subject, $message, array $params = null, $methods_override = "") {
376 
377  $notify_service = _elgg_services()->notifications;
378 
379  // Sanitise
380  if (!is_array($to)) {
381  $to = [(int) $to];
382  }
383  $from = (int) $from;
384  //$subject = sanitise_string($subject);
385  // Get notification methods
386  if (($methods_override) && (!is_array($methods_override))) {
387  $methods_override = [$methods_override];
388  }
389 
390  $result = [];
391 
392  foreach ($to as $guid) {
393  // Results for a user are...
394  $result[$guid] = [];
395 
396  $recipient = get_entity($guid);
397  if (empty($recipient)) {
398  continue;
399  }
400 
401  // Are we overriding delivery?
402  $methods = $methods_override;
403  if (empty($methods)) {
404  $methods = [];
405 
406  if (!($recipient instanceof ElggUser)) {
407  // not sending to a user so can't get user notification settings
408  continue;
409  }
410 
411  $tmp = $recipient->getNotificationSettings();
412  if (empty($tmp)) {
413  // user has no notification settings
414  continue;
415  }
416 
417  foreach ($tmp as $k => $v) {
418  // Add method if method is turned on for user!
419  if ($v) {
420  $methods[] = $k;
421  }
422  }
423  }
424 
425  if (empty($methods)) {
426  continue;
427  }
428 
429  // Deliver
430  foreach ($methods as $method) {
431  $handler = $notify_service->getDeprecatedHandler($method);
432  /* @var callable $handler */
433  if (!$handler || !is_callable($handler)) {
434  elgg_log("No handler registered for the method $method", 'INFO');
435  continue;
436  }
437 
438  elgg_log("Sending message to $guid using $method");
439 
440  // Trigger handler and retrieve result.
441  try {
442  $result[$guid][$method] = call_user_func(
443  $handler,
444  $from ? get_entity($from) : null,
445  get_entity($guid),
446  $subject,
447  $message,
448  $params
449  );
450  } catch (Exception $e) {
451  elgg_log($e, 'ERROR');
452  }
453  }
454  }
455 
456  return $result;
457 }
458 
507 function notify_user($to, $from = 0, $subject = '', $message = '', array $params = [], $methods_override = null) {
508 
509  $params['subject'] = $subject;
510  $params['body'] = $message;
511  $params['methods_override'] = $methods_override;
512 
513  if ($from) {
514  $sender = get_entity($from);
515  } else {
516  $sender = elgg_get_site_entity();
517  }
518  if (!$sender) {
519  return [];
520  }
521 
522  $recipients = [];
523  $to = (array) $to;
524  foreach ($to as $guid) {
525  $recipient = get_entity($guid);
526  if (!$recipient) {
527  continue;
528  }
529  $recipients[] = $recipient;
530  }
531 
532  return _elgg_services()->notifications->sendInstantNotifications($sender, $recipients, $params);
533 }
534 
543 
544  if (!$email instanceof \Elgg\Email) {
545  elgg_deprecated_notice(__FUNCTION__ . '
546  should be given a single instance of \Elgg\Email
547  ', '3.0');
548 
549  $args = func_get_args();
551  'from' => array_shift($args),
552  'to' => array_shift($args),
553  'subject' => array_shift($args),
554  'body' => array_shift($args),
555  'params' => array_shift($args) ? : [],
556  ]);
557  }
558 
559  return _elgg_services()->emails->send($email);
560 }
561 
573 function elgg_set_email_transport(\Zend\Mail\Transport\TransportInterface $mailer) {
574  _elgg_services()->setValue('mailer', $mailer);
575 }
576 
584 
586  if (!$user) {
587  return;
588  }
589 
590  $method = get_input('method');
591 
592  $current_settings = $user->getNotificationSettings();
593 
594  $result = false;
595  foreach ($method as $k => $v) {
596  // check if setting has changed and skip if not
597  if ($current_settings[$k] == ($v == 'yes')) {
598  continue;
599  }
600 
601  $result = $user->setNotificationSetting($k, ($v == 'yes'));
602  if (!$result) {
603  register_error(elgg_echo('notifications:usersettings:save:fail'));
604  }
605  }
606 
607  if ($result) {
608  system_message(elgg_echo('notifications:usersettings:save:ok'));
609  }
610 }
611 
624 function _elgg_notifications_test($hook, $type, $tests) {
626  return $tests;
627 }
628 
632 return function(\Elgg\EventsService $events, \Elgg\HooksRegistrationService $hooks) {
633  $events->registerHandler('init', 'system', '_elgg_notifications_init');
634 
635  $hooks->registerHandler('unit_test', 'system', '_elgg_notifications_test');
636 };
$action
Definition: full.php:111
if(!$item instanceof ElggRiverItem) $object
Definition: responses.php:23
if(!$user||!$user->canDelete()) $name
Definition: delete.php:22
elgg_register_notification_method($name)
Register a delivery method for notifications.
$params
Saves global plugin settings.
Definition: save.php:13
_elgg_notifications_cron()
Process notification queue.
elgg_send_email($email)
Send an email to any email address.
elgg_unregister_notification_event($object_type, $object_subtype)
Unregister a notification event.
_elgg_enqueue_notification_event($action, $type, $object)
Queue a notification event for later handling.
Events service.
static factory(array $options=[])
Create an email instance form an array of options.
Definition: Email.php:64
$guid
Removes an admin notice.
elgg parse_url
Parse a URL into its parts.
Definition: elgglib.js:442
$args
Some servers don&#39;t allow PHP to check the rewrite, so try via AJAX.
$target_guid
Definition: groups.php:22
$type
Definition: delete.php:21
$email
Definition: register.php:18
elgg_register_plugin_hook_handler($hook, $type, $callback, $priority=500)
Definition: elgglib.php:634
_elgg_notifications_smtp_thread_headers($hook, $type, $email)
Adds default thread SMTP headers to group messages correctly.
elgg_echo($message_key, array $args=[], $language="")
Given a message key, returns an appropriately translated full-text string.
Definition: languages.php:21
notify_user($to, $from=0, $subject= '', $message= '', array $params=[], $methods_override=null)
Notify a user via their preferences.
elgg_remove_subscription($user_guid, $method, $target_guid)
Unsubscribe a user to notifications about a target entity.
elgg_get_subscriptions_for_container($container_guid)
Get the subscriptions for the content created inside this container.
_elgg_notifications_init()
Notification init.
$user_guid
Validate a user.
Definition: validate.php:6
_elgg_save_notification_user_settings()
Save personal notification settings - input comes from request.
elgg_unregister_notification_method($name)
Unregister a delivery method for notifications.
Configuration exception.
Base class for events and hooks.
elgg_set_email_transport(\Zend\Mail\Transport\TransportInterface $mailer)
Replace default email transport.
$container
Definition: delete.php:23
get_input($variable, $default=null, $filter_result=true)
Get some input from variables passed submitted through GET or POST.
Definition: input.php:27
elgg_log($message, $level=\Psr\Log\LogLevel::NOTICE)
Log a message.
Definition: elgglib.php:786
$user
Definition: ban.php:7
if(!$menu instanceof\Elgg\Menu\PreparedMenu) $actions
Definition: user_hover.php:16
elgg_deprecated_notice($msg, $dep_version, $backtrace_level=1)
Log a notice about deprecated use of a function, view, etc.
Definition: elgglib.php:841
elgg_get_site_url()
Get the URL for the current (or specified) site, ending with "/".
elgg_extend_view($view, $view_extension, $priority=501)
Extends a view with another view.
Definition: views.php:293
elgg_get_site_entity()
Get the current site entity.
Definition: entities.php:130
elgg_extract($key, $array, $default=null, $strict=true)
Checks for $array[$key] and returns its value if it exists, else returns $default.
Definition: elgglib.php:1131
elgg system_message
Wrapper function for system_messages.
Definition: elgglib.js:382
elgg_register_event_handler($event, $object_type, $callback, $priority=500)
Definition: elgglib.php:449
_elgg_send_email_notification($hook, $type, $result, $params)
Send an email notification.
_elgg_notifications_test($hook, $type, $tests)
Register unit tests.
elgg_add_subscription($user_guid, $method, $target_guid)
Subscribe a user to notifications about a target entity.
elgg_register_notification_event($object_type, $object_subtype, array $actions=[])
Register a notification event.
elgg register_error
Wrapper function for system_messages.
Definition: elgglib.js:391
class
Definition: placeholder.php:21
_elgg_services()
Get the global service provider.
Definition: elgglib.php:1292
elgg_get_logged_in_user_entity()
Return the current logged in user, or null if no user is logged in.
Definition: sessions.php:29
elgg_get_notification_methods()
Returns registered delivery methods for notifications [ &#39;email&#39; => &#39;email&#39;, &#39;sms&#39; => &#39;sms&#39;...
$container_guid
$handler
Definition: add.php:7
_elgg_notifications_smtp_default_message_id_header($hook, $type, $email)
Adds default Message-ID header to all e-mails.
$subject
Definition: useradd.php:59
_elgg_notify_user($to, $from, $subject, $message, array $params=null, $methods_override="")
Notify a user via their preferences.
get_entity($guid)
Loads and returns an entity object from a guid.
Definition: entities.php:87