1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12:
13: class Folks_Notification {
14:
15: 16: 17:
18: static private $instances = array();
19:
20: 21: 22:
23: protected $_params;
24:
25: 26: 27: 28: 29: 30:
31: public function __construct($params = array())
32: {
33: $this->_params = $params;
34: }
35:
36: 37: 38: 39: 40: 41: 42: 43: 44: 45:
46: public function notifyAll($subject, $body, $attachments = array(), $user = null)
47: {
48: $result = false;
49:
50: if (empty($user)) {
51: if ($GLOBALS['registry']->isAuthenticated()) {
52: $user = $GLOBALS['registry']->getAuth();
53: } else {
54: return true;
55: }
56: }
57:
58: foreach ($GLOBALS['conf']['notification'] as $driver => $params) {
59: if ($params['enabled'] && $params['users']) {
60: $instance = $this->singleton($driver, $params);
61: if ($instance instanceof PEAR_Error) {
62: return $instance;
63: }
64: if (!$instance->isAvailable('users')) {
65: continue;
66: }
67: $result = $instance->notify($user, $subject, $body, $attachments);
68: if ($result instanceof PEAR_Error) {
69: return $result;
70: }
71: }
72: }
73:
74: return $result;
75: }
76:
77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87:
88: public function notifyAllFriends($subject, $body, $attachments = array(), $user = null)
89: {
90: $result = false;
91:
92: if (empty($user)) {
93: if ($GLOBALS['registry']->isAuthenticated()) {
94: $user = $GLOBALS['registry']->getAuth();
95: } else {
96: return true;
97: }
98: }
99:
100: foreach ($GLOBALS['conf']['notification'] as $driver => $params) {
101: if ($params['enabled'] && $params['friends']) {
102: $instance = $this->singleton($driver, $params);
103: if ($instance instanceof PEAR_Error) {
104: return $instance;
105: }
106: if (!$instance->isAvailable('friends')) {
107: continue;
108: }
109: $result = $instance->notifyFriends($user, $subject, $body, $attachments);
110: if ($result instanceof PEAR_Error) {
111: return $result;
112: }
113: }
114: }
115:
116: return $result;
117: }
118:
119: 120: 121: 122: 123: 124: 125: 126: 127:
128: public function notifyAdmins($subject, $body, $attachments = array())
129: {
130: $result = false;
131:
132: $admins = $this->getAdmins();
133: if (empty($admins)) {
134: return true;
135: }
136:
137: foreach ($GLOBALS['conf']['notification'] as $driver => $params) {
138: if ($params['enabled'] && $params['admins']) {
139: $instance = $this->singleton($driver, $params);
140: if ($instance instanceof PEAR_Error) {
141: return $instance;
142: }
143: if (!$instance->isAvailable('admins')) {
144: continue;
145: }
146: $result = $instance->notify($admins, $subject, $body, $attachments);
147: if ($result instanceof PEAR_Error) {
148: return $result;
149: }
150: }
151: }
152:
153: return $result;
154: }
155:
156: 157: 158: 159: 160:
161: public function getAdmins()
162: {
163: $name = $GLOBALS['registry']->getApp() . ':admin';
164:
165: if ($GLOBALS['injector']->getInstance('Horde_Perms')->exists($name)) {
166: $permission = $GLOBALS['injector']->getInstance('Horde_Perms')->getPermission($name);
167: if ($permission instanceof PEAR_Error) {
168: return $permission;
169: } else {
170: $admins = $permission->getUserPermissions(Horde_Perms::DELETE);
171: if ($admins instanceof PEAR_Error) {
172: return $admins;
173: }
174: $admins = array_keys($admins);
175: }
176: }
177:
178: if (empty($admins)) {
179: return $GLOBALS['conf']['auth']['admins'];
180: } else {
181: return $admins;
182: }
183: }
184:
185: 186: 187: 188: 189: 190: 191:
192: public function getMethods($type = 'user')
193: {
194: $methods = array();
195:
196: foreach ($GLOBALS['conf']['notification'] as $driver => $params) {
197: if (empty($params['enabled'])) {
198: continue;
199: }
200: $instance = $this->singleton($driver, $params);
201: if ($instance instanceof PEAR_Error) {
202: return $instance;
203: }
204: if (!$instance->isAvailable($type)) {
205: continue;
206: }
207: $methods[$driver] = $instance->getName();
208: }
209:
210: return $methods;
211: }
212:
213: 214: 215: 216: 217: 218: 219:
220: protected function _getUserFromAddr($user)
221: {
222: return $GLOBALS['injector']->getInstance('Horde_Core_Factory_Identity')->create($user)->getValue('from_addr');
223: }
224:
225: 226: 227: 228: 229: 230: 231: 232: 233: 234: 235: 236: 237: 238:
239: static protected function factory($driver, $params = null)
240: {
241: include_once FOLKS_BASE . '/lib/Notification/' . $driver . '.php';
242:
243: if ($params === null) {
244: $params = $GLOBALS['conf']['notification'][$driver];
245: }
246:
247: $class = 'Folks_Notification_' . $driver;
248: if (class_exists($class)) {
249: return new $class($params);
250: } else {
251: return PEAR::raiseError(sprintf(_("Notification driver %s does not exists."), $driver));
252: }
253: }
254:
255: 256: 257: 258: 259: 260: 261: 262: 263: 264: 265:
266: static public function singleton($driver, $params = null)
267: {
268: if (!array_key_exists($driver, self::$instances)) {
269: self::$instances[$driver] = self::factory($driver, $params);
270: }
271:
272: return self::$instances[$driver];
273: }
274: }
275: