<?php
/**
 * @version $Id$
 * @package    Contact_Enhanced
 * @subpackage Controllers
 * @author     Douglas Machado {@link http://ideal.fok.com.br}
* @author     Created on 15-Dec-09
 * @license		GNU/GPL, see license.txt
 * Contact Enhanced  is free software. This version may have been modified pursuant
 * to the GNU General Public License, and as distributed it includes or
 * is derivative of works licensed under the GNU General Public License or
 * other free or open source software licenses.
 * @license		GNU/GPL, see license.txt
 * Contact Enhanced  is free software. This version may have been modified pursuant
 * to the GNU General Public License, and as distributed it includes or
 * is derivative of works licensed under the GNU General Public License or
 * other free or open source software licenses.
 */

//-- No direct access
defined('_JEXEC') or die('=;)');

jimport('joomla.application.component.controller');

/**
 * Contact_Enhanced Controller
 *
 * @package    Contact Enhanced
 * @subpackage Controllers
 */
class Contact_EnhancedsControllerconfig extends JController
{
    /**
     * constructor (registers additional tasks to methods)
     * @return void
     */
    function __construct()
    {
        parent::__construct();
        
        $this->registerTask( 'apply',	'save' );
        $this->registerTask( 'edit',	'view' );
        
    }// function

    /**
     * display the edit form
     * @return void
     */
    function view()
    {
        
        JRequest::setVar('view',	'config');
        JRequest::setVar('layout',	'default');
        JRequest::setVar('hidemainmenu', 1);

        parent::display();
    }// function

    /**
     * save a record (and redirect to main page)
     * @return void
     */
	function save()
	{
		global $mainframe;

		// Check for request forgeries
		JRequest::checkToken() or jexit( 'Invalid Token' );

		// Set FTP credentials, if given
		jimport('joomla.client.helper');
		JClientHelper::setCredentialsFromRequest('ftp');
		$ftp = JClientHelper::getCredentials('ftp');


		$config = new JRegistry('ceconfig');
		$config_array = array();

		// MAIN SETTINGS
		$config_array['image_path']			= JRequest::getVar('image_path', '', 'post', 'string');
			
		//Directory exists?
		if(!JFolder::exists(JPATH_SITE.DS.$config_array['image_path'])){
			$this->set('_task','apply'); // Force to reload congiguration page
			JError::raiseNotice('404', 'Image Path: Directory does not exists');
		}
		
		if(substr($config_array['image_path'], -1) != '/' AND strlen($config_array['image_path']) > 0){
			$config_array['image_path'] .= '/';
		}
		
		// SECURITY SETTINGS
		$config_array['uploaded_files_path']	= JRequest::getVar('uploaded_files_path', JPATH_COMPONENT.DS.'uploadedfiles'.DS, 'post', 'string');
		if(substr($config_array['uploaded_files_path'], -1) != DS AND strlen($config_array['uploaded_files_path']) > 0){
			$config_array['uploaded_files_path'] .= DS;
		}elseif(strlen(trim($config_array['image_path'])) < 2){
			$config_array['image_path'] = JPATH_COMPONENT.DS.'uploadedfiles'.DS;
		}
		// Directory exists?
		if(!JFolder::exists($config_array['uploaded_files_path'])){
			$this->set('_task','apply'); // Force to reload congiguration page
			JError::raiseNotice('404', 'Uploaded files Path: Directory does not exists');
		}
		
		// MAPS SETTINGS
		$config_array['maps_show']			= JRequest::getVar('maps_show', '', 'post', 'string');
		$config_array['maps_api_key']		= JRequest::getVar('maps_api_key', '', 'post', 'string');
		

		$config->loadArray($config_array);

		
		// Get the path of the configuration file
		$fname = JPATH_COMPONENT.DS.'configuration.php';

		// Try to make configuration.php writeable
		jimport('joomla.filesystem.path');
		if (!$ftp['enabled'] && JPath::isOwner($fname) && !JPath::setPermissions($fname, '0755')) { /// was 0644 however we were having problems when upgrading the system
			JError::raiseNotice('SOME_ERROR_CODE', 'Could not make configuration.php writable');
		}

		// Get the config registry in PHP class format and write it to configuation.php
		if (JFile::write($fname, $config->toString('PHP', 'ceconfig', array('class' => 'CEConfig')))) {
			$msg = JText::_('The Configuration Details have been updated');
		} else {
			$msg = JText::_('ERRORCONFIGFILE');
		}

		// Redirect appropriately
		$task = $this->getTask();
		switch ($task) {
			case 'apply' :
				$this->setRedirect('index.php?option='.COMPONENT_FOLDER.'&controller=config&task=edit', $msg);
				break;

			case 'save' :
			default :
				$this->setRedirect('index.php?option='.COMPONENT_FOLDER, $msg);
				break;
		}

		// Try to make configuration.php unwriteable
		//if (!$ftp['enabled'] && JPath::isOwner($fname) && !JPath::setPermissions($fname, '0444')) {
		/*if ($config_array['ftp_enable']==0 && !$ftp['enabled'] && JPath::isOwner($fname) && !JPath::setPermissions($fname, '0444')) {
			JError::raiseNotice('SOME_ERROR_CODE', 'Could not make configuration.php unwritable');
		}*/
	}

    /**
     * cancel editing a record
     * @return void
     */
    function cancel()
    {
        $msg = JText::_( 'Operation Cancelled' );
        $this->setRedirect( 'index.php?option=com_contact_enhanced', $msg );
    }//function

}//class
