notification classes
authorDuck (Jakob Munih) <duck@obala.net>
Thu, 12 Mar 2009 13:25:14 +0000 (14:25 +0100)
committerDuck (Jakob Munih) <duck@obala.net>
Thu, 12 Mar 2009 13:25:14 +0000 (14:25 +0100)
16 files changed:
folks/lib/Notification.php [new file with mode: 0644]
folks/lib/Notification/.svn/all-wcprops [new file with mode: 0644]
folks/lib/Notification/.svn/entries [new file with mode: 0644]
folks/lib/Notification/.svn/format [new file with mode: 0644]
folks/lib/Notification/.svn/prop-base/facebook.php.svn-base [new file with mode: 0644]
folks/lib/Notification/.svn/prop-base/letter.php.svn-base [new file with mode: 0644]
folks/lib/Notification/.svn/prop-base/mail.php.svn-base [new file with mode: 0644]
folks/lib/Notification/.svn/prop-base/tickets.php.svn-base [new file with mode: 0644]
folks/lib/Notification/.svn/text-base/facebook.php.svn-base [new file with mode: 0644]
folks/lib/Notification/.svn/text-base/letter.php.svn-base [new file with mode: 0644]
folks/lib/Notification/.svn/text-base/mail.php.svn-base [new file with mode: 0644]
folks/lib/Notification/.svn/text-base/tickets.php.svn-base [new file with mode: 0644]
folks/lib/Notification/facebook.php [new file with mode: 0644]
folks/lib/Notification/letter.php [new file with mode: 0644]
folks/lib/Notification/mail.php [new file with mode: 0644]
folks/lib/Notification/tickets.php [new file with mode: 0644]

