<?php
class Admin_SettingsController extends Zend_Controller_Action
{
	public function init()
	{
		$this->view->addHelperPath('library/Atmail/View/Helper/', 'Atmail_View_Helper');
		$this->_helper->pluginCall('initAdminController', $this);
		 
		$this->request  = $this->getRequest();
		$this->requestParams = $this->request->getParams();
		
		$this->view->baseUrl = $this->request->getBaseUrl();
		$this->view->appBaseUrl = $this->request->getBaseUrl() . (strpos($this->request->getBaseUrl(),'index.php')?'':'/' . 'index.php');
		$this->view->siteBaseUrl = Zend_Registry::get('siteBaseUrl');
		$this->view->moduleBaseUrl = $this->view->appBaseUrl . ($this->request->module == 'default'?'':'/' . $this->request->module);
		$this->view->global = Zend_Registry::get('config')->global;
		$this->dbAdapter = Zend_Registry::get('dbAdapter');
        $this->dbTables = new dbTables();

		require_once 'application/models/admin.php';
		
		$this->admin = new admin();
		
		// check is an XML request or not only allow index to be rendered normally (i.e. full page load)
		if ($this->request->action == 'index' )
		{
			return;
			
		}
			
		if( !isset($this->view->errors) )
		{
			    
			$this->view->errors = array();
			
		}
		if( !isset($this->view->notices) )    
		{
			
			$this->view->notices = array();
			
		}
	 	if( !isset($this->view->jsonIdsToRender) )    
		{
			
			$this->view->jsonIdsToRender = array();
			
		}
		require_once 'library/jQuery/jQuery.php';
		
	}

	public function preDispatch()
	{
		
		// Check the session is active, otherwise redirect to the login page
		$this->view->userData = $this->admin->getCurrentAdminData();
		
		if( empty($this->view->userData) ) 
		{

			$this->_forward( 'timeout', 'index', 'default' );
			return;			

		}

		if( $this->view->userData['UMasterAdmin'] != '1')
		{
			throw new Exception('MasterAdmin rights required.');	
		}
		
		Atmail_Locale::setupLocaleAndTranslation( $this->view->userData['Language'] );
		
	}

	public function indexAction()
	{
	}

	public function globalAction()
	{

		$this->view->global = Zend_Registry::get('config')->global;
		$this->view->dovecot = Zend_Registry::get('config')->dovecot;
		$dbConfig = new Zend_Config_Ini('config/dbconfig.ini', 'production');
		
		$this->view->sql_user = $dbConfig->database->params->username;
		$this->view->sql_table = $dbConfig->database->params->dbname;
		$hosts = $dbConfig->database->params->host;
		if( isset($dbConfig->database2->params->host) && $dbConfig->database2->params->host != '' ) $hosts .= ',' . $dbConfig->database2->params->host;
		if( isset($dbConfig->database3->params->host) && $dbConfig->database3->params->host != '' ) $hosts .= ',' . $dbConfig->database3->params->host;
		if( isset($dbConfig->database4->params->host) && $dbConfig->database4->params->host != '' ) $hosts .= ',' . $dbConfig->database4->params->host;
		if( isset($dbConfig->database5->params->host) && $dbConfig->database5->params->host != '' ) $hosts .= ',' . $dbConfig->database5->params->host;
			
		$this->view->sql_host = $hosts;
		
		// Discover if the clean-logs.sh cron script is in place
		$dailyCronDirs = array("/etc/cron.daily", "/etc/periodic/daily");
		$this->view->cleanLogsScript = "";
		$this->view->dailyCronDir = "";
		foreach ($dailyCronDirs as $cronDir)
		{
			if (is_dir($cronDir))
			{
				$this->view->dailyCronDir = $cronDir;
				if (file_exists("$cronDir/clean-logs.sh"))
				{
					$this->view->cleanLogsScript = "$cronDir/clean-logs.sh";
					break;
				}

				if (file_exists("$cronDir/clean-logs.php"))
				{
					$this->view->cleanLogsScript = "$cronDir/clean-logs.php";
					break;
				}

			}
		}
		$groupDefaultResult = $this->admin->groupGet('default');
		$this->view->groupData = $groupDefaultResult['response']['results'];

		$this->view->haveMcrypt = function_exists('mcrypt_encrypt') && function_exists('mcrypt_decrypt');
		$this->view->haveMasterKey = isset(Zend_Registry::get('dbConfig')->database->params->masterkey);
	}

