00001 <?php
00022 global $NOTIFICATION_HANDLERS;
00023 $NOTIFICATION_HANDLERS = array();
00024
00038 function register_notification_handler($method, $handler, $params = NULL) {
00039 global $NOTIFICATION_HANDLERS;
00040
00041 if (is_callable($handler, true)) {
00042 $NOTIFICATION_HANDLERS[$method] = new stdClass;
00043
00044 $NOTIFICATION_HANDLERS[$method]->handler = $handler;
00045 if ($params) {
00046 foreach ($params as $k => $v) {
00047 $NOTIFICATION_HANDLERS[$method]->$k = $v;
00048 }
00049 }
00050
00051 return true;
00052 }
00053
00054 return false;
00055 }
00056
00065 function unregister_notification_handler($method) {
00066 global $NOTIFICATION_HANDLERS;
00067
00068 if (isset($NOTIFICATION_HANDLERS[$method])) {
00069 unset($NOTIFICATION_HANDLERS[$method]);
00070 }
00071 }
00072
00088 function notify_user($to, $from, $subject, $message, array $params = NULL, $methods_override = "") {
00089 global $NOTIFICATION_HANDLERS;
00090
00091
00092 if (!is_array($to)) {
00093 $to = array((int)$to);
00094 }
00095 $from = (int)$from;
00096
00097
00098
00099 if (($methods_override) && (!is_array($methods_override))) {
00100 $methods_override = array($methods_override);
00101 }
00102
00103 $result = array();
00104
00105 foreach ($to as $guid) {
00106
00107 $result[$guid] = array();
00108
00109 if ($guid) {
00110
00111 $methods = $methods_override;
00112 if (!$methods) {
00113 $tmp = (array)get_user_notification_settings($guid);
00114 $methods = array();
00115 foreach ($tmp as $k => $v) {
00116
00117 if ($v) {
00118 $methods[] = $k;
00119 }
00120 }
00121 }
00122
00123 if ($methods) {
00124
00125 foreach ($methods as $method) {
00126
00127 if (!isset($NOTIFICATION_HANDLERS[$method])) {
00128 continue;
00129 }
00130
00131
00132 $details = $NOTIFICATION_HANDLERS[$method];
00133 $handler = $details->handler;
00134
00135
00136 if ((!$NOTIFICATION_HANDLERS[$method]) || (!$handler) || (!is_callable($handler))) {
00137 error_log(elgg_echo('NotificationException:NoHandlerFound', array($method)));
00138 }
00139
00140 elgg_log("Sending message to $guid using $method");
00141
00142
00143 try {
00144 $result[$guid][$method] = call_user_func($handler,
00145 $from ? get_entity($from) : NULL,
00146 get_entity($guid),
00147 $subject,
00148 $message,
00149 $params
00150 );
00151 } catch (Exception $e) {
00152 error_log($e->getMessage());
00153 }
00154
00155 }
00156 }
00157 }
00158 }
00159
00160 return $result;
00161 }
00162
00170 function get_user_notification_settings($user_guid = 0) {
00171 $user_guid = (int)$user_guid;
00172
00173 if ($user_guid == 0) {
00174 $user_guid = elgg_get_logged_in_user_guid();
00175 }
00176
00177
00178
00179 $all_metadata = elgg_get_metadata(array(
00180 'guid' => $user_guid,
00181 'limit' => 0
00182 ));
00183 if ($all_metadata) {
00184 $prefix = "notification:method:";
00185 $return = new stdClass;
00186
00187 foreach ($all_metadata as $meta) {
00188 $name = substr($meta->name, strlen($prefix));
00189 $value = $meta->value;
00190
00191 if (strpos($meta->name, $prefix) === 0) {
00192 $return->$name = $value;
00193 }
00194 }
00195
00196 return $return;
00197 }
00198
00199 return false;
00200 }
00201
00211 function set_user_notification_setting($user_guid, $method, $value) {
00212 $user_guid = (int)$user_guid;
00213 $method = sanitise_string($method);
00214
00215 $user = get_entity($user_guid);
00216 if (!$user) {
00217 $user = elgg_get_logged_in_user_entity();
00218 }
00219
00220 if (($user) && ($user instanceof ElggUser)) {
00221 $prefix = "notification:method:$method";
00222 $user->$prefix = $value;
00223 $user->save();
00224
00225 return true;
00226 }
00227
00228 return false;
00229 }
00230
00244 function email_notify_handler(ElggEntity $from, ElggUser $to, $subject, $message,
00245 array $params = NULL) {
00246
00247 global $CONFIG;
00248
00249 if (!$from) {
00250 $msg = elgg_echo('NotificationException:MissingParameter', array('from'));
00251 throw new NotificationException($msg);
00252 }
00253
00254 if (!$to) {
00255 $msg = elgg_echo('NotificationException:MissingParameter', array('to'));
00256 throw new NotificationException($msg);
00257 }
00258
00259 if ($to->email == "") {
00260 $msg = elgg_echo('NotificationException:NoEmailAddress', array($to->guid));
00261 throw new NotificationException($msg);
00262 }
00263
00264
00265 $to = $to->email;
00266
00267
00268 $site = elgg_get_site_entity();
00269
00270 if (!($from instanceof ElggUser) && $from->email) {
00271 $from = $from->email;
00272 } else if ($site && $site->email) {
00273
00274 $from = $site->email;
00275 } else {
00276
00277 $from = 'noreply@' . get_site_domain($CONFIG->site_guid);
00278 }
00279
00280 return elgg_send_email($from, $to, $subject, $message);
00281 }
00282
00296 function elgg_send_email($from, $to, $subject, $body, array $params = NULL) {
00297 global $CONFIG;
00298
00299 if (!$from) {
00300 $msg = elgg_echo('NotificationException:MissingParameter', array('from'));
00301 throw new NotificationException($msg);
00302 }
00303
00304 if (!$to) {
00305 $msg = elgg_echo('NotificationException:MissingParameter', array('to'));
00306 throw new NotificationException($msg);
00307 }
00308
00309
00310 $mail_params = array(
00311 'to' => $to,
00312 'from' => $from,
00313 'subject' => $subject,
00314 'body' => $body,
00315 'params' => $params
00316 );
00317
00318 $result = elgg_trigger_plugin_hook('email', 'system', $mail_params, NULL);
00319 if ($result !== NULL) {
00320 return $result;
00321 }
00322
00323 $header_eol = "\r\n";
00324 if (isset($CONFIG->broken_mta) && $CONFIG->broken_mta) {
00325
00326 $header_eol = "\n";
00327 }
00328
00329
00330 if (strtolower(substr(PHP_OS, 0, 3)) == 'win') {
00331
00332 if (strpos($to, '<')) {
00333 preg_match('/<(.*)>/', $to, $matches);
00334 $to = $matches[1];
00335 }
00336 if (strpos($from, '<')) {
00337 preg_match('/<(.*)>/', $from, $matches);
00338 $from = $matches[1];
00339 }
00340 }
00341
00342 $headers = "From: $from{$header_eol}"
00343 . "Content-Type: text/plain; charset=UTF-8; format=flowed{$header_eol}"
00344 . "MIME-Version: 1.0{$header_eol}"
00345 . "Content-Transfer-Encoding: 8bit{$header_eol}";
00346
00347
00348
00349 $subject = preg_replace("/(\r\n|\r|\n)/", " ", $subject);
00350
00351 $subject = html_entity_decode($subject, ENT_COMPAT, 'UTF-8');
00352 if (is_callable('mb_encode_mimeheader')) {
00353 $subject = mb_encode_mimeheader($subject, "UTF-8", "B");
00354 }
00355
00356
00357 $body = html_entity_decode($body, ENT_COMPAT, 'UTF-8');
00358 $body = elgg_strip_tags($body);
00359 $body = preg_replace("/(\r\n|\r)/", "\n", $body);
00360 $body = preg_replace("/^From/", ">From", $body);
00361
00362 return mail($to, $subject, wordwrap($body), $headers);
00363 }
00364
00371 function notification_init() {
00372
00373 register_notification_handler("email", "email_notify_handler");
00374
00375
00376 elgg_extend_view('forms/account/settings', 'core/settings/account/notifications');
00377
00378 elgg_register_plugin_hook_handler('usersettings:save', 'user', 'notification_user_settings_save');
00379 }
00380
00388 function notification_user_settings_save() {
00389 global $CONFIG;
00390
00391 include($CONFIG->path . "actions/notifications/settings/usersettings/save.php");
00392 }
00393
00403 function register_notification_object($entity_type, $object_subtype, $language_name) {
00404 global $CONFIG;
00405
00406 if ($entity_type == '') {
00407 $entity_type = '__BLANK__';
00408 }
00409 if ($object_subtype == '') {
00410 $object_subtype = '__BLANK__';
00411 }
00412
00413 if (!isset($CONFIG->register_objects)) {
00414 $CONFIG->register_objects = array();
00415 }
00416
00417 if (!isset($CONFIG->register_objects[$entity_type])) {
00418 $CONFIG->register_objects[$entity_type] = array();
00419 }
00420
00421 $CONFIG->register_objects[$entity_type][$object_subtype] = $language_name;
00422 }
00423
00432 function register_notification_interest($user_guid, $author_guid) {
00433 return add_entity_relationship($user_guid, 'notify', $author_guid);
00434 }
00435
00444 function remove_notification_interest($user_guid, $author_guid) {
00445 return remove_entity_relationship($user_guid, 'notify', $author_guid);
00446 }
00447
00461 function object_notifications($event, $object_type, $object) {
00462
00463 if ($object instanceof ElggEntity) {
00464
00465
00466
00467 global $CONFIG, $SESSION, $NOTIFICATION_HANDLERS;
00468
00469 $hookresult = elgg_trigger_plugin_hook('object:notifications', $object_type, array(
00470 'event' => $event,
00471 'object_type' => $object_type,
00472 'object' => $object,
00473 ), false);
00474 if ($hookresult === true) {
00475 return true;
00476 }
00477
00478
00479 $object_type = $object->getType();
00480 if (empty($object_type)) {
00481 $object_type = '__BLANK__';
00482 }
00483
00484 $object_subtype = $object->getSubtype();
00485 if (empty($object_subtype)) {
00486 $object_subtype = '__BLANK__';
00487 }
00488
00489 if (isset($CONFIG->register_objects[$object_type][$object_subtype])) {
00490 $subject = $CONFIG->register_objects[$object_type][$object_subtype];
00491 $string = $subject . ": " . $object->getURL();
00492
00493
00494
00495 foreach ($NOTIFICATION_HANDLERS as $method => $foo) {
00496 $interested_users = elgg_get_entities_from_relationship(array(
00497 'site_guids' => ELGG_ENTITIES_ANY_VALUE,
00498 'relationship' => 'notify' . $method,
00499 'relationship_guid' => $object->container_guid,
00500 'inverse_relationship' => TRUE,
00501 'type' => 'user',
00502 'limit' => false
00503 ));
00504
00505
00506 if ($interested_users && is_array($interested_users)) {
00507 foreach ($interested_users as $user) {
00508 if ($user instanceof ElggUser && !$user->isBanned()) {
00509 if (($user->guid != $SESSION['user']->guid) && has_access_to_entity($object, $user)
00510 && $object->access_id != ACCESS_PRIVATE) {
00511 $body = elgg_trigger_plugin_hook('notify:entity:message', $object->getType(), array(
00512 'entity' => $object,
00513 'to_entity' => $user,
00514 'method' => $method), $string);
00515 if (empty($body) && $body !== false) {
00516 $body = $string;
00517 }
00518 if ($body !== false) {
00519 notify_user($user->guid, $object->container_guid, $subject, $body,
00520 null, array($method));
00521 }
00522 }
00523 }
00524 }
00525 }
00526 }
00527 }
00528 }
00529 }
00530
00531
00532 elgg_register_event_handler('init', 'system', 'notification_init', 0);
00533 elgg_register_event_handler('create', 'object', 'object_notifications');