aboutsummaryrefslogtreecommitdiff
path: root/engine/lib/notification.php
blob: 15448f720a1a7f2b6aa87252cdade1a665b0b547 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
<?php
	/**
	 * Notifications
	 * This file contains classes and functions which allow plugins to register and send notifications.
	 * 
	 * @package Elgg
	 * @subpackage API
	 * @license http://www.gnu.org/licenses/old-licenses/gpl-2.0.html GNU Public License version 2
	 * @author Marcus Povey
	 * @copyright Curverider Ltd 2008
	 * @link http://elgg.org/
	 */

	/** Notification handlers */
	$NOTIFICATION_HANDLERS = array();
	
	/**
	 * This function registers a handler for a given notification type (eg "email")
	 *
	 * @param string $method The method
	 * @param string $handler The handler function, in the format "handler($to_guid, $message, array $params = NULL)"
	 */
	function register_notification_handler($method, $handler)
	{
		global $NOTIFICATION_HANDLERS;
		
		if (is_callable($handler)) 
		{
			$NOTIFICATION_HANDLERS[$method] = $handler;
			
			return true;
		}
		
		return false;
	}
	
	/**
	 * Send a notification message using a pre-registered method.
	 *
	 * @param string $method The method used 
	 * @param mixed $to Either a guid or an array of guid's to notify
	 * @param string $message A message
	 * @param array $params Optional method specific parameters as an associative array, for example "subject", "from"
	 * @return boolean
	 * @throws NotificationException
	 */
	function notify($method, $to, $message, array $params = NULL)
	{
		global $NOTIFICATION_HANDLERS;
		
		// Sanitise
		if (!is_array($to))
			$to = (int)$to;
			
		if ($method=="")
			throw new NotificationException("No notification method specified.");
			
		if ((!array_key_exists($method, $NOTIFICATION_HANDLERS)) || (!is_callable($NOTIFICATION_HANDLERS[$method])))
			throw new NotificationExceptions("No handler found for '$method' or it was not callable.");
			
		if (!is_array($to))
			$to = array($to);
			
		foreach ($to as $guid)	
				if (!$NOTIFICATION_HANDLERS[$method]($guid, sanitise_string($method), $params))
					throw new NotificationException("There was an error while notifying $guid");
			
		return true;
	}
	
	/**
	 * Notification exception.
	 * @author Marcus Povey
	 */
	class NotificationException extends Exception {}


	/**
	 * Send a notification via email.
	 * 
	 * Parameters accept "from" and "subject" as values.
	 */
	function email_notify_handler($to_guid, $message, array $params = NULL)
	{
		$to_guid = (int)$to_guid;
		
		$entity = get_entity($to_guid);
		
		if ((!($entity instanceof ElggUser)) || ($entity->email==""))
			throw new NotificationException("Could not get the email address for GUID:$to_guid");
		
		
		$to = $entity->email;
		$subject = $params['subject'];
		$from = $params['from'];
		
		$headers = "From: $from\r\n";
				
		return mail($to, $subject, $message, $headers);
	}

	/**
	 * Correctly initialise notifications and register the email handler.
	 *
	 */
	function notification_init()
	{
		register_notification_handler("email", "email_notify_handler");
	}

	// Register a startup event
	register_event_handler('init','system','notification_init',0);	
?>