<?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 CCommon_Plugin extends ap_Plugin
{
/**
* @var array
*/
var $_settings = array();
/**
* @var array
*/
var $Tabs = array(
array('Admin Panel', 'Admin Panel Settings', 'common'),
array(array('Upgrade to Pro', 'License Key'), 'Main Settings', 'main', true)
);
/**
* @var bool
*/
var $UseGlobal = true;
/**
* @param string $tab
* @return string|false
*/
function GetScreenName($_tab)
{
$_ap =& $this->GetAp();
$_return = false;
switch ($_tab)
{
case 'common':
$_return = 'ap_Screen_Standard';
break;
case 'main':
$_return = $_ap->PType() ? 'ap_Screen_Standard' : 'ap_Screen_InfoTab';
break;
}
return $_return;
}
/**
* @param array $_cfg
* @return true|string
*/
function ValidateCfg($_cfg)
{
if (!isset($_cfg['adminpanel_data_path']))
{
return AP_LANG_NAME.' data folder path should be specified in "'.AP_CFG_FILE.'" config file.';
}
if (!@file_exists($_cfg['adminpanel_data_path'].'/settings/'.AP_XML_CFG_FILE))
{
return 'Path to '.AP_LANG_NAME.' data folder is incorrect as file "'.AP_XML_CFG_FILE.'" is not found.';
}
return true;
}
/**
* @return string
*/
function WriteAction()
{
$_ap =& $this->GetAp();
$_ref = null;
$_path = $_ap->GetCfg('adminpanel_data_path');
if ($_path === false || !is_writeable($_path.'/settings/'.AP_XML_CFG_FILE))
{
$this->_setError(ap_Utils::TakePhrase('CM_FILE_ERROR_OCCURRED'));
}
else if (isset($_POST['form_id']) && strlen($_POST['form_id']) > 0)
{
switch ($_POST['form_id'])
{
case 'license':
if(!$_ap->PType())
{
break;
}
$_arg = array();
$_result = true;
$_dbString = '';
if (isset($_POST['txtLicenseKey']))
{
$_arg['license_key'] = $_POST['txtLicenseKey'];
$_arg['db_string'] =& $_dbString;
if (!$this->_saveLicenseKeyData($_POST['txtLicenseKey']) ||
!$_ap->GlobalFunction('setLicenseKey', $_arg))
{
$_result = false;
}
}
else
{
$_result = false;
}
if ($_result)
{
$_idx =& $_ap->Indexs();
$this->_setInfo(ap_Utils::TakePhrase('CM_INFO_SAVESUCCESSFUL'));
if (in_array(AP_TYPE_WEBMAIL, $_idx) && $_ap->Qret($_POST['txtLicenseKey']))
{
if (strlen(trim($_dbString)) == 0)
{
$_ref = '?tab=wm&mode=db';
}
else
{
$_ref = '?mode=license';
}
}
else
{
$_ref = '?mode=license';
}
}
else
{
$this->_setError(ap_Utils::TakePhrase('CM_INFO_SAVEUNSUCCESSFUL'));
$_ref = '?mode=license';
}
break;
case 'auth':
$_arg = array();
$_result = true;
$_login = $_password = $_host = $_port = null;
if (isset($_POST['txtLogin']) && strlen($_POST['txtLogin']) > 0)
{
$_login = $_POST['txtLogin'];
}
if (isset($_POST['txtPassword1']) && isset($_POST['txtPassword2']))
{
if ($_POST['txtPassword1'] == $_POST['txtPassword2'])
{
if ($_POST['txtPassword1'] != AP_DUMMYPASSWORD)
{
$_password = $_POST['txtPassword1'];
}
}
else
{
$_result = false;
$this->_setError(ap_Utils::TakePhrase('CM_PASSWORDS_NOT_MATCH'));
}
}
if ($_result && in_array(AP_TYPE_XMAIL, $this->_pluginIndexs))
{
$_host = (isset($_POST['txtHost']) && strlen($_POST['txtHost']) > 0)? $_POST['txtHost'] : $_host;
$_port = (isset($_POST['txtPort']) && strlen($_POST['txtPort']) > 0) ? $_POST['txtPort'] : $_port;
$_arg['login'] = $_login;
$_arg['password'] = $_password;
if (!$_ap->GlobalFunction('setAdminAuth', $_arg))
{
$_result = false;
$this->_setError(ap_Utils::TakePhrase('CM_FAILED_SAVE_SETTINGS'));
}
}
if ($_result)
{
if (!$this->_saveAuthData($_login, $_password, $_host, $_port))
{
$this->_setError(ap_Utils::TakePhrase('CM_FAILED_SAVE_SETTINGS'));
$_result = false;
}
}
if ($_result)
{
$this->_setInfo(ap_Utils::TakePhrase('CM_INFO_SAVESUCCESSFUL'));
}
$_ref = '?mode=auth';
break;
case 'enable':
if ($_ap->IsEnable())
{
if (isset($_POST['clear_log_btn']))
{
if (isset($_POST['txtLogFile']))
{
if (@file_exists($_POST['txtLogFile']))
{
if (@unlink($_POST['txtLogFile']))
{
$this->_setInfo(ap_Utils::TakePhrase('CM_INFO_LOGCLEARSUCCESSFUL'));
}
else
{
$this->_setError(ap_Utils::TakePhrase('CM_INFO_ERROR'));
}
}
else
{
$this->_setInfo(ap_Utils::TakePhrase('CM_INFO_LOGCLEARSUCCESSFUL'));
}
}
else
{
$this->_setError(ap_Utils::TakePhrase('CM_INFO_ERROR'));
}
}
else if (isset($_POST['clear_all_logs_btn']))
{
$_path = $_path.'/logs/';
if (@is_dir($_path))
{
$_dh = @opendir($_path);
if ($_dh)
{
while (($_file = @readdir($_dh)) !== false)
{
if ($_file != '.' && $_file != '..' &&
($_file == AP_LOG_FILE || substr($_file, 0, 4) == 'log_'))
{
@unlink($_path.'/'.$_file);
}
}
@closedir($_dh);
}
}
}
}
$_ref = '?mode=enable';
break;
}
}
return $_ref;
}
function WritePop()
{
$_ap =& $this->GetAp();
if ($_ap->IsEnable())
{
$_path = $_ap->GetCfg('adminpanel_data_path');
if ($_path === false || !@is_dir($_path))
{
$this->_setError(ap_Utils::TakePhrase('CM_FILE_ERROR_OCCURRED'));
}
$_type = isset($_GET['type']) ? $_GET['type'] : 'null';
switch ($_type)
{
default:
break;
case 'log':
case 'log_all':
$_text = '';
$_minisize = 50000;
$_fileName = $_path.'/logs/'.AP_LOG_FILE;
if (@file_exists($_fileName))
{
$_size = @filesize($_fileName);
if ($_size && $_size > 0)
{
$_fh = @fopen($_fileName, 'rb');
if ($_fh)
{
if ($_type == 'log' && $_size > $_minisize)
{
@fseek($_fh, $_size - $_minisize);
$_text = @fread($_fh, $_minisize);
}
else
{
$_text = @fread($_fh, $_size);
}
}
else
{
$_text = 'log file '.AP_LOG_FILE.' can\'t be read';
}
}
}
$_text = strlen($_text) > 0 ? $_text : 'log file '.AP_LOG_FILE.' is empty';
$_text = str_replace('<?xml', '<?_xml', $_text);
header('Content-Type: text/plain; charset=utf-8');
header('Content-Length: '.strlen($_text));
echo $_text;
break;
case 'info':
phpinfo();
break;
}
}
}
function IncludeCommon()
{
include $this->PluginPath().'/common/constans.php';
$this->_initSettingsData();
}
function InitScreen(&$_screen, $_action)
{
$_ap =& $this->GetAp();
switch ($_action)
{
case 'initRootPath':
$_screen->SetRootPath($this->PluginPath());
return true;
case 'initMenu':
$this->_initMenu($_screen);
return true;
case 'initStandardData':
$this->_initCommonData($_screen);
return true;
case 'initTemplate':
$_screen->_template = ($_ap->XType()) ? 'main-xmail.php' : 'main-webmail.php';
return true;
case 'initInfoData':
$this->_initInfoData($_screen);
return true;
}
return false;
}
function _saveAuthData($_login, $_password, $_host = null, $_port = null)
{
$_ap =& $this->GetAp();
$_path = $_ap->GetCfg('adminpanel_data_path');
$_xml = new XmlDocument();
if($_path !== false && $_xml->LoadFromFile($_path.'/settings/'.AP_XML_CFG_FILE))
{
$_plugins =& $_xml->XmlRoot->GetChildNodeByTagName('login');
if (!$_plugins)
{
$_plugins = new XmlDomNode('login');
$_xml->XmlRoot->AppendChild($_plugins);
}
if ($_plugins)
{
$_xml_login =& $_plugins->GetChildNodeByTagName('user');
if ($_xml_login)
{
$_xml_login->Value = $_login;
}
else
{
$_xml_login = new XmlDomNode('user', $_login);
$_plugins->AppendChild($_xml_login);
}
if ($_password !== null)
{
$_xml_pass =& $_plugins->GetChildNodeByTagName('password');
if ($_xml_pass)
{
$_xml_pass->Value = $_password;
}
else
{
$_xml_pass = new XmlDomNode('password', $_password);
$_plugins->AppendChild($_xml_pass);
}
}
if ($_host !== null)
{
$_xml_host =& $_plugins->GetChildNodeByTagName('host');
if ($_xml_host)
{
$_xml_host->Value = $_host;
}
else
{
$_xml_host = new XmlDomNode('host', $_host);
$_plugins->AppendChild($_xml_host);
}
}
if ($_port !== null)
{
$_xml_port =& $_plugins->GetChildNodeByTagName('port');
if ($_xml_port)
{
$_xml_port->Value = (int) $_port;
}
else
{
$_xml_port = new XmlDomNode('port', (int) $_port);
$_plugins->AppendChild($_xml_port);
}
}
if ($_xml->SaveToFile($_path.'/settings/'.AP_XML_CFG_FILE))
{
return true;
}
}
}
else
{
$_ap->GlobalError(ap_Utils::TakePhrase('CM_FILE_ERROR_OCCURRED'));
}
return false;
}
/**
* @param string $_licenseKey
* @return bool
*/
function _saveLicenseKeyData($_licenseKey)
{
$_ap =& $this->GetAp();
$_path = $_ap->GetCfg('adminpanel_data_path');
$_xml = new XmlDocument();
if($_path !== false && $_xml->LoadFromFile($_path.'/settings/'.AP_XML_CFG_FILE))
{
$_license_node =& $_xml->XmlRoot->GetChildNodeByTagName('licensekey');
if ($_license_node)
{
$_license_node->Value = $_licenseKey;
}
else
{
$_plugins = new XmlDomNode('licensekey', $_licenseKey);
$_xml->XmlRoot->AppendChild($_plugins);
}
if ($_xml->SaveToFile($_path.'/settings/'.AP_XML_CFG_FILE))
{
return true;
}
}
else
{
$_ap->GlobalError(ap_Utils::TakePhrase('CM_FILE_ERROR_OCCURRED'));
}
return false;
}
function _initMenu(&$_screen)
{
$_ap =& $_screen->GetAp();
switch ($_ap->Tab())
{
case 'common':
$_screen->AddMenuItem(CM_MODE_AUTH, CM_TABNAME_AUTH, 'auth.php');
if ($_ap->IsEnable())
{
$_screen->AddMenuItem(CM_MODE_ENABLE, CM_TABNAME_ENABLE, 'enable.php');
}
$_screen->SetDefaultMode(CM_MODE_AUTH);
break;
case 'main':
if ($_ap->PType())
{
$_screen->AddMenuItem(CM_MODE_LICENSE, CM_TABNAME_LICENSE, 'license.php');
$_screen->SetDefaultMode(CM_MODE_LICENSE);
}
break;
}
}
function _initCommonData(&$_screen)
{
$_ap =& $_screen->GetAp();
switch ($_ap->Tab())
{
case 'common':
/* auth */
if (isset($this->_settings['user'], $this->_settings['password']))
{
$_screen->data->SetValue('UserName', $this->_settings['user']);
if (strlen($this->_settings['password']) > 0)
{
$_screen->data->SetValue('txtPassword1', AP_DUMMYPASSWORD);
$_screen->data->SetValue('txtPassword2', AP_DUMMYPASSWORD);
}
}
$_idx =& $_ap->Indexs();
if (in_array(AP_TYPE_XMAIL, $_idx))
{
if (isset($this->_settings['host'], $this->_settings['port']))
{
$_screen->data->SetValue('txtHost', $this->_settings['host']);
$_screen->data->SetValue('txtPort', (int) $this->_settings['port']);
}
}
else
{
$_screen->data->SetValue('hideClass', 'wm_hide');
}
/* enable */
if ($_ap->IsEnable())
{
$_path = $_ap->GetCfg('adminpanel_data_path');
if ($_path !== false && @is_dir($_path))
{
$_fileName = $_path.'/logs/'.AP_LOG_FILE;
$_size = 0;
$_isExist = false;
if (@file_exists($_fileName))
{
$_isExist = true;
$_size = filesize($_fileName);
}
$_screen->data->SetValue('classLogButtons', ($_size > 0) ? 'wm_button' : 'wm_hide');
$_size = ap_Utils::GetFriendlySize($_size);
$_temp = @substr(sprintf('%o', fileperms($_path)), -4);
$_path .= ' ('.$_temp.')';
$_screen->data->SetValue('txtDataFolder', $_path);
$_screen->data->SetValue('txtLogFile', $_fileName);
$_fileName .= ($_isExist) ? ' ('.$_size.')' : ' (doesn\'t exist)';
$_screen->data->SetValue('txtLogFileInfo', $_fileName);
}
}
break;
case 'main':
if ($_ap->PType() && isset($this->_settings[AP_TEST_P]))
{
$_screen->data->SetValue('txtLicenseKey', $this->_settings[AP_TEST_P]);
if (!$_ap->Qret())
{
if (strlen($this->_settings[AP_TEST_P]) == 0)
{
$_screen->data->SetValue('txtLicenseKey', '');
$_screen->data->SetValue('txtLicenseKeyText', ap_Utils::TakePhrase('CM_ENTER_VALID_KEY_HERE'));
}
else
{
$_screen->data->SetValue('txtHideGetTrialClass', 'class="wm_hide"');
$_screen->data->SetValue('txtLicenseKeyText', ap_Utils::TakePhrase('CM_ENTER_VALID_KEY'));
}
$_screen->data->SetValue('txtGetTrialId', (CAdminPanel::BType()) ? 44 : 22);
}
else
{
$_screen->data->SetValue('txtHideGetTrialClass', 'class="wm_hide"');
}
}
break;
}
}
function _initInfoData(&$_screen)
{
$_ap =& $_screen->GetAp();
switch ($_ap->Tab())
{
case 'main':
$_ap->AddCssFile($_ap->AdminFolder().'/plugins/'.$_screen->GetPluginFolderName().'/styles/styles.css');
$_screen->data->SetValue('pluginImagePath', $_ap->AdminFolder().'/plugins/'.$_screen->GetPluginFolderName().'/images/');
break;
}
}
function _initSettingsData()
{
$_ap =& $this->GetAp();
$_loginArray = $_ap->GetCfg('login');
$this->_settings[AP_TEST_P] = $_ap->GetCfg(AP_TEST_P);
if (is_array($_loginArray))
{
foreach ($_loginArray as $_key => $_value)
{
$this->_settings[$_key] = $_value;
}
}
else
{
$_ap->GlobalError(ap_Utils::TakePhrase('CM_FILE_ERROR_OCCURRED'));
}
$_idx =& $_ap->Indexs();
if (!isset($this->_settings['user'], $this->_settings['password']) ||
(in_array(AP_TYPE_XMAIL, $_idx) && !isset($this->_settings['host'], $this->_settings['port'])))
{
$_ap->GlobalError(ap_Utils::TakePhrase('CM_NOT_ENOUGH_DATA_FOR_LOGIN'));
}
}
}