	public function globalsaveAction()
	{	
		if( $this->view->global['demo'] == 1 )
			throw new Exception('Disabled in online demo');
        
		//get existing encryption type
		//change password encryption if selected.
        $doPasswordConvert = true;
		$userPasswordEncryptionTypeCurrent = Zend_Registry::get('config')->global['userPasswordEncryptionType'];
		$externalUserPasswordEncryptionTypeCurrent = Zend_Registry::get('config')->global['externalUserPasswordEncryptionType'];
		$userPasswordEncryptionType = isset($this->requestParams['fields']['userPasswordEncryptionType']) ? $this->requestParams['fields']['userPasswordEncryptionType'] : false;
		$externalUserPasswordEncryptionType = isset($this->requestParams['fields']['externalUserPasswordEncryptionType']) ? $this->requestParams['fields']['externalUserPasswordEncryptionType'] : false;
		$master_key = (isset($this->requestParams['fields']['master_key'])) ? $this->requestParams['fields']['master_key'] : (isset(Zend_Registry::get('dbConfig')->database->params->masterkey) ? false : '');
		if(isset($this->requestParams['fields']['master_key']))
		{
			unset($this->requestParams['fields']['master_key']);
		}
		
		if( 
				($userPasswordEncryptionType && $userPasswordEncryptionType == Atmail_Enum::PASSWORD_ENCRYPTED && $userPasswordEncryptionType != $userPasswordEncryptionTypeCurrent)
			||	($externalUserPasswordEncryptionType && $externalUserPasswordEncryptionType == Atmail_Enum::PASSWORD_ENCRYPTED && $externalUserPasswordEncryptionType != $externalUserPasswordEncryptionTypeCurrent) )
		{
			if($master_key == '')
			{
				if(!isset(Zend_Registry::get('dbConfig')->database->params->masterkey))
				{
					throw new Atmail_Config_Exception("No master key has been provided.");
				}
				else
				{
					$master_key = false;
				}
			}
		}
		        
	
		if($this->requestParams['dovecot']['authType'] == 'ldap') 
		{

			//split hostname on comma for multiple backup LDAP server support
			$hosts = explode(',', $this->requestParams['dovecot']['ldap_host']);
			foreach( $hosts as $host )
			{
				
				try
				{
			
					// First check, can we connect to LDAP?
					$options = array('host' => trim($host));
					$ldap = new Zend_Ldap($options);
					// Trigger an auth fail, however required to throw an exception if the LDAP server does not connect
					$var = $ldap->bind('atmailtest', 'test');
					$doPasswordConvert = false;
				
				}
				catch( Exception $e )
				{

					$lastExceptionMessage = $e->getMessage();
					preg_match('/0x51/', $lastExceptionMessage, $m);

					// Show if the LDAP server fails, network error (0x51)
					if($m[0] == '0x51')
						throw new Exception('LDAP error: ' . $lastExceptionMessage);

				}
				
			}

		}

		// Else, continue as normal if LDAP or SQL
		try
		{
			
			$failure = false;
			require_once 'application/models/config.php';
					
			//if password unchanged then no change
			if( !isset($this->requestParams['fields']['sql_pass']) || $this->requestParams['fields']['sql_pass'] == md5('__UNCHANGED__') )
				$this->requestParams['fields']['sql_pass'] = Zend_Registry::get('config')->global['sql_pass'];
			
				
			$dbArray = 	array(
						'host'     => $this->requestParams['fields']['sql_host'],
						'username' => $this->requestParams['fields']['sql_user'],
						'password' => $this->requestParams['fields']['sql_pass'],
						'dbname'   => $this->requestParams['fields']['sql_table']
					);
			$dbHosts = explode(',', $this->requestParams['fields']['sql_host']);
			// Attempt connection to SQL server
			$host = '';
			$k = 0;
			require_once('library/Zend/Db/Adapter/Pdo/Mysql.php');
			try
	        {
				
				foreach( $dbHosts as $k => $host )
				{
				
					$dbHosts[$k] = trim($dbHosts[$k]);
					if( $dbHosts[$k] == '' ||$dbHosts[$k] == 'localhost' )
						$dbHosts[$k] = '127.0.0.1';
					$dbArray['host'] = $dbHosts[$k];
					$db = new Zend_Db_Adapter_Pdo_Mysql($dbArray);
					$db->getConnection();
				
				}
				
			}
			catch (Exception $e)
			{
			    
				throw new Atmail_Config_Exception("Unable to connect to " . $dbHosts[$k] . " with supplied settings: " . $e->getMessage() );
			    
			}
			$dbArray['host'] = implode(',',$dbHosts);
			$this->requestParams['fields']['sql_host'] = implode(',',$dbHosts);
			// enable imap logging if debug = 1
			if( $this->requestParams['fields']['debug'] == 1 )
				$this->requestParams['fields']['imapLogging'] = 1;
			else
				$this->requestParams['fields']['imapLogging'] = 0;

			config::save( 'global', $this->requestParams['fields'] );
			if( $this->view->global['install_type'] == 'server' )
				config::save( 'dovecot', $this->requestParams['dovecot'] );

			// First, update the SQL config in existing config files on disk
			config::modifyDbConfigInExistingServerConfigFiles( Zend_Registry::get('config')->global );
			
			// publish db config
			if( !config::saveIniChanges(Zend_Registry::get('config')->global) )
				$failure = true;
			else
			{
				
				//load up and replace dbConfig in the registry because other config writers depend on it containing the updated fields
				$dbConfig = new Zend_Config_Ini( APP_ROOT . 'config/dbconfig.ini', 'production');
				Zend_Registry::set('dbConfig', $dbConfig);
				
			}
			if( !$failure && $this->view->global['install_type'] == 'server' )
			{
			
				// Next, update Dovecot ( LDAP or SQL auth used )
				config::publishServerConfigFiles('dovecot');

				// Required to publish SMTP auth ( LDAP or SQL )
				config::publishServerConfigFiles('exim');
				
				// Call the SA save function, to store the new mysql pass in all the SA config files (bayes, awl, etc)
				config::publishServerConfigFiles('spamassassin');
			
				// Which type is the user using, write to the new file
				if($this->request->dovecot['authType'] == 'ldap') 
					config::publishServerConfigFiles('dovecotldap');
				else
					config::publishServerConfigFiles('dovecotsql');
			
			}
			
			if( !$failure && $master_key !== false && $master_key != '')
			{
				// new master key
				// this is used for all session functions if found
				// make sure when this is processed that we update the sessions with encryption
				if(!config::saveIniChanges(array_merge(array('master_key' => $master_key), Zend_Registry::get('config')->global)) )
				{
					$failure = true;
				}
				else
				{				
					jQuery::evalScript("$('#userPasswordMasterKey').hide();");

					// configuration has been pushed
					// lets encrypt the sessions
					Atmail_Session_SaveHandler_DbUserSession::enableEncryptedSessions($master_key);
					Atmail_Session_SaveHandler_DbAdminSession::enableEncryptedSessions($master_key);
				}
			}
			
			//change password encryption is changed.
			if( !$failure
				&& $doPasswordConvert
				&&  (
						($userPasswordEncryptionType && $userPasswordEncryptionType != $userPasswordEncryptionTypeCurrent)
					||	($externalUserPasswordEncryptionType && $externalUserPasswordEncryptionType != $externalUserPasswordEncryptionTypeCurrent)
					)
				)
			{

				if($userPasswordEncryptionType && $userPasswordEncryptionType != $userPasswordEncryptionTypeCurrent)
				{
					// changeing user
					users::encryptPasswords( $userPasswordEncryptionTypeCurrent, $userPasswordEncryptionType );						
				}
				if($externalUserPasswordEncryptionType && $externalUserPasswordEncryptionType != $externalUserPasswordEncryptionTypeCurrent)
				{
					// external user update
					users::encryptPasswords( $externalUserPasswordEncryptionTypeCurrent, $externalUserPasswordEncryptionType );						
				}

			}
			// Prompt to save/reset services
			if($failure)
			{
				throw new Atmail_Config_Exception("There was a failure saving your changes");
			}
			else
			{

				// Success!
				if( $this->view->global['install_type'] == 'server' ) {
					jQuery::evalScript("$.simpleFlash('Settings saved. Restart Atmail services for changes to take effect','important');");
				}
				else {
					jQuery::evalScript("$.simpleFlash('Settings saved.');"); 
				}
			}
			
			$defaultSettings = array();
			if( array_key_exists('licenseType', $this->request->fields) )
			{
				
				$this->admin->groupUpdate( 'default', array('licenseType' => $this->request->fields['licenseType']) );
				
			}
		}
		catch( Exception $e )
		{

			jQuery::evalScript("$('#licence-error',  '#global').hide();");
			throw new Exception('Failed saving config: ' . $e->getMessage());	

		}
		$this->render('global/jsonresponse', null, true);
		
	}

