00001 <?php
00025 $NOTIFICATION_HANDLERS = array();
00026
00034 function register_notification_handler($method, $handler, $params = NULL)
00035 {
00036 global $NOTIFICATION_HANDLERS;
00037
00038 if (is_callable($handler))
00039 {
00040 $NOTIFICATION_HANDLERS[$method] = new stdClass;
00041
00042 $NOTIFICATION_HANDLERS[$method]->handler = $handler;
00043 if ($params)
00044 {
00045 foreach ($params as $k => $v)
00046 $NOTIFICATION_HANDLERS[$method]->$k = $v;
00047 }
00048
00049 return true;
00050 }
00051
00052 return false;
00053 }
00054
00068 function notify_user($to, $from, $subject, $message, array $params = NULL, $methods_override = "")
00069 {
00070 global $NOTIFICATION_HANDLERS, $CONFIG;
00071
00072
00073 if (!is_array($to))
00074 $to = array((int)$to);
00075 $from = (int)$from;
00076
00077
00078
00079 if (($methods_override) && (!is_array($methods_override)))
00080 $methods_override = array($methods_override);
00081
00082 $result = array();
00083
00084 foreach ($to as $guid)
00085 {
00086
00087 $result[$guid] = array();
00088
00089 if ($guid) {
00090
00091 $methods = $methods_override;
00092 if (!$methods)
00093 {
00094 $tmp = (array)get_user_notification_settings($guid);
00095 $methods = array();
00096 foreach($tmp as $k => $v)
00097 if ($v) $methods[] = $k;
00098 }
00099
00100 if ($methods)
00101 {
00102
00103 foreach ($methods as $method)
00104 {
00105
00106 $details = $NOTIFICATION_HANDLERS[$method];
00107 $handler = $details->handler;
00108
00109 if ((!$NOTIFICATION_HANDLERS[$method]) || (!$handler))
00110 error_log(sprintf(elgg_echo('NotificationException:NoHandlerFound'), $method));
00111
00112 if ($CONFIG->debug)
00113 error_log("Sending message to $guid using $method");
00114
00115
00116 try {
00117 $result[$guid][$method] = $handler(
00118 $from ? get_entity($from) : NULL,
00119 get_entity($guid),
00120 $subject,
00121 $message,
00122 $params
00123 );
00124 } catch (Exception $e) {
00125 error_log($e->getMessage());
00126 }
00127
00128 }
00129 }
00130 }
00131 }
00132
00133 return $result;
00134 }
00135
00142 function get_user_notification_settings($user_guid = 0)
00143 {
00144 $user_guid = (int)$user_guid;
00145
00146 if ($user_guid == 0) $user_guid = get_loggedin_userid();
00147
00148 $all_metadata = get_metadata_for_entity($user_guid);
00149 if ($all_metadata)
00150 {
00151 $prefix = "notification:method:";
00152 $return = new stdClass;
00153
00154 foreach ($all_metadata as $meta)
00155 {
00156 $name = substr($meta->name, strlen($prefix));
00157 $value = $meta->value;
00158
00159 if (strpos($meta->name, $prefix) === 0)
00160 $return->$name = $value;
00161 }
00162
00163 return $return;
00164 }
00165
00166 return false;
00167 }
00168
00177 function set_user_notification_setting($user_guid, $method, $value)
00178 {
00179 $user_guid = (int)$user_guid;
00180 $method = sanitise_string($method);
00181
00182 $user = get_entity($user_guid);
00183 if (!$user) $user = get_loggedin_user();
00184
00185 if (($user) && ($user instanceof ElggUser))
00186 {
00187 $prefix = "notification:method:$method";
00188 $user->$prefix = $value;
00189 $user->save();
00190
00191 return true;
00192 }
00193
00194 return false;
00195 }
00196
00201 class NotificationException extends Exception {}
00202
00203
00214 function email_notify_handler(ElggEntity $from, ElggUser $to, $subject, $message, array $params = NULL)
00215 {
00216 global $CONFIG;
00217
00218 if (!$from)
00219 throw new NotificationException(sprintf(elgg_echo('NotificationException:MissingParameter'), 'from'));
00220
00221 if (!$to)
00222 throw new NotificationException(sprintf(elgg_echo('NotificationException:MissingParameter'), 'to'));
00223
00224 if ($to->email=="")
00225 throw new NotificationException(sprintf(elgg_echo('NotificationException:NoEmailAddress'), $to->guid));
00226
00227
00228 $subject = preg_replace("/(\r\n|\r|\n)/", " ", $subject);
00229
00230
00231 $to = $to->email;
00232
00233
00234 $site = get_entity($CONFIG->site_guid);
00235 if ((isset($from->email)) && (!($from instanceof ElggUser)))
00236 $from = $from->email;
00237 else if (($site) && (isset($site->email)))
00238 $from = $site->email;
00239 else if (isset($from->url))
00240 {
00241 $breakdown = parse_url($from->url);
00242 $from = 'noreply@' . $breakdown['host'];
00243 }
00244 else
00245 $from = 'noreply@' . get_site_domain($CONFIG->site_guid);
00246
00247 if (is_callable('mb_internal_encoding')) {
00248 mb_internal_encoding('UTF-8');
00249 }
00250 $site = get_entity($CONFIG->site_guid);
00251 $sitename = $site->name;
00252 if (is_callable('mb_encode_mimeheader')) {
00253 $sitename = mb_encode_mimeheader($site->name,"UTF-8", "B");
00254 }
00255
00256 $header_eol = "\r\n";
00257 if (
00258 (isset($CONFIG->broken_mta)) &&
00259 ($CONFIG->broken_mta)
00260 )
00261 $header_eol = "\n";
00262
00263 $from_email = "\"$sitename\" <$from>";
00264 if (strtolower(substr(PHP_OS, 0 , 3)) == 'win')
00265 $from_email = "$from";
00266
00267 $headers = "From: $from_email{$header_eol}"
00268 . "Content-Type: text/plain; charset=UTF-8; format=flowed{$header_eol}"
00269 . "MIME-Version: 1.0{$header_eol}"
00270 . "Content-Transfer-Encoding: 8bit{$header_eol}";
00271
00272 if (is_callable('mb_encode_mimeheader')) {
00273 $subject = mb_encode_mimeheader($subject,"UTF-8", "B");
00274 }
00275
00276
00277 $message = html_entity_decode($message, ENT_COMPAT, 'UTF-8');
00278 $message = strip_tags($message);
00279 $message = preg_replace("/(\r\n|\r)/", "\n", $message);
00280 $message = preg_replace("/^From/", ">From", $message);
00281
00282 return mail($to, $subject, wordwrap($message), $headers);
00283 }
00284
00289 function notification_init()
00290 {
00291
00292 register_notification_handler("email", "email_notify_handler");
00293
00294
00295 extend_elgg_settings_page('notifications/settings/usersettings', 'usersettings/user');
00296
00297 register_plugin_hook('usersettings:save','user','notification_user_settings_save');
00298
00299
00300
00301
00302
00303 expose_function('user.notification.get', 'get_user_notification_settings', array(
00304 'user_guid' => array ('type' => 'int')
00305 ), elgg_echo('user.notification.get'));
00306
00307 expose_function('user.notification.set', 'set_user_notification_settings', array(
00308 'user_guid' => array ('type' => 'int'),
00309 'method' => array ('type' => 'string'),
00310 'value' => array ('type' => 'bool')
00311 ), elgg_echo('user.notification.set'));
00312
00313 }
00314
00315 function notification_user_settings_save() {
00316
00317 global $CONFIG;
00318 @include($CONFIG->path . "actions/notifications/settings/usersettings/save.php");
00319
00320 }
00321
00329 function register_notification_object($entity_type, $object_subtype, $english_name) {
00330 global $CONFIG;
00331
00332 if ($entity_type == '') $entity_type = '__BLANK__';
00333 if ($object_subtype == '') $object_subtype = '__BLANK__';
00334
00335 if (!isset($CONFIG->register_objects)) {
00336 $CONFIG->register_objects = array();
00337 }
00338 if (!isset($CONFIG->register_objects[$entity_type])) {
00339 $CONFIG->register_objects[$entity_type] = array();
00340 }
00341 $CONFIG->register_objects[$entity_type][$object_subtype] = $english_name;
00342 }
00343
00351 function register_notification_interest($user_guid, $author_guid) {
00352 return add_entity_relationship($user_guid, 'notify', $author_guid);
00353 }
00354
00362 function remove_notification_interest($user_guid, $author_guid) {
00363 return remove_entity_relationship($user_guid, 'notify', $author_guid);
00364 }
00365
00372 function object_notifications($event, $object_type, $object) {
00373
00374
00375 if ($object instanceof ElggEntity) {
00376
00377
00378 global $CONFIG, $SESSION, $NOTIFICATION_HANDLERS;
00379
00380 $hookresult = trigger_plugin_hook('object:notifications',$object_type,array(
00381 'event' => $event,
00382 'object_type' => $object_type,
00383 'object' => $object,
00384 ),false);
00385 if ($hookresult === true) return true;
00386
00387
00388 $object_type = $object->getType(); if (empty($object_type)) $object_type = '__BLANK__';
00389 $object_subtype = $object->getSubtype(); if (empty($object_subtype)) $object_subtype = '__BLANK__';
00390 if (isset($CONFIG->register_objects[$object_type][$object_subtype])) {
00391
00392 $descr = $CONFIG->register_objects[$object_type][$object_subtype];
00393 $string = $descr . ": " . $object->getURL();
00394
00395
00396
00397 foreach($NOTIFICATION_HANDLERS as $method => $foo)
00398 if ($interested_users = get_entities_from_relationship('notify' . $method,$object->container_guid,true,'user','',0,'',99999)) {
00399
00400 if (is_array($interested_users))
00401 foreach($interested_users as $user) {
00402 if ($user instanceof ElggUser) {
00403
00404 if (!$user->isBanned())
00405 if (($user->guid != $SESSION['user']->guid) && has_access_to_entity($object,$user)
00406 && $object->access_id != ACCESS_PRIVATE) {
00407
00408 $methodstring = trigger_plugin_hook('notify:entity:message',$object->getType(),array(
00409 'entity' => $object,
00410 'to_entity' => $user,
00411 'method' => $method),$string);
00412 if (empty($methodstring) && $methodstring !== false) $methodstring = $string;
00413 if ($methodstring !== false)
00414 notify_user($user->guid,$object->container_guid,$descr,$methodstring,NULL,array($method));
00415 }
00416 }
00417 }
00418 }
00419
00420 }
00421
00422 }
00423
00424 }
00425
00426
00427 register_elgg_event_handler('init','system','notification_init',0);
00428 register_elgg_event_handler('create','object','object_notifications');
00429
00430 ?>