<?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');

//ACL check
if(version_compare(JVERSION, '1.6', '>=') &&
		!(JFactory::getUser()->authorise('core.manage', 'com_mobilejoomla')))
	return JError::raiseWarning(403, JText::_('JERROR_ALERTNOAUTHOR'));

jimport('joomla.filesystem.file');
jimport('joomla.filesystem.folder');
jimport('joomla.event.dispatcher');

require_once(JPATH_COMPONENT.'/classes/mobilejoomla.php');
MobileJoomla::loadLanguageFile('com_mobilejoomla', JPATH_ADMINISTRATOR);

require_once(JPATH_COMPONENT.'/mobilejoomla.html.php');

$task = JRequest::getCmd('task');
$app = JFactory::getApplication();

JPluginHelper::importPlugin('mobile');

$dispatcher = JDispatcher::getInstance();
$dispatcher->trigger('onMJBeforeDispatch', array($task));

$extmanager = JRequest::getCmd('extmanager');
if(!empty($extmanager))
{
	require_once dirname(__FILE__).'/classes/mjextmanager.php';

	if(!headers_sent())
		header('Content-Type: text/html');

	switch(JRequest::getCmd('extmanager'))
	{
	case 'view_modules':
		require dirname(__FILE__).'/admin_tpl/modules.php';
		break;

	case 'view_plugins':
		require dirname(__FILE__).'/admin_tpl/plugins.php';
		break;

	case 'set_module_state':
		$id = JRequest::getInt('id');
		$markup = JRequest::getWord('markup');
		$published = MJExtManager::changeState('#__mj_modules', $id, $markup);
		echo MJExtManager::getImage($published);
		break;

	case 'set_plugin_state':
		$id = JRequest::getInt('id');
		$markup = JRequest::getWord('markup');
		$published = MJExtManager::changeState('#__mj_plugins', $id, $markup);
		echo MJExtManager::getImage($published);
		break;
	}
	$app->close();
}

// TODO: transform into JController-based controller
switch($task)
{
	case 'apply':
		saveconfig($task);
		break;
	case 'cancel':
		$app->redirect('index.php');
		break;
	case 'update':
		update();
		break;
	default:
		showconfig();
		break;
}

function showconfig()
{
	$isJoomla15 = (substr(JVERSION,0,3) == '1.5');

	/** @var array $MobileJoomla_Settings */
	include JPATH_ADMINISTRATOR.'/components/com_mobilejoomla/config.php';

	include_once JPATH_ADMINISTRATOR.'/components/com_mobilejoomla/classes/jhtmlmjconfig.php';

	$app = JFactory::getApplication();
	if(!JFile::exists(JPATH_ADMINISTRATOR.'/components/com_mobilejoomla/config.php'))
		$app->enqueueMessage(JText::_('COM_MJ__CONFIG_MISSING'), 'error');
	elseif(!is_writable(JPATH_ADMINISTRATOR.'/components/com_mobilejoomla/config.php'))
		$app->enqueueMessage(JText::_('COM_MJ__CONFIG_UNWRITEABLE'), 'warning');

	$db = JFactory::getDBO();
	$query = 'SELECT DISTINCT(position) FROM #__modules WHERE client_id = 0';
	$db->setQuery($query);
	if(version_compare(JVERSION, '3.0', '>='))
		$positions = $db->loadColumn();
	else
		$positions = $db->loadResultArray();
	$positions = (is_array($positions)) ? $positions : array ();

	$templateBaseDir = JPATH_SITE.'/templates/';
	$templates = array ();
	$templates[] = array ('value' => '');

	jimport('joomla.installer.installer');
	$templateDirs = JFolder::folders($templateBaseDir);
	foreach($templateDirs as $templateDir)
	{
		$templateFile = $templateBaseDir.$templateDir.'/templateDetails.xml';
		if(!JFile::exists($templateFile))
			continue;
		if(version_compare(JVERSION, '2.5.5', '<'))
			$xml = JApplicationHelper::parseXMLInstallFile($templateFile);
		else
			$xml = JInstaller::parseXMLInstallFile($templateFile);
		if($xml['type'] != 'template')
			continue;
		$templates[] = array ('value' => $templateDir);

		$xml = simplexml_load_file($templateFile);
		if(isset($xml->positions[0]))
			foreach($xml->positions[0] as $position)
				$positions[] = (string)$position;
	}
	$positions[] = '';
	$positions = array_unique($positions);
	sort($positions);

	$modulepositions = array ();
	foreach($positions as $position)
		$modulepositions[] = array ('value' => $position);

	$lists = array ();

	JPluginHelper::importPlugin('mobile');
	$mjver = HTML_mobilejoomla::getMJVersion();
	if(stripos($mjver, 'pro')===false)
	{
		include_once JPATH_ADMINISTRATOR.'/components/com_mobilejoomla/classes/mjprostub.php';
		$dispatcher = JDispatcher::getInstance();
		new plgMobileMJProStub($dispatcher);
	}

	$lists['dbsize'] = $app->triggerEvent('onGetDatabaseSize');

	$img = array (JHtml::_('select.option', 0, JText::_('COM_MJ__IMG_DONT_RESCALE')),
	              JHtml::_('select.option', 2, JText::_('COM_MJ__IMG_RESCALE')),
	              JHtml::_('select.option', 3, JText::_('COM_MJ__IMG_FIXED_RESCALE_RATIO')),
	              JHtml::_('select.option', 1, JText::_('COM_MJ__IMG_REMOVE_ALL')));
	$caching = array (JHtml::_('select.option', 1, JText::_('COM_MJ__JOOMLA')),
					  JHtml::_('select.option', 0, JText::_('COM_MJ__DISABLE')));
	$gzip = array (JHtml::_('select.option', 1, JText::_('COM_MJ__AUTO')),
				   JHtml::_('select.option', 0, JText::_('COM_MJ__OFF')));
	$simplehead = array (JHtml::_('select.option', 0, JText::_('COM_MJ__HEAD_STANDARD')),
	                     JHtml::_('select.option', 1, JText::_('COM_MJ__HEAD_SIMPLIFIED')));
	$contenttype = array (JHtml::_('select.option', 0, JText::_('COM_MJ__AUTO')),
	                      JHtml::_('select.option', 1, 'application/vnd.wap.xhtml+xml'),
	                      JHtml::_('select.option', 2, 'application/xhtml+xml'),
	                      JHtml::_('select.option', 3, 'text/html'),
	                      JHtml::_('select.option', 4, 'text/xhtml'));
	$xhtmldoctype = array (JHtml::_('select.option', 0, JText::_('COM_MJ__NONE')),
	                       JHtml::_('select.option', 1, 'WAPFORUM/WML2.0'),
	                       JHtml::_('select.option', 2, 'WAPFORUM/XHTML Mobile 1.0'),
	                       JHtml::_('select.option', 3, 'WAPFORUM/XHTML Mobile 1.1'),
	                       JHtml::_('select.option', 4, 'OMA/XHTML Mobile 1.2'),
	                       JHtml::_('select.option', 5, 'W3C/XHTML Basic 1.0'),
	                       JHtml::_('select.option', 6, 'W3C/XHTML Basic 1.1'),
	                       JHtml::_('select.option', 7, 'W3C/XHTML 1.0 Transitional'),
	                       JHtml::_('select.option', 8, 'W3C/XHTML 1.0 Strict'),
	                       JHtml::_('select.option', 9, 'W3C/HTML 4.0 Mobile'));
	$wmldoctype = array (JHtml::_('select.option', 0, JText::_('COM_MJ__NONE')),
	                     JHtml::_('select.option', 1, 'WAPFORUM/WML1.1'),
	                     JHtml::_('select.option', 2, 'WAPFORUM/WML1.2'));

	$lists['private.templates'] = $templates;
	$lists['private.gzip'] = $gzip;
	$lists['private.img'] = $img;
	$lists['private.modulepositions'] = $modulepositions;

	//Global settings
	$lists['global.removetags'] = JHtml::_('mjconfig.booleanparam', 'global.removetags', $MobileJoomla_Settings);
	$lists['global.img'] = JHtml::_('mjconfig.listparam', 'global.img', $img, $MobileJoomla_Settings);
	$lists['global.img_addstyles'] = JHtml::_('mjconfig.booleanparam', 'global.img_addstyles', $MobileJoomla_Settings);
	$lists['global.componenthome'] = JHtml::_('mjconfig.booleanparam', 'global.componenthome', $MobileJoomla_Settings);
	$lists['global.gzip'] = JHtml::_('mjconfig.radioparam', 'global.gzip', $gzip, $MobileJoomla_Settings);

	//Plugin settings
	$lists['caching'] = JHtml::_('mjconfig.radioparam', 'caching', $caching, $MobileJoomla_Settings);
	$lists['httpcaching'] = JHtml::_('mjconfig.booleanparam', 'httpcaching', $MobileJoomla_Settings);

	//XHTML/WAP2.0 devices
	$lists['xhtml.template'] = JHtml::_('mjconfig.templateparam', 'xhtml.template', $templates, $MobileJoomla_Settings);
	$lists['xhtml.gzip'] = JHtml::_('mjconfig.g_radioparam', 'xhtml.gzip', $gzip, $MobileJoomla_Settings);

	//WAP devices
	$lists['wml.template'] = JHtml::_('mjconfig.templateparam', 'wml.template', $templates, $MobileJoomla_Settings);
	$lists['wml.gzip'] = JHtml::_('mjconfig.g_radioparam', 'wml.gzip', $gzip, $MobileJoomla_Settings);

	//IMODE devices
	$lists['chtml.template'] = JHtml::_('mjconfig.templateparam', 'chtml.template', $templates, $MobileJoomla_Settings);
	$lists['chtml.gzip'] = JHtml::_('mjconfig.g_radioparam', 'chtml.gzip', $gzip, $MobileJoomla_Settings);

	//iPhone/iPod devices
	$lists['iphone.template'] = JHtml::_('mjconfig.templateparam', 'iphone.template', $templates, $MobileJoomla_Settings);
	$lists['iphone.gzip'] = JHtml::_('mjconfig.g_radioparam', 'iphone.gzip', $gzip, $MobileJoomla_Settings);

	//mobile_smartphone template setting
	$lists['xhtml.header1'] = JHtml::_('mjconfig.positionparam', 'xhtml.header1', $modulepositions, $MobileJoomla_Settings);
	$lists['xhtml.header2'] = JHtml::_('mjconfig.positionparam', 'xhtml.header2', $modulepositions, $MobileJoomla_Settings);
	$lists['xhtml.header3'] = JHtml::_('mjconfig.positionparam', 'xhtml.header3', $modulepositions, $MobileJoomla_Settings);
	$lists['xhtml.middle1'] = JHtml::_('mjconfig.positionparam', 'xhtml.middle1', $modulepositions, $MobileJoomla_Settings);
	$lists['xhtml.middle2'] = JHtml::_('mjconfig.positionparam', 'xhtml.middle2', $modulepositions, $MobileJoomla_Settings);
	$lists['xhtml.middle3'] = JHtml::_('mjconfig.positionparam', 'xhtml.middle3', $modulepositions, $MobileJoomla_Settings);
	$lists['xhtml.componenthome'] = JHtml::_('mjconfig.g_booleanparam', 'xhtml.componenthome', $MobileJoomla_Settings);
	$lists['xhtml.footer1'] = JHtml::_('mjconfig.positionparam', 'xhtml.footer1', $modulepositions, $MobileJoomla_Settings);
	$lists['xhtml.footer2'] = JHtml::_('mjconfig.positionparam', 'xhtml.footer2', $modulepositions, $MobileJoomla_Settings);
	$lists['xhtml.footer3'] = JHtml::_('mjconfig.positionparam', 'xhtml.footer3', $modulepositions, $MobileJoomla_Settings);
	$lists['xhtml.jfooter'] = JHtml::_('mjconfig.booleanparam', 'xhtml.jfooter', $MobileJoomla_Settings);
	$lists['xhtml.simplehead'] = JHtml::_('mjconfig.listparam', 'xhtml.simplehead', $simplehead, $MobileJoomla_Settings);
	$lists['xhtml.allowextedit'] = JHtml::_('mjconfig.booleanparam', 'xhtml.allowextedit', $MobileJoomla_Settings);
	$lists['xhtml.removetags'] = JHtml::_('mjconfig.g_booleanparam', 'xhtml.removetags', $MobileJoomla_Settings);
	$lists['xhtml.removescripts'] = JHtml::_('mjconfig.booleanparam', 'xhtml.removescripts', $MobileJoomla_Settings);
	$lists['xhtml.img'] = JHtml::_('mjconfig.g_listparam', 'xhtml.img', $img, $MobileJoomla_Settings);
	$lists['xhtml.img_addstyles'] = JHtml::_('mjconfig.g_booleanparam', 'xhtml.img_addstyles', $MobileJoomla_Settings);
	$lists['xhtml.entitydecode'] = JHtml::_('mjconfig.booleanparam', 'xhtml.entitydecode', $MobileJoomla_Settings);
	$lists['xhtml.embedcss'] = JHtml::_('mjconfig.booleanparam', 'xhtml.embedcss', $MobileJoomla_Settings);
	$lists['xhtml.contenttype'] = JHtml::_('mjconfig.listparam', 'xhtml.contenttype', $contenttype, $MobileJoomla_Settings);
	$lists['xhtml.xmlhead'] = JHtml::_('mjconfig.booleanparam', 'xhtml.xmlhead', $MobileJoomla_Settings);
	$lists['xhtml.doctype'] = JHtml::_('mjconfig.listparam', 'xhtml.doctype', $xhtmldoctype, $MobileJoomla_Settings);
	$lists['xhtml.xmlns'] = JHtml::_('mjconfig.booleanparam', 'xhtml.xmlns', $MobileJoomla_Settings);

	//mobile_wap template setting
	$lists['wml.header1'] = JHtml::_('mjconfig.positionparam', 'wml.header1', $modulepositions, $MobileJoomla_Settings);
	$lists['wml.header2'] = JHtml::_('mjconfig.positionparam', 'wml.header2', $modulepositions, $MobileJoomla_Settings);
	$lists['wml.header3'] = JHtml::_('mjconfig.positionparam', 'wml.header3', $modulepositions, $MobileJoomla_Settings);
	$lists['wml.middle1'] = JHtml::_('mjconfig.positionparam', 'wml.middle1', $modulepositions, $MobileJoomla_Settings);
	$lists['wml.middle2'] = JHtml::_('mjconfig.positionparam', 'wml.middle2', $modulepositions, $MobileJoomla_Settings);
	$lists['wml.middle3'] = JHtml::_('mjconfig.positionparam', 'wml.middle3', $modulepositions, $MobileJoomla_Settings);
	$lists['wml.componenthome'] = JHtml::_('mjconfig.g_booleanparam', 'wml.componenthome', $MobileJoomla_Settings);
	$lists['wml.footer1'] = JHtml::_('mjconfig.positionparam', 'wml.footer1', $modulepositions, $MobileJoomla_Settings);
	$lists['wml.footer2'] = JHtml::_('mjconfig.positionparam', 'wml.footer2', $modulepositions, $MobileJoomla_Settings);
	$lists['wml.footer3'] = JHtml::_('mjconfig.positionparam', 'wml.footer3', $modulepositions, $MobileJoomla_Settings);
	$lists['wml.jfooter'] = JHtml::_('mjconfig.booleanparam', 'wml.jfooter', $MobileJoomla_Settings);
	$lists['wml.cards'] = JHtml::_('mjconfig.positionparam', 'wml.cards', $modulepositions, $MobileJoomla_Settings);
	$lists['wml.removetags'] = JHtml::_('mjconfig.g_booleanparam', 'wml.removetags', $MobileJoomla_Settings);
	$lists['wml.img'] = JHtml::_('mjconfig.g_listparam', 'wml.img', $img, $MobileJoomla_Settings);
	$lists['wml.entitydecode'] = JHtml::_('mjconfig.booleanparam', 'wml.entitydecode', $MobileJoomla_Settings);
	$lists['wml.doctype'] = JHtml::_('mjconfig.listparam', 'wml.doctype', $wmldoctype, $MobileJoomla_Settings);

	//mobile_imode template setting
	$lists['chtml.header1'] = JHtml::_('mjconfig.positionparam', 'chtml.header1', $modulepositions, $MobileJoomla_Settings);
	$lists['chtml.header2'] = JHtml::_('mjconfig.positionparam', 'chtml.header2', $modulepositions, $MobileJoomla_Settings);
	$lists['chtml.header3'] = JHtml::_('mjconfig.positionparam', 'chtml.header3', $modulepositions, $MobileJoomla_Settings);
	$lists['chtml.middle1'] = JHtml::_('mjconfig.positionparam', 'chtml.middle1', $modulepositions, $MobileJoomla_Settings);
	$lists['chtml.middle2'] = JHtml::_('mjconfig.positionparam', 'chtml.middle2', $modulepositions, $MobileJoomla_Settings);
	$lists['chtml.middle3'] = JHtml::_('mjconfig.positionparam', 'chtml.middle3', $modulepositions, $MobileJoomla_Settings);
	$lists['chtml.componenthome'] = JHtml::_('mjconfig.g_booleanparam', 'chtml.componenthome', $MobileJoomla_Settings);
	$lists['chtml.footer1'] = JHtml::_('mjconfig.positionparam', 'chtml.footer1', $modulepositions, $MobileJoomla_Settings);
	$lists['chtml.footer2'] = JHtml::_('mjconfig.positionparam', 'chtml.footer2', $modulepositions, $MobileJoomla_Settings);
	$lists['chtml.footer3'] = JHtml::_('mjconfig.positionparam', 'chtml.footer3', $modulepositions, $MobileJoomla_Settings);
	$lists['chtml.jfooter'] = JHtml::_('mjconfig.booleanparam', 'chtml.jfooter', $MobileJoomla_Settings);
	$lists['chtml.removetags'] = JHtml::_('mjconfig.g_booleanparam', 'chtml.removetags', $MobileJoomla_Settings);
	$lists['chtml.img'] = JHtml::_('mjconfig.g_listparam', 'chtml.img', $img, $MobileJoomla_Settings);
	$lists['chtml.entitydecode'] = JHtml::_('mjconfig.booleanparam', 'chtml.entitydecode', $MobileJoomla_Settings);
	$lists['chtml.doctype'] = JHtml::_('mjconfig.booleanparam', 'chtml.doctype', $MobileJoomla_Settings);

	//mobile_iphone template setting
	$lists['iphone.header1'] = JHtml::_('mjconfig.positionparam', 'iphone.header1', $modulepositions, $MobileJoomla_Settings);
	$lists['iphone.header2'] = JHtml::_('mjconfig.positionparam', 'iphone.header2', $modulepositions, $MobileJoomla_Settings);
	$lists['iphone.header3'] = JHtml::_('mjconfig.positionparam', 'iphone.header3', $modulepositions, $MobileJoomla_Settings);
	$lists['iphone.middle1'] = JHtml::_('mjconfig.positionparam', 'iphone.middle1', $modulepositions, $MobileJoomla_Settings);
	$lists['iphone.middle2'] = JHtml::_('mjconfig.positionparam', 'iphone.middle2', $modulepositions, $MobileJoomla_Settings);
	$lists['iphone.middle3'] = JHtml::_('mjconfig.positionparam', 'iphone.middle3', $modulepositions, $MobileJoomla_Settings);
	$lists['iphone.componenthome'] = JHtml::_('mjconfig.g_booleanparam', 'iphone.componenthome', $MobileJoomla_Settings);
	$lists['iphone.footer1'] = JHtml::_('mjconfig.positionparam', 'iphone.footer1', $modulepositions, $MobileJoomla_Settings);
	$lists['iphone.footer2'] = JHtml::_('mjconfig.positionparam', 'iphone.footer2', $modulepositions, $MobileJoomla_Settings);
	$lists['iphone.footer3'] = JHtml::_('mjconfig.positionparam', 'iphone.footer3', $modulepositions, $MobileJoomla_Settings);
	$lists['iphone.jfooter'] = JHtml::_('mjconfig.booleanparam', 'iphone.jfooter', $MobileJoomla_Settings);
	$lists['iphone.img'] = JHtml::_('mjconfig.g_listparam', 'iphone.img', $img, $MobileJoomla_Settings);
	$lists['iphone.img_addstyles'] = JHtml::_('mjconfig.g_booleanparam', 'iphone.img_addstyles', $MobileJoomla_Settings);
	$lists['iphone.removetags'] = JHtml::_('mjconfig.g_booleanparam', 'iphone.removetags', $MobileJoomla_Settings);

	function menuoptions()
	{
		$db = JFactory::getDBO();
		$isJoomla15 = (substr(JVERSION,0,3) == '1.5');

		if(version_compare(JVERSION, '3.0', '>=')) // 3.0+
			$query = 'SELECT id, menutype, title, link, type, parent_id FROM #__menu WHERE published=1 ORDER BY menutype, parent_id, lft';
		elseif(version_compare(JVERSION, '1.6', '>=')) //1.6-2.5
			$query = 'SELECT id, menutype, title, link, type, parent_id FROM #__menu WHERE published=1 ORDER BY menutype, parent_id, ordering';
		else //1.5
			$query = 'SELECT id, menutype, name AS title, link, type, parent AS parent_id FROM #__menu WHERE published=1 ORDER BY menutype, parent, ordering';
		$db->setQuery($query);
		$mitems = $db->loadObjectList();
		$children = array();
		foreach($mitems as $v)
		{
			$pt = $v->parent_id;
			$list = @$children[$pt] ? $children[$pt] : array();
			array_push($list, $v);
			$children[$pt] = $list;
		}
		$list = array();
		$id = $isJoomla15 ? 0 : 1;
		if(@$children[$id])
			TreeRecurse($id, '', $list, $children);
		$mitems = array();
		$mitems[] = JHtml::_('select.option', '', '&nbsp;');
		$lastMenuType = null;
		foreach($list as $list_a)
		{
			if($list_a->menutype != $lastMenuType)
			{
				if($lastMenuType)
					$mitems[] = JHtml::_('select.option', '</OPTGROUP>' );
				$mitems[] = JHtml::_('select.option', '<OPTGROUP>', $list_a->menutype);
				$lastMenuType = $list_a->menutype;
			}
			if($list_a->type == 'component')
				$link = $list_a->link.'&Itemid='.$list_a->id;
			else
				$link = '-';
			$mitems[] = JHtml::_('select.option', $link, $list_a->treename, 'value', 'text', $link=='-');
		}
		if($lastMenuType !== null)
			$mitems[] = JHtml::_('select.option', '</OPTGROUP>' );
		return $mitems;
	}
	function TreeRecurse($id, $indent, &$list, &$children, $level=0)
	{
		foreach($children[$id] as $v)
		{
			$id = $v->id;
			$list[$id] = $v;
			$list[$id]->treename = $indent.$v->title;
			if(@$children[$id] && $level<=99)
				TreeRecurse($id, $indent.'&nbsp;&nbsp;', $list, $children, $level+1);
		}
	}
	$lists['menuoptions'] = menuoptions();

	function getPluginPath($folder, $name)
	{
		return JPATH_PLUGINS
				. '/' . $folder
				. (version_compare(JVERSION, '1.6', '>=') ? '/' . $name : '')
				. '/' . $name . '.php';
	}

	$parsed = parse_url($MobileJoomla_Settings['desktop_url']);
	$domain = $parsed['host'];

	$stdTemplates = array('mobile_smartphone', 'mobile_iphone', 'mobile_imode', 'mobile_wap');
	$isStdTpl = true;
	foreach($MobileJoomla_Settings as $key=>$value)
		if(preg_match('#\.template$#', $key) && !in_array($value, $stdTemplates))
		{
			$isStdTpl = false;
			break;
		}

	$recommend = array();
	$recommend['mj'] = '1.2.13.pro';
	$recommend['j']  = JVERSION;
	$recommend['domain'] = $domain;
	$recommend['stdtpl'] = (int)$isStdTpl;
	$recommend['adremover'] = (int)file_exists(getPluginPath('mobile', 'adremover'));
	$recommend['jcomments'] = (int)(file_exists(JPATH_ROOT.'/components/com_jcomments') && !file_exists(getPluginPath('mobile', 'mobilejcomments')));
	$recommend['kunena']    = (int)(file_exists(JPATH_ROOT.'/components/com_kunena')    && !file_exists(getPluginPath('mobile', 'mobilekunena')));
	$lists['recommend_params'] = $recommend;

	HTML_mobilejoomla::showconfig($lists, $MobileJoomla_Settings);
}

