<?php
/**
 * Mobile Joomla! Pro
 * http://www.mobilejoomla.com
 *
 * @version		1.2.13.pro
 * @license		GNU/GPL v2 - http://www.gnu.org/licenses/gpl-2.0.html
 * @copyright	(C) 2008-2015 Kuneri Ltd.
 * @date		February 2015
 */
defined('_JEXEC') or die('Restricted access');

jimport('joomla.plugin.plugin');

class plgMobileMJPro_Backup extends JPlugin
{
	function __construct(&$subject, $config)
	{
		parent::__construct($subject, $config);
	}

	function onMJBeforeDispatch($task)
	{
		jimport('joomla.filesystem.file');
		$app = JFactory::getApplication();
		switch($task)
		{
		case 'backup':
			include(JPATH_ADMINISTRATOR.'/components/com_mobilejoomla/config.php');

			$MobileJoomla_Settings['backup_date'] = date('Y-m-d H:i:s');
			$json = json_encode($MobileJoomla_Settings);

			$filename = 'config.backup.json';
			JFile::write(JPATH_ADMINISTRATOR.'/components/com_mobilejoomla/'.$filename, $json);

			header ('Content-Type: text/json');
			header ("Content-Disposition: attachment; filename=\"{$filename}\"");
			header ('Content-Transfer-Encoding: binary');
			header ('Content-Length: ' . strlen($json));
			header ('Cache-Control: no-transform');
			echo $json;

			$app->close();
			break;

		case 'restore':
			$file = JRequest::getVar('backupfile', null, 'files', 'array' );
			$src = $file['tmp_name'];
			if(!is_uploaded_file($src))
				$app->redirect('index.php?option=com_mobilejoomla', JText::_('COM_MJ__UPLOAD_ERROR'));

			$json = JFile::read($src);
			$data = json_decode($json, true);

			include(JPATH_ADMINISTRATOR.'/components/com_mobilejoomla/config.php');
			$params = array ();
			foreach($MobileJoomla_Settings as $key=>$value)
			{
				if($key == 'version')
					continue;

				if(isset($data[$key]))
					$MobileJoomla_Settings[$key] = $data[$key];

				if(is_numeric($MobileJoomla_Settings[$key]))
					$params[] = "'$key'=>".$MobileJoomla_Settings[$key];
				else
					$params[] = "'$key'=>'".addcslashes($MobileJoomla_Settings[$key], "'")."'";
			}

			$config = "<?php\n"
					. "defined( '_JEXEC' ) or die( 'Restricted access' );\n"
					. "\n"
					. "\$MobileJoomla_Settings=array(\n"
					. "'version'=>'".HTML_mobilejoomla::getMJVersion()."',\n"
					. implode(",\n", $params)."\n"
					. ");\n"
					. "?>";

			if(JFile::write(JPATH_ADMINISTRATOR.'/components/com_mobilejoomla/config.php', $config))
			{
				$app->redirect('index.php?option=com_mobilejoomla',
								JText::_('COM_MJ__CONFIG_UPDATED'));
			}
			else
			{
				$app->redirect('index.php?option=com_mobilejoomla',
								JText::_('COM_MJ__UNABLE_OPEN_CONFIG'));
			}
			break;
		}
	}

	function onMJDisplayConfig(&$config_blobs, &$MobileJoomla_Settings, $lists)
	{
		$doc = JFactory::getDocument();
		$doc->addStyleSheet(JURI::root(true).'/media/mjpro/backup.css');
		echo '<form id="restore_form" style="display:none" method="post" enctype="multipart/form-data" action="index.php?option=com_mobilejoomla&amp;task=restore"></form>';
		$last_backup_time = '';
		$backup_file = JPATH_ADMINISTRATOR.'/components/com_mobilejoomla/config.backup.json';
		if(is_file($backup_file))
		{
			$json = JFile::read($backup_file);
			$data = json_decode($json, true);
			if(isset($data['backup_date']) && $data['backup_date'])
				$last_backup_time = '<span class="backup-last">'.JText::_('COM_MJ__LAST_BACKUP').' '.$data['backup_date'].'</span>';
		}
		$blob = array(
				array(
					'label_blob' => JHTML::_('mjconfig.label', 'COM_MJ__BACKUP_SETTINGS', 'COM_MJ__BACKUP_SETTINGS_DESC'),
					'input_blob' => '<p id="backup-settings"><a href="index.php?option=com_mobilejoomla&task=backup">'.JText::_('COM_MJ__BACKUP').'</a> '.$last_backup_time.'</p>'
				),
				array(
					'label_blob' => JHTML::_('mjconfig.label', 'COM_MJ__RESTORE', 'COM_MJ__RESTORE_DESC'),
					'input_blob' => '<p id="restore_file"><input type="file" name="backupfile" /><input type="button" class="backup-restore" value="'.JText::_('COM_MJ__RESTORE_BTN').'" onclick="document.getElementById(\'restore_form\').appendChild(document.getElementById(\'restore_file\'));document.getElementById(\'restore_form\').submit();" /></p>'
				)
			);
		array_unshift_assoc($config_blobs['COM_MJ__ADVANCED_SETTINGS'][0], $blob, 'COM_MJ__BACKUP_RESTORE');
	}
}
