<?php
require_once(WM_ROOTPATH.'core/base/base_model.php');
define('START_PAGE_IS_MAILBOX', 0);
define('START_PAGE_IS_NEW_MESSAGE', 1);
define('START_PAGE_IS_SETTINGS', 2);
define('START_PAGE_IS_CONTACTS', 3);
define('START_PAGE_IS_CALENDAR', 4);
/**
* Model for API integration
*/
class WebMailModel extends BaseModel
{
public function IsReady()
{
return true;
}
public function SetDriver()
{
return true;
}
public function SetComandCreator()
{
return true;
}
/**
* @param Account $account
* @return bool
*/
protected function _CreateAccount(&$account)
{
require_once(WM_ROOTPATH.'common/class_account.php');
require_once(WM_ROOTPATH.'common/class_dbstorage.php');
require_once(WM_ROOTPATH.'common/class_mailprocessor.php');
$settings =& Settings::CreateInstance();
if (!$settings || !$settings->isLoad)
{
throw new WebMailModelException('settings error');
}
if (!$settings->IncludeLang())
{
throw new WebMailModelException('lang error');
}
$dbStorage =& DbStorageCreator::CreateDatabaseStorage($account);
if ($dbStorage && $dbStorage->Connect())
{
$domain =& $dbStorage->SelectDomainByName(EmailAddress::GetDomainFromEmail($account->Email));
if (null !== $domain)
{
$domain->UpdateAccount($account, $settings);
}
if ($settings->EnableWmServer && $account->MailProtocol == MAILPROTOCOL_WMSERVER)
{
$account->Delimiter = '.';
}
}
else
{
throw new WebMailModelException(getGlobalError());
}
$processor = new MailProcessor($account);
if ($processor && $processor->MailStorage->Connect())
{
$validate = $account->ValidateData();
if ($validate !== true)
{
throw new WebMailModelException($validate);
}
else
{
$inboxSync = $account->GetDefaultFolderSync($settings);
$user = new User();
$user->Id = $account->IdUser;
if (!$dbStorage->IsSettingsExists($account->IdUser))
{
if (!$dbStorage->InsertSettings($account))
{
throw new WebMailModelException(getGlobalError());
}
}
if ($user->CreateAccount($account, $inboxSync, true))
{
return true;
}
}
}
throw new WebMailModelException(getGlobalError());
}
/**
* @param string $email
* @param string $login
* @return bool
*/
protected function _AccountExist($email, $login)
{
try
{
$this->GetAccountByMailLogin($email, $login);
}
catch(BaseException $e)
{
throw new WebMailModelException('', 0, $e);
}
return true;
}
protected function _GetAccountByMailLogin($email, $login)
{
require_once(WM_ROOTPATH.'common/class_account.php');
require_once(WM_ROOTPATH.'common/class_dbstorage.php');
$settings =& Settings::CreateInstance();
if (!$settings || !$settings->isLoad)
{
throw new WebMailModelException('settings error');
}
if (!$settings->IncludeLang())
{
throw new WebMailModelException('lang error');
}
$acct = null;
$dbStorage =& DbStorageCreator::CreateDatabaseStorage($acct, $settings);
if ($dbStorage->Connect())
{
$acct =& $dbStorage->SelectAccountFullDataByLogin($email, $login);
if ($acct)
{
if ($acct->IdDomain > 0)
{
$domain =& $dbStorage->SelectDomainById($acct->IdDomain);
if (null !== $domain)
{
$domain->UpdateAccount($acct, $settings);
}
}
return $acct;
}
}
throw new WebMailModelException(getGlobalError());
}
protected function _GetAccountByEmail($email)
{
require_once(WM_ROOTPATH.'common/class_account.php');
require_once(WM_ROOTPATH.'common/class_dbstorage.php');
$settings =& Settings::CreateInstance();
if (!$settings || !$settings->isLoad)
{
throw new WebMailModelException('settings error');
}
if (!$settings->IncludeLang())
{
throw new WebMailModelException('lang error');
}
$account = null;
$dbStorage =& DbStorageCreator::CreateDatabaseStorage($account, $settings);
if ($dbStorage->Connect())
{
$account =& $dbStorage->SelectAccountDataOnlyByEmail($email);
if ($account !== null)
{
return $account;
}
}
throw new WebMailModelException(getGlobalError());
}
protected function _GetAccountById($id)
{
require_once(WM_ROOTPATH.'common/class_account.php');
require_once(WM_ROOTPATH.'common/class_dbstorage.php');
$settings =& Settings::CreateInstance();
if (!$settings || !$settings->isLoad)
{
throw new WebMailModelException('settings error');
}
if (!$settings->IncludeLang())
{
throw new WebMailModelException('lang error');
}
$acct = null;
$dbStorage =& DbStorageCreator::CreateDatabaseStorage($acct, $settings);
if ($dbStorage->Connect())
{
$acct =& $dbStorage->SelectAccountData($id);
if ($acct)
{
if ($acct->IdDomain > 0)
{
$domain =& $dbStorage->SelectDomainById($acct->IdDomain);
if (null !== $domain)
{
$domain->UpdateAccount($acct, $settings);
}
}
return $acct;
}
}
throw new WebMailModelException(getGlobalError());
}
/**
* @param string $email
* @param string $login
* @param string $password = null
* @return bool
*/
protected function _UserLoginByEmail($email, $login, $password = null)
{
require_once(WM_ROOTPATH.'common/class_account.php');
require_once(WM_ROOTPATH.'common/class_dbstorage.php');
$newAccount = new Account();
$settings =& Settings::CreateInstance();
if (!$settings || !$settings->isLoad)
{
throw new WebMailModelException('settings error');
}
if (!$settings->IncludeLang())
{
throw new WebMailModelException('lang error');
}
$loginArray =& Account::LoadFromDbByLogin($email, $login);
if ($loginArray != null)
{
if ($loginArray[2] == '1')
{
if ($password === null)
{
@session_write_close();
@session_name('PHPWEBMAILSESSID');
@session_start();
$_SESSION[ACCOUNT_ID] = $loginArray[0];
$_SESSION[USER_ID] = $loginArray[3];
return true;
}
else if ($password == ConvertUtils::DecodePassword($loginArray[1], $newAccount))
{
@session_write_close();
@session_name('PHPWEBMAILSESSID');
@session_start();
$_SESSION[ACCOUNT_ID] = $loginArray[0];
$_SESSION[USER_ID] = $loginArray[3];
return true;
}
else
{
$account =& Account::LoadFromDb($loginArray[0]);
$account->MailIncPassword = $password;
$newprocessor = new MailProcessor($account);
if ($newprocessor->MailStorage->Connect(true))
{
if ($account->Update())
{
@session_write_close();
@session_name('PHPWEBMAILSESSID');
@session_start();
$_SESSION[ACCOUNT_ID] = $account->Id;
$_SESSION[USER_ID] = $account->IdUser;
$_SESSION[SESSION_LANG] = $account->DefaultLanguage;
return true;
}
else
{
throw new WebMailModelException(getGlobalError());
}
}
else
{
throw new WebMailModelException(ErrorPOP3IMAP4Auth);
}
}
}
else
{
throw new WebMailModelException(PROC_CANT_LOG_NONDEF);
}
}
else
{
throw new WebMailModelException(ErrorPOP3IMAP4Auth);
}
}
/**
* @param int $startPage = null
* @param string $toEmail = null
* @param bool $isSeparated = false
* @return string|bool
*/
protected function _GetApplicationBaseUrl($startPage = null, $toEmail = null, $isSeparated = false)
{
require_once(WM_ROOTPATH.'common/class_settings.php');
$settings =& Settings::CreateInstance();
if (!$settings || !$settings->isLoad)
{
throw new WebMailModelException('settings error');
}
if (!isset($_SESSION[ACCOUNT_ID]) || !isset($_SESSION[USER_ID]))
{
throw new WebMailModelException('access error');
}
if ($isSeparated)
{
$_SESSION[SEPARATED] = $isSeparated;
}
$url = 'webmail.php?check=1';
if (null !== $startPage)
{
switch ($startPage)
{
case START_PAGE_IS_NEW_MESSAGE:
$url .= '&start='.START_PAGE_IS_NEW_MESSAGE;
if ($toEmail && strlen($toEmail) > 0)
{
$url .= '&to='.$toEmail;
}
break;
case START_PAGE_IS_MAILBOX:
case START_PAGE_IS_SETTINGS:
case START_PAGE_IS_CONTACTS:
$url .= '&start='.$startPage;
break;
case START_PAGE_IS_CALENDAR:
if ($isSeparated)
{
$url = 'calendar.php';
}
else
{
$url .= '&start='.START_PAGE_IS_CALENDAR;
}
break;
}
}
return $url;
}
/**
* @param string $email
* @return int
*/
protected function _CheckCountOfUserAccounts($email)
{
require_once(WM_ROOTPATH.'common/class_account.php');
require_once(WM_ROOTPATH.'common/class_dbstorage.php');
$settings =& Settings::CreateInstance();
if (!$settings || !$settings->isLoad)
{
throw new WebMailModelException('settings error');
}
if (!$settings->IncludeLang())
{
throw new WebMailModelException('lang error');
}
$acct = null;
$dbStorage =& DbStorageCreator::CreateDatabaseStorage($acct, $settings);
if ($dbStorage->Connect())
{
return $dbStorage->CheckCountOfUserAccounts($email);
}
throw new WebMailModelException(getGlobalError());
}
}
class WebMailModelException extends BaseModelException
{}