2007-12-18 16:53:38 +01:00
< ? php
/**
2011-04-14 15:43:16 +02:00
* EGroupware - Notifications
2007-12-18 16:53:38 +01:00
*
* @ 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
*
2011-04-14 11:50:35 +02:00
* @ var egw_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
*
2011-04-14 11:50:35 +02:00
* @ var xajaxResponse
2009-02-11 20:32:26 +01:00
*/
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 ();
2011-04-14 11:50:35 +02:00
$this -> config = ( object ) config :: read ( self :: _appname );
2007-12-18 16:53:38 +01:00
$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 () {
2010-12-03 11:44:47 +01:00
if ( $GLOBALS [ 'egw_info' ][ 'user' ][ 'apps' ][ 'felamimail' ]) $this -> check_mailbox ();
2011-04-14 11:50:35 +02:00
// update currentusers
if ( $GLOBALS [ 'egw_info' ][ 'user' ][ 'apps' ][ 'admin' ] &&
$GLOBALS [ 'egw_info' ][ 'user' ][ 'preferences' ][ 'common' ][ 'show_currentusers' ])
{
2011-06-08 11:14:17 +02:00
$this -> response -> jquery ( '#currentusers' , 'text' , array (( string ) $GLOBALS [ 'egw' ] -> session -> session_count ()));
2011-04-14 11:50:35 +02:00
}
2009-04-03 22:17:22 +02:00
$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
2011-04-13 19:51:02 +02:00
/**
2011-04-14 11:50:35 +02:00
* Let the user confirm that they have seen the message .
2011-04-13 19:51:02 +02:00
* After they ' ve seen it , remove it from the database
2011-04-14 15:43:16 +02:00
*
* @ param int | array $notify_id one or more notify_id ' s
2011-04-13 19:51:02 +02:00
*/
2011-04-14 15:43:16 +02:00
public function confirm_message ( $notify_id )
{
if ( $notify_id )
{
$this -> db -> delete ( self :: _notification_table , array (
'notify_id' => $notify_id ,
), __LINE__ , __FILE__ , self :: _appname );
}
2011-04-13 19:51:02 +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 () {
2010-12-03 11:44:47 +01:00
//error_log(__METHOD__.__LINE__.array2string($this->preferences[self::_mailappname]['notify_folders']));
if ( ! isset ( $this -> preferences [ self :: _mailappname ][ 'notify_folders' ]) || $this -> preferences [ self :: _mailappname ][ 'notify_folders' ] == 'none' ) {
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
2011-06-27 11:09:17 +02:00
$activeProfile = ( int ) $GLOBALS [ 'egw_info' ][ 'user' ][ 'preferences' ][ 'felamimail' ][ 'ActiveProfileID' ];
//error_log(__METHOD__.__LINE__.' (user: '.$this->recipient->account_lid.') Active Profile:'.$activeProfile);
$bofelamimail = felamimail_bo :: getInstance ( true , $activeProfile );
2011-07-29 11:22:33 +02:00
$activeProfile = $GLOBALS [ 'egw_info' ][ 'user' ][ 'preferences' ][ 'felamimail' ][ 'ActiveProfileID' ] = $bofelamimail -> profileID ;
2011-06-27 11:09:17 +02:00
// buffer felamimail sessiondata, as they are needed for information exchange by the app itself
$bufferFMailSession = $bofelamimail -> sessionData ;
if ( ! $bofelamimail -> openConnection ( $activeProfile )) {
2009-02-11 20:32:26 +01:00
// TODO: This is ugly. Log a bit nicer!
2011-07-29 11:22:33 +02:00
error_log ( __METHOD__ . __LINE__ . ' #' . self :: _appname . ' (user: ' . $this -> recipient -> account_lid . '): cannot connect to mailbox with Profile:' . $activeProfile . '. Please check your prefs!' );
error_log ( __METHOD__ . __LINE__ . ' # Instance=' . $GLOBALS [ 'egw_info' ][ 'user' ][ 'domain' ] . ', User=' . $GLOBALS [ 'egw_info' ][ 'user' ][ 'account_lid' ]);
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 );
2011-06-21 14:25:54 +02:00
$cutoffdate = time ();
$cutoffdate = $cutoffdate - ( 60 * 60 * 24 * 14 ); // last 14 days
2011-08-23 15:34:16 +02:00
$_filter = array ( 'status' => array ( 'UNSEEN' , 'UNDELETED' ), 'type' => " SINCE " , 'string' => date ( " d-M-Y " , $cutoffdate ));
2011-07-29 11:22:33 +02:00
//error_log(__METHOD__.__LINE__.' (user: '.$this->recipient->account_lid.') Mailbox:'.$notify_folder.' filter:'.array2string($_filter));
2011-06-27 11:09:17 +02:00
// $_folderName, $_startMessage, $_numberOfMessages, $_sort, $_reverse, $_filter, $_thisUIDOnly=null, $_cacheResult=true
$headers = $bofelamimail -> getHeaders ( $notify_folder , 1 , 999 , 0 , true , $_filter , null , false );
2009-02-11 20:32:26 +01:00
if ( is_array ( $headers [ 'header' ]) && count ( $headers [ 'header' ]) > 0 ) {
foreach ( $headers [ 'header' ] as $id => $header ) {
2011-07-29 11:22:33 +02:00
//error_log(__METHOD__.__LINE__.' Found Message:'.$header['uid'].' Subject:'.$header['subject']);
2009-02-11 20:32:26 +01:00
// 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-11-26 16:40:12 +01:00
// restore the felamimail session data, as they are needed by the app itself
$bofelamimail -> sessionData = $bufferFMailSession ;
2011-03-10 13:59:16 +01:00
$bofelamimail -> saveSessionData ();
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 () {
$message = '' ;
2011-04-14 15:43:16 +02:00
$rs = $this -> db -> select ( self :: _notification_table , '*' , array (
2009-04-03 22:17:22 +02:00
'account_id' => $this -> recipient -> account_id ,
),
__LINE__ , __FILE__ , false , '' , self :: _appname );
if ( $rs -> NumRows () > 0 ) {
foreach ( $rs as $notification ) {
2011-04-14 15:43:16 +02:00
$this -> response -> addScriptCall ( 'append_notification_message' , $notification [ 'notify_id' ], $notification [ 'notify_message' ]);
2009-04-03 22:17:22 +02:00
}
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
}