websiteWebsite
codingteam CodingTeam
A free forge, lightweight and extensible.

 

Browse the code

Revision log Information on the revision
Revision: 361 (differences)
Author: xbright
Log message: * Added a notification system (supports email and XMPP), fixes #286
* Can send notifications on private messages, bugs activities and forum answers
Change revision:
<?php
#    This file is a part of CodingTeam. See <http://www.codingteam.net>.
#    Copyright (C) 2007-2010 CodingTeam (See AUTHORS and THANKS for details)
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU Affero General Public License as
#    published by the Free Software Foundation, version 3 only.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU Affero General Public License for more details.
#
#    You should have received a copy of the GNU Affero General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
/**
 * @file
 * This file contains the Notification class
 *
 * Send notifications to users via Jabber or mail.
 */
 
 
/**
 * Notification class
 */
class Notifications {
    private $db;
 
    function __construct ($db)
    {
        $this->ct_db = $db;
    }
 
 
    /**
     * Send
     *
     * Send the notification to an user. Subject and notification have to be
     * untranslated.
     * @param $userid
     *   The identifier of the user.
     * @param $subject
     *   An array that contains subject of the notification and args.
     * @param $text
     *   An array that contains text to notify and args.
     * @param $defaultlang
     *   The default lang to be restored.
     */
    public function send($userid, $_subject, $_text, $defaultlang=FALSE)
    {
        // Get the user
        $user = getUser($userid, $this->ct_db);
        $type = $user->getNotifs();
 
        if ($type == 'no')
            return FALSE;
 
        if (in_array($type, array('xmpp', 'mail')))
        {
            // Change locale settings
            setlocale(LC_ALL, $user->getLang().'.UTF-8');
 
            $subject = i18n($_subject[0], $_subject[1]);
            $text = i18n($_text[0], $_text[1]);
 
            // Send the notification via Jabber
            if ($type == 'xmpp')
            {
                $jabberid = $user->getJid();
 
                if (empty($jabberid))
                    return FALSE;
 
                $this->ct_db->insert('notifications',
                                     array('jabberid'     => $jabberid,
                                           'subject'      => $subject,
                                           'notification' => $text));
            }
            // Send the notification via mail
            elseif ($type == 'mail')
            {
                sendmail($subject, $text, $user->getEmail(), $this->ct_db);
            }
 
            // Restore local settings
            if ($defaultlang)
                setlocale(LC_ALL, $defaultlang.'.UTF-8');
        }
 
        // Return FALSE if we go here
        return FALSE;
    }
 
 
    /**
     * Periodic send
     *
     * Send XMPP notifications.
     */
    public function periodic_send()
    {
        $notifs = $this->ct_db->select('notifications', FALSE);
        if (count($notifs) == 0)
            return FALSE;
 
        set_time_limit(0);
        $cfg = getClass('config', $this->ct_db);
 
        require_once(CT_BASEDIR.'/inc/libs/class.jabber.php');
 
        $jab = new JabberPHP;
        $jab->server = $cfg->get('jabber', 'server');
        $jab->port = 5222;
        $jab->username = $cfg->get('jabber', 'username');
        $jab->password = $cfg->get('jabber', 'password');
        $jab->resource = 'NOTIFBOT';
 
        $jab->Connect();
        $jab->SendAuth();
 
        foreach ($notifs as $notif)
        {
            $id = $notif['id'];
            $jabberid = $notif['jabberid'];
            $subject = $notif['subject'];
            $text = $notif['notification'];
 
            $content = array('subject' => $subject,
                             'body'    => $text);
 
            $res = $jab->SendMessage($jabberid, 'normal', NULL, $content);
 
            if ($res)
                $this->ct_db->delete('notifications', array('id' => $id));
        }
 
        $jab->Disconnect();
    }
}
?>