. // Copyright © 2007-2014 Erwan Briand // // 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 . /** * @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 (optional) * The default lang to be restored (can be ommited if redirect). */ 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'))) { // Get the locale for this language $locale = getClass('locales'); $countries = $locale->getCountries($user->getLang()); foreach ($countries as $country) if ($lcall = $locale->isSupported($user->getLang().'_'.$country)) $visitor_locale = $lcall; // Change locale settings setlocale(LC_ALL, $visitor_locale); $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); } // 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; $cfg = getClass('config', $this->ct_db); require_once(CT_BASEDIR.'/inc/libs/xmpphp/XMPP.php'); $jab = new XMPPHP_XMPP($cfg->get('jabber', 'server'), 5222, $cfg->get('jabber', 'username'), $cfg->get('jabber', 'password'), 'NOTIFBOT'); $jab->connect(); $jab->processUntil('session_start'); $jab->presence(); foreach ($notifs as $notif) { $id = $notif['id']; $jabberid = $notif['jabberid']; $subject = $notif['subject']; $text = $notif['notification']; $res = $jab->message($jabberid, $text, 'normal', $subject); $this->ct_db->delete('notifications', array('id' => $id)); } $jab->disconnect(); } } ?>