2013-02-08 20:11:44 +01:00
< ? php
/**
* EGroupware - Mail - worker class
*
* @ link http :// www . egroupware . org
* @ package mail
2013-10-24 09:15:48 +02:00
* @ author Stylite AG [ info @ stylite . de ]
2014-01-13 10:36:51 +01:00
* @ copyright ( c ) 2013 - 2014 by Stylite AG < info - AT - stylite . de >
2013-02-08 20:11:44 +01:00
* @ license http :// opensource . org / licenses / gpl - license . php GPL - GNU General Public License
* @ version $Id $
*/
/**
* Mail worker class
* - provides backend functionality for all classes in Mail
* - provides classes that may be used by other apps too
2013-11-10 21:53:51 +01:00
*
* @ link https :// github . com / horde / horde / blob / master / imp / lib / Contents . php
2013-02-08 20:11:44 +01:00
*/
class mail_bo
{
/**
* the current selected user profile
* @ var int
*/
var $profileID = 0 ;
/**
* the current display char set
* @ var string
*/
static $displayCharset ;
2013-11-19 12:17:32 +01:00
static $activeFolderCache ;
2013-10-22 12:06:21 +02:00
static $folderStatusCache ;
2013-02-08 20:11:44 +01:00
/**
* Active preferences
*
* @ var array
*/
var $mailPreferences ;
/**
* active html Options
*
* @ var array
*/
var $htmlOptions ;
2013-03-05 15:09:35 +01:00
/**
* Active mimeType
*
* @ var string
*/
var $activeMimeType ;
2013-02-08 20:11:44 +01:00
/**
* Active incomming ( IMAP ) Server Object
*
2013-11-06 22:02:19 +01:00
* @ var emailadmin_imap
2013-02-08 20:11:44 +01:00
*/
var $icServer ;
/**
* Active outgoing ( smtp ) Server Object
*
2013-11-06 22:02:19 +01:00
* @ var emailadmin_smtp
2013-02-08 20:11:44 +01:00
*/
var $ogServer ;
/**
* errorMessage
*
* @ var string $errorMessage
*/
var $errorMessage ;
/**
* switch to enable debug ; sometimes debuging is quite handy , to see things . check with the error log to see results
* @ var boolean
*/
static $debug = false ; //true;
/**
* static used to hold the mail Config values
* @ array
*/
static $mailConfig ;
/**
* static used to configure tidy - if tidy is loadable , this config is used with tidy to straighten out html , instead of using purifiers tidy mode
*
* @ array
*/
static $tidy_config = array ( 'clean' => true , 'output-html' => true , 'join-classes' => true , 'join-styles' => true , 'show-body-only' => " auto " , 'word-2000' => true , 'wrap' => 0 );
/**
* static used to configure htmLawed , for use with emails
*
* @ array
*/
static $htmLawed_config = array ( 'comment' => 1 , //remove comments
2013-11-06 22:02:19 +01:00
'make_tag_strict' => 3 , // 3 is a new own config value, to indicate that transformation is to be performed, but don't transform font as size transformation of numeric sizes to keywords alters the intended result too much
'keep_bad' => 2 , //remove tags but keep element content (4 and 6 keep element content only if text (pcdata) is valid in parent element as per specs, this may lead to textloss if balance is switched on)
'balance' => 1 , //turn off tag-balancing (config['balance']=>0). That will not introduce any security risk; only standards-compliant tag nesting check/filtering will be turned off (basic tag-balance will remain; i.e., there won't be any unclosed tag, etc., after filtering)
'direct_list_nest' => 1 ,
'allow_for_inline' => array ( 'table' , 'div' , 'li' , 'p' ), //block elements allowed for nesting when only inline is allowed; Example span does not allow block elements as table; table is the only element tested so far
// tidy eats away even some wanted whitespace, so we switch it off;
// we used it for its compacting and beautifying capabilities, which resulted in better html for further processing
'tidy' => 0 ,
'elements' => " * -script " ,
'deny_attribute' => 'on*' ,
'schemes' => 'href: file, ftp, http, https, mailto; src: cid, data, file, ftp, http, https; *:file, http, https, cid, src' ,
'hook_tag' => " hl_email_tag_transform " ,
);
2013-02-08 20:11:44 +01:00
/**
* static used define abbrevations for common access rights
*
* @ array
*/
static $aclShortCuts = array ( '' => array ( 'label' => 'none' , 'title' => 'The user has no rights whatsoever.' ),
2013-11-06 22:02:19 +01:00
'lrs' => array ( 'label' => 'readable' , 'title' => 'Allows a user to read the contents of the mailbox.' ),
'lprs' => array ( 'label' => 'post' , 'title' => 'Allows a user to read the mailbox and post to it through the delivery system by sending mail to the submission address of the mailbox.' ),
'ilprs' => array ( 'label' => 'append' , 'title' => 'Allows a user to read the mailbox and append messages to it, either via IMAP or through the delivery system.' ),
'cdilprsw' => array ( 'label' => 'write' , 'title' => 'Allows a user to read the maibox, post to it, append messages to it, and delete messages or the mailbox itself. The only right not given is the right to change the ACL of the mailbox.' ),
'acdilprsw' => array ( 'label' => 'all' , 'title' => 'The user has all possible rights on the mailbox. This is usually granted to users only on the mailboxes they own.' ),
'custom' => array ( 'label' => 'custom' , 'title' => 'User defined combination of rights for the ACL' ),
);
2013-02-08 20:11:44 +01:00
/**
* Folders that get automatic created AND get translated to the users language
* their creation is also controlled by users mailpreferences . if set to none / dont use folder
* the folder will not be automatically created . This is controlled in mail_bo -> getFolderObjects
* so changing names here , must include a change of keywords there as well . Since these
* foldernames are subject to translation , keep that in mind too , if you change names here .
2013-11-27 14:07:54 +01:00
* lang ( 'Drafts' ), lang ( 'Templates' ), lang ( 'Sent' ), lang ( 'Trash' ), lang ( 'Junk' ), lang ( 'Outbox' )
2013-02-08 20:11:44 +01:00
* ActiveSync :
* Outbox is needed by Nokia Clients to be able to send Mails
* @ var array
*/
static $autoFolders = array ( 'Drafts' , 'Templates' , 'Sent' , 'Trash' , 'Junk' , 'Outbox' );
/**
* Array to cache the specialUseFolders , if existing
* @ var array
*/
static $specialUseFolders ;
/**
2013-11-06 22:02:19 +01:00
* IDNA2 instance
*
* @ var egw_idna
2013-02-08 20:11:44 +01:00
*/
static $idna2 ;
/**
* Hold instances by profileID for getInstance () singleton
*
* @ var array
*/
private static $instances = array ();
/**
* Singleton for mail_bo
*
* @ param boolean $_restoreSession = true
* @ param int $_profileID = 0
* @ param boolean $_validate = true - flag wether the profileid should be validated or not , if validation is true , you may receive a profile
* not matching the input profileID , if we can not find a profile matching the given ID
2014-01-16 11:51:22 +01:00
* @ param mixed boolean / object $_icServerObject - if object , return instance with object set as icServer
* immediately , if boolean === true use oldImapServer in constructor
2013-11-06 22:02:19 +01:00
* @ return mail_bo
2013-02-08 20:11:44 +01:00
*/
2013-12-17 10:55:47 +01:00
public static function getInstance ( $_restoreSession = true , & $_profileID = 0 , $_validate = true , $_oldImapServerObject = false )
2013-02-08 20:11:44 +01:00
{
2013-11-19 13:03:53 +01:00
//$_restoreSession=false;
2013-02-11 12:46:35 +01:00
//error_log(__METHOD__.__LINE__.' RestoreSession:'.$_restoreSession.' ProfileId:'.$_profileID.' called from:'.function_backtrace());
2014-01-16 11:51:22 +01:00
if ( $_oldImapServerObject instanceof Horde_Imap_Client )
{
self :: $instances [ $_profileID ] -> icServer = $_oldImapServerObject ;
self :: $instances [ $_profileID ] -> accountid = $_oldImapServerObject -> ImapServerId ;
2014-01-24 11:30:45 +01:00
self :: $instances [ $_profileID ] -> profileID = $_oldImapServerObject -> ImapServerId ;
2014-01-16 11:51:22 +01:00
self :: $instances [ $_profileID ] -> mailPreferences = $GLOBALS [ 'egw_info' ][ 'user' ][ 'preferences' ][ 'mail' ];
self :: $instances [ $_profileID ] -> htmlOptions = self :: $instances [ $_profileID ] -> mailPreferences [ 'htmlOptions' ];
return self :: $instances [ $_profileID ];
}
2013-02-08 20:11:44 +01:00
if ( $_profileID == 0 )
{
if ( isset ( $GLOBALS [ 'egw_info' ][ 'user' ][ 'preferences' ][ 'mail' ][ 'ActiveProfileID' ]) && ! empty ( $GLOBALS [ 'egw_info' ][ 'user' ][ 'preferences' ][ 'mail' ][ 'ActiveProfileID' ]))
{
$profileID = ( int ) $GLOBALS [ 'egw_info' ][ 'user' ][ 'preferences' ][ 'mail' ][ 'ActiveProfileID' ];
}
else
{
2013-10-29 17:16:56 +01:00
$profileID = emailadmin_bo :: getUserDefaultAccID ();
2013-02-08 20:11:44 +01:00
}
if ( $profileID != $_profileID ) $_restoreSession == false ;
$_profileID = $profileID ;
if ( self :: $debug ) error_log ( __METHOD__ . __LINE__ . ' called with profileID==0 using ' . $profileID . ' instead->' . function_backtrace ());
}
2013-12-17 10:55:47 +01:00
// no validation or restoreSession for old ImapServer Object, just fetch it and return it
if ( $_oldImapServerObject === true )
{
return new mail_bo ( 'utf-8' , false , $_profileID , true );
}
2013-02-08 20:11:44 +01:00
if ( $_profileID != 0 && $_validate )
{
2013-10-29 17:16:56 +01:00
$profileID = self :: validateProfileID ( $_profileID );
2013-02-08 20:11:44 +01:00
if ( $profileID != $_profileID )
{
if ( self :: $debug )
{
error_log ( __METHOD__ . __LINE__ . ' Validation of profile with ID:' . $_profileID . ' failed. Using ' . $profileID . ' instead.' );
error_log ( __METHOD__ . __LINE__ . ' # Instance=' . $GLOBALS [ 'egw_info' ][ 'user' ][ 'domain' ] . ', User=' . $GLOBALS [ 'egw_info' ][ 'user' ][ 'account_lid' ]);
}
$_profileID = $profileID ;
$GLOBALS [ 'egw' ] -> preferences -> add ( 'mail' , 'ActiveProfileID' , $_profileID , 'user' );
// save prefs
$GLOBALS [ 'egw' ] -> preferences -> save_repository ( true );
}
egw_cache :: setSession ( 'mail' , 'activeProfileID' , $_profileID );
}
//error_log(__METHOD__.__LINE__.' RestoreSession:'.$_restoreSession.' ProfileId:'.$_profileID.' called from:'.function_backtrace());
2013-12-05 10:08:14 +01:00
if ( $_profileID && ( ! isset ( self :: $instances [ $_profileID ]) || $_restoreSession === false ))
2013-02-08 20:11:44 +01:00
{
self :: $instances [ $_profileID ] = new mail_bo ( 'utf-8' , $_restoreSession , $_profileID );
}
else
{
2013-10-29 17:16:56 +01:00
//refresh objects
try
2013-08-21 12:47:20 +02:00
{
2013-10-30 14:27:26 +01:00
self :: $instances [ $_profileID ] -> icServer = emailadmin_account :: read ( $_profileID ) -> imapServer ();
self :: $instances [ $_profileID ] -> ogServer = emailadmin_account :: read ( $_profileID ) -> smtpServer ();
2013-10-29 17:16:56 +01:00
// TODO: merge mailprefs into userprefs, for easy treatment
2013-10-30 14:27:26 +01:00
self :: $instances [ $_profileID ] -> mailPreferences = $GLOBALS [ 'egw_info' ][ 'user' ][ 'preferences' ][ 'mail' ];
self :: $instances [ $_profileID ] -> htmlOptions = self :: $instances [ $_profileID ] -> mailPreferences [ 'htmlOptions' ];
2013-10-29 17:16:56 +01:00
} catch ( egw_exception $e )
2013-02-08 20:11:44 +01:00
{
2013-10-29 17:16:56 +01:00
$newprofileID = emailadmin_bo :: getUserDefaultAccID ();
2013-08-21 12:47:20 +02:00
// try loading the default profile for the user
2013-10-29 17:16:56 +01:00
error_log ( __METHOD__ . __LINE__ . " Loading the Profile for ProfileID " . $_profileID . ' failed for icServer; ' . $e -> getMessage () . ' Trigger new instance for Default-Profile ' . $newprofileID . '. called from:' . function_backtrace ());
2013-12-05 10:08:14 +01:00
if ( $newprofileID )
{
2014-02-14 13:20:25 +01:00
try
{
self :: $instances [ $newprofileID ] = new mail_bo ( 'utf-8' , false , $newprofileID );
}
catch ( Exception $e )
{
2014-02-14 13:31:34 +01:00
throw new egw_exception ( __METHOD__ . " failed to instanciate mail_bo for $newprofileID with error: " . $e -> getMessage ());
2014-02-14 13:20:25 +01:00
}
2013-12-05 10:08:14 +01:00
$_profileID = $newprofileID ;
}
2014-02-14 13:31:34 +01:00
else
{
throw new egw_exception ( __METHOD__ . " Loading the Profile for ProfileID > " . $_profileID . '< failed for icServer; ' . $e -> getMessage () . ' Trigger new instance for Default-Profile >' . $newprofileID . '< failed' );
}
2013-02-08 20:11:44 +01:00
}
}
self :: $instances [ $_profileID ] -> profileID = $_profileID ;
if ( ! isset ( self :: $instances [ $_profileID ] -> idna2 )) self :: $instances [ $_profileID ] -> idna2 = new egw_idna ;
//if ($_profileID==0); error_log(__METHOD__.__LINE__.' RestoreSession:'.$_restoreSession.' ProfileId:'.$_profileID);
if ( is_null ( self :: $mailConfig )) self :: $mailConfig = config :: read ( 'mail' );
return self :: $instances [ $_profileID ];
}
/**
2013-11-16 11:30:01 +01:00
* Validate given account acc_id to make sure account is valid for current user
2013-02-08 20:11:44 +01:00
*
2013-11-16 11:30:01 +01:00
* Validation checks :
* - non - empty imap - host
* - non - empty imap - username
*
* @ param int $_acc_id = 0
* @ return int validated acc_id -> either acc_id given , or first valid one
2013-02-08 20:11:44 +01:00
*/
2013-11-16 11:30:01 +01:00
public static function validateProfileID ( $_acc_id = 0 )
2013-02-08 20:11:44 +01:00
{
2013-11-16 11:30:01 +01:00
if ( $_acc_id )
2013-02-08 20:11:44 +01:00
{
2013-11-08 18:56:47 +01:00
try {
2013-11-16 11:30:01 +01:00
$account = emailadmin_account :: read ( $_acc_id );
if ( $account -> acc_imap_host && $account -> acc_imap_username )
2013-11-08 18:56:47 +01:00
{
2013-11-16 11:30:01 +01:00
return $_acc_id ;
2013-11-08 18:56:47 +01:00
}
2013-11-16 11:30:01 +01:00
if ( self :: $debug ) error_log ( __METHOD__ . " ( $_acc_id ) account NOT valid, no imap-host! " );
2013-11-08 18:56:47 +01:00
}
2013-12-05 13:32:32 +01:00
catch ( Exception $e ) {
2013-11-16 11:30:01 +01:00
unset ( $e );
if ( self :: $debug ) error_log ( __METHOD__ . " ( $_acc_id ) account NOT found! " );
2013-11-08 18:56:47 +01:00
}
2013-02-08 20:11:44 +01:00
}
2013-11-08 18:56:47 +01:00
// no account specified or specified account not found or not valid
// --> search existing account for first valid one and return that
2013-11-16 11:30:01 +01:00
foreach ( emailadmin_account :: search ( $only_current_user = true , 'acc_imap_host' ) as $acc_id => $imap_host )
2013-02-08 20:11:44 +01:00
{
2013-11-16 11:30:01 +01:00
if ( ! empty ( $imap_host ) && ( $account = emailadmin_account :: read ( $acc_id )) && $account -> acc_imap_username )
2013-02-08 20:11:44 +01:00
{
2013-11-16 11:30:01 +01:00
if ( self :: $debug && $_acc_id ) error_log ( __METHOD__ . " ( $_acc_id ) using $acc_id instead " );
return $acc_id ;
2013-02-08 20:11:44 +01:00
}
}
2013-11-16 11:30:01 +01:00
if ( self :: $debug ) error_log ( __METHOD__ . " ( $_acc_id ) NO valid account found! " );
return $_acc_id ;
2013-02-08 20:11:44 +01:00
}
2013-11-16 11:30:01 +01:00
2013-02-08 20:11:44 +01:00
/**
* Private constructor , use mail_bo :: getInstance () instead
*
* @ param string $_displayCharset = 'utf-8'
* @ param boolean $_restoreSession = true
* @ param int $_profileID = 0
2013-12-17 10:55:47 +01:00
* @ param boolean $_oldImapServerObject = false
2013-02-08 20:11:44 +01:00
*/
2013-12-17 10:55:47 +01:00
private function __construct ( $_displayCharset = 'utf-8' , $_restoreSession = true , $_profileID = 0 , $_oldImapServerObject = false )
2013-02-08 20:11:44 +01:00
{
2013-04-12 15:10:27 +02:00
if ( ! empty ( $_displayCharset )) self :: $displayCharset = $_displayCharset ;
2013-02-08 20:11:44 +01:00
if ( $_restoreSession )
{
//error_log(__METHOD__." Session restore ".function_backtrace());
$this -> restoreSessionData ();
$lv_mailbox = $this -> sessionData [ 'mailbox' ];
$firstMessage = $this -> sessionData [ 'previewMessage' ];
}
else
{
$this -> restoreSessionData ();
$lv_mailbox = $this -> sessionData [ 'mailbox' ];
$firstMessage = $this -> sessionData [ 'previewMessage' ];
$this -> sessionData = array ();
$this -> forcePrefReload ();
}
2014-02-14 13:20:25 +01:00
try
{
$this -> profileID = self :: validateProfileID ( $_profileID );
$this -> accountid = $GLOBALS [ 'egw_info' ][ 'user' ][ 'account_id' ];
2013-02-08 20:11:44 +01:00
2014-02-14 13:20:25 +01:00
//error_log(__METHOD__.__LINE__." ProfileID ".$this->profileID.' called from:'.function_backtrace());
$acc = emailadmin_account :: read ( $this -> profileID );
}
catch ( Exception $e )
{
throw new egw_exception ( __METHOD__ . " failed to instanciate mail_bo for $_profileID / " . $this -> profileID . " with error: " . $e -> getMessage ());;
}
2013-10-30 10:59:05 +01:00
//error_log(__METHOD__.__LINE__.array2string($acc->imapServer()));
2013-12-17 10:55:47 +01:00
$this -> icServer = ( $_oldImapServerObject ? $acc -> oldImapServer () : $acc -> imapServer ());
2013-10-29 17:16:56 +01:00
$this -> ogServer = $acc -> smtpServer ();
// TODO: merge mailprefs into userprefs, for easy treatment
$this -> mailPreferences = $GLOBALS [ 'egw_info' ][ 'user' ][ 'preferences' ][ 'mail' ];
$this -> htmlOptions = $this -> mailPreferences [ 'htmlOptions' ];
if ( isset ( $this -> icServer -> ImapServerId ) && ! empty ( $this -> icServer -> ImapServerId ))
{
$_profileID = $this -> profileID = $GLOBALS [ 'egw_info' ][ 'user' ][ 'preferences' ][ 'mail' ][ 'ActiveProfileID' ] = $this -> icServer -> ImapServerId ;
2013-02-08 20:11:44 +01:00
}
if ( is_null ( self :: $mailConfig )) self :: $mailConfig = config :: read ( 'mail' );
if ( ! isset ( self :: $idna2 )) self :: $idna2 = new egw_idna ;
}
/**
* forceEAProfileLoad
* used to force the load of a specific emailadmin profile ; we assume administrative use only ( as of now )
* @ param int $_profile_id must be a value lower than 0 ( emailadmin profile )
* @ return object instance of mail_bo ( by reference )
*/
public static function & forceEAProfileLoad ( $_profile_id )
{
$mail = mail_bo :: getInstance ( false , $_profile_id , false );
//_debug_array( $_profile_id);
2013-11-29 06:24:39 +01:00
$mail -> icServer = emailadmin_account :: read ( $_profile_id ) -> imapServer ();
$mail -> ogServer = emailadmin_account :: read ( $_profile_id ) -> smtpServer ();
2013-02-08 20:11:44 +01:00
return $mail ;
}
/**
* trigger the force of the reload of the SessionData by resetting the session to an empty array
*/
public static function forcePrefReload ()
{
// unset the mail_preferences session object, to force the reload/rebuild
$GLOBALS [ 'egw' ] -> session -> appsession ( 'mail_preferences' , 'mail' , serialize ( array ()));
$GLOBALS [ 'egw' ] -> session -> appsession ( 'session_data' , 'emailadmin' , serialize ( array ()));
2014-02-07 09:33:23 +01:00
mail_bo :: resetFolderObjectCache ();
2013-02-08 20:11:44 +01:00
}
/**
* restore the SessionData
*/
function restoreSessionData ()
{
2013-11-19 12:17:32 +01:00
$this -> sessionData = array (); //egw_cache::getCache(egw_cache::SESSION,'mail','session_data',$callback=null,$callback_params=array(),$expiration=60*60*1);
self :: $activeFolderCache = egw_cache :: getCache ( egw_cache :: INSTANCE , 'email' , 'activeMailbox' . trim ( $GLOBALS [ 'egw_info' ][ 'user' ][ 'account_id' ]), $callback = null , $callback_params = array (), $expiration = 60 * 60 * 10 );
if ( ! empty ( self :: $activeFolderCache [ $this -> profileID ])) $this -> sessionData [ 'mailbox' ] = self :: $activeFolderCache [ $this -> profileID ];
2013-02-08 20:11:44 +01:00
}
/**
* saveSessionData saves session data
*/
function saveSessionData ()
{
2013-11-19 14:31:58 +01:00
//error_log(__METHOD__.__LINE__.array2string(array_keys($this->sessionData)));
2013-11-19 12:17:32 +01:00
if ( ! empty ( $this -> sessionData [ 'mailbox' ])) self :: $activeFolderCache [ $this -> profileID ] = $this -> sessionData [ 'mailbox' ];
if ( isset ( self :: $activeFolderCache ) && is_array ( self :: $activeFolderCache ))
2013-02-08 20:11:44 +01:00
{
2013-11-19 13:03:53 +01:00
egw_cache :: setCache ( egw_cache :: INSTANCE , 'email' , 'activeMailbox' . trim ( $GLOBALS [ 'egw_info' ][ 'user' ][ 'account_id' ]), self :: $activeFolderCache , $expiration = 60 * 60 * 10 );
2013-02-08 20:11:44 +01:00
}
}
/**
* resets the various cache objects where connection error Objects may be cached
*
* @ param int $_ImapServerId the profileID to look for
*/
static function resetConnectionErrorCache ( $_ImapServerId = null )
{
//error_log(__METHOD__.__LINE__.' for Profile:'.array2string($_ImapServerId) .' for user:'.trim($GLOBALS['egw_info']['user']['account_id']));
$account_id = $GLOBALS [ 'egw_info' ][ 'user' ][ 'account_id' ];
if ( is_array ( $_ImapServerId ))
{
// called via hook
$account_id = $_ImapServerId [ 'account_id' ];
unset ( $_ImapServerId );
$_ImapServerId = null ;
}
if ( is_null ( $_ImapServerId ))
{
$buff = array ();
$isConError = array ();
$waitOnFailure = array ();
}
else
{
$buff = egw_cache :: getCache ( egw_cache :: INSTANCE , 'email' , 'icServerIMAP_connectionError' . trim ( $account_id ));
if ( isset ( $buff [ $_ImapServerId ]))
{
unset ( $buff [ $_ImapServerId ]);
}
$isConError = egw_cache :: getCache ( egw_cache :: INSTANCE , 'email' , 'icServerSIEVE_connectionError' . trim ( $account_id ));
if ( isset ( $isConError [ $_ImapServerId ]))
{
unset ( $isConError [ $_ImapServerId ]);
}
$waitOnFailure = egw_cache :: getCache ( egw_cache :: INSTANCE , 'email' , 'ActiveSyncWaitOnFailure' . trim ( $GLOBALS [ 'egw_info' ][ 'user' ][ 'account_id' ]), $callback = null , $callback_params = array (), $expiration = 60 * 60 * 2 );
if ( isset ( $waitOnFailure [ $_ImapServerId ]))
{
unset ( $waitOnFailure [ $_ImapServerId ]);
}
}
egw_cache :: setCache ( egw_cache :: INSTANCE , 'email' , 'icServerIMAP_connectionError' . trim ( $account_id ), $buff , $expiration = 60 * 15 );
egw_cache :: setCache ( egw_cache :: INSTANCE , 'email' , 'icServerSIEVE_connectionError' . trim ( $account_id ), $isConError , $expiration = 60 * 15 );
egw_cache :: setCache ( egw_cache :: INSTANCE , 'email' , 'ActiveSyncWaitOnFailure' . trim ( $GLOBALS [ 'egw_info' ][ 'user' ][ 'account_id' ]), $waitOnFailure , $expiration = 60 * 60 * 2 );
}
/**
* resets the various cache objects where Folder Objects may be cached
*
* @ param int $_ImapServerId the profileID to look for
*/
static function resetFolderObjectCache ( $_ImapServerId = null )
{
//error_log(__METHOD__.__LINE__.' called for Profile:'.$_ImapServerId.'->'.function_backtrace());
if ( is_null ( $_ImapServerId ))
{
$folders2return = array ();
$folderInfo = array ();
}
else
{
$folders2return = egw_cache :: getCache ( egw_cache :: INSTANCE , 'email' , 'folderObjects' . trim ( $GLOBALS [ 'egw_info' ][ 'user' ][ 'account_id' ]), $callback = null , $callback_params = array (), $expiration = 60 * 60 * 1 );
if ( isset ( $folders2return [ $_ImapServerId ]))
{
unset ( $folders2return [ $_ImapServerId ]);
}
$folderInfo = egw_cache :: getCache ( egw_cache :: INSTANCE , 'email' , 'icServerFolderExistsInfo' . trim ( $GLOBALS [ 'egw_info' ][ 'user' ][ 'account_id' ]), null , array (), $expiration = 60 * 5 );
if ( isset ( $folderInfo [ $_ImapServerId ]))
{
unset ( $folderInfo [ $_ImapServerId ]);
}
$lastFolderUsedForMove = egw_cache :: getCache ( egw_cache :: INSTANCE , 'email' , 'lastFolderUsedForMove' . trim ( $GLOBALS [ 'egw_info' ][ 'user' ][ 'account_id' ]), null , array (), $expiration = 60 * 60 * 1 );
if ( isset ( $lastFolderUsedForMove [ $_ImapServerId ]))
{
unset ( $lastFolderUsedForMove [ $_ImapServerId ]);
}
$folderBasicInfo = egw_cache :: getCache ( egw_cache :: INSTANCE , 'email' , 'folderBasicInfo' . trim ( $GLOBALS [ 'egw_info' ][ 'user' ][ 'account_id' ]), null , array (), $expiration = 60 * 60 * 1 );
if ( isset ( $folderBasicInfo [ $_ImapServerId ]))
{
unset ( $folderBasicInfo [ $_ImapServerId ]);
}
$_specialUseFolders = egw_cache :: getCache ( egw_cache :: INSTANCE , 'email' , 'specialUseFolders' . trim ( $GLOBALS [ 'egw_info' ][ 'user' ][ 'account_id' ]), null , array (), $expiration = 60 * 60 * 12 );
if ( isset ( $_specialUseFolders [ $_ImapServerId ]))
{
unset ( $_specialUseFolders [ $_ImapServerId ]);
self :: $specialUseFolders = null ;
}
}
egw_cache :: setCache ( egw_cache :: INSTANCE , 'email' , 'folderObjects' . trim ( $GLOBALS [ 'egw_info' ][ 'user' ][ 'account_id' ]), $folders2return , $expiration = 60 * 60 * 1 );
egw_cache :: setCache ( egw_cache :: INSTANCE , 'email' , 'icServerFolderExistsInfo' . trim ( $GLOBALS [ 'egw_info' ][ 'user' ][ 'account_id' ]), $folderInfo , $expiration = 60 * 5 );
egw_cache :: setCache ( egw_cache :: INSTANCE , 'email' , 'lastFolderUsedForMove' . trim ( $GLOBALS [ 'egw_info' ][ 'user' ][ 'account_id' ]), $lastFolderUsedForMove , $expiration = 60 * 60 * 1 );
egw_cache :: setCache ( egw_cache :: INSTANCE , 'email' , 'folderBasicInfo' . trim ( $GLOBALS [ 'egw_info' ][ 'user' ][ 'account_id' ]), $folderBasicInfo , $expiration = 60 * 60 * 1 );
egw_cache :: setCache ( egw_cache :: INSTANCE , 'email' , 'specialUseFolders' . trim ( $GLOBALS [ 'egw_info' ][ 'user' ][ 'account_id' ]), $_specialUseFolders , $expiration = 60 * 60 * 12 );
}
/**
* checks if the imap server supports a given capability
*
* @ param string $_capability the name of the capability to check for
* @ return bool
*/
function hasCapability ( $_capability )
{
$rv = $this -> icServer -> hasCapability ( strtoupper ( $_capability ));
//error_log(__METHOD__.__LINE__." $_capability:".array2string($rv));
return $rv ;
}
2013-12-17 10:55:47 +01:00
/**
* getUserEMailAddresses - function to gather the emailadresses connected to the current mail - account
2014-01-24 11:30:45 +01:00
* @ param string $_profileID the ID of the mailaccount to check for identities , if null current mail - account is used
2013-12-17 10:55:47 +01:00
* @ return array - array ( email => realname )
*/
2014-01-24 11:30:45 +01:00
function getUserEMailAddresses ( $_profileID = null ) {
$acc = emailadmin_account :: read (( ! empty ( $_profileID ) ? $_profileID : $this -> profileID ));
2014-01-30 15:53:27 +01:00
//error_log(__METHOD__.__LINE__.':'.array2string($acc));
2013-12-17 10:55:47 +01:00
$identities = $acc -> identities ();
2014-01-30 15:53:27 +01:00
$userEMailAdresses = array ( $acc [ 'ident_email' ] => $acc [ 'ident_realname' ]);
2013-12-17 10:55:47 +01:00
foreach ( $identities as $ik => $ident ) {
//error_log(__METHOD__.__LINE__.':'.$ik.'->'.array2string($ident));
$identity = emailadmin_account :: read_identity ( $ik );
2014-01-30 15:53:27 +01:00
if ( ! empty ( $identity [ 'ident_email' ]) && ! isset ( $userEMailAdresses [ $identity [ 'ident_email' ]])) $userEMailAdresses [ $identity [ 'ident_email' ]] = $identity [ 'ident_realname' ];
2013-12-17 10:55:47 +01:00
}
//error_log(__METHOD__.__LINE__.array2string($userEMailAdresses));
return $userEMailAdresses ;
}
/**
* getAllIdentities - function to gather the identities connected to the current user
* @ return array - array ( email => realname )
*/
2014-01-14 14:35:39 +01:00
static function getAllIdentities () {
2013-12-17 10:55:47 +01:00
$userEMailAdresses = array ();
2014-01-13 14:34:50 +01:00
foreach ( emailadmin_account :: search ( $only_current_user = true , $just_name = true ) as $acc_id => $identity_name )
{
$acc = emailadmin_account :: read ( $acc_id );
2014-02-10 09:20:23 +01:00
$userEMailAdresses [ $acc [ 'ident_id' ]] = array ( 'acc_id' => $acc_id , 'ident_id' => $acc [ 'ident_id' ], 'ident_email' => $acc [ 'ident_email' ], 'ident_org' => $acc [ 'ident_org' ], 'ident_realname' => $acc [ 'ident_realname' ], 'ident_signature' => $acc [ 'ident_signature' ]);
2014-01-13 14:34:50 +01:00
$identities = $acc -> identities ( $acc_id );
foreach ( $identities as $ik => $ident ) {
//error_log(__METHOD__.__LINE__.':'.$ik.'->'.array2string($ident));
$identity = emailadmin_account :: read_identity ( $ik );
2014-01-13 16:40:34 +01:00
//error_log(__METHOD__.__LINE__.':'.$ik.'->'.array2string($identity));
2014-02-10 09:20:23 +01:00
if ( ! isset ( $userEMailAdresses [ $identity [ 'ident_id' ]])) $userEMailAdresses [ $identity [ 'ident_id' ]] = array ( 'acc_id' => $acc_id , 'ident_id' => $identity [ 'ident_id' ], 'ident_email' => $identity [ 'ident_email' ], 'ident_org' => $identity [ 'ident_org' ], 'ident_realname' => $identity [ 'ident_realname' ], 'ident_signature' => $identity [ 'ident_signature' ]);
2014-01-13 14:34:50 +01:00
}
2013-12-17 10:55:47 +01:00
}
//error_log(__METHOD__.__LINE__.array2string($userEMailAdresses));
return $userEMailAdresses ;
}
2014-01-13 16:40:34 +01:00
/**
* getAccountIdentities - function to gather the identities connected to the current mailaccount
* @ param int $acc_id to pass all conected identities back
* @ return array - array ( email => realname )
*/
function getAccountIdentities ( $acc_id ) {
$userEMailAdresses = array ();
$acc = emailadmin_account :: read ( $acc_id );
2014-02-10 09:20:23 +01:00
$userEMailAdresses [ $acc [ 'ident_id' ]] = array ( 'acc_id' => $acc_id , 'ident_id' => $acc [ 'ident_id' ], 'ident_email' => $acc [ 'ident_email' ], 'ident_org' => $acc [ 'ident_org' ], 'ident_realname' => $acc [ 'ident_realname' ], 'ident_signature' => $acc [ 'ident_signature' ]);
2014-01-13 16:40:34 +01:00
$identities = $acc -> identities ();
foreach ( $identities as $ik => $ident ) {
//error_log(__METHOD__.__LINE__.':'.$ik.'->'.array2string($ident));
$identity = emailadmin_account :: read_identity ( $ik );
//error_log(__METHOD__.__LINE__.':'.$ik.'->'.array2string($identity));
2014-02-10 09:20:23 +01:00
if ( ! isset ( $userEMailAdresses [ $identity [ 'ident_id' ]])) $userEMailAdresses [ $identity [ 'ident_id' ]] = array ( 'ident_id' => $identity [ 'ident_id' ], 'ident_email' => $identity [ 'ident_email' ], 'ident_org' => $identity [ 'ident_org' ], 'ident_realname' => $identity [ 'ident_realname' ], 'ident_signature' => $identity [ 'ident_signature' ]);
2014-01-13 16:40:34 +01:00
}
//error_log(__METHOD__.__LINE__.array2string($userEMailAdresses));
return $userEMailAdresses ;
}
2013-12-17 10:55:47 +01:00
/**
* getDefaultIdentity - function to gather the default identitiy connected to the current mailaccount
* @ return int - id of the identity
*/
function getDefaultIdentity () {
// retrieve the signature accociated with the identity
$id = $this -> getIdentitiesWithAccounts ( $_accountData );
$acc = emailadmin_account :: read ( $this -> profileID );
2013-12-17 16:58:51 +01:00
$accountDataIT = ( $_accountData [ $this -> profileID ] ? $acc -> identities ( $this -> profileID , false , 'ident_id' ) : $acc -> identities ( $_accountData [ $id ], false , 'ident_id' ));
2013-12-17 10:55:47 +01:00
foreach ( $accountDataIT as $it => $accountData )
{
2013-12-17 16:58:51 +01:00
return $accountData ;
2013-12-17 10:55:47 +01:00
}
}
2013-02-08 20:11:44 +01:00
/**
* getIdentitiesWithAccounts
*
* @ param array reference to pass all identities back
* @ return the default Identity ( active ) or 0
*/
function getIdentitiesWithAccounts ( & $identities )
{
// account select box
$selectedID = $this -> profileID ;
2013-10-29 17:16:56 +01:00
$allAccountData = emailadmin_account :: search ( $only_current_user = true , $just_name = false , $order_by = null );
2013-02-08 20:11:44 +01:00
if ( $allAccountData ) {
2013-10-29 17:16:56 +01:00
$rememberFirst = $selectedFound = null ;
2013-11-21 14:22:34 +01:00
foreach ( $allAccountData as $tmpkey => $icServers )
2013-02-08 20:11:44 +01:00
{
2013-10-29 17:16:56 +01:00
if ( is_null ( $rememberFirst )) $rememberFirst = $tmpkey ;
if ( $tmpkey == $selectedID ) $selectedFound = true ;
2013-11-21 14:22:34 +01:00
//error_log(__METHOD__.__LINE__.' Key:'.$tmpkey.'->'.array2string($icServers->acc_imap_host));
$host = $icServers -> acc_imap_host ;
2013-10-29 17:16:56 +01:00
if ( empty ( $host )) continue ;
2013-11-21 14:22:34 +01:00
$identities [ $icServers -> acc_id ] = $icServers [ 'ident_realname' ] . ' ' . $icServers [ 'ident_org' ] . ' <' . $icServers [ 'ident_email' ] . '>' ;
//error_log(__METHOD__.__LINE__.' Key:'.$tmpkey.'->'.array2string($identities[$icServers->acc_id]));
2013-02-08 20:11:44 +01:00
}
}
2013-10-29 17:16:56 +01:00
return ( $selectedFound ? $selectedID : $rememberFirst );
2013-02-08 20:11:44 +01:00
}
2013-07-20 09:23:55 +02:00
/**
* generateIdentityString
* construct the string representing an Identity passed by $identity
* @ var array / object $identity , identity object that holds realname , organization , emailaddress and signatureid
* @ var boolean $fullString full or false = NamePart only is returned
* @ return string - constructed of identity object data as defined in mailConfig
*/
static function generateIdentityString ( $identity , $fullString = true )
{
2014-01-13 16:40:34 +01:00
//error_log(__METHOD__.__LINE__.array2string($identity));
2013-07-20 09:23:55 +02:00
if ( is_null ( self :: $mailConfig )) self :: $mailConfig = config :: read ( 'mail' );
// not set? -> use default, means full display of all available data
if ( ! isset ( self :: $mailConfig [ 'how2displayIdentities' ])) self :: $mailConfig [ 'how2displayIdentities' ] = '' ;
switch ( self :: $mailConfig [ 'how2displayIdentities' ])
{
case 'email' ;
//$retData = str_replace('@',' ',$identity->emailAddress).($fullString===true?' <'.$identity->emailAddress.'>':'');
2013-11-20 16:05:25 +01:00
$retData = $identity [ 'ident_email' ] . ( $fullString === true ? ' <' . $identity [ 'ident_email' ] . '>' : '' );
2013-07-20 09:23:55 +02:00
break ;
case 'nameNemail' ;
2013-11-20 16:05:25 +01:00
$retData = ( ! empty ( $identity [ 'ident_realname' ]) ? $identity [ 'ident_realname' ] : substr_replace ( $identity [ 'ident_email' ], '' , strpos ( $identity [ 'ident_email' ], '@' ))) . ( $fullString === true ? ' <' . $identity [ 'ident_email' ] . '>' : '' );
2013-07-20 09:23:55 +02:00
break ;
case 'orgNemail' ;
2013-11-20 16:05:25 +01:00
$retData = ( ! empty ( $identity [ 'ident_org' ]) ? $identity [ 'ident_org' ] : substr_replace ( $identity [ 'ident_email' ], '' , 0 , strpos ( $identity [ 'ident_email' ], '@' ) + 1 )) . ( $fullString === true ? ' <' . $identity [ 'ident_email' ] . '>' : '' );
2013-07-20 09:23:55 +02:00
break ;
default :
2013-11-20 16:05:25 +01:00
$retData = $identity [ 'ident_realname' ] . ( ! empty ( $identity [ 'ident_org' ]) ? ' ' . $identity [ 'ident_org' ] : '' ) . ( $fullString === true ? ' <' . $identity [ 'ident_email' ] . '>' : '' );
2013-07-20 09:23:55 +02:00
}
return $retData ;
}
2013-02-11 18:05:29 +01:00
/**
* closes a connection on the active Server ( $this -> icServer )
*
* @ return void
*/
function closeConnection () {
//if ($icServer->_connected) error_log(__METHOD__.__LINE__.' disconnect from Server');
2013-11-21 14:22:34 +01:00
//error_log(__METHOD__."() ".function_backtrace());
2013-10-29 17:16:56 +01:00
$this -> icServer -> disconnect ();
2013-02-11 18:05:29 +01:00
}
2013-02-08 20:11:44 +01:00
/**
* reopens a connection for the active Server ( $this -> icServer ), and selects the folder given
*
* @ param string $_foldername , folder to open / select
* @ return void
*/
function reopen ( $_foldername )
{
2013-11-19 09:17:49 +01:00
//error_log(__METHOD__.__LINE__."('$_foldername') ".function_backtrace());
2013-02-08 20:11:44 +01:00
// TODO: trying to reduce traffic to the IMAP Server here, introduces problems with fetching the bodies of
// eMails when not in "current-Folder" (folder that is selected by UI)
static $folderOpened ;
//if (empty($folderOpened) || $folderOpened!=$_foldername)
//{
2013-02-28 10:28:08 +01:00
//error_log( __METHOD__.__LINE__." $_foldername ".function_backtrace());
2013-02-08 20:11:44 +01:00
//error_log(__METHOD__.__LINE__.' Connected with icServer for Profile:'.$this->profileID.'?'.print_r($this->icServer->_connected,true));
2013-10-29 17:16:56 +01:00
if ( $this -> folderIsSelectable ( $_foldername )) {
$tretval = $this -> icServer -> openMailbox ( $_foldername );
2013-02-08 20:11:44 +01:00
}
$folderOpened = $_foldername ;
//}
}
/**
* openConnection
*
* @ param int $_icServerID
* @ param boolean $_adminConnection
2013-11-06 22:02:19 +01:00
* @ throws Horde_Imap_Client_Exception on connection error or authentication failure
* @ throws InvalidArgumentException on missing credentials
2013-02-08 20:11:44 +01:00
*/
function openConnection ( $_icServerID = 0 , $_adminConnection = false )
{
//error_log( "-------------------------->open connection ".function_backtrace());
//error_log(__METHOD__.__LINE__.' ->'.array2string($this->icServer));
2013-11-06 22:02:19 +01:00
$mailbox = null ;
if ( $this -> folderExists ( $this -> sessionData [ 'mailbox' ])) $mailbox = $this -> sessionData [ 'mailbox' ];
if ( empty ( $mailbox )) $mailbox = $this -> icServer -> getCurrentMailbox ();
2013-12-10 16:59:54 +01:00
/*
2013-11-06 22:02:19 +01:00
if ( isset ( emailadmin_imap :: $supports_keywords [ $_icServerID ]))
2013-10-30 14:27:26 +01:00
{
2013-11-06 22:02:19 +01:00
$this -> icServer -> openMailbox ( $mailbox );
2013-10-30 14:27:26 +01:00
}
2013-11-06 22:02:19 +01:00
else
2013-10-30 14:27:26 +01:00
{
2013-11-06 22:02:19 +01:00
$this -> icServer -> examineMailbox ( $mailbox );
2013-10-30 14:27:26 +01:00
}
2013-12-10 16:59:54 +01:00
*/
// the above should detect if there is a known information about supporting KEYWORDS
// but does not work as expected :-(
$this -> icServer -> examineMailbox ( $mailbox );
2013-10-29 17:16:56 +01:00
//error_log(__METHOD__." using existing Connection ProfileID:".$_icServerID.' Status:'.print_r($this->icServer->_connected,true));
//error_log(__METHOD__.__LINE__."->open connection for Server with profileID:".$_icServerID.function_backtrace());
2013-02-08 20:11:44 +01:00
//make sure we are working with the correct hierarchyDelimiter on the current connection, calling getHierarchyDelimiter with false to reset the cache
$hD = $this -> getHierarchyDelimiter ( false );
self :: $specialUseFolders = $this -> getSpecialUseFolders ();
}
2013-10-03 18:09:25 +02:00
/**
* getQuotaRoot
* return the qouta of the users INBOX
*
* @ return mixed array / boolean
*/
function getQuotaRoot ()
{
static $quota ;
if ( isset ( $quota )) return $quota ;
if ( ! $this -> icServer -> hasCapability ( 'QUOTA' )) {
$quota = false ;
return false ;
}
$quota = $this -> icServer -> getStorageQuotaRoot ( 'INBOX' );
//error_log(__METHOD__.__LINE__.array2string($quota));
if ( is_array ( $quota )) {
$quota = array (
'usage' => $quota [ 'USED' ],
'limit' => $quota [ 'QMAX' ],
);
} else {
$quota = false ;
}
return $quota ;
}
2013-02-08 20:11:44 +01:00
/**
* getTimeOut
*
* @ param string _use decide if the use is for IMAP or SIEVE , by now only the default differs
*
* @ return int - timeout ( either set or default 20 / 10 )
*/
static function getTimeOut ( $_use = 'IMAP' )
{
$timeout = $GLOBALS [ 'egw_info' ][ 'user' ][ 'preferences' ][ 'mail' ][ 'connectionTimeout' ];
if ( empty ( $timeout )) $timeout = ( $_use == 'SIEVE' ? 10 : 20 ); // this is the default value
return $timeout ;
}
/**
* _getNameSpaces , fetch the namespace from icServer
* @ return array $nameSpace array ( peronal => array , others => array , shared => array )
*/
function _getNameSpaces ()
{
static $nameSpace ;
$foldersNameSpace = array ();
$delimiter = $this -> getHierarchyDelimiter ();
// TODO: cache by $this->icServer->ImapServerId
2013-10-22 17:30:25 +02:00
if ( is_null ( $nameSpace )) $nameSpace = $this -> icServer -> getNameSpaceArray ();
2013-10-25 15:37:53 +02:00
//error_log(__METHOD__.__LINE__.array2string($nameSpace));
2013-02-08 20:11:44 +01:00
if ( is_array ( $nameSpace )) {
foreach ( $nameSpace as $type => $singleNameSpace ) {
$prefix_present = false ;
2013-10-25 15:37:53 +02:00
if ( $type == 'personal' && ( $singleNameSpace [ 'name' ] == '#mh/' || count ( $nameSpace ) == 1 ) && ( $this -> folderExists ( 'Mail' ) || $this -> folderExists ( 'INBOX' )))
2013-02-08 20:11:44 +01:00
{
$foldersNameSpace [ $type ][ 'prefix_present' ] = 'forced' ;
// uw-imap server with mailbox prefix or dovecot maybe
2013-10-25 15:37:53 +02:00
$foldersNameSpace [ $type ][ 'prefix' ] = ( $this -> folderExists ( 'Mail' ) ? 'Mail' : ( ! empty ( $singleNameSpace [ 'name' ]) ? $singleNameSpace [ 'name' ] : '' ));
2013-02-08 20:11:44 +01:00
}
2013-10-25 15:37:53 +02:00
elseif ( $type == 'personal' && ( $singleNameSpace [ 'name' ] == '#mh/' || count ( $nameSpace ) == 1 ) && $this -> folderExists ( 'mail' ))
2013-02-08 20:11:44 +01:00
{
$foldersNameSpace [ $type ][ 'prefix_present' ] = 'forced' ;
// uw-imap server with mailbox prefix or dovecot maybe
$foldersNameSpace [ $type ][ 'prefix' ] = 'mail' ;
} else {
$foldersNameSpace [ $type ][ 'prefix_present' ] = true ;
2013-10-25 15:37:53 +02:00
$foldersNameSpace [ $type ][ 'prefix' ] = $singleNameSpace [ 'name' ];
2013-02-08 20:11:44 +01:00
}
$foldersNameSpace [ $type ][ 'delimiter' ] = $delimiter ;
//echo "############## $type->".print_r($foldersNameSpace[$type],true)." ###################<br>";
}
}
//error_log(__METHOD__.__LINE__.array2string($foldersNameSpace));
return $foldersNameSpace ;
}
/**
* getFolderPrefixFromNamespace , wrapper to extract the folder prefix from folder compared to given namespace array
* @ var array $nameSpace
* @ var string $_folderName
* @ return string the prefix ( may be an empty string )
*/
function getFolderPrefixFromNamespace ( $nameSpace , $folderName )
{
foreach ( $nameSpace as $type => $singleNameSpace )
{
//if (substr($singleNameSpace['prefix'],0,strlen($folderName))==$folderName) return $singleNameSpace['prefix'];
if ( substr ( $folderName , 0 , strlen ( $singleNameSpace [ 'prefix' ])) == $singleNameSpace [ 'prefix' ]) return $singleNameSpace [ 'prefix' ];
}
return " " ;
}
/**
* getHierarchyDelimiter
* @ var boolean $_useCache
* @ return string the hierarchyDelimiter
*/
function getHierarchyDelimiter ( $_useCache = true )
{
static $HierarchyDelimiter ;
if ( is_null ( $HierarchyDelimiter )) $HierarchyDelimiter =& egw_cache :: getSession ( 'mail' , 'HierarchyDelimiter' );
if ( $_useCache === false ) unset ( $HierarchyDelimiter [ $this -> icServer -> ImapServerId ]);
if ( isset ( $HierarchyDelimiter [ $this -> icServer -> ImapServerId ]) &&! empty ( $HierarchyDelimiter [ $this -> icServer -> ImapServerId ]))
{
$this -> icServer -> mailboxDelimiter = $HierarchyDelimiter [ $this -> icServer -> ImapServerId ];
return $HierarchyDelimiter [ $this -> icServer -> ImapServerId ];
}
$HierarchyDelimiter [ $this -> icServer -> ImapServerId ] = '/' ;
if (( $this -> icServer instanceof defaultimap ))
{
2013-10-24 09:15:48 +02:00
$HierarchyDelimiter [ $this -> icServer -> ImapServerId ] = $this -> icServer -> getDelimiter ();
2013-02-08 20:11:44 +01:00
if ( PEAR :: isError ( $HierarchyDelimiter [ $this -> icServer -> ImapServerId ])) $HierarchyDelimiter [ $this -> icServer -> ImapServerId ] = '/' ;
}
$this -> icServer -> mailboxDelimiter = $HierarchyDelimiter [ $this -> icServer -> ImapServerId ];
return $HierarchyDelimiter [ $this -> icServer -> ImapServerId ];
}
/**
* getSpecialUseFolders
* @ ToDo : could as well be static , when icServer is passed
* @ return mixed null / array
*/
function getSpecialUseFolders ()
{
//error_log(__METHOD__.__LINE__.':'.$this->icServer->ImapServerId.' Connected:'.$this->icServer->_connected);
static $_specialUseFolders ;
if ( is_null ( $_specialUseFolders ) || empty ( $_specialUseFolders )) $_specialUseFolders = egw_cache :: getCache ( egw_cache :: INSTANCE , 'email' , 'specialUseFolders' . trim ( $GLOBALS [ 'egw_info' ][ 'user' ][ 'account_id' ]), $callback = null , $callback_params = array (), $expiration = 60 * 60 * 24 * 5 );
if ( isset ( $_specialUseFolders [ $this -> icServer -> ImapServerId ]) &&! empty ( $_specialUseFolders [ $this -> icServer -> ImapServerId ]))
{
if (( $this -> icServer instanceof defaultimap ))
{
//error_log(__METHOD__.__LINE__.array2string($specialUseFolders[$this->icServer->ImapServerId]));
// array('Drafts', 'Templates', 'Sent', 'Trash', 'Junk', 'Outbox');
2013-10-31 15:45:11 +01:00
if ( empty ( $this -> icServer -> acc_folder_trash ) && ( $f = array_search ( 'Trash' ,( array ) $_specialUseFolders [ $this -> icServer -> ImapServerId ]))) $this -> icServer -> acc_folder_trash = $f ;
if ( empty ( $this -> icServer -> acc_folder_draft ) && ( $f = array_search ( 'Drafts' ,( array ) $_specialUseFolders [ $this -> icServer -> ImapServerId ]))) $this -> icServer -> acc_folder_draft = $f ;
if ( empty ( $this -> icServer -> acc_folder_sent ) && ( $f = array_search ( 'Sent' ,( array ) $_specialUseFolders [ $this -> icServer -> ImapServerId ]))) $this -> icServer -> acc_folder_sent = $f ;
if ( empty ( $this -> icServer -> acc_folder_template ) && ( $f = array_search ( 'Templates' ,( array ) $_specialUseFolders [ $this -> icServer -> ImapServerId ]))) $this -> icServer -> acc_folder_template = $f ;
2013-02-08 20:11:44 +01:00
}
//error_log(__METHOD__.__LINE__.array2string($_specialUseFolders[$this->icServer->ImapServerId]));
self :: $specialUseFolders = $_specialUseFolders [ $this -> icServer -> ImapServerId ]; // make sure this one is set on function call
return $_specialUseFolders [ $this -> icServer -> ImapServerId ];
}
2013-10-29 17:16:56 +01:00
if (( $this -> icServer instanceof defaultimap ) )
2013-02-08 20:11:44 +01:00
{
//error_log(__METHOD__.__LINE__);
if (( $this -> hasCapability ( 'SPECIAL-USE' )))
{
//error_log(__METHOD__.__LINE__);
2013-12-17 13:16:11 +01:00
try
{
$ret = $this -> icServer -> getSpecialUseFolders ();
} catch ( Exception $e )
{
$ret = null ;
}
if ( empty ( $ret ))
2013-02-08 20:11:44 +01:00
{
$_specialUseFolders [ $this -> icServer -> ImapServerId ] = array ();
}
else
{
foreach ( $ret as $k => $f )
{
if ( isset ( $f [ 'ATTRIBUTES' ]) && ! empty ( $f [ 'ATTRIBUTES' ]) &&
2013-12-17 13:16:11 +01:00
! in_array ( '\\nonexistent' , $f [ 'ATTRIBUTES' ]))
2013-02-08 20:11:44 +01:00
{
foreach ( self :: $autoFolders as $i => $n ) // array('Drafts', 'Templates', 'Sent', 'Trash', 'Junk', 'Outbox');
{
2013-12-17 13:16:11 +01:00
if ( in_array ( '\\' . strtolower ( $n ), $f [ 'ATTRIBUTES' ])) $_specialUseFolders [ $this -> icServer -> ImapServerId ][ $f [ 'MAILBOX' ]] = $n ;
2013-02-08 20:11:44 +01:00
}
}
}
}
egw_cache :: setCache ( egw_cache :: INSTANCE , 'email' , 'specialUseFolders' . trim ( $GLOBALS [ 'egw_info' ][ 'user' ][ 'account_id' ]), $_specialUseFolders , $expiration = 60 * 60 * 24 * 5 );
}
//error_log(__METHOD__.__LINE__.array2string($_specialUseFolders[$this->icServer->ImapServerId]));
2013-10-31 15:45:11 +01:00
if ( empty ( $this -> icServer -> acc_folder_trash ) && ( $f = array_search ( 'Trash' ,( array ) $_specialUseFolders [ $this -> icServer -> ImapServerId ]))) $this -> icServer -> acc_folder_trash = $f ;
if ( empty ( $this -> icServer -> acc_folder_draft ) && ( $f = array_search ( 'Drafts' ,( array ) $_specialUseFolders [ $this -> icServer -> ImapServerId ]))) $this -> icServer -> acc_folder_draft = $f ;
if ( empty ( $this -> icServer -> acc_folder_sent ) && ( $f = array_search ( 'Sent' ,( array ) $_specialUseFolders [ $this -> icServer -> ImapServerId ]))) $this -> icServer -> acc_folder_sent = $f ;
if ( empty ( $this -> icServer -> acc_folder_template ) && ( $f = array_search ( 'Templates' ,( array ) $_specialUseFolders [ $this -> icServer -> ImapServerId ]))) $this -> icServer -> acc_folder_template = $f ;
2013-02-08 20:11:44 +01:00
}
self :: $specialUseFolders = $_specialUseFolders [ $this -> icServer -> ImapServerId ]; // make sure this one is set on function call
//error_log(__METHOD__.__LINE__.array2string($_specialUseFolders[$this->icServer->ImapServerId]));
return $_specialUseFolders [ $this -> icServer -> ImapServerId ];
}
/**
* get IMAP folder status regarding NoSelect
*
* returns true or false regarding the noselect attribute
*
* @ param foldertoselect string the foldername
*
* @ return boolean
*/
function folderIsSelectable ( $folderToSelect )
{
$retval = true ;
if ( $folderToSelect && ( $folderStatus = $this -> getFolderStatus ( $folderToSelect , false , true ))) {
2013-04-09 16:43:55 +02:00
if ( ! empty ( $folderStatus [ 'attributes' ]) && stripos ( array2string ( $folderStatus [ 'attributes' ]), 'noselect' ) !== false )
2013-02-08 20:11:44 +01:00
{
$retval = false ;
}
}
return $retval ;
}
/**
* get IMAP folder status , wrapper to store results within a single request
*
* returns an array information about the imap folder
*
* @ param folderName string the foldername
* @ param ignoreStatusCache bool ignore the cache used for counters
*
* @ return array
*/
function _getStatus ( $folderName , $ignoreStatusCache = false )
{
static $folderStatus ;
if ( ! $ignoreStatusCache && isset ( $folderStatus [ $this -> icServer -> ImapServerId ][ $folderName ]))
{
//error_log(__METHOD__.__LINE__.' Using cache for status on Server:'.$this->icServer->ImapServerId.' for folder:'.$folderName.'->'.array2string($folderStatus[$this->icServer->ImapServerId][$folderName]));
return $folderStatus [ $this -> icServer -> ImapServerId ][ $folderName ];
}
$folderStatus [ $this -> icServer -> ImapServerId ][ $folderName ] = $this -> icServer -> getStatus ( $folderName );
return $folderStatus [ $this -> icServer -> ImapServerId ][ $folderName ];
}
/**
* get IMAP folder status
*
2013-02-12 14:01:43 +01:00
* returns an array information about the imap folder , may be used as wrapper to retrieve results from cache
2013-02-08 20:11:44 +01:00
*
* @ param _folderName string the foldername
* @ param ignoreStatusCache bool ignore the cache used for counters
* @ param basicInfoOnly bool retrieve only names and stuff returned by getMailboxes
*
* @ return array
*/
function getFolderStatus ( $_folderName , $ignoreStatusCache = false , $basicInfoOnly = false )
{
2013-11-19 09:17:49 +01:00
if ( self :: $debug ) error_log ( __METHOD__ . __LINE__ . " called with: $_folderName , $ignoreStatusCache , $basicInfoOnly " );
2013-02-08 20:11:44 +01:00
if ( ! is_string ( $_folderName ) || empty ( $_folderName )) // something is wrong. Do not proceed
{
return false ;
}
static $folderInfoCache ; // reduce traffic on single request
static $folderBasicInfo ;
if ( is_null ( $folderBasicInfo ))
{
2013-10-25 15:37:53 +02:00
$folderBasicInfo = egw_cache :: getCache ( egw_cache :: INSTANCE , 'email' , 'folderBasicInfo' . trim ( $GLOBALS [ 'egw_info' ][ 'user' ][ 'account_id' ]), null , array (), $expiration = 60 * 60 * 1 );
$folderInfoCache = $folderBasicInfo [ $this -> profileID ];
2013-02-08 20:11:44 +01:00
}
if ( isset ( $folderInfoCache [ $_folderName ]) && $ignoreStatusCache == false && $basicInfoOnly ) return $folderInfoCache [ $_folderName ];
$retValue = array ();
$retValue [ 'subscribed' ] = false ;
2013-11-19 12:17:32 +01:00
/*
2013-10-29 17:16:56 +01:00
if ( ! $icServer = emailadmin_account :: read ( $this -> profileID )) {
2013-02-08 20:11:44 +01:00
if ( self :: $debug ) error_log ( __METHOD__ . " no Server found for Folder: " . $_folderName );
return false ;
}
2013-11-19 12:17:32 +01:00
*/
2013-02-08 20:11:44 +01:00
// does the folder exist???
if ( is_null ( $folderInfoCache ) || ! isset ( $folderInfoCache [ $_folderName ])) $folderInfoCache [ $_folderName ] = $this -> icServer -> getMailboxes ( '' , $_folderName , true );
$folderInfo = $folderInfoCache [ $_folderName ];
//error_log(__METHOD__.__LINE__.array2string($folderInfo).'->'.function_backtrace());
2013-10-25 15:37:53 +02:00
if (( $folderInfo instanceof PEAR_Error ) || ! is_array ( $folderInfo [ 0 ])) {
2013-02-08 20:11:44 +01:00
if ( self :: $debug || $folderInfo instanceof PEAR_Error ) error_log ( __METHOD__ . " returned Info for folder $_folderName : " . print_r ( $folderInfo -> message , true ));
if ( ( $folderInfo instanceof PEAR_Error ) || PEAR :: isError ( $r = $this -> _getStatus ( $_folderName )) || $r == 0 ) return false ;
2013-10-25 15:37:53 +02:00
if ( ! is_array ( $folderInfo [ 0 ]))
2013-02-08 20:11:44 +01:00
{
// no folder info, but there is a status returned for the folder: something is wrong, try to cope with it
2013-10-25 15:37:53 +02:00
$folderInfo [ 0 ] = is_array ( $folderInfo ) ? $folderInfo : array ( 'HIERACHY_DELIMITER' => $this -> getHierarchyDelimiter (),
2013-10-25 12:23:36 +02:00
'ATTRIBUTES' => '' );
2013-10-25 15:37:53 +02:00
if ( empty ( $folderInfo [ 0 ][ 'HIERACHY_DELIMITER' ]) || ( isset ( $folderInfo [ 0 ][ 'delimiter' ]) && empty ( $folderInfo [ 0 ][ 'delimiter' ])))
2013-03-05 15:09:35 +01:00
{
//error_log(__METHOD__.__LINE__.array2string($folderInfo));
2013-10-25 15:37:53 +02:00
$folderInfo [ 0 ][ 'HIERACHY_DELIMITER' ] = $this -> getHierarchyDelimiter ();
2013-03-05 15:09:35 +01:00
}
2013-02-08 20:11:44 +01:00
}
}
#if(!is_array($folderInfo[0])) {
# return false;
#}
2013-10-25 15:37:53 +02:00
$retValue [ 'delimiter' ] = ( $folderInfo [ 0 ][ 'HIERACHY_DELIMITER' ] ? $folderInfo [ 0 ][ 'HIERACHY_DELIMITER' ] : $folderInfo [ 0 ][ 'delimiter' ]);
$retValue [ 'attributes' ] = ( $folderInfo [ 0 ][ 'ATTRIBUTES' ] ? $folderInfo [ 0 ][ 'ATTRIBUTES' ] : $folderInfo [ 0 ][ 'attributes' ]);
2013-02-08 20:11:44 +01:00
$shortNameParts = explode ( $retValue [ 'delimiter' ], $_folderName );
$retValue [ 'shortName' ] = array_pop ( $shortNameParts );
2013-10-30 14:27:26 +01:00
$retValue [ 'displayName' ] = $_folderName ;
$retValue [ 'shortDisplayName' ] = $retValue [ 'shortName' ];
2013-02-08 20:11:44 +01:00
if ( strtoupper ( $retValue [ 'shortName' ]) == 'INBOX' ) {
$retValue [ 'displayName' ] = lang ( 'INBOX' );
$retValue [ 'shortDisplayName' ] = lang ( 'INBOX' );
}
// translate the automatic Folders (Sent, Drafts, ...) like the INBOX
elseif ( in_array ( $retValue [ 'shortName' ], self :: $autoFolders ))
{
$retValue [ 'displayName' ] = $retValue [ 'shortDisplayName' ] = lang ( $retValue [ 'shortName' ]);
}
if ( ! ( $folderInfo instanceof PEAR_Error )) $folderBasicInfo [ $this -> profileID ][ $_folderName ] = $retValue ;
egw_cache :: setCache ( egw_cache :: INSTANCE , 'email' , 'folderBasicInfo' . trim ( $GLOBALS [ 'egw_info' ][ 'user' ][ 'account_id' ]), $folderBasicInfo , $expiration = 60 * 60 * 1 );
2013-03-05 15:09:35 +01:00
if ( $basicInfoOnly || ( isset ( $retValue [ 'attributes' ]) && stripos ( array2string ( $retValue [ 'attributes' ]), 'noselect' ) !== false ))
2013-02-08 20:11:44 +01:00
{
return $retValue ;
}
2014-01-09 09:36:22 +01:00
$subscribedFolders = $this -> icServer -> listSubscribedMailboxes ( '' , $_folderName );
2013-02-08 20:11:44 +01:00
if ( is_array ( $subscribedFolders ) && count ( $subscribedFolders ) == 1 ) {
$retValue [ 'subscribed' ] = true ;
}
if ( PEAR :: isError ( $folderStatus = $this -> _getStatus ( $_folderName , $ignoreStatusCache )) ) {
if ( self :: $debug ) error_log ( __METHOD__ . " returned folderStatus for Folder $_folderName : " . print_r ( $folderStatus -> message , true ));
} else {
$nameSpace = $this -> _getNameSpaces ();
if ( isset ( $nameSpace [ 'personal' ])) unset ( $nameSpace [ 'personal' ]);
$prefix = $this -> getFolderPrefixFromNamespace ( $nameSpace , $_folderName );
$retValue [ 'messages' ] = $folderStatus [ 'MESSAGES' ];
$retValue [ 'recent' ] = $folderStatus [ 'RECENT' ];
$retValue [ 'uidnext' ] = $folderStatus [ 'UIDNEXT' ];
$retValue [ 'uidvalidity' ] = $folderStatus [ 'UIDVALIDITY' ];
$retValue [ 'unseen' ] = $folderStatus [ 'UNSEEN' ];
if ( //$retValue['unseen']==0 &&
2013-10-31 15:45:11 +01:00
( isset ( $this -> mailPreferences [ 'trustServersUnseenInfo' ]) && // some servers dont serve the UNSEEN information
$this -> mailPreferences [ 'trustServersUnseenInfo' ] == false ) ||
( isset ( $this -> mailPreferences [ 'trustServersUnseenInfo' ]) &&
$this -> mailPreferences [ 'trustServersUnseenInfo' ] == 2 &&
2013-02-08 20:11:44 +01:00
$prefix != '' && stripos ( $_folderName , $prefix ) !== false )
)
{
2013-10-31 15:45:11 +01:00
//error_log(__METHOD__." returned folderStatus for Folder $_folderName:".print_r($prefix,true).' TS:'.$this->mailPreferences['trustServersUnseenInfo']);
2013-02-08 20:11:44 +01:00
// we filter for the combined status of unseen and undeleted, as this is what we show in list
$sortResult = $this -> getSortedList ( $_folderName , $_sort = 0 , $_reverse = 1 , $_filter = array ( 'status' => array ( 'UNSEEN' , 'UNDELETED' )), $byUid = true , false );
2013-10-24 09:15:48 +02:00
$retValue [ 'unseen' ] = $sortResult [ 'count' ];
2013-02-08 20:11:44 +01:00
}
}
return $retValue ;
}
/**
* getHeaders
*
* this function is a wrapper function for getSortedList and populates the resultList thereof with headerdata
*
* @ param string $_folderName ,
* @ param int $_startMessage ,
* @ param int $_numberOfMessages , number of messages to return
* @ param array $_sort , sort by criteria
* @ param boolean $_reverse , reverse sorting of the result array ( may be switched , as it is passed to getSortedList by reference )
* @ param array $_filter , filter to apply to getSortedList
* @ param mixed $_thisUIDOnly = null , if given fetch the headers of this uid only ( either one , or array of uids )
* @ param boolean $_cacheResult = true try touse the cache of getSortedList
* @ return array result as array ( header => array , total => int , first => int , last => int )
*/
function getHeaders ( $_folderName , $_startMessage , $_numberOfMessages , $_sort , $_reverse , $_filter , $_thisUIDOnly = null , $_cacheResult = true )
{
//self::$debug=true;
if ( self :: $debug ) error_log ( __METHOD__ . __LINE__ . function_backtrace ());
if ( self :: $debug ) error_log ( __METHOD__ . __LINE__ . " $_folderName , $_startMessage , $_numberOfMessages , $_sort , $_reverse , " . array2string ( $_filter ) . " , $_thisUIDOnly " );
$reverse = ( bool ) $_reverse ;
// get the list of messages to fetch
2013-11-19 13:03:53 +01:00
if ( self :: $debug ) $starttime = microtime ( true );
2013-02-08 20:11:44 +01:00
$this -> reopen ( $_folderName );
if ( self :: $debug )
{
$endtime = microtime ( true ) - $starttime ;
error_log ( __METHOD__ . " time used for reopen: " . $endtime . ' for Folder:' . $_folderName );
}
2013-10-31 09:55:33 +01:00
//$currentFolder = $this->icServer->getCurrentMailbox();
2013-10-29 17:16:56 +01:00
//if ($currentFolder != $_folderName); $this->icServer->openMailbox($_folderName);
2013-02-08 20:11:44 +01:00
$rByUid = true ; // try searching by uid. this var will be passed by reference to getSortedList, and may be set to false, if UID retrieval fails
#print "<pre>";
#$this->icServer->setDebug(true);
if ( $_thisUIDOnly === null )
{
if (( $_startMessage || $_numberOfMessages ) && ! isset ( $_filter [ 'range' ]))
{
// this will not work we must calculate the range we want to retieve as e.g.: 0:20 retirieves the first 20 mails and sorts them
// if sort capability is applied to the range fetched, not sort first and fetch the range afterwards
$start = $_startMessage - 1 ;
$end = $_startMessage - 1 + $_numberOfMessages ;
//$_filter['range'] ="$start:$end";
//$_filter['range'] ="$_startMessage:*";
}
if ( self :: $debug ) error_log ( __METHOD__ . __LINE__ . " $_folderName , $_sort , $reverse , " . array2string ( $_filter ) . " , $rByUid " );
if ( self :: $debug ) $starttime = microtime ( true );
2013-11-04 12:22:38 +01:00
//see this example below for a 12 week datefilter (since)
//$_filter = array('status'=>array('UNDELETED'),'type'=>"SINCE",'string'=> date("d-M-Y", $starttime-(3600*24*7*12)));
2013-10-24 09:15:48 +02:00
$_sortResult = $this -> getSortedList ( $_folderName , $_sort , $reverse , $_filter , $rByUid , $_cacheResult );
$sortResult = $_sortResult [ 'match' ] -> ids ;
2013-02-08 20:11:44 +01:00
if ( self :: $debug )
{
$endtime = microtime ( true ) - $starttime ;
error_log ( __METHOD__ . " time used for getSortedList: " . $endtime . ' for Folder:' . $_folderName . ' Filter:' . array2string ( $_filter ) . ' Ids:' . array2string ( $_thisUIDOnly ));
}
if ( self :: $debug ) error_log ( __METHOD__ . __LINE__ . array2string ( $sortResult ));
#$this->icServer->setDebug(false);
#print "</pre>";
// nothing found
if ( ! is_array ( $sortResult ) || empty ( $sortResult )) {
$retValue = array ();
$retValue [ 'info' ][ 'total' ] = 0 ;
$retValue [ 'info' ][ 'first' ] = 0 ;
$retValue [ 'info' ][ 'last' ] = 0 ;
return $retValue ;
}
2013-10-24 09:15:48 +02:00
$total = $_sortResult [ 'count' ];
2013-02-08 20:11:44 +01:00
#_debug_array($sortResult);
#_debug_array(array_slice($sortResult, -5, -2));
2013-10-25 12:23:36 +02:00
//error_log("REVERSE: $reverse");
2013-02-08 20:11:44 +01:00
if ( $reverse === true ) {
if ( $_startMessage <= $total )
{
$startMessage = $_startMessage - 1 ;
}
else
{
//error_log(__METHOD__.__LINE__.' Start:'.$_startMessage.' NumberOfMessages:'.$_numberOfMessages.' Total:'.$total);
if ( $_startMessage + $_numberOfMessages > $total )
{
$numberOfMessages = $total % $_numberOfMessages ;
//$numberOfMessages = abs($_startMessage-$total-1);
if ( $numberOfMessages > 0 && $numberOfMessages <= $_numberOfMessages ) $_numberOfMessages = $numberOfMessages ;
//error_log(__METHOD__.__LINE__.' Start:'.$_startMessage.' NumberOfMessages:'.$_numberOfMessages.' Total:'.$total);
}
$startMessage = ( $total - $_numberOfMessages ) - 1 ;
//$retValue['info']['first'] = $startMessage;
//$retValue['info']['last'] = $total;
}
if ( $startMessage + $_numberOfMessages > $total )
{
$_numberOfMessages = $_numberOfMessages - ( $total - ( $startMessage + $_numberOfMessages ));
//$retValue['info']['first'] = $startMessage;
//$retValue['info']['last'] = $total;
}
if ( $startMessage > 0 ) {
if ( self :: $debug ) error_log ( __METHOD__ . __LINE__ . ' StartMessage:' . ( - ( $_numberOfMessages + $startMessage )) . ', ' .- $startMessage . ' Number of Messages:' . count ( $sortResult ));
$sortResult = array_slice ( $sortResult , - ( $_numberOfMessages + $startMessage ), - $startMessage );
} else {
if ( self :: $debug ) error_log ( __METHOD__ . __LINE__ . ' StartMessage:' . ( - ( $_numberOfMessages + ( $_startMessage - 1 ))) . ', AllTheRest, Number of Messages:' . count ( $sortResult ));
$sortResult = array_slice ( $sortResult , - ( $_numberOfMessages + ( $_startMessage - 1 )));
}
$sortResult = array_reverse ( $sortResult );
} else {
if ( self :: $debug ) error_log ( __METHOD__ . __LINE__ . ' StartMessage:' . ( $_startMessage - 1 ) . ', ' . $_numberOfMessages . ' Number of Messages:' . count ( $sortResult ));
$sortResult = array_slice ( $sortResult , $_startMessage - 1 , $_numberOfMessages );
}
if ( self :: $debug ) error_log ( __METHOD__ . __LINE__ . array2string ( $sortResult ));
}
else
{
$sortResult = ( is_array ( $_thisUIDOnly ) ? $_thisUIDOnly : ( array ) $_thisUIDOnly );
}
2013-10-24 09:15:48 +02:00
//$queryString = implode(',', $sortResult);
2013-02-08 20:11:44 +01:00
// fetch the data for the selected messages
if ( self :: $debug ) $starttime = microtime ( true );
2013-10-24 15:34:53 +02:00
$uidsToFetch = new Horde_Imap_Client_Ids ();
$uidsToFetch -> add ( $sortResult );
$fquery = new Horde_Imap_Client_Fetch_Query ();
// $fquery->headers('headers', array('Subject', 'From', 'To', 'Cc', 'Date'), array('peek' => true,'cache' => true));
$fquery -> envelope ();
$fquery -> size ();
2013-11-05 15:16:40 +01:00
$fquery -> structure ();
2013-10-24 15:34:53 +02:00
$fquery -> flags ();
// $fquery->imapDate();
$headersNew = $this -> icServer -> fetch ( $_folderName , $fquery , array (
'ids' => $uidsToFetch ,
));
//error_log(__METHOD__.__LINE__.array2string($headersNew->ids()));
2013-02-08 20:11:44 +01:00
if ( PEAR :: isError ( $headersNew ) && empty ( $queryString ))
{
$headersNew = array ();
$sortResult = array ();
}
if ( self :: $debug )
{
$endtime = microtime ( true ) - $starttime ;
error_log ( __METHOD__ . " time used for getSummary: " . $endtime . ' for Folder:' . $_folderName . ' Filter:' . array2string ( $_filter ));
error_log ( __METHOD__ . __LINE__ . ' Query:' . $queryString . ' Result:' . array2string ( $headersNew ));
}
$count = 0 ;
foreach (( array ) $sortResult as $uid ) {
$sortOrder [ $uid ] = $count ++ ;
}
$count = 0 ;
2013-10-24 15:34:53 +02:00
if ( is_object ( $headersNew )) {
2013-02-08 20:11:44 +01:00
if ( self :: $debug ) $starttime = microtime ( true );
2013-10-24 15:34:53 +02:00
foreach ( $headersNew -> ids () as $id ) {
$_headerObject = $headersNew -> get ( $id );
$uid = $headerObject [ 'UID' ] = ( $_headerObject -> getUid () ? $_headerObject -> getUid () : $id );
//error_log(__METHOD__.__LINE__.array2string($_headerObject));
$headerObject [ 'MSG_NUM' ] = $_headerObject -> getSeq ();
$headerObject [ 'SIZE' ] = $_headerObject -> getSize ();
2013-10-29 17:16:56 +01:00
$headerObject [ 'DATE' ] = $_headerObject -> getEnvelope () -> date ;
2013-10-24 15:34:53 +02:00
$headerObject [ 'INTERNALDATE' ] = $_headerObject -> getImapDate ();
2013-10-29 17:16:56 +01:00
$headerObject [ 'SUBJECT' ] = $_headerObject -> getEnvelope () -> subject ;
$headerObject [ 'FROM' ] = $_headerObject -> getEnvelope () -> from -> addresses ;
$headerObject [ 'TO' ] = $_headerObject -> getEnvelope () -> to -> addresses ;
$headerObject [ 'CC' ] = $_headerObject -> getEnvelope () -> cc -> addresses ;
2013-11-29 14:29:49 +01:00
foreach ( array ( 'FROM' , 'TO' , 'CC' ) as $_k => $key )
{
$address = array ();
foreach ( $headerObject [ $key ] as $k => $ad )
{
if ( stripos ( $ad , '@' ) === false )
{
$remember = $k ;
}
else
{
$address [] = ( ! is_null ( $remember ) ? $headerObject [ $key ][ $remember ] . ' ' : '' ) . $ad ;
$remember = null ;
}
}
$headerObject [ $key ] = $address ;
}
2013-10-24 15:34:53 +02:00
$headerObject [ 'FLAGS' ] = $_headerObject -> getFlags ();
2013-11-05 15:16:40 +01:00
$mailStructureObject = $_headerObject -> getStructure ();
2013-10-24 17:29:57 +02:00
//error_log(__METHOD__.__LINE__.array2string($headerObject));
2013-11-05 15:16:40 +01:00
//error_log(__METHOD__.__LINE__.' MimeMap:'.array2string($mailStructureObject->contentTypeMap()));
//foreach ($_headerObject->getStructure()->getParts() as $p => $part)
$headerObject [ 'ATTACHMENTS' ] = null ;
2014-02-13 13:59:43 +01:00
$skipParts = array ();
2013-11-05 15:16:40 +01:00
foreach ( $mailStructureObject -> contentTypeMap () as $mime_id => $mime_type )
{
if ( $mime_id == 0 ) $messageMimeType = $mime_type ;
$part = $mailStructureObject -> getPart ( $mime_id );
2014-01-27 16:04:18 +01:00
$partdisposition = $part -> getDisposition ();
2014-02-13 13:59:43 +01:00
$partPrimaryType = $part -> getPrimaryType ();
2014-01-27 16:04:18 +01:00
$cid = $part -> getContentId ();
2014-02-13 13:59:43 +01:00
if ( $mime_type == 'message/rfc822' )
{
//error_log(__METHOD__.__LINE__.' Uid:'.$uid.'->'.$mime_id.':'.array2string($part->contentTypeMap()));
foreach ( $part -> contentTypeMap () as $sub_id => $sub_type ) if ( $sub_id != $mime_id ) $skipParts [ $sub_id ] = $sub_type ;
}
//error_log(__METHOD__.__LINE__.' Uid:'.$uid.'->'.$mime_id.':'.array2string($skipParts));
if ( array_key_exists ( $mime_id , $skipParts )) continue ;
if ( $partdisposition == 'attachment' ||
( $partdisposition == 'inline' && $partPrimaryType == 'image' && empty ( $cid )) ||
( $partdisposition == 'inline' && $partPrimaryType != 'image' && $partPrimaryType != 'multipart' && $partPrimaryType != 'text' ))
2013-11-05 15:16:40 +01:00
{
$headerObject [ 'ATTACHMENTS' ][ $mime_id ] = $part -> getAllDispositionParameters ();
$headerObject [ 'ATTACHMENTS' ][ $mime_id ][ 'mimeType' ] = $mime_type ;
2014-01-27 16:04:18 +01:00
$headerObject [ 'ATTACHMENTS' ][ $mime_id ][ 'uid' ] = $uid ;
$headerObject [ 'ATTACHMENTS' ][ $mime_id ][ 'cid' ] = $cid ;
2013-11-05 15:16:40 +01:00
$headerObject [ 'ATTACHMENTS' ][ $mime_id ][ 'partID' ] = $mime_id ;
if ( ! isset ( $headerObject [ 'ATTACHMENTS' ][ $mime_id ][ 'name' ])) $headerObject [ 'ATTACHMENTS' ][ $mime_id ][ 'name' ] = $part -> getName ();
//error_log(__METHOD__.__LINE__.' PartDisposition:'.$mime_id.'->'.array2string($part->getName()));
//error_log(__METHOD__.__LINE__.' PartDisposition:'.$mime_id.'->'.array2string($part->getAllDispositionParameters()));
//error_log(__METHOD__.__LINE__.' Attachment:'.$mime_id.'->'.array2string($headerObject['ATTACHMENTS'][$mime_id]));
}
}
//error_log(__METHOD__.__LINE__.' FindBody (plain):'.array2string($mailStructureObject->findBody('plain')));
//error_log(__METHOD__.__LINE__.' FindBody (html):'.array2string($mailStructureObject->findBody('html')));
2013-02-08 20:11:44 +01:00
//if($count == 0) error_log(__METHOD__.array2string($headerObject));
if ( empty ( $headerObject [ 'UID' ])) continue ;
2013-10-24 15:34:53 +02:00
//$uid = ($rByUid ? $headerObject['UID'] : $headerObject['MSG_NUM']);
2013-02-08 20:11:44 +01:00
// make dates like "Mon, 23 Apr 2007 10:11:06 UT" working with strtotime
if ( substr ( $headerObject [ 'DATE' ], - 2 ) === 'UT' ) {
$headerObject [ 'DATE' ] .= 'C' ;
}
if ( substr ( $headerObject [ 'INTERNALDATE' ], - 2 ) === 'UT' ) {
$headerObject [ 'INTERNALDATE' ] .= 'C' ;
}
//error_log(__METHOD__.__LINE__.' '.$headerObject['SUBJECT'].'->'.$headerObject['DATE']);
//error_log(__METHOD__.__LINE__.' '.$this->decode_subject($headerObject['SUBJECT']).'->'.$headerObject['DATE']);
2013-11-05 15:16:40 +01:00
if ( isset ( $headerObject [ 'ATTACHMENTS' ]) && count ( $headerObject [ 'ATTACHMENTS' ])) foreach ( $headerObject [ 'ATTACHMENTS' ] as $pID => $a ) $retValue [ 'header' ][ $sortOrder [ $uid ]][ 'attachments' ][] = $a ;
2013-02-08 20:11:44 +01:00
$retValue [ 'header' ][ $sortOrder [ $uid ]][ 'subject' ] = $this -> decode_subject ( $headerObject [ 'SUBJECT' ]);
$retValue [ 'header' ][ $sortOrder [ $uid ]][ 'size' ] = $headerObject [ 'SIZE' ];
$retValue [ 'header' ][ $sortOrder [ $uid ]][ 'date' ] = self :: _strtotime (( $headerObject [ 'DATE' ] &&! ( $headerObject [ 'DATE' ] == 'NIL' ) ? $headerObject [ 'DATE' ] : $headerObject [ 'INTERNALDATE' ]), 'ts' , true );
$retValue [ 'header' ][ $sortOrder [ $uid ]][ 'internaldate' ] = self :: _strtotime ( $headerObject [ 'INTERNALDATE' ], 'ts' , true );
2013-11-05 15:16:40 +01:00
$retValue [ 'header' ][ $sortOrder [ $uid ]][ 'mimetype' ] = $messageMimeType ;
2013-02-08 20:11:44 +01:00
$retValue [ 'header' ][ $sortOrder [ $uid ]][ 'id' ] = $headerObject [ 'MSG_NUM' ];
$retValue [ 'header' ][ $sortOrder [ $uid ]][ 'uid' ] = $headerObject [ 'UID' ];
$retValue [ 'header' ][ $sortOrder [ $uid ]][ 'priority' ] = ( $headerObject [ 'PRIORITY' ] ? $headerObject [ 'PRIORITY' ] : 3 );
if ( is_array ( $headerObject [ 'FLAGS' ])) {
2013-07-24 09:34:17 +02:00
$retValue [ 'header' ][ $sortOrder [ $uid ]] = array_merge ( $retValue [ 'header' ][ $sortOrder [ $uid ]], self :: prepareFlagsArray ( $headerObject ));
2013-02-08 20:11:44 +01:00
}
2013-11-05 15:16:40 +01:00
//error_log(__METHOD__.__LINE__.$headerObject['SUBJECT'].'->'.array2string($_headerObject->getEnvelope()->__get('from')));
2013-10-24 17:29:57 +02:00
if ( is_array ( $headerObject [ 'FROM' ]) && $headerObject [ 'FROM' ][ 0 ]) {
$retValue [ 'header' ][ $sortOrder [ $uid ]][ 'sender_address' ] = self :: decode_header ( $headerObject [ 'FROM' ][ 0 ]);
2013-02-08 20:11:44 +01:00
}
2013-10-24 17:29:57 +02:00
if ( is_array ( $headerObject [ 'TO' ]) && $headerObject [ 'TO' ][ 0 ]) {
$retValue [ 'header' ][ $sortOrder [ $uid ]][ 'to_address' ] = self :: decode_header ( $headerObject [ 'TO' ][ 0 ]);
2013-02-08 20:11:44 +01:00
if ( count ( $headerObject [ 'TO' ]) > 1 )
{
$ki = 0 ;
foreach ( $headerObject [ 'TO' ] as $k => $add )
{
if ( $k == 0 ) continue ;
//error_log(__METHOD__.__LINE__."-> $k:".array2string($add));
2013-12-03 17:46:32 +01:00
$retValue [ 'header' ][ $sortOrder [ $uid ]][ 'additional_to_addresses' ][ $ki ] = self :: decode_header ( $add );
2013-02-08 20:11:44 +01:00
//error_log(__METHOD__.__LINE__.array2string($retValue['header'][$sortOrder[$uid]]['additional_to_addresses'][$ki]));
$ki ++ ;
}
}
}
2013-12-03 15:38:50 +01:00
if ( is_array ( $headerObject [ 'CC' ]) && $headerObject [ 'CC' ][ 0 ]) {
$ki = 0 ;
foreach ( $headerObject [ 'CC' ] as $k => $add )
{
//error_log(__METHOD__.__LINE__."-> $k:".array2string($add));
2013-12-03 17:46:32 +01:00
$retValue [ 'header' ][ $sortOrder [ $uid ]][ 'cc_addresses' ][ $ki ] = self :: decode_header ( $add );
2013-12-03 15:38:50 +01:00
//error_log(__METHOD__.__LINE__.array2string($retValue['header'][$sortOrder[$uid]]['additional_to_addresses'][$ki]));
$ki ++ ;
}
}
2013-10-24 17:29:57 +02:00
//error_log(__METHOD__.__LINE__.array2string($retValue['header'][$sortOrder[$uid]]));
2013-02-08 20:11:44 +01:00
$count ++ ;
}
2013-11-19 13:03:53 +01:00
if ( self :: $debug )
2013-02-08 20:11:44 +01:00
{
$endtime = microtime ( true ) - $starttime ;
2013-10-24 09:15:48 +02:00
error_log ( __METHOD__ . __LINE__ . " time used: " . $endtime . ' for Folder:' . $_folderName );
2013-02-08 20:11:44 +01:00
}
//self::$debug=false;
// sort the messages to the requested displayorder
if ( is_array ( $retValue [ 'header' ])) {
$countMessages = $total ;
2013-10-22 12:06:21 +02:00
if ( isset ( $_filter [ 'range' ])) $countMessages = self :: $folderStatusCache [ $this -> profileID ][ $_folderName ][ 'messages' ];
2013-02-08 20:11:44 +01:00
ksort ( $retValue [ 'header' ]);
$retValue [ 'info' ][ 'total' ] = $total ;
//if ($_startMessage>$total) $_startMessage = $total-($count-1);
$retValue [ 'info' ][ 'first' ] = $_startMessage ;
$retValue [ 'info' ][ 'last' ] = $_startMessage + $count - 1 ;
return $retValue ;
} else {
$retValue = array ();
$retValue [ 'info' ][ 'total' ] = 0 ;
$retValue [ 'info' ][ 'first' ] = 0 ;
$retValue [ 'info' ][ 'last' ] = 0 ;
return $retValue ;
}
} else {
if ( $headersNew == null && empty ( $_thisUIDOnly )) error_log ( __METHOD__ . " -> retrieval of Message Details to Query $queryString failed: " . print_r ( $headersNew , TRUE ));
$retValue = array ();
$retValue [ 'info' ][ 'total' ] = 0 ;
$retValue [ 'info' ][ 'first' ] = 0 ;
$retValue [ 'info' ][ 'last' ] = 0 ;
return $retValue ;
}
}
2013-07-24 09:34:17 +02:00
/**
* static function prepareFlagsArray
* prepare headerObject to return some standardized array to tell which flags are set for a message
* @ param array $headerObject - array to process , a full return array from icServer -> getSummary
* @ return array array of flags
*/
static function prepareFlagsArray ( $headerObject )
{
if ( is_array ( $headerObject [ 'FLAGS' ])) $headerFlags = array_map ( 'strtolower' , $headerObject [ 'FLAGS' ]);
2013-10-24 15:34:53 +02:00
$retValue = array ();
$retValue [ 'recent' ] = in_array ( '\\recent' , $headerFlags );
$retValue [ 'flagged' ] = in_array ( '\\flagged' , $headerFlags );
$retValue [ 'answered' ] = in_array ( '\\answered' , $headerFlags );
$retValue [ 'forwarded' ] = in_array ( '$forwarded' , $headerFlags );
$retValue [ 'deleted' ] = in_array ( '\\deleted' , $headerFlags );
$retValue [ 'seen' ] = in_array ( '\\seen' , $headerFlags );
$retValue [ 'draft' ] = in_array ( '\\draft' , $headerFlags );
$retValue [ 'mdnsent' ] = in_array ( 'mdnsent' , $headerFlags );
$retValue [ 'mdnnotsent' ] = in_array ( 'mdnnotsent' , $headerFlags );
$retValue [ 'label1' ] = in_array ( '$label1' , $headerFlags );
$retValue [ 'label2' ] = in_array ( '$label2' , $headerFlags );
$retValue [ 'label3' ] = in_array ( '$label3' , $headerFlags );
$retValue [ 'label4' ] = in_array ( '$label4' , $headerFlags );
$retValue [ 'label5' ] = in_array ( '$label5' , $headerFlags );
//error_log(__METHOD__.__LINE__.array2string($retValue));
2013-07-24 09:34:17 +02:00
return $retValue ;
}
2013-02-08 20:11:44 +01:00
/**
* fetches a sorted list of messages from the imap server
* private function
*
* @ todo implement sort based on Net_IMAP
* @ param string $_folderName the name of the folder in which the messages get searched
* @ param integer $_sort the primary sort key
* @ param bool $_reverse sort the messages ascending or descending
* @ param array $_filter the search filter
* @ param bool $resultByUid if set to true , the result is to be returned by uid , if the server does not reply
* on a query for uids , the result may be returned by IDs only , this will be indicated by this param
* @ param bool $setSession if set to true the session will be populated with the result of the query
* @ return mixed bool / array false or array of ids
*/
function getSortedList ( $_folderName , $_sort , & $_reverse , $_filter , & $resultByUid = true , $setSession = true )
{
2013-11-19 12:17:32 +01:00
$folderStatus = $this -> icServer -> examineMailbox ( $_folderName );
2013-11-06 16:11:15 +01:00
//error_log(__METHOD__.__LINE__.' F:'.$_folderName.' S:'.array2string($folderStatus));
2013-02-08 20:11:44 +01:00
//error_log(__METHOD__.__LINE__.' Filter:'.array2string($_filter));
$try2useCache = true ;
static $eMailListContainsDeletedMessages ;
if ( is_null ( $eMailListContainsDeletedMessages )) $eMailListContainsDeletedMessages = egw_cache :: getCache ( egw_cache :: INSTANCE , 'email' , 'eMailListContainsDeletedMessages' . trim ( $GLOBALS [ 'egw_info' ][ 'user' ][ 'account_id' ]), $callback = null , $callback_params = array (), $expiration = 60 * 60 * 1 );
// this indicates, that there is no Filter set, and the returned set/subset should not contain DELETED Messages, nor filtered for UNDELETED
if ( $setSession == true && (( strpos ( array2string ( $_filter ), 'UNDELETED' ) === false && strpos ( array2string ( $_filter ), 'DELETED' ) === false )))
{
//$starttime = microtime(true);
//$deletedMessages = $this->getSortedList($_folderName, $_sort=0, $_reverse=1, $_filter=array('status'=>array('DELETED')),$byUid=true,false);
2013-11-19 09:17:49 +01:00
if ( is_null ( $eMailListContainsDeletedMessages ) || empty ( $eMailListContainsDeletedMessages [ $this -> profileID ]) || empty ( $eMailListContainsDeletedMessages [ $this -> profileID ][ $_folderName ])) $eMailListContainsDeletedMessages = egw_cache :: getCache ( egw_cache :: INSTANCE , 'email' , 'eMailListContainsDeletedMessages' . trim ( $GLOBALS [ 'egw_info' ][ 'user' ][ 'account_id' ]), $callback = null , $callback_params = array (), $expiration = 60 * 60 * 1 );
2013-02-08 20:11:44 +01:00
$deletedMessages = $this -> getSortedList ( $_folderName , 0 , $three = 1 , array ( 'status' => array ( 'DELETED' )), $five = true , false );
//error_log(__METHOD__.__LINE__.array2string($deletedMessages));
2013-10-24 09:15:48 +02:00
$eMailListContainsDeletedMessages [ $this -> profileID ][ $_folderName ] = $deletedMessages [ 'count' ];
2013-02-08 20:11:44 +01:00
egw_cache :: setCache ( egw_cache :: INSTANCE , 'email' , 'eMailListContainsDeletedMessages' . trim ( $GLOBALS [ 'egw_info' ][ 'user' ][ 'account_id' ]), $eMailListContainsDeletedMessages , $expiration = 60 * 60 * 1 );
//$endtime = microtime(true);
//$r = ($endtime-$starttime);
2013-11-19 09:17:49 +01:00
//error_log(__METHOD__.__LINE__.' Profile:'.$this->profileID.' Folder:'.$_folderName.' -> EXISTS/SessStat:'.array2string($folderStatus['MESSAGES']).'/'.self::$folderStatusCache[$this->profileID][$_folderName]['messages'].' ListContDelMsg/SessDeleted:'.$eMailListContainsDeletedMessages[$this->profileID][$_folderName].'/'.self::$folderStatusCache[$this->profileID][$_folderName]['deleted']);
2013-02-08 20:11:44 +01:00
//error_log(__METHOD__.__LINE__.' Took:'.$r.'(s) setting eMailListContainsDeletedMessages for Profile:'.$this->profileID.' Folder:'.$_folderName.' to '.$eMailListContainsDeletedMessages[$this->profileID][$_folderName]);
}
2013-11-19 12:17:32 +01:00
$try2useCache = false ;
$filter = $this -> createIMAPFilter ( $_folderName , $_filter );
//_debug_array($filter);
2014-01-14 16:54:48 +01:00
//error_log(__METHOD__.__LINE__.array2string($filter));
2013-11-19 12:17:32 +01:00
if ( $this -> icServer -> hasCapability ( 'SORT' )) {
if ( self :: $debug ) error_log ( __METHOD__ . " Mailserver has SORT Capability, SortBy: $_sort Reverse: $_reverse " );
$sortOrder = $this -> _getSortString ( $_sort , $_reverse );
if ( $_reverse && in_array ( Horde_Imap_Client :: SORT_REVERSE , $sortOrder )) $_reverse = false ; // as we reversed the result already
2014-01-14 16:54:48 +01:00
if ( self :: $debug ) error_log ( __METHOD__ . " Mailserver runs SORT: SortBy: $sortOrder Filter: " . array2string ( $filter ));
2013-11-19 12:17:32 +01:00
$sortResult = $this -> icServer -> search ( $_folderName , $filter , array (
'sort' => $sortOrder ,));
// if there is an PEAR Error, we assume that the server is not capable of sorting
if ( PEAR :: isError ( $sortResult ))
2013-06-05 10:54:54 +02:00
{
2013-11-19 12:17:32 +01:00
$resultByUid = false ;
$sortOrder = array ( Horde_Imap_Client :: SORT_SEQUENCE );
if ( $_reverse ) array_unshift ( $sortOrder , Horde_Imap_Client :: SORT_REVERSE );
2013-10-24 09:15:48 +02:00
$sortResult = $this -> icServer -> search ( $_folderName , $filter , array (
2013-11-19 12:17:32 +01:00
'sort' => $sortOrder ));
2013-02-08 20:11:44 +01:00
if ( PEAR :: isError ( $sortResult ))
{
2013-11-19 12:17:32 +01:00
$sortResult = self :: $folderStatusCache [ $this -> profileID ][ $_folderName ][ 'sortResult' ];
2013-02-08 20:11:44 +01:00
}
}
2013-11-19 12:17:32 +01:00
if ( self :: $debug ) error_log ( __METHOD__ . print_r ( $sortResult , true ));
} else {
if ( self :: $debug ) error_log ( __METHOD__ . " Mailserver has NO SORT Capability " );
//$sortOrder = array(Horde_Imap_Client::SORT_SEQUENCE);
//if ($_reverse) array_unshift($sortOrder,Horde_Imap_Client::SORT_REVERSE);
$sortResult = $this -> icServer -> search ( $_folderName , $filter , array () /* array (
'sort' => $sortOrder ) */ );
if ( is_array ( $sortResult [ 'match' ])) {
sort ( $sortResult [ 'match' ], SORT_NUMERIC );
2013-02-08 20:11:44 +01:00
}
2013-11-19 12:17:32 +01:00
if ( self :: $debug ) error_log ( __METHOD__ . " using Filter: " . print_r ( $filter , true ) . " -> " . print_r ( $sortResult , true ));
2013-02-08 20:11:44 +01:00
}
if ( $setSession )
{
2013-11-19 12:17:32 +01:00
self :: $folderStatusCache [ $this -> profileID ][ $_folderName ][ 'uidValidity' ] = $folderStatus [ 'UIDVALIDITY' ];
self :: $folderStatusCache [ $this -> profileID ][ $_folderName ][ 'messages' ] = $folderStatus [ 'MESSAGES' ];
self :: $folderStatusCache [ $this -> profileID ][ $_folderName ][ 'deleted' ] = $eMailListContainsDeletedMessages [ $this -> profileID ][ $_folderName ];
self :: $folderStatusCache [ $this -> profileID ][ $_folderName ][ 'uidnext' ] = $folderStatus [ 'UIDNEXT' ];
self :: $folderStatusCache [ $this -> profileID ][ $_folderName ][ 'filter' ] = $_filter ;
self :: $folderStatusCache [ $this -> profileID ][ $_folderName ][ 'sortResult' ] = $sortResult ;
self :: $folderStatusCache [ $this -> profileID ][ $_folderName ][ 'sort' ] = $_sort ;
2013-02-08 20:11:44 +01:00
}
2013-10-24 09:15:48 +02:00
//_debug_array($sortResult['match']->ids);
2013-02-08 20:11:44 +01:00
return $sortResult ;
}
/**
* convert the sort value from the gui ( integer ) into a string
*
* @ param mixed _sort the integer sort order / or a valid and handeled SORTSTRING ( right now : UID / ARRIVAL / INTERNALDATE ( -> ARRIVAL ))
* @ param bool _reverse wether to add REVERSE to the Sort String or not
2013-10-24 09:15:48 +02:00
* @ return the sort sequence for horde search
2013-02-08 20:11:44 +01:00
*/
function _getSortString ( $_sort , $_reverse = false )
{
$_reverse = false ;
if ( is_numeric ( $_sort ))
{
switch ( $_sort ) {
case 2 :
2013-10-24 09:15:48 +02:00
$retValue = array ( Horde_Imap_Client :: SORT_FROM );
2013-02-08 20:11:44 +01:00
break ;
case 4 :
2013-10-24 09:15:48 +02:00
$retValue = array ( Horde_Imap_Client :: SORT_TO );
2013-02-08 20:11:44 +01:00
break ;
case 3 :
2013-10-24 09:15:48 +02:00
$retValue = array ( Horde_Imap_Client :: SORT_SUBJECT );
2013-02-08 20:11:44 +01:00
break ;
case 6 :
2013-10-24 09:15:48 +02:00
$retValue = array ( Horde_Imap_Client :: SORT_SIZE );
2013-02-08 20:11:44 +01:00
break ;
case 0 :
default :
2013-10-24 09:15:48 +02:00
$retValue = array ( Horde_Imap_Client :: SORT_DATE );
2013-02-08 20:11:44 +01:00
//$retValue = 'ARRIVAL';
break ;
}
}
else
{
2013-09-23 10:33:30 +02:00
switch ( strtoupper ( $_sort )) {
case 'FROMADDRESS' :
2013-10-24 09:15:48 +02:00
$retValue = array ( Horde_Imap_Client :: SORT_FROM );
2013-09-23 10:33:30 +02:00
break ;
case 'TOADDRESS' :
2013-10-24 09:15:48 +02:00
$retValue = array ( Horde_Imap_Client :: SORT_TO );
2013-09-23 10:33:30 +02:00
break ;
case 'SUBJECT' :
2013-10-24 09:15:48 +02:00
$retValue = array ( Horde_Imap_Client :: SORT_SUBJECT );
2013-09-23 10:33:30 +02:00
break ;
case 'SIZE' :
2013-10-24 09:15:48 +02:00
$retValue = array ( Horde_Imap_Client :: SORT_SIZE );
2013-09-23 10:33:30 +02:00
break ;
2013-02-08 20:11:44 +01:00
case 'ARRIVAL' :
2013-10-24 15:34:53 +02:00
$retValue = array ( Horde_Imap_Client :: SORT_ARRIVAL );
break ;
case 'UID' : // should be equivalent to INTERNALDATE, which is ARRIVAL, which should be highest (latest) uid should be newest date
2013-02-08 20:11:44 +01:00
case 'INTERNALDATE' :
2013-10-24 09:15:48 +02:00
$retValue = array ( Horde_Imap_Client :: SORT_SEQUENCE );
2013-02-08 20:11:44 +01:00
break ;
2013-09-23 10:33:30 +02:00
case 'DATE' :
2013-02-08 20:11:44 +01:00
default :
2013-10-24 09:15:48 +02:00
$retValue = array ( Horde_Imap_Client :: SORT_DATE );
2013-02-08 20:11:44 +01:00
break ;
}
}
2013-10-24 09:15:48 +02:00
if ( $_reverse ) array_unshift ( $retValue , Horde_Imap_Client :: SORT_REVERSE );
2013-02-08 20:11:44 +01:00
//error_log(__METHOD__.__LINE__.' '.($_reverse?'REVERSE ':'').$_sort.'->'.$retValue);
2013-10-24 09:15:48 +02:00
return $retValue ;
2013-02-08 20:11:44 +01:00
}
/**
* this function creates an IMAP filter from the criterias given
*
* @ param string $_folder used to determine the search to TO or FROM on QUICK Search wether it is a send - folder or not
* @ param array $_criterias contains the search / filter criteria
2013-10-24 09:15:48 +02:00
* @ return Horde_Imap_Client_Search_Query the IMAP filter
2013-02-08 20:11:44 +01:00
*/
function createIMAPFilter ( $_folder , $_criterias )
{
2013-10-24 09:15:48 +02:00
$imapFilter = new Horde_Imap_Client_Search_Query ();
2013-11-04 12:22:38 +01:00
2013-02-08 20:11:44 +01:00
//_debug_array($_criterias);
if ( self :: $debug ) error_log ( __METHOD__ . __LINE__ . ' Criterias:' . ( ! is_array ( $_criterias ) ? " none -> returning $all " : array2string ( $_criterias )));
2014-01-20 12:53:19 +01:00
if (( ! is_array ( $_criterias ) || $_criterias [ 'status' ] == 'any' ) && ( ! isset ( $_criterias [ 'string' ]) || empty ( $_criterias [ 'string' ]))) {
2014-01-14 16:54:48 +01:00
$imapFilter -> flag ( 'DELETED' , $set = false );
return $imapFilter ;
2013-02-08 20:11:44 +01:00
}
2014-01-14 16:54:48 +01:00
//error_log(__METHOD__.__LINE__.print_r($_criterias, true));
2013-11-04 12:22:38 +01:00
$queryValid = false ;
2013-02-08 20:11:44 +01:00
if ( ! empty ( $_criterias [ 'string' ])) {
$criteria = strtoupper ( $_criterias [ 'type' ]);
switch ( $criteria ) {
case 'QUICK' :
2013-11-04 12:22:38 +01:00
$imapFilter -> headerText ( 'SUBJECT' , $_criterias [ 'string' ], $not = false );
$imapFilter2 = new Horde_Imap_Client_Search_Query ();
2013-02-08 20:11:44 +01:00
if ( $this -> isSentFolder ( $_folder )) {
2013-11-04 12:22:38 +01:00
$imapFilter2 -> headerText ( 'TO' , $_criterias [ 'string' ], $not = false );
2013-02-08 20:11:44 +01:00
} else {
2013-11-04 12:22:38 +01:00
$imapFilter2 -> headerText ( 'FROM' , $_criterias [ 'string' ], $not = false );
2013-02-08 20:11:44 +01:00
}
2013-11-04 12:22:38 +01:00
$imapFilter -> orSearch ( $imapFilter2 );
$queryValid = true ;
2013-02-08 20:11:44 +01:00
break ;
case 'FROM' :
2013-11-04 12:22:38 +01:00
case 'TO' :
case 'CC' :
case 'BCC' :
2013-02-08 20:11:44 +01:00
case 'SUBJECT' :
2013-11-04 12:22:38 +01:00
$imapFilter -> headerText ( $criteria , $_criterias [ 'string' ], $not = false );
$queryValid = true ;
break ;
case 'BODY' :
2013-02-08 20:11:44 +01:00
case 'TEXT' :
2013-11-04 12:22:38 +01:00
$imapFilter -> text ( $_criterias [ 'string' ],( $criteria == 'BODY' ? true : false ), $not = false );
2013-02-08 20:11:44 +01:00
break ;
case 'SINCE' :
2013-11-04 12:22:38 +01:00
$imapFilter -> dateSearch ( new DateTime ( $_criterias [ 'string' ]), Horde_Imap_Client_Search_Query :: DATE_SINCE , $header = true , $not = false );
break ;
2013-02-08 20:11:44 +01:00
case 'BEFORE' :
2013-11-04 12:22:38 +01:00
$imapFilter -> dateSearch ( new DateTime ( $_criterias [ 'string' ]), Horde_Imap_Client_Search_Query :: DATE_BEFORE , $header = true , $not = false );
break ;
2013-02-08 20:11:44 +01:00
case 'ON' :
2013-11-04 12:22:38 +01:00
$imapFilter -> dateSearch ( new DateTime ( $_criterias [ 'string' ]), Horde_Imap_Client_Search_Query :: DATE_ON , $header = true , $not = false );
2013-02-08 20:11:44 +01:00
break ;
}
}
foreach (( array ) $_criterias [ 'status' ] as $k => $criteria ) {
$criteria = strtoupper ( $criteria );
switch ( $criteria ) {
case 'ANSWERED' :
case 'DELETED' :
case 'FLAGGED' :
case 'RECENT' :
case 'SEEN' :
2014-01-23 15:46:57 +01:00
$imapFilter -> flag ( $criteria , $set = true );
$queryValid = true ;
break ;
2013-02-08 20:11:44 +01:00
case 'KEYWORD1' :
case 'KEYWORD2' :
case 'KEYWORD3' :
case 'KEYWORD4' :
case 'KEYWORD5' :
2014-01-23 15:46:57 +01:00
$imapFilter -> flag ( str_ireplace ( 'KEYWORD' , '$LABEL' , $criteria ), $set = true );
2013-11-04 12:22:38 +01:00
$queryValid = true ;
break ;
case 'NEW' :
$imapFilter -> flag ( 'RECENT' , $set = true );
$imapFilter -> flag ( 'SEEN' , $set = false );
$queryValid = true ;
break ;
case 'OLD' :
$imapFilter -> flag ( 'RECENT' , $set = false );
break ;
2014-01-09 12:26:40 +01:00
// operate only on system flags
// $systemflags = array(
// 'ANSWERED', 'DELETED', 'DRAFT', 'FLAGGED', 'RECENT', 'SEEN'
// );
2013-11-04 12:22:38 +01:00
case 'UNANSWERED' :
2014-01-09 12:26:40 +01:00
$imapFilter -> flag ( 'ANSWERED' , $set = false );
$queryValid = true ;
break ;
2013-11-04 12:22:38 +01:00
case 'UNDELETED' :
2014-01-09 12:26:40 +01:00
$imapFilter -> flag ( 'DELETED' , $set = false );
$queryValid = true ;
break ;
2013-11-04 12:22:38 +01:00
case 'UNFLAGGED' :
2014-01-09 12:26:40 +01:00
$imapFilter -> flag ( 'FLAGGED' , $set = false );
$queryValid = true ;
break ;
2013-11-04 12:22:38 +01:00
case 'UNSEEN' :
2014-01-09 12:26:40 +01:00
$imapFilter -> flag ( 'SEEN' , $set = false );
2013-11-04 12:22:38 +01:00
$queryValid = true ;
2013-02-08 20:11:44 +01:00
break ;
}
}
if ( isset ( $_criterias [ 'range' ]) && ! empty ( $_criterias [ 'range' ]))
{
2013-11-04 12:22:38 +01:00
//$imapFilter .= $_criterias['range'].' ';
2013-02-08 20:11:44 +01:00
}
2013-11-04 12:22:38 +01:00
if ( self :: $debug )
{
$query_str = $imapFilter -> build ();
error_log ( __METHOD__ . __LINE__ . ' ' . $query_str [ 'query' ]);
}
if ( $queryValid == false ) {
2014-01-14 16:54:48 +01:00
$imapFilter -> flag ( 'DELETED' , $set = false );
return $imapFilter ;
2013-02-08 20:11:44 +01:00
} else {
2013-11-04 12:22:38 +01:00
return $imapFilter ;
2013-02-08 20:11:44 +01:00
}
}
/**
* decode header ( or envelope information )
* if array given , note that only values will be converted
* @ param mixed $_string input to be converted , if array call decode_header recursively on each value
* @ param mixed / boolean $_tryIDNConversion ( true / false AND FORCE ) : try IDN Conversion on domainparts of emailADRESSES
* @ return mixed - based on the input type
*/
static function decode_header ( $_string , $_tryIDNConversion = false )
{
if ( is_array ( $_string ))
{
foreach ( $_string as $k => $v )
{
$_string [ $k ] = self :: decode_header ( $v , $_tryIDNConversion );
}
return $_string ;
}
else
{
$_string = translation :: decodeMailHeader ( $_string , self :: $displayCharset );
if ( $_tryIDNConversion === true && stripos ( $_string , '@' ) !== false )
{
2014-01-15 11:31:08 +01:00
$rfcAddr = imap_rfc822_parse_adrlist ( str_replace ( ',' , '\,' , $_string ), '' );
2013-02-08 20:11:44 +01:00
if ( ! isset ( self :: $idna2 )) self :: $idna2 = new egw_idna ;
2013-08-21 12:47:20 +02:00
$stringA = array ();
2013-02-08 20:11:44 +01:00
//$_string = str_replace($rfcAddr[0]->host,self::$idna2->decode($rfcAddr[0]->host),$_string);
2013-08-21 12:47:20 +02:00
foreach (( array ) $rfcAddr as $_rfcAddr )
{
if ( $_rfcAddr -> host == '.SYNTAX-ERROR.' )
{
$stringA = array ();
break ; // skip idna conversion if we encounter an error here
}
$stringA [] = imap_rfc822_write_address ( $_rfcAddr -> mailbox , self :: $idna2 -> decode ( $_rfcAddr -> host ), $_rfcAddr -> personal );
}
if ( ! empty ( $stringA )) $_string = implode ( ',' , $stringA );
2013-02-08 20:11:44 +01:00
}
if ( $_tryIDNConversion === 'FORCE' )
{
//error_log(__METHOD__.__LINE__.'->'.$_string.'='.self::$idna2->decode($_string));
$_string = self :: $idna2 -> decode ( $_string );
}
return $_string ;
}
}
/**
* decode subject
* if array given , note that only values will be converted
* @ param mixed $_string input to be converted , if array call decode_header recursively on each value
* @ param boolean $decode try decoding
* @ return mixed - based on the input type
*/
function decode_subject ( $_string , $decode = true )
{
#$string = $_string;
if ( $_string == 'NIL' )
{
return 'No Subject' ;
}
if ( $decode ) $_string = self :: decode_header ( $_string );
2013-05-23 10:16:25 +02:00
// make sure its utf-8
$test = @ json_encode ( $_string );
if (( $test == " null " || $test === false || is_null ( $test )) && strlen ( $_string ) > 0 )
{
$_string = utf8_encode ( $_string );
}
2013-02-08 20:11:44 +01:00
return $_string ;
}
2013-04-12 11:22:23 +02:00
/**
* decodeEntityFolderName - remove html entities
* @ param string _folderName the foldername
* @ return string the converted string
*/
function decodeEntityFolderName ( $_folderName )
{
return html_entity_decode ( $_folderName , ENT_QUOTES , self :: $displayCharset );
}
2013-02-08 20:11:44 +01:00
/**
* convert a mailboxname from utf7 - imap to displaycharset
*
* @ param string _folderName the foldername
* @ return string the converted string
*/
function encodeFolderName ( $_folderName )
{
return translation :: convert ( $_folderName , 'UTF7-IMAP' , self :: $displayCharset );
}
2013-04-12 11:22:23 +02:00
/**
* convert the foldername from display charset to UTF - 7
*
* @ param string _parent the parent foldername
* @ return ISO - 8859 - 1 / UTF7 - IMAP encoded string
*/
function _encodeFolderName ( $_folderName ) {
return translation :: convert ( $_folderName , self :: $displayCharset , 'ISO-8859-1' );
#return translation::convert($_folderName, self::$displayCharset, 'UTF7-IMAP');
}
2013-05-21 10:46:54 +02:00
/**
* create a new folder under given parent folder
*
* @ param string _parent the parent foldername
* @ param string _folderName the new foldername
* @ param bool _subscribe subscribe to the new folder
*
* @ return mixed name of the newly created folder or false on error
*/
function createFolder ( $_parent , $_folderName , $_subscribe = false )
{
if ( self :: $debug ) error_log ( __METHOD__ . __LINE__ . " -> " . " $_parent , $_folderName , $_subscribe " );
2013-12-12 14:34:35 +01:00
$parent = $_parent ; //$this->_encodeFolderName($_parent);
$folderName = $_folderName ; //$this->_encodeFolderName($_folderName);
2013-05-21 10:46:54 +02:00
if ( empty ( $parent )) {
$newFolderName = $folderName ;
} else {
$HierarchyDelimiter = $this -> getHierarchyDelimiter ();
$newFolderName = $parent . $HierarchyDelimiter . $folderName ;
}
if ( self :: $debug ) error_log ( __METHOD__ . __LINE__ . '->' . $newFolderName );
2014-02-05 10:14:05 +01:00
if ( $this -> folderExists ( $newFolderName , true ))
2013-05-21 10:46:54 +02:00
{
error_log ( __METHOD__ . __LINE__ . " Folder $newFolderName already exists. " );
return $newFolderName ;
}
$rv = $this -> icServer -> createMailbox ( $newFolderName );
if ( PEAR :: isError ( $rv ) ) {
error_log ( __METHOD__ . __LINE__ . ' create Folder ' . $newFolderName . '->' . $rv -> message . ' Namespace:' . array2string ( $this -> icServer -> getNameSpaces ()));
return false ;
}
$srv = $this -> icServer -> subscribeMailbox ( $newFolderName );
if ( PEAR :: isError ( $srv ) ) {
error_log ( __METHOD__ . __LINE__ . ' subscribe to new folder ' . $newFolderName . '->' . $srv -> message );
return false ;
}
return $newFolderName ;
}
2013-04-12 11:22:23 +02:00
/**
* rename a folder
*
* @ param string _oldFolderName the old foldername
* @ param string _parent the parent foldername
* @ param string _folderName the new foldername
*
* @ return mixed name of the newly created folder or false on error
*/
function renameFolder ( $_oldFolderName , $_parent , $_folderName )
{
2013-12-12 16:10:25 +01:00
$oldFolderName = $_oldFolderName ; //$this->_encodeFolderName($_oldFolderName);
$parent = $_parent ; //$this->_encodeFolderName($_parent);
$folderName = $_folderName ; //$this->_encodeFolderName($_folderName);
2013-04-12 11:22:23 +02:00
if ( empty ( $parent )) {
$newFolderName = $folderName ;
} else {
$HierarchyDelimiter = $this -> getHierarchyDelimiter ();
$newFolderName = $parent . $HierarchyDelimiter . $folderName ;
}
2013-04-29 16:56:33 +02:00
if ( self :: $debug ) error_log ( " create folder: $newFolderName " );
2013-12-12 16:10:25 +01:00
try
{
$rv = $this -> icServer -> renameMailbox ( $oldFolderName , $newFolderName );
}
catch ( Exception $e )
{
throw new egw_exception ( __METHOD__ . " failed for $oldFolderName (rename to: $newFolderName ) with error: " . $e -> getMessage ());;
2013-04-12 11:22:23 +02:00
}
return $newFolderName ;
}
2013-05-13 16:42:42 +02:00
/**
* delete an existing folder
*
* @ param string _folderName the name of the folder to be deleted
*
* @ return bool true on success , PEAR Error on failure
*/
function deleteFolder ( $_folderName )
{
2013-12-12 14:34:35 +01:00
//$folderName = $this->_encodeFolderName($_folderName);
try
{
$this -> icServer -> subscribeMailbox ( $_folderName , false );
$this -> icServer -> deleteMailbox ( $_folderName );
}
catch ( Exception $e )
{
throw new egw_exception ( " Deleting Folder $_foldername failed! Error: " . $e -> getMessage ());;
2013-05-13 16:42:42 +02:00
}
return true ;
}
2013-04-12 11:22:23 +02:00
function subscribe ( $_folderName , $_status )
{
if ( self :: $debug ) error_log ( __METHOD__ . " :: " . ( $_status ? " " : " un " ) . " subscribe: " . $_folderName );
if ( $_status === true ) {
2013-12-12 14:34:35 +01:00
try
{
$rv = $this -> icServer -> subscribeMailbox ( $_folderName );
}
catch ( Exception $e )
{
error_log ( __METHOD__ . " :: " . ( $_status ? " " : " un " ) . " subscribe: " . $_folderName . " failed: " . $e -> getMessage );
2013-04-12 11:22:23 +02:00
return false ;
}
} else {
2013-12-12 14:34:35 +01:00
try
{
$rv = $this -> icServer -> subscribeMailbox ( $_folderName , false );
}
catch ( Exception $e )
{
error_log ( __METHOD__ . " :: " . ( $_status ? " " : " un " ) . " subscribe: " . $_folderName . " failed: " . $e -> getMessage );
2013-04-12 11:22:23 +02:00
return false ;
}
}
return true ;
}
2014-01-09 09:36:22 +01:00
/**
* fetchUnSubscribedFolders : get unsubscribed IMAP folder list
*
* returns an array of unsubscribed IMAP folder names .
*
* @ return array with folder names . eg .: 1 => INBOX / TEST
*/
function fetchUnSubscribedFolders ()
{
$unSubscribedMailboxes = $this -> icServer -> listUnSubscribedMailboxes ();
//error_log(__MEtHOD__.__LINE__.array2string($unSubscribedMailboxes));
return $unSubscribedMailboxes ;
}
2013-02-11 18:05:29 +01:00
/**
* get IMAP folder objects
*
* returns an array of IMAP folder objects . Put INBOX folder in first
* position . Preserves the folder seperator for later use . The returned
* array is indexed using the foldername . Use cachedObjects when retrieving subscribedFolders
*
* @ param boolean _subscribedOnly get subscribed or all folders
* @ param boolean _getCounters get get messages counters
* @ param boolean _alwaysGetDefaultFolders this triggers to ignore the possible notavailableautofolders - preference
* as activeSync needs all folders like sent , trash , drafts , templates and outbox - if not present devices may crash
2013-07-20 15:10:17 +02:00
* @ param boolean _useCacheIfPossible - if set to false cache will be ignored and reinitialized
2013-02-11 18:05:29 +01:00
*
* @ return array with folder objects . eg .: INBOX => { inbox object }
*/
2013-07-20 15:10:17 +02:00
function getFolderObjects ( $_subscribedOnly = false , $_getCounters = false , $_alwaysGetDefaultFolders = false , $_useCacheIfPossible = true )
2013-02-11 18:05:29 +01:00
{
2013-11-19 16:14:36 +01:00
if ( self :: $debug ) error_log ( __METHOD__ . __LINE__ . ' ServerID:' . $this -> icServer -> ImapServerId . " , subscribedOnly: $_subscribedOnly , getCounters: $_getCounters , alwaysGetDefaultFolders: $_alwaysGetDefaultFolders , _useCacheIfPossible: $_useCacheIfPossible " );
2013-02-11 18:05:29 +01:00
static $folders2return ;
2014-02-14 14:38:08 +01:00
// always use static on single request if info is available;
// so if you require subscribed/unsubscribed results on a single request you MUST
// set $_useCacheIfPossible to false !
if ( $_useCacheIfPossible && isset ( $folders2return [ $this -> icServer -> ImapServerId ]) && ! empty ( $folders2return [ $this -> icServer -> ImapServerId ]))
{
return $folders2return [ $this -> icServer -> ImapServerId ];
}
2013-02-11 18:05:29 +01:00
if ( $_subscribedOnly && $_getCounters === false )
{
if ( is_null ( $folders2return )) $folders2return = egw_cache :: getCache ( egw_cache :: INSTANCE , 'email' , 'folderObjects' . trim ( $GLOBALS [ 'egw_info' ][ 'user' ][ 'account_id' ]), $callback = null , $callback_params = array (), $expiration = 60 * 60 * 1 );
2013-07-20 15:10:17 +02:00
if ( $_useCacheIfPossible && isset ( $folders2return [ $this -> icServer -> ImapServerId ]) && ! empty ( $folders2return [ $this -> icServer -> ImapServerId ]))
2013-02-11 18:05:29 +01:00
{
2013-12-12 14:34:35 +01:00
//error_log(__METHOD__.__LINE__.' using Cached folderObjects'.array2string($folders2return[$this->icServer->ImapServerId]));
2013-10-30 14:27:26 +01:00
return $folders2return [ $this -> icServer -> ImapServerId ];
2013-02-11 18:05:29 +01:00
}
}
$isUWIMAP = false ;
$delimiter = $this -> getHierarchyDelimiter ();
$inboxData = new stdClass ;
$inboxData -> name = 'INBOX' ;
$inboxData -> folderName = 'INBOX' ;
$inboxData -> displayName = lang ( 'INBOX' );
$inboxData -> delimiter = $delimiter ;
$inboxData -> shortFolderName = 'INBOX' ;
$inboxData -> shortDisplayName = lang ( 'INBOX' );
$inboxData -> subscribed = true ;
if ( $_getCounters == true ) {
$inboxData -> counter = self :: getMailBoxCounters ( 'INBOX' );
}
// force unsubscribed by preference showAllFoldersInFolderPane
if ( $_subscribedOnly == true &&
2013-10-31 15:45:11 +01:00
isset ( $this -> mailPreferences [ 'showAllFoldersInFolderPane' ]) &&
$this -> mailPreferences [ 'showAllFoldersInFolderPane' ] == 1 )
2013-02-11 18:05:29 +01:00
{
$_subscribedOnly = false ;
}
#$inboxData->attributes = 64;
$inboxFolderObject = array ( 'INBOX' => $inboxData );
#_debug_array($folders);
//$nameSpace = $this->icServer->getNameSpaces();
$nameSpace = $this -> _getNameSpaces ();
//_debug_array($nameSpace);
//_debug_array($delimiter);
if ( isset ( $nameSpace [ '#mh/' ])) {
// removed the uwimap code
// but we need to reintroduce him later
// uw imap does not return the attribute of a folder, when requesting subscribed folders only
// dovecot has the same problem too
} else {
if ( is_array ( $nameSpace )) {
foreach ( $nameSpace as $type => $singleNameSpace ) {
$prefix_present = $nameSpace [ $type ][ 'prefix_present' ];
$foldersNameSpace [ $type ] = $nameSpace [ $type ];
if ( is_array ( $singleNameSpace )) {
// fetch and sort the subscribed folders
2014-01-09 09:36:22 +01:00
$subscribedMailboxes = $this -> icServer -> listSubscribedMailboxes ( $foldersNameSpace [ $type ][ 'prefix' ]);
2013-02-11 18:05:29 +01:00
if ( empty ( $subscribedMailboxes ) && $type == 'shared' )
{
2014-01-09 09:36:22 +01:00
$subscribedMailboxes = $this -> icServer -> listSubscribedMailboxes ( '' , 0 );
2013-02-11 18:05:29 +01:00
}
//echo "subscribedMailboxes";_debug_array($subscribedMailboxes);
if ( PEAR :: isError ( $subscribedMailboxes ) ) {
continue ;
}
$foldersNameSpace [ $type ][ 'subscribed' ] = $subscribedMailboxes ;
2013-10-25 15:37:53 +02:00
//if (is_array($foldersNameSpace[$type]['subscribed'])) sort($foldersNameSpace[$type]['subscribed']);
2013-02-11 18:05:29 +01:00
//_debug_array($foldersNameSpace);
if ( $_subscribedOnly == true ) {
$foldersNameSpace [ $type ][ 'all' ] = ( is_array ( $foldersNameSpace [ $type ][ 'subscribed' ]) ? $foldersNameSpace [ $type ][ 'subscribed' ] : array ());
continue ;
}
// only check for Folder in FolderMaintenance for Performance Reasons
if ( ! $_subscribedOnly ) {
foreach (( array ) $foldersNameSpace [ $type ][ 'subscribed' ] as $folderName )
{
if ( $foldersNameSpace [ $type ][ 'prefix' ] == $folderName || $foldersNameSpace [ $type ][ 'prefix' ] == $folderName . $foldersNameSpace [ $type ][ 'delimiter' ]) continue ;
//echo __METHOD__."Checking $folderName for existence<br>";
2014-02-05 10:14:05 +01:00
if ( ! $this -> folderExists ( $folderName , true )) {
2013-11-19 14:31:58 +01:00
//echo("eMail Folder $folderName failed to exist; should be unsubscribed; Trying ...");
2014-02-05 10:14:05 +01:00
if ( $this -> subscribe ( $folderName , false ))
2013-02-11 18:05:29 +01:00
{
2013-11-19 14:31:58 +01:00
$r = " success. " ;
2013-02-11 18:05:29 +01:00
} else {
2013-11-19 14:31:58 +01:00
$r = " failed. " ;
2013-02-11 18:05:29 +01:00
}
2014-02-05 10:14:05 +01:00
error_log ( __METHOD__ . " -> $folderName in NS: $type failed to be here; should be unsubscribed.... " . $r );
2013-02-11 18:05:29 +01:00
}
}
}
// fetch and sort all folders
//echo $type.'->'.$foldersNameSpace[$type]['prefix'].'->'.($type=='shared'?0:2)."<br>";
2014-02-06 10:09:57 +01:00
try
2013-02-11 18:05:29 +01:00
{
2014-02-06 10:09:57 +01:00
$allMailboxesExt = $this -> icServer -> getMailboxes ( $foldersNameSpace [ $type ][ 'prefix' ], 2 , true );
}
catch ( Exception $e )
{
error_log ( __METHOD__ . __LINE__ . ' Failed to retrieve all Boxes:' . $e -> getMessage ());
2013-02-11 18:05:29 +01:00
$allMailboxesExt = array ();
}
if ( empty ( $allMailboxesExt ) && $type == 'shared' )
{
2014-02-06 10:09:57 +01:00
try
{
$allMailboxesExt = $this -> icServer -> getMailboxes ( '' , 0 , true );
}
catch ( Exception $e )
{
$allMailboxesExt = array ();
}
2013-02-11 18:05:29 +01:00
}
else
{
if ( $prefix_present == 'forced' && $type == 'personal' ) // you cannot trust dovecots assumed prefix
{
2014-02-06 10:09:57 +01:00
try
{
$allMailboxesExtAll = $this -> icServer -> getMailboxes ( '' , 0 , true );
}
catch ( Exception $e )
{
$allMailboxesExtAll = array ();
}
2013-02-11 18:05:29 +01:00
foreach ( $allMailboxesExtAll as $kaMEA => $aMEA )
{
if ( ! in_array ( $aMEA , $allMailboxesExt )) $allMailboxesExt [] = $aMEA ;
}
}
}
$allMailBoxesExtSorted = array ();
2013-12-05 16:09:35 +01:00
if ( ! is_array ( $allMailboxesExt ))
{
2013-12-12 14:34:35 +01:00
//error_log(__METHOD__.__LINE__.' Expected Array but got:'.array2string($allMailboxesExt). 'Type:'.$type.' Prefix:'.$foldersNameSpace[$type]['prefix']);
continue ;
//$allMailboxesExt=array();
2013-12-05 16:09:35 +01:00
}
2013-02-11 18:05:29 +01:00
foreach ( $allMailboxesExt as $mbx ) {
//echo __METHOD__;_debug_array($mbx);
//error_log(__METHOD__.__LINE__.array2string($mbx));
if ( isset ( $allMailBoxesExtSorted [ $mbx [ 'MAILBOX' ]]) ||
isset ( $allMailBoxesExtSorted [ $mbx [ 'MAILBOX' ] . $foldersNameSpace [ $type ][ 'delimiter' ]]) ||
( substr ( $mbx [ 'MAILBOX' ], - 1 ) == $foldersNameSpace [ $type ][ 'delimiter' ] && isset ( $allMailBoxesExtSorted [ substr ( $mbx [ 'MAILBOX' ], 0 , - 1 )]))
) continue ;
//echo '#'.$mbx['MAILBOX'].':'.array2string($mbx)."#<br>";
$allMailBoxesExtSorted [ $mbx [ 'MAILBOX' ]] = $mbx ;
}
if ( is_array ( $allMailBoxesExtSorted )) ksort ( $allMailBoxesExtSorted );
//_debug_array($allMailBoxesExtSorted);
$allMailboxes = array ();
foreach (( array ) $allMailBoxesExtSorted as $mbx ) {
//echo $mbx['MAILBOX']."<br>";
2013-12-03 15:38:50 +01:00
if ( in_array ( '\HasChildren' , $mbx [ " ATTRIBUTES " ]) || in_array ( '\Haschildren' , $mbx [ " ATTRIBUTES " ]) || in_array ( '\haschildren' , $mbx [ " ATTRIBUTES " ])) {
2013-02-11 18:05:29 +01:00
unset ( $buff );
//$buff = $this->icServer->getMailboxes($mbx['MAILBOX'].$delimiter,0,false);
if ( ! in_array ( $mbx [ 'MAILBOX' ], $allMailboxes )) $buff = self :: getMailBoxesRecursive ( $mbx [ 'MAILBOX' ], $delimiter , $foldersNameSpace [ $type ][ 'prefix' ], 1 );
if ( PEAR :: isError ( $buff ) ) {
continue ;
}
#_debug_array($buff);
if ( is_array ( $buff )) $allMailboxes = array_merge ( $allMailboxes , $buff );
}
if ( ! in_array ( $mbx [ 'MAILBOX' ], $allMailboxes )) $allMailboxes [] = $mbx [ 'MAILBOX' ];
//echo "Result:";_debug_array($allMailboxes);
}
$foldersNameSpace [ $type ][ 'all' ] = $allMailboxes ;
if ( is_array ( $foldersNameSpace [ $type ][ 'all' ])) sort ( $foldersNameSpace [ $type ][ 'all' ]);
}
}
}
// check for autocreated folders
if ( isset ( $foldersNameSpace [ 'personal' ][ 'prefix' ])) {
$personalPrefix = $foldersNameSpace [ 'personal' ][ 'prefix' ];
$personalDelimiter = $foldersNameSpace [ 'personal' ][ 'delimiter' ];
if ( ! empty ( $personalPrefix )) {
if ( substr ( $personalPrefix , - 1 ) != $personalDelimiter ) {
$folderPrefix = $personalPrefix . $personalDelimiter ;
} else {
$folderPrefix = $personalPrefix ;
}
}
else
{
if ( substr ( $personalPrefix , - 1 ) != $personalDelimiter ) {
$folderPrefixAsInbox = 'INBOX' . $personalDelimiter ;
} else {
$folderPrefixAsInbox = 'INBOX' ;
}
}
2013-10-31 15:45:11 +01:00
if ( ! $_alwaysGetDefaultFolders && $this -> mailPreferences [ 'notavailableautofolders' ] && ! empty ( $this -> mailPreferences [ 'notavailableautofolders' ]))
2013-02-11 18:05:29 +01:00
{
2013-10-31 15:45:11 +01:00
$foldersToCheck = array_diff ( self :: $autoFolders , explode ( ',' , $this -> mailPreferences [ 'notavailableautofolders' ]));
2013-02-11 18:05:29 +01:00
} else {
$foldersToCheck = self :: $autoFolders ;
}
//error_log(__METHOD__.__LINE__." foldersToCheck:".array2string($foldersToCheck));
2013-10-31 15:45:11 +01:00
//error_log(__METHOD__.__LINE__." foldersToCheck:".array2string( $this->mailPreferences['sentFolder']));
2013-02-11 18:05:29 +01:00
foreach ( $foldersToCheck as $personalFolderName ) {
$folderName = ( ! empty ( $personalPrefix ) ? $folderPrefix . $personalFolderName : $personalFolderName );
//error_log(__METHOD__.__LINE__." foldersToCheck: $personalFolderName / $folderName");
if ( ! is_array ( $foldersNameSpace [ 'personal' ][ 'all' ]) || ! in_array ( $folderName , $foldersNameSpace [ 'personal' ][ 'all' ])) {
$createfolder = true ;
switch ( $personalFolderName )
{
case 'Drafts' : // => Entwürfe
$draftFolder = $this -> getDraftFolder ();
if ( $draftFolder && $draftFolder == 'none' )
$createfolder = false ;
break ;
case 'Junk' : //] => Spammails
2014-01-10 11:33:53 +01:00
$junkFolder = $this -> getJunkFolder ();
if ( $junkFolder && $junkFolder == 'none' )
2013-02-11 18:05:29 +01:00
$createfolder = false ;
break ;
case 'Sent' : //] => Gesendet
// ToDo: we may need more sophistcated checking here
$sentFolder = $this -> getSentFolder ();
if ( $sentFolder && $sentFolder == 'none' )
$createfolder = false ;
break ;
case 'Trash' : //] => Papierkorb
$trashFolder = $this -> getTrashFolder ();
if ( $trashFolder && $trashFolder == 'none' )
$createfolder = false ;
break ;
case 'Templates' : //] => Vorlagen
$templateFolder = $this -> getTemplateFolder ();
if ( $templateFolder && $templateFolder == 'none' )
$createfolder = false ;
break ;
case 'Outbox' : // Nokia Outbox for activesync
2013-10-31 15:45:11 +01:00
//if ($this->mailPreferences['outboxFolder'] && $this->mailPreferences['outboxFolder']=='none')
2013-02-11 18:05:29 +01:00
$createfolder = false ;
if ( $GLOBALS [ 'egw_info' ][ 'user' ][ 'apps' ][ 'activesync' ]) $createfolder = true ;
break ;
}
// check for the foldername as constructed with prefix (or not)
2014-02-05 10:14:05 +01:00
if ( $createfolder && $this -> folderExists ( $folderName ))
2013-02-11 18:05:29 +01:00
{
$createfolder = false ;
}
// check for the folder as it comes (no prefix)
2014-02-05 10:14:05 +01:00
if ( $createfolder && $personalFolderName != $folderName && $this -> folderExists ( $personalFolderName ))
2013-02-11 18:05:29 +01:00
{
$createfolder = false ;
$folderName = $personalFolderName ;
}
// check for the folder as it comes with INBOX prefixed
$folderWithInboxPrefixed = $folderPrefixAsInbox . $personalFolderName ;
2014-02-05 10:14:05 +01:00
if ( $createfolder && $folderWithInboxPrefixed != $folderName && $this -> folderExists ( $folderWithInboxPrefixed ))
2013-02-11 18:05:29 +01:00
{
$createfolder = false ;
$folderName = $folderWithInboxPrefixed ;
}
// now proceed with the folderName that may be altered in the progress of testing for existence
if ( $createfolder === false && $_alwaysGetDefaultFolders )
{
if ( ! in_array ( $folderName , $foldersNameSpace [ 'personal' ][ 'all' ])) $foldersNameSpace [ 'personal' ][ 'all' ][] = $folderName ;
if ( ! in_array ( $folderName , $foldersNameSpace [ 'personal' ][ 'subscribed' ])) $foldersNameSpace [ 'personal' ][ 'subscribed' ][] = $folderName ;
}
if ( $createfolder === true && $this -> createFolder ( '' , $folderName , true )) {
$foldersNameSpace [ 'personal' ][ 'all' ][] = $folderName ;
$foldersNameSpace [ 'personal' ][ 'subscribed' ][] = $folderName ;
} else {
#print "FOLDERNAME failed: $folderName<br>";
}
}
}
}
}
//echo "<br>FolderNameSpace To Process:";_debug_array($foldersNameSpace);
$autoFolderObjects = array ();
foreach ( array ( 'personal' , 'others' , 'shared' ) as $type ) {
if ( isset ( $foldersNameSpace [ $type ])) {
if ( $_subscribedOnly ) {
if ( ! PEAR :: isError ( $foldersNameSpace [ $type ][ 'subscribed' ]) ) $listOfFolders = $foldersNameSpace [ $type ][ 'subscribed' ];
} else {
if ( ! PEAR :: isError ( $foldersNameSpace [ $type ][ 'all' ])) $listOfFolders = $foldersNameSpace [ $type ][ 'all' ];
}
foreach (( array ) $listOfFolders as $folderName ) {
//echo "<br>FolderToCheck:$folderName<br>";
if ( $_subscribedOnly && ! ( in_array ( $folderName , $foldersNameSpace [ $type ][ 'all' ]) || in_array ( $folderName . $foldersNameSpace [ $type ][ 'delimiter' ], $foldersNameSpace [ $type ][ 'all' ]))) {
#echo "$folderName failed to be here <br>";
continue ;
}
2014-02-06 10:09:57 +01:00
if ( isset ( $folders [ $folderName ])) continue ;
if ( isset ( $autoFolderObjects [ $folderName ])) continue ;
2013-02-11 18:05:29 +01:00
$folderParts = explode ( $delimiter , $folderName );
$shortName = array_pop ( $folderParts );
$folderObject = new stdClass ;
$folderObject -> delimiter = $delimiter ;
$folderObject -> folderName = $folderName ;
$folderObject -> shortFolderName = $shortName ;
if ( ! $_subscribedOnly ) {
#echo $folderName."->".$type."<br>";
#_debug_array($foldersNameSpace[$type]['subscribed']);
$folderObject -> subscribed = in_array ( $folderName , $foldersNameSpace [ $type ][ 'subscribed' ]);
}
if ( $_getCounters == true ) {
$folderObject -> counter = $this -> getMailBoxCounters ( $folderName );
}
if ( strtoupper ( $folderName ) == 'INBOX' ) {
$folderName = 'INBOX' ;
$folderObject -> folderName = 'INBOX' ;
$folderObject -> shortFolderName = 'INBOX' ;
$folderObject -> displayName = lang ( 'INBOX' );
$folderObject -> shortDisplayName = lang ( 'INBOX' );
$folderObject -> subscribed = true ;
// translate the automatic Folders (Sent, Drafts, ...) like the INBOX
} elseif ( in_array ( $shortName , self :: $autoFolders )) {
$tmpfolderparts = explode ( $delimiter , $folderObject -> folderName );
array_pop ( $tmpfolderparts );
$folderObject -> displayName = implode ( $delimiter , $tmpfolderparts ) . $delimiter . lang ( $shortName );
$folderObject -> shortDisplayName = lang ( $shortName );
unset ( $tmpfolderparts );
} else {
2013-10-30 14:27:26 +01:00
$folderObject -> displayName = $folderObject -> folderName ;
$folderObject -> shortDisplayName = $shortName ;
2013-02-11 18:05:29 +01:00
}
//$folderName = $folderName;
if ( in_array ( $shortName , self :: $autoFolders ) && self :: searchValueInFolderObjects ( $shortName , $autoFolderObjects ) === false ) {
$autoFolderObjects [ $folderName ] = $folderObject ;
} else {
$folders [ $folderName ] = $folderObject ;
}
2014-02-06 10:09:57 +01:00
//error_log(__METHOD__.__LINE__.':'.$folderObject->folderName);
2013-02-11 18:05:29 +01:00
}
}
}
if ( is_array ( $autoFolderObjects ) && ! empty ( $autoFolderObjects )) {
uasort ( $autoFolderObjects , array ( $this , " sortByAutoFolderPos " ));
}
if ( is_array ( $folders )) uasort ( $folders , array ( $this , " sortByDisplayName " ));
//$folders2return = array_merge($autoFolderObjects,$folders);
//_debug_array($folders2return); #exit;
2013-11-19 16:14:36 +01:00
$folders2return [ $this -> icServer -> ImapServerId ] = array_merge (( array ) $inboxFolderObject ,( array ) $autoFolderObjects ,( array ) $folders );
if (( $_subscribedOnly && $_getCounters === false ) ||
( $_subscribedOnly == false && $_getCounters === false &&
isset ( $this -> mailPreferences [ 'showAllFoldersInFolderPane' ]) &&
$this -> mailPreferences [ 'showAllFoldersInFolderPane' ] == 1 ))
{
egw_cache :: setCache ( egw_cache :: INSTANCE , 'email' , 'folderObjects' . trim ( $GLOBALS [ 'egw_info' ][ 'user' ][ 'account_id' ]), $folders2return , $expiration = 60 * 60 * 1 );
}
2013-02-11 18:05:29 +01:00
return $folders2return [ $this -> icServer -> ImapServerId ];
}
/**
* search Value In FolderObjects
*
* Helper function to search for a specific value within the foldertree objects
* @ param string $needle
* @ param array $haystack , array of folderobjects
* @ return MIXED false or key
*/
static function searchValueInFolderObjects ( $needle , $haystack )
{
$rv = false ;
foreach ( $haystack as $k => $v )
{
foreach ( $v as $sk => $sv ) if ( trim ( $sv ) == trim ( $needle )) return $k ;
}
return $rv ;
}
/**
* sortByDisplayName
*
* Helper function to sort folder - objects by displayname
* @ param object $a
* @ param object $b , array of folderobjects
* @ return int expect values ( 0 , 1 or - 1 )
*/
function sortByDisplayName ( $a , $b )
{
// 0, 1 und -1
return strcasecmp ( $a -> displayName , $b -> displayName );
}
/**
* sortByAutoFolderPos
*
* Helper function to sort folder - objects by auto Folder Position
* @ param object $a
* @ param object $b , array of folderobjects
* @ return int expect values ( 0 , 1 or - 1 )
*/
function sortByAutoFolderPos ( $a , $b )
{
// 0, 1 und -1
$pos1 = array_search ( trim ( $a -> shortFolderName ), self :: $autoFolders );
$pos2 = array_search ( trim ( $b -> shortFolderName ), self :: $autoFolders );
if ( $pos1 == $pos2 ) return 0 ;
return ( $pos1 < $pos2 ) ? - 1 : 1 ;
}
/**
* getMailBoxCounters
*
* function to retrieve the counters for a given folder
* @ param string $folderName
* @ return mixed false or array of counters array ( MESSAGES , UNSEEN , RECENT , UIDNEXT , UIDVALIDITY )
*/
function getMailBoxCounters ( $folderName )
{
$folderStatus = $this -> _getStatus ( $folderName );
2013-11-04 12:22:38 +01:00
//error_log(__METHOD__.__LINE__." FolderStatus:".array2string($folderStatus));
2013-02-11 18:05:29 +01:00
if ( PEAR :: isError ( $folderStatus )) {
if ( self :: $debug ) error_log ( __METHOD__ . " returned FolderStatus for Folder $folderName : " . print_r ( $folderStatus -> message , true ));
return false ;
}
if ( is_array ( $folderStatus )) {
$status = new stdClass ;
$status -> messages = $folderStatus [ 'MESSAGES' ];
$status -> unseen = $folderStatus [ 'UNSEEN' ];
$status -> recent = $folderStatus [ 'RECENT' ];
$status -> uidnext = $folderStatus [ 'UIDNEXT' ];
$status -> uidvalidity = $folderStatus [ 'UIDVALIDITY' ];
return $status ;
}
return false ;
}
/**
* getMailBoxesRecursive
*
* function to retrieve mailboxes recursively from given mailbox
* @ param string $_mailbox
* @ param string $delimiter
* @ param string $prefix
* @ param string $reclevel 0 , counter to keep track of the current recursionlevel
* @ return array of mailboxes
*/
function getMailBoxesRecursive ( $_mailbox , $delimiter , $prefix , $reclevel = 0 )
{
#echo __METHOD__." retrieve SubFolders for $_mailbox$delimiter <br>";
$maxreclevel = 25 ;
if ( $reclevel > $maxreclevel ) {
error_log ( __METHOD__ . " Recursion Level Exeeded ( $reclevel ) while looking up $_mailbox $delimiter " );
return array ();
}
$reclevel ++ ;
// clean up double delimiters
$_mailbox = preg_replace ( '~' . ( $delimiter == '.' ? " \\ " . $delimiter : $delimiter ) . '+~s' , $delimiter , $_mailbox );
//get that mailbox in question
$mbx = $this -> icServer -> getMailboxes ( $_mailbox , 1 , true );
#_debug_array($mbx);
2013-12-03 15:38:50 +01:00
//error_log(__METHOD__.__LINE__.' Delimiter:'.array2string($delimiter));
//error_log(__METHOD__.__LINE__.array2string($mbx));
if ( is_array ( $mbx [ 0 ][ " ATTRIBUTES " ]) && ( in_array ( '\HasChildren' , $mbx [ 0 ][ " ATTRIBUTES " ]) || in_array ( '\Haschildren' , $mbx [ 0 ][ " ATTRIBUTES " ]) || in_array ( '\haschildren' , $mbx [ 0 ][ " ATTRIBUTES " ]))) {
2013-02-11 18:05:29 +01:00
// if there are children fetch them
//echo $mbx[0]['MAILBOX']."<br>";
unset ( $buff );
$buff = $this -> icServer -> getMailboxes ( $mbx [ 0 ][ 'MAILBOX' ] . ( $mbx [ 0 ][ 'MAILBOX' ] == $prefix ? '' : $delimiter ), 2 , false );
//$buff = $this->icServer->getMailboxes($mbx[0]['MAILBOX'],2,false);
//_debug_array($buff);
2013-12-03 15:38:50 +01:00
$allMailboxes = array ();
foreach ( $buff as $mbxname ) {
//error_log(__METHOD__.__LINE__.array2string($mbxname));
$mbxname = preg_replace ( '~' . ( $delimiter == '.' ? " \\ " . $delimiter : $delimiter ) . '+~s' , $delimiter , $mbxname [ 'MAILBOX' ]);
#echo "About to recur in level $reclevel:".$mbxname."<br>";
if ( $mbxname != $mbx [ 0 ][ 'MAILBOX' ] && $mbxname != $prefix && $mbxname != $mbx [ 0 ][ 'MAILBOX' ] . $delimiter )
{
$allMailboxes = array_merge ( $allMailboxes , self :: getMailBoxesRecursive ( $mbxname , $delimiter , $prefix , $reclevel ));
2013-02-11 18:05:29 +01:00
}
}
2013-12-03 15:38:50 +01:00
if ( ! ( in_array ( '\NoSelect' , $mbx [ 0 ][ " ATTRIBUTES " ]) || in_array ( '\Noselect' , $mbx [ 0 ][ " ATTRIBUTES " ]) || in_array ( '\noselect' , $mbx [ 0 ][ " ATTRIBUTES " ]))) $allMailboxes [] = $mbx [ 0 ][ 'MAILBOX' ];
return $allMailboxes ;
2013-02-11 18:05:29 +01:00
} else {
return array ( $_mailbox );
}
}
/**
* _getSpecialUseFolder
* abstraction layer for getDraftFolder , getTemplateFolder , getTrashFolder and getSentFolder
* @ param string $type the type to fetch ( Drafts | Template | Trash | Sent )
* @ param boolean $_checkexistance , trigger check for existance
* @ return mixed string or false
*/
function _getSpecialUseFolder ( $_type , $_checkexistance = TRUE )
{
static $types = array (
2013-11-22 14:55:09 +01:00
'Drafts' => array ( 'prefName' => 'draftFolder' , 'profileKey' => 'acc_folder_draft' , 'autoFolderName' => 'Drafts' ),
2013-10-31 15:45:11 +01:00
'Template' => array ( 'prefName' => 'templateFolder' , 'profileKey' => 'acc_folder_template' , 'autoFolderName' => 'Templates' ),
'Trash' => array ( 'prefName' => 'trashFolder' , 'profileKey' => 'acc_folder_trash' , 'autoFolderName' => 'Trash' ),
'Sent' => array ( 'prefName' => 'sentFolder' , 'profileKey' => 'acc_folder_sent' , 'autoFolderName' => 'Sent' ),
2014-01-10 11:33:53 +01:00
'Junk' => array ( 'prefName' => 'junkFolder' , 'profileKey' => 'acc_folder_junk' , 'autoFolderName' => 'Junk' ),
2013-02-11 18:05:29 +01:00
);
if ( ! isset ( $types [ $_type ]))
{
error_log ( __METHOD__ . __LINE__ . ' ' . $_type . ' not supported for ' . __METHOD__ );
return false ;
}
if ( is_null ( self :: $specialUseFolders ) || empty ( self :: $specialUseFolders )) self :: $specialUseFolders = $this -> getSpecialUseFolders ();
//highest precedence
2014-01-13 10:36:51 +01:00
try
{
$_folderName = $this -> icServer -> $types [ $_type ][ 'profileKey' ];
}
catch ( Exception $e )
{
$_folderName = false ;
}
2014-01-10 11:33:53 +01:00
// No folder Prefs any more
2013-02-11 18:05:29 +01:00
//check prefs next
2014-01-10 11:33:53 +01:00
//if (empty($_folderName)) $_folderName = $this->mailPreferences[$types[$_type]['prefName']];
2013-02-11 18:05:29 +01:00
// does the folder exist???
2014-02-05 10:14:05 +01:00
if ( $_checkexistance && $_folderName != 'none' && ! $this -> folderExists ( $_folderName )) {
2013-02-11 18:05:29 +01:00
$_folderName = false ;
}
//no (valid) folder found yet; try specialUseFolders
if ( empty ( $_folderName ) && is_array ( self :: $specialUseFolders ) && ( $f = array_search ( $_type , self :: $specialUseFolders ))) $_folderName = $f ;
//no specialUseFolder; try some Defaults
if ( empty ( $_folderName ) && isset ( $types [ $_type ]))
{
$nameSpace = $this -> _getNameSpaces ();
$prefix = '' ;
if ( isset ( $nameSpace [ 'personal' ])) $prefix = $nameSpace [ 'personal' ][ 'prefix' ];
2014-02-05 10:14:05 +01:00
if ( $this -> folderExists ( $prefix . $types [ $_type ][ 'autoFolderName' ])) $_folderName = $prefix . $types [ $_type ][ 'autoFolderName' ];
2013-02-11 18:05:29 +01:00
}
return $_folderName ;
}
2014-01-10 11:33:53 +01:00
/**
* getDraftFolder wrapper for _getSpecialUseFolder Type Drafts
* @ param boolean $_checkexistance , trigger check for existance
* @ return mixed string or false
*/
function getJunkFolder ( $_checkexistance = TRUE )
{
return $this -> _getSpecialUseFolder ( 'Junk' , $_checkexistance );
}
2013-02-11 18:05:29 +01:00
/**
* getDraftFolder wrapper for _getSpecialUseFolder Type Drafts
* @ param boolean $_checkexistance , trigger check for existance
* @ return mixed string or false
*/
function getDraftFolder ( $_checkexistance = TRUE )
{
return $this -> _getSpecialUseFolder ( 'Drafts' , $_checkexistance );
}
/**
* getTemplateFolder wrapper for _getSpecialUseFolder Type Template
* @ param boolean $_checkexistance , trigger check for existance
* @ return mixed string or false
*/
function getTemplateFolder ( $_checkexistance = TRUE )
{
return $this -> _getSpecialUseFolder ( 'Template' , $_checkexistance );
}
/**
* getTrashFolder wrapper for _getSpecialUseFolder Type Trash
* @ param boolean $_checkexistance , trigger check for existance
* @ return mixed string or false
*/
function getTrashFolder ( $_checkexistance = TRUE )
{
return $this -> _getSpecialUseFolder ( 'Trash' , $_checkexistance );
}
/**
* getSentFolder wrapper for _getSpecialUseFolder Type Sent
* @ param boolean $_checkexistance , trigger check for existance
* @ return mixed string or false
*/
function getSentFolder ( $_checkexistance = TRUE )
{
return $this -> _getSpecialUseFolder ( 'Sent' , $_checkexistance );
}
/**
* isSentFolder is the given folder the sent folder or at least a subfolder of it
* @ param string $_foldername , folder to perform the check on
* @ param boolean $_checkexistance , trigger check for existance
* @ return boolean
*/
function isSentFolder ( $_folderName , $_checkexistance = TRUE )
{
$sentFolder = $this -> getSentFolder ();
if ( empty ( $sentFolder )) {
return false ;
}
// does the folder exist???
2014-02-05 10:14:05 +01:00
if ( $_checkexistance && ! $this -> folderExists ( $_folderName )) {
2013-02-11 18:05:29 +01:00
return false ;
}
if ( false !== stripos ( $_folderName , $sentFolder )) {
return true ;
} else {
return false ;
}
}
/**
* checks if the Outbox folder exists and is part of the foldername to be checked
* @ param string $_foldername , folder to perform the check on
* @ param boolean $_checkexistance , trigger check for existance
* @ return boolean
*/
function isOutbox ( $_folderName , $_checkexistance = TRUE )
{
if ( stripos ( $_folderName , 'Outbox' ) === false ) {
return false ;
}
// does the folder exist???
2014-02-05 10:14:05 +01:00
if ( $_checkexistance && ! $this -> folderExists ( $_folderName )) {
2013-02-11 18:05:29 +01:00
return false ;
}
return true ;
}
/**
* isDraftFolder is the given folder the sent folder or at least a subfolder of it
* @ param string $_foldername , folder to perform the check on
* @ param boolean $_checkexistance , trigger check for existance
* @ return boolean
*/
function isDraftFolder ( $_folderName , $_checkexistance = TRUE )
{
$draftFolder = $this -> getDraftFolder ();
if ( empty ( $draftFolder )) {
return false ;
}
// does the folder exist???
2014-02-05 10:14:05 +01:00
if ( $_checkexistance && ! $this -> folderExists ( $_folderName )) {
2013-02-11 18:05:29 +01:00
return false ;
}
if ( false !== stripos ( $_folderName , $draftFolder )) {
return true ;
} else {
return false ;
}
}
/**
* isTrashFolder is the given folder the sent folder or at least a subfolder of it
* @ param string $_foldername , folder to perform the check on
* @ param boolean $_checkexistance , trigger check for existance
* @ return boolean
*/
function isTrashFolder ( $_folderName , $_checkexistance = TRUE )
{
$trashFolder = $this -> getTrashFolder ();
if ( empty ( $trashFolder )) {
return false ;
}
// does the folder exist???
2014-02-05 10:14:05 +01:00
if ( $_checkexistance && ! $this -> folderExists ( $_folderName )) {
2013-02-11 18:05:29 +01:00
return false ;
}
if ( false !== stripos ( $_folderName , $trashFolder )) {
return true ;
} else {
return false ;
}
}
/**
* isTemplateFolder is the given folder the sent folder or at least a subfolder of it
* @ param string $_foldername , folder to perform the check on
* @ param boolean $_checkexistance , trigger check for existance
* @ return boolean
*/
function isTemplateFolder ( $_folderName , $_checkexistance = TRUE )
{
$templateFolder = $this -> getTemplateFolder ();
if ( empty ( $templateFolder )) {
return false ;
}
// does the folder exist???
2014-02-05 10:14:05 +01:00
if ( $_checkexistance && ! $this -> folderExists ( $_folderName )) {
2013-02-11 18:05:29 +01:00
return false ;
}
if ( false !== stripos ( $_folderName , $templateFolder )) {
return true ;
} else {
return false ;
}
}
/**
* folderExists checks for existance of a given folder
* @ param string $_foldername , folder to perform the check on
* @ param boolean $_forceCheck , trigger check for existance on icServer
* @ return mixed string or false
*/
function folderExists ( $_folder , $_forceCheck = false )
{
static $folderInfo ;
$forceCheck = $_forceCheck ;
if ( empty ( $_folder ))
{
// this error is more or less without significance, unless we force the check
if ( $_forceCheck === true ) error_log ( __METHOD__ . __LINE__ . ' Called with empty Folder:' . $_folder . function_backtrace ());
return false ;
}
// reduce traffic within the Instance per User; Expire every 5 Minutes
//error_log(__METHOD__.__LINE__.' Called with Folder:'.$_folder.function_backtrace());
if ( is_null ( $folderInfo )) $folderInfo = egw_cache :: getCache ( egw_cache :: INSTANCE , 'email' , 'icServerFolderExistsInfo' . trim ( $GLOBALS [ 'egw_info' ][ 'user' ][ 'account_id' ]), null , array (), $expiration = 60 * 5 );
//error_log(__METHOD__.__LINE__.'Cached Info on Folder:'.$_folder.' for Profile:'.$this->profileID.($forceCheck?'(forcedCheck)':'').':'.array2string($folderInfo));
if ( ! empty ( $folderInfo ) && isset ( $folderInfo [ $this -> profileID ]) && isset ( $folderInfo [ $this -> profileID ][ $_folder ]) && $forceCheck === false )
{
//error_log(__METHOD__.__LINE__.' Using cached Info on Folder:'.$_folder.' for Profile:'.$this->profileID);
return $folderInfo [ $this -> profileID ][ $_folder ];
}
else
{
if ( $forceCheck === false )
{
//error_log(__METHOD__.__LINE__.' No cached Info on Folder:'.$_folder.' for Profile:'.$this->profileID.' FolderExistsInfoCache:'.array2string($folderInfo[$this->profileID]));
$forceCheck = true ; // try to force the check, in case there is no connection, we may need that
}
}
// does the folder exist???
//error_log(__METHOD__."->Connected?".$this->icServer->_connected.", ".$_folder.", ".($forceCheck?' forceCheck activated':'dont check on server'));
2013-10-29 17:16:56 +01:00
if ( $forceCheck || empty ( $folderInfo ) || ! isset ( $folderInfo [ $this -> profileID ]) || ! isset ( $folderInfo [ $this -> profileID ][ $_folder ])) {
2013-02-11 18:05:29 +01:00
//error_log(__METHOD__."->NotConnected and forceCheck with profile:".$this->profileID);
//return false;
//try to connect
}
if (( $this -> icServer instanceof defaultimap ))
{
$folderInfo [ $this -> profileID ][ $_folder ] = $this -> icServer -> mailboxExist ( $_folder ); //LIST Command, may return OK, but no attributes
2014-02-05 10:14:05 +01:00
//error_log(__METHOD__.__LINE__.' Profile:'.$this->profileID.'->'.$_folder.':'.array2string($folderInfo[$this->profileID][$_folder]));
2013-02-11 18:05:29 +01:00
if ( $folderInfo [ $this -> profileID ][ $_folder ] == false )
{
// some servers dont serve the LIST command in certain cases; this is a ServerBUG and
// we try to work around it here.
2013-10-31 15:45:11 +01:00
if (( isset ( $this -> mailPreferences [ 'trustServersUnseenInfo' ]) &&
$this -> mailPreferences [ 'trustServersUnseenInfo' ] == false ) ||
( isset ( $this -> mailPreferences [ 'trustServersUnseenInfo' ]) &&
$this -> mailPreferences [ 'trustServersUnseenInfo' ] == 2 )
2013-02-11 18:05:29 +01:00
)
{
$nameSpace = $this -> _getNameSpaces ();
if ( isset ( $nameSpace [ 'personal' ])) unset ( $nameSpace [ 'personal' ]);
$prefix = $this -> getFolderPrefixFromNamespace ( $nameSpace , $_folder );
if ( $prefix != '' && stripos ( $_folder , $prefix ) !== false )
{
if ( ! PEAR :: isError ( $r = $this -> _getStatus ( $_folder )) && is_array ( $r )) $folderInfo [ $this -> profileID ][ $_folder ] = true ;
}
}
}
}
//error_log(__METHOD__.__LINE__.' Folder Exists:'.$folderInfo[$this->profileID][$_folder].function_backtrace());
if ( ! empty ( $folderInfo ) && isset ( $folderInfo [ $this -> profileID ][ $_folder ]) &&
( $folderInfo [ $this -> profileID ][ $_folder ] instanceof PEAR_Error ) || $folderInfo [ $this -> profileID ][ $_folder ] !== true )
{
2013-09-23 10:33:30 +02:00
if ( $folderInfo [ $this -> profileID ][ $_folder ] instanceof PEAR_Error ) error_log ( __METHOD__ . __LINE__ . array2string ( $folderInfo [ $this -> profileID ][ $_folder ] -> message ));
2013-02-11 18:05:29 +01:00
$folderInfo [ $this -> profileID ][ $_folder ] = false ; // set to false, whatever it was (to have a valid returnvalue for the static return)
}
egw_cache :: setCache ( egw_cache :: INSTANCE , 'email' , 'icServerFolderExistsInfo' . trim ( $GLOBALS [ 'egw_info' ][ 'user' ][ 'account_id' ]), $folderInfo , $expiration = 60 * 5 );
return ( ! empty ( $folderInfo ) && isset ( $folderInfo [ $this -> profileID ][ $_folder ]) ? $folderInfo [ $this -> profileID ][ $_folder ] : false );
}
2013-02-12 18:48:04 +01:00
/**
* remove any messages which are marked as deleted or
* remove any messages from the trashfolder
*
* @ param string _folderName the foldername
* @ return nothing
*/
function compressFolder ( $_folderName = false )
{
$folderName = ( $_folderName ? $_folderName : $this -> sessionData [ 'mailbox' ]);
$deleteOptions = $GLOBALS [ 'egw_info' ][ 'user' ][ 'preferences' ][ 'mail' ][ 'deleteOptions' ];
$trashFolder = $this -> getTrashFolder ();
2013-10-29 17:16:56 +01:00
$this -> icServer -> openMailbox ( $folderName );
2013-02-12 18:48:04 +01:00
if ( $folderName == $trashFolder && $deleteOptions == " move_to_trash " ) {
2013-11-04 12:22:38 +01:00
$this -> deleteMessages ( 'all' , $folderName , 'remove_immediately' );
2013-02-12 18:48:04 +01:00
} else {
2013-11-04 12:22:38 +01:00
$this -> icServer -> expunge ( $folderName );
2013-02-12 18:48:04 +01:00
}
}
2013-02-28 10:28:08 +01:00
/**
* delete a Message
*
* @ param mixed array / string _messageUID array of ids to flag , or 'all'
* @ param string _folder foldername
* @ param string _forceDeleteMethod - " no " , or deleteMethod like 'move_to_trash' , " mark_as_deleted " , " remove_immediately "
*
* @ return bool true , as we do not handle return values yet
*/
function deleteMessages ( $_messageUID , $_folder = NULL , $_forceDeleteMethod = 'no' )
{
2013-11-21 14:22:34 +01:00
//error_log(__METHOD__.__LINE__.'->'.array2string($_messageUID).','.array2string($_folder).', '.$_forceDeleteMethod);
2013-02-28 10:28:08 +01:00
$msglist = '' ;
$oldMailbox = '' ;
if ( is_null ( $_folder ) || empty ( $_folder )) $_folder = $this -> sessionData [ 'mailbox' ];
2014-01-23 12:56:34 +01:00
if ( empty ( $_messageUID ))
2013-02-28 10:28:08 +01:00
{
2014-01-23 12:56:34 +01:00
if ( self :: $debug ) error_log ( __METHOD__ . " no messages Message(s): " . implode ( ',' , $_messageUID ));
return false ;
}
elseif ( $_messageUID === 'all' )
{
$_messageUID = null ;
2013-02-28 10:28:08 +01:00
}
2013-10-31 15:45:11 +01:00
else
{
$uidsToDelete = new Horde_Imap_Client_Ids ();
2014-01-23 12:56:34 +01:00
if ( ! ( is_object ( $_messageUID ) || is_array ( $_messageUID ))) $_messageUID = ( array ) $_messageUID ;
2013-10-31 15:45:11 +01:00
$uidsToDelete -> add ( $_messageUID );
}
2013-02-28 10:28:08 +01:00
$deleteOptions = $_forceDeleteMethod ; // use forceDeleteMethod if not "no", or unknown method
2013-10-31 15:45:11 +01:00
if ( $_forceDeleteMethod === 'no' || ! in_array ( $_forceDeleteMethod , array ( 'move_to_trash' , " mark_as_deleted " , " remove_immediately " ))) $deleteOptions = ( $this -> mailPreferences [ 'deleteOptions' ] ? $this -> mailPreferences [ 'deleteOptions' ] : " mark_as_deleted " );
2013-02-28 10:28:08 +01:00
//error_log(__METHOD__.__LINE__.'->'.array2string($_messageUID).','.$_folder.'/'.$this->sessionData['mailbox'].' Option:'.$deleteOptions);
$trashFolder = $this -> getTrashFolder ();
2013-03-05 15:09:35 +01:00
$draftFolder = $this -> getDraftFolder (); //$GLOBALS['egw_info']['user']['preferences']['mail']['draftFolder'];
$templateFolder = $this -> getTemplateFolder (); //$GLOBALS['egw_info']['user']['preferences']['mail']['templateFolder'];
2013-02-28 10:28:08 +01:00
if (( $_folder == $trashFolder && $deleteOptions == " move_to_trash " ) ||
( $_folder == $draftFolder )) {
$deleteOptions = " remove_immediately " ;
}
2013-10-31 09:55:33 +01:00
if ( $this -> icServer -> getCurrentMailbox () != $_folder ) {
$oldMailbox = $this -> icServer -> getCurrentMailbox ();
2013-10-29 17:16:56 +01:00
$this -> icServer -> openMailbox ( $_folder );
2013-02-28 10:28:08 +01:00
}
2014-01-23 12:56:34 +01:00
//error_log(__METHOD__.__LINE__.'->'.array2string($_messageUID).','.$_folder.'/'.$this->sessionData['mailbox'].' Option:'.$deleteOptions);
2013-02-28 10:28:08 +01:00
$updateCache = false ;
switch ( $deleteOptions ) {
case " move_to_trash " :
2014-01-23 12:56:34 +01:00
//error_log(__METHOD__.__LINE__);
2013-02-28 10:28:08 +01:00
$updateCache = true ;
2013-10-31 09:55:33 +01:00
if ( ! empty ( $trashFolder )); {
if ( self :: $debug ) error_log ( __METHOD__ . __LINE__ . implode ( ' : ' , $_messageUID ));
if ( self :: $debug ) error_log ( __METHOD__ . __LINE__ . " $trashFolder <= $_folder / " . $this -> sessionData [ 'mailbox' ]);
2013-02-28 10:28:08 +01:00
// copy messages
2013-10-31 15:45:11 +01:00
$retValue = $this -> icServer -> copy ( $_folder , $trashFolder , array ( 'ids' => $uidsToDelete , 'move' => true ));
2013-02-28 10:28:08 +01:00
}
break ;
case " mark_as_deleted " :
2014-01-23 12:56:34 +01:00
//error_log(__METHOD__.__LINE__);
2013-02-28 10:28:08 +01:00
// mark messages as deleted
2013-11-04 12:22:38 +01:00
if ( is_null ( $_messageUID )) $_messageUID = 'all' ;
2013-02-28 10:28:08 +01:00
foreach (( array ) $_messageUID as $key => $uid )
{
//flag messages, that are flagged for deletion as seen too
$this -> flagMessages ( 'read' , $uid , $_folder );
$flags = $this -> getFlags ( $uid );
2013-10-31 09:55:33 +01:00
$this -> flagMessages ( 'delete' , $uid , $_folder );
2013-02-28 10:28:08 +01:00
//error_log(__METHOD__.__LINE__.array2string($flags));
if ( strpos ( array2string ( $flags ), 'Deleted' ) !== false ) $undelete [] = $uid ;
unset ( $flags );
}
foreach (( array ) $undelete as $key => $uid )
{
$this -> flagMessages ( 'undelete' , $uid , $_folder );
}
break ;
case " remove_immediately " :
2014-01-23 12:56:34 +01:00
//error_log(__METHOD__.__LINE__);
2013-02-28 10:28:08 +01:00
$updateCache = true ;
2013-11-04 12:22:38 +01:00
if ( is_null ( $_messageUID )) $_messageUID = 'all' ;
2014-01-23 12:56:34 +01:00
if ( is_object ( $_messageUID ))
2013-10-31 09:55:33 +01:00
{
2014-01-23 12:56:34 +01:00
$this -> flagMessages ( 'delete' , $_messageUID , $_folder );
}
else
{
foreach (( array ) $_messageUID as $key => $uid )
{
//flag messages, that are flagged for deletion as seen too
$this -> flagMessages ( 'delete' , $uid , $_folder );
}
2013-10-31 09:55:33 +01:00
}
2013-02-28 10:28:08 +01:00
// delete the messages finaly
2013-11-04 12:22:38 +01:00
$this -> icServer -> expunge ( $_folder );
2013-02-28 10:28:08 +01:00
break ;
}
if ( $oldMailbox != '' ) {
2013-10-29 17:16:56 +01:00
$this -> icServer -> openMailbox ( $oldMailbox );
2013-02-28 10:28:08 +01:00
}
return true ;
}
2013-06-05 16:24:56 +02:00
/**
* get flags for a Message
*
* @ param mixed string _messageUID array of id to retrieve the flags for
*
* @ return null / array flags
*/
function getFlags ( $_messageUID ) {
2013-10-31 10:13:56 +01:00
$uidsToFetch = new Horde_Imap_Client_Ids ();
2014-01-23 12:56:34 +01:00
if ( ! ( is_object ( $_messageUID ) || is_array ( $_messageUID ))) $_messageUID = ( array ) $_messageUID ;
$uidsToFetch -> add ( $_messageUID );
2013-10-31 10:13:56 +01:00
$_folderName = $this -> icServer -> getCurrentMailbox ();
$fquery = new Horde_Imap_Client_Fetch_Query ();
$fquery -> flags ();
$headersNew = $this -> icServer -> fetch ( $_folderName , $fquery , array (
'ids' => $uidsToFetch ,
));
if ( is_object ( $headersNew )) {
foreach ( $headersNew -> ids () as $id ) {
$_headerObject = $headersNew -> get ( $id );
$flags = $_headerObject -> getFlags ();
}
2013-06-05 16:24:56 +02:00
}
2013-10-31 10:13:56 +01:00
2013-06-05 16:24:56 +02:00
return $flags ;
}
/**
* get and parse the flags response for the Notifyflag for a Message
*
* @ param string _messageUID array of id to retrieve the flags for
* @ param array flags - to avoid additional server call
*
* @ return null / boolean
*/
function getNotifyFlags ( $_messageUID , $flags = null )
{
2013-10-31 10:13:56 +01:00
if ( $flags === null ) $flags = $this -> getFlags ( $_messageUID );
2013-06-05 16:24:56 +02:00
if ( self :: $debug ) error_log ( __METHOD__ . $_messageUID . ' Flags:' . array2string ( $flags ));
if ( PEAR :: isError ( $flags ))
{
return null ;
}
if ( stripos ( array2string ( $flags ), 'MDNSent' ) !== false )
return true ;
if ( stripos ( array2string ( $flags ), 'MDNnotSent' ) !== false )
return false ;
return null ;
}
2013-02-19 17:30:59 +01:00
/**
* flag a Message
*
* @ param string _flag ( readable name )
* @ param mixed array / string _messageUID array of ids to flag , or 'all'
* @ param string _folder foldername
*
* @ todo handle handle icserver -> setFlags returnValue
*
* @ return bool true , as we do not handle icserver -> setFlags returnValue
*/
function flagMessages ( $_flag , $_messageUID , $_folder = NULL )
{
2013-10-31 10:13:56 +01:00
//error_log(__METHOD__.__LINE__.'->' .$_flag." ".array2string($_messageUID).",$_folder /".$this->sessionData['mailbox']);
2014-01-23 12:56:34 +01:00
if ( empty ( $_messageUID ))
{
if ( self :: $debug ) error_log ( __METHOD__ . " no messages Message(s): " . implode ( ',' , $_messageUID ));
return false ;
}
elseif ( $_messageUID === 'all' )
{
$uidsToModify = null ;
2013-02-19 17:30:59 +01:00
}
2013-10-31 09:55:33 +01:00
else
{
$uidsToModify = new Horde_Imap_Client_Ids ();
2014-01-23 12:56:34 +01:00
if ( ! ( is_object ( $_messageUID ) || is_array ( $_messageUID ))) $_messageUID = ( array ) $_messageUID ;
2013-10-31 09:55:33 +01:00
$uidsToModify -> add ( $_messageUID );
}
2013-02-19 17:30:59 +01:00
2013-10-29 17:16:56 +01:00
$this -> icServer -> openMailbox (( $_folder ? $_folder : $this -> sessionData [ 'mailbox' ]));
2013-10-31 09:55:33 +01:00
$folder = $this -> icServer -> getCurrentMailbox ();;
2013-02-19 17:30:59 +01:00
switch ( $_flag ) {
2013-10-31 09:55:33 +01:00
case " delete " :
$ret = $this -> icServer -> store ( $folder , array ( 'add' => array ( '\\Deleted' ), 'ids' => $uidsToModify ));
break ;
2013-02-19 17:30:59 +01:00
case " undelete " :
2013-10-31 09:55:33 +01:00
$ret = $this -> icServer -> store ( $folder , array ( 'remove' => array ( '\\Deleted' ), 'ids' => $uidsToModify ));
2013-02-19 17:30:59 +01:00
break ;
case " flagged " :
2013-10-31 09:55:33 +01:00
$ret = $this -> icServer -> store ( $folder , array ( 'add' => array ( '\\Flagged' ), 'ids' => $uidsToModify ));
2013-02-19 17:30:59 +01:00
break ;
case " read " :
2014-01-13 14:06:29 +01:00
case " seen " :
2013-10-31 09:55:33 +01:00
$ret = $this -> icServer -> store ( $folder , array ( 'add' => array ( '\\Seen' ), 'ids' => $uidsToModify ));
2013-02-19 17:30:59 +01:00
break ;
case " forwarded " :
2013-10-31 09:55:33 +01:00
$ret = $this -> icServer -> store ( $folder , array ( 'add' => array ( '$Forwarded' ), 'ids' => $uidsToModify ));
2013-02-19 17:30:59 +01:00
case " answered " :
2013-10-31 09:55:33 +01:00
$ret = $this -> icServer -> store ( $folder , array ( 'add' => array ( '\\Answered' ), 'ids' => $uidsToModify ));
2013-02-19 17:30:59 +01:00
break ;
case " unflagged " :
2013-10-31 09:55:33 +01:00
$ret = $this -> icServer -> store ( $folder , array ( 'remove' => array ( '\\Flagged' ), 'ids' => $uidsToModify ));
2013-02-19 17:30:59 +01:00
break ;
case " unread " :
2014-01-13 14:06:29 +01:00
case " unseen " :
2013-10-31 09:55:33 +01:00
$ret = $this -> icServer -> store ( $folder , array ( 'remove' => array ( '\\Seen' , '\\Answered' , '$Forwarded' ), 'ids' => $uidsToModify ));
2013-02-19 17:30:59 +01:00
break ;
case " mdnsent " :
2013-10-31 09:55:33 +01:00
$ret = $this -> icServer -> store ( $folder , array ( 'add' => array ( 'MDNSent' ), 'ids' => $uidsToModify ));
2013-02-19 17:30:59 +01:00
break ;
case " mdnnotsent " :
2013-10-31 09:55:33 +01:00
$ret = $this -> icServer -> store ( $folder , array ( 'add' => array ( 'MDNnotSent' ), 'ids' => $uidsToModify ));
2013-02-19 17:30:59 +01:00
break ;
case " label1 " :
case " labelone " :
2013-10-31 09:55:33 +01:00
$ret = $this -> icServer -> store ( $folder , array ( 'add' => array ( '$label1' ), 'ids' => $uidsToModify ));
2013-02-19 17:30:59 +01:00
break ;
case " unlabel1 " :
case " unlabelone " :
2013-10-31 09:55:33 +01:00
$ret = $this -> icServer -> store ( $folder , array ( 'remove' => array ( '$label1' ), 'ids' => $uidsToModify ));
2013-02-19 17:30:59 +01:00
break ;
case " label2 " :
case " labeltwo " :
2013-10-31 09:55:33 +01:00
$ret = $this -> icServer -> store ( $folder , array ( 'add' => array ( '$label2' ), 'ids' => $uidsToModify ));
2013-02-19 17:30:59 +01:00
break ;
case " unlabel2 " :
case " unlabeltwo " :
2013-10-31 09:55:33 +01:00
$ret = $this -> icServer -> store ( $folder , array ( 'remove' => array ( '$label2' ), 'ids' => $uidsToModify ));
2013-02-19 17:30:59 +01:00
break ;
case " label3 " :
case " labelthree " :
2013-10-31 09:55:33 +01:00
$ret = $this -> icServer -> store ( $folder , array ( 'add' => array ( '$label3' ), 'ids' => $uidsToModify ));
2013-02-19 17:30:59 +01:00
break ;
case " unlabel3 " :
case " unlabelthree " :
2013-10-31 09:55:33 +01:00
$ret = $this -> icServer -> store ( $folder , array ( 'remove' => array ( '$label3' ), 'ids' => $uidsToModify ));
2013-02-19 17:30:59 +01:00
break ;
case " label4 " :
case " labelfour " :
2013-10-31 09:55:33 +01:00
$ret = $this -> icServer -> store ( $folder , array ( 'add' => array ( '$label4' ), 'ids' => $uidsToModify ));
2013-02-19 17:30:59 +01:00
break ;
case " unlabel4 " :
case " unlabelfour " :
2013-10-31 09:55:33 +01:00
$ret = $this -> icServer -> store ( $folder , array ( 'remove' => array ( '$label4' ), 'ids' => $uidsToModify ));
2013-02-19 17:30:59 +01:00
break ;
case " label5 " :
case " labelfive " :
2013-10-31 09:55:33 +01:00
$ret = $this -> icServer -> store ( $folder , array ( 'add' => array ( '$label5' ), 'ids' => $uidsToModify ));
2013-02-19 17:30:59 +01:00
break ;
case " unlabel5 " :
case " unlabelfive " :
2013-10-31 09:55:33 +01:00
$ret = $this -> icServer -> store ( $folder , array ( 'remove' => array ( '$label5' ), 'ids' => $uidsToModify ));
2013-02-19 17:30:59 +01:00
break ;
2013-12-06 11:35:39 +01:00
case " unlabel " :
$ret = $this -> icServer -> store ( $folder , array ( 'remove' => array ( '$label1' ), 'ids' => $uidsToModify ));
$ret = $this -> icServer -> store ( $folder , array ( 'remove' => array ( '$label2' ), 'ids' => $uidsToModify ));
$ret = $this -> icServer -> store ( $folder , array ( 'remove' => array ( '$label3' ), 'ids' => $uidsToModify ));
$ret = $this -> icServer -> store ( $folder , array ( 'remove' => array ( '$label4' ), 'ids' => $uidsToModify ));
$ret = $this -> icServer -> store ( $folder , array ( 'remove' => array ( '$label5' ), 'ids' => $uidsToModify ));
break ;
2013-06-04 17:29:53 +02:00
}
2013-02-19 17:30:59 +01:00
2013-10-22 12:06:21 +02:00
self :: $folderStatusCache [ $this -> profileID ][( ! empty ( $_folder ) ? $_folder : $this -> sessionData [ 'mailbox' ])][ 'uidValidity' ] = 0 ;
2013-02-19 17:30:59 +01:00
//error_log(__METHOD__.__LINE__.'->' .$_flag." ".array2string($_messageUID).",".($_folder?$_folder:$this->sessionData['mailbox']));
return true ; // as we do not catch/examine setFlags returnValue
}
2013-04-29 16:56:33 +02:00
/**
* move Message ( s )
*
* @ param string _foldername target folder
* @ param mixed array / string _messageUID array of ids to flag , or 'all'
* @ param boolean $deleteAfterMove - decides if a mail is moved ( true ) or copied ( false )
* @ param string $currentFolder
* @ param boolean $returnUIDs - control wether or not the action called should return the new uids
* caveat : not all servers do support that
2013-12-18 13:14:18 +01:00
* @ param int $_targetProfileID - target profile ID , should only be handed over when target server is differen from source
2013-04-29 16:56:33 +02:00
*
* @ return mixed / bool true , false or new uid
*/
2013-12-18 13:14:18 +01:00
function moveMessages ( $_foldername , $_messageUID , $deleteAfterMove = true , $currentFolder = Null , $returnUIDs = false , $_targetProfileID = Null )
2013-04-29 16:56:33 +02:00
{
$msglist = '' ;
$deleteOptions = $GLOBALS [ 'egw_info' ][ " user " ][ " preferences " ][ " mail " ][ " deleteOptions " ];
2014-01-23 12:56:34 +01:00
if ( empty ( $_messageUID ))
2013-04-29 16:56:33 +02:00
{
2014-01-23 12:56:34 +01:00
if ( self :: $debug ) error_log ( __METHOD__ . " no messages Message(s): " . implode ( ',' , $_messageUID ));
return false ;
}
elseif ( $_messageUID === 'all' )
{
$uidsToMove = null ;
2013-11-21 14:22:34 +01:00
}
else
{
$uidsToMove = new Horde_Imap_Client_Ids ();
2014-01-23 12:56:34 +01:00
if ( ! ( is_object ( $_messageUID ) || is_array ( $_messageUID ))) $_messageUID = ( array ) $_messageUID ;
2013-11-21 14:22:34 +01:00
$uidsToMove -> add ( $_messageUID );
}
2013-12-18 16:14:12 +01:00
$sourceFolder = ( ! empty ( $currentFolder ) ? $currentFolder : $this -> sessionData [ 'mailbox' ]);
2013-12-18 13:14:18 +01:00
if ( ! is_null ( $_targetProfileID ) && $_targetProfileID !== $this -> icServer -> ImapServerId )
{
2013-12-18 16:14:12 +01:00
$sourceFolder = $this -> icServer -> getMailbox ( $sourceFolder );
2013-12-18 13:14:18 +01:00
$target = emailadmin_account :: read ( $_targetProfileID ) -> imapServer ();
2014-02-18 16:22:25 +01:00
$foldername = $target -> getMailbox ( $_foldername );
//error_log(__METHOD__.__LINE__.' Sourceserver:'.$this->icServer->ImapServerId.' TargetServer:'.$_targetProfileID.' TargetFolderObject:'.array2string($foldername));
2013-12-18 16:14:12 +01:00
$uidsToFetch = new Horde_Imap_Client_Ids ();
$uidsToFetch -> add ( $_messageUID );
2013-04-29 16:56:33 +02:00
2013-12-18 16:14:12 +01:00
$fquery = new Horde_Imap_Client_Fetch_Query ();
2014-01-17 13:02:03 +01:00
$fquery -> fullText ( array ( 'peek' => true ));
2013-12-18 16:14:12 +01:00
$fquery -> flags ();
$headersNew = $this -> icServer -> fetch ( $sourceFolder , $fquery , array (
'ids' => $uidsToFetch ,
));
if ( is_object ( $headersNew )) {
$c = 0 ;
$retUid = new Horde_Imap_Client_Ids ();
// we copy chunks of 5 to avoid too much memory and/or server stress
foreach ( $headersNew as $id => $_headerObject ) {
$c ++ ;
$flags = $_headerObject -> getFlags (); //unseen status seems to be lost when retrieving the full message
//error_log(__METHOD__.__LINE__.array2string($flags));
$body = $_headerObject -> getFullMsg ();
$dataNflags [] = array ( 'data' => array ( array ( 't' => 'text' , 'v' => " $body " )), 'flags' => $flags );
if ( $c == 5 )
{
2014-02-18 16:22:25 +01:00
$ret = $target -> append ( $foldername , $dataNflags );
2013-12-18 16:14:12 +01:00
$retUid -> add ( $ret );
unset ( $dataNflags );
time_nanosleep ( 0 , 500000 ); // sleep 500 miliseconds
$c = 0 ;
}
}
if ( isset ( $dataNflags ))
{
2014-02-18 16:22:25 +01:00
$ret = $target -> append ( $foldername , $dataNflags );
2013-12-18 16:14:12 +01:00
$retUid -> add ( $ret );
unset ( $dataNflags );
}
2014-01-17 13:02:03 +01:00
if ( $deleteAfterMove )
{
$this -> deleteMessages ( $_messageUID , $sourceFolder , $_forceDeleteMethod = 'remove_immediately' );
}
2013-12-18 16:14:12 +01:00
}
2013-11-21 14:22:34 +01:00
}
2013-12-18 16:14:12 +01:00
else
2013-11-21 14:22:34 +01:00
{
2013-12-18 16:14:12 +01:00
try
{
$retUid = $this -> icServer -> copy ( $sourceFolder , $_foldername , array ( 'ids' => $uidsToMove , 'move' => $deleteAfterMove ));
}
catch ( exception $e )
{
error_log ( __METHOD__ . __LINE__ . " Copying to Folder $_foldername failed! Error: " . $e -> getMessage ());
throw new egw_exception ( " Copying to Folder $_foldername failed! Error: " . $e -> getMessage ());
return false ;
}
2013-11-21 14:22:34 +01:00
}
2013-06-04 17:29:53 +02:00
2013-04-29 16:56:33 +02:00
//error_log(__METHOD__.__LINE__.array2string($retUid));
return ( $returnUIDs ? $retUid : true );
}
2013-02-08 20:11:44 +01:00
/**
* Helper function to handle wrong or unrecognized timezones
* returns the date as it is parseable by strtotime , or current timestamp if everything failes
* @ param string date to be parsed / formatted
* @ param string format string , if none is passed , use the users common dateformat supplemented by the time hour : minute : second
* @ return string returns the date as it is parseable by strtotime , or current timestamp if everything failes
*/
static function _strtotime ( $date = '' , $format = NULL , $convert2usertime = false )
{
if ( $format == NULL ) $format = $GLOBALS [ 'egw_info' ][ 'user' ][ 'preferences' ][ 'common' ][ 'dateformat' ] . ' ' . ( $GLOBALS [ 'egw_info' ][ 'user' ][ 'preferences' ][ 'common' ][ 'timeformat' ] == 12 ? 'h:i:s a' : 'H:i:s' );
$date2return = ( $convert2usertime ? egw_time :: server2user ( $date , $format ) : egw_time :: to ( $date , $format ));
if ( $date2return == null )
{
$dtarr = explode ( ' ' , $date );
$test = null ;
while ( $test === null && count ( $dtarr ) >= 1 )
{
array_pop ( $dtarr );
$test = ( $convert2usertime ? egw_time :: server2user ( implode ( ' ' , $dtarr ), $format ) : egw_time :: to ( implode ( ' ' , $dtarr ), $format ));
if ( $test ) $date2return = $test ;
}
if ( $test === null ) $date2return = egw_time :: to ( 'now' , $format );
}
return $date2return ;
}
/**
* htmlentities
* helperfunction to cope with wrong encoding in strings
* @ param string $_string input to be converted
2013-02-11 18:05:29 +01:00
* @ param mixed $charset false or string -> Target charset , if false mail_bo displayCharset will be used
2013-02-08 20:11:44 +01:00
* @ return string
*/
static function htmlentities ( $_string , $_charset = false )
{
//setting the charset (if not given)
if ( $_charset === false ) $_charset = self :: $displayCharset ;
$_stringORG = $_string ;
$_string = @ htmlentities ( $_string , ENT_QUOTES , $_charset , false );
2013-03-05 15:09:35 +01:00
if ( empty ( $_string ) && ! empty ( $_stringORG )) $_string = @ htmlentities ( translation :: convert ( $_stringORG , translation :: detect_encoding ( $_stringORG ), $_charset ), ENT_QUOTES | ENT_IGNORE , $_charset , false );
2013-02-08 20:11:44 +01:00
return $_string ;
}
2013-03-05 15:09:35 +01:00
/**
* htmlspecialchars
2013-10-14 15:30:04 +02:00
* helperfunction to cope with wrong encoding in strings ;
* seems to be outdated and not needed any more for et2
2013-03-05 15:09:35 +01:00
* @ param string $_string input to be converted
* @ param mixed $charset false or string -> Target charset , if false mail displayCharset will be used
* @ return string
*/
static function htmlspecialchars ( $_string , $_charset = false )
{
2013-10-14 15:30:04 +02:00
return $_string ;
/*
2013-03-05 15:09:35 +01:00
//setting the charset (if not given)
if ( $_charset === false ) $_charset = self :: $displayCharset ;
$_stringORG = $_string ;
$_string = @ htmlspecialchars ( $_string , ENT_QUOTES , $_charset , false );
2013-10-05 11:47:34 +02:00
if ( empty ( $_string ) && ! empty ( $_stringORG )) $_string = @ htmlspecialchars ( translation :: convert ( $_stringORG , translation :: detect_encoding ( $_stringORG ), $_charset ), ENT_QUOTES | ENT_IGNORE , $_charset , false );
2013-03-05 15:09:35 +01:00
return $_string ;
2013-10-14 15:30:04 +02:00
*/
2013-03-05 15:09:35 +01:00
}
/**
* clean a message from elements regarded as potentially harmful
* param string / reference $_html is the text to be processed
* param boolean $usepurify - obsolet , as we always use htmlLawed
* param boolean $cleanTags - use tidy ( if available ) to clean / balance tags
* return nothing
*/
static function getCleanHTML ( & $_html , $usepurify = false , $cleanTags = true )
{
// remove CRLF and TAB as it is of no use in HTML.
// but they matter in <pre>, so we rather don't
//$_html = str_replace("\r\n",' ',$_html);
//$_html = str_replace("\t",' ',$_html);
//error_log($_html);
//repair doubleencoded ampersands, and some stuff htmLawed stumbles upon with balancing switched on
$_html = str_replace ( array ( '&amp;' , '<DIV><BR></DIV>' , " <DIV> </DIV> " , '<div> </div>' , '</td></font>' , '<br><td>' , '<tr></tr>' , '<o:p></o:p>' , '<o:p>' , '</o:p>' ),
array ( '&' , '<BR>' , '<BR>' , '<BR>' , '</font></td>' , '<td>' , '' , '' , '' , '' ), $_html );
//$_html = str_replace(array('&amp;'),array('&'),$_html);
2013-04-12 15:10:27 +02:00
if ( stripos ( $_html , 'style' ) !== false ) translation :: replaceTagsCompletley ( $_html , 'style' ); // clean out empty or pagewide style definitions / left over tags
if ( stripos ( $_html , 'head' ) !== false ) translation :: replaceTagsCompletley ( $_html , 'head' ); // Strip out stuff in head
//if (stripos($_html,'![if')!==false && stripos($_html,'<![endif]>')!==false) translation::replaceTagsCompletley($_html,'!\[if','<!\[endif\]>',false); // Strip out stuff in ifs
//if (stripos($_html,'!--[if')!==false && stripos($_html,'<![endif]-->')!==false) translation::replaceTagsCompletley($_html,'!--\[if','<!\[endif\]-->',false); // Strip out stuff in ifs
2013-03-05 15:09:35 +01:00
//error_log(__METHOD__.__LINE__.$_html);
// force the use of kses, as it is still have the edge over purifier with some stuff
$usepurify = true ;
if ( $usepurify )
{
// we need a customized config, as we may allow external images, $GLOBALS['egw_info']['user']['preferences']['mail']['allowExternalIMGs']
if ( get_magic_quotes_gpc () === 1 ) $_html = stripslashes ( $_html );
// Strip out doctype in head, as htmlLawed cannot handle it TODO: Consider extracting it and adding it afterwards
2013-04-12 15:10:27 +02:00
if ( stripos ( $_html , '!doctype' ) !== false ) translation :: replaceTagsCompletley ( $_html , '!doctype' );
if ( stripos ( $_html , '?xml:namespace' ) !== false ) translation :: replaceTagsCompletley ( $_html , '\?xml:namespace' , '/>' , false );
if ( stripos ( $_html , '?xml version' ) !== false ) translation :: replaceTagsCompletley ( $_html , '\?xml version' , '\?>' , false );
if ( strpos ( $_html , '!CURSOR' ) !== false ) translation :: replaceTagsCompletley ( $_html , '!CURSOR' );
2013-03-05 15:09:35 +01:00
// htmLawed filter only the 'body'
//preg_match('`(<htm.+?<body[^>]*>)(.+?)(</body>.*?</html>)`ims', $_html, $matches);
//if ($matches[2])
//{
// $hasOther = true;
// $_html = $matches[2];
//}
// purify got switched to htmLawed
// some testcode to test purifying / htmlawed
//$_html = "<BLOCKQUOTE>hi <div> there </div> kram <br> </blockquote>".$_html;
$_html = html :: purify ( $_html , self :: $htmLawed_config , array (), true );
//if ($hasOther) $_html = $matches[1]. $_html. $matches[3];
// clean out comments , should not be needed as purify should do the job.
$search = array (
'@url\(http:\/\/[^\)].*?\)@si' , // url calls e.g. in style definitions
'@<!--[\s\S]*?[ \t\n\r]*-->@' , // Strip multi-line comments including CDATA
);
$_html = preg_replace ( $search , " " , $_html );
// remove non printable chars
$_html = preg_replace ( '/([\000-\012])/' , '' , $_html );
//error_log(__METHOD__.':'.__LINE__.':'.$_html);
}
// using purify above should have tidied the tags already sufficiently
if ( $usepurify == false && $cleanTags == true )
{
if ( extension_loaded ( 'tidy' ))
{
$tidy = new tidy ();
$cleaned = $tidy -> repairString ( $_html , self :: $tidy_config , 'utf8' );
// Found errors. Strip it all so there's some output
if ( $tidy -> getStatus () == 2 )
{
error_log ( __METHOD__ . __LINE__ . ' ->' . $tidy -> errorBuffer );
}
else
{
$_html = $cleaned ;
}
}
else
{
//$to = ini_get('max_execution_time');
//@set_time_limit(10);
$htmLawed = new egw_htmLawed ();
$_html = $htmLawed -> egw_htmLawed ( $_html );
//error_log(__METHOD__.__LINE__.$_html);
//@set_time_limit($to);
}
}
}
2013-02-19 17:30:59 +01:00
/**
* Header and Bodystructure stuff
*/
2013-03-05 15:09:35 +01:00
/**
* getMimePartCharset - fetches the charset mimepart if it exists
* @ param $_mimePartObject structure object
* @ return mixed mimepart or false if no CHARSET is found , the missing charset has to be handled somewhere else ,
* as we cannot safely assume any charset as we did earlier
*/
function getMimePartCharset ( $_mimePartObject )
{
//$charSet = 'iso-8859-1';//self::$displayCharset; //'iso-8859-1'; // self::displayCharset seems to be asmarter fallback than iso-8859-1
$CharsetFound = false ;
//echo "#".$_mimePartObject->encoding.'#<br>';
if ( is_array ( $_mimePartObject -> parameters )) {
if ( isset ( $_mimePartObject -> parameters [ 'CHARSET' ])) {
$charSet = $_mimePartObject -> parameters [ 'CHARSET' ];
$CharsetFound = true ;
}
}
// this one is dirty, but until I find something that does the trick of detecting the encoding, ....
//if ($CharsetFound == false && $_mimePartObject->encoding == "QUOTED-PRINTABLE") $charSet = 'iso-8859-1'; //assume quoted-printable to be ISO
//if ($CharsetFound == false && $_mimePartObject->encoding == "BASE64") $charSet = 'utf-8'; // assume BASE64 to be UTF8
return ( $CharsetFound ? $charSet : $CharsetFound );
}
/**
* decodeMimePart - fetches the charset mimepart if it exists
* @ param string $_mimeMessage - the message to be decoded
* @ param string $_encoding - the encoding used BASE64 and QUOTED - PRINTABLE is supported
* @ param string $_charset - not used
* @ return string decoded mimePart
*/
function decodeMimePart ( $_mimeMessage , $_encoding , $_charset = '' )
{
// decode the part
if ( self :: $debug ) error_log ( __METHOD__ . " () with $_encoding and $_charset : " . print_r ( $_mimeMessage , true ));
switch ( strtoupper ( $_encoding ))
{
case 'BASE64' :
// use imap_base64 to decode, not any longer, as it is strict, and fails if it encounters invalid chars
return base64_decode ( $_mimeMessage ); //imap_base64($_mimeMessage);
break ;
case 'QUOTED-PRINTABLE' :
// use imap_qprint to decode
return quoted_printable_decode ( $_mimeMessage );
break ;
default :
// it is either not encoded or we don't know about it
return $_mimeMessage ;
break ;
}
}
/**
* getMultipartAlternative
* get part of the message , if its stucture is indicating its of multipart alternative style
* a wrapper for multipartmixed
* @ param string / int $_uid the messageuid ,
2013-11-10 21:53:51 +01:00
* @ param Horde_Mime_Part $_structure structure for parsing
2013-03-05 15:09:35 +01:00
* @ param string $_htmlMode , how to display a message , html , plain text , ...
* @ param boolean $_preserveSeen flag to preserve the seenflag by using body . peek
* @ return array containing the desired part
*/
2013-11-10 21:53:51 +01:00
function getMultipartAlternative ( $_uid , Horde_Mime_Part $_structure , $_htmlMode , $_preserveSeen = false )
2013-03-05 15:09:35 +01:00
{
// a multipart/alternative has exactly 2 parts (text and html OR text and something else)
// sometimes there are 3 parts, when there is an ics/ical attached/included-> we want to show that
// as attachment AND as abstracted ical information (we use our notification style here).
2014-02-10 17:37:21 +01:00
$partText = $partCalendar = $partHTML = null ;
2013-03-05 15:09:35 +01:00
if ( self :: $debug ) _debug_array ( array ( " METHOD " => __METHOD__ , " LINE " => __LINE__ , " STRUCTURE " => $_structure ));
2014-02-13 13:59:43 +01:00
//error_log(__METHOD__.__LINE__);
2013-11-19 14:18:30 +01:00
$ignore_first_part = true ;
2013-11-10 21:53:51 +01:00
foreach ( $_structure -> contentTypeMap () as $mime_id => $mime_type )
{
2014-02-10 17:37:21 +01:00
//error_log(__METHOD__."($_uid, ".$_structure->getMimeId().") $mime_id: $mime_type"." ignoreFirstPart:".$ignore_first_part);
2013-11-10 21:53:51 +01:00
if ( self :: $debug ) echo __METHOD__ . " ( $_uid , partID= " . $_structure -> getMimeId () . " ) $mime_id : $mime_type <br> " ;
2013-11-19 14:18:30 +01:00
if ( $ignore_first_part )
{
$ignore_first_part = false ;
continue ; // ignore multipart/alternative itself
}
2013-11-10 21:53:51 +01:00
$mimePart = $_structure -> getPart ( $mime_id );
switch ( $mimePart -> getPrimaryType ())
{
case 'text' :
switch ( $mimePart -> getSubType ())
{
case 'calendar' : // only if there is no partText set already
2014-02-10 17:37:21 +01:00
//if ($partText) break;
if ( $mimePart -> getBytes () > 0 ) $partCalendar = $mimePart ;
break ;
2013-11-10 21:53:51 +01:00
// fall throught
case 'plain' :
if ( $mimePart -> getBytes () > 0 ) $partText = $mimePart ;
break ;
case 'html' :
if ( $mimePart -> getBytes () > 0 ) $partHTML = $mimePart ;
break ;
}
break ;
case 'multipart' :
switch ( $mimePart -> getSubType ())
{
case 'related' :
case 'mixed' :
if ( count ( $mimePart -> getParts ()) > 1 )
{
// in a multipart alternative we treat the multipart/related as html part
2014-02-13 13:59:43 +01:00
if ( self :: $debug ) error_log ( __METHOD__ . " process MULTIPART/ " . $mimePart -> getSubType () . " with array as subparts " );
2013-11-10 21:53:51 +01:00
$partHTML = $mimePart ;
2014-02-13 13:59:43 +01:00
break 3 ; // GET OUT OF LOOP, will be processed according to type
2013-11-10 21:53:51 +01:00
}
break ;
case 'alternative' :
if ( count ( $mimePart -> getParts ()) > 1 )
{
//cascading multipartAlternative structure, assuming only the first one is to be used
2013-11-16 14:10:47 +01:00
return $this -> getMultipartAlternative ( $_uid , $mimePart , $_htmlMode , $_preserveSeen );
2013-11-10 21:53:51 +01:00
}
}
2013-03-05 15:09:35 +01:00
}
}
2014-02-10 17:37:21 +01:00
// seems we have a multipart/alternative; as we assume we are able to display events
// the text/calendar part has higher precedence then text/plain
if ( $partCalendar && $partText ) $partText = $partCalendar ;
2013-11-10 21:53:51 +01:00
switch ( $_htmlMode )
{
2013-03-05 15:09:35 +01:00
case 'html_only' :
case 'always_display' :
2013-11-10 21:53:51 +01:00
if ( $partHTML )
{
switch ( $partHTML -> getSubType ())
{
case 'related' :
return $this -> getMultipartRelated ( $_uid , $partHTML , $_htmlMode , $_preserveSeen );
case 'mixed' :
return $this -> getMultipartMixed ( $_uid , $partHTML , $_htmlMode , $_preserveSeen );
default :
return $this -> getTextPart ( $_uid , $partHTML , $_htmlMode , $_preserveSeen );
2013-03-05 15:09:35 +01:00
}
2013-11-10 21:53:51 +01:00
}
elseif ( $partText && $_htmlMode == 'always_display' )
{
2013-03-05 15:09:35 +01:00
return $this -> getTextPart ( $_uid , $partText , $_htmlMode , $_preserveSeen );
}
break ;
2013-11-10 21:53:51 +01:00
2013-03-05 15:09:35 +01:00
case 'only_if_no_text' :
2013-11-10 21:53:51 +01:00
if ( $partText )
{
2013-03-05 15:09:35 +01:00
return $this -> getTextPart ( $_uid , $partText , $_htmlMode , $_preserveSeen );
2013-11-10 21:53:51 +01:00
}
if ( $partHTML )
{
if ( $partHTML -> getPrimaryType ())
{
2013-03-05 15:09:35 +01:00
return $this -> getMultipartRelated ( $_uid , $partHTML , $_htmlMode , $_preserveSeen );
}
2013-11-10 21:53:51 +01:00
return $this -> getTextPart ( $_uid , $partHTML , 'always_display' , $_preserveSeen );
2013-03-05 15:09:35 +01:00
}
break ;
default :
2013-11-10 21:53:51 +01:00
if ( $partText )
{
2013-03-05 15:09:35 +01:00
return $this -> getTextPart ( $_uid , $partText , $_htmlMode , $_preserveSeen );
}
2013-11-10 21:53:51 +01:00
$bodyPart = array (
'body' => lang ( " no plain text part found " ),
'mimeType' => 'text/plain' ,
'charSet' => self :: $displayCharset ,
);
2013-03-05 15:09:35 +01:00
break ;
}
return $bodyPart ;
}
/**
2013-11-10 21:53:51 +01:00
* Get part of the message , if its stucture is indicating its of multipart mixed style
*
* @ param int $_uid the messageuid ,
* @ param Horde_Mime_Part $_structure = '' , if given use structure for parsing
2013-03-05 15:09:35 +01:00
* @ param string $_htmlMode , how to display a message , html , plain text , ...
* @ param boolean $_preserveSeen flag to preserve the seenflag by using body . peek
* @ return array containing the desired part
*/
2013-11-10 21:53:51 +01:00
function getMultipartMixed ( $_uid , Horde_Mime_Part $_structure , $_htmlMode , $_preserveSeen = false )
2013-03-05 15:09:35 +01:00
{
if ( self :: $debug ) echo __METHOD__ . " $_uid , $_htmlMode <br> " ;
$bodyPart = array ();
if ( self :: $debug ) _debug_array ( $_structure );
2013-11-10 21:53:51 +01:00
2013-11-19 14:18:30 +01:00
$ignore_first_part = true ;
2013-11-10 21:53:51 +01:00
foreach ( $_structure -> contentTypeMap () as $mime_id => $mime_type )
{
//error_log(__METHOD__."($_uid, ".$_structure->getMimeId().") $mime_id: $mime_type");
if ( self :: $debug ) echo __METHOD__ . " ( $_uid , partID= " . $_structure -> getMimeId () . " ) $mime_id : $mime_type <br> " ;
2013-11-19 14:18:30 +01:00
if ( $ignore_first_part )
{
$ignore_first_part = false ;
continue ; // ignore multipart/mixed itself
}
2013-11-10 21:53:51 +01:00
$part = $_structure -> getPart ( $mime_id );
switch ( $part -> getPrimaryType ())
{
case 'multipart' :
switch ( $part -> getSubType ())
{
case 'alternative' :
2013-11-19 14:18:30 +01:00
return array ( $this -> getMultipartAlternative ( $_uid , $part , $_htmlMode , $_preserveSeen ));
2013-03-05 15:09:35 +01:00
2013-11-10 21:53:51 +01:00
case 'mixed' :
case 'signed' :
$bodyPart = array_merge ( $bodyPart , $this -> getMultipartMixed ( $_uid , $part , $_htmlMode , $_preserveSeen ));
2013-03-05 15:09:35 +01:00
break ;
2013-11-10 21:53:51 +01:00
case 'related' :
$bodyPart = array_merge ( $bodyPart , $this -> getMultipartRelated ( $_uid , $part , $_htmlMode , $_preserveSeen ));
2013-03-05 15:09:35 +01:00
break ;
}
break ;
2013-11-10 21:53:51 +01:00
case 'text' :
switch ( $part -> getSubType ())
{
case 'plain' :
case 'html' :
case 'calendar' : // inline ics/ical files
if ( $part -> getDisposition () != 'attachment' )
{
2013-03-05 15:09:35 +01:00
$bodyPart [] = $this -> getTextPart ( $_uid , $part , $_htmlMode , $_preserveSeen );
}
//error_log(__METHOD__.__LINE__.' ->'.$part->type."/".$part->subType.' -> BodyPart:'.array2string($bodyPart[count($bodyPart)-1]));
break ;
}
break ;
2013-11-10 21:53:51 +01:00
case 'message' :
if ( $part -> getSubType () == 'delivery-status' )
{
2013-03-05 15:09:35 +01:00
$bodyPart [] = $this -> getTextPart ( $_uid , $part , $_htmlMode , $_preserveSeen );
}
break ;
default :
// do nothing
// the part is a attachment
}
}
return $bodyPart ;
}
/**
* getMultipartRelated
* get part of the message , if its stucture is indicating its of multipart related style
* a wrapper for multipartmixed
* @ param string / int $_uid the messageuid ,
2013-11-10 21:53:51 +01:00
* @ param Horde_Mime_Part $_structure , if given use structure for parsing
2013-03-05 15:09:35 +01:00
* @ param string $_htmlMode , how to display a message , html , plain text , ...
* @ param boolean $_preserveSeen flag to preserve the seenflag by using body . peek
* @ return array containing the desired part
*/
2013-11-10 21:53:51 +01:00
function getMultipartRelated ( $_uid , Horde_Mime_Part $_structure , $_htmlMode , $_preserveSeen = false )
2013-03-05 15:09:35 +01:00
{
return $this -> getMultipartMixed ( $_uid , $_structure , $_htmlMode , $_preserveSeen );
}
/**
2013-11-10 21:53:51 +01:00
* Fetch a body part
*
* @ param int $_uid
* @ param string $_partID = null
* @ param string $_folder = null
* @ param boolean $_preserveSeen = false
* @ param boolean $_stream = false true return a stream , false return string
* @ param string & $_encoding = null on return : transfer encoding of returned part
* @ return string | resource
*/
function getBodyPart ( $_uid , $_partID = null , $_folder = null , $_preserveSeen = false , $_stream = false , & $_encoding = null )
{
if ( self :: $debug ) error_log ( __METHOD__ . " ( $_uid , $_partID , $_folder , $_preserveSeen ) " );
if ( empty ( $_folder ))
{
$_folder = ( $this -> sessionData [ 'mailbox' ] ? $this -> sessionData [ 'mailbox' ] : $this -> icServer -> getCurrentMailbox ());
}
2013-11-19 13:03:53 +01:00
//error_log(__METHOD__.__LINE__.array2string($_folder).'/'.$this->icServer->getCurrentMailbox().'/'. $this->sessionData['mailbox']);
2013-11-10 21:53:51 +01:00
// querying contents of body part
$uidsToFetch = new Horde_Imap_Client_Ids ();
2014-01-23 12:56:34 +01:00
if ( ! ( is_object ( $_uid ) || is_array ( $_uid ))) $_uid = ( array ) $_uid ;
$uidsToFetch -> add ( $_uid );
2013-11-10 21:53:51 +01:00
$fquery = new Horde_Imap_Client_Fetch_Query ();
$fquery -> bodyPart ( $_partID , array (
'peek' => $_preserveSeen ,
'decode' => true , // try decode on server, does NOT neccessary work
));
$part = $this -> icServer -> fetch ( $_folder , $fquery , array (
'ids' => $uidsToFetch ,
)) -> first ();
if ( ! $part ) return null ;
$_encoding = $part -> getBodyPartDecode ( $_partID );
return $part -> getBodyPart ( $_partID , $_stream );
}
/**
* Get Body from message
*
* @ param int $_uid the messageuid
* @ param Horde_Mime_Part $_structure = null , if given use structure for parsing
* @ param string $_htmlMode how to display a message : 'html_only' , 'always_display' , 'only_if_no_text' or ''
2013-03-05 15:09:35 +01:00
* @ param boolean $_preserveSeen flag to preserve the seenflag by using body . peek
2013-11-10 21:53:51 +01:00
* @ param boolean $_stream = false true return a stream , false return string
2013-03-05 15:09:35 +01:00
* @ return array containing the desired text part , mimeType and charset
*/
2013-11-10 21:53:51 +01:00
function getTextPart ( $_uid , Horde_Mime_Part $_structure , $_htmlMode = '' , $_preserveSeen = false , $_stream = false )
2013-03-05 15:09:35 +01:00
{
//error_log(__METHOD__.__LINE__.'->'.$_uid.':'.array2string($_structure).' '.function_backtrace());
$bodyPart = array ();
if ( self :: $debug ) _debug_array ( array ( $_structure , function_backtrace ()));
2013-11-10 21:53:51 +01:00
if ( $_structure -> getSubType () == 'html' && ! in_array ( $_htmlMode , array ( 'html_only' , 'always_display' , 'only_if_no_text' )))
2013-03-05 15:09:35 +01:00
{
$bodyPart = array (
'error' => 1 ,
'body' => lang ( " displaying html messages is disabled " ),
'mimeType' => 'text/html' ,
'charSet' => self :: $displayCharset ,
);
2013-11-10 21:53:51 +01:00
}
elseif ( $_structure -> getSubType () == 'plain' && $_htmlMode == 'html_only' )
{
2013-03-05 15:09:35 +01:00
$bodyPart = array (
'error' => 1 ,
'body' => lang ( " displaying plain messages is disabled " ),
'mimeType' => 'text/plain' , // make sure we do not return mimeType text/html
'charSet' => self :: $displayCharset ,
);
2013-11-10 21:53:51 +01:00
}
else
{
2013-03-05 15:09:35 +01:00
// some Servers append PropertyFile___ ; strip that here for display
2013-11-10 21:53:51 +01:00
// RB: not sure what this is: preg_replace('/PropertyFile___$/','',$this->decodeMimePart($mimePartBody, $_structure->encoding, $this->getMimePartCharset($_structure))),
$this -> fetchPartContents ( $_uid , $_structure , $_stream , $_preserveSeen );
2013-03-05 15:09:35 +01:00
$bodyPart = array (
2013-11-10 21:53:51 +01:00
'body' => $_structure -> getContents ( array (
'stream' => $_stream ,
)),
'mimeType' => $_structure -> getType () == 'text/html' ? 'text/html' : 'text/plain' ,
'charSet' => $_structure -> getCharset (),
2013-03-05 15:09:35 +01:00
);
2013-11-10 21:53:51 +01:00
/* RB : not sure this is still necessary
2013-03-05 15:09:35 +01:00
if ( $_structure -> type == 'TEXT' && $_structure -> subType == 'PLAIN' &&
is_array ( $_structure -> parameters ) && isset ( $_structure -> parameters [ 'FORMAT' ]) &&
trim ( strtolower ( $_structure -> parameters [ 'FORMAT' ])) == 'flowed'
)
{
if ( self :: $debug ) error_log ( __METHOD__ . __LINE__ . " detected TEXT/PLAIN Format:flowed -> removing leading blank (' \r \n ') per line " );
$bodyPart [ 'body' ] = str_replace ( " \r \n " , " \r \n " , $bodyPart [ 'body' ]);
}
2013-11-10 21:53:51 +01:00
*/
if ( $_structure -> getSubType () == 'calendar' )
2013-03-05 15:09:35 +01:00
{
2013-11-10 21:53:51 +01:00
$bodyPart [ 'body' ] = $this -> getEvent ( $_structure -> getContents (), $_structure -> getContentTypeParameter ( 'METHOD' ));
2013-03-05 15:09:35 +01:00
}
}
return $bodyPart ;
}
/**
2013-11-10 21:53:51 +01:00
* Return inline ical as html
*
* @ param string $ical iCal data
* @ param string $method iTip method eg . 'REPLY'
* @ return string text to display instead
*/
function getEvent ( $ical , $method = null )
{
// we get an inline CALENDAR ical/ics, we display it using the calendar notification style
$calobj = new calendar_ical ;
$calboupdate = new calendar_boupdate ;
// timezone stuff
$tz_diff = $GLOBALS [ 'egw_info' ][ 'user' ][ 'preferences' ][ 'common' ][ 'tz_offset' ] - $this -> common_prefs [ 'tz_offset' ];
// form an event out of ical
$events = $calobj -> icaltoegw ( $ical );
$event =& $events [ 0 ];
// preset the olddate
$olddate = $calboupdate -> format_date ( $event [ 'start' ] + $tz_diff );
// search egw, if we can find it
$eventid = $calobj -> find_event ( array ( 'uid' => $event [ 'uid' ]));
if (( int ) $eventid [ 0 ] > 0 )
{
// we found an event, we use the first one
$oldevent = $calobj -> read ( $eventid );
// we set the olddate, to comply with the possible merge params for the notification message
if ( $oldevent != False && $oldevent [ $eventid [ 0 ]][ 'start' ] != $event [ $eventid [ 0 ]][ 'start' ]) {
$olddate = $calboupdate -> format_date ( $oldevent [ $eventid [ 0 ]][ 'start' ] + $tz_diff );
}
// we merge the changes and the original event
$event = array_merge ( $oldevent [ $eventid [ 0 ]], $event );
// for some strange reason, the title of the old event is not replaced with the new title
// if you klick on the ics and import it into egw, so we dont show the title here.
// so if it is a mere reply, we dont use the new title (more detailed info/work needed here)
if ( $method == 'REPLY' ) $event [ 'title' ] = $oldevent [ $eventid [ 0 ]][ 'title' ];
}
// we prepare the message
$details = $calboupdate -> _get_event_details ( $event , $action , $event_arr );
$details [ 'olddate' ] = $olddate ;
//_debug_array($_structure);
list ( $subject , $info ) = $calboupdate -> get_update_message ( $event , $method != 'REPLY' );
$info = $GLOBALS [ 'egw' ] -> preferences -> parse_notify ( $info , $details );
// we set the bodyPart, we only show the event, we dont actually do anything, as we expect the user to
// click on the attached ics to update his own eventstore
$text = $subject ;
$text .= " \n " . $info ;
$text .= " \n \n " . lang ( 'Event Details follow' ) . " : \n " ;
foreach ( $event_arr as $key => $val )
{
if ( strlen ( $details [ $key ])) {
switch ( $key ){
case 'access' :
case 'priority' :
case 'link' :
break ;
default :
$text .= sprintf ( " %-20s %s \n " , $val [ 'field' ] . ':' , $details [ $key ]);
break ;
}
}
}
return $text ;
}
/**
* Get Body of message
*
* @ param int $_uid the messageuid ,
2013-03-05 15:09:35 +01:00
* @ param string $_htmlOptions , how to display a message , html , plain text , ...
2013-11-10 21:53:51 +01:00
* @ param string $_partID = null , the partID , may be omitted
* @ param Horde_Mime_Part $_structure = null if given use structure for parsing
2013-03-05 15:09:35 +01:00
* @ param boolean $_preserveSeen flag to preserve the seenflag by using body . peek
2014-01-20 14:14:18 +01:00
* @ param string $_folder folder to work on
2013-03-05 15:09:35 +01:00
* @ return array containing the message body , mimeType and charset
*/
2013-11-10 21:53:51 +01:00
function getMessageBody ( $_uid , $_htmlOptions = '' , $_partID = null , Horde_Mime_Part $_structure = null , $_preserveSeen = false , $_folder = '' )
2013-03-05 15:09:35 +01:00
{
if ( self :: $debug ) echo __METHOD__ . " $_uid , $_htmlOptions , $_partID <br> " ;
if ( $_htmlOptions != '' ) {
$this -> htmlOptions = $_htmlOptions ;
}
2013-11-10 21:53:51 +01:00
if ( empty ( $_folder ))
2013-06-07 15:58:17 +02:00
{
$_folder = $this -> sessionData [ 'mailbox' ];
}
2013-11-10 21:53:51 +01:00
if ( ! isset ( $_structure ))
2013-11-06 16:11:15 +01:00
{
2013-11-10 21:53:51 +01:00
$_structure = $this -> getStructure ( $_uid , $_partID , $_folder , $_preserveSeen );
2013-11-06 16:11:15 +01:00
}
2014-01-17 14:14:40 +01:00
if ( ! is_object ( $_structure ))
{
return array (
array (
2014-01-17 14:40:41 +01:00
'error' => 1 ,
'body' => 'Error: Could not fetch structure on mail:' . $_uid . " as $_htmlOptions " . 'for Mailprofile' . $this -> icServer -> ImapServerId . ' User:' . $GLOBALS [ 'egw_info' ][ 'user' ][ 'account_lid' ],
2014-01-17 14:14:40 +01:00
'mimeType' => 'text/plain' ,
'charSet' => self :: $displayCharset ,
)
);
}
2014-02-03 10:28:31 +01:00
if ( ! empty ( $_partID ))
{
$_structure -> contentTypeMap ();
$_structure = $_structure -> getPart ( $_partID );
//_debug_array($_structure->getMimeId()); exit;
}
2013-11-10 21:53:51 +01:00
switch ( $_structure -> getPrimaryType ())
{
case 'application' :
return array (
array (
'body' => '' ,
'mimeType' => 'text/plain' ,
'charSet' => 'iso-8859-1' ,
)
);
case 'multipart' :
switch ( $_structure -> getSubType ())
{
case 'alternative' :
$bodyParts = array ( $this -> getMultipartAlternative ( $_uid , $_structure , $this -> htmlOptions , $_preserveSeen ));
break ;
case 'nil' : // multipart with no Alternative
case 'mixed' :
case 'report' :
case 'signed' :
$bodyParts = $this -> getMultipartMixed ( $_uid , $_structure , $this -> htmlOptions , $_preserveSeen );
break ;
case 'related' :
$bodyParts = $this -> getMultipartRelated ( $_uid , $_structure , $this -> htmlOptions , $_preserveSeen );
break ;
}
return self :: normalizeBodyParts ( $bodyParts );
case 'video' :
case 'audio' : // some servers send audiofiles and imagesfiles directly, without any stuff surround it
case 'image' : // they are displayed as Attachment NOT INLINE
2013-03-05 15:09:35 +01:00
return array (
array (
'body' => '' ,
2014-02-03 10:28:31 +01:00
'mimeType' => $_structure -> getSubType (),
2013-03-05 15:09:35 +01:00
),
);
2013-11-10 21:53:51 +01:00
case 'text' :
2013-03-05 15:09:35 +01:00
$bodyPart = array ();
2013-11-10 21:53:51 +01:00
if ( $_structure -> getDisposition () != 'attachment' )
{
switch ( $_structure -> getSubType ())
{
case 'calendar' :
2013-03-05 15:09:35 +01:00
// this is handeled in getTextPart
2013-11-10 21:53:51 +01:00
case 'html' :
case 'plain' :
2013-03-05 15:09:35 +01:00
default :
2013-11-10 21:53:51 +01:00
$bodyPart = array ( $this -> getTextPart ( $_uid , $_structure , $this -> htmlOptions , $_preserveSeen ));
2013-03-05 15:09:35 +01:00
}
} else {
// what if the structure->disposition is attachment ,...
}
return self :: normalizeBodyParts ( $bodyPart );
2013-11-10 21:53:51 +01:00
case 'attachment' :
case 'message' :
switch ( $_structure -> getSubType ())
{
case 'rfc822' :
$newStructure = $_structure -> getParts ();
2014-02-03 10:28:31 +01:00
if ( self :: $debug ) { echo __METHOD__ . " Message -> RFC -> NewStructure: " ; _debug_array ( $newStructure [ 0 ]);}
return self :: normalizeBodyParts ( $this -> getMessageBody ( $_uid , $_htmlOptions , $newStructure [ 0 ] -> getMimeId (), $newStructure [ 0 ], $_preserveSeen , $_folder ));
2013-03-05 15:09:35 +01:00
}
break ;
2013-11-10 21:53:51 +01:00
2013-03-05 15:09:35 +01:00
default :
2013-11-10 21:53:51 +01:00
if ( self :: $debug ) _debug_array ( $_structure );
2013-03-05 15:09:35 +01:00
return array (
array (
2013-11-10 21:53:51 +01:00
'body' => lang ( 'The mimeparser can not parse this message.' ) . $_structure -> getType (),
2013-03-05 15:09:35 +01:00
'mimeType' => 'text/plain' ,
2013-11-10 21:53:51 +01:00
'charSet' => self :: $displayCharset ,
2013-03-05 15:09:35 +01:00
)
);
}
}
/**
* normalizeBodyParts - function to gather and normalize all body Information
* @ param _bodyParts - Body Array
* @ return array - a normalized Bodyarray
*/
static function normalizeBodyParts ( $_bodyParts )
{
2013-11-10 21:53:51 +01:00
// RB: dont think this is still necessary
return $_bodyParts ;
2013-03-05 15:09:35 +01:00
if ( is_array ( $_bodyParts ))
{
foreach ( $_bodyParts as $singleBodyPart )
{
if ( ! isset ( $singleBodyPart [ 'body' ])) {
$buff = self :: normalizeBodyParts ( $singleBodyPart );
foreach (( array ) $buff as $val ) $body2return [] = $val ;
continue ;
}
$body2return [] = $singleBodyPart ;
}
}
else
{
$body2return = $_bodyParts ;
}
return $body2return ;
}
/**
* getdisplayableBody - creates the bodypart of the email as textual representation
* @ param object $mailClass the mailClass object to be used
* @ param array $bodyParts with the bodyparts
* @ return string a preformatted string with the mails converted to text
*/
static function & getdisplayableBody ( & $mailClass , $bodyParts , $preserveHTML = false )
{
for ( $i = 0 ; $i < count ( $bodyParts ); $i ++ )
{
if ( ! isset ( $bodyParts [ $i ][ 'body' ])) {
$bodyParts [ $i ][ 'body' ] = self :: getdisplayableBody ( $mailClass , $bodyParts [ $i ], $preserveHTML );
$message .= empty ( $bodyParts [ $i ][ 'body' ]) ? '' : $bodyParts [ $i ][ 'body' ];
continue ;
}
if ( isset ( $bodyParts [ $i ][ 'error' ])) continue ;
if ( empty ( $bodyParts [ $i ][ 'body' ])) continue ;
// some characterreplacements, as they fail to translate
$sar = array (
'@(\x84|\x93|\x94)@' ,
'@(\x96|\x97|\x1a)@' ,
'@(\x82|\x91|\x92)@' ,
'@(\x85)@' ,
'@(\x86)@' ,
'@(\x99)@' ,
'@(\xae)@' ,
);
$rar = array (
'"' ,
'-' ,
'\'' ,
'...' ,
'&' ,
'(TM)' ,
'(R)' ,
);
if (( $bodyParts [ $i ][ 'mimeType' ] == 'text/html' || $bodyParts [ $i ][ 'mimeType' ] == 'text/plain' ) &&
strtoupper ( $bodyParts [ $i ][ 'charSet' ]) != 'UTF-8' )
{
$bodyParts [ $i ][ 'body' ] = preg_replace ( $sar , $rar , $bodyParts [ $i ][ 'body' ]);
}
if ( $bodyParts [ $i ][ 'charSet' ] === false ) $bodyParts [ $i ][ 'charSet' ] = translation :: detect_encoding ( $bodyParts [ $i ][ 'body' ]);
// add line breaks to $bodyParts
//error_log(__METHOD__.__LINE__.' Charset:'.$bodyParts[$i]['charSet'].'->'.$bodyParts[$i]['body']);
$newBody = translation :: convert ( $bodyParts [ $i ][ 'body' ], $bodyParts [ $i ][ 'charSet' ]);
//error_log(__METHOD__.__LINE__.' MimeType:'.$bodyParts[$i]['mimeType'].'->'.$newBody);
/*
// in a way, this tests if we are having real utf-8 (the displayCharset) by now; we should if charsets reported (or detected) are correct
if ( strtoupper ( self :: $displayCharset ) == 'UTF-8' )
{
$test = json_encode ( $newBody );
//error_log(__METHOD__.__LINE__.'#'.$test.'# ->'.strlen($newBody).' Error:'.json_last_error());
if ( json_last_error () != JSON_ERROR_NONE && strlen ( $newBody ) > 0 )
{
// this should not be needed, unless something fails with charset detection/ wrong charset passed
error_log ( __METHOD__ . __LINE__ . ' Charset Reported:' . $bodyParts [ $i ][ 'charSet' ] . ' Carset Detected:' . translation :: detect_encoding ( $bodyParts [ $i ][ 'body' ]));
$newBody = utf8_encode ( $newBody );
}
}
*/
//error_log(__METHOD__.__LINE__.' before purify:'.$newBody);
$mailClass -> activeMimeType = 'text/plain' ;
if ( $bodyParts [ $i ][ 'mimeType' ] == 'text/html' ) {
$mailClass -> activeMimeType = $bodyParts [ $i ][ 'mimeType' ];
// as translation::convert reduces \r\n to \n and purifier eats \n -> peplace it with a single space
$newBody = str_replace ( " \n " , " " , $newBody );
// convert HTML to text, as we dont want HTML in infologs
if ( extension_loaded ( 'tidy' ))
{
$tidy = new tidy ();
$cleaned = $tidy -> repairString ( $newBody , self :: $tidy_config , 'utf8' );
// Found errors. Strip it all so there's some output
if ( $tidy -> getStatus () == 2 )
{
error_log ( __METHOD__ . __LINE__ . ' ->' . $tidy -> errorBuffer );
}
else
{
$newBody = $cleaned ;
}
if ( ! $preserveHTML )
{
// filter only the 'body', as we only want that part, if we throw away the html
preg_match ( '`(<htm.+?<body[^>]*>)(.+?)(</body>.*?</html>)`ims' , $newBody , $matches );
if ( $matches [ 2 ])
{
$hasOther = true ;
$newBody = $matches [ 2 ];
}
}
}
else
{
// htmLawed filter only the 'body'
preg_match ( '`(<htm.+?<body[^>]*>)(.+?)(</body>.*?</html>)`ims' , $newBody , $matches );
if ( $matches [ 2 ])
{
$hasOther = true ;
$newBody = $matches [ 2 ];
}
$htmLawed = new egw_htmLawed ();
2013-10-10 13:44:01 +02:00
// the next line should not be needed, but produces better results on HTML 2 Text conversion,
// as we switched off HTMLaweds tidy functionality
2013-03-05 15:09:35 +01:00
$newBody = str_replace ( array ( '&amp;' , '<DIV><BR></DIV>' , " <DIV> </DIV> " , '<div> </div>' ), array ( '&' , '<BR>' , '<BR>' , '<BR>' ), $newBody );
$newBody = $htmLawed -> egw_htmLawed ( $newBody );
if ( $hasOther && $preserveHTML ) $newBody = $matches [ 1 ] . $newBody . $matches [ 3 ];
}
//error_log(__METHOD__.__LINE__.' after purify:'.$newBody);
2013-10-05 11:03:51 +02:00
if ( $preserveHTML == false ) $newBody = translation :: convertHTMLToText ( $newBody , self :: $displayCharset , true , $stripalltags = true );
2013-03-05 15:09:35 +01:00
//error_log(__METHOD__.__LINE__.' after convertHTMLToText:'.$newBody);
if ( $preserveHTML == false ) $newBody = nl2br ( $newBody ); // we need this, as htmLawed removes \r\n
$mailClass -> getCleanHTML ( $newBody , false , $preserveHTML ); // remove stuff we regard as unwanted
if ( $preserveHTML == false ) $newBody = str_replace ( " <br /> " , " \r \n " , $newBody );
//error_log(__METHOD__.__LINE__.' after getClean:'.$newBody);
$message .= $newBody ;
continue ;
}
$newBody = self :: htmlspecialchars ( $newBody );
//error_log(__METHOD__.__LINE__.' Body(after specialchars):'.$newBody);
$newBody = strip_tags ( $newBody ); //we need to fix broken tags (or just stuff like "<800 USD/p" )
//error_log(__METHOD__.__LINE__.' Body(after strip tags):'.$newBody);
$newBody = htmlspecialchars_decode ( $newBody , ENT_QUOTES );
//error_log(__METHOD__.__LINE__.' Body (after hmlspc_decode):'.$newBody);
$message .= $newBody ;
//continue;
}
return $message ;
}
2013-04-12 15:30:54 +02:00
static function wordwrap ( $str , $cols , $cut , $dontbreaklinesstartingwith = false )
{
$lines = explode ( " \n " , $str );
$newStr = '' ;
foreach ( $lines as $line )
{
// replace tabs by 8 space chars, or any tab only counts one char
//$line = str_replace("\t"," ",$line);
//$newStr .= wordwrap($line, $cols, $cut);
$allowedLength = $cols - strlen ( $cut );
if ( strlen ( $line ) > $allowedLength &&
( $dontbreaklinesstartingwith == false ||
( $dontbreaklinesstartingwith &&
strlen ( $dontbreaklinesstartingwith ) >= 1 &&
substr ( $line , 0 , strlen ( $dontbreaklinesstartingwith )) != $dontbreaklinesstartingwith
)
)
)
{
$s = explode ( " " , $line );
$line = " " ;
$linecnt = 0 ;
foreach ( $s as $k => $v ) {
$cnt = strlen ( $v );
// only break long words within the wordboundaries,
// but it may destroy links, so we check for href and dont do it if we find one
// we check for any html within the word, because we do not want to break html by accident
if ( $cnt > $allowedLength && stripos ( $v , 'href=' ) === false && stripos ( $v , 'onclick=' ) === false && $cnt == strlen ( html_entity_decode ( $v )))
{
$v = wordwrap ( $v , $allowedLength , $cut , true );
}
// the rest should be broken at the start of the new word that exceeds the limit
if ( $linecnt + $cnt > $allowedLength ) {
$v = $cut . $v ;
#$linecnt = 0;
$linecnt = strlen ( $v ) - strlen ( $cut );
} else {
$linecnt += $cnt ;
}
if ( strlen ( $v )) $line .= ( strlen ( $line ) ? " " : " " ) . $v ;
}
}
$newStr .= $line . " \n " ;
}
return $newStr ;
}
2013-02-28 10:28:08 +01:00
/**
2013-06-05 16:24:56 +02:00
* getMessageEnvelope
* get parsed headers from message
* @ param string / int $_uid the messageuid ,
* @ param string / int $_partID = '' , the partID , may be omitted
* @ param boolean $decode flag to do the decoding on the fly
2014-01-20 09:55:07 +01:00
* @ param string $_folder folder to work on
2013-06-05 16:24:56 +02:00
* @ return array the message header
*/
2014-01-20 09:55:07 +01:00
function getMessageEnvelope ( $_uid , $_partID = '' , $decode = false , $_folder = '' )
2013-06-05 16:24:56 +02:00
{
2014-01-20 09:55:07 +01:00
//error_log(__METHOD__.__LINE__.":$_uid,$_partID,$decode,$_folder".function_backtrace());
if ( empty ( $_folder )) $_folder = ( $this -> sessionData [ 'mailbox' ] ? $this -> sessionData [ 'mailbox' ] : $this -> icServer -> getCurrentMailbox ());
//error_log(__METHOD__.__LINE__.":$_uid,$_partID,$decode,$_folder");
2014-01-31 11:21:58 +01:00
if ( empty ( $_partID ) || $_partID == 'null' ) {
2013-11-06 16:11:15 +01:00
$uidsToFetch = new Horde_Imap_Client_Ids ();
2014-01-23 12:56:34 +01:00
if ( ! ( is_object ( $_uid ) || is_array ( $_uid ))) $_uid = ( array ) $_uid ;
$uidsToFetch -> add ( $_uid );
2013-11-06 16:11:15 +01:00
$fquery = new Horde_Imap_Client_Fetch_Query ();
$envFields = new Horde_Mime_Headers ();
$fquery -> envelope ();
$headersNew = $this -> icServer -> fetch ( $_folder , $fquery , array (
'ids' => $uidsToFetch ,
));
if ( is_object ( $headersNew )) {
foreach ( $headersNew as $id => $_headerObject ) {
$env = $_headerObject -> getEnvelope ();
//_debug_array($envFields->singleFields());
foreach ( $envFields -> singleFields () as $e => $v )
{
switch ( $v )
{
case 'to' :
case 'from' :
case 'cc' :
case 'bcc' :
case 'sender' :
2013-11-27 16:40:00 +01:00
//error_log(__METHOD__.__LINE__.array2string($env->$v->addresses));
2013-11-06 16:11:15 +01:00
$envelope [ $v ] = $env -> $v -> addresses ;
2013-11-27 16:40:00 +01:00
$address = array ();
foreach ( $envelope [ $v ] as $k => $ad )
{
if ( stripos ( $ad , '@' ) === false )
{
$remember = $k ;
}
else
{
$address [] = ( ! is_null ( $remember ) ? $envelope [ $v ][ $remember ] . ' ' : '' ) . $ad ;
$remember = null ;
}
}
$envelope [ $v ] = $address ;
2013-11-06 16:11:15 +01:00
break ;
case 'date' :
2013-11-20 16:05:25 +01:00
$envelope [ $v ] = egw_time :: to ( $env -> $v );
2013-11-06 16:11:15 +01:00
break ;
default :
$envelope [ $v ] = $env -> $v ;
}
}
}
2013-06-05 16:24:56 +02:00
}
2013-11-06 16:11:15 +01:00
$envelope = array_change_key_case ( $envelope , CASE_UPPER );
//if ($decode) _debug_array($envelope);
2013-10-08 16:23:46 +02:00
//error_log(__METHOD__.__LINE__.array2string($envelope));
2013-11-06 16:11:15 +01:00
return ( $decode ? self :: decode_header ( $envelope , true ) : $envelope );
2013-06-05 16:24:56 +02:00
} else {
2014-01-20 09:55:07 +01:00
$headers = $this -> getMessageHeader ( $_uid , $_partID , true , true , $_folder );
2014-02-03 10:28:31 +01:00
//error_log(__METHOD__.__LINE__.':'.array2string($headers));
2013-06-05 16:24:56 +02:00
//_debug_array($headers);
$newData = array (
'DATE' => $headers [ 'DATE' ],
'SUBJECT' => ( $decode ? self :: decode_header ( $headers [ 'SUBJECT' ]) : $headers [ 'SUBJECT' ]),
'MESSAGE_ID' => $headers [ 'MESSAGE-ID' ]
);
//_debug_array($newData);
$recepientList = array ( 'FROM' , 'TO' , 'CC' , 'BCC' , 'SENDER' , 'REPLY_TO' );
foreach ( $recepientList as $recepientType ) {
if ( isset ( $headers [ $recepientType ])) {
if ( $decode ) $headers [ $recepientType ] = self :: decode_header ( $headers [ $recepientType ], true );
$addresses = imap_rfc822_parse_adrlist ( $headers [ $recepientType ], '' );
foreach ( $addresses as $singleAddress ) {
$addressData = array (
'PERSONAL_NAME' => $singleAddress -> personal ? $singleAddress -> personal : 'NIL' ,
'AT_DOMAIN_LIST' => $singleAddress -> adl ? $singleAddress -> adl : 'NIL' ,
'MAILBOX_NAME' => $singleAddress -> mailbox ? $singleAddress -> mailbox : 'NIL' ,
'HOST_NAME' => $singleAddress -> host ? $singleAddress -> host : 'NIL' ,
'EMAIL' => $singleAddress -> host ? $singleAddress -> mailbox . '@' . $singleAddress -> host : $singleAddress -> mailbox ,
);
if ( $addressData [ 'PERSONAL_NAME' ] != 'NIL' ) {
$addressData [ 'RFC822_EMAIL' ] = imap_rfc822_write_address ( $singleAddress -> mailbox , $singleAddress -> host , $singleAddress -> personal );
} else {
$addressData [ 'RFC822_EMAIL' ] = 'NIL' ;
}
2014-02-03 10:28:31 +01:00
$newData [ $recepientType ][] = ( $addressData [ 'RFC822_EMAIL' ] != 'NIL' ? $addressData [ 'RFC822_EMAIL' ] : $addressData [ 'EMAIL' ]); //$addressData;
2013-06-05 16:24:56 +02:00
}
} else {
if ( $recepientType == 'SENDER' || $recepientType == 'REPLY_TO' ) {
$newData [ $recepientType ] = $newData [ 'FROM' ];
} else {
$newData [ $recepientType ] = array ();
}
}
}
//if ($decode) _debug_array($newData);
return $newData ;
}
}
/**
* getMessageHeader
2013-02-28 10:28:08 +01:00
* get parsed headers from message
* @ param string / int $_uid the messageuid ,
* @ param string / int $_partID = '' , the partID , may be omitted
* @ param boolean $decode flag to do the decoding on the fly
2013-11-06 16:11:15 +01:00
* @ param boolean $preserveUnSeen flag to preserve the seen flag where applicable
2014-01-20 09:55:07 +01:00
* @ param string $_folder folder to work on
2013-06-05 16:24:56 +02:00
* @ return array the message header
2013-02-28 10:28:08 +01:00
*/
2014-01-18 14:48:34 +01:00
function getMessageHeader ( $_uid , $_partID = '' , $decode = false , $preserveUnSeen = false , $_folder = '' )
2013-02-28 10:28:08 +01:00
{
2014-01-20 09:55:07 +01:00
//error_log(__METHOD__.__LINE__.':'.$_uid.', '.$_partID.', '.$decode.', '.$preserveUnSeen.', '.$_folder);
2014-01-18 14:48:34 +01:00
if ( empty ( $_folder )) $_folder = ( $this -> sessionData [ 'mailbox' ] ? $this -> sessionData [ 'mailbox' ] : $this -> icServer -> getCurrentMailbox ());
2013-11-06 16:11:15 +01:00
$uidsToFetch = new Horde_Imap_Client_Ids ();
2014-01-23 12:56:34 +01:00
if ( ! ( is_object ( $_uid ) || is_array ( $_uid ))) $_uid = ( array ) $_uid ;
$uidsToFetch -> add ( $_uid );
2013-11-06 16:11:15 +01:00
$fquery = new Horde_Imap_Client_Fetch_Query ();
2014-02-03 10:28:31 +01:00
if ( $_partID != '' )
{
$fquery -> headerText ( array ( 'id' => $_partID , 'peek' => $preserveUnSeen ));
$fquery -> structure ();
}
else
{
$fquery -> headerText ( array ( 'peek' => $preserveUnSeen ));
}
2013-11-06 16:11:15 +01:00
$headersNew = $this -> icServer -> fetch ( $_folder , $fquery , array (
'ids' => $uidsToFetch ,
));
if ( is_object ( $headersNew )) {
foreach ( $headersNew as $id => $_headerObject ) {
$retValue = $_headerObject -> getHeaderText ( 0 , Horde_Imap_Client_Data_Fetch :: HEADER_PARSE ) -> toArray ();
if ( $_partID != '' )
{
$mailStructureObject = $_headerObject -> getStructure ();
foreach ( $mailStructureObject -> contentTypeMap () as $mime_id => $mime_type )
{
if ( $mime_id == $_partID )
{
2014-02-03 10:28:31 +01:00
//error_log(__METHOD__.__LINE__."$mime_id == $_partID".array2string($_headerObject->getHeaderText($mime_id,Horde_Imap_Client_Data_Fetch::HEADER_PARSE)->toArray()));
2013-11-06 16:11:15 +01:00
$retValue = $_headerObject -> getHeaderText ( $mime_id , Horde_Imap_Client_Data_Fetch :: HEADER_PARSE ) -> toArray ();
2014-02-03 10:28:31 +01:00
break ;
2013-11-06 16:11:15 +01:00
}
}
}
}
2013-02-28 10:28:08 +01:00
}
2013-11-06 16:11:15 +01:00
$retValue = array_change_key_case ( $retValue , CASE_UPPER );
2014-01-15 11:31:08 +01:00
//error_log(__METHOD__.__LINE__.array2string($retValue));
2013-08-21 12:47:20 +02:00
// if SUBJECT is an array, use thelast one, as we assume something with the unfolding for the subject did not work
if ( is_array ( $retValue [ 'SUBJECT' ]))
{
$retValue [ 'SUBJECT' ] = $retValue [ 'SUBJECT' ][ count ( $retValue [ 'SUBJECT' ]) - 1 ];
}
2014-01-20 09:55:07 +01:00
//error_log(__METHOD__.__LINE__.':'.array2string($decode ? self::decode_header($retValue,true):$retValue));
2013-02-28 10:28:08 +01:00
return ( $decode ? self :: decode_header ( $retValue , true ) : $retValue );
}
/**
* getMessageRawHeader
* get messages raw header data
* @ param string / int $_uid the messageuid ,
* @ param string / int $_partID = '' , the partID , may be omitted
2014-01-20 09:55:07 +01:00
* @ param string $_folder folder to work on
2013-02-28 10:28:08 +01:00
* @ return string the message header
*/
2014-01-20 09:55:07 +01:00
function getMessageRawHeader ( $_uid , $_partID = '' , $_folder = '' )
2013-02-28 10:28:08 +01:00
{
static $rawHeaders ;
2014-01-20 09:55:07 +01:00
if ( empty ( $_folder )) $_folder = ( $this -> sessionData [ 'mailbox' ] ? $this -> sessionData [ 'mailbox' ] : $this -> icServer -> getCurrentMailbox ());
2013-02-28 10:28:08 +01:00
//error_log(__METHOD__.__LINE__." Try Using Cache for raw Header $_uid, $_partID in Folder $_folder");
if ( is_null ( $rawHeaders )) $rawHeaders = egw_cache :: getCache ( egw_cache :: INSTANCE , 'email' , 'rawHeadersCache' . trim ( $GLOBALS [ 'egw_info' ][ 'user' ][ 'account_id' ]), $callback = null , $callback_params = array (), $expiration = 60 * 60 * 1 );
2014-01-20 09:55:07 +01:00
if ( isset ( $rawHeaders [ $this -> icServer -> ImapServerId ][ $_folder ][ $_uid ][( empty ( $_partID ) ? 'NIL' : $_partID )]))
2013-02-28 10:28:08 +01:00
{
//error_log(__METHOD__.__LINE__." Using Cache for raw Header $_uid, $_partID in Folder $_folder");
2014-01-20 09:55:07 +01:00
return $rawHeaders [ $this -> icServer -> ImapServerId ][ $_folder ][ $_uid ][( empty ( $_partID ) ? 'NIL' : $_partID )];
2013-02-28 10:28:08 +01:00
}
2013-11-06 16:11:15 +01:00
$uidsToFetch = new Horde_Imap_Client_Ids ();
2014-01-30 16:35:20 +01:00
$uid = $_uid ;
if ( ! ( is_object ( $_uid ) || is_array ( $_uid ))) $uid = ( array ) $_uid ;
$uidsToFetch -> add ( $uid );
2013-02-28 10:28:08 +01:00
2013-11-06 16:11:15 +01:00
$fquery = new Horde_Imap_Client_Fetch_Query ();
2014-02-03 10:28:31 +01:00
if ( $_partID != '' )
{
$fquery -> headerText ( array ( 'id' => $_partID , 'peek' => true ));
$fquery -> structure ();
}
else
{
$fquery -> headerText ( array ( 'peek' => true ));
}
2013-11-06 16:11:15 +01:00
$headersNew = $this -> icServer -> fetch ( $_folder , $fquery , array (
'ids' => $uidsToFetch ,
));
if ( is_object ( $headersNew )) {
foreach ( $headersNew as $id => $_headerObject ) {
$retValue = $_headerObject -> getHeaderText ();
if ( $_partID != '' )
{
$mailStructureObject = $_headerObject -> getStructure ();
foreach ( $mailStructureObject -> contentTypeMap () as $mime_id => $mime_type )
{
if ( $mime_id == $_partID )
{
$retValue = $_headerObject -> getHeaderText ( $mime_id );
}
}
}
}
2013-02-28 10:28:08 +01:00
}
2014-01-20 09:55:07 +01:00
$rawHeaders [ $this -> icServer -> ImapServerId ][ $_folder ][ $_uid ][( empty ( $_partID ) ? 'NIL' : $_partID )] = $retValue ;
2013-02-28 10:28:08 +01:00
egw_cache :: setCache ( egw_cache :: INSTANCE , 'email' , 'rawHeadersCache' . trim ( $GLOBALS [ 'egw_info' ][ 'user' ][ 'account_id' ]), $rawHeaders , $expiration = 60 * 60 * 1 );
return $retValue ;
}
2013-04-12 15:10:27 +02:00
/**
* getStyles - extracts the styles from the given bodyparts
* @ param array $bodyParts with the bodyparts
* @ return string a preformatted string with the mails converted to text
*/
static function & getStyles ( $_bodyParts )
{
$style = '' ;
if ( empty ( $_bodyParts )) return " " ;
foreach (( array ) $_bodyParts as $singleBodyPart ) {
if ( ! isset ( $singleBodyPart [ 'body' ])) {
$singleBodyPart [ 'body' ] = self :: getStyles ( $singleBodyPart );
$style .= $singleBodyPart [ 'body' ];
continue ;
}
if ( $singleBodyPart [ 'charSet' ] === false ) $singleBodyPart [ 'charSet' ] = translation :: detect_encoding ( $singleBodyPart [ 'body' ]);
$singleBodyPart [ 'body' ] = translation :: convert (
$singleBodyPart [ 'body' ],
strtolower ( $singleBodyPart [ 'charSet' ])
);
$ct = 0 ;
if ( stripos ( $singleBodyPart [ 'body' ], '<style' ) !== false ) $ct = preg_match_all ( '#<style(?:\s.*)?>(.+)</style>#isU' , $singleBodyPart [ 'body' ], $newStyle );
if ( $ct > 0 )
{
//error_log(__METHOD__.__LINE__.array2string($newStyle[0]));
$style2buffer = implode ( '' , $newStyle [ 0 ]);
}
if ( $style2buffer && strtoupper ( self :: $displayCharset ) == 'UTF-8' )
{
//error_log(__METHOD__.__LINE__.array2string($style2buffer));
$test = json_encode ( $style2buffer );
//error_log(__METHOD__.__LINE__.'#'.$test.'# ->'.strlen($style2buffer).' Error:'.json_last_error());
//if (json_last_error() != JSON_ERROR_NONE && strlen($style2buffer)>0)
if ( $test == " null " && strlen ( $style2buffer ) > 0 )
{
// this should not be needed, unless something fails with charset detection/ wrong charset passed
error_log ( __METHOD__ . __LINE__ . ' Found Invalid sequence for utf-8 in CSS:' . $style2buffer . ' Charset Reported:' . $singleBodyPart [ 'charSet' ] . ' Carset Detected:' . translation :: detect_encoding ( $style2buffer ));
$style2buffer = utf8_encode ( $style2buffer );
}
}
$style .= $style2buffer ;
}
// clean out comments and stuff
$search = array (
'@url\(http:\/\/[^\)].*?\)@si' , // url calls e.g. in style definitions
// '@<!--[\s\S]*?[ \t\n\r]*-->@', // Strip multi-line comments including CDATA
// '@<!--[\s\S]*?[ \t\n\r]*--@', // Strip broken multi-line comments including CDATA
);
$style = preg_replace ( $search , " " , $style );
// CSS Security
// http://code.google.com/p/browsersec/wiki/Part1#Cascading_stylesheets
$css = preg_replace ( '/(javascript|expession|-moz-binding)/i' , '' , $style );
if ( stripos ( $css , 'script' ) !== false ) translation :: replaceTagsCompletley ( $css , 'script' ); // Strip out script that may be included
// we need this, as styledefinitions are enclosed with curly brackets; and template stuff tries to replace everything between curly brackets that is having no horizontal whitespace
// as the comments as <!-- styledefinition --> in stylesheet are outdated, and ck-editor does not understand it, we remove it
$css = str_replace ( array ( ':' , '<!--' , '-->' ), array ( ': ' , '' , '' ), $css );
//error_log(__METHOD__.__LINE__.$css);
// TODO: we may have to strip urls and maybe comments and ifs
return $css ;
}
2013-02-28 10:28:08 +01:00
/**
* getMessageRawBody
* get the message raw body
* @ param string / int $_uid the messageuid ,
* @ param string / int $_partID = '' , the partID , may be omitted
2014-01-20 14:14:18 +01:00
* @ param string $_folder folder to work on
2013-02-28 10:28:08 +01:00
* @ return string the message body
*/
2014-01-20 14:14:18 +01:00
function getMessageRawBody ( $_uid , $_partID = '' , $_folder = '' )
2013-02-28 10:28:08 +01:00
{
//TODO: caching einbauen static!
static $rawBody ;
2014-01-16 16:22:18 +01:00
if ( is_null ( $rawBody )) $rawBody = array ();
2014-01-20 14:14:18 +01:00
if ( empty ( $_folder )) $_folder = ( $this -> sessionData [ 'mailbox' ] ? $this -> sessionData [ 'mailbox' ] : $this -> icServer -> getCurrentMailbox ());
2014-01-20 09:55:07 +01:00
if ( isset ( $rawBody [ $this -> icServer -> ImapServerId ][ $_folder ][ $_uid ][( empty ( $_partID ) ? 'NIL' : $_partID )]))
2013-02-28 10:28:08 +01:00
{
//error_log(__METHOD__.__LINE__." Using Cache for raw Body $_uid, $_partID in Folder $_folder");
2014-01-20 09:55:07 +01:00
return $rawBody [ $this -> icServer -> ImapServerId ][ $_folder ][ $_uid ][( empty ( $_partID ) ? 'NIL' : $_partID )];
2013-02-28 10:28:08 +01:00
}
2013-11-06 16:11:15 +01:00
$uidsToFetch = new Horde_Imap_Client_Ids ();
2014-01-30 16:35:20 +01:00
$uid = $_uid ;
if ( ! ( is_object ( $_uid ) || is_array ( $_uid ))) $uid = ( array ) $_uid ;
$uidsToFetch -> add ( $uid );
2013-12-18 16:14:12 +01:00
$fquery = new Horde_Imap_Client_Fetch_Query ();
2014-01-15 09:45:05 +01:00
$fquery -> fullText ( array ( 'peek' => true ));
2013-12-18 16:14:12 +01:00
if ( $_partID != '' )
{
$fquery -> structure ();
2014-01-15 09:45:05 +01:00
$fquery -> bodyPart ( $_partID , array ( 'peek' => true ));
2013-12-18 16:14:12 +01:00
}
$headersNew = $this -> icServer -> fetch ( $_folder , $fquery , array (
'ids' => $uidsToFetch ,
));
if ( is_object ( $headersNew )) {
foreach ( $headersNew as $id => $_headerObject ) {
$body = $_headerObject -> getFullMsg ();
if ( $_partID != '' )
{
$mailStructureObject = $_headerObject -> getStructure ();
//_debug_array($mailStructureObject->contentTypeMap());
foreach ( $mailStructureObject -> contentTypeMap () as $mime_id => $mime_type )
{
if ( $mime_id == $_partID )
{
$body = $_headerObject -> getBodyPart ( $mime_id );
}
}
}
}
}
2014-02-10 14:23:32 +01:00
//error_log(__METHOD__.__LINE__."[$this->icServer->ImapServerId][$_folder][$_uid][".(empty($_partID)?'NIL':$_partID)."]");
2014-01-20 09:55:07 +01:00
$rawBody [ $this -> icServer -> ImapServerId ][ $_folder ][ $_uid ][( empty ( $_partID ) ? 'NIL' : $_partID )] = $body ;
2013-02-28 10:28:08 +01:00
return $body ;
}
2013-02-19 17:30:59 +01:00
/**
2013-11-10 21:53:51 +01:00
* Get structure of a mail or part of a mail
*
* @ param int $_uid
* @ param string $_partID = null
* @ param string $_folder = null
* @ param boolean $_preserveSeen = false flag to preserve the seenflag by using body . peek
* @ param Horde_Imap_Client_Fetch_Query $fquery = null default query just structure
* @ return Horde_Mime_Part
*/
function getStructure ( $_uid , $_partID = null , $_folder = null , $_preserveSeen = false )
{
2013-11-19 09:17:49 +01:00
if ( self :: $debug ) error_log ( __METHOD__ . __LINE__ . " : $_uid , $_partID " );
2013-11-10 21:53:51 +01:00
if ( empty ( $_folder ))
{
$_folder = ( $this -> sessionData [ 'mailbox' ] ? $this -> sessionData [ 'mailbox' ] : $this -> icServer -> getCurrentMailbox ());
}
$uidsToFetch = new Horde_Imap_Client_Ids ();
2014-01-23 12:56:34 +01:00
if ( ! ( is_object ( $_uid ) || is_array ( $_uid ))) $_uid = ( array ) $_uid ;
$uidsToFetch -> add ( $_uid );
2014-01-17 14:05:23 +01:00
try
{
$_fquery = new Horde_Imap_Client_Fetch_Query ();
// not sure why Klaus add these, seem not necessary
// $fquery->envelope();
// $fquery->size();
$_fquery -> structure ();
if ( $_partID ) $_fquery -> bodyPart ( $_partID , array ( 'peek' => $_preserveSeen ));
$mail = $this -> icServer -> fetch ( $_folder , $_fquery , array (
'ids' => $uidsToFetch ,
)) -> first ();
2013-11-10 21:53:51 +01:00
2014-01-17 14:05:23 +01:00
return $mail -> getStructure ();
}
catch ( Exception $e )
{
2014-01-18 14:11:45 +01:00
error_log ( __METHOD__ . __LINE__ . ' Could not fetch structure on mail:' . $_uid . ' Serverprofile->' . $this -> icServer -> ImapServerId . ' Message:' . $e -> getMessage () . ' Stack:' . function_backtrace ());
2014-01-17 14:05:23 +01:00
return null ;
}
2013-11-10 21:53:51 +01:00
}
/**
* Parse the structure for attachments
*
* Returns not the attachments itself , but an array of information about the attachment
*
* @ param int $_uid the messageuid ,
* @ param string $_partID = null , the partID , may be omitted
* @ param Horde_Mime_Part $_structure = null if given use structure for parsing
2013-02-19 17:30:59 +01:00
* @ param boolean $fetchEmbeddedImages = true ,
* @ param boolean $fetchTextCalendar = false ,
* @ param boolean $resolveTNEF = true
2014-01-20 17:03:32 +01:00
* @ param string $_folder folder to work on
2013-02-19 17:30:59 +01:00
* @ return array an array of information about the attachment : array of array ( name , size , mimeType , partID , encoding )
*/
2014-01-20 17:03:32 +01:00
function getMessageAttachments ( $_uid , $_partID = null , Horde_Mime_Part $_structure = null , $fetchEmbeddedImages = true , $fetchTextCalendar = false , $resolveTNEF = true , $_folder = '' )
2013-02-19 17:30:59 +01:00
{
2013-02-20 12:31:57 +01:00
if ( self :: $debug ) error_log ( __METHOD__ . " : $_uid , $_partID " );
2014-01-20 17:03:32 +01:00
if ( empty ( $_folder )) $_folder = ( $this -> sessionData [ 'mailbox' ] ? $this -> sessionData [ 'mailbox' ] : $this -> icServer -> getCurrentMailbox ());
2013-02-19 17:30:59 +01:00
2013-11-10 21:53:51 +01:00
if ( ! isset ( $_structure ))
{
2014-01-20 17:03:32 +01:00
$_structure = $this -> getStructure ( $_uid , $_partID , $_folder , true );
2014-02-03 10:28:31 +01:00
//error_log(__METHOD__.__LINE__.':'.print_r($_structure->contentTypeMap(),true));
2013-11-10 21:53:51 +01:00
}
2014-01-14 16:21:54 +01:00
if ( ! $_structure || ! $_structure -> contentTypeMap ()) return array ();
2014-02-03 10:28:31 +01:00
if ( ! empty ( $_partID )) $_structure = $_structure -> getPart ( $_partID );
2014-02-13 13:59:43 +01:00
$skipParts = array ();
2013-11-10 21:53:51 +01:00
foreach ( $_structure -> contentTypeMap () as $mime_id => $mime_type )
{
$part = $_structure -> getPart ( $mime_id );
2014-02-13 13:59:43 +01:00
//error_log(__METHOD__.__LINE__.':'.' Uid:'.$uid.' Part:'.$_partID.'->'.array2string($part->getMimeId()));
//error_log(__METHOD__.__LINE__.':'.' Uid:'.$uid.' Part:'.$_partID.'->'.$part->getPrimaryType().'/'.$part->getSubType().'->'.$part->getDisposition());
//error_log(__METHOD__.__LINE__.':'.' Uid:'.$uid.' Part:'.$_partID.'->'.array2string($part->getAllDispositionParameters()));
//error_log(__METHOD__.__LINE__.':'.' Uid:'.$uid.' Part:'.$_partID.'->'.array2string($part->getAllContentTypeParameters()));
$partDisposition = $part -> getDisposition ();
$partPrimaryType = $part -> getPrimaryType ();
// we only want to retrieve the attachments of the current mail, not those of possible
// attached mails
if ( $mime_type == 'message/rfc822' && $_partID != $mime_id )
{
//error_log(__METHOD__.__LINE__.' Uid:'.$uid.'->'.$mime_id.':'.array2string($part->contentTypeMap()));
foreach ( $part -> contentTypeMap () as $sub_id => $sub_type ) if ( $sub_id != $mime_id ) $skipParts [ $sub_id ] = $sub_type ;
}
//error_log(__METHOD__.__LINE__.' Uid:'.$uid.' Part:'.$_partID.'->'.$mime_id.':'.array2string($skipParts));
if ( array_key_exists ( $mime_id , $skipParts )) continue ;
if ( $partDisposition == 'attachment' ||
(( $partDisposition == 'inline' || empty ( $partDisposition )) && $partPrimaryType == 'image' && $part -> getContentId () == '' ) ||
(( $partDisposition == 'inline' || empty ( $partDisposition )) && $partPrimaryType != 'image' && $partPrimaryType != 'text' && $partPrimaryType != 'multipart' ) ||
( $fetchEmbeddedImages && ( $partDisposition == 'inline' || empty ( $partDisposition )) && $partPrimaryType == 'image' ) ||
( $fetchTextCalendar && $partPrimaryType == 'text' && $part -> getSubType () == 'calendar' ))
2013-11-10 21:53:51 +01:00
{
2014-02-03 10:28:31 +01:00
// if type is message/rfc822 and _partID is gien, and MimeID equals partID
// we attempt to fetch "ourselves"
if ( $_partID == $part -> getMimeId () && $part -> getPrimaryType () == 'message' ) continue ;
2013-11-10 21:53:51 +01:00
$attachment = $part -> getAllDispositionParameters ();
$attachment [ 'mimeType' ] = $mime_type ;
$attachment [ 'uid' ] = $_uid ;
$attachment [ 'partID' ] = $mime_id ;
if ( ! isset ( $attachment [ 'name' ])) $attachment [ 'name' ] = $part -> getName ();
2014-02-10 17:37:21 +01:00
if ( $fetchTextCalendar )
{
//error_log(__METHOD__.__LINE__.array2string($part->getAllContentTypeParameters()));
$method = $part -> getContentTypeParameter ( 'method' );
if ( $method ) $attachment [ 'method' ] = $method ;
if ( ! isset ( $attachment [ 'name' ])) $attachment [ 'name' ] = 'event.ics' ;
}
2013-11-10 21:53:51 +01:00
$attachment [ 'size' ] = $part -> getBytes ();
if (( $cid = $part -> getContentId ())) $attachment [ 'cid' ] = $cid ;
$attachments [] = $attachment ;
2013-02-19 17:30:59 +01:00
}
}
return $attachments ;
}
2013-04-09 16:43:55 +02:00
/**
* retrieve a attachment
*
* @ param int _uid the uid of the message
* @ param string _partID the id of the part , which holds the attachment
* @ param int _winmail_nr winmail . dat attachment nr .
2013-11-20 10:30:25 +01:00
* @ param boolean _returnPart flag to indicate if the attachment is to be returned as horde mime part object
* @ param boolean _stream flag to indicate if the attachment is to be fetched or returned as filepointer
2013-04-09 16:43:55 +02:00
*
* @ return array
*/
2013-11-20 10:30:25 +01:00
function getAttachment ( $_uid , $_partID , $_winmail_nr = 0 , $_returnPart = true , $_stream = false )
2013-04-09 16:43:55 +02:00
{
2013-11-06 16:11:15 +01:00
$_folder = ( $this -> sessionData [ 'mailbox' ] ? $this -> sessionData [ 'mailbox' ] : $this -> icServer -> getCurrentMailbox ());
2013-04-09 16:43:55 +02:00
2013-11-06 16:11:15 +01:00
$uidsToFetch = new Horde_Imap_Client_Ids ();
2014-01-23 12:56:34 +01:00
if ( ! ( is_object ( $_uid ) || is_array ( $_uid ))) $_uid = ( array ) $_uid ;
$uidsToFetch -> add ( $_uid );
2013-11-06 16:11:15 +01:00
$fquery = new Horde_Imap_Client_Fetch_Query ();
$fquery -> structure ();
2014-01-16 11:51:22 +01:00
$fquery -> bodyPart ( $_partID , array ( 'peek' => true ));
2013-11-06 16:11:15 +01:00
$headersNew = $this -> icServer -> fetch ( $_folder , $fquery , array (
'ids' => $uidsToFetch ,
));
if ( is_object ( $headersNew )) {
foreach ( $headersNew as $id => $_headerObject ) {
$body = $_headerObject -> getFullMsg ();
if ( $_partID != '' )
{
$mailStructureObject = $_headerObject -> getStructure ();
2013-11-20 11:00:16 +01:00
$mailStructureObject -> contentTypeMap ();
2013-11-06 16:11:15 +01:00
$part = $mailStructureObject -> getPart ( $_partID );
2014-02-10 17:37:21 +01:00
if ( $part -> getDisposition () == 'attachment' || $part -> getDisposition () == 'inline' || $part -> getPrimaryType () == 'text' && $part -> getSubType () == 'calendar' )
2013-11-06 16:11:15 +01:00
{
$headerObject [ 'ATTACHMENTS' ][ $mime_id ] = $part -> getAllDispositionParameters ();
2013-11-29 06:24:39 +01:00
2013-11-06 16:11:15 +01:00
$structure_bytes = $part -> getBytes ();
2014-02-10 14:23:32 +01:00
$structure_mime = $part -> getType ();
$structure_partID = $part -> getMimeId ();
2013-11-20 10:30:25 +01:00
$filename = $part -> getName ();
2014-01-16 11:51:22 +01:00
$this -> fetchPartContents ( $_uid , $part , $_stream , $_preserveSeen = true );
2013-11-20 10:30:25 +01:00
if ( $_returnPart ) return $part ;
2013-11-06 16:11:15 +01:00
}
}
}
2013-04-09 16:43:55 +02:00
}
2013-08-21 12:47:20 +02:00
$ext = mime_magic :: mime2ext ( $structure -> type . '/' . $structure -> subType );
if ( $ext && stripos ( $filename , '.' ) === false && stripos ( $filename , $ext ) === false ) $filename = trim ( $filename ) . '.' . $ext ;
2013-04-09 16:43:55 +02:00
$attachmentData = array (
2013-11-20 10:30:25 +01:00
'type' => $structure_mime ,
2013-04-09 16:43:55 +02:00
'filename' => $filename ,
2013-11-20 10:30:25 +01:00
'attachment' => $part -> getContents ( array ( 'stream' => $_stream ))
2013-04-09 16:43:55 +02:00
);
2013-11-20 10:30:25 +01:00
/*
2013-04-09 16:43:55 +02:00
// try guessing the mimetype, if we get the application/octet-stream
if ( strtolower ( $attachmentData [ 'type' ]) == 'application/octet-stream' ) $attachmentData [ 'type' ] = mime_magic :: filename2mime ( $attachmentData [ 'filename' ]);
# if the attachment holds a winmail number and is a winmail.dat then we have to handle that.
if ( $filename == 'winmail.dat' && $_winmail_nr > 0 &&
( $wmattach = $this -> decode_winmail ( $_uid , $_partID , $_winmail_nr ) ) )
{
2013-08-21 12:47:20 +02:00
$ext = mime_magic :: mime2ext ( $wmattach [ 'type' ]);
if ( $ext && stripos ( $wmattach [ 'name' ], '.' ) === false && stripos ( $wmattach [ 'name' ], $ext ) === false ) $wmattach [ 'name' ] = trim ( $wmattach [ 'name' ]) . '.' . $ext ;
2013-04-09 16:43:55 +02:00
$attachmentData = array (
'type' => $wmattach [ 'type' ],
'filename' => $wmattach [ 'name' ],
'attachment' => $wmattach [ 'attachment' ],
);
}
2013-11-20 10:30:25 +01:00
*/
2013-04-09 16:43:55 +02:00
return $attachmentData ;
}
2013-04-12 15:10:27 +02:00
/**
* Fetch a specific attachment from a message by it ' s cid
*
* this function is based on a on " Building A PHP-Based Mail Client "
* http :// www . devshed . com
*
* @ param string | int $_uid
* @ param string $_cid
* @ param string $_part
2013-11-10 21:53:51 +01:00
* @ param boolean $_stream = null null do NOT fetch content , use fetchPartContents later
* true :
* @ return Horde_Mime_Part
2013-04-12 15:10:27 +02:00
*/
2013-11-10 21:53:51 +01:00
function getAttachmentByCID ( $_uid , $_cid , $_part , $_stream = null )
2013-04-12 15:10:27 +02:00
{
// some static variables to avoid fetching the same mail multiple times
2013-11-10 21:53:51 +01:00
static $uid = null , $part = null , $structure = null ;
2013-06-12 13:50:30 +02:00
//error_log(__METHOD__.__LINE__.":$_uid, $_cid, $_part");
2013-04-12 15:10:27 +02:00
if ( empty ( $_cid )) return false ;
if ( $_uid != $uid || $_part != $part )
{
2013-11-10 21:53:51 +01:00
$structure = $this -> getStructure ( $uid = $_uid , $part = $_part );
2013-04-12 15:10:27 +02:00
}
2013-11-10 21:53:51 +01:00
/** @var Horde_Mime_Part */
$attachment = null ;
foreach ( $structure -> contentTypeMap () as $mime_id => $mime_type )
2013-04-12 15:10:27 +02:00
{
2013-11-10 21:53:51 +01:00
$part = $structure -> getPart ( $mime_id );
if ( $part -> getPrimaryType () == 'image' &&
(( $cid = $part -> getContentId ()) &&
// RB: seem a bit fague to search for inclusion in both ways
( strpos ( $cid , $_cid ) !== false || strpos ( $_cid , $cid ) !== false )) ||
(( $name = $part -> getName ()) &&
( strpos ( $name , $_cid ) !== false || strpos ( $_cid , $name ) !== false )))
2013-04-12 15:10:27 +02:00
{
2013-11-10 21:53:51 +01:00
// if we have a direct match, dont search any further
if ( $cid == $_cid )
2013-04-12 15:10:27 +02:00
{
2013-11-10 21:53:51 +01:00
$attachment = $part ;
2013-04-12 15:10:27 +02:00
}
2013-11-10 21:53:51 +01:00
// everything else we only consider after we checked all
if ( ! isset ( $attachment )) $attachment = $part ;
2014-02-04 14:37:26 +01:00
// do we want content fetched, can be done later, if not needed
if ( isset ( $_stream ))
{
$this -> fetchPartContents ( $_uid , $attachment , $_stream );
}
if ( isset ( $attachment )) break ;
2013-04-12 15:10:27 +02:00
}
}
2013-11-10 21:53:51 +01:00
// set name as filename, if not set
2013-12-20 14:25:11 +01:00
if ( $attachment && ! $attachment -> getDispositionParameter ( 'filename' ))
2013-04-12 15:10:27 +02:00
{
2013-11-10 21:53:51 +01:00
$attachment -> setDispositionParameter ( 'filename' , $attachment -> getName ());
2013-04-12 15:10:27 +02:00
}
2013-11-10 21:53:51 +01:00
// guess type, if not set
2013-12-20 14:25:11 +01:00
if ( $attachment && $attachment -> getType () == 'application/octet-stream' )
2013-04-12 15:10:27 +02:00
{
2013-11-10 21:53:51 +01:00
$attachment -> setType ( mime_magic :: filename2mime ( $attachment -> getDispositionParameter ( 'filename' )));
2013-04-12 15:10:27 +02:00
}
2013-11-10 21:53:51 +01:00
//error_log(__METHOD__."($_uid, '$_cid', '$_part') returning ".array2string($attachment));
return $attachment ;
}
2013-04-12 15:10:27 +02:00
2013-11-10 21:53:51 +01:00
/**
* Fetch and add contents to a part
*
* To get contents you use $part -> getContents ();
*
* @ param int $_uid
* @ param Horde_Mime_Part $part
* @ param boolean $_stream = false true return a stream , false a string
* @ param boolean $_preserveSeen flag to preserve the seenflag by using body . peek
* @ return Horde_Mime_Part
*/
2014-02-04 14:37:26 +01:00
public function fetchPartContents ( $_uid , Horde_Mime_Part $part = null , $_stream = false , $_preserveSeen = false )
2013-11-10 21:53:51 +01:00
{
2014-02-04 14:37:26 +01:00
if ( is_null ( $part )) return null ; //new Horde_Mime_Part;
2013-11-16 12:25:09 +01:00
$encoding = null ;
2013-11-10 21:53:51 +01:00
// we need to set content on structure to decode transfer encoding
$part -> setContents (
2013-11-16 12:25:09 +01:00
$this -> getBodyPart ( $_uid , $part -> getMimeId (), null , $_preserveSeen , $_stream , $encoding ),
2013-11-10 21:53:51 +01:00
array ( 'encoding' => $encoding ));
2013-04-12 15:10:27 +02:00
2013-11-10 21:53:51 +01:00
return $part ;
2013-04-12 15:10:27 +02:00
}
2013-04-09 16:43:55 +02:00
2013-09-05 16:52:11 +02:00
/**
* save a message in folder
* throws exception on failure
* @ todo set flags again
*
* @ param string _folderName the foldername
* @ param string _header the header of the message
* @ param string _body the body of the message
* @ param string _flags the imap flags to set for the saved message
*
* @ return the id of the message appended or exception
*/
function appendMessage ( $_folderName , $_header , $_body , $_flags )
{
2013-11-21 12:10:31 +01:00
//error_log(__METHOD__.__LINE__."$_folderName, $_header, $_body, $_flags");
2013-09-05 16:52:11 +02:00
$header = ltrim ( str_replace ( " \n " , " \r \n " , $_header ));
$body = str_replace ( " \n " , " \r \n " , $_body );
2014-02-03 12:29:12 +01:00
// the recent flag is the default enforced here ; as we assume the _flags is always set,
// we default it to hordes default (Recent) (, other wise we should not pass the parameter
// for flags at all)
if ( empty ( $_flags )) $_flags = '\\Recent' ;
2013-12-09 10:57:03 +01:00
//if (!is_array($_flags) && stripos($_flags,',')!==false) $_flags=explode(',',$_flags);
//if (!is_array($_flags)) $_flags = (array) $_flags;
2013-11-21 12:10:31 +01:00
try
{
$dataNflags = array ();
$dataNflags [] = array ( 'data' => array ( array ( 't' => 'text' , 'v' => " $header " . " $body " )), 'flags' => array ( $_flags ));
$messageid = $this -> icServer -> append ( $_folderName , $dataNflags );
}
catch ( Exception $e )
{
if ( self :: $debug ) error_log ( " Could not append Message: " . $e -> getMessage ());
throw new egw_exception_wrong_userinput ( lang ( " Could not append Message: " . $e -> getMessage ));
2013-09-05 16:52:11 +02:00
//return false;
}
//error_log(__METHOD__.__LINE__.' appended UID:'.$messageid);
//$messageid = true; // for debug reasons only
if ( $messageid === true ) // try to figure out the message uid
{
$list = $this -> getHeaders ( $_folderName , $_startMessage = 1 , $_numberOfMessages = 1 , $_sort = 'INTERNALDATE' , $_reverse = true , $_filter = array (), $_thisUIDOnly = null , $_cacheResult = false );
if ( $list )
{
if ( self :: $debug ) error_log ( __METHOD__ . __LINE__ . ' MessageUid:' . $messageid . ' but found:' . array2string ( $list ));
$messageid = $list [ 'header' ][ 0 ][ 'uid' ];
}
}
return $messageid ;
}
2013-07-20 09:23:55 +02:00
/**
* getRandomString - function to be used to fetch a random string and md5 encode that one
* @ param none
* @ return string - a random number which is md5 encoded
*/
static function getRandomString () {
mt_srand (( float ) microtime () * 1000000 );
return md5 ( mt_rand ( 100000 , 999999 ));
}
2013-03-05 15:09:35 +01:00
/**
* functions to allow access to mails through other apps to fetch content
* used in infolog , tracker
*/
/**
* get_mailcontent - fetches the actual mailcontent , and returns it as well defined array
* @ param object mailClass the mailClassobject to be used
* @ param uid the uid of the email to be processed
* @ param partid the partid of the email
* @ param mailbox the mailbox , that holds the message
* @ param preserveHTML flag to pass through to getdisplayableBody
2013-07-24 09:34:17 +02:00
* @ param addHeaderSection flag to be able to supress headersection
2014-01-07 14:03:53 +01:00
* @ param includeAttachments flag to be able to supress possible attachments
2013-07-18 10:12:09 +02:00
* @ return array / bool with 'mailaddress' => $mailaddress ,
2013-03-05 15:09:35 +01:00
* 'subject' => $subject ,
* 'message' => $message ,
* 'attachments' => $attachments ,
2013-07-18 10:12:09 +02:00
* 'headers' => $headers ,; boolean false on failure
2013-03-05 15:09:35 +01:00
*/
2014-01-07 14:03:53 +01:00
static function get_mailcontent ( & $mailClass , $uid , $partid = '' , $mailbox = '' , $preserveHTML = false , $addHeaderSection = true , $includeAttachments = true )
2013-03-05 15:09:35 +01:00
{
//echo __METHOD__." called for $uid,$partid <br>";
2014-01-20 14:14:18 +01:00
$headers = $mailClass -> getMessageHeader ( $uid , $partid , true , false , $mailbox );
2013-07-18 10:12:09 +02:00
if ( empty ( $headers )) return false ;
2013-03-05 15:09:35 +01:00
// dont force retrieval of the textpart, let mailClass preferences decide
2014-01-20 14:14:18 +01:00
$bodyParts = $mailClass -> getMessageBody ( $uid ,( $preserveHTML ? 'always_display' : 'only_if_no_text' ), $partid , null , false , $mailbox );
2013-10-10 13:44:01 +02:00
// if we do not want HTML but there is no TextRepresentation with the message itself, try converting
if ( ! $preserveHTML && $bodyParts [ 0 ][ 'mimeType' ] == 'text/html' )
{
foreach ( $bodyParts as $i => $part )
{
if ( $bodyParts [ $i ][ 'mimeType' ] == 'text/html' )
{
$bodyParts [ $i ][ 'body' ] = translation :: convertHTMLToText ( $bodyParts [ $i ][ 'body' ], $bodyParts [ $i ][ 'charSet' ], true , $stripalltags = true );
$bodyParts [ $i ][ 'mimeType' ] = 'text/plain' ;
}
}
}
2013-03-05 15:09:35 +01:00
//error_log(array2string($bodyParts));
2014-01-07 14:03:53 +01:00
$attachments = $includeAttachments ? $mailClass -> getMessageAttachments ( $uid , $partid ) : array ();
2013-03-05 15:09:35 +01:00
if ( $mailClass -> isSentFolder ( $mailbox )) $mailaddress = $headers [ 'TO' ];
elseif ( isset ( $headers [ 'FROM' ])) $mailaddress = $headers [ 'FROM' ];
elseif ( isset ( $headers [ 'SENDER' ])) $mailaddress = $headers [ 'SENDER' ];
if ( isset ( $headers [ 'CC' ])) $mailaddress .= ',' . $headers [ 'CC' ];
//_debug_array($headers);
$subject = $headers [ 'SUBJECT' ];
$message = self :: getdisplayableBody ( $mailClass , $bodyParts , $preserveHTML );
if ( $preserveHTML && $mailClass -> activeMimeType == 'text/plain' ) $message = '<pre>' . $message . '</pre>' ;
2013-07-24 09:34:17 +02:00
$headdata = ( $addHeaderSection ? self :: createHeaderInfoSection ( $headers , '' , $preserveHTML ) : '' );
2013-03-05 15:09:35 +01:00
$message = $headdata . $message ;
//echo __METHOD__.'<br>';
//_debug_array($attachments);
if ( is_array ( $attachments ))
{
foreach ( $attachments as $num => $attachment )
{
if ( $attachment [ 'mimeType' ] == 'MESSAGE/RFC822' )
{
//_debug_array($mailClass->getMessageHeader($uid, $attachment['partID']));
//_debug_array($mailClass->getMessageBody($uid,'', $attachment['partID']));
//_debug_array($mailClass->getMessageAttachments($uid, $attachment['partID']));
$mailcontent = self :: get_mailcontent ( $mailClass , $uid , $attachment [ 'partID' ]);
$headdata = '' ;
if ( $mailcontent [ 'headers' ])
{
$headdata = self :: createHeaderInfoSection ( $mailcontent [ 'headers' ], '' , $preserveHTML );
}
if ( $mailcontent [ 'message' ])
{
$tempname = tempnam ( $GLOBALS [ 'egw_info' ][ 'server' ][ 'temp_dir' ], $GLOBALS [ 'egw_info' ][ 'flags' ][ 'currentapp' ] . " _ " );
$attachedMessages [] = array (
'type' => 'TEXT/PLAIN' ,
'name' => $mailcontent [ 'subject' ] . '.txt' ,
'tmp_name' => $tempname ,
);
$tmpfile = fopen ( $tempname , 'w' );
fwrite ( $tmpfile , $headdata . $mailcontent [ 'message' ]);
fclose ( $tmpfile );
}
foreach ( $mailcontent [ 'attachments' ] as $tmpattach => $tmpval )
{
$attachedMessages [] = $tmpval ;
}
unset ( $attachments [ $num ]);
}
else
{
2013-11-20 11:00:16 +01:00
$attachments [ $num ] = array_merge ( $attachments [ $num ], $mailClass -> getAttachment ( $uid , $attachment [ 'partID' ], 0 , false , false ));
2013-03-05 15:09:35 +01:00
if ( isset ( $attachments [ $num ][ 'charset' ])) {
if ( $attachments [ $num ][ 'charset' ] === false ) $attachments [ $num ][ 'charset' ] = translation :: detect_encoding ( $attachments [ $num ][ 'attachment' ]);
translation :: convert ( $attachments [ $num ][ 'attachment' ], $attachments [ $num ][ 'charset' ]);
}
$attachments [ $num ][ 'type' ] = $attachments [ $num ][ 'mimeType' ];
$attachments [ $num ][ 'tmp_name' ] = tempnam ( $GLOBALS [ 'egw_info' ][ 'server' ][ 'temp_dir' ], $GLOBALS [ 'egw_info' ][ 'flags' ][ 'currentapp' ] . " _ " );
$tmpfile = fopen ( $attachments [ $num ][ 'tmp_name' ], 'w' );
fwrite ( $tmpfile , $attachments [ $num ][ 'attachment' ]);
fclose ( $tmpfile );
unset ( $attachments [ $num ][ 'attachment' ]);
}
}
if ( is_array ( $attachedMessages )) $attachments = array_merge ( $attachments , $attachedMessages );
}
return array (
'mailaddress' => $mailaddress ,
'subject' => $subject ,
'message' => $message ,
'attachments' => $attachments ,
'headers' => $headers ,
);
}
/**
* createHeaderInfoSection - creates a textual headersection from headerobject
* @ param array header headerarray may contain SUBJECT , FROM , SENDER , TO , CC , BCC , DATE , PRIORITY , IMPORTANCE
2013-08-21 12:47:20 +02:00
* @ param string headline Text tom use for headline , if SUPPRESS , supress headline and footerline
2013-03-05 15:09:35 +01:00
* @ param bool createHTML do it with HTML breaks
* @ return string a preformatted string with the information of the header worked into it
*/
static function createHeaderInfoSection ( $header , $headline = '' , $createHTML = false )
{
$headdata = null ;
2013-08-21 12:47:20 +02:00
//error_log(__METHOD__.__LINE__.array2string($header).function_backtrace());
2013-03-05 15:09:35 +01:00
if ( $header [ 'SUBJECT' ]) $headdata = lang ( 'subject' ) . ': ' . $header [ 'SUBJECT' ] . ( $createHTML ? " <br /> " : " \n " );
if ( $header [ 'FROM' ]) $headdata .= lang ( 'from' ) . ': ' . self :: convertAddressArrayToString ( $header [ 'FROM' ], $createHTML ) . ( $createHTML ? " <br /> " : " \n " );
if ( $header [ 'SENDER' ]) $headdata .= lang ( 'sender' ) . ': ' . self :: convertAddressArrayToString ( $header [ 'SENDER' ], $createHTML ) . ( $createHTML ? " <br /> " : " \n " );
if ( $header [ 'TO' ]) $headdata .= lang ( 'to' ) . ': ' . self :: convertAddressArrayToString ( $header [ 'TO' ], $createHTML ) . ( $createHTML ? " <br /> " : " \n " );
if ( $header [ 'CC' ]) $headdata .= lang ( 'cc' ) . ': ' . self :: convertAddressArrayToString ( $header [ 'CC' ], $createHTML ) . ( $createHTML ? " <br /> " : " \n " );
if ( $header [ 'BCC' ]) $headdata .= lang ( 'bcc' ) . ': ' . self :: convertAddressArrayToString ( $header [ 'BCC' ], $createHTML ) . ( $createHTML ? " <br /> " : " \n " );
if ( $header [ 'DATE' ]) $headdata .= lang ( 'date' ) . ': ' . $header [ 'DATE' ] . ( $createHTML ? " <br /> " : " \n " );
if ( $header [ 'PRIORITY' ] && $header [ 'PRIORITY' ] != 'normal' ) $headdata .= lang ( 'priority' ) . ': ' . $header [ 'PRIORITY' ] . ( $createHTML ? " <br /> " : " \n " );
if ( $header [ 'IMPORTANCE' ] && $header [ 'IMPORTANCE' ] != 'normal' ) $headdata .= lang ( 'importance' ) . ': ' . $header [ 'IMPORTANCE' ] . ( $createHTML ? " <br /> " : " \n " );
//if ($mailcontent['headers']['ORGANIZATION']) $headdata .= lang('organization').': '.$mailcontent['headers']['ORGANIZATION']."\
if ( ! empty ( $headdata ))
{
2013-08-21 12:47:20 +02:00
if ( ! empty ( $headline ) && $headline != 'SUPPRESS' ) $headdata = " ---------------------------- $headline ---------------------------- " . ( $createHTML ? " <br /> " : " \n " ) . $headdata ;
if ( empty ( $headline )) $headdata = ( $headline != 'SUPPRESS' ? " -------------------------------------------------------- " . ( $createHTML ? " <br /> " : " \n " ) : '' ) . $headdata ;
$headdata .= ( $headline != 'SUPPRESS' ? " -------------------------------------------------------- " . ( $createHTML ? " <br /> " : " \n " ) : '' );
2013-03-05 15:09:35 +01:00
}
else
{
2013-08-21 12:47:20 +02:00
$headdata = ( $headline != 'SUPPRESS' ? " -------------------------------------------------------- " . ( $createHTML ? " <br /> " : " \n " ) : '' );
2013-03-05 15:09:35 +01:00
}
return $headdata ;
}
2013-09-02 17:15:35 +02:00
/**
* adaptSubjectForImport - strips subject from unwanted Characters , and does some normalization
* to meet expectations
* @ param string $subject string to process
* @ return string
*/
static function adaptSubjectForImport ( $subject )
{
$subject = str_replace ( '$$' , '__' ,( $subject ? $subject : lang ( '(no subject)' )));
$subject = str_ireplace ( array ( '[FWD]' , '[' , ']' , '{' , '}' , '<' , '>' ), array ( 'Fwd:' , ' ' , ' ' , ' ' , ' ' , ' ' , ' ' ), trim ( $subject ));
return $subject ;
}
2013-03-05 15:09:35 +01:00
/**
* convertAddressArrayToString - converts an mail envelope Address Array To String
* @ param array $rfcAddressArray an addressarray as provided by mail retieved via egw_pear ....
* @ return string a comma separated string with the mailaddress ( es ) converted to text
*/
static function convertAddressArrayToString ( $rfcAddressArray , $createHTML = false )
{
//error_log(__METHOD__.__LINE__.array2string($rfcAddressArray));
$returnAddr = '' ;
if ( is_array ( $rfcAddressArray ))
{
foreach (( array ) $rfcAddressArray as $addressData ) {
//error_log(__METHOD__.__LINE__.array2string($addressData));
if ( $addressData [ 'MAILBOX_NAME' ] == 'NIL' ) {
continue ;
}
if ( strtolower ( $addressData [ 'MAILBOX_NAME' ]) == 'undisclosed-recipients' ) {
continue ;
}
if ( $addressData [ 'RFC822_EMAIL' ])
{
$addressObjectA = imap_rfc822_parse_adrlist (( get_magic_quotes_gpc () ? stripslashes ( $addressData [ 'RFC822_EMAIL' ]) : $addressData [ 'RFC822_EMAIL' ]), '' );
}
else
{
$emailaddress = ( $addressData [ 'PERSONAL_NAME' ] ? $addressData [ 'PERSONAL_NAME' ] . ' <' . $addressData [ 'EMAIL' ] . '>' : $addressData [ 'EMAIL' ]);
$addressObjectA = imap_rfc822_parse_adrlist (( get_magic_quotes_gpc () ? stripslashes ( $emailaddress ) : $emailaddress ), '' );
}
$addressObject = $addressObjectA [ 0 ];
//error_log(__METHOD__.__LINE__.array2string($addressObject));
if ( $addressObject -> host == '.SYNTAX-ERROR.' ) continue ;
//$mb =(string)$addressObject->mailbox;
//$h = (string)$addressObject->host;
//$p = (string)$addressObject->personal;
$returnAddr .= ( strlen ( $returnAddr ) > 0 ? ',' : '' );
//error_log(__METHOD__.__LINE__.$p.' <'.$mb.'@'.$h.'>');
$buff = imap_rfc822_write_address ( $addressObject -> mailbox , self :: $idna2 -> decode ( $addressObject -> host ), $addressObject -> personal );
$buff = str_replace ( array ( '<' , '>' ), array ( '[' , ']' ), $buff );
if ( $createHTML ) $buff = mail_bo :: htmlspecialchars ( $buff );
//error_log(__METHOD__.__LINE__.' Address: '.$returnAddr);
$returnAddr .= $buff ;
}
}
else
{
// do not mess with strings, return them untouched /* ToDo: validate string as Address */
$rfcAddressArray = self :: decode_header ( $rfcAddressArray , true );
$rfcAddressArray = str_replace ( array ( '<' , '>' ), array ( '[' , ']' ), $rfcAddressArray );
if ( is_string ( $rfcAddressArray )) return ( $createHTML ? mail_bo :: htmlspecialchars ( $rfcAddressArray ) : $rfcAddressArray );
}
return $returnAddr ;
}
2013-08-21 12:47:20 +02:00
/**
* Merges a given content with contact data
*
* @ param string $content
* @ param array $ids array with contact id ( s )
* @ param string & $err error - message on error
* @ return string / boolean merged content or false on error
*/
function merge ( $content , $ids , $mimetype = '' )
{
$contacts = new addressbook_bo ();
$mergeobj = new addressbook_merge ();
if ( empty ( $mimetype )) $mimetype = ( strlen ( strip_tags ( $content )) == strlen ( $content ) ? 'text/plain' : 'text/html' );
$rv = $mergeobj -> merge_string ( $content , $ids , $err , $mimetype , array (), self :: $displayCharset );
if ( empty ( $rv ) && ! empty ( $content ) && ! empty ( $err )) $rv = $content ;
if ( ! empty ( $err ) && ! empty ( $content ) && ! empty ( $ids )) error_log ( __METHOD__ . __LINE__ . ' Merge failed for Ids:' . array2string ( $ids ) . ' ContentType:' . $mimetype . ' Content:' . $content . ' Reason:' . array2string ( $err ));
return $rv ;
}
2013-10-02 16:50:24 +02:00
/**
* Returns a string showing the size of the message / attachment
*
* @ param integer $bytes
* @ return string formatted string
*/
static function show_readable_size ( $bytes )
{
$bytes /= 1024 ;
$type = 'k' ;
if ( $bytes / 1024 > 1 )
{
$bytes /= 1024 ;
$type = 'M' ;
2013-10-07 14:35:38 +02:00
if ( $bytes / 1024 > 1 )
{
$bytes *= 10 ;
settype ( $bytes , 'integer' );
$bytes /= 10 ;
$bytes /= 1024 ;
$type = 'G' ;
}
2013-10-02 16:50:24 +02:00
}
if ( $bytes < 10 )
{
$bytes *= 10 ;
settype ( $bytes , 'integer' );
$bytes /= 10 ;
}
else
settype ( $bytes , 'integer' );
return $bytes . ' ' . $type ;
}
2013-10-05 11:47:34 +02:00
static function detect_qp ( & $sting ) {
$needle = '/(=[0-9][A-F])|(=[A-F][0-9])|(=[A-F][A-F])|(=[0-9][0-9])/' ;
return preg_match ( " $needle " , $string );
}
2013-09-05 16:52:11 +02:00
/**
* checkFileBasics
* check if formdata meets basic restrictions ( in tmp dir , or vfs , mimetype , etc . )
*
* @ param array $_formData passed by reference Array with information of name , type , file and size , mimetype may be adapted
* @ param string $IDtoAddToFileName id to enrich the returned tmpfilename
* @ param string $reqMimeType / ( default message / rfc822 , if set to false , mimetype check will not be performed
* @ return mixed $fullPathtoFile or exception
*/
static function checkFileBasics ( & $_formData , $IDtoAddToFileName = '' , $reqMimeType = 'message/rfc822' )
{
//error_log(__METHOD__.__FILE__.array2string($_formData).' Id:'.$IDtoAddToFileName.' ReqMimeType:'.$reqMimeType);
$importfailed = $tmpFileName = false ;
if ( $_formData [ 'size' ] != 0 && ( is_uploaded_file ( $_formData [ 'file' ]) ||
realpath ( dirname ( $_formData [ 'file' ])) == realpath ( $GLOBALS [ 'egw_info' ][ 'server' ][ 'temp_dir' ]) ||
parse_url ( $_formData [ 'file' ], PHP_URL_SCHEME ) == 'vfs' ))
{
// ensure existance of eGW temp dir
// note: this is different from apache temp dir,
// and different from any other temp file location set in php.ini
if ( ! file_exists ( $GLOBALS [ 'egw_info' ][ 'server' ][ 'temp_dir' ]))
{
@ mkdir ( $GLOBALS [ 'egw_info' ][ 'server' ][ 'temp_dir' ], 0700 );
}
// if we were NOT able to create this temp directory, then make an ERROR report
if ( ! file_exists ( $GLOBALS [ 'egw_info' ][ 'server' ][ 'temp_dir' ]))
{
$alert_msg .= 'Error:' . '<br>'
. 'Server is unable to access phpgw tmp directory' . '<br>'
. $GLOBALS [ 'egw_info' ][ 'server' ][ 'temp_dir' ] . '<br>'
. 'Please check your configuration' . '<br>'
. '<br>' ;
}
// sometimes PHP is very clue-less about MIME types, and gives NO file_type
// rfc default for unknown MIME type is:
if ( $reqMimeType == 'message/rfc822' )
{
$mime_type_default = 'message/rfc' ;
}
else
{
$mime_type_default = $reqMimeType ;
}
// check the mimetype by extension. as browsers seem to report crap
// maybe its application/octet-stream -> this may mean that we could not determine the type
// so we check for the suffix too
// trust vfs mime-types, trust the mimetype if it contains a method
if (( substr ( $_formData [ 'file' ], 0 , 6 ) !== 'vfs://' || $_formData [ 'type' ] == 'application/octet-stream' ) && stripos ( $_formData [ 'type' ], 'method=' ) === false )
{
$buff = explode ( '.' , $_formData [ 'name' ]);
$suffix = '' ;
if ( is_array ( $buff )) $suffix = array_pop ( $buff ); // take the last extension to check with ext2mime
if ( ! empty ( $suffix )) $sfxMimeType = mime_magic :: ext2mime ( $suffix );
if ( ! empty ( $suffix ) && ! empty ( $sfxMimeType ) &&
( strlen ( trim ( $_formData [ 'type' ])) == 0 || ( strtolower ( trim ( $_formData [ 'type' ])) != $sfxMimeType )))
{
error_log ( __METHOD__ . __LINE__ . ' Data:' . array2string ( $_formData ));
error_log ( __METHOD__ . __LINE__ . ' Form reported Mimetype:' . $_formData [ 'type' ] . ' but seems to be:' . $sfxMimeType );
$_formData [ 'type' ] = $sfxMimeType ;
}
}
if ( trim ( $_formData [ 'type' ]) == '' )
{
$_formData [ 'type' ] = 'application/octet-stream' ;
}
// if reqMimeType is set to false do not test for that
if ( $reqMimeType )
{
// so if PHP did not pass any file_type info, then substitute the rfc default value
if ( substr ( strtolower ( trim ( $_formData [ 'type' ])), 0 , strlen ( $mime_type_default )) != $mime_type_default )
{
if ( ! ( strtolower ( trim ( $_formData [ 'type' ])) == " application/octet-stream " && $sfxMimeType == $reqMimeType ))
{
//error_log("Message rejected, no message/rfc. Is:".$_formData['type']);
$importfailed = true ;
$alert_msg .= lang ( " File rejected, no %2. Is:%1 " , $_formData [ 'type' ], $reqMimeType );
}
if (( strtolower ( trim ( $_formData [ 'type' ])) != $reqMimeType && $sfxMimeType == $reqMimeType ))
{
$_formData [ 'type' ] = mime_magic :: ext2mime ( $suffix );
}
}
}
// as FreeBSD seems to have problems with the generated temp names we append some more random stuff
$randomString = chr ( rand ( 65 , 90 )) . chr ( rand ( 48 , 57 )) . chr ( rand ( 65 , 90 )) . chr ( rand ( 48 , 57 )) . chr ( rand ( 65 , 90 ));
$tmpFileName = $GLOBALS [ 'egw_info' ][ 'server' ][ 'temp_dir' ] .
SEP .
$GLOBALS [ 'egw_info' ][ 'user' ][ 'account_id' ] .
trim ( $IDtoAddToFileName ) . basename ( $_formData [ 'file' ]) . '_' . $randomString ;
if ( parse_url ( $_formData [ 'file' ], PHP_URL_SCHEME ) == 'vfs' )
{
$tmpFileName = $_formData [ 'file' ]; // no need to store it somewhere
}
elseif ( is_uploaded_file ( $_formData [ 'file' ]))
{
move_uploaded_file ( $_formData [ 'file' ], $tmpFileName ); // requirement for safe_mode!
}
else
{
rename ( $_formData [ 'file' ], $tmpFileName );
}
} else {
//error_log("Import of message ".$_formData['file']." failes to meet basic restrictions");
$importfailed = true ;
$alert_msg .= lang ( " Processing of file %1 failed. Failed to meet basic restrictions. " , $_formData [ 'name' ]);
}
if ( $importfailed == true )
{
throw new egw_exception_wrong_userinput ( $alert_msg );
}
else
{
if ( parse_url ( $tmpFileName , PHP_URL_SCHEME ) == 'vfs' )
{
egw_vfs :: load_wrapper ( 'vfs' );
}
return $tmpFileName ;
}
}
/**
* processURL2InlineImages - parses a html text for images , and adds them as inline attachment
* we do not use the functionality of the phpmailer here , as phpmailers functionality requires
* files to be present within the filesystem , which we do not require as we make this happen
* ( we load the file , and store it temporarily for the use of attaching it to the file send
* @ param object $_mailObject instance of the egw_mailer / phpmailer Object to be used
* @ param string $_html2parse the html to parse and to be altered , if conditions meet
* @ return void
*/
static function processURL2InlineImages ( & $_mailObject , & $_html2parse )
{
$imageC = 0 ;
preg_match_all ( " /(src|background)= \" (.*) \" /Ui " , $_html2parse , $images );
if ( isset ( $images [ 2 ])) {
foreach ( $images [ 2 ] as $i => $url ) {
//$isData = false;
$basedir = '' ;
$needTempFile = true ;
//error_log(__METHOD__.__LINE__.$url);
//error_log(__METHOD__.__LINE__.$GLOBALS['egw_info']['server']['webserver_url']);
//error_log(__METHOD__.__LINE__.array2string($GLOBALS['egw_info']['user']));
// do not change urls for absolute images (thanks to corvuscorax)
if ( ! ( substr ( $url , 0 , strlen ( 'data:' )) == 'data:' )) {
//error_log(__METHOD__.__LINE__.' -> '.$i.': '.array2string($images[$i]));
$filename = basename ( $url );
$directory = dirname ( $url );
( $directory == '.' ) ? $directory = '' : '' ;
$cid = 'cid:' . md5 ( $filename );
$ext = pathinfo ( $filename , PATHINFO_EXTENSION );
$mimeType = $_mailObject -> _mime_types ( $ext );
if ( strlen ( $directory ) > 1 && substr ( $directory , - 1 ) != '/' ) { $directory .= '/' ; }
$myUrl = $directory . $filename ;
if ( $myUrl [ 0 ] == '/' ) // local path -> we only allow path's that are available via http/https (or vfs)
{
$basedir = ( $_SERVER [ 'HTTPS' ] ? 'https://' : 'http://' . $_SERVER [ 'HTTP_HOST' ]);
}
// use vfs instead of url containing webdav.php
// ToDo: we should test if the webdav url is of our own scope, as we cannot handle foreign
// webdav.php urls as vfs
if ( strpos ( $myUrl , 'webdav.php' ) !== false ) // we have a webdav link, so we build a vfs/sqlfs link of it.
{
egw_vfs :: load_wrapper ( 'vfs' );
list ( $garbage , $vfspart ) = explode ( 'webdav.php' , $myUrl , 2 );
$myUrl = $vfspart ;
$basedir = 'vfs://default' ;
$needTempFile = false ;
}
if ( strlen ( $basedir ) > 1 && substr ( $basedir , - 1 ) != '/' && $myUrl [ 0 ] != '/' ) { $basedir .= '/' ; }
//error_log(__METHOD__.__LINE__.$basedir.$myUrl);
if ( $needTempFile ) $data = file_get_contents ( $basedir . urldecode ( $myUrl ));
}
if ( substr ( $url , 0 , strlen ( 'data:' )) == 'data:' )
{
//error_log(__METHOD__.__LINE__.' -> '.$i.': '.array2string($images[$i]));
// we only support base64 encoded data
$tmp = substr ( $url , strlen ( 'data:' ));
list ( $mimeType , $data ) = explode ( ';base64,' , $tmp );
list ( $what , $exactly ) = explode ( '/' , $mimeType );
$needTempFile = true ;
$filename = ( $what ? $what : 'data' ) . $imageC ++. '.' . $exactly ;
$cid = 'cid:' . md5 ( $filename );
$data = base64_decode ( $data );
//$isData = true;
}
if ( $data || $needTempFile === false )
{
if ( $needTempFile )
{
$attachment_file = tempnam ( $GLOBALS [ 'egw_info' ][ 'server' ][ 'temp_dir' ], $GLOBALS [ 'egw_info' ][ 'flags' ][ 'currentapp' ] . " _ " );
$tmpfile = fopen ( $attachment_file , 'w' );
fwrite ( $tmpfile , $data );
fclose ( $tmpfile );
}
else
{
$attachment_file = $basedir . urldecode ( $myUrl );
}
//error_log(__METHOD__.__LINE__.' '.$url.' -> '.$basedir.$myUrl. ' TmpFile:'.$tmpfile);
//error_log(__METHOD__.__LINE__.' '.$url.' -> '.$mimeType. ' TmpFile:'.$attachment_file);
if ( $_mailObject -> AddEmbeddedImage ( $attachment_file , md5 ( $filename ), $filename , 'base64' , $mimeType ) )
{
//$_html2parse = preg_replace("/".$images[1][$i]."=\"".preg_quote($url, '/')."\"/Ui", $images[1][$i]."=\"".$cid."\"", $_html2parse);
$_html2parse = str_replace ( $images [ 1 ][ $i ] . " = \" " . $url . " \" " , $images [ 1 ][ $i ] . " = \" " . $cid . " \" " , $_html2parse );
}
}
}
}
}
/**
* importMessageToMergeAndSend
*
* @ param object & bo_merge bo_merge object
* @ param string $document the full filename
* @ param array $SendAndMergeTocontacts array of contact ids
* @ param string $_folder ( passed by reference ) will set the folder used . must be set with a folder , but will hold modifications if
* folder is modified
* @ param string $importID ID for the imported message , used by attachments to identify them unambiguously
* @ return mixed array of messages with success and failed messages or exception
*/
2014-01-15 17:29:30 +01:00
function importMessageToMergeAndSend ( bo_merge $bo_merge , $document , $SendAndMergeTocontacts , & $_folder , & $importID = '' )
2013-09-05 16:52:11 +02:00
{
$importfailed = false ;
$processStats = array ( 'success' => array (), 'failed' => array ());
if ( empty ( $SendAndMergeTocontacts ))
{
$importfailed = true ;
$alert_msg .= lang ( " Import of message %1 failed. No Contacts to merge and send to specified. " , $_formData [ 'name' ]);
}
// check if formdata meets basic restrictions (in tmp dir, or vfs, mimetype, etc.)
/* as the file is provided by bo_merge , we do not check
try
{
$tmpFileName = mail_bo :: checkFileBasics ( $_formData , $importID );
}
catch ( egw_exception_wrong_userinput $e )
{
$importfailed = true ;
$alert_msg .= $e -> getMessage ();
}
*/
$tmpFileName = $document ;
// -----------------------------------------------------------------------
if ( $importfailed === false )
{
$mailObject = new egw_mailer ();
try
{
$this -> parseFileIntoMailObject ( $mailObject , $tmpFileName , $Header , $Body );
}
catch ( egw_exception_assertion_failed $e )
{
$importfailed = true ;
$alert_msg .= $e -> getMessage ();
}
//_debug_array($Body);
$this -> openConnection ();
if ( empty ( $_folder ))
{
$_folder = $this -> getSentFolder ();
}
$delimiter = $this -> getHierarchyDelimiter ();
if ( $_folder == 'INBOX' . $delimiter ) $_folder = 'INBOX' ;
if ( $importfailed === false )
{
$Subject = $mailObject -> Subject ;
//error_log(__METHOD__.__LINE__.' Subject:'.$Subject);
$Body = $mailObject -> Body ;
//error_log(__METHOD__.__LINE__.' Body:'.$Body);
//error_log(__METHOD__.__LINE__.' BodyContentType:'.$mailObject->BodyContentType);
$AltBody = $mailObject -> AltBody ;
//error_log(__METHOD__.__LINE__.' AltBody:'.$AltBody);
//error_log(__METHOD__.__LINE__.array2string($mailObject->GetReplyTo()));
// Fetch ReplyTo - Address if existing to check if we are to replace it
$replyTo = $mailObject -> GetReplyTo ();
if ( isset ( $replyTo [ 'replace@import.action' ]))
{
$mailObject -> ClearReplyTos ();
2014-01-14 16:21:54 +01:00
$activeMailProfiles = $this -> mail -> getAccountIdentities ( $this -> profileID );
$activeMailProfile = array_shift ( $activeMailProfiles );
$mailObject -> AddReplyTo ( self :: $idna2 -> encode ( $activeMailProfile [ 'ident_email' ]), mail_bo :: generateIdentityString ( $activeMailProfile , false ));
2013-09-05 16:52:11 +02:00
}
foreach ( $SendAndMergeTocontacts as $k => $val )
{
$mailObject -> ErrorInfo = $errorInfo = '' ;
//$mailObject->SMTPDebug = 5;
$mailObject -> set ( 'error_count' , 0 );
$sendOK = $openComposeWindow = $openAsDraft = null ;
//error_log(__METHOD__.__LINE__.' Id To Merge:'.$val);
2014-01-17 11:47:35 +01:00
if ( /*$GLOBALS['egw_info']['flags']['currentapp'] == 'addressbook' &&*/
2013-09-05 16:52:11 +02:00
count ( $SendAndMergeTocontacts ) > 1 &&
is_numeric ( $val ) || $GLOBALS [ 'egw' ] -> accounts -> name2id ( $val )) // do the merge
{
//error_log(__METHOD__.__LINE__.array2string($mailObject));
$contact = $bo_merge -> contacts -> read ( $val );
//error_log(__METHOD__.__LINE__.' ID:'.$val.' Data:'.array2string($contact));
$email = ( $contact [ 'email' ] ? $contact [ 'email' ] : $contact [ 'email_home' ]);
$nfn = ( $contact [ 'n_fn' ] ? $contact [ 'n_fn' ] : $contact [ 'n_given' ] . ' ' . $contact [ 'n_family' ]);
2014-01-15 17:29:30 +01:00
$activeMailProfiles = $this -> getAccountIdentities ( $this -> profileID );
2014-01-14 16:21:54 +01:00
$activeMailProfile = array_shift ( $activeMailProfiles );
2013-09-05 16:52:11 +02:00
//error_log(__METHOD__.__LINE__.array2string($activeMailProfile));
2014-01-14 16:21:54 +01:00
$mailObject -> From = $activeMailProfile [ 'ident_email' ];
2013-09-05 16:52:11 +02:00
//$mailObject->From = $_identity->emailAddress;
$mailObject -> FromName = $mailObject -> EncodeHeader ( self :: generateIdentityString ( $activeMailProfile , false ));
$mailObject -> MessageID = '' ;
$mailObject -> ClearAllRecipients ();
$mailObject -> ClearCustomHeaders ();
$mailObject -> AddAddress ( self :: $idna2 -> encode ( $email ), $mailObject -> EncodeHeader ( $nfn ));
$mailObject -> Subject = $bo_merge -> merge_string ( $Subject , $val , $e , 'text/plain' , array (), self :: $displayCharset );
if ( ! empty ( $AltBody ))
{
$mailObject -> IsHTML ( true );
}
elseif ( empty ( $AltBody ) && $mailObject -> BodyContentType == 'text/html' )
{
$mailObject -> IsHTML ( true );
2013-10-05 11:03:51 +02:00
$AltBody = translation :: convertHTMLToText ( $Body , self :: $displayCharset , false , $stripalltags = true );
2013-09-05 16:52:11 +02:00
}
else
{
$mailObject -> IsHTML ( false );
}
//error_log(__METHOD__.__LINE__.' ContentType:'.$mailObject->BodyContentType);
if ( ! empty ( $Body )) $mailObject -> Body = $bo_merge -> merge_string ( $Body , $val , $e , $mailObject -> BodyContentType , array (), self :: $displayCharset );
//error_log(__METHOD__.__LINE__.' Result:'.$mailObject->Body.' error:'.array2string($e));
if ( ! empty ( $AltBody )) $mailObject -> AltBody = $bo_merge -> merge_string ( $AltBody , $val , $e , $mailObject -> AltBodyContentType , array (), self :: $displayCharset );
2014-01-15 17:29:30 +01:00
$ogServer = emailadmin_account :: read ( $this -> profileID ) -> smtpServer ();
2013-09-05 16:52:11 +02:00
#_debug_array($ogServer);
$mailObject -> Host = $ogServer -> host ;
$mailObject -> Port = $ogServer -> port ;
// SMTP Auth??
if ( $ogServer -> smtpAuth ) {
$mailObject -> SMTPAuth = true ;
// check if username contains a ; -> then a sender is specified (and probably needed)
list ( $username , $senderadress ) = explode ( ';' , $ogServer -> username , 2 );
if ( isset ( $senderadress ) && ! empty ( $senderadress )) $mailObject -> Sender = $senderadress ;
$mailObject -> Username = $username ;
$mailObject -> Password = $ogServer -> password ;
}
//error_log(__METHOD__.__LINE__.array2string($mailObject));
// set a higher timeout for big messages
@ set_time_limit ( 120 );
$sendOK = true ;
try {
$mailObject -> Send ();
}
catch ( phpmailerException $e ) {
$sendOK = false ;
$errorInfo = $e -> getMessage ();
if ( $mailObject -> ErrorInfo ) // use the complete mailer ErrorInfo, for full Information
{
if ( stripos ( $mailObject -> ErrorInfo , $errorInfo ) === false )
{
$errorInfo = 'Send Failed for ' . $mailObject -> Subject . ' to ' . $nfn . '<' . $email . '> Error:' . $mailObject -> ErrorInfo . '<br>' . $errorInfo ;
}
else
{
2014-01-16 15:05:07 +01:00
$errorInfo = 'Send Failed for ' . $mailObject -> Subject . ' to ' . $nfn . '<' . $email . '> Error:' . $mailObject -> ErrorInfo ;
2013-09-05 16:52:11 +02:00
}
}
//error_log(__METHOD__.__LINE__.array2string($errorInfo));
}
}
elseif ( ! $k ) // 1. entry, further entries will fail for apps other then addressbook
{
$openAsDraft = true ;
$mailObject -> MessageID = '' ;
$mailObject -> ClearAllRecipients ();
$mailObject -> ClearCustomHeaders ();
2014-01-17 11:47:35 +01:00
if ( /*$GLOBALS['egw_info']['flags']['currentapp'] == 'addressbook' &&*/
2013-09-05 16:52:11 +02:00
is_numeric ( $val ) || $GLOBALS [ 'egw' ] -> accounts -> name2id ( $val )) // do the merge
{
$contact = $bo_merge -> contacts -> read ( $val );
//error_log(__METHOD__.__LINE__.array2string($contact));
$email = ( $contact [ 'email' ] ? $contact [ 'email' ] : $contact [ 'email_home' ]);
$nfn = ( $contact [ 'n_fn' ] ? $contact [ 'n_fn' ] : $contact [ 'n_given' ] . ' ' . $contact [ 'n_family' ]);
$mailObject -> AddAddress ( self :: $idna2 -> encode ( $email ), $mailObject -> EncodeHeader ( $nfn ));
}
$mailObject -> Subject = $bo_merge -> merge_string ( $Subject , $val , $e , 'text/plain' , array (), self :: $displayCharset );
if ( ! empty ( $AltBody ))
{
$mailObject -> IsHTML ( true );
}
elseif ( empty ( $AltBody ) && $mailObject -> BodyContentType == 'text/html' )
{
$mailObject -> IsHTML ( true );
2013-10-05 11:03:51 +02:00
$AltBody = translation :: convertHTMLToText ( $Body , self :: $displayCharset , false , $stripalltags = true );
2013-09-05 16:52:11 +02:00
}
else
{
$mailObject -> IsHTML ( false );
}
//error_log(__METHOD__.__LINE__.' ContentType:'.$mailObject->BodyContentType);
if ( ! empty ( $Body )) $mailObject -> Body = $bo_merge -> merge_string ( $Body , $val , $e , $mailObject -> BodyContentType , array (), self :: $displayCharset );
//error_log(__METHOD__.__LINE__.' Result:'.$mailObject->Body.' error:'.array2string($e));
if ( ! empty ( $AltBody )) $mailObject -> AltBody = $bo_merge -> merge_string ( $AltBody , $val , $e , $mailObject -> AltBodyContentType , array (), self :: $displayCharset );
$_folder = $this -> getDraftFolder ();
}
if ( $sendOK || $openAsDraft )
{
$BCCmail = '' ;
if ( $this -> folderExists ( $_folder , true ))
{
if ( $this -> isSentFolder ( $_folder ))
{
$flags = '\\Seen' ;
} elseif ( $this -> isDraftFolder ( $_folder )) {
$flags = '\\Draft' ;
} else {
$flags = '' ;
}
unset ( $mailObject -> sentHeader );
unset ( $mailObject -> sentBody );
$savefailed = false ;
try
{
$messageUid = $this -> appendMessage ( $_folder ,
$BCCmail . $mailObject -> getMessageHeader (),
$mailObject -> getMessageBody (),
$flags );
}
catch ( egw_exception_wrong_userinput $e )
{
$savefailed = true ;
$alert_msg .= lang ( " Save of message %1 failed. Could not save message to folder %2 due to: %3 " , $Subject , $_folder , $e -> getMessage ());
}
// no send, save successful, and message_uid present
if ( $savefailed === false && $messageUid && is_null ( $sendOK ))
{
2014-01-15 17:29:30 +01:00
$importID = $messageUid ;
2013-09-05 16:52:11 +02:00
$openComposeWindow = true ;
}
}
else
{
$savefailed = true ;
$alert_msg .= lang ( " Saving of message %1 failed. Destination Folder %2 does not exist. " , $Subject , $_folder );
}
if ( $sendOK )
{
$processStats [ 'success' ][ $val ] = 'Send succeeded to ' . $nfn . '<' . $email . '>' . ( $savefailed ? ' but failed to store to Folder:' . $_folder : '' );
}
else
{
if ( ! $openComposeWindow ) $processStats [ 'failed' ][ $val ] = $errorInfo ? $errorInfo : 'Send failed to ' . $nfn . '<' . $email . '> See error_log for details' ;
}
}
if ( ! is_null ( $sendOK ) && $sendOK === false && is_null ( $openComposeWindow ))
{
$processStats [ 'failed' ][ $val ] = $errorInfo ? $errorInfo : 'Send failed to ' . $nfn . '<' . $email . '> See error_log for details' ;
}
}
}
unset ( $mailObject );
}
// set the url to open when refreshing
if ( $importfailed == true )
{
throw new egw_exception_wrong_userinput ( $alert_msg );
}
else
{
//error_log(__METHOD__.__LINE__.array2string($processStats));
return $processStats ;
}
}
/**
* functions to allow the parsing of message / rfc files
* used in felamimail to import mails , or parsev a message from file enrich it with addressdata ( merge ) and send it right away .
*/
/**
* parseFileIntoMailObject - parses a message / rfc mail from file to the mailobject and returns the header and body via reference
* throws egw_exception_assertion_failed when the required Pear Class is not found / loadable
* @ param object $mailObject instance of the SMTP Mailer Object
* @ param string $tmpFileName string that points / leads to the file to be imported
* @ param string & $Header reference used to return the imported Mailheader
* @ param string & $Body reference to return the imported Body
* @ return void Mailheader and body is returned via Reference in $Header $Body
*/
function parseFileIntoMailObject ( $mailObject , $tmpFileName , & $Header , & $Body )
{
$message = file_get_contents ( $tmpFileName );
try
{
return $this -> parseRawMessageIntoMailObject ( $mailObject , $message , $Header , $Body );
}
catch ( egw_exception_assertion_failed $e )
{ // not sure that this is needed to pass on exeptions
throw new egw_exception_assertion_failed ( $e -> getMessage ());
}
}
/**
* parseRawMessageIntoMailObject - parses a message / rfc mail from file to the mailobject and returns the header and body via reference
* throws egw_exception_assertion_failed when the required Pear Class is not found / loadable
* @ param object $mailObject instance of the SMTP Mailer Object
* @ param string $message string containing the RawMessage
* @ param string & $Header reference used to return the imported Mailheader
* @ param string & $Body reference to return the imported Body
* @ return void Mailheader and body is returned via Reference in $Header $Body
*/
function parseRawMessageIntoMailObject ( $mailObject , $message , & $Header , & $Body )
{
/**
* pear / Mail_mimeDecode requires package " pear/Mail_Mime " ( version >= 1.4 . 0 , excluded versions : 1.4 . 0 )
* ./ pear upgrade Mail_Mime
* ./ pear install Mail_mimeDecode
*/
//echo '<pre>'.$message.'</pre>';
//error_log(__METHOD__.__LINE__.$message);
if ( class_exists ( 'Mail_mimeDecode' , false ) == false && ( @ include_once 'Mail/mimeDecode.php' ) === false ) throw new egw_exception_assertion_failed ( lang ( 'Required PEAR class Mail/mimeDecode.php not found.' ));
$mailDecode = new Mail_mimeDecode ( $message );
$structure = $mailDecode -> decode ( array ( 'include_bodies' => true , 'decode_bodies' => true , 'decode_headers' => true ));
//error_log(__METHOD__.__LINE__.array2string($structure));
//_debug_array($structure);
//exit;
// now create a message to view, save it in Drafts and open it
$mailObject -> PluginDir = EGW_SERVER_ROOT . " /phpgwapi/inc/ " ;
$mailObject -> IsSMTP ();
$mailObject -> CharSet = self :: $displayCharset ; // some default, may be altered by BodyImport
if ( isset ( $structure -> ctype_parameters [ 'charset' ])) $mailObject -> CharSet = trim ( $structure -> ctype_parameters [ 'charset' ]);
$mailObject -> Encoding = 'quoted-printable' ; // some default, may be altered by BodyImport
/*
$mailObject -> AddAddress ( $emailAddress , $addressObject -> personal );
$mailObject -> AddCC ( $emailAddress , $addressObject -> personal );
$mailObject -> AddBCC ( $emailAddress , $addressObject -> personal );
$mailObject -> AddReplyto ( $emailAddress , $addressObject -> personal );
*/
$result = '' ;
$contenttypecalendar = '' ;
$myReplyTo = '' ;
foreach (( array ) $structure -> headers as $key => $val )
{
//error_log(__METHOD__.__LINE__.$key.'->'.$val);
foreach (( array ) $val as $i => $v )
{
if ( $key != 'content-type' && $key != 'content-transfer-encoding' &&
$key != 'message-id' &&
$key != 'subject' &&
$key != 'from' &&
$key != 'to' &&
$key != 'cc' &&
$key != 'bcc' &&
$key != 'reply-to' &&
$key != 'x-priority' ) // the omitted values to that will be set at the end
{
$Header .= $mailObject -> HeaderLine ( $key , trim ( $v ));
}
}
switch ( $key )
{
case 'x-priority' :
$mailObject -> Priority = $val ;
break ;
case 'message-id' :
$mailObject -> MessageID = $val ; // ToDo: maybe we want to regenerate the message id all the time
break ;
case 'sender' :
$mailObject -> Sender = $val ;
break ;
case 'to' :
case 'cc' :
case 'bcc' :
case 'from' :
case 'reply-to' :
$address_array = imap_rfc822_parse_adrlist (( get_magic_quotes_gpc () ? stripslashes ( $val ) : $val ), '' );
$i = 0 ;
foreach (( array ) $address_array as $addressObject )
{
$mb = $addressObject -> mailbox . ( ! empty ( $addressObject -> host ) ? '@' . $addressObject -> host : '' );
$pName = $addressObject -> personal ;
if ( $key == 'from' )
{
$mailObject -> From = $mb ;
$mailObject -> FromName = $pName ;
}
${$key} [ $i ] = array ( $mb , $pName );
$i ++ ;
}
if ( $key == 'reply-to' )
{
$myReplyTo = ${$key} ;
//break; // break early as we add that later
}
$Header .= $mailObject -> TextLine ( trim ( $mailObject -> AddrAppend ( ucfirst ( $key ), ${$key} )));
break ;
case 'content-transfer-encoding' :
$mailObject -> Encoding = $val ;
break ;
case 'content-type' :
//error_log(__METHOD__.__LINE__.' '.$key.'->'.$val);
if ( stripos ( $val , 'calendar' )) $contenttypecalendar = $val ;
break ;
case 'subject' :
$mailObject -> Subject = $mailObject -> EncodeHeader ( $mailObject -> SecureHeader ( $val ));
$Header .= $mailObject -> HeaderLine ( 'Subject' , $mailObject -> Subject );
break ;
default :
// stuff like X- ...
//$mailObject->AddCustomHeader('X-Mailer: FeLaMiMail');
if ( ! strtolower ( substr ( $key , 0 , 2 )) == 'x-' ) break ;
//case 'priority': // priority is a cusom header field
// $mailObject->Priority = $val;
// break;
case 'disposition-notification-To' :
case 'organization' :
foreach (( array ) $val as $i => $v ) $mailObject -> AddCustomHeader ( $key . ': ' . $v );
break ;
}
}
// handle reply-to, wich may be set, set the first one found
if ( ! empty ( $myReplyTo ))
{
$mailObject -> ClearReplyTos ();
$mailObject -> AddReplyTo ( $myReplyTo [ 0 ][ 0 ], $myReplyTo [ 0 ][ 1 ]);
}
$seemsToBePlainMessage = false ;
if ( strtolower ( $structure -> ctype_primary ) == 'text' && $structure -> body )
{
$mailObject -> IsHTML ( strtolower ( $structure -> ctype_secondary ) == 'html' ? true : false );
if ( strtolower ( $structure -> ctype_primary ) == 'text' && strtolower ( $structure -> ctype_secondary ) == 'plain' &&
is_array ( $structure -> ctype_parameters ) && isset ( $structure -> ctype_parameters [ 'format' ]) &&
trim ( strtolower ( $structure -> ctype_parameters [ 'format' ])) == 'flowed'
)
{
if ( self :: $debug ) error_log ( __METHOD__ . __LINE__ . " detected TEXT/PLAIN Format:flowed -> removing leading blank (' \r \n ') per line " );
$structure -> body = str_replace ( " \r \n " , " \r \n " , $structure -> body );
}
$mailObject -> Body = $structure -> body ;
$seemsToBePlainMessage = true ;
}
$this -> createBodyFromStructure ( $mailObject , $structure , $parenttype = null );
$mailObject -> SetMessageType ();
$mailObject -> CreateHeader (); // this sets the boundary stufff
//echo "Boundary:".$mailObject->FetchBoundary(1).'<br>';
//$boundary ='';
//if (isset($structure->ctype_parameters['boundary'])) $boundary = ' boundary="'.$mailObject->FetchBoundary(1).'";';
if ( $seemsToBePlainMessage && ! empty ( $contenttypecalendar ) && strtolower ( $mailObject -> ContentType ) == 'text/plain' )
{
$Header .= $mailObject -> HeaderLine ( 'Content-Transfer-Encoding' , $mailObject -> Encoding );
$Header .= $mailObject -> HeaderLine ( 'Content-type' , $contenttypecalendar );
}
else
{
$Header .= $mailObject -> GetMailMIME ();
}
$Body = $mailObject -> getMessageBody (); // this is a method of the egw_mailer/phpmailer class
//_debug_array($Header);
//_debug_array($Body);
//_debug_array($mailObject);
//exit;
}
/**
* createBodyFromStructure - fetches / creates the bodypart of the email as textual representation
* is called recursively to be able to fetch the stuctureparts of the mail parsed from Mail / mimeDecode
* @ param object $mailObject instance of the SMTP Mailer Object
* @ param array $structure array that represents structure and content of a mail parsed from Mail / mimeDecode
* @ param string $parenttype type of the parent node
* @ return void Parsed Information is passed to the mailObject to be processed there
*/
function createBodyFromStructure ( $mailObject , $structure , $parenttype = null , $decode = false )
{
static $attachmentnumber ;
static $isHTML ;
static $alternatebodyneeded ;
if ( is_null ( $isHTML )) $isHTML = strtolower ( $structure -> ctype_secondary ) == 'html' ? true : false ;
if ( is_null ( $attachmentnumber )) $attachmentnumber = 0 ;
if ( $structure -> parts && strtolower ( $structure -> ctype_primary ) == 'multipart' )
{
if ( is_null ( $alternatebodyneeded )) $alternatebodyneeded = false ;
foreach ( $structure -> parts as $part )
{
//error_log(__METHOD__.__LINE__.' Structure Content Type:'.$structure->ctype_primary.'/'.$structure->ctype_secondary.' Decoding:'.($decode?'on':'off'));
//error_log(__METHOD__.__LINE__.' '.$structure->ctype_primary.'/'.$structure->ctype_secondary.' => '.$part->ctype_primary.'/'.$part->ctype_secondary);
//error_log(__METHOD__.__LINE__.' Part:'.array2string($part));
$partFetched = false ;
//echo __METHOD__.__LINE__.$structure->ctype_primary.'/'.$structure->ctype_secondary.'<br>';
if ( $part -> headers [ 'content-transfer-encoding' ]) $mailObject -> Encoding = $part -> headers [ 'content-transfer-encoding' ];
//$mailObject->IsHTML($part->ctype_secondary=='html'?true:false); // we do not set this here, as the default is text/plain
if ( isset ( $part -> ctype_parameters [ 'charset' ])) $mailObject -> CharSet = trim ( $part -> ctype_parameters [ 'charset' ]);
if (( strtolower ( $structure -> ctype_secondary ) == 'alternative' ||
strtolower ( $structure -> ctype_secondary ) == 'mixed' ||
// strtolower($structure->ctype_secondary)=='related' || // may hold text/plain directly ?? I doubt it ??
strtolower ( $structure -> ctype_secondary ) == 'signed' ) && strtolower ( $part -> ctype_primary ) == 'text' && strtolower ( $part -> ctype_secondary ) == 'plain' && $part -> body )
{
//echo __METHOD__.__LINE__.$part->ctype_primary.'/'.$part->ctype_secondary.'<br>';
//error_log(__METHOD__.__LINE__.$part->ctype_primary.'/'.$part->ctype_secondary.' already fetched Content is HTML='.$isHTML.' Body:'.$part->body);
$bodyPart = $part -> body ;
if ( $decode ) $bodyPart = $this -> decodeMimePart ( $part -> body ,( $part -> headers [ 'content-transfer-encoding' ] ? $part -> headers [ 'content-transfer-encoding' ] : 'base64' ));
/*
if ( strtolower ( $part -> ctype_primary ) == 'text' && strtolower ( $part -> ctype_secondary ) == 'plain' &&
is_array ( $part -> ctype_parameters ) && isset ( $part -> ctype_parameters [ 'format' ]) &&
trim ( strtolower ( $part -> ctype_parameters [ 'format' ])) == 'flowed'
)
{
if ( self :: $debug ) error_log ( __METHOD__ . __LINE__ . " detected TEXT/PLAIN Format:flowed -> removing leading blank (' \r \n ') per line " );
$bodyPart = str_replace ( " \r \n " , " \r \n " , $bodyPart );
}
*/
$mailObject -> Body = ( $isHTML == false ? $mailObject -> Body : '' ) . $bodyPart ;
$mailObject -> AltBody .= $bodyPart ;
$partFetched = true ;
}
if (( strtolower ( $structure -> ctype_secondary ) == 'alternative' ||
strtolower ( $structure -> ctype_secondary ) == 'mixed' ||
strtolower ( $structure -> ctype_secondary ) == 'related' || // may hold text/html directly
strtolower ( $structure -> ctype_secondary ) == 'signed' ) &&
strtolower ( $part -> ctype_primary ) == 'text' && strtolower ( $part -> ctype_secondary ) == 'html' && $part -> body )
{
//echo __METHOD__.__LINE__.$part->ctype_primary.'/'.$part->ctype_secondary.'<br>';
//error_log(__METHOD__.__LINE__.$part->ctype_primary.'/'.$part->ctype_secondary.' already fetched Content is HTML='.$isHTML.' Body:'.$part->body);
$bodyPart = $part -> body ;
if ( $decode ) $bodyPart = $this -> decodeMimePart ( $part -> body ,( $part -> headers [ 'content-transfer-encoding' ] ? $part -> headers [ 'content-transfer-encoding' ] : 'base64' ));
$mailObject -> IsHTML ( true ); // we need/want that here, because looping through all message parts may mess up the message body mimetype
$mailObject -> Body = ( $isHTML ? $mailObject -> Body : '' ) . $bodyPart ;
$alternatebodyneeded = true ;
$isHTML = true ;
$partFetched = true ;
}
if (( strtolower ( $structure -> ctype_secondary ) == 'alternative' ||
strtolower ( $structure -> ctype_secondary ) == 'mixed' ||
strtolower ( $structure -> ctype_secondary ) == 'signed' ) &&
strtolower ( $part -> ctype_primary ) == 'text' && strtolower ( $part -> ctype_secondary ) == 'calendar' && $part -> body )
{
//error_log(__METHOD__.__LINE__.$part->ctype_primary.'/'.$part->ctype_secondary.' BodyPart:'.array2string($part));
$bodyPart = $part -> body ;
if ( $decode ) $bodyPart = $this -> decodeMimePart ( $part -> body ,( $part -> headers [ 'content-transfer-encoding' ] ? $part -> headers [ 'content-transfer-encoding' ] : 'base64' ));
$mailObject -> AltExtended = $bodyPart ;
// "text/calendar; charset=utf-8; name=meeting.ics; method=REQUEST"
// [ctype_parameters] => Array([charset] => utf-8[name] => meeting.ics[method] => REQUEST)
$mailObject -> AltExtendedContentType = $part -> ctype_primary . '/' . $part -> ctype_secondary . ';' .
( $part -> ctype_parameters [ 'name' ] ? ' name=' . $part -> ctype_parameters [ 'name' ] . ';' : '' ) .
( $part -> ctype_parameters [ 'method' ] ? ' method=' . $part -> ctype_parameters [ 'method' ] . '' : '' );
$partFetched = true ;
}
if (( strtolower ( $structure -> ctype_secondary ) == 'mixed' ||
strtolower ( $structure -> ctype_secondary ) == 'related' ||
strtolower ( $structure -> ctype_secondary ) == 'alternative' ||
strtolower ( $structure -> ctype_secondary ) == 'signed' ) && strtolower ( $part -> ctype_primary ) == 'multipart' )
{
//error_log( __METHOD__.__LINE__." Recursion to fetch subparts:".$part->ctype_primary.'/'.$part->ctype_secondary);
$this -> createBodyFromStructure ( $mailObject , $part , $parenttype = null , $decode );
}
//error_log(__METHOD__.__LINE__.$structure->ctype_primary.'/'.$structure->ctype_secondary.' => '.$part->ctype_primary.'/'.$part->ctype_secondary.' Part:'.array2string($part));
if ( $part -> body && (( strtolower ( $structure -> ctype_secondary ) == 'mixed' && strtolower ( $part -> ctype_primary ) != 'multipart' ) ||
trim ( strtolower ( $part -> disposition )) == 'attachment' ||
trim ( strtolower ( $part -> disposition )) == 'inline' ||
isset ( $part -> headers [ 'content-id' ])))
{
//error_log(__METHOD__.__LINE__.$structure->ctype_secondary.'=>'.$part->ctype_primary.'/'.$part->ctype_secondary.'->'.array2string($part));
$attachmentnumber ++ ;
$filename = trim (( $part -> ctype_parameters [ 'name' ] ? $part -> ctype_parameters [ 'name' ] : $part -> d_parameters [ 'filename' ]));
if ( strlen ( $filename ) == 0 )
{
//error_log(__METHOD__.__LINE__.$structure->ctype_secondary.'=>'.$part->ctype_primary.'/'.$part->ctype_secondary.'->'.array2string($part));
foreach ( array ( 'content-type' , 'content-disposition' ) as $k => $v )
{
foreach ( array ( 'filename' , 'name' ) as $sk => $n )
{
if ( stripos ( $part -> headers [ $v ], $n ) !== false )
{
$buff = explode ( $n , $part -> headers [ $v ]);
//error_log(__METHOD__.__LINE__.array2string($buff));
$namepart = array_pop ( $buff );
//$disposition = array_pop($buff);
//error_log(__METHOD__.__LINE__.$namepart);
$fp = strpos ( $namepart , '"' );
//error_log(__METHOD__.__LINE__.' Start:'.$fp);
if ( $fp !== false )
{
$np = strpos ( $namepart , '"' , $fp + 1 );
//error_log(__METHOD__.__LINE__.' End:'.$np);
if ( $np !== false )
{
$filename = trim ( substr ( $namepart , $fp + 1 , $np - $fp - 1 ));
$filename = $mailObject -> EncodeHeader ( $filename );
if ( ! empty ( $filename ))
{
if ( strpos ( $part -> disposition , ';' ) !== false )
{
//chance is, disposition is broken too
$dbuff = explode ( ';' , $part -> disposition );
$part -> disposition = trim ( $dbuff [ 0 ]);
}
break 2 ;
}
}
}
}
}
}
}
if ( strlen ( $filename ) == 0 ) $filename = 'noname_' . $attachmentnumber ;
//error_log(__METHOD__.__LINE__.' '.$filename);
//echo $part->headers['content-transfer-encoding'].'#<br>';
if ( $decode ) $part -> body = $this -> decodeMimePart ( $part -> body ,( $part -> headers [ 'content-transfer-encoding' ] ? $part -> headers [ 'content-transfer-encoding' ] : 'base64' ));
if (( trim ( strtolower ( $part -> disposition )) == 'attachment' || trim ( strtolower ( $part -> disposition )) == 'inline' || isset ( $part -> headers [ 'content-id' ])) && $partFetched == false )
{
if ( trim ( strtolower ( $part -> disposition )) == 'inline' || $part -> headers [ 'content-id' ])
{
$part -> headers [ 'content-id' ] = str_replace ( array ( '<' , '>' ), '' , $part -> headers [ 'content-id' ]);
$dirname = $this -> accountid . '_' . $this -> profileID . '_' . $this -> sessionData [ 'mailbox' ] . $part -> headers [ 'content-id' ];
if ( self :: $debug ) error_log ( __METHOD__ . __LINE__ . ' Dirname:' . $dirname );
$dirname = md5 ( $dirname );
$dir = $GLOBALS [ 'egw_info' ][ 'server' ][ 'temp_dir' ] . " /fmail_import/ $dirname " ;
if ( self :: $debug ) error_log ( __METHOD__ . __LINE__ . ' Dir to save attachment to:' . $dir );
if ( ! file_exists ( " $dir " ) )
{
@ mkdir ( $dir , 0700 , true );
}
$rp = mail_bo :: getRandomString ();
file_put_contents ( " $dir / $rp $filename " , $part -> body );
2013-11-06 22:02:19 +01:00
2013-09-05 16:52:11 +02:00
$path = " $dir / $rp $filename " ;
$mailObject -> AddEmbeddedImage ( $path , $part -> headers [ 'content-id' ], $filename , ( $part -> headers [ 'content-transfer-encoding' ] ? $part -> headers [ 'content-transfer-encoding' ] : 'base64' ), $part -> ctype_primary . '/' . $part -> ctype_secondary );
}
else
{
//error_log(__METHOD__.__LINE__.' Add String '.($part->disposition=='attachment'?'Attachment':'Part').' of type:'.$part->ctype_primary.'/'.$part->ctype_secondary);
$mailObject -> AddStringAttachment ( $part -> body , //($part->headers['content-transfer-encoding']?base64_decode($part->body):$part->body),
$filename ,
( $part -> headers [ 'content-transfer-encoding' ] ? $part -> headers [ 'content-transfer-encoding' ] : 'base64' ),
$part -> ctype_primary . '/' . $part -> ctype_secondary
);
}
}
if ( ! ( trim ( strtolower ( $part -> disposition )) == 'attachment' || trim ( strtolower ( $part -> disposition )) == 'inline' || isset ( $part -> headers [ 'content-id' ])) && $partFetched == false )
{
//error_log(__METHOD__.__LINE__.' Add String '.($part->disposition=='attachment'?'Attachment':'Part').' of type:'.$part->ctype_primary.'/'.$part->ctype_secondary.' Body:'.$part->body);
$mailObject -> AddStringPart ( $part -> body , //($part->headers['content-transfer-encoding']?base64_decode($part->body):$part->body),
$filename ,
( $part -> headers [ 'content-transfer-encoding' ] ? $part -> headers [ 'content-transfer-encoding' ] : 'base64' ),
$part -> ctype_primary . '/' . $part -> ctype_secondary
);
}
}
}
if ( $alternatebodyneeded == false ) $mailObject -> AltBody = '' ;
}
}
2013-02-19 17:30:59 +01:00
/**
* Hook stuff
*/
2013-02-08 20:11:44 +01:00
/**
* hook to add account
*
* this function is a wrapper function for emailadmin
*
* @ param _hookValues contains the hook values as array
* @ return nothing
*/
function addAccount ( $_hookValues )
{
2014-01-10 11:33:53 +01:00
error_log ( __METHOD__ . __LINE__ . ' NOT DONE JET!' );
//$_profile_id=????
//$icServer = emailadmin_account::read($_profile_id)->imapServer();
//$ogServer = emailadmin_account::read($_profile_id)->smtpServer();
2013-02-08 20:11:44 +01:00
2014-01-10 11:33:53 +01:00
/*
if (( $icServer instanceof defaultimap )) {
// if not connected, try opening an admin connection
if ( ! $icServer -> _connected ) $this -> openConnection ( $this -> profileID , true );
$icServer -> addAccount ( $_hookValues );
if ( $icServer -> _connected ) $this -> closeConnection (); // close connection afterwards
2013-02-08 20:11:44 +01:00
}
2014-01-10 11:33:53 +01:00
if (( $ogServer instanceof emailadmin_smtp )) {
$ogServer -> addAccount ( $_hookValues );
}
*/
2013-02-08 20:11:44 +01:00
}
/**
* hook to delete account
*
* this function is a wrapper function for emailadmin
*
* @ param _hookValues contains the hook values as array
* @ return nothing
*/
function deleteAccount ( $_hookValues )
{
2014-01-10 11:33:53 +01:00
error_log ( __METHOD__ . __LINE__ . ' NOT DONE JET!' );
//$_profile_id=????
//$icServer = emailadmin_account::read($_profile_id)->imapServer();
//$ogServer = emailadmin_account::read($_profile_id)->smtpServer();
/*
if (( $icServer instanceof defaultimap )) {
//try to connect with admin rights, when not connected
if ( ! $icServer -> _connected ) $this -> openConnection ( $this -> profileID , true );
$icServer -> deleteAccount ( $_hookValues );
if ( $icServer -> _connected ) $this -> closeConnection (); // close connection
}
2013-02-08 20:11:44 +01:00
2014-01-10 11:33:53 +01:00
if (( $ogServer instanceof emailadmin_smtp )) {
$ogServer -> deleteAccount ( $_hookValues );
2013-02-08 20:11:44 +01:00
}
2014-01-10 11:33:53 +01:00
*/
2013-02-08 20:11:44 +01:00
}
/**
* hook to update account
*
* this function is a wrapper function for emailadmin
*
* @ param _hookValues contains the hook values as array
* @ return nothing
*/
function updateAccount ( $_hookValues )
{
2014-01-10 11:33:53 +01:00
error_log ( __METHOD__ . __LINE__ . ' NOT DONE JET!' );
//$_profile_id=????
//$icServer = emailadmin_account::read($_profile_id)->imapServer();
//$ogServer = emailadmin_account::read($_profile_id)->smtpServer();
/*
2013-02-08 20:11:44 +01:00
if (( $icServer instanceof defaultimap )) {
$icServer -> updateAccount ( $_hookValues );
}
if (( $ogServer instanceof emailadmin_smtp )) {
$ogServer -> updateAccount ( $_hookValues );
}
2014-01-10 11:33:53 +01:00
*/
2013-02-08 20:11:44 +01:00
}
}