Overview

Packages

  • Folks
  • None

Classes

  • Folks
  • Folks_Activity_Form
  • Folks_Api
  • Folks_Application
  • Folks_Block_Activities
  • Folks_Block_Friends
  • Folks_Block_Know
  • Folks_Block_New
  • Folks_Block_Random
  • Folks_Block_Recent
  • Folks_Driver
  • Folks_Driver_sql
  • Folks_Friends
  • Folks_Friends_application
  • Folks_Friends_facebook
  • Folks_Friends_prefs
  • Folks_Friends_shared
  • Folks_Friends_sql
  • Folks_Login_Form
  • Folks_Notification
  • Folks_Notification_facebook
  • Folks_Notification_letter
  • Folks_Notification_mail
  • Folks_Notification_tickets
  • Folks_Search_Form
  • Overview
  • Package
  • Class
  • Tree
  1: <?php
  2: /**
  3:  * Folks Notification Class.
  4:  *
  5:  * Copyright Obala d.o.o. (www.obala.si)
  6:  *
  7:  * See the enclosed file COPYING for license information (GPL). If you
  8:  * did not receive this file, see http://www.horde.org/licenses/gpl.
  9:  *
 10:  * @author  Duck <duck@obala.net>
 11:  * @package Folks
 12:  */
 13: class Folks_Notification {
 14: 
 15:     /**
 16:      * Instances
 17:      */
 18:     static private $instances = array();
 19: 
 20:     /**
 21:      * Driver parameters
 22:      */
 23:     protected $_params;
 24: 
 25:     /**
 26:      * Constructor
 27:      *
 28:      * @param array $params   A hash containing any additional configuration
 29:      *                        or connection parameters a subclass might need.
 30:      */
 31:     public function __construct($params = array())
 32:     {
 33:         $this->_params = $params;
 34:     }
 35: 
 36:     /**
 37:      * Notify user in all available drivers
 38:      *
 39:      * @param string $subject     Subject of message
 40:      * @param string $body        Body of message
 41:      * @param array  $attachments Attached files
 42:      * @param mixed  $user        User or array of users to send notification to
 43:      *
 44:      * @return true on succes, PEAR_Error on failure
 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:      * Notify user's friends in all available drivers
 79:      *
 80:      * @param mixed  $user        User or array of users to send notification to
 81:      * @param string $subject     Subject of message
 82:      * @param string $body        Body of message
 83:      * @param array  $attachments Attached files
 84:      * @param string $user        User to send notifications to
 85:      *
 86:      * @return true on succes, PEAR_Error on failure
 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:      * Notify user in all available drivers
121:      *
122:      * @param string $subject     Subject of message
123:      * @param string $body        Body of message
124:      * @param array  $attachments Attached files
125:      *
126:      * @return true on succes, PEAR_Error on failure
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:      * Get current scope admins
158:      *
159:      * @return Array of user with delete permission in "scope:admin" permission
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:      * Returns all avaiable methods
187:      *
188:      * @param string $type Type of notification to check
189:      *
190:      * @return true on succes, PEAR_Error on failure
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:      * Try to get read user from address
215:      *
216:      * @param string $user Username
217:      *
218:      * @return string User email
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:      * Attempts to return a concrete Folks_Notification instance based on $driver.
227:      *
228:      * @param string $driver  The type of the concrete Folks_Notification subclass
229:      *                        to return.  The class name is based on the
230:      *                        storage driver ($driver).  The code is
231:      *                        dynamically included.
232:      *
233:      * @param array $params   A hash containing any additional configuration
234:      *                        or connection parameters a subclass might need.
235:      *
236:      * @return Folks_Notification  The newly created concrete Folks_Notification
237:      *                          instance, or false on an error.
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:      * Singleton for driver object
257:      *
258:      * @param string $driver  The type of the concrete Folks_Notification subclass
259:      *                        to return.  The class name is based on the
260:      *                        storage Notification ($driver).  The code is
261:      *                        dynamically included.
262:      *
263:      * @param array $params   A hash containing any additional configuration
264:      *                        or connection parameters a subclass might need.
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: 
API documentation generated by ApiGen