	public function webmailAction()
	{

		$this->view->global = Zend_Registry::get('config')->global;
        $groupDefaultResult = $this->admin->groupGet('default');

		if( $groupDefaultResult['status'] == 'success' )
		{
			$this->view->groupDefault = $groupDefaultResult['response']['results'];
		}                                
		else
		{
			throw new Exception( $groupDefaultResult['response']['message'] );
		}
	}

	public function webmailsaveAction()
	{

		if( $this->view->global['demo'] == 1 )
		{

			throw new Exception('Disabled in online demo');

		}
		
		// Turn off SMTP auth user/password if toggled off
		if( empty($this->request->fields['smtp_auth']) )
		{
			$this->request->fields['smtpauth_password'] = "";
			$this->request->fields['smtpauth_username'] = "";			
		}
		
		// If smtp auth is not defined, set as null
		if( empty($this->request->fields['smtpauth_password']) )
		{
			$this->request->fields['smtpauth_password'] = "";
		}

		if( empty($this->request->fields['smtpauth_username']) )
		{
			$this->request->fields['smtpauth_username'] = "";
		}

		// Verify the submitted data// Verify the submitted data
		try
		{
			$smtpHost = $this->request->fields['smtphost'];
			if (empty($smtpHost))
				$smtpHost = 'localhost';
				
			// Try to connect to SMTP host
			require_once('Zend/Mail/Protocol/Smtp/Auth/Login.php');
			require_once('Zend/Mail/Protocol/Smtp.php');

			if ($this->request->fields['smtpauth_username'] != "" && $this->request->fields['smtpauth_password'] != "")
			{
	    			$protocol = new Zend_Mail_Protocol_Smtp_Auth_Login($smtpHost, null, array('username' => $this->request->fields['smtpauth_username'], 'password' => $this->request->fields['smtpauth_password']));
			}
			else
			{
	   			$protocol = new Zend_Mail_Protocol_Smtp($smtpHost);
			}
		
		    $protocol->connect();
		    if(PHP_OS == "Darwin")
		   	{
		   		$protocol->helo(php_uname('n'));
		    }
		    else
		    {
  		   		$protocol->helo($smtpHost);
		    }
		    try{
		        $protocol->mail('root');
		    	$protocol->rcpt('root');
			}
			catch(Zend_Exception $e)
			{
				if(strpos($e->getMessage(), 'need fully-qualified hostname')!=FALSE)
					throw($e);
			}
			
			
			if($this->request->fields['cacheType'] == 'memcache')
			{
				// try the details provided	
				$memcache = new Memcache;
				$server = array(
					'host'				=> $this->request->fields['cacheMemcacheServer'], 
					'port'				=> $this->request->fields['cacheMemcachePort'], 
					'persistent'		=> true, 
					'weight'			=> 1, 
					'timeout'			=> $this->request->fields['cacheMemcacheTimeout'], 
					'retry_interval'	=> 15
				);
				$memcache->addServer(	$server['host'], $server['port'], $server['persistent'],
                                        $server['weight'], $server['timeout'],
                                        $server['retry_interval']);
				$memcache->set('test', array('value', time(), 1200), 0, 1200);
				$test = $memcache->get('test');
				if(!is_array($test) || (is_array($test) && $test[0] != 'value'))
				{
					throw new Exception('Cannot communicate with memcache service.');	
				}
			}

			if($this->request->fields['remoteServersOverwrite'])
			{
				$this->request->fields['remoteServers'] = '';
			}

			if($this->request->fields['remoteDomainsOverwrite'])
			{
				$this->request->fields['remoteDomains'] = '';
			}
			$this->request->fields['remoteDomains'] = str_replace(" ", "\n", $this->request->fields['remoteDomains']);
			$this->request->fields['remoteServers'] = str_replace(" ", "\n", $this->request->fields['remoteServers']);
			
			//pull out groupDefault data before pushing rest to Config
			$defaultSettings = array();
			if( array_key_exists('Webmail', $this->request->fields) )
			{
				
				$defaultSettings = array('Webmail' => $this->request->fields['Webmail']);
				unset( $this->request->fields['Webmail'] );
				$this->admin->groupUpdate('default', $defaultSettings);
				
			}
			
			require_once 'application/models/config.php';
			if ( array_key_exists('default_domain', $this->request->fields) )
			{
				config::save('dovecot', array('default_domain' => $this->request->fields['default_domain']));
				unset($this->request->fields['default_domain']);
			}
			
			config::save('global', $this->request->fields);

			jQuery::addMessage( $this->view->translate('Settings saved.'));
		
		}
	    catch (Zend_Exception $e)
	    { 	
			$error = $e->getMessage();

	    	if($this->request->fields['smtpauth_username'] != "" && $this->request->fields['smtpauth_password'] != "")
	    	{
	    		$error .= "<br><br>The SMTP authentication details provided are incorrect. Please verify you have the correct username and password. Check your SMTP configuration for further details, or use an SMTP server which you have IP relay permissions.";
	    	}
			
			if(strpos($error, 'need fully-qualified hostname') != FALSE)
			{
				// fully qualified domain name for helo required (probably osx)
				// so lets tell the user in a human readable way
				$error .= "<br><br>The SMTP authentication details provided are incorrect for this server. Please verify you are using the fully qualified hostname for the smtp server. I.e. 'example.hostname' rather then 'localhost'.<br><br>It appears that your machines hostname is : " . php_uname('n');
			}
	
			jQuery::addError('Failed saving config: ' . $error);
		}

		$this->render('global/jsonresponse', null, true);
		
	}

	public function groupwareAction()
	{

		//$this->view->global = Zend_Registry::get('config')->global;
        $groupDefaultResult = $this->admin->groupGet('default');
		
		if( $groupDefaultResult['status'] == 'success' )
		{
			
			$this->view->groupDefault = $groupDefaultResult['response']['results'];
			
		}                                
		else
		{
			
			throw new Exception( $groupDefaultResult['response']['message'] );
			
		}

	}

	public function groupwaresaveAction()
	{
		if( $this->view->global['demo'] == 1 )
		{
			
			throw new Exception('Disabled in online demo');
			
		}
		
		try
		{
		
			//pull out groupDefault data before pushing rest to Config
			$defaultSettings = array();
			if( array_key_exists('GroupwareZone', $this->request->fields) || array_key_exists('Sync', $this->request->fields) )
			{
				
				if( array_key_exists('GroupwareZone', $this->request->fields) )
				{
					
					$defaultSettings['GroupwareZone'] = $this->request->fields['GroupwareZone'];
					//unset( $this->request->fields['GroupwareZone'] );
					
				}
				if( array_key_exists('Sync', $this->request->fields) )
				{
					
					$defaultSettings['Sync'] = $this->request->fields['Sync'];
					//unset( $this->request->fields['Sync'] );
					
				}
				if( array_key_exists('WebSyncShared', $this->request->fields) )
				{
					
					$defaultSettings['WebSyncShared'] = $this->request->fields['WebSyncShared'];
					//unset( $this->request->fields['WebSyncShared'] );
					
				}
				if( array_key_exists('WebSyncGlobal', $this->request->fields) )
				{
					
					$defaultSettings['WebSyncGlobal'] = $this->request->fields['WebSyncGlobal'];
					//unset( $this->request->fields['WebSyncGlobal'] );
					
				}
				if( array_key_exists('SharedAbook', $this->request->fields) )
				{
					$defaultSettings['SharedAbook'] = $this->request->fields['SharedAbook'];
					//unset( $this->request->fields['SharedAbook'] );
				}
				if( count($defaultSettings) > 0 )
				{
					$this->admin->groupUpdate('default', $defaultSettings);
				}
			}
			
			//require_once 'application/models/config.php';
			//config::save('global', $this->request->fields);
			
			jQuery::addMessage( $this->view->translate('The settings have been updated.') );
		
		}
		catch( Exception $e )
		{
		
			jQuery::addMessage('Failed saving config: ' . $e->getMessage());
		
		}

		$this->render('global/jsonresponse', null, true);
		
	}
	
