Location: PHPKode > scripts > AfterLogic WebMail Lite PHP > afterlogic-webmail-lite-php/webmail/common/class_xmailstorage.php
<?php

/*
 * AfterLogic WebMail Pro PHP by AfterLogic Corp. <hide@address.com>
 *
 * Copyright (C) 2002-2010  AfterLogic Corp. (www.afterlogic.com)
 * Distributed under the terms of the license described in COPYING
 * 
 */

	defined('WM_ROOTPATH') || define('WM_ROOTPATH', (dirname(__FILE__) . '/'));
	
	require_once(WM_ROOTPATH.'common/class_mailstorage.php');
	require_once(WM_ROOTPATH.'common/class_account.php');
	require_once(WM_ROOTPATH.'common/class_folders.php');
	require_once(WM_ROOTPATH.'common/class_webmailmessages.php');
	require_once(WM_ROOTPATH.'common/wmserver/class_wmserver_fs.php');

	class XMailStorage extends MailServerStorage
	{
		/**
		 * @access private
		 * @var WMserverFS
		 */
		var $_wmserver;
		
		/**
		 * @access private
		 * @var CWmServerConsole
		 */
		var $_wmadmin;
		
		/**
		 * @access private
		 * @var CLog
		 */
		var $_log;	

		/**
		 * @access public
		 * @param Account $account
		 * @return XMailStorage
		 */
		function XMailStorage(&$account, &$mp)
		{
			$this->mailproc =& $mp;
			
			MailServerStorage::MailServerStorage($account);
				
			$email = $account->Email;
			$basePath = $this->_settings->WmServerRootPath;
				
			$this->_wmserver = new WMserverFS($basePath, $email);
			$this->_wmadmin = new CWmServerConsole();
			$this->_log =& CLog::CreateInstance();
		}
		
		/**
		 * @param $arg[optional] = false
		 * @return bool
		 */
		function Connect($arg = false)
		{
			if ($this->_wmadmin->_socket || !$arg)
			{
				return true;
			}

			if (!@is_dir($this->_wmserver->basePath) || !$this->_wmadmin->Connect())
			{
				/* setGlobalError($this->_wmadmin->GetError()); */
				setGlobalError(ErrorPOP3Connect);
				return false;
			}
			else
			{
				register_shutdown_function(array(&$this, 'Disconnect'));
			}

			if (!$this->_wmadmin->UserConnect($this->Account))
			{
				/* setGlobalError($this->_wmadmin->GetError()); */
				setGlobalError(ErrorPOP3IMAP4Auth);
				return false;
			}
			
			return true;
		}
		
		/**
		 * @return bool
		 */
		function Disconnect()
		{
			return $this->_wmadmin->Disconnect();
		}

		/**
		 * @return FolderCollection
		 */
		function &GetFolders()
		{
			$folderCollection = new FolderCollection();
			$folders = $this->_wmserver->getFolders($this->Account->Delimiter);

			$existsIndex = array();
			$folderCollection = $this->GetFolderCollectionFromArrays($folders, $folders, $this->Account->Delimiter, $existsIndex);
		
			/* custom class */
			wm_Custom::StaticUseMethod('ChangeServerXMailFoldersAfterGet', array(&$folderCollection));
						
			return $folderCollection;
		}
		
		/**
		 * @param Folder $folder
		 * @return bool
		 */
		function CreateFolder(&$folder)
		{
			return $this->_wmserver->createMailbox($folder->FullName);
		}
		
		/**
		 * @param Folder $folder
		 * @return bool
		 */
		function GetFolderMessageCount(&$folder)
		{
			$countArray = $this->_wmserver->getAllAndUreadMsgCount($folder->FullName);
			if($countArray && count($countArray) == 2)
			{
				$folder->MessageCount = $countArray[0];
				$folder->UnreadMessageCount = $countArray[1];
				return true;
			}
			return false;
		}
		
		/**
		 * @param array $messageUidSet
		 * @param bool $indexAsUid
		 * @param Folder $folder
		 * @param int $flags
		 * @param short $action
		 * @return bool
		 */
		function SetMessagesFlags($messageUidSet, $indexAsUid, $folder, $flags, $action)
		{
			if ($indexAsUid)
			{
				$params = $this->_wmserver->getParamsMessages($folder->FullName);
				if (isset($params['u']))
				{
					if ($messageUidSet === null)
					{
						foreach ($params['u'] as $uid => $value)
						{
							$this->_wmserver->setFlags($value['filename'], $folder, $flags, $action);
						}
						
						return true;
					}
					else
					{
						foreach ($messageUidSet as $uid)
						{
							if (isset($params['u'][$uid]['filename']))
							{
								$this->_wmserver->setFlags($params['u'][$uid]['filename'], $folder, $flags, $action);
							}
						}
						
						return true;
					}
				}
			}
			
			return false;
		}

		/**
		 * @param array $messageUidSet
		 * @param bool $indexAsUid
		 * @param Folder $folder
		 * @param int $flags
		 * @param short $action
		 * @return bool
		 */
		function SetMessagesFlag($Uid, $folder, $flags, $action)
		{
			$fileName = $this->_wmserver->getFileNameByUid($Uid, $folder->FullName);
			if ($fileName !== false)
			{
				$this->_wmserver->setFlags($fileName, $folder, $flags, $action);
				return true;
			}
			
			return false;
		}
		
		/**
		 * @param FolderCollection $folders
		 * @return bool
		 */
		function Synchronize(&$folders)
		{
			$result = true;
			$dbStorage =& DbStorageCreator::CreateDatabaseStorage($this->Account);
			if($dbStorage->Connect())
			{
				$folderList = $folders->CreateFolderListFromTree();
				for($i = 0, $icount = $folderList->Count(); $i < $icount; $i++)
				{
					$folder =& $folderList->Get($i);
					$result &= $this->_synchronizeFolderWithOpenDbConnection($folder, $dbStorage);
					unset($folder);
				}
				return $result;
			}
			return false;
		}
		
		/**
		 * @param Folder $folder
		 * @return bool
		 */
		function SynchronizeFolder(&$folder)
		{
			$dbStorage = &DbStorageCreator::CreateDatabaseStorage($this->Account);
			if($dbStorage->Connect())
			{
				return $this->_synchronizeFolderWithOpenDbConnection($folder, $dbStorage);
			}
			return false;
		}
		
		/**
		 * @return bool
		 */
		function SynchronizeFolders()
		{
			$result = true;
			$dbStorage =& DbStorageCreator::CreateDatabaseStorage($this->Account);
			$serverFoldersTree = &$this->GetFolders();
			if($serverFoldersTree != null && $dbStorage->Connect())
			{
				$dbFoldersTree =& $dbStorage->GetFolders();
				$serverFoldersList =& $serverFoldersTree->CreateFolderListFromTree();
				$dbFoldersList =& $dbFoldersTree->CreateFolderListFromTree();
				$delimiter = $this->Account->Delimiter;
				
				$serverFoldersListKeys = array_keys($serverFoldersList->Instance());
				$dbFoldersListKeys = array_keys($dbFoldersList->Instance());
				
				foreach($serverFoldersListKeys as $mkey)
				{
					$mailFolder = &$serverFoldersList->Get($mkey);
					$folderExist = false;
					foreach($dbFoldersListKeys as $skey)
					{
						$dbFolder =& $dbFoldersList->Get($skey);
						if(trim($mailFolder->FullName, $delimiter) == trim($dbFolder->FullName, $delimiter))
						{
							$folderExist = true;
							if($mailFolder->SubFolders != null)
							{
								$subKeys = array_keys($mailFolder->SubFolders->Instance());
								foreach($subKeys as $subkey)
								{
									$subFld =& $mailFolder->SubFolders->Get($subkey);
									$subFld->IdParent = $dbFolder->IdDb;
									unset($subFld);
								}
							}
							break;
						}
						unset($dbFolder);
					}

					if(!$folderExist)
					{
						$result &= $dbStorage->CreateFolder($mailFolder);
					}
				}
				
				foreach($dbFoldersListKeys as $mkey)
				{
					$dbFolder =& $dbFoldersList->Get($mkey);
					$folderExist = false;
					foreach($serverFoldersListKeys as $skey)
					{
						$mailFolder =& $serverFoldersList->Get($skey);
						if(trim($mailFolder->FullName, $delimiter) == trim($dbFolder->FullName, $delimiter))
						{
							$folderExist = true;
							break;
						}
						unset($mailFolder);
					}

					if (!$folderExist)
					{
						$result &= $dbStorage->ClearDbFolder($dbFolder, $this->Account);
						$result &= $dbStorage->DeleteFolder($dbFolder);
					}
					unset($dbFolder);
				}
			}
			return $result;
		}
		
		/**
		 * @param Folder $folder
		 * @return bool
		 */
		function DeleteFolder(&$folder)
		{
			if ($folder->Type == FOLDERTYPE_Custom)
			{
				return $this->_wmserver->deleteMailbox($folder->FullName);
			}
			return false;
		}
		
		/**
		 * @param string $user
		 * @param string $domain
		 * @return array
		 */
		function GetAutoresponder($user, $domain)
		{
			return $this->_wmadmin->GetAutoresponder($user, $domain);
		}
		
		/**
		 * @param string $user
		 * @param string $domain
		 * @param string $subject
		 * @param string $body
		 * @return bool
		 */
		function SetAutoresponder($user, $domain, $subject, $body)
		{
			return $this->_wmadmin->SetAutoresponder($user, $domain, $subject, $body);
		}
		
		/**
		 * @param string $user
		 * @param string $domain
		 * @return bool
		 */
		function DisableAutoresponder($user, $domain)
		{
			return $this->_wmadmin->DisableAutoresponder($user, $domain);
		}

		/**
		 * @param Folder $folder
		 * @param bool $isHide
		 */
		function SubscribeFolder($folder, $isHide)
		{
			$this->_wmserver->setSubscribeFolder($folder->FullName, !$isHide);
		}
		
		function RenameFolder($folder, $newName)
		{
			if ($folder && $folder->FullName != $newName)
			{
				if ($this->_wmserver->renameFolder( $folder->FullName, $newName))
				{
					if ($folder->Hide)
					{
						$this->_wmserver->setSubscribeFolder($folder->FullName, false);
						$this->_wmserver->setSubscribeFolder($newName, true);
					}
					else 
					{
						$this->_wmserver->setSubscribeFolder($folder->FullName, true);
						$this->_wmserver->setSubscribeFolder($newName, false);
					}
					
					return true;
				}
			}
			return false;
		}
		
		/**
		 * @param array $messageIdUidSet
		 * @param Folder $folder
		 * @return bool
		 */
		function SpamMessages($messageIdUidSet, $folder, $isSpam = true)
		{
			$messageUidSet = array_values($messageIdUidSet);
			
			$result = true;
			$fullFolderPath = $this->_wmserver->_changePath($folder->FullName);
			$user = $this->_wmserver->userName;
			$domain = $this->_wmserver->domain;
			$params = $this->_wmserver->getParamsMessages($folder->FullName);
			if ($params !== false && isset($params['u']))
			{
				$xmailUids =& $params['u'];
				foreach ($messageUidSet as $uid)
				{
					if (isset($xmailUids[$uid]))
					{
						$result &= ($isSpam) ? 
							$this->_wmadmin->SpamMessage($user, $domain, $fullFolderPath.'/cur/'.$xmailUids[$uid]['filename'], $this->Account->IsSpamAccount()) : 
							$this->_wmadmin->NotSpamMessage($user, $domain, $fullFolderPath.'/cur/'.$xmailUids[$uid]['filename'], $this->Account->IsSpamAccount());
					}
				}

				return $result;
			}
			
			return false;
		}
		
		/**
		 * @param array $messageUidSet
		 * @param Folder $fromFolder
		 * @param Folder $toFolder
		 * @return bool
		 */
		function MoveMessages(&$messageUidSet, $fromFolder, $toFolder)
		{
			if (!$fromFolder || !$toFolder)
			{
				return false;
			}
			
			$result = true;
			if ($fromFolder->FullName == $toFolder->FullName)
			{
				return $result;
			}
			
			$params = $this->_wmserver->getParamsMessages($fromFolder->FullName);
			if ($params !== false && isset($params['u']))
			{
				$xmailUids =& $params['u'];
				$nextUid4ToFolder = $this->_wmserver->getNextUid($toFolder->FullName, count($messageUidSet));
				foreach ($messageUidSet as $key => $uid)
				{
					if (isset($xmailUids[$uid]))
					{
						$newFileName = $this->_changeUidByNextUid($xmailUids[$uid]['filename'], $nextUid4ToFolder);
						$messageUidSet[$key] = $this->_changeUidByNextUid($uid, $nextUid4ToFolder);
						$result &= $this->_wmserver->moveMessage($xmailUids[$uid]['filename'], $newFileName, $fromFolder->FullName, $toFolder->FullName);
						$nextUid4ToFolder = $nextUid4ToFolder + 1;
					}
					else
					{
						$result = false;
					}
				}

				return $result;
			}
			
			return false;
		}
		
		function _changeUidByNextUid($uid, $nextUid)
		{
			$filearray = explode('.', $uid);
			if (count($filearray) > 1)
			{
				$filearray[1] = (int) $nextUid;
			}
			return implode('.', $filearray);
		}
		
		function DeleteMessageFromServer($uid, $folder)
		{
			$filename = $this->_wmserver->getFileNameByUid($uid, $folder->FullName);
			if ($filename)
			{
				return $this->_wmserver->deleteMessage($filename, $folder->FullName);
			}
			
			return false;
		}

		/**
		 * @param array $messageIndexSet
		 * @param bool $indexAsUid
		 * @param Folder $folder
		 * @return bool
		 */
		function DeleteMessages(&$messageIndexSet, $indexAsUid, $folder)
		{
			$result = false;
			if (!$indexAsUid)
			{
				return $result;
			}
			
			$params = $this->_wmserver->getParamsMessages($folder->FullName);
			if ($params !== false && isset($params['u']))
			{
				$result = true;
				$xmailUids =& $params['u'];
				foreach ($messageIndexSet as $uid)
				{
					if (isset($xmailUids[$uid]))
					{
						$result &= $this->_wmserver->deleteMessage($xmailUids[$uid]['filename'], $folder->FullName);
					}
					else
					{
						$result = false;
					}
				}
			}
			
			return $result;
		}
		
		function LoadMessage($messageUid, $indesAsUid, $folder)
		{
			if (!$indesAsUid)
			{
				return false;
			}
			
			$response = null;
			$fileName = $this->_wmserver->getFileNameByUid($messageUid, $folder->FullName);
			
			if ($fileName !== false)
			{
				$response = $this->_wmserver->getMessage($fileName, $folder->FullName);
			}
			
			if ($response)
			{
				$msg = new WebMailMessage();
				$msg->LoadMessageFromRawBody($response, true);
				$msg->Uid = $messageUid;
				$this->_setMessageFlags($msg, $this->_wmserver->_getFlagsFromFileName($fileName));
				return $msg;
			}
			else 
			{
				setGlobalError(PROC_MSG_HAS_DELETED);
			}
			
			return false;
		}
		
		/**
		 * @param WebMailMessage $message
		 * @param Folder $folder
		 * @return bool
		 */
		function SaveMessage(&$message, &$folder)
		{
			$newxUid = $this->_wmserver->getNextUid($folder->FullName);
			if (false === $newxUid)
			{
				return false;
			}
			$message->Uid = ConvertUtils::GenerateXmailUid($newxUid);
			$raw =& $message->TryToGetOriginalMailMessage();
			return $this->_wmserver->saveMessage($folder->FullName, $this->getMessageFileName($message, strlen($raw)), $raw);
		}
		
		/**
		 * @param Folder $folder
		 * @return bool
		 */
		function PurgeFolder(&$folder)
		{
			if ($folder)
			{
				return $this->_wmserver->purgeFolder($folder->FullName);
			}
			return false;
		}
		
		/**
		 * @param Folder $folder
		 * @return bool
		 */
		function ClearFolder(&$folder)
		{
			if ($folder)
			{
				return $this->_wmserver->clearFolder($folder->FullName);
			}
			return false;
		}
		
		/**
		 * @param Folder $folders
		 * @param DbStorage $dbStorage
		 * @param int $lastIdMsg
		 * @return bool
		 */
		function _synchronizeFolderWithOpenDbConnection(&$folder, &$dbStorage)
		{
			$result = true;
			if($folder->SyncType == FOLDERSYNC_DontSync || $folder->SyncType == FOLDERSYNC_DirectMode || $folder->Hide)
			{
				if ($this->UpdateFolderHandler != null && $folder->SyncType == FOLDERSYNC_DirectMode)
				{
					call_user_func_array($this->UpdateFolderHandler, array($folder->IdDb, $folder->FullName));
				}
				return $result;
			}

			$foldername = '';
			if ($this->DownloadedMessagesHandler != null)
			{
				$foldername = $folder->GetFolderName($this->Account);
				call_user_func_array($this->DownloadedMessagesHandler, array($foldername, 0));
			}
			
			/* get uid, flags and size from server */
			$paramsMessages = $this->_wmserver->getParamsMessages($folder->FullName, GETPARAMSTYPE_ALL);
			if (!is_array($paramsMessages) || count($paramsMessages) != 2)
			{
				return false;
			}
			
			$xmailOnlyUids = array();
			$xmailUidFilenames = array();
			$xmailUidFlags = array();
			$xmailUidSizes = array();
			
			if (isset($paramsMessages['u']))
			{
				foreach($paramsMessages['u'] as $uid => $value)
				{
					$xmailUidFilenames[$uid] = $value['filename'];
					$xmailUidFlags[$uid] = $value['flags'];
					$xmailUidSizes[$uid] = $value['size'];
					$xmailOnlyUids[] = $uid;
				}
			}
			
			$dbUidsIdMsgsFlags =& $dbStorage->SelectIdMsgAndUidByIdMsgDesc($folder);
			
			$dbUids = $dbUidsFlag = $dbUidsId = array();
			
			foreach($dbUidsIdMsgsFlags as $value)
			{
				$dbUidsFlag[$value[1]] = $value[2];
				$dbUidsId[$value[1]] = $value[0];
				$dbUids[] = $value[1];
			}
			
			/* array need added to DB */
			$newUids = array_diff($xmailOnlyUids, $dbUids);
			
			$syncCycles = count($newUids);
			
			if ($folder->SyncType == FOLDERSYNC_AllHeadersOnly || $folder->SyncType == FOLDERSYNC_AllEntireMessages)
			{
				/* intersect uids */			
				$currentUids = array_intersect($xmailOnlyUids, $dbUids);
				
				/* update messages whith different flags */
				foreach($currentUids as $currentUid)
				{
					$flagBD = $dbUidsFlag[$currentUid];
					$flagXmail = $this->getIntFlags($xmailUidFlags[$currentUid]);
					
					if ($flagBD != $flagXmail)
					{
						$dbStorage->UpdateMessageFlags(array($dbUidsId[$currentUid]), false, $folder, $flagXmail, $this->Account);
						if ($this->UpdateFolderHandler != null)
						{
							call_user_func_array($this->UpdateFolderHandler, array($folder->IdDb, $folder->FullName));
						}
					}
				}
			}
			
			/* Delete from DB */
			if($folder->SyncType == FOLDERSYNC_AllHeadersOnly || $folder->SyncType == FOLDERSYNC_AllEntireMessages)
			{			
				/* Array delete from DB */
				$uidsToDelete = array_diff($dbUids, $xmailOnlyUids);
				if (count($uidsToDelete) > 0)
				{
					$result &= $dbStorage->DeleteMessages($uidsToDelete, true, $folder);
					if ($this->UpdateFolderHandler != null)
					{
						call_user_func_array($this->UpdateFolderHandler, array($folder->IdDb, $folder->FullName));
					}
					
					$result &= $dbStorage->UpdateMailboxSize();
				}
			}
			
			/* Get size all messages in DB */
			$mailBoxesSize = $dbStorage->SelectMailboxesSize();
			
			if ($this->DownloadedMessagesHandler != null && $syncCycles > 0)
			{
				call_user_func_array($this->DownloadedMessagesHandler, array($foldername, $syncCycles));
			}
			
			$_filters = &$dbStorage->SelectFilters($this->Account->Id, true);


			if (null == $this->mailproc)
			{
				$this->mailproc = new MailProcessor($this->Account);
			}

			$ffolders =& $this->mailproc->GetFolders();
		
			foreach ($newUids as $nUid)
			{	
				if (isset($xmailUidSizes[$nUid]))
				{
					$mailBoxesSize += (int) $xmailUidSizes[$nUid];
				}
					
				if ($this->_settings->EnableMailboxSizeLimit && $this->Account->MailboxLimit > 0 && $this->Account->MailboxLimit < $mailBoxesSize)
				{
					$result = false;
					setGlobalError(ErrorGetMailLimit);
					break;
				}	

				if($this->DownloadedMessagesHandler != null)
				{
					call_user_func($this->DownloadedMessagesHandler);
				}
				
				$msg = null;
				if (isset($xmailUidFilenames[$nUid]) && isset($xmailUidFlags[$nUid]) && isset($xmailUidSizes[$nUid]))
				{
					$msg =& $this->_loadMessageHeader($folder->FullName, $xmailUidFilenames[$nUid], $nUid, $xmailUidFlags[$nUid], $xmailUidSizes[$nUid]);
				}
				
				/* write to DB */
				if ($msg != null)
				{
					if(!$this->ApplyFilters($msg, $dbStorage, $folder, $_filters, $ffolders))
					{
						$result = false;
						break;
					}
				}
				unset($msg);
			}
			
			$result &= count($newUids) > 0 ? $dbStorage->UpdateMailboxSize() : true;
			return $result;
		}
		
		/**
		 * @param WebMailMessage $message
		 * @param DbStorage $dbStorage
		 * @param Folder $folder
		 * @param Filters $filters
		 * @param Folders $folders
		 * @return bool
		 */	
		function ApplyFilters(&$message, &$dbStorage, &$folder, &$filters, $folders)
		{
			$result = true;
			$needToSave = true;
			
			if ($folder->Type == FOLDERTYPE_Inbox && $result && isset($GLOBALS['useFilters']))
			{
				if (null == $this->mailproc)
				{
					$this->mailproc = new MailProcessor($this->Account);
				}

				$mailProcessor =& $this->mailproc;
				
				$toFolder = null;
				$foldersInfo = array();
				
				$filtersKey = array_keys($filters->Instance());
				foreach ($filtersKey as $key)
				{
					$filter =& $filters->Get($key);
					$action = $filter->GetActionToApply($message);
	
					$messageIdUidSet = array($message->IdMsg => $message->Uid);
					
					switch ($action)
					{
						case FILTERACTION_DeleteFromServerImmediately:
							$result &= $mailProcessor->DeleteFromServerImmediately($messageIdUidSet, $folder);
							$needToSave = false;
							break 2;
							
						case FILTERACTION_MoveToFolder:

							$tempFolder = null;
							if (!isset($foldersInfo[$filter->IdAcct.'_'.$filter->IdFolder]))
							{
								$tempFolder = new Folder($filter->IdAcct, $filter->IdFolder, '');
								$dbStorage->GetFolderInfo($tempFolder);
								$foldersInfo[$filter->IdAcct.'_'.$filter->IdFolder] =& $tempFolder;
							}
							else 
							{
								$tempFolder =& $foldersInfo[$filter->IdAcct.'_'.$filter->IdFolder];
							}
							
							if ($tempFolder)
							{
								$result &= $dbStorage->SaveMessageHeader($message, $tempFolder, false);
								$result &= $mailProcessor->MoveMessages($messageIdUidSet, $folder, $tempFolder);
								if ($this->UpdateFolderHandler != null)
								{
									call_user_func_array($this->UpdateFolderHandler, array($tempFolder->IdDb, $tempFolder->FullName));
								}
							}
							unset($tempFolder);
							$needToSave = false;
							break 2;

						case FILTERACTION_MarkGrey:
							$result &= $mailProcessor->SetFlags($messageIdUidSet, $folder, MESSAGEFLAGS_Grayed, ACTION_Set, false);
							$message->Flags |= MESSAGEFLAGS_Grayed;
							break;

						case FILTERACTION_SpamDetect:
							
							if ($folders && !$this->Account->IsSpamAccount())
							{
								$toFolder =& $folders->GetFolderByType(FOLDERTYPE_Spam);
								if ($toFolder)
								{
									$result &= $dbStorage->SaveMessageHeader($message, $toFolder, false);
									$result &= $mailProcessor->MoveMessages($messageIdUidSet, $folder, $toFolder);
									if ($this->UpdateFolderHandler != null)
									{
										call_user_func_array($this->UpdateFolderHandler, array($toFolder->IdDb, $toFolder->FullName));
									}
								}
							}
							$needToSave = false;
							break 2;
						case FILTERACTION_VirusDetect:
							
							if ($folders)
							{
								$toFolder =& $folders->GetFolderByType(FOLDERTYPE_Virus);
								if ($toFolder)
								{
									$result &= $dbStorage->SaveMessageHeader($message, $toFolder, false);
									$result &= $mailProcessor->MoveMessages($messageIdUidSet, $folder, $toFolder);
									if ($this->UpdateFolderHandler != null)
									{
										call_user_func_array($this->UpdateFolderHandler, array($toFolder->IdDb, $toFolder->FullName));
									}								
								}
							}
							$needToSave = false;
							break 2;
					}
					
					unset($filter);
				}
			}
			
			if ($needToSave)
			{
				$result &= $dbStorage->SaveMessageHeader($message, $folder, false);
				if ($this->UpdateFolderHandler != null)
				{
					call_user_func_array($this->UpdateFolderHandler, array($folder->IdDb, $folder->FullName));
				}		
			}
			
			return $result;
		}
		
		/**
		 * @param string $fullName
		 * @param string $messageIndexSet
		 * @param string $imapUids
		 * @param string $imapUidFlags
		 * @param string $imapUidSizes
		 * @return WebMailMessage
		 */
		function _loadMessageHeader($fullName, $fuid, $uid, $flags, $size)
		{
			$msg = null;
	
			$response = $this->_wmserver->getMessageHeader($fuid, $fullName);
			if($response)
			{
				$msg = new WebMailMessage();
				$msg->LoadMessageFromRawBody($response);
				$msg->IdMsg = -1;
				$msg->Uid = $uid;
				$msg->Size = $size;
				$this->_setMessageFlags($msg, $flags);
			}
			
			return $msg;
		}
	
		function SetFolderType(&$folderObj, &$existsIndex)
		{
			switch ($folderObj->Type)
			{
				case FOLDERTYPE_Inbox:
					if (isset($existsIndex['InboxAdd'])) $folderObj->Type = FOLDERTYPE_Custom;
					$existsIndex['InboxAdd'] = true;
					break;
				case FOLDERTYPE_SentItems:
					if (isset($existsIndex['SentAdd'])) $folderObj->Type = FOLDERTYPE_Custom;
					$existsIndex['SentAdd'] = true;
					break;
				case FOLDERTYPE_Drafts:
					if (isset($existsIndex['DraftsAdd'])) $folderObj->Type = FOLDERTYPE_Custom;
					$existsIndex['DraftsAdd'] = true;
					break;
				case FOLDERTYPE_Spam:
					if (isset($existsIndex['SpamAdd'])) $folderObj->Type = FOLDERTYPE_Custom;
					$existsIndex['SpamAdd'] = true;
					break;
				case FOLDERTYPE_Virus:
					if (isset($existsIndex['VirusAdd'])) $folderObj->Type = FOLDERTYPE_Custom;
					$existsIndex['VirusAdd'] = true;
					break;
				case FOLDERTYPE_Trash:
					if (isset($existsIndex['TrashAdd'])) $folderObj->Type = FOLDERTYPE_Custom;
					$existsIndex['TrashAdd'] = true;
					break;
				default:
					$folderObj->Type = FOLDERTYPE_Custom;
					break;					
			}
		}

		/**
		 * @param WebMailMessage $message
		 * @param int $size
		 */
		function getMessageFileName(&$message, $size)
		{
			$_sep = ConvertUtils::IsWin() ? '_' : ':';
			return $message->Uid.$this->_wmserver->_uidSizeD.$size.$_sep.$this->_wmserver->_uidD.$this->_intFlagToXmailFlag($message->Flags);
		}

		/**
		 * @param int $flags
		 */
		function _intFlagToXmailFlag($flags)
		{
			$out = array();
			
			if (($flags & MESSAGEFLAGS_Seen) == MESSAGEFLAGS_Seen)
			{
				$out[] = 'S';	
			}
			if (($flags & MESSAGEFLAGS_Deleted) == MESSAGEFLAGS_Deleted)
			{
				$out[] = 'T';	
			}
			if (($flags & MESSAGEFLAGS_Flagged) == MESSAGEFLAGS_Flagged)
			{
				$out[] = 'F';	
			}
			if (($flags & MESSAGEFLAGS_Draft) == MESSAGEFLAGS_Draft)
			{
				$out[] = 'D';	
			}
			
			sort($out);
			return implode('', $out);
		}
		
		/**
		 * @param string $strFlags
		 * @return int
		 */
		function getIntFlags($flags)
		{
			$intFlags = 0;
			$flags = explode(' ', strtolower($flags));
			foreach($flags as $flag)
			{
				switch(trim($flag))
				{
					case '\seen':		$intFlags |= MESSAGEFLAGS_Seen;		break;
					case '\answered':	$intFlags |= MESSAGEFLAGS_Answered;	break;
					case '\flagged':	$intFlags |= MESSAGEFLAGS_Flagged;	break;
					case '\deleted':	$intFlags |= MESSAGEFLAGS_Deleted;	break;
					case '\draft':		$intFlags |= MESSAGEFLAGS_Draft;	break;
					case '\recent':		$intFlags |= MESSAGEFLAGS_Recent;	break;
				}
			}
			return $intFlags;
		}
		
		/**
		 * @access private
		 * @param WebMailMessage $message
		 * @param string $flags
		 */
		function _setMessageFlags(&$message, $flags)
		{
			$message->Flags = 0;
			$flags = strtolower($flags);
			if (false !== strpos($flags, 'seen'))
			{
				$message->Flags |= MESSAGEFLAGS_Seen;
			}
			if (false !== strpos($flags, 'answered'))
			{
				$message->Flags |= MESSAGEFLAGS_Answered;
			}
			if (false !== strpos($flags, 'flagged'))
			{
				$message->Flags |= MESSAGEFLAGS_Flagged;
			}
			if (false !== strpos($flags, 'deleted'))
			{
				$message->Flags |= MESSAGEFLAGS_Deleted;
			}
			if (false !== strpos($flags, 'draft'))
			{
				$message->Flags |= MESSAGEFLAGS_Draft;
			}
			if (false !== strpos($flags, 'recent'))
			{
				$message->Flags |= MESSAGEFLAGS_Recent;
			}
		}
		
		/**
		 * @param string $flags
		 * @return string
		 */
		function _getStrFlags($flags)
		{
			$strFlags = array();
			$flags = strtolower($flags);
			if (false !== strpos($flags, 'seen'))
			{
				$strFlags[] = 'S';
			}
			if (false !== strpos($flags, 'flagged'))
			{
				$strFlags[] = 'F';
			}
			if (false !== strpos($flags, 'deleted'))
			{
				$strFlags[] = 'T';
			}
			if (false !== strpos($flags, 'draft'))
			{
				$strFlags[] = 'D';
			}
			
			sort($strFlags);
			$strFlags = implode('', $strFlags);
			
			return $strFlags;
		}

	}
Return current item: AfterLogic WebMail Lite PHP