diff --git a/folks/lib/Notification.php b/folks/lib/Notification.php
new file mode 100644 (file)
index 0000000..436c02a
--- /dev/null
@@ -0,0 +1,273 @@
+<?php
+/**
+ * Folks Notification Class.
+ *
+ * $Id: Driver.php 1400 2009-03-09 09:58:40Z duck $
+ *
+ * Copyright Obala d.o.o. (www.obala.si)
+ *
+ * See the enclosed file COPYING for license information (GPL). If you
+ * did not receive this file, see http://www.fsf.org/copyleft/gpl.html.
+ *
+ * @author  Duck <duck@obala.net>
+ * @package Folks
+ */
+class Folks_Notification {
+
+    /**
+     * Instances
+     */
+    static private $instances = array();
+
+    /**
+     * Driver parameters
+     */
+    protected $_params;
+
+    /**
+     * Constructor
+     *
+     * @param array $params   A hash containing any additional configuration
+     *                        or connection parameters a subclass might need.
+     */
+    public function __construct($params = array())
+    {
+        $this->_params = $params;
+    }
+
+    /**
+     * Notify user in all available drivers
+     *
+     * @param string $subject     Subject of message
+     * @param string $body        Body of message
+     * @param array  $attachments Attached files
+     * @param mixed  $user        User or array of users to send notification to
+     *
+     * @return true on succes, PEAR_Error on failure
+     */
+    public function notifyAll($subject, $body, $attachments = array(), $user = null)
+    {
+        $result = false;
+
+        if (empty($user)) {
+            if (Auth::isAuthenticated()) {
+                $user = Auth::getAuth();
+            } else {
+                return true;
+            }
+        }
+
+        foreach ($GLOBALS['conf']['notification'] as $driver => $params) {
+            if ($params['enabled'] && $params['users']) {
+                $instance = $this->singleton($driver, $params);
+                if ($instance instanceof PEAR_Error) {
+                    return $instance;
+                }
+                if (!$instance->isAvailable('users')) {
+                    continue;
+                }
+                $result = $instance->notify($user, $subject, $body, $attachments);
+                if ($result instanceof PEAR_Error) {
+                    return $result;
+                }
+            }
+        }
+
+        return $result;
+    }
+
+    /**
+     * Notify user's friends in all available drivers
+     *
+     * @param mixed  $user        User or array of users to send notification to
+     * @param string $subject     Subject of message
+     * @param string $body        Body of message
+     * @param array  $attachments Attached files
+     * @param string $user        User to send notifications to
+     *
+     * @return true on succes, PEAR_Error on failure
+     */
+    public function notifyAllFriends($subject, $body, $attachments = array(), $user = null)
+    {
+        $result = false;
+
+        if (empty($user)) {
+            if (Auth::isAuthenticated()) {
+                $user = Auth::getAuth();
+            } else {
+                return true;
+            }
+        }
+
+        foreach ($GLOBALS['conf']['notification'] as $driver => $params) {
+            if ($params['enabled'] && $params['friends']) {
+                $instance = $this->singleton($driver, $params);
+                if ($instance instanceof PEAR_Error) {
+                    return $instance;
+                }
+                if (!$instance->isAvailable('friends')) {
+                    continue;
+                }
+                $result = $instance->notifyFriends($user, $subject, $body, $attachments);
+                if ($result instanceof PEAR_Error) {
+                    return $result;
+                }
+            }
+        }
+
+        return $result;
+    }
+
+    /**
+     * Notify user in all available drivers
+     *
+     * @param string $subject     Subject of message
+     * @param string $body        Body of message
+     * @param array  $attachments Attached files
+     *
+     * @return true on succes, PEAR_Error on failure
+     */
+    public function notifyAdmins($subject, $body, $attachments = array())
+    {
+        $result = false;
+
+        $admins = $this->getAdmins();
+        if (empty($admins)) {
+            return true;
+        }
+
+        foreach ($GLOBALS['conf']['notification'] as $driver => $params) {
+            if ($params['enabled'] && $params['admins']) {
+                $instance = $this->singleton($driver, $params);
+                if ($instance instanceof PEAR_Error) {
+                    return $instance;
+                }
+                if (!$instance->isAvailable('admins')) {
+                    continue;
+                }
+                $result = $instance->notify($admins, $subject, $body, $attachments);
+                if ($result instanceof PEAR_Error) {
+                    return $result;
+                }
+            }
+        }
+
+        return $result;
+    }
+
+    /**
+     * Get current scope admins
+     *
+     * @return Array of user with delete permission in "scope:admin" permission
+     */
+    public function getAdmins()
+    {
+        $name = $GLOBALS['registry']->getApp() . ':admin';
+
+        if ($GLOBALS['perms']->exists($name)) {
+            $permission = $GLOBALS['perms']->getPermission($name);
+            if ($permission instanceof PEAR_Error) {
+                return $permission;
+            } else {
+                $admins = $permission->getUserPermissions(PERM_DELETE);
+                if ($admins instanceof PEAR_Error) {
+                    return $admins;
+                }
+            }
+        }
+
+        if (empty($admins)) {
+            return $GLOBALS['conf']['auth']['admins'];
+        } else {
+            return $admins;
+        }
+    }
+
+    /**
+     * Returns all avaiable methods
+     *
+     * @return true on succes, PEAR_Error on failure
+     */
+    public function getMethods()
+    {
+        $methods = array();
+
+        foreach ($GLOBALS['conf']['notification'] as $driver => $params) {
+            if (empty($params['enabled'])) {
+                continue;
+            }
+            $instance = $this->singleton($driver, $params);
+            if ($instance instanceof PEAR_Error) {
+                Horde::logMessage($instance, __FILE__, __LINE__, PEAR_LOG_ERR);
+            } else {
+                $methods[$driver] = $instance->getName();
+            }
+        }
+
+        return $methods;
+    }
+
+    /**
+     * Try to get read user from address
+     *
+     * @param string $user Username
+     *
+     * @return string User email
+     */
+    protected function _getUserFromAddr($user)
+    {
+        require_once 'Horde/Identity.php';
+        $identity = Identity::singleton('none', $user);
+        return $identity->getValue('from_addr');
+    }
+
+    /**
+     * Attempts to return a concrete Folks_Notification instance based on $driver.
+     *
+     * @param string $driver  The type of the concrete Folks_Notification subclass
+     *                        to return.  The class name is based on the
+     *                        storage driver ($driver).  The code is
+     *                        dynamically included.
+     *
+     * @param array $params   A hash containing any additional configuration
+     *                        or connection parameters a subclass might need.
+     *
+     * @return Folks_Notification  The newly created concrete Folks_Notification
+     *                          instance, or false on an error.
+     */
+    static protected function factory($driver, $params = null)
+    {
+        include_once FOLKS_BASE . '/lib/Notification/' . $driver . '.php';
+
+        if ($params === null) {
+            $params = $GLOBALS['conf']['notification'][$driver];
+        }
+
+        $class = 'Folks_Notification_' . $driver;
+        if (class_exists($class)) {
+            return new $class($params);
+        } else {
+            return PEAR::raiseError(sprintf(_("Notification driver %s does not exists."), $driver));
+        }
+    }
+
+    /**
+     * Singleton for driver object
+     *
+     * @param string $driver  The type of the concrete Folks_Notification subclass
+     *                        to return.  The class name is based on the
+     *                        storage Notification ($driver).  The code is
+     *                        dynamically included.
+     *
+     * @param array $params   A hash containing any additional configuration
+     *                        or connection parameters a subclass might need.
+     */
+    static public function singleton($driver, $params = null)
+    {
+        if (!array_key_exists($driver, self::$instances)) {
+            self::$instances[$driver] = self::factory($driver, $params);
+        }
+
+        return self::$instances[$driver];
+    }
+}
\ No newline at end of file
diff --git a/folks/lib/Notification/.svn/all-wcprops b/folks/lib/Notification/.svn/all-wcprops
new file mode 100644 (file)
index 0000000..6a26f4f
--- /dev/null
@@ -0,0 +1,29 @@
+K 25
+svn:wc:ra_dav:version-url
+V 49
+/svn/repos/!svn/ver/1343/folks/lib/Notification
+END
+facebook.php
+K 25
+svn:wc:ra_dav:version-url
+V 62
+/svn/repos/!svn/ver/1469/folks/lib/Notification/facebook.php
+END
+letter.php
+K 25
+svn:wc:ra_dav:version-url
+V 60
+/svn/repos/!svn/ver/1470/folks/lib/Notification/letter.php
+END
+mail.php
+K 25
+svn:wc:ra_dav:version-url
+V 58
+/svn/repos/!svn/ver/1469/folks/lib/Notification/mail.php
+END
+tickets.php
+K 25
+svn:wc:ra_dav:version-url
+V 61
+/svn/repos/!svn/ver/1469/folks/lib/Notification/tickets.php
+END
diff --git a/folks/lib/Notification/.svn/entries b/folks/lib/Notification/.svn/entries
new file mode 100644 (file)
index 0000000..cc50fc8
--- /dev/null
@@ -0,0 +1,164 @@
+9
+
+dir
+1367
+http://svn.obala.si/svn/repos/folks/lib/Notification
+http://svn.obala.si/svn/repos
+
+
+
+2009-03-05T13:25:20.591445Z
+1343
+duck
+
+
+svn:special svn:externals svn:needs-lock
+
+
+
+
+
+
+
+
+
+
+
+64c303f7-8c91-43f2-bf98-80ae1d46e4ff
+\f
+facebook.php
+file
+1469
+
+
+
+2009-03-12T10:53:26.000000Z
+844690b38a65f8179b23c1346190e245
+2009-03-12T10:51:11.048553Z
+1469
+duck
+has-props
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+4394
+\f
+letter.php
+file
+1470
+
+
+
+2009-03-12T10:54:40.000000Z
+9b813aedf152b6d837b0ef06cd490e01
+2009-03-12T10:52:44.969970Z
+1470
+duck
+has-props
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+2400
+\f
+mail.php
+file
+1469
+
+
+
+2009-03-12T10:52:58.000000Z
+f5dd1ba35c8bf2dcad0d3302e2296021
+2009-03-12T10:51:11.048553Z
+1469
+duck
+has-props
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+3138
+\f
+tickets.php
+file
+1469
+
+
+
+2009-03-12T10:53:26.000000Z
+260a1d54eb98aae552e999095f083b7b
+2009-03-12T10:51:11.048553Z
+1469
+duck
+has-props
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+2410
+\f
diff --git a/folks/lib/Notification/.svn/format b/folks/lib/Notification/.svn/format
new file mode 100644 (file)
index 0000000..ec63514
--- /dev/null
@@ -0,0 +1 @@
+9
diff --git a/folks/lib/Notification/.svn/prop-base/facebook.php.svn-base b/folks/lib/Notification/.svn/prop-base/facebook.php.svn-base
new file mode 100644 (file)
index 0000000..7b57b30
--- /dev/null
@@ -0,0 +1,9 @@
+K 13
+svn:eol-style
+V 6
+native
+K 12
+svn:keywords
+V 23
+Author Date Id Revision
+END
diff --git a/folks/lib/Notification/.svn/prop-base/letter.php.svn-base b/folks/lib/Notification/.svn/prop-base/letter.php.svn-base
new file mode 100644 (file)
index 0000000..0f29ce6
--- /dev/null
@@ -0,0 +1,5 @@
+K 2
+Id
+V 3
+set
+END
diff --git a/folks/lib/Notification/.svn/prop-base/mail.php.svn-base b/folks/lib/Notification/.svn/prop-base/mail.php.svn-base
new file mode 100644 (file)
index 0000000..0f29ce6
--- /dev/null
@@ -0,0 +1,5 @@
+K 2
+Id
+V 3
+set
+END
diff --git a/folks/lib/Notification/.svn/prop-base/tickets.php.svn-base b/folks/lib/Notification/.svn/prop-base/tickets.php.svn-base
new file mode 100644 (file)
index 0000000..7b57b30
--- /dev/null
@@ -0,0 +1,9 @@
+K 13
+svn:eol-style
+V 6
+native
+K 12
+svn:keywords
+V 23
+Author Date Id Revision
+END
diff --git a/folks/lib/Notification/.svn/text-base/facebook.php.svn-base b/folks/lib/Notification/.svn/text-base/facebook.php.svn-base
new file mode 100644 (file)
index 0000000..279cc8a
--- /dev/null
@@ -0,0 +1,149 @@
+<?php
+/**
+ * Folks Notification Class.
+ *
+ * $Id$
+ *
+ * Copyright Obala d.o.o. (www.obala.si)
+ *
+ * See the enclosed file COPYING for license information (GPL). If you
+ * did not receive this file, see http://www.fsf.org/copyleft/gpl.html.
+ *
+ * @author  Duck <duck@obala.net>
+ * @package Folks
+ */
+class Folks_Notification_facebook extends Folks_Notification {
+
+    /**
+     * FB object
+     */
+    private $_fb;
+
+    /**
+     * FB connection parameters
+     */
+    private $_fbp;
+
+    /**
+     * Returns method human name
+     */
+    public function getName()
+    {
+        return _("Facebook");
+    }
+
+    /**
+     * Checks if a driver is available for a certain notification type
+     *
+     * @param string $type Notification type
+     *
+     * @return boolean
+     */
+    public function isAvailable($type)
+    {
+        // Check FB installation
+        if (!$GLOBALS['conf']['facebook']['enabled']) {
+            return false;
+        }
+
+        // Chacke FB user config
+        $fbp = unserialize($GLOBALS['prefs']->getValue('facebook'));
+        if (!$fbp || empty($fbp['uid'])) {
+            return false;
+        }
+
+        return true;
+    }
+
+    /**
+     * Notify user
+     *
+     * @param mixed  $user        User or array of users to send notification to
+     * @param string $subject     Subject of message
+     * @param string $body        Body of message
+     * @param array  $attachments Attached files
+     *
+     * @return true on succes, PEAR_Error on failure
+     */
+    public function notify($user, $subject, $body, $attachments = array())
+    {
+        if (!$this->_loadFB()) {
+            return $this->_fb;
+        }
+
+        try {
+            $message = '<strong>' . $subject . ':</strong> ' . $body;
+            $result = $this->_fb->notifications->send(array($this->_fbp['uid']), $message, 'user_to_user');
+        } catch (Horde_Service_Facebook_Exception $e) {
+            return PEAR::raiseError($e->getMessage(), $e->getCode());
+        }
+
+        return $result;
+    }
+
+    /**
+     * Notify user
+     *
+     * @param mixed  $user        User or array of users to send notification to
+     * @param string $subject     Subject of message
+     * @param string $body        Body of message
+     * @param array  $attachments Attached files
+     *
+     * @return true on succes, PEAR_Error on failure
+     */
+    public function notifyFriends($user, $subject, $body, $attachments = array())
+    {
+        if (!$this->_loadFB()) {
+            return $this->_fb;
+        }
+
+        try {
+            $friends = $this->_fb->friends->get(null, $this->_fbp['uid']);
+        } catch (Horde_Service_Facebook_Exception $e) {
+            return PEAR::raiseError($e->getMessage(), $e->getCode());
+        }
+
+        try {
+            $message = '<strong>' . $subject . ':</strong> ' . $body;
+            $result = $this->_fb->notifications->send($friends, $message, 'user_to_user');
+        } catch (Horde_Service_Facebook_Exception $e) {
+            return PEAR::raiseError($e->getMessage(), $e->getCode());
+        }
+
+        return $result;
+    }
+
+    /**
+     * Load FB content
+     */
+    private function _loadFB()
+    {
+        if ($this->_fb) {
+            return true;
+        }
+
+        // Check FB installation
+        if (!$GLOBALS['conf']['facebook']['enabled']) {
+            $this->_fb = PEAR::raiseError(_("No Facebook integration exists."));
+            return false;
+        }
+
+        // Check FB user config
+        $this->_fbp = unserialize($GLOBALS['prefs']->getValue('facebook'));
+        if (!$this->_fbp || empty($this->_fbp['uid'])) {
+            $this->_fb = PEAR::raiseError(sprintf(_("Could not find authorization for %s to interact with your Facebook account."), $GLOBALS['registry']->get('name', 'horde')));
+            return false;
+        }
+
+        // Create FB Object
+        $this->_fb = new Horde_Service_Facebook($GLOBALS['conf']['facebook']['key'],
+                                                $GLOBALS['conf']['facebook']['secret'],
+                                                array('http_client' => new Horde_Http_Client(),
+                                                      'http_request' => new Horde_Controller_Request_Http()));
+
+        // Set Auth user
+        $this->_fb->auth->setUser($this->_fbp['uid'], $this->_fbp['sid'], 0);
+
+        return true;
+    }
+}
diff --git a/folks/lib/Notification/.svn/text-base/letter.php.svn-base b/folks/lib/Notification/.svn/text-base/letter.php.svn-base
new file mode 100644 (file)
index 0000000..9569127
--- /dev/null
@@ -0,0 +1,83 @@
+<?php
+/**
+ * Folks Notification Class.
+ *
+ * $Id: Driver.php 1400 2009-03-09 09:58:40Z duck $
+ *
+ * Copyright Obala d.o.o. (www.obala.si)
+ *
+ * See the enclosed file COPYING for license information (GPL). If you
+ * did not receive this file, see http://www.fsf.org/copyleft/gpl.html.
+ *
+ * @author  Duck <duck@obala.net>
+ * @package Folks
+ */
+class Folks_Notification_letter extends Folks_Notification {
+
+    /**
+     * Returns method human name
+     */
+    public function getName()
+    {
+        return $GLOBALS['registry']->get('name', 'letter');
+    }
+
+    /**
+     * Checks if a driver is available for a certain notification type
+     *
+     * @param string $type Notification type
+     *
+     * @return boolean
+     */
+    public function isAvailable($type)
+    {
+        if ($type == 'friends') {
+            return $GLOBALS['registry']->hasMethod('users/getFriends');
+        }
+
+        return true;
+    }
+
+    /**
+     * Notify user
+     *
+     * @param mixed  $user        User or array of users to send notification to
+     * @param string $subject     Subject of message
+     * @param string $body        Body of message
+     * @param array  $attachments Attached files
+     *
+     * @return true on succes, PEAR_Error on failure
+     */
+    public function notify($user, $subject, $body, $attachments = array())
+    {
+        if (empty($user)) {
+            return true;
+        }
+
+        return $GLOBALS['registry']->callByPackage(
+            'letter', 'sendMessage', array($user,
+                                           array('title' => $subject,
+                                                 'content' => $body,
+                                                 'attachments' => $attachments)));
+    }
+
+    /**
+     * Notify user's friends
+     *
+     * @param mixed  $user        User or array of users to send notification to
+     * @param string $subject     Subject of message
+     * @param string $body        Body of message
+     * @param array  $attachments Attached files
+     *
+     * @return true on succes, PEAR_Error on failure
+     */
+    public function notifyFriends($user, $subject, $body, $attachments = array())
+    {
+        $friends = $GLOBALS['registry']->call('users/getFriends');
+        if ($friends instanceof PEAR_Error) {
+            return $friends;
+        }
+
+        return $this->notify($friends, $subject, $body, $attachments);
+    }
+}
diff --git a/folks/lib/Notification/.svn/text-base/mail.php.svn-base b/folks/lib/Notification/.svn/text-base/mail.php.svn-base
new file mode 100644 (file)
index 0000000..ab8b469
--- /dev/null
@@ -0,0 +1,109 @@
+<?php
+/**
+ * Folks Notification Class.
+ *
+ * $Id: Driver.php 1400 2009-03-09 09:58:40Z duck $
+ *
+ * Copyright Obala d.o.o. (www.obala.si)
+ *
+ * See the enclosed file COPYING for license information (GPL). If you
+ * did not receive this file, see http://www.fsf.org/copyleft/gpl.html.
+ *
+ * @author  Duck <duck@obala.net>
+ * @package Folks
+ */
+class Folks_Notification_mail extends Folks_Notification {
+
+    /**
+     * Returns method human name
+     */
+    public function getName()
+    {
+        return _("E-mail");
+    }
+
+    /**
+     * Checks if a driver is available for a certain notification type
+     *
+     * @param string $type Notification type
+     *
+     * @return boolean
+     */
+    public function isAvailable($type)
+    {
+        if ($type == 'friends') {
+            return $GLOBALS['registry']->hasMethod('users/getFriends');
+        }
+
+        return true;
+    }
+
+    /**
+     * Notify user
+     *
+     * @param mixed  $user        User or array of users to send notification to
+     * @param string $subject     Subject of message
+     * @param string $body        Body of message
+     * @param array  $attachments Attached files
+     *
+     * @return true on succes, PEAR_Error on failure
+     */
+    public function notify($user, $subject, $body, $attachments = array())
+    {
+        if (empty($user)) {
+            return true;
+        }
+
+        list($mail_driver, $mail_params) = Horde::getMailerConfig();
+        require_once FOLKS_BASE . '/lib/version.php';
+
+        $mail = new Horde_Mime_Mail($subject, $body, null,
+                                    $this->_params['from_addr'],
+                                    NLS::getCharset());
+
+        $mail->addHeader('User-Agent', 'Folks ' . FOLKS_VERSION);
+        $mail->addHeader('X-Originating-IP', $_SERVER['REMOTE_ADDR']);
+        $mail->addHeader('X-Remote-Browser', $_SERVER['HTTP_USER_AGENT']);
+
+        foreach ($attachments as $file) {
+            if (file_exists($file)) {
+                $mail->addAttachment($file, null, null, NLS::getCharset());
+            }
+        }
+
+        if (is_string($user)) {
+            $user = array($user);
+        }
+
+        foreach ($user as $recipent) {
+            $to = $this->_getUserFromAddr($recipent);
+            if (empty($to)) {
+                continue;
+            }
+            $mail->addHeader('To', $to, NLS::getCharset(), true);
+            $mail->send($mail_driver, $mail_params);
+        }
+
+        return true;
+    }
+
+    /**
+     * Notify user's friends
+     *
+     * @param mixed  $user        User or array of users to send notification to
+     * @param string $subject     Subject of message
+     * @param string $body        Body of message
+     * @param array  $attachments Attached files
+     *
+     * @return true on succes, PEAR_Error on failure
+     */
+    public function notifyFriends($user, $subject, $body, $attachments = array())
+    {
+        $friends = $GLOBALS['registry']->call('users/getFriends');
+        if ($friends instanceof PEAR_Error) {
+            return $friends;
+        }
+
+        return $this->notify($friends, $subject, $body, $attachments);
+    }
+}
diff --git a/folks/lib/Notification/.svn/text-base/tickets.php.svn-base b/folks/lib/Notification/.svn/text-base/tickets.php.svn-base
new file mode 100644 (file)
index 0000000..f813207
--- /dev/null
@@ -0,0 +1,84 @@
+<?php
+/**
+ * Folks Notification Class.
+ *
+ * $Id$
+ *
+ * Copyright Obala d.o.o. (www.obala.si)
+ *
+ * See the enclosed file COPYING for license information (GPL). If you
+ * did not receive this file, see http://www.fsf.org/copyleft/gpl.html.
+ *
+ * @author  Duck <duck@obala.net>
+ * @package Folks
+ */
+class Folks_Notification_tickets extends Folks_Notification {
+
+    /**
+     * Returns method human name
+     */
+    public function getName()
+    {
+        return $GLOBALS['registry']->get('name', 'whups');
+    }
+
+    /**
+     * Checks if a driver is available for a certain notification type
+     *
+     * @param string $type Notification type
+     *
+     * @return boolean
+     */
+    public function isAvailable($type)
+    {
+        if (!$GLOBALS['registry']->hasInterface('tickets') ||
+                $type == 'admins') {
+            return false;
+        }
+
+        return false;
+    }
+
+    /**
+     * Notify user
+     *
+     * @param mixed  $user        User or array of users to send notification to
+     * @param string $subject     Subject of message
+     * @param string $body        Body of message
+     * @param array  $attachments Attached files
+     *
+     * @return true on succes, PEAR_Error on failure
+     */
+    public function notify($user, $subject, $body, $attachments = array())
+    {
+        global $registry;
+
+        $info = array_merge($this->_params['ticket_params'],
+                            array('summary' => $subject,
+                                    'comment' => $body,
+                                    'user_email' => $this->_getUserFromAddr()));
+
+        $ticket_id = $registry->call('tickets/addTicket', array($info));
+        if ($ticket_id instanceof PEAR_Error) {
+            return $ticket_id;
+        }
+
+        if (empty($attachments) ||
+            !$registry->hasMethod('tickets/addAttachment')) {
+            return $result;
+        }
+
+        foreach ($attachments as $attachment) {
+            $result = $registry->call(
+                'tickets/addAttachment',
+                        array('ticket_id' => $ticket_id,
+                                'name' => $attachment['name'],
+                                'data' => file_get_contents($attachment['file'])));
+            if ($result instanceof PEAR_Error) {
+                return $result;
+            }
+        }
+
+        return true;
+    }
+}
diff --git a/folks/lib/Notification/facebook.php b/folks/lib/Notification/facebook.php
new file mode 100644 (file)
index 0000000..bdd6697
--- /dev/null
@@ -0,0 +1,149 @@
+<?php
+/**
+ * Folks Notification Class.
+ *
+ * $Id: facebook.php 1469 2009-03-12 10:51:11Z duck $
+ *
+ * Copyright Obala d.o.o. (www.obala.si)
+ *
+ * See the enclosed file COPYING for license information (GPL). If you
+ * did not receive this file, see http://www.fsf.org/copyleft/gpl.html.
+ *
+ * @author  Duck <duck@obala.net>
+ * @package Folks
+ */
+class Folks_Notification_facebook extends Folks_Notification {
+
+    /**
+     * FB object
+     */
+    private $_fb;
+
+    /**
+     * FB connection parameters
+     */
+    private $_fbp;
+
+    /**
+     * Returns method human name
+     */
+    public function getName()
+    {
+        return _("Facebook");
+    }
+
+    /**
+     * Checks if a driver is available for a certain notification type
+     *
+     * @param string $type Notification type
+     *
+     * @return boolean
+     */
+    public function isAvailable($type)
+    {
+        // Check FB installation
+        if (!$GLOBALS['conf']['facebook']['enabled']) {
+            return false;
+        }
+
+        // Chacke FB user config
+        $fbp = unserialize($GLOBALS['prefs']->getValue('facebook'));
+        if (!$fbp || empty($fbp['uid'])) {
+            return false;
+        }
+
+        return true;
+    }
+
+    /**
+     * Notify user
+     *
+     * @param mixed  $user        User or array of users to send notification to
+     * @param string $subject     Subject of message
+     * @param string $body        Body of message
+     * @param array  $attachments Attached files
+     *
+     * @return true on succes, PEAR_Error on failure
+     */
+    public function notify($user, $subject, $body, $attachments = array())
+    {
+        if (!$this->_loadFB()) {
+            return $this->_fb;
+        }
+
+        try {
+            $message = '<strong>' . $subject . ':</strong> ' . $body;
+            $result = $this->_fb->notifications->send(array($this->_fbp['uid']), $message, 'user_to_user');
+        } catch (Horde_Service_Facebook_Exception $e) {
+            return PEAR::raiseError($e->getMessage(), $e->getCode());
+        }
+
+        return $result;
+    }
+
+    /**
+     * Notify user
+     *
+     * @param mixed  $user        User or array of users to send notification to
+     * @param string $subject     Subject of message
+     * @param string $body        Body of message
+     * @param array  $attachments Attached files
+     *
+     * @return true on succes, PEAR_Error on failure
+     */
+    public function notifyFriends($user, $subject, $body, $attachments = array())
+    {
+        if (!$this->_loadFB()) {
+            return $this->_fb;
+        }
+
+        try {
+            $friends = $this->_fb->friends->get(null, $this->_fbp['uid']);
+        } catch (Horde_Service_Facebook_Exception $e) {
+            return PEAR::raiseError($e->getMessage(), $e->getCode());
+        }
+
+        try {
+            $message = '<strong>' . $subject . ':</strong> ' . $body;
+            $result = $this->_fb->notifications->send($friends, $message, 'user_to_user');
+        } catch (Horde_Service_Facebook_Exception $e) {
+            return PEAR::raiseError($e->getMessage(), $e->getCode());
+        }
+
+        return $result;
+    }
+
+    /**
+     * Load FB content
+     */
+    private function _loadFB()
+    {
+        if ($this->_fb) {
+            return true;
+        }
+
+        // Check FB installation
+        if (!$GLOBALS['conf']['facebook']['enabled']) {
+            $this->_fb = PEAR::raiseError(_("No Facebook integration exists."));
+            return false;
+        }
+
+        // Check FB user config
+        $this->_fbp = unserialize($GLOBALS['prefs']->getValue('facebook'));
+        if (!$this->_fbp || empty($this->_fbp['uid'])) {
+            $this->_fb = PEAR::raiseError(sprintf(_("Could not find authorization for %s to interact with your Facebook account."), $GLOBALS['registry']->get('name', 'horde')));
+            return false;
+        }
+
+        // Create FB Object
+        $this->_fb = new Horde_Service_Facebook($GLOBALS['conf']['facebook']['key'],
+                                                $GLOBALS['conf']['facebook']['secret'],
+                                                array('http_client' => new Horde_Http_Client(),
+                                                      'http_request' => new Horde_Controller_Request_Http()));
+
+        // Set Auth user
+        $this->_fb->auth->setUser($this->_fbp['uid'], $this->_fbp['sid'], 0);
+
+        return true;
+    }
+}
diff --git a/folks/lib/Notification/letter.php b/folks/lib/Notification/letter.php
new file mode 100644 (file)
index 0000000..9569127
--- /dev/null
@@ -0,0 +1,83 @@
+<?php
+/**
+ * Folks Notification Class.
+ *
+ * $Id: Driver.php 1400 2009-03-09 09:58:40Z duck $
+ *
+ * Copyright Obala d.o.o. (www.obala.si)
+ *
+ * See the enclosed file COPYING for license information (GPL). If you
+ * did not receive this file, see http://www.fsf.org/copyleft/gpl.html.
+ *
+ * @author  Duck <duck@obala.net>
+ * @package Folks
+ */
+class Folks_Notification_letter extends Folks_Notification {
+
+    /**
+     * Returns method human name
+     */
+    public function getName()
+    {
+        return $GLOBALS['registry']->get('name', 'letter');
+    }
+
+    /**
+     * Checks if a driver is available for a certain notification type
+     *
+     * @param string $type Notification type
+     *
+     * @return boolean
+     */
+    public function isAvailable($type)
+    {
+        if ($type == 'friends') {
+            return $GLOBALS['registry']->hasMethod('users/getFriends');
+        }
+
+        return true;
+    }
+
+    /**
+     * Notify user
+     *
+     * @param mixed  $user        User or array of users to send notification to
+     * @param string $subject     Subject of message
+     * @param string $body        Body of message
+     * @param array  $attachments Attached files
+     *
+     * @return true on succes, PEAR_Error on failure
+     */
+    public function notify($user, $subject, $body, $attachments = array())
+    {
+        if (empty($user)) {
+            return true;
+        }
+
+        return $GLOBALS['registry']->callByPackage(
+            'letter', 'sendMessage', array($user,
+                                           array('title' => $subject,
+                                                 'content' => $body,
+                                                 'attachments' => $attachments)));
+    }
+
+    /**
+     * Notify user's friends
+     *
+     * @param mixed  $user        User or array of users to send notification to
+     * @param string $subject     Subject of message
+     * @param string $body        Body of message
+     * @param array  $attachments Attached files
+     *
+     * @return true on succes, PEAR_Error on failure
+     */
+    public function notifyFriends($user, $subject, $body, $attachments = array())
+    {
+        $friends = $GLOBALS['registry']->call('users/getFriends');
+        if ($friends instanceof PEAR_Error) {
+            return $friends;
+        }
+
+        return $this->notify($friends, $subject, $body, $attachments);
+    }
+}
diff --git a/folks/lib/Notification/mail.php b/folks/lib/Notification/mail.php
new file mode 100644 (file)
index 0000000..ab8b469
--- /dev/null
@@ -0,0 +1,109 @@
+<?php
+/**
+ * Folks Notification Class.
+ *
+ * $Id: Driver.php 1400 2009-03-09 09:58:40Z duck $
+ *
+ * Copyright Obala d.o.o. (www.obala.si)
+ *
+ * See the enclosed file COPYING for license information (GPL). If you
+ * did not receive this file, see http://www.fsf.org/copyleft/gpl.html.
+ *
+ * @author  Duck <duck@obala.net>
+ * @package Folks
+ */
+class Folks_Notification_mail extends Folks_Notification {
+
+    /**
+     * Returns method human name
+     */
+    public function getName()
+    {
+        return _("E-mail");
+    }
+
+    /**
+     * Checks if a driver is available for a certain notification type
+     *
+     * @param string $type Notification type
+     *
+     * @return boolean
+     */
+    public function isAvailable($type)
+    {
+        if ($type == 'friends') {
+            return $GLOBALS['registry']->hasMethod('users/getFriends');
+        }
+
+        return true;
+    }
+
+    /**
+     * Notify user
+     *
+     * @param mixed  $user        User or array of users to send notification to
+     * @param string $subject     Subject of message
+     * @param string $body        Body of message
+     * @param array  $attachments Attached files
+     *
+     * @return true on succes, PEAR_Error on failure
+     */
+    public function notify($user, $subject, $body, $attachments = array())
+    {
+        if (empty($user)) {
+            return true;
+        }
+
+        list($mail_driver, $mail_params) = Horde::getMailerConfig();
+        require_once FOLKS_BASE . '/lib/version.php';
+
+        $mail = new Horde_Mime_Mail($subject, $body, null,
+                                    $this->_params['from_addr'],
+                                    NLS::getCharset());
+
+        $mail->addHeader('User-Agent', 'Folks ' . FOLKS_VERSION);
+        $mail->addHeader('X-Originating-IP', $_SERVER['REMOTE_ADDR']);
+        $mail->addHeader('X-Remote-Browser', $_SERVER['HTTP_USER_AGENT']);
+
+        foreach ($attachments as $file) {
+            if (file_exists($file)) {
+                $mail->addAttachment($file, null, null, NLS::getCharset());
+            }
+        }
+
+        if (is_string($user)) {
+            $user = array($user);
+        }
+
+        foreach ($user as $recipent) {
+            $to = $this->_getUserFromAddr($recipent);
+            if (empty($to)) {
+                continue;
+            }
+            $mail->addHeader('To', $to, NLS::getCharset(), true);
+            $mail->send($mail_driver, $mail_params);
+        }
+
+        return true;
+    }
+
+    /**
+     * Notify user's friends
+     *
+     * @param mixed  $user        User or array of users to send notification to
+     * @param string $subject     Subject of message
+     * @param string $body        Body of message
+     * @param array  $attachments Attached files
+     *
+     * @return true on succes, PEAR_Error on failure
+     */
+    public function notifyFriends($user, $subject, $body, $attachments = array())
+    {
+        $friends = $GLOBALS['registry']->call('users/getFriends');
+        if ($friends instanceof PEAR_Error) {
+            return $friends;
+        }
+
+        return $this->notify($friends, $subject, $body, $attachments);
+    }
+}
diff --git a/folks/lib/Notification/tickets.php b/folks/lib/Notification/tickets.php
new file mode 100644 (file)
index 0000000..bc6fe94
--- /dev/null
@@ -0,0 +1,84 @@
+<?php
+/**
+ * Folks Notification Class.
+ *
+ * $Id: tickets.php 1469 2009-03-12 10:51:11Z duck $
+ *
+ * Copyright Obala d.o.o. (www.obala.si)
+ *
+ * See the enclosed file COPYING for license information (GPL). If you
+ * did not receive this file, see http://www.fsf.org/copyleft/gpl.html.
+ *
+ * @author  Duck <duck@obala.net>
+ * @package Folks
+ */
+class Folks_Notification_tickets extends Folks_Notification {
+
+    /**
+     * Returns method human name
+     */
+    public function getName()
+    {
+        return $GLOBALS['registry']->get('name', 'whups');
+    }
+
+    /**
+     * Checks if a driver is available for a certain notification type
+     *
+     * @param string $type Notification type
+     *
+     * @return boolean
+     */
+    public function isAvailable($type)
+    {
+        if (!$GLOBALS['registry']->hasInterface('tickets') ||
+                $type == 'admins') {
+            return false;
+        }
+
+        return false;
+    }
+
+    /**
+     * Notify user
+     *
+     * @param mixed  $user        User or array of users to send notification to
+     * @param string $subject     Subject of message
+     * @param string $body        Body of message
+     * @param array  $attachments Attached files
+     *
+     * @return true on succes, PEAR_Error on failure
+     */
+    public function notify($user, $subject, $body, $attachments = array())
+    {
+        global $registry;
+
+        $info = array_merge($this->_params['ticket_params'],
+                            array('summary' => $subject,
+                                    'comment' => $body,
+                                    'user_email' => $this->_getUserFromAddr()));
+
+        $ticket_id = $registry->call('tickets/addTicket', array($info));
+        if ($ticket_id instanceof PEAR_Error) {
+            return $ticket_id;
+        }
+
+        if (empty($attachments) ||
+            !$registry->hasMethod('tickets/addAttachment')) {
+            return $result;
+        }
+
+        foreach ($attachments as $attachment) {
+            $result = $registry->call(
+                'tickets/addAttachment',
+                        array('ticket_id' => $ticket_id,
+                                'name' => $attachment['name'],
+                                'data' => file_get_contents($attachment['file'])));
+            if ($result instanceof PEAR_Error) {
+                return $result;
+            }
+        }
+
+        return true;
+    }
+}