<?php
/**
 * SimpleScripts Joomla Updater
 *
 * Joomla has a predefined upgrade process. This will run that process without requiring
 * the user to click on anything in the admin section of the database.
 *
 * @author Chuck Burgess
 **/

define( '_JEXEC', 1 );
define('JPATH_BASE', dirname(__FILE__) );//this is when we are in the root
define( 'DS', DIRECTORY_SEPARATOR );

require_once ( JPATH_BASE .DS.'includes'.DS.'defines.php' );
require_once ( JPATH_BASE .DS.'includes'.DS.'framework.php' );

$mainframe = JFactory::getApplication('site');
$mainframe->initialise();
$config 	= JFactory::getConfig();
$newConfig = (array) $config;

foreach ($newConfig as $key => $value) {
	$value = (array) $value;
	$database['name'] = $value['db'];
	$database['type'] = $value['dbtype'];
    $database['host'] = $value['host'];
    $database['user'] = $value['user'];
    $database['password'] = $value['password'];
    $database['prefix'] = $value['dbprefix'];
}

$dbLink = mysql_connect($database['host'], $database['user'], $database['password']);
if (!$dbLink) {
	die('Cannot connect to db');
	exit;
}
mysql_select_db($database['name'], $dbLink);

$schemaTable = $database['prefix'] . 'schemas';

$query = "SELECT `version_id` FROM " . $schemaTable . " WHERE `extension_id` = '700'";
$result = mysql_query($query);
$array = mysql_fetch_assoc($result);
$dbVersion = $array['version_id'];

$version = null;
if (preg_match('/^2.5.0/', $dbVersion)) {
	$version = '2.5.0';
} elseif (preg_match('/^2.5.1/', $dbVersion)) {
	$version = '2.5.1';
} elseif (preg_match('/^2.5.2/', $dbVersion)) {
	$version = '2.5.2';
} elseif (preg_match('/^2.5.3/', $dbVersion)) {
	$version = '2.5.3';
} elseif (preg_match('/^2.5.4/', $dbVersion)) {
	$version = '2.5.4';
} elseif (preg_match('/^2.5.5/', $dbVersion)) {
	$version = '2.5.5';
} elseif (preg_match('/^2.5.6/', $dbVersion)) {
	$version = '2.5.6';
} elseif (preg_match('/^2.5.7/', $dbVersion)) {
	$version = '2.5.7';
} elseif (preg_match('/^2.5.8/', $dbVersion)) {
	$version = '2.5.8';
}

$sql = null;
switch($version) {
	case '2.5.0':
		$sql[] = "SELECT @old_params:= CONCAT(SUBSTRING_INDEX(SUBSTRING(params, LOCATE('\"filters\":', params)), '}}', 1), '}}') as filters FROM `" . $database['prefix'] . "extensions` WHERE name = 'com_content'";
		$sql[] = "UPDATE `" . $database['prefix'] . "extensions` SET params=CONCAT('{',SUBSTRING(params, 2, CHAR_LENGTH(params)-2),IF(params='','',','),@old_params,'}') WHERE name = 'com_config'";

	case '2.5.1':
		// database changes for 2.5.1
		$sql[] = "INSERT INTO `" . $database['prefix'] . "extensions` (`extension_id`, `name`, `type`, `element`, `folder`, `client_id`, `enabled`, `access`, `protected`, `manifest_cache`, `params`, `custom_data`, `system_data`, `checked_out`, `checked_out_time`, `ordering`, `state`) VALUES (314, 'mod_version', 'module', 'mod_version', '', 1, 1, 1, 0, '{\"legacy\":false,\"name\":\"mod_version\",\"type\":\"module\",\"creationDate\":\"January 2012\",\"author\":\"Joomla! Project\",\"copyright\":\"Copyright (C) 2005 - 2012 Open Source Matters. All rights reserved.\",\"authorEmail\":\"admin@joomla.org\",\"authorUrl\":\"www.joomla.org\",\"version\":\"2.5.0\",\"description\":\"MOD_VERSION_XML_DESCRIPTION\",\"group\":\"\"}', '{\"format\":\"short\",\"product\":\"1\",\"cache\":\"0\"}', '', '', 0, '0000-00-00 00:00:00', 0, 0)";
		$sql[] = "INSERT INTO `" . $database['prefix'] . "modules` (`title`, `note`, `content`, `ordering`, `position`, `checked_out`, `checked_out_time`, `publish_up`, `publish_down`, `published`, `module`, `access`, `showtitle`, `params`, `client_id`, `language`) VALUES ('Joomla Version', '', '', 1, 'footer', 0, '0000-00-00 00:00:00', '0000-00-00 00:00:00', '0000-00-00 00:00:00', 1, 'mod_version', 3, 1, '{\"format\":\"short\",\"product\":\"1\",\"layout\":\"_:default\",\"moduleclass_sfx\":\"\",\"cache\":\"0\"}', 1, '*')";
		$sql[] = "INSERT INTO `" . $database['prefix'] . "modules_menu` (`moduleid`, `menuid`) VALUES (LAST_INSERT_ID(), 0)";

	case '2.5.2':
	case '2.5.3':
	case '2.5.4':
		// database changes for 2.5.4
		$sql[] = "INSERT INTO `" . $database['prefix'] . "extensions` (`extension_id`, `name`, `type`, `element`, `folder`, `client_id`, `enabled`, `access`, `protected`, `manifest_cache`, `params`, `custom_data`, `system_data`, `checked_out`, `checked_out_time`, `ordering`, `state`) VALUES (28, 'com_joomlaupdate', 'component', 'com_joomlaupdate', '', 1, 1, 0, 1, '{\"legacy\":false,\"name\":\"com_joomlaupdate\",\"type\":\"component\",\"creationDate\":\"February 2012\",\"author\":\"Joomla! Project\",\"copyright\":\"(C) 2005 - 2012 Open Source Matters. All rights reserved.\",\"authorEmail\":\"admin@joomla.org\",\"authorUrl\":\"www.joomla.org\",\"version\":\"2.5.2\",\"description\":\"COM_JOOMLAUPDATE_XML_DESCRIPTION\",\"group\":\"\"}', '{}', '', '', 0, '0000-00-00 00:00:00', 0, 0)";
		$sql[] = "INSERT INTO `" . $database['prefix'] . "menu` (`id`, `menutype`, `title`, `alias`, `note`, `path`, `link`, `type`, `published`, `parent_id`, `level`, `component_id`, `ordering`, `checked_out`, `checked_out_time`, `browserNav`, `access`, `img`, `template_style_id`, `params`, `lft`, `rgt`, `home`, `language`, `client_id`) VALUES (22, 'menu', 'com_joomlaupdate', 'Joomla! Update', '', 'Joomla! Update', 'index.php?option=com_joomlaupdate', 'component', 0, 1, 1, 28, 0, 0, '0000-00-00 00:00:00', 0, 0, 'class:joomlaupdate', 0, '', 41, 42, 0, '*', 1)";
		// database changes for 2.5.4
		$sql[] = "ALTER TABLE `" . $database['prefix'] . "languages` ADD COLUMN `access` integer unsigned NOT NULL default 0 AFTER `published`";
		$sql[] = "ALTER TABLE `" . $database['prefix'] . "languages` ADD KEY `idx_access` (`access`)";
		$sql[] = "UPDATE `" . $database['prefix'] . "categories` SET `extension` = 'com_users.notes' WHERE `extension` = 'com_users'";
		$sql[] = "UPDATE `" . $database['prefix'] . "extensions` SET `enabled` = '1' WHERE `protected` = '1' AND `type` <> 'plugin'";

	case '2.5.5':
		// database changes for 2.5.5
		$sql[] = "ALTER TABLE `" . $database['prefix'] . "redirect_links` ADD COLUMN `hits` INT(10) UNSIGNED NOT NULL DEFAULT '0' AFTER `comment`";
		$sql[] = "ALTER TABLE `" . $database['prefix'] . "users` ADD COLUMN `lastResetTime` datetime NOT NULL DEFAULT '0000-00-00 00:00:00' COMMENT 'Date of last password reset'";
		$sql[] = "ALTER TABLE `" . $database['prefix'] . "users` ADD COLUMN `resetCount` int(11) NOT NULL DEFAULT '0' COMMENT 'Count of password resets since lastResetTime'";

	case '2.5.6':

	case '2.5.7':
		// database changes for 2.5.7
		$sql[] = "INSERT INTO `" . $database['prefix'] . "update_sites` (`name`, `type`, `location`, `enabled`, `last_check_timestamp`) VALUES('Accredited Joomla! Translations','collection','http://update.joomla.org/language/translationlist.xml',1,0)";
		$sql[] = "INSERT INTO `" . $database['prefix'] . "update_sites_extensions` (`update_site_id`, `extension_id`) VALUES(LAST_INSERT_ID(),600)";
		$sql[] = "UPDATE  `" . $database['prefix'] . "assets` SET name=REPLACE( name, 'com_user.notes.category','com_users.category'  )";
		$sql[] = "UPDATE  `" . $database['prefix'] . "categories` SET extension=REPLACE( extension, 'com_user.notes.category','com_users.category'  )";
		//$sql[] = "UPDATE  `" . $database['prefix'] . "schemas` SET `version_id` = '2.5.8' WHERE `extension_id` = '700'";

	case '2.5.8':
		$sql[] = "UPDATE  `" . $database['prefix'] . "schemas` SET `version_id` = '2.5.9' WHERE `extension_id` = '700'";

	case '2.5.9':
		break;

	default:
		break;
}

if ($sql) {
	foreach ($sql as $query) {
		$result = mysql_query($query);
	}
}

mysql_close($dbLink);
@unlink(__FILE__);
exit;

