123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728 |
- <?php
- function elgg_register_notification_event($object_type, $object_subtype, array $actions = array()) {
- _elgg_services()->notifications->registerEvent($object_type, $object_subtype, $actions);
- }
- function elgg_unregister_notification_event($object_type, $object_subtype) {
- return _elgg_services()->notifications->unregisterEvent($object_type, $object_subtype);
- }
- function elgg_register_notification_method($name) {
- _elgg_services()->notifications->registerMethod($name);
- }
- function elgg_unregister_notification_method($name) {
- return _elgg_services()->notifications->unregisterMethod($name);
- }
- function elgg_add_subscription($user_guid, $method, $target_guid) {
- $methods = _elgg_services()->notifications->getMethods();
- $db = _elgg_services()->db;
- $subs = new \Elgg\Notifications\SubscriptionsService($db, $methods);
- return $subs->addSubscription($user_guid, $method, $target_guid);
- }
- function elgg_remove_subscription($user_guid, $method, $target_guid) {
- $methods = _elgg_services()->notifications->getMethods();
- $db = _elgg_services()->db;
- $subs = new \Elgg\Notifications\SubscriptionsService($db, $methods);
- return $subs->removeSubscription($user_guid, $method, $target_guid);
- }
- function elgg_get_subscriptions_for_container($container_guid) {
- $methods = _elgg_services()->notifications->getMethods();
- $db = _elgg_services()->db;
- $subs = new \Elgg\Notifications\SubscriptionsService($db, $methods);
- return $subs->getSubscriptionsForContainer($container_guid);
- }
- function _elgg_enqueue_notification_event($action, $type, $object) {
- _elgg_services()->notifications->enqueueEvent($action, $type, $object);
- }
- function _elgg_notifications_cron() {
-
-
- $stop_time = time() + 45;
- _elgg_services()->notifications->processQueue($stop_time);
- }
- function _elgg_send_email_notification($hook, $type, $result, $params) {
-
- $message = $params['notification'];
- $sender = $message->getSender();
- $recipient = $message->getRecipient();
- if (!$sender) {
- return false;
- }
- if (!$recipient || !$recipient->email) {
- return false;
- }
- $to = $recipient->email;
- $site = elgg_get_site_entity();
-
- if (!($sender instanceof \ElggUser) && $sender->email) {
- $from = $sender->email;
- } else if ($site->email) {
- $from = $site->email;
- } else {
-
- $from = 'noreply@' . $site->getDomain();
- }
- return elgg_send_email($from, $to, $message->subject, $message->body, $params);
- }
- function _elgg_notifications_smtp_thread_headers($hook, $type, $returnvalue, $params) {
- $notificationParams = elgg_extract('params', $returnvalue, array());
-
- $notification = elgg_extract('notification', $notificationParams);
- if (!($notification instanceof \Elgg\Notifications\Notification)) {
- return $returnvalue;
- }
- $hostname = parse_url(elgg_get_site_url(), PHP_URL_HOST);
- $urlPath = parse_url(elgg_get_site_url(), PHP_URL_PATH);
- $object = elgg_extract('object', $notification->params);
-
- $event = elgg_extract('event', $notification->params);
- if (($object instanceof \ElggEntity) && ($event instanceof \Elgg\Notifications\Event)) {
- if ($event->getAction() === 'create') {
-
-
- $messageId = "<{$urlPath}.entity.{$object->guid}@{$hostname}>";
- } else {
- $mt = microtime(true);
- $messageId = "<{$urlPath}.entity.{$object->guid}.$mt@{$hostname}>";
- }
- $returnvalue['headers']["Message-ID"] = $messageId;
- $container = $object->getContainerEntity();
-
- if (($container instanceof \ElggEntity) && ($object instanceof \ElggComment)) {
- $threadMessageId = "<{$urlPath}.entity.{$container->guid}@{$hostname}>";
- $returnvalue['headers']['In-Reply-To'] = $threadMessageId;
- $returnvalue['headers']['References'] = $threadMessageId;
- }
- }
- return $returnvalue;
- }
- function _elgg_notifications_init() {
- elgg_register_plugin_hook_handler('cron', 'minute', '_elgg_notifications_cron', 100);
- elgg_register_event_handler('all', 'all', '_elgg_enqueue_notification_event');
-
- elgg_register_notification_method('email');
- elgg_register_plugin_hook_handler('send', 'notification:email', '_elgg_send_email_notification');
- elgg_register_plugin_hook_handler('email', 'system', '_elgg_notifications_smtp_thread_headers');
-
- elgg_extend_view('forms/account/settings', 'core/settings/account/notifications');
- elgg_register_plugin_hook_handler('usersettings:save', 'user', '_elgg_save_notification_user_settings');
- }
- function _elgg_notify_user($to, $from, $subject, $message, array $params = null, $methods_override = "") {
- $notify_service = _elgg_services()->notifications;
-
- if (!is_array($to)) {
- $to = array((int)$to);
- }
- $from = (int)$from;
-
-
- if (($methods_override) && (!is_array($methods_override))) {
- $methods_override = array($methods_override);
- }
- $result = array();
- foreach ($to as $guid) {
-
- $result[$guid] = array();
- if ($guid) {
-
- $methods = $methods_override;
- if (!$methods) {
- $tmp = get_user_notification_settings($guid);
- $methods = array();
-
- if (is_object($tmp)) {
- foreach ($tmp as $k => $v) {
-
- if ($v) {
- $methods[] = $k;
- }
- }
- }
- }
- if ($methods) {
-
- foreach ($methods as $method) {
-
- $handler = $notify_service->getDeprecatedHandler($method);
-
- if (!$handler || !is_callable($handler)) {
- elgg_log("No handler registered for the method $method", 'WARNING');
- continue;
- }
- elgg_log("Sending message to $guid using $method");
-
- try {
- $result[$guid][$method] = call_user_func($handler,
- $from ? get_entity($from) : null,
- get_entity($guid),
- $subject,
- $message,
- $params
- );
- } catch (Exception $e) {
- error_log($e->getMessage());
- }
- }
- }
- }
- }
- return $result;
- }
- function notify_user($to, $from, $subject, $message, array $params = array(), $methods_override = "") {
- if (!is_array($to)) {
- $to = array((int)$to);
- }
- $from = (int)$from;
- $from = get_entity($from) ? $from : elgg_get_site_entity()->guid;
- $sender = get_entity($from);
- $summary = elgg_extract('summary', $params, '');
-
- if (($methods_override) && (!is_array($methods_override))) {
- $methods_override = array($methods_override);
- }
- $result = array();
- $available_methods = _elgg_services()->notifications->getMethods();
- if (!$available_methods) {
-
- return $result;
- }
-
- $event = null;
- if (isset($params['object']) && isset($params['action'])) {
- $event = new \Elgg\Notifications\Event($params['object'], $params['action'], $sender);
- }
- $params['event'] = $event;
- foreach ($to as $guid) {
-
- $result[$guid] = array();
- if ($guid) {
-
- $methods = $methods_override;
- if (!$methods) {
- $tmp = (array)get_user_notification_settings($guid);
- $methods = array();
- foreach ($tmp as $k => $v) {
-
- if ($v) {
- $methods[] = $k;
- }
- }
- }
- if ($methods) {
-
- foreach ($methods as $method) {
- if (!in_array($method, $available_methods)) {
-
-
- continue;
- }
- if (_elgg_services()->hooks->hasHandler('send', "notification:$method")) {
-
- $recipient = get_entity($guid);
- if (!$recipient) {
- continue;
- }
- $language = $recipient->language;
- $notification = new \Elgg\Notifications\Notification($sender, $recipient, $language, $subject, $message, $summary, $params);
- $params['notification'] = $notification;
- $result[$guid][$method] = _elgg_services()->hooks->trigger('send', "notification:$method", $params, false);
- } else {
- $result[$guid][$method] = _elgg_notify_user($guid, $from, $subject, $message, $params, array($method));
- }
- }
- }
- }
- }
- return $result;
- }
- function get_user_notification_settings($user_guid = 0) {
- $user_guid = (int)$user_guid;
- if ($user_guid == 0) {
- $user_guid = elgg_get_logged_in_user_guid();
- }
-
-
- $all_metadata = elgg_get_metadata(array(
- 'guid' => $user_guid,
- 'limit' => 0
- ));
- if ($all_metadata) {
- $prefix = "notification:method:";
- $return = new \stdClass;
- foreach ($all_metadata as $meta) {
- $name = substr($meta->name, strlen($prefix));
- $value = $meta->value;
- if (strpos($meta->name, $prefix) === 0) {
- $return->$name = $value;
- }
- }
- return $return;
- }
- return false;
- }
- function set_user_notification_setting($user_guid, $method, $value) {
- $user_guid = (int)$user_guid;
- $method = sanitise_string($method);
- $user = get_entity($user_guid);
- if (!$user) {
- $user = elgg_get_logged_in_user_entity();
- }
- if (($user) && ($user instanceof \ElggUser)) {
- $prefix = "notification:method:$method";
- $user->$prefix = $value;
- $user->save();
- return true;
- }
- return false;
- }
- function elgg_send_email($from, $to, $subject, $body, array $params = null) {
- global $CONFIG;
- if (!$from) {
- $msg = "Missing a required parameter, '" . 'from' . "'";
- throw new \NotificationException($msg);
- }
- if (!$to) {
- $msg = "Missing a required parameter, '" . 'to' . "'";
- throw new \NotificationException($msg);
- }
- $headers = array(
- "Content-Type" => "text/plain; charset=UTF-8; format=flowed",
- "MIME-Version" => "1.0",
- "Content-Transfer-Encoding" => "8bit",
- );
-
- $mail_params = array(
- 'to' => $to,
- 'from' => $from,
- 'subject' => $subject,
- 'body' => $body,
- 'headers' => $headers,
- 'params' => $params,
- );
-
-
-
- $result = elgg_trigger_plugin_hook('email', 'system', $mail_params, $mail_params);
- if (is_array($result)) {
- foreach (array('to', 'from', 'subject', 'body', 'headers') as $key) {
- if (isset($result[$key])) {
- ${$key} = $result[$key];
- }
- }
- } elseif ($result !== null) {
- return $result;
- }
- $header_eol = "\r\n";
- if (isset($CONFIG->broken_mta) && $CONFIG->broken_mta) {
-
- $header_eol = "\n";
- }
-
- if (strtolower(substr(PHP_OS, 0, 3)) == 'win') {
-
- if (strpos($to, '<')) {
- preg_match('/<(.*)>/', $to, $matches);
- $to = $matches[1];
- }
- if (strpos($from, '<')) {
- preg_match('/<(.*)>/', $from, $matches);
- $from = $matches[1];
- }
- }
-
- if (empty($headers['From'])) {
- $headers['From'] = $from;
- }
-
- $headers_string = '';
- foreach ($headers as $key => $value) {
- $headers_string .= "$key: $value{$header_eol}";
- }
-
- $subject = preg_replace("/(\r\n|\r|\n)/", " ", $subject);
-
- $subject = html_entity_decode($subject, ENT_QUOTES, 'UTF-8');
- if (is_callable('mb_encode_mimeheader')) {
- $subject = mb_encode_mimeheader($subject, "UTF-8", "B");
- }
-
- $body = html_entity_decode($body, ENT_QUOTES, 'UTF-8');
- $body = elgg_strip_tags($body);
- $body = preg_replace("/(\r\n|\r)/", "\n", $body);
- $body = preg_replace("/^From/", ">From", $body);
- $body = wordwrap($body);
- return mail($to, $subject, $body, $headers_string);
- }
- function _elgg_save_notification_user_settings() {
- $method = get_input('method');
- $current_settings = get_user_notification_settings();
- $result = false;
- foreach ($method as $k => $v) {
-
- if ($current_settings->$k == ($v == 'yes')) {
- continue;
- }
- $result = set_user_notification_setting(elgg_get_logged_in_user_guid(), $k, ($v == 'yes') ? true : false);
- if (!$result) {
- register_error(elgg_echo('notifications:usersettings:save:fail'));
- }
- }
- if ($result) {
- system_message(elgg_echo('notifications:usersettings:save:ok'));
- }
- }
- function _elgg_notifications_test($hook, $type, $tests) {
- global $CONFIG;
- $tests[] = "{$CONFIG->path}engine/tests/ElggCoreDatabaseQueueTest.php";
- return $tests;
- }
- return function(\Elgg\EventsService $events, \Elgg\HooksRegistrationService $hooks) {
- $events->registerHandler('init', 'system', '_elgg_notifications_init');
- $hooks->registerHandler('unit_test', 'system', '_elgg_notifications_test');
- };
|