2007-12-18 16:53:38 +01:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* eGroupWare - Notifications
|
|
|
|
*
|
|
|
|
* @license http://opensource.org/licenses/gpl-license.php GPL - GNU General Public License
|
|
|
|
* @package notifications
|
|
|
|
* @subpackage ajaxpopup
|
|
|
|
* @link http://www.egroupware.org
|
|
|
|
* @author Cornelius Weiss <nelius@cwtech.de>, Christian Binder <christian@jaytraxx.de>
|
|
|
|
*/
|
2009-04-28 22:24:05 +02:00
|
|
|
|
2007-12-18 16:53:38 +01:00
|
|
|
/**
|
|
|
|
* Ajax methods for notifications
|
|
|
|
*/
|
2009-04-03 22:36:28 +02:00
|
|
|
class notifications_ajax {
|
2007-12-18 16:53:38 +01:00
|
|
|
/**
|
|
|
|
* Appname
|
|
|
|
*/
|
|
|
|
const _appname = 'notifications';
|
2009-04-28 22:24:05 +02:00
|
|
|
|
2009-02-11 20:32:26 +01:00
|
|
|
/**
|
|
|
|
* Mailappname
|
|
|
|
*/
|
|
|
|
const _mailappname = 'felamimail';
|
2009-04-28 22:24:05 +02:00
|
|
|
|
2007-12-18 16:53:38 +01:00
|
|
|
/**
|
|
|
|
* Notification table in SQL database
|
|
|
|
*/
|
|
|
|
const _notification_table = 'egw_notificationpopup';
|
2009-04-28 22:24:05 +02:00
|
|
|
|
2007-12-18 16:53:38 +01:00
|
|
|
/**
|
|
|
|
* holds account object for user to notify
|
|
|
|
*
|
|
|
|
* @var object
|
|
|
|
*/
|
|
|
|
private $recipient;
|
2009-04-28 22:24:05 +02:00
|
|
|
|
2007-12-18 16:53:38 +01:00
|
|
|
/**
|
|
|
|
* holds config object (sitewide application config)
|
|
|
|
*
|
|
|
|
* @var object
|
|
|
|
*/
|
|
|
|
private $config;
|
2009-04-28 22:24:05 +02:00
|
|
|
|
2007-12-18 16:53:38 +01:00
|
|
|
/**
|
2009-02-11 20:32:26 +01:00
|
|
|
* holds preferences array of user to notify
|
2007-12-18 16:53:38 +01:00
|
|
|
*
|
2009-02-11 20:32:26 +01:00
|
|
|
* @var array
|
2007-12-18 16:53:38 +01:00
|
|
|
*/
|
|
|
|
private $preferences;
|
2009-04-28 22:24:05 +02:00
|
|
|
|
2007-12-18 16:53:38 +01:00
|
|
|
/**
|
2008-03-22 13:34:27 +01:00
|
|
|
* reference to global db object
|
2007-12-18 16:53:38 +01:00
|
|
|
*
|
2009-02-11 20:32:26 +01:00
|
|
|
* @var db
|
2007-12-18 16:53:38 +01:00
|
|
|
*/
|
|
|
|
private $db;
|
2009-04-28 22:24:05 +02:00
|
|
|
|
2009-02-11 20:32:26 +01:00
|
|
|
/**
|
|
|
|
* holds the users session data
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
var $session_data;
|
2009-04-28 22:24:05 +02:00
|
|
|
|
2009-02-11 20:32:26 +01:00
|
|
|
/**
|
|
|
|
* holds the users session data defaults
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
var $session_data_defaults = array(
|
|
|
|
'notified_mail_uids' => array(),
|
|
|
|
);
|
2009-04-28 22:24:05 +02:00
|
|
|
|
2009-02-11 20:32:26 +01:00
|
|
|
/**
|
|
|
|
* the xml response object
|
|
|
|
*
|
|
|
|
* @var response
|
|
|
|
*/
|
|
|
|
private $response;
|
2009-04-28 22:24:05 +02:00
|
|
|
|
2007-12-18 16:53:38 +01:00
|
|
|
/**
|
2009-04-03 22:36:28 +02:00
|
|
|
* constructor
|
2007-12-18 16:53:38 +01:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
public function __construct() {
|
2009-02-11 20:32:26 +01:00
|
|
|
$this->response = new xajaxResponse();
|
2007-12-18 16:53:38 +01:00
|
|
|
$this->recipient = (object)$GLOBALS['egw']->accounts->read();
|
|
|
|
|
|
|
|
$config = new config(self::_appname);
|
|
|
|
$this->config = (object)$config->read_repository();
|
|
|
|
|
|
|
|
$prefs = new preferences($this->recipient->account_id);
|
|
|
|
$preferences = $prefs->read();
|
2009-02-11 20:32:26 +01:00
|
|
|
$this->preferences = $prefs->read();
|
2007-12-18 16:53:38 +01:00
|
|
|
|
2008-03-22 13:34:27 +01:00
|
|
|
$this->db = $GLOBALS['egw']->db;
|
2007-12-18 16:53:38 +01:00
|
|
|
}
|
2009-04-28 22:24:05 +02:00
|
|
|
|
2009-02-11 20:32:26 +01:00
|
|
|
/**
|
2009-04-03 22:36:28 +02:00
|
|
|
* destructor
|
2009-02-11 20:32:26 +01:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
public function __destruct() {}
|
2009-04-28 22:24:05 +02:00
|
|
|
|
2007-12-18 16:53:38 +01:00
|
|
|
/**
|
2009-04-03 22:17:22 +02:00
|
|
|
* public AJAX trigger function to be called by the JavaScript client
|
|
|
|
*
|
|
|
|
* this function calls all other recurring AJAX notifications methods
|
|
|
|
* to have ONE single recurring AJAX call per user
|
2009-04-28 22:24:05 +02:00
|
|
|
*
|
2007-12-18 16:53:38 +01:00
|
|
|
* @return xajax response
|
|
|
|
*/
|
2009-04-03 22:17:22 +02:00
|
|
|
public function get_notifications() {
|
|
|
|
$this->check_mailbox();
|
|
|
|
$this->get_egwpopup();
|
2009-04-28 22:24:05 +02:00
|
|
|
|
2009-02-11 20:32:26 +01:00
|
|
|
return $this->response->getXML();
|
|
|
|
}
|
2009-04-28 22:24:05 +02:00
|
|
|
|
2009-02-11 20:32:26 +01:00
|
|
|
/**
|
|
|
|
* checks users mailbox and sends a notification if new mails have arrived
|
2009-04-28 22:24:05 +02:00
|
|
|
*
|
2009-04-03 22:17:22 +02:00
|
|
|
* @return boolean true or false
|
2009-02-11 20:32:26 +01:00
|
|
|
*/
|
2009-04-03 22:17:22 +02:00
|
|
|
private function check_mailbox() {
|
2009-02-11 20:32:26 +01:00
|
|
|
if(!isset($this->preferences[self::_mailappname]['notify_folders'])) {
|
2009-04-03 22:17:22 +02:00
|
|
|
return true; //no pref set for notifying - exit
|
2009-02-11 20:32:26 +01:00
|
|
|
}
|
|
|
|
$notify_folders = explode(',', $this->preferences[self::_mailappname]['notify_folders']);
|
2009-04-03 22:17:22 +02:00
|
|
|
if(count($notify_folders) == 0) {
|
|
|
|
return true; //no folders configured for notifying - exit
|
2009-02-11 20:32:26 +01:00
|
|
|
}
|
2009-04-28 22:24:05 +02:00
|
|
|
// explicit require bofelamimail, 'til it get a autoloading conform name
|
|
|
|
require_once(EGW_INCLUDE_ROOT.'/felamimail/inc/class.bofelamimail.inc.php');
|
|
|
|
|
2009-02-11 20:32:26 +01:00
|
|
|
$bofelamimail = new bofelamimail($GLOBALS['egw']->translation->charset());
|
|
|
|
if(!$bofelamimail->openConnection()) {
|
|
|
|
// TODO: This is ugly. Log a bit nicer!
|
|
|
|
error_log(self::_appname.' (user: '.$this->recipient->account_lid.'): cannot connect to mailbox. Please check your prefs!');
|
2009-04-03 22:17:22 +02:00
|
|
|
return false; // cannot connect to mailbox
|
2009-02-11 20:32:26 +01:00
|
|
|
}
|
2009-04-28 22:24:05 +02:00
|
|
|
|
2009-02-11 20:32:26 +01:00
|
|
|
$this->restore_session_data();
|
2009-04-28 22:24:05 +02:00
|
|
|
|
2009-02-11 20:32:26 +01:00
|
|
|
$recent_messages = array();
|
2009-02-14 17:49:10 +01:00
|
|
|
$folder_status = array();
|
2009-02-11 20:32:26 +01:00
|
|
|
foreach($notify_folders as $id=>$notify_folder) {
|
2009-02-14 17:49:10 +01:00
|
|
|
if(!is_array($this->session_data['notified_mail_uids'][$notify_folder])) {
|
|
|
|
$this->session_data['notified_mail_uids'][$notify_folder] = array();
|
|
|
|
}
|
|
|
|
$folder_status[$notify_folder] = $bofelamimail->getFolderStatus($notify_folder);
|
2009-02-11 20:32:26 +01:00
|
|
|
$headers = $bofelamimail->getHeaders($notify_folder, 1, false, 0, true, array('status'=>'UNSEEN'));
|
|
|
|
if(is_array($headers['header']) && count($headers['header']) > 0) {
|
|
|
|
foreach($headers['header'] as $id=>$header) {
|
|
|
|
// check if unseen mail has already been notified
|
2009-02-14 17:49:10 +01:00
|
|
|
if(!in_array($header['uid'], $this->session_data['notified_mail_uids'][$notify_folder])) {
|
2009-02-11 20:32:26 +01:00
|
|
|
// got a REAL recent message
|
|
|
|
$header['folder'] = $notify_folder;
|
2009-02-14 17:49:10 +01:00
|
|
|
$header['folder_display_name'] = $folder_status[$notify_folder]['displayName'];
|
|
|
|
$header['folder_base64'] = base64_encode($notify_folder);
|
2009-02-11 20:32:26 +01:00
|
|
|
$recent_messages[] = $header;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-04-28 22:24:05 +02:00
|
|
|
|
2009-02-11 20:32:26 +01:00
|
|
|
if(count($recent_messages) > 0) {
|
2009-02-14 17:49:10 +01:00
|
|
|
// create notify message
|
|
|
|
$notification_subject = lang("You've got new mail");
|
|
|
|
$values = array();
|
|
|
|
$values[] = array(); // content array starts at index 1
|
2009-02-11 20:32:26 +01:00
|
|
|
foreach($recent_messages as $id=>$recent_message) {
|
2009-02-14 17:49:10 +01:00
|
|
|
$values[] = array(
|
|
|
|
'mail_uid' => $recent_message['uid'],
|
|
|
|
'mail_folder' => $recent_message['folder_display_name'],
|
|
|
|
'mail_folder_base64' => $recent_message['folder_base64'],
|
|
|
|
'mail_subject' => $recent_message['subject'],
|
|
|
|
'mail_from' => !empty($recent_message['sender_name']) ? $recent_message['sender_name'] : $recent_message['sender_address'],
|
|
|
|
'mail_received' => $recent_message['date'],
|
|
|
|
);
|
2009-02-11 20:32:26 +01:00
|
|
|
// save notification status
|
2009-02-14 17:49:10 +01:00
|
|
|
$this->session_data['notified_mail_uids'][$recent_message['folder']][] = $recent_message['uid'];
|
2009-02-11 20:32:26 +01:00
|
|
|
}
|
2009-04-28 22:24:05 +02:00
|
|
|
|
2009-02-14 17:49:10 +01:00
|
|
|
// create etemplate
|
|
|
|
$tpl = new etemplate();
|
|
|
|
$tpl->read('notifications.checkmailbox');
|
|
|
|
$notification_message = $tpl->exec(false, $values, false, false, false, 1);
|
2009-04-28 22:24:05 +02:00
|
|
|
|
2009-02-11 20:32:26 +01:00
|
|
|
// send notification
|
|
|
|
$notification = new notifications();
|
|
|
|
$notification->set_receivers(array($this->recipient->account_id));
|
2009-02-14 17:49:10 +01:00
|
|
|
$notification->set_message($notification_message);
|
2009-02-11 20:32:26 +01:00
|
|
|
$notification->set_sender($this->recipient->account_id);
|
2009-02-14 17:49:10 +01:00
|
|
|
$notification->set_subject($notification_subject);
|
2009-02-11 20:32:26 +01:00
|
|
|
$notification->set_skip_backends(array('email'));
|
|
|
|
$notification->send();
|
|
|
|
}
|
2009-04-28 22:24:05 +02:00
|
|
|
|
2009-02-11 20:32:26 +01:00
|
|
|
$this->save_session_data();
|
2009-04-03 22:17:22 +02:00
|
|
|
return true;
|
|
|
|
}
|
2009-04-28 22:24:05 +02:00
|
|
|
|
2009-04-03 22:17:22 +02:00
|
|
|
/**
|
|
|
|
* gets all egwpopup notifications for calling user
|
2009-04-28 22:24:05 +02:00
|
|
|
*
|
2009-04-03 22:17:22 +02:00
|
|
|
* @return boolean true or false
|
|
|
|
*/
|
|
|
|
private function get_egwpopup() {
|
|
|
|
$session_id = $GLOBALS['egw_info']['user']['sessionid'];
|
|
|
|
$message = '';
|
2009-04-28 22:24:05 +02:00
|
|
|
$rs = $this->db->select(self::_notification_table,
|
2009-04-03 22:17:22 +02:00
|
|
|
'*', array(
|
|
|
|
'account_id' => $this->recipient->account_id,
|
|
|
|
'session_id' => $session_id,
|
|
|
|
),
|
|
|
|
__LINE__,__FILE__,false,'',self::_appname);
|
|
|
|
if ($rs->NumRows() > 0) {
|
|
|
|
foreach ($rs as $notification) {
|
|
|
|
$this->response->addScriptCall('append_notification_message',$notification['message']);
|
|
|
|
}
|
|
|
|
$myval=$this->db->delete(self::_notification_table,array(
|
|
|
|
'account_id' => $this->recipient->account_id,
|
|
|
|
'session_id' => $session_id,
|
|
|
|
),__LINE__,__FILE__,self::_appname);
|
2009-04-28 22:24:05 +02:00
|
|
|
|
2009-04-03 22:17:22 +02:00
|
|
|
switch($this->preferences[self::_appname]['egwpopup_verbosity']) {
|
|
|
|
case 'low':
|
|
|
|
$this->response->addScript('notificationbell_switch("active");');
|
|
|
|
break;
|
|
|
|
case 'high':
|
|
|
|
$this->response->addAlert(lang('eGroupWare has notifications for you'));
|
|
|
|
$this->response->addScript('egwpopup_display();');
|
|
|
|
break;
|
|
|
|
case 'medium':
|
|
|
|
default:
|
|
|
|
$this->response->addScript('egwpopup_display();');
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
2009-02-11 20:32:26 +01:00
|
|
|
}
|
2009-04-28 22:24:05 +02:00
|
|
|
|
2009-02-11 20:32:26 +01:00
|
|
|
/**
|
|
|
|
* restores the users session data for notifications
|
2009-04-28 22:24:05 +02:00
|
|
|
*
|
2009-02-11 20:32:26 +01:00
|
|
|
* @return boolean true
|
|
|
|
*/
|
|
|
|
private function restore_session_data() {
|
|
|
|
$session_data = $GLOBALS['egw']->session->appsession('session_data',self::_appname);
|
|
|
|
if(is_array($session_data)) {
|
|
|
|
$this->session_data = $session_data;
|
|
|
|
} else {
|
|
|
|
$this->session_data = $this->session_data_defaults;
|
|
|
|
}
|
2009-04-28 22:24:05 +02:00
|
|
|
|
2009-02-11 20:32:26 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* saves the users session data for notifications
|
2009-04-28 22:24:05 +02:00
|
|
|
*
|
2009-02-11 20:32:26 +01:00
|
|
|
* @return boolean true
|
|
|
|
*/
|
|
|
|
private function save_session_data() {
|
|
|
|
$GLOBALS['egw']->session->appsession('session_data',self::_appname,$this->session_data);
|
|
|
|
return true;
|
2007-12-18 16:53:38 +01:00
|
|
|
}
|
2009-02-11 20:32:26 +01:00
|
|
|
}
|