websiteWebsite
codingteam CodingTeam
A free forge, lightweight and extensible.

 

Browse the code

Revision log Information on the revision
Revision: 152 (differences)
Author: xbright
Log message: * A few bugfixes and improvements
Change revision:
<?php
# ***** BEGIN LICENSE BLOCK *****
#
#    This file is a part of CodingTeam. See <http://www.codingteam.net>.
#    Copyright (C) 2007-2008 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/>.
#
# ***** END LICENSE BLOCK *****
 
/**
 * @file
 * This file contains the Notification class
 *
 * Send notifications to users via Jabber or mail.
 */
 
 
/**
 * Notification class
 */
class Notification {
    private $db;
    
    function __construct ($db)
    {
        $this->db = $db;
    }
 
    /**
     * Send
     *
     * Send the notification to an user
     * @param $userid
     *   The identifier of the user.
     * @param $text
     *   The text to notify.
     * @param $defaultlang
     *   The default lang to be restored.
     */
    function send ($userid, $text, $defaultlang)
    {
        // Get the user
        $user = getUser($userid, $this->db);
        $type = $user->getNotif();
 
        if (in_array($type, array('jabber', 'mail')))
        {
            // Change locale settings
            putenv('LANG='.$user->getLang().'.UTF-8');
            setlocale(LC_ALL, $user->getLang().'.UTF-8');
 
            // Send the notification via Jabber
            if ($type == 'jabber')
            {
                $cfg = getClass('config', $this->db);
 
                // Use the XML-RPC librairy to speak to the bot
                require($basedir.'/inc/libs/xmlrpc/xmlrpc.inc');
 
                // Start a client connection
                $host = $cfg->get('fenrir', 'xmlrpc-host').':'.$cfg->get('fenrir', 'xmlrpc-port');
                $client = new xmlrpc_client($host);
                $message = new xmlrpcmsg('send', array(new xmlrpcval(_('CodingTeam notification'), 'string'),
                                                       new xmlrpcval($text, 'string'),
                                                       new xmlrpcval($user->getJid(), 'string'),
                                                       new xmlrpcval($cfg->get('fenrir', 'xmlrpc-password'), 'string')
                                                      ));
 
                // Send the notification
                $resp = $client->send($message);
                if (!$resp->faultCode())
                    return TRUE;
            }
            // Send the notification via mail
            elseif ($type == 'mail')
            {
                if (sendmail(_('CodingTeam notification'), $text, $user->getEmail(), $this->db))
                    return TRUE;
            }
 
            // Restore local settings
            putenv('LANG='.$this->lang.'.UTF-8');
            setlocale(LC_ALL, $this->lang.'.UTF-8');
        }
 
        // Return FALSE if we go here
        return FALSE;
    }
}
?>