	public function brandingAction()
	{
		
		$this->view->global = Zend_Registry::get('config')->global;
		
	}

	public function brandingsaveAction()
	{
		if( $this->view->global['demo'] == 1 )
		{
			
			throw new Exception('Disabled in online demo');
			
		}
		
		// Set the following fields as blank in the DB if the admin specifies
		foreach(array('footer_msg', 'welcome_msg', 'webmail_url', 'mailserver_hostname') as $field)
		{
		
			if(!isset($this->request->fields[$field]))
			{
		
				$this->request->fields[$field] = "";
		
			}
		
		}

		try
		{
			require_once 'application/models/config.php';
			config::save('global', $this->request->fields);
			jQuery::addMessage( $this->view->translate('The settings have been updated.') );
		
		}
		catch( Exception $e )
		{
			
			jQuery::addMessage('Failed saving config: ' . $e->getMessage());
			
		}

		$this->render('global/jsonresponse', null, true);
		
	}

	public function defaultsAction()
	{
		
		// Load the languages available
		$this->view->localeString = $this->view->UserSettings['Language'];
		$this->view->availableLocales = Atmail_Locale::getAvailableLocales();
		
		$this->view->defaultUserSettings = Zend_Registry::get('config')->defaultUserSettings;
		$this->view->global = Zend_Registry::get('config')->global;
		
		require_once 'application/models/calendar.php';
		
		$tzList =  calendar::getZoneInfoList();
		$this->view->timezoneList = '';
		
		foreach($tzList as $tz)
		{
			$this->view->timezoneList .= '<option value="' . $tz . '">' . $tz . '</option>';
		}
		
		//get list of available themes
		$this->view->availableThemes = findAvailableThemes();
		
	}

