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

/*
 * AfterLogic Admin Panel by AfterLogic Corp. <hide@address.com>
 *
 * Copyright (C) 2002-2010  AfterLogic Corp. (www.afterlogic.com)
 * Distributed under the terms of the license described in LICENSE.txt
 * 
 */

	class XMailStorage extends MailServerStorage
	{
		/**
		 * @access private
		 * @var CWmServerConsoleAdm
		 */
		var $_wmadmin;
		
		/**
		 * @var	string
		 */
		var $_userName;
		
		/**
		 * @var	string
		 */
		var $_domain;
		
		/**
		 * @var	string
		 */
		var $_basePath;
		
		/**
		 * @var	string
		 */
		var $_path;
		
		var $_settings;
		
		/**
		 * @access public
		 * @param Account $account
		 * @return XMailStorage
		 */
		function XMailStorage(&$account, $pathToClassFolder, $settings)
		{
			MailServerStorage::MailServerStorage($account);
				
			$parsedEmail = ap_Utils::ParseEmail($account->Email);
		
			if ($parsedEmail !== false)
			{
				$this->_userName = trim($parsedEmail[0]);
				$this->_domain = trim($parsedEmail[1]);
			}
		
			$this->_settings =& $settings;
			$this->_basePath = $settings->WmServerRootPath;
			$this->_path = $this->_basePath.'/domains/'.$this->_domain.'/'.$this->_userName.'/Maildir/';
			
			require_once $pathToClassFolder.'libs/class_xmail.php';
			$this->_wmadmin = new CWmServerConsoleAdm($settings);
		}
		
		/**
		 * @param $arg[optional] = false
		 * @return bool
		 */
		function Connect($arg = false)
		{
			if($this->_wmadmin->_socket != false || !$arg)
			{
				return true;
			}

			if (!@is_dir($this->_basePath) || !$this->_wmadmin->Connect())
			{
				$this->SetError(ap_Utils::TakePhrase('WM_ERROR_XMAIL_CONNECT'));
				return false;
			}
			else
			{
				register_shutdown_function(array(&$this, 'Disconnect'));
			}
	
			return true;
		}
		
		/**
		 * @return bool
		 */
		function Disconnect()
		{
			return $this->_wmadmin->Disconnect();
		}

		/**
		 * @return FolderCollection
		 */
		function &GetFolders(&$accountDelimiter)
		{
			$folderCollection = new FolderCollection();
			$folders = $this->_getFolders();
			
			$accountDelimiter = '.';
			
			$existsIndex = array(); 
			$folderCollection = $this->GetFolderCollectionFromArrays($folders, $folders, $accountDelimiter, $existsIndex);
			
			/* custom class */
			ap_Custom::StaticUseMethod('wm_ChangeServerXMailFoldersAfterGet', array(&$folderCollection));
			
			return $folderCollection;
		}
		
		function SetFolderType(&$folderObj, &$existsIndex)
		{
			switch ($folderObj->Type)
			{
				case WM_FOLDERTYPE_Inbox:
					if (isset($existsIndex['InboxAdd'])) $folderObj->Type = WM_FOLDERTYPE_Custom;
					$existsIndex['InboxAdd'] = true;
					break;
				case WM_FOLDERTYPE_SentItems:
					if (isset($existsIndex['SentAdd'])) $folderObj->Type = WM_FOLDERTYPE_Custom;
					$existsIndex['SentAdd'] = true;
					break;
				case WM_FOLDERTYPE_Drafts:
					if (isset($existsIndex['DraftsAdd'])) $folderObj->Type = WM_FOLDERTYPE_Custom;
					$existsIndex['DraftsAdd'] = true;
					break;
				case WM_FOLDERTYPE_Spam:
					if (isset($existsIndex['SpamAdd'])) $folderObj->Type = WM_FOLDERTYPE_Custom;
					$existsIndex['SpamAdd'] = true;
					break;
				case WM_FOLDERTYPE_Virus:
					if (isset($existsIndex['VirusAdd'])) $folderObj->Type = WM_FOLDERTYPE_Custom;
					$existsIndex['VirusAdd'] = true;
					break;
				case WM_FOLDERTYPE_Trash:
					if (isset($existsIndex['TrashAdd'])) $folderObj->Type = WM_FOLDERTYPE_Custom;
					$existsIndex['TrashAdd'] = true;
					break;
				default:
					$folderObj->Type = WM_FOLDERTYPE_Custom;
					break;					
			}
		}
		
		/**
		 * @return bool
		 */
		function UpdateMaxMailBox()
		{
			return $this->_wmadmin->ChangeMaxMailBox($this->_domain, $this->_userName, (int) round(GetGoodBigInt($this->_account->MailboxLimit) / 1024));
		}
		
		/**
		 * @return	array
		 */
		function _getFolders()
		{
			$folders = array();

			if (@is_dir($this->_path))
			{
				if (@is_dir($this->_path.'new/') && @is_dir($this->_path.'cur/'))
				{
					$folders[] = 'INBOX';
				}
				
				$dir = @dir($this->_path);
				if ($dir)
				{
					while (false !== ($entry = $dir->read()))
					{
						if($entry != '.' && $entry != '..' && strlen($entry) > 0 && $entry{0} == '.' && @is_dir($this->_path.$entry))
						{	
							$folders[] = substr($entry, 1);
						}
					}
				}
			}
			else 
			{
				CAdminPanel::Log('XM : Can\'t get xmail folders '.$this->_path);
			}
		
			return $folders;
		}
		
		/**
		 * @param Folder $folder
		 * @return bool
		 */
		function CreateFolder($folder)
		{
			$fullName = $folder->FullName;
			$result = false;
			if (strtolower($fullName) == 'inbox')
			{
				return $result;
			}
			else if (@is_dir($this->_path) && !@is_dir($this->_path.'.'.$fullName))
			{
				$result = @mkdir($this->_path.'.'.$fullName);
				$result &= @mkdir($this->_path.'.'.$fullName.'/cur/');
				$result &= @mkdir($this->_path.'.'.$fullName.'/new/');
				$result &= @mkdir($this->_path.'.'.$fullName.'/tmp/');
				
				$result &= $this->saveNewInfoTab($this->_path.'.'.$fullName.'/.info.tab');
				$this->setSubscribeFolder($fullName, true);
			}
	
			return $result;
		}
		
		/**
		 * @param string $_filename
		 * @return bool
		 */
		function saveNewInfoTab($_filename)
		{
			if (!@file_exists($_filename))
			{
				$content = "IS.SUBSCRIBED 0\r\nUIDVALIDITY ".time()."\r\nUIDNEXT 1\r\n";
				$this->_saveFileByFileName($_filename, $content);
				return true;
			}
			return false;
		}		
		
		/**
		 * @param string $fileName
		 * @param string $content
		 * @return bool
		 */
		function _saveFileByFileName($fileName, $content)
		{
			$f = @fopen($fileName, 'wb');
			if ($f)
			{
				@fwrite($f, $content); 
				@fclose($f);
				return true;
			}
			
			return false;
		}
		
		function _changePath($fullName)
		{
			$path = $this->_path;
			if (strtoupper($fullName) != 'INBOX')
			{
				$path .= '.'.$fullName;
			}
			return rtrim($path, '/\\');
		}
		
		function setSubscribeFolder($folderName, $isSubscribe = true)
		{
			$_folderName = $this->_changePath($folderName);
			$_subfile = rtrim($this->_path, '/\\').'/.sub.tab';
			$_filename = $_folderName.'/.info.tab';
			
			if (@file_exists($_filename))
			{
				$_fo = @fopen($_filename, 'r+');
				if ($_fo)
				{
					$_next = false;
					$_contents = @fread($_fo, filesize($_filename));
					$_pos = strpos($_contents, 'IS.SUBSCRIBED');
					if ($_pos !== false)
					{
						$_pos = $_pos + 14;
						$_next = (int) trim(substr($_contents, $_pos, 1));
						
						if ($_next != (int) $isSubscribe)
						{
							if (fseek($_fo, $_pos) === 0)
							{
								$_new = (int) $isSubscribe;
								fputs($_fo, $_new, 1);
							}
						}
					}
	
					@fclose($_fo); 
				}
			}
			
			$file = array();
			$exist = false;
			if (@file_exists($_subfile))
			{
				$file = @file($_subfile);
				foreach ($file as $key => $line) 
				{
					if (trim('.'.$folderName) == trim($line))
					{
						$exist = true;
						if (!$isSubscribe)
						{
							unset($file[$key]);
						}
					}
				}
			}
			
			if ($isSubscribe && !$exist)
			{
				$_fo = @fopen($_subfile, 'a');
				if ($_fo)
				{
					$text = trim('.'.$folderName)."\r\n";
					@fputs($_fo, $text, strlen($text));
					@fclose($_fo); 
				}
			}
			else if (!$isSubscribe && $exist)
			{
				$_fo = @fopen($_subfile, 'w');
				if ($_fo)
				{
					$text = implode('', $file);
					@fputs($_fo, $text, strlen($text));
					@fclose($_fo); 
				}
			}
		}
		
		/**
		 * @param Folder $folder
		 * @return bool
		 */
		function DeleteFolder($folder)
		{
			$fullName = $folder->FullName;
			
			$result = true;
			switch (strtolower($fullName))
			{
				case 'inbox':
				case 'sent':
				case 'drafts':
				case 'spam':
				case 'trash':
					break;
				default:
					if (@is_dir($this->_path.'.'.$fullName) && @is_dir($this->_path.'.'.$fullName.'/cur/') &&
							@is_dir($this->_path.'.'.$fullName.'/new/') && @is_dir($this->_path.'.'.$fullName.'/tmp/'))
					{
						$result = @unlink($this->_path.'.'.$fullName.'/.info.tab'); 
						$result &= @rmdir($this->_path.'.'.$fullName.'/cur/');
						$result &= @rmdir($this->_path.'.'.$fullName.'/new/');
						$result &= @rmdir($this->_path.'.'.$fullName.'/tmp/');
						$result &= @rmdir($this->_path.'.'.$fullName);
						
						
						if (!$result)
						{
							@mkdir($this->_path.'.'.$fullName);
							@mkdir($this->_path.'.'.$fullName.'/cur/');
							@mkdir($this->_path.'.'.$fullName.'/new/');
							@mkdir($this->_path.'.'.$fullName.'/tmp/'); 
							$this->saveNewInfoTab($this->_path.'.'.$fullName.'/.info.tab');					
						}
					}
					break;
			}

			return $result;
		}
	}
Return current item: AfterLogic WebMail Lite PHP