function saveconfig()
{
	$configfname = JPATH_ADMINISTRATOR.'/components/com_mobilejoomla/config.php';
	/** @var $MobileJoomla_Settings array */
	include($configfname);

	$settings = array ('caching', 'httpcaching', 'pcpage', 'templatewidth', 'jpegquality',
					   'desktop_url', 'mobile_sitename',
					   'xhtml.template', 'xhtml.homepage', 'xhtml.gzip', 'xhtml.domain', 'xhtml.redirect', 'xhtml.buffer_width',
					   'wml.template', 'wml.homepage', 'wml.gzip', 'wml.domain', 'wml.redirect', 'wml.buffer_width',
					   'chtml.template', 'chtml.homepage', 'chtml.gzip', 'chtml.domain', 'chtml.redirect', 'chtml.buffer_width',
					   'iphone.template', 'iphone.homepage', 'iphone.gzip', 'iphone.domain', 'iphone.redirect', 'iphone.buffer_width',
					   'global.gzip', 'global.removetags', 'global.img', 'global.img_addstyles', 'global.homepage', 'global.componenthome',
					   'xhtml.header1', 'xhtml.header2', 'xhtml.header3',
					   'xhtml.middle1', 'xhtml.middle2', 'xhtml.middle3',
					   'xhtml.componenthome',
					   'xhtml.footer1', 'xhtml.footer2', 'xhtml.footer3',
					   'xhtml.jfooter', 'xhtml.simplehead', 'xhtml.allowextedit',
					   'xhtml.removetags', 'xhtml.removescripts', 'xhtml.img', 'xhtml.img_addstyles',
					   'xhtml.entitydecode', 'xhtml.embedcss', 'xhtml.contenttype', 'xhtml.xmlhead',
					   'xhtml.doctype', 'xhtml.xmlns',
					   'wml.header1', 'wml.header2', 'wml.header3',
					   'wml.middle1', 'wml.middle2', 'wml.middle3',
					   'wml.componenthome',
					   'wml.footer1', 'wml.footer2', 'wml.footer3',
					   'wml.cards', 'wml.jfooter',
					   'wml.removetags', 'wml.img', 'wml.entitydecode', 'wml.doctype',
					   'chtml.header1', 'chtml.header2', 'chtml.header3',
					   'chtml.middle1', 'chtml.middle2', 'chtml.middle3',
					   'chtml.componenthome',
					   'chtml.footer1', 'chtml.footer2', 'chtml.footer3',
					   'chtml.jfooter', 'chtml.removetags', 'chtml.img',
					   'chtml.entitydecode', 'chtml.doctype',
					   'iphone.header1', 'iphone.header2', 'iphone.header3',
					   'iphone.middle1', 'iphone.middle2', 'iphone.middle3',
					   'iphone.componenthome',
					   'iphone.footer1', 'iphone.footer2', 'iphone.footer3',
					   'iphone.jfooter', 'iphone.img', 'iphone.img_addstyles',
					   'iphone.removetags');

	$dispatcher = JDispatcher::getInstance();
	$dispatcher->trigger('onMJBeforeSave', array(&$settings, &$MobileJoomla_Settings));

	if($_POST['mjconfig_desktop_url'] == '')
		$_POST['mjconfig_desktop_url'] = JUri::root();
	elseif(strpos($_POST['mjconfig_desktop_url'], '/') === false)
		$_POST['mjconfig_desktop_url'] = 'http://'.$_POST['mjconfig_desktop_url'].'/';
	else
	{
		$desktop_url = @parse_url($_POST['mjconfig_desktop_url']);
		if(!isset($desktop_url['scheme']))
			$desktop_url['scheme'] = 'http';
		if(!isset($desktop_url['host']))
			$desktop_url['host'] = $_SERVER['HTTP_HOST'];
		if(isset($desktop_url['port']))
			$desktop_url['host'] .= ':' . $desktop_url['port'];
		if(!isset($desktop_url['path']))
			$desktop_url['path'] = '/';
		if(substr($desktop_url['path'], -1, 1) != '/')
			$desktop_url['path'] .= '/';
		$_POST['mjconfig_desktop_url'] = $desktop_url['scheme'] . '://' . $desktop_url['host'] . $desktop_url['path'];
	}

	$params = array ();
	foreach($settings as $param)
	{
		$post_name = 'mjconfig_'.str_replace('.', '-', $param);
		if(isset($_POST[$post_name]))
		{
			if(!get_magic_quotes_gpc())
				$_POST[$post_name] = addcslashes($_POST[$post_name], "'");
			$MobileJoomla_Settings[$param] = $_POST[$post_name];
		}
		if(is_numeric($MobileJoomla_Settings[$param]))
			$params[] = "'$param'=>".$MobileJoomla_Settings[$param];
		else
			$params[] = "'$param'=>'".$MobileJoomla_Settings[$param]."'";
	}
	$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"
			. "?>";

	$app = JFactory::getApplication();
	if(JFile::write($configfname, $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'));
	}
}

function _initStatus()
{
	JError::setErrorHandling(E_ERROR, 'Message');
	@set_time_limit(1200);
	@ini_set('max_execution_time', 1200);
}
function _sendStatus()
{
	$msg = array();
	/** @var JException $error */
	foreach(JError::getErrors() as $error)
		if($error->get('level'))
			$msg[] = $error->get('message');
	if(count($msg))
		$msg = '<p>'.implode('</p><p>', $msg).'</p>';
	else
		$msg = 'ok';
	echo $msg;
	jexit();
}
function update()
{
	jimport('joomla.installer.helper');
	jimport('joomla.installer.installer');
	$app = JFactory::getApplication();
	$option = JRequest::getString('option');

	$state = JRequest::getWord('state');
	switch($state)
	{
	case 'download':
		_initStatus();
		$url = 'http://www.mobilejoomla.com/latest.php';
		$filename = JInstallerHelper::downloadPackage($url);
		if($filename)
			$app->setUserState( "$option.updatefilename", $filename );
		_sendStatus();
		break;
	case 'unpack':
		_initStatus();
		$filename = $app->getUserState( "$option.updatefilename", false );
		$config = JFactory::getConfig();
		if(substr(JVERSION,0,3)=='1.5')
			$path = $config->getValue('config.tmp_path');
		else
			$path = $config->get('tmp_path');
		$path .= '/'.$filename;
		if($path)
		{
			$result = JInstallerHelper::unpack($path);
			$app->setUserState( "$option.updatefilename", false );
			if($result!==false)
			{
				$app->setUserState( "$option.updatedir", $result['dir'] );
				JFile::delete($path);
			}
		}
		else
			JError::raiseWarning(1, JText::_('COM_MJ__UPDATE_UNKNOWN_PATH'));
		_sendStatus();
		break;
	case 'install':
		_initStatus();
		$dir = $app->getUserState( "$option.updatedir", false );
		if($dir)
		{
			$installer = new JInstaller();
			$installer->install($dir);
			$app->setUserState( "$option.updatedir", false );
			JFolder::delete($dir);
		}
		else
			JError::raiseWarning(1, JText::_('COM_MJ__UPDATE_UNKNOWN_PATH'));
		_sendStatus();
		break;
	default: // TODO: move into view
?>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js"></script>
<script type="text/javascript">
//<![CDATA[
jQuery.noConflict();
function mjOnError()
{
	jQuery("#mjlink").css('display','block');
}
function mjTestStatus(textStatus)
{
	switch(textStatus){
		case "success": break;
		case "notmodified": break;
		case "error": jQuery("#mjstatus").html("<?php echo JText::_('COM_MJ__UPDATE_AJAX_ERROR'); ?>"); mjOnError(); break;
		case "timeout": jQuery("#mjstatus").html("<?php echo JText::_('COM_MJ__UPDATE_AJAX_TIMEOUT'); ?>"); mjOnError(); break;
		case "abort": jQuery("#mjstatus").html("<?php echo JText::_('COM_MJ__UPDATE_AJAX_ABORT'); ?>"); mjOnError(); break;
		case "parsererror": jQuery("#mjstatus").html("<?php echo JText::_('COM_MJ__UPDATE_AJAX_PARSEERROR'); ?>"); mjOnError(); break;
	}
}
function mjAjaxDownload()
{
	jQuery("#mjdownload").addClass("highlight").addClass("ajaxload");
	jQuery.ajax({
		type: "GET",
		url: "index.php?option=com_mobilejoomla&task=update&state=download&tmpl=none",
		success: function(data){
			if(data!="ok") {
				jQuery("#mjdownload").addClass("error");
				jQuery("#mjstatus").html(data);
				mjOnError();
			} else {
				jQuery("#mjdownload").addClass("pass");
				mjAjaxUnpack();
			}
		},
		error: function(){
			jQuery("#mjdownload").addClass("error");
			mjOnError();
		},
		complete: function(jqXHR, textStatus){
			jQuery("#mjdownload").removeClass("ajaxload");
			mjTestStatus(textStatus);
		}
	});
}
function mjAjaxUnpack()
{
	jQuery("#mjunpack").addClass("highlight").addClass("ajaxload");
	jQuery.ajax({
		type: "GET",
		url: "index.php?option=com_mobilejoomla&task=update&state=unpack&tmpl=none",
		success: function(data){
			if(data!="ok") {
				jQuery("#mjunpack").addClass("error");
				jQuery("#mjstatus").html(data);
				mjOnError();
			} else {
				jQuery("#mjunpack").addClass("pass");
				mjAjaxInstall();
			}
		},
		error: function(){
			jQuery("#mjunpack").addClass("error");
			mjOnError();
		},
		complete: function(jqXHR, textStatus){
			jQuery("#mjunpack").removeClass("ajaxload");
			mjTestStatus(textStatus);
		}
	});
}
function mjAjaxInstall()
{
	jQuery("#mjinstall").addClass("highlight").addClass("ajaxload");
	jQuery.ajax({
		type: "GET",
		url: "index.php?option=com_mobilejoomla&task=update&state=install&tmpl=none",
		success: function(data){
			if(data!="ok") {
				jQuery("#mjinstall").addClass("error");
				jQuery("#mjstatus").html(data);
				mjOnError();
			} else {
				jQuery("#mjinstall").addClass("pass");
				window.parent.location.reload();
			}
		},
		error: function(){
			jQuery("#mjinstall").addClass("error");
			mjOnError();
		},
		complete: function(jqXHR, textStatus){
			jQuery("#mjinstall").removeClass("ajaxload");
			mjTestStatus(textStatus);
		}
	});
}
jQuery(document).ready(mjAjaxDownload);
//]]>
</script>
<style type="text/css">
.mjheader {
	font-size: 20px;
	font-weight: bold;
	line-height: 48px;
	margin-left: 5px;
	padding-left: 5px;
}
#mjstages {
	list-style-type: none;
	margin: 0;
	padding: 0;
}
#mjstages li {
	height: 22px;
	padding: 10px 0 0 32px;
	margin: 0;
}
#mjlink {
	display: none;
	background: url("components/com_mobilejoomla/images/warning.png") no-repeat scroll 10px 50% #FDFBB9;
	font-weight: bold;
	-moz-border-radius: 8px;
	-webkit-border-radius: 8px;
	border-radius: 8px;
	border: 3px solid #f00;
	line-height: 135%;
	margin-top: 15px;
	padding: 10px 10px 10px 48px;
	text-align: left;
}
#mjstatus {
	font-size: 80%;
	padding: 16px 8px 0;
}
.highlight {
	font-weight: bold;
}
.ajaxload {
	background: url("components/com_mobilejoomla/images/ajax-loader.gif") no-repeat scroll 0 50% #FFF;
	line-height: 100%;
	text-align: left;
}
.pass {
	background: url("components/com_mobilejoomla/images/tick.png") no-repeat scroll 8px 50% #FFF;
	line-height: 100%;
	text-align: left;
}
.error {
	background: url("components/com_mobilejoomla/images/error.png") no-repeat scroll 8px 50% #FDFBB9;
	color: #f00;
	line-height: 100%;
	text-align: left;
}
</style>
<div class="mjheader"><?php echo JText::_('COM_MJ__UPDATE_HEADER'); ?></div>
<ul id="mjstages">
	<li id="mjdownload"><?php echo JText::_('COM_MJ__UPDATE_DOWNLOAD'); ?></li>
	<li id="mjunpack"><?php echo JText::_('COM_MJ__UPDATE_UNPACK'); ?></li>
	<li id="mjinstall"><?php echo JText::_('COM_MJ__UPDATE_INSTALL'); ?></li>
</ul>
<div id="mjlink"><?php echo JText::_('COM_MJ__UPDATE_DOWNLOAD_LINK'); ?></div>
<div id="mjstatus"></div>
<?php
	}
}