	public function defaultssaveAction()
	{
		
		if( $this->view->global['demo'] == 1 )
		{
			
			throw new Exception('Disabled in online demo');
			
		}
		if(array_key_exists('AutoReload_Bitfield', $this->requestParams['fields']))
		{
			unset($this->requestParams['fields']['AutoReload_Bitfield']);
		}

		// Set the following fields as blank in the DB if the admin specifies
		foreach(array('CalDavUrl', 'CardDavUrl') as $field)
		{
			
			if( !isset($this->request->fields[$field]) )
			{
			
				$this->request->fields[$field] = "";
			
			}
			
		}

		if( empty($this->request->fields['ViewThreads']) )
		{
			
			$this->request->fields['ViewThreads'] = 0;
			
		}
		
		//process and remove themeEnabled saving to global
		require_once 'application/models/config.php';
		if( isset($this->requestParams['fields']['themeEnabled']) )
		{
			
			try
			{

				config::save('global', array('themeEnabled' => $this->requestParams['fields']['themeEnabled']) );

                //reset existing user themes if changing is disabled
				if( (String)$this->requestParams['fields']['themeEnabled'] === '0' )
				{
					
					$this->dbAdapter->update($this->dbTables->UserSettings, array('cssStyleTheme' => $this->requestParams['fields']['cssStyleTheme']) );
					
				}
			    unset($this->requestParams['fields']['themeEnabled']);
				
			}
			catch( Exception $e )
			{

				jQuery::addMessage('Failed saving themeEnabled: ' . $e->getMessage());
                $this->render('global/jsonresponse', null, true);
				return;
				
			}

		}

		try
		{
		
			config::save('defaultUserSettings', $this->requestParams['fields']);
			jQuery::addMessage( $this->view->translate('The settings have been updated.') );
		
		}
		catch( Exception $e )
		{
			
			jQuery::addMessage('Failed saving config: ' . $e->getMessage());
			
		}

		$this->render('global/jsonresponse', null, true);
		
	}

        public function ldapAction()
        {

                $this->view->global = Zend_Registry::get('config')->global;

        }

        public function ldapsaveAction()
        {

                if( $this->view->global['demo'] == 1 )
                {

                        throw new Exception('Disabled in online demo');

                }

                try
                {

                        require_once 'application/models/config.php';
                        config::save('global', $this->request->fields);
                	config::publishServerConfigFiles('dovecotldap');
		        jQuery::addMessage( $this->view->translate('The LDAP settings have been updated.') );

                }
                catch( Exception $e )
                {

                        jQuery::addMessage('Failed saving config: ' . $e->getMessage());

                }

                $this->render('global/jsonresponse', null, true);

        }

	public function uploadlogoAction()
	{

		require_once 'library/PHPThumb/ThumbLib.inc.php';
		if( $this->view->global['demo'] == 1 )
			throw new Exception('Disabled in online demo');

		$params = $this->getRequest()->getParams();
		if( !array_key_exists('type', $params) )
			$params['type'] = 'webmail';
		//use as is if no GD or ImageMagick
		try
		{

			if(array_key_exists('type', $params) && $params['type'] == 'login')
			{

				$inputName = 'Logo-login';
				$thumb = PhpThumbFactory::create($_FILES[$inputName]['tmp_name']);
				$thumb->resize(400, 400);

			}
			else
			{

				$inputName = 'Logo';
				$thumb = PhpThumbFactory::create($_FILES[$inputName]['tmp_name']);
				$thumb->resize(216, 51);

			}
			$imageDb = base64_encode($thumb->getImageAsString());
			$imageStat = $thumb->getNewDimensions();

		}
		catch( Exception $e )
		{

			//if we get here then use uploaded file as is.
			$imageDb = base64_encode( file_get_contents($_FILES[$inputName]['tmp_name']) );
			if( array_key_exists('type', $params) && $params['type'] == 'login' )
				$imageStat = array('newWidth' => '98', 'newHeight' => '51');
			else
				$imageStat = array('newWidth' => '78', 'newHeight' => '58');

		}
		$result = $this->saveBrandingImgToDb($imageStat, $imageDb);
		unlink($_FILES[$inputName]['tmp_name']);
		if( $result !== false )
		{

			if( array_key_exists('type', $params) && $params['type'] == 'login' )
				jQuery::addMessage( $this->view->translate('The Login logo has been updated.') );
			else
				jQuery::addMessage( $this->view->translate('The main UI logo has been updated.') );

		} 
		else 
			jQuery::addMessage('Failed saving branding logo');
        $this->render('global/jsonresponse', null, true);

	}

	// Display a subadmin/branded logo
	public function viewlogoAction()
	{

		$requestParams = $this->getRequest()->getParams();

		// If empty, assume login image
		if(empty($requestParams['type']))
			$requestParams['type'] = 'login';

		// First, find the Subadmins main root/URL.
		//$serverName = $_SERVER['SERVER_NAME'];

		//$serverNameOverwrite = $this->dbAdapter->fetchOne("select keyDomain from Config where " . $this->dbAdapter->quoteInto('keyValue = ?', $serverName) . " and keyDomain != ''");

		//if( !empty($serverNameOverwrite) ) {
		//	$serverName = $serverNameOverwrite;
		//}

		//$imageData = $this->dbAdapter->fetchOne("select ImageData from ImageBranding where Account=? and Type=?", array($serverName, $requestParams['type']));
		$imageData = $this->dbAdapter->fetchOne("select ImageData from ImageBranding where Type=?", array($requestParams['type']));

		if(empty($imageData)) {

			// Now display
			$this->getResponse()->setHeader('Content-Type','image/png')->appendBody(file_get_contents("images/1x1.png"));

		} else {

			// Now display
			$this->getResponse()->setHeader('Content-Type','image/png')->appendBody(base64_decode($imageData));

		}


		$this->_helper->viewRenderer->setNoRender();

	}

	private function saveBrandingImgToDb($imageStat, $imageDb, $username='')
	{

		if( $this->view->userData['UMasterAdmin'] == '1' && !empty($username) ) {

		} else {
			$username = $this->view->userData['Username'];
		}

		// Check if an image already exists, if so, SQL update
		$imageExists = $this->dbAdapter->fetchOne("select id from ImageBranding where Account=? and Type=?", array(
			$username,
			$this->requestParams['type']
		));

		if( !empty($imageExists) ) {

			// Already exists, update the existing copy
			$stats = $this->dbAdapter->update("ImageBranding", array(
					'Account' => $username,
					'ImageData' => $imageDb,
					'ImageHeight' => $imageStat['newHeight'],
					'ImageWidth' => $imageStat['newWidth'],
					'Type' => $this->requestParams['type']
				),

				"Account = " . $this->dbAdapter->quote($username) . " AND " .
				"Type = " . $this->dbAdapter->quote($this->requestParams['type'])
			);


		} else {

			// New record, add in!
			$this->dbAdapter->insert("ImageBranding", array(
				'Account' => $username,
				'ImageData' => $imageDb,
				'ImageHeight' => $imageStat['newHeight'],
				'ImageWidth' => $imageStat['newWidth'],
				'Type' => $this->requestParams['type'],
				'DateAdded' => new Zend_Db_Expr('NOW()')
			));
		}

		return 1;

	}
	
}
