Index: branches/5.1.x/core/units/helpers/skin_helper.php
===================================================================
--- branches/5.1.x/core/units/helpers/skin_helper.php	(revision 13689)
+++ branches/5.1.x/core/units/helpers/skin_helper.php	(revision 13690)
@@ -1,232 +1,232 @@
 <?php
 /**
 * @version	$Id$
 * @package	In-Portal
 * @copyright	Copyright (C) 1997 - 2009 Intechnic. All rights reserved.
 * @license      GNU/GPL
 * In-Portal is Open Source software.
 * This means that this software may have been modified pursuant
 * 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.
 * See http://www.in-portal.org/license for copyright notices and details.
 */
 
 	defined('FULL_PATH') or die('restricted access!');
 
 	class SkinHelper extends kHelper {
 
 		/**
 		 * Allows to read some skin fields and build link to admin skin file.
 		 * Will automatically compile skin file, when missing.
 		 *
 		 * @param Array $params
 		 * @return string
 		 */
 		function AdminSkinTag($params)
 		{
 			if (array_key_exists('type', $params)) {
 				// returns given field of skin
 				return $this->_getStyleField( $params['type'] );
 			}
 
 			$style_info = $this->_getStyleInfo();
 
 			if (file_exists( $this->getSkinPath() )) {
 				// returns last compiled skin
 				$ret = $this->getSkinPath(true);
 			}
 			else {
 				// search for previously compiled skin
 				$last_compiled = $this->_getLastCompiled( mb_strtolower($style_info['Name']) );
 				if ($last_compiled) {
 					// found
 					$ret = $this->getSkinPath(true, $last_compiled);
 				}
 				else {
 					// not found (try to compile on the fly)
 					$skin =& $this->Application->recallObject('skin.-item', null, Array ('skip_autoload' => true));
 					/* @var $skin kDBItem */
 
 					$skin->Load(1, 'IsPrimary');
 					$last_compiled = $this->compile($skin);
 					$ret = $last_compiled ? $this->getSkinPath(true, $last_compiled) : '';
 				}
 			}
 
 			if (array_key_exists('file_only', $params) && $params['file_only']) {
 				return $ret;
 			}
 
 			return '<link rel="stylesheet" rev="stylesheet" href="' . $ret . '" type="text/css" media="screen"/>';
 		}
 
 		/**
 		 * Compiles given skin object
 		 *
 		 * @param kDBItem $object
 		 */
 		function compile(&$object)
 		{
 			$ret = $object->GetDBField('CSS');
 			$options = $object->GetDBField('Options');
 			$options = unserialize($options);
-			$options['base_url'] = Array ('Value' => rtrim($this->Application->BaseURL(), '/'));
+			$options['base_url'] = Array ('Value' => rtrim(BASE_PATH, '/'));
 
 			foreach ($options as $key => $row) {
 				$ret = str_replace('@@' . $key . '@@', $row['Value'], $ret);
 			}
 
 			$compile_ts = adodb_mktime();
 			$css_file = $this->_getStylesheetPath() . DIRECTORY_SEPARATOR . 'admin-' . mb_strtolower($object->GetDBField('Name')) . '-' . $compile_ts . '.css';
 
 			$fp = fopen($css_file, 'w');
 			if (!$fp) {
 				return false;
 			}
 
 			$prev_css = $this->_getStylesheetPath() . '/admin-' . mb_strtolower($object->GetDBField('Name')) . '-' . $object->GetDBField('LastCompiled') . '.css';
 			if (file_exists($prev_css)) {
 				unlink($prev_css);
 			}
 
 			fwrite($fp, $ret);
 			fclose($fp);
 
 			$sql = 'UPDATE ' . $object->TableName . '
 					SET LastCompiled = ' . $compile_ts . '
 					WHERE ' . $object->IDField . ' = ' . $object->GetID();
 			$this->Conn->Query($sql);
 
 			$this->Application->incrementCacheSerial('skin');
 			$this->Application->incrementCacheSerial('skin', $object->GetID());
 
 			return $compile_ts;
 		}
 
 		/**
 		 * Returns fields of primary admin skin
 		 *
 		 * @return Array
 		 */
 		function _getStyleInfo()
 		{
 			$cache_key = 'primary_skin_info[%SkinSerial%]';
 			$ret = $this->Application->getCache($cache_key);
 
 			if ($ret === false) {
 				$this->Conn->nextQueryCachable = true;
 				$sql = 'SELECT *
 						FROM ' . TABLE_PREFIX . 'Skins
 						WHERE IsPrimary = 1';
 				$ret = $this->Conn->GetRow($sql);
 
 				$this->Application->setCache($cache_key, $ret);
 			}
 
 			return $ret;
 		}
 
 		/**
 		 * Returns requested field value of primary admin skin
 		 *
 		 * @param string $field
 		 * @return string
 		 */
 		function _getStyleField($field)
 		{
 			if ($field == 'logo') {
 				// old style method of calling
 				$field = 'Logo';
 			}
 
 			$style_info = $this->_getStyleInfo();
 
 			if (!$style_info[$field]) {
 				return '';
 			}
 
 			$image_fields = Array ('Logo', 'LogoBottom', 'LogoLogin');
 			if (in_array($field, $image_fields)) {
 				return $this->_getStylesheetPath(true) . '/' . $style_info[$field];
 			}
 
 			return $style_info[$field];
 		}
 
 		/**
 		 * Returns path, where compiled skin and it's image files are stored
 		 *
 		 * @param bool $url
 		 * @return string
 		 */
 		function _getStylesheetPath($url = false)
 		{
 			if ($url) {
 				return $this->Application->BaseURL( str_replace(DIRECTORY_SEPARATOR, '/', WRITEBALE_BASE) ) . 'user_files';
 			}
 
 			return WRITEABLE . DIRECTORY_SEPARATOR . 'user_files';
 		}
 
 		/**
 		 * Returns full path to primary admin skin using given or last compiled date
 		 *
 		 * @param string $url
 		 * @param int $compile_date
 		 * @return string
 		 */
 		function getSkinPath($url = false, $compile_date = null)
 		{
 			$style_info = $this->_getStyleInfo();
 
 			if (!isset($compile_date)) {
 				$compile_date = $style_info['LastCompiled'];
 			}
 
 			$style_name = 'admin-' . mb_strtolower($style_info['Name']) . '-' . $compile_date . '.css';
 
 			return $this->_getStylesheetPath($url) . ($url ? '/' : DIRECTORY_SEPARATOR) . $style_name;
 		}
 
 		/**
 		 * Returns maximal compilation date for given skin name
 		 *
 		 * @param string $style_name
 		 * @return int
 		 */
 		function _getLastCompiled($style_name)
 		{
 			$last_compiled = 0;
 			$dh = dir($this->_getStylesheetPath() . DIRECTORY_SEPARATOR);
 
 			while (false !== ($file = $dh->read())) {
 				if ( preg_match('/admin-(.*)-([\d]+).css/', $file, $rets) ) {
 					if ($rets[1] == $style_name && $rets[2] > $last_compiled) {
 						$last_compiled = $rets[2];
 					}
 				}
 			}
 
 			$dh->close();
 
 			return $last_compiled;
 		}
 
 		/**
 		 * Deletes all compiled versions of all skins
 		 *
 		 */
 		function deleteCompiled()
 		{
 			$dh = dir($this->_getStylesheetPath() . DIRECTORY_SEPARATOR);
 
 			while (false !== ($file = $dh->read())) {
 				if ( preg_match('/admin-(.*)-([\d]+).css/', $file, $rets) ) {
 					unlink($dh->path . $file);
 				}
 			}
 
 			$dh->close();
 		}
 	}
\ No newline at end of file
Index: branches/5.1.x/core/units/helpers/minifiers/minify_helper.php
===================================================================
--- branches/5.1.x/core/units/helpers/minifiers/minify_helper.php	(revision 13689)
+++ branches/5.1.x/core/units/helpers/minifiers/minify_helper.php	(revision 13690)
@@ -1,245 +1,246 @@
 <?php
 /**
 * @version	$Id$
 * @package	In-Portal
 * @copyright	Copyright (C) 1997 - 2010 Intechnic. All rights reserved.
 * @license      GNU/GPL
 * In-Portal is Open Source software.
 * This means that this software may have been modified pursuant
 * 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.
 * See http://www.in-portal.org/license for copyright notices and details.
 */
 
 	defined('FULL_PATH') or die('restricted access!');
 
 	class MinifyHelper extends kHelper {
 
 		/**
 		 * Debug mode mark
 		 *
 		 * @var bool
 		 */
 		var $debugMode = false;
 
 		/**
 		 * Path to compress information file
 		 *
 		 * @var string
 		 */
 		var $infoFile = '';
 
 		/**
 		 * Compress information
 		 *
 		 * @var Array
 		 */
 		var $compressInfo = Array ();
 
 		function MinifyHelper()
 		{
 			parent::kHelper();
 
 			$this->debugMode = $this->Application->isDebugMode(false);
 			$this->infoFile = WRITEABLE . '/cache/compress_info.txt';
 
 			if ( file_exists($this->infoFile) ) {
 				$this->compressInfo = unserialize( file_get_contents($this->infoFile) );
 			}
 		}
 
 		/**
 		 * When used as non-block tag, then compress given files and return url to result
 		 *
 		 * @param Array $files
 		 * @return string
 		 */
 		function CompressScriptTag($params)
 		{
 			// put to queue
 			if (array_key_exists('to', $params)) {
 				$files = $this->Application->GetVar($params['to'], '');
 				$this->Application->SetVar($params['to'], $files . '|' . $params['files']);
 
 				return '';
 			}
 
 			if (array_key_exists('from', $params)) {
 				// get from queue
 				$files = $this->Application->GetVar($params['from']);
 			}
 			else {
 				// get from tag
 				$files = $params['files'];
 			}
 
 			$files = $this->_getTemplatePaths( array_map('trim', explode('|', $files)) );
 			$extension = pathinfo($files[0], PATHINFO_EXTENSION);
 
 			$hash = ($this->debugMode ? 'd' : 'c') . '_' . $this->_getHash($files);
 			$file_mask = 'cache/' . $hash . '_%s.' . $extension;
 
 			$was_compressed = array_key_exists($hash, $this->compressInfo);
 
 			if ($was_compressed) {
 				$current_file = WRITEABLE . '/' . sprintf($file_mask, $this->compressInfo[$hash]);
 
 				if (!file_exists($current_file)) {
 					// when info exists, but file doesn't -> re-compress
 					$was_compressed = false;
 				}
 
 				if ($this->debugMode) {
 					// check if any of listed files was changed since compressed file was created (only, when in debug mode)
 					foreach ($files as $file) {
 						if (filemtime($file) > $this->compressInfo[$hash]) {
 							$was_compressed = false;
 							break;
 						}
 					}
 				}
 			}
 
 			if (!$was_compressed) {
 				$string = '';
 				$path_length = strlen(FULL_PATH) + 1;
 
 				foreach ($files as $file) {
 					// add filename before for easier debugging
 					if ($this->debugMode) {
 						$string .= '/* === File: ' . substr($file, $path_length) . ' === */' . "\n";
 						$string .= '/* ' . str_repeat('=', strlen(substr($file, $path_length)) + 14) . ' */' . "\n\n";
 					}
 
 					// add file content
 					$string .= file_get_contents($file) . "\n\n";
 				}
 
 				// remove previous version of compressed file
 				if (isset($current_file)) {
 					if (file_exists($current_file)) {
 						unlink($current_file);
 					}
 				}
 
 				// replace templates base
 				$templates_base = $this->Application->ProcessParsedTag('m', 'TemplatesBase', Array ());
+				$templates_base = preg_replace('/^' . preg_quote($this->Application->BaseURL(), '/') . '/', BASE_PATH . '/', $templates_base);
 				$string = str_replace('@templates_base@', rtrim($templates_base, '/'), $string);
 
 				// compress collected data
 				$this->compressInfo[$hash] = adodb_mktime();
 
 				if (!$this->debugMode) {
 					// don't compress merged js/css file in debug mode to allow js/css debugging
 					$this->compressString($string, $extension);
 				}
 
 				// save compressed file
 				$fp = fopen(WRITEABLE . '/' . sprintf($file_mask, $this->compressInfo[$hash]), 'w');
 				fwrite($fp, $string);
 				fclose($fp);
 
 				$this->_saveInfo();
 			}
 
 			// got compressed file -> use it
 			return $this->Application->BaseURL(WRITEBALE_BASE) . sprintf($file_mask, $this->compressInfo[$hash]);
 		}
 
 		/**
 		 * Returns hash string based on given files
 		 *
 		 * @param Array $files
 		 * @return int
 		 */
 		function _getHash($files)
 		{
 			$hash = $files;
 
 			if ($this->Application->isAdmin) {
 				array_unshift($hash, 'A:1');
 			}
 			else {
 				array_unshift($hash, 'A:0;T:' . $this->Application->GetVar('m_theme'));
 			}
 
 			return crc32( implode('|', $hash) );
 		}
 
 		/**
 		 * Saves file with compress information
 		 *
 		 */
 		function _saveInfo()
 		{
 			$fp = fopen($this->infoFile, 'w');
 			fwrite($fp, serialize($this->compressInfo));
 			fclose($fp);
 		}
 
 		/**
 		 * Deletes compression info file
 		 *
 		 * @todo also delete all listed there files
 		 */
 		function delete()
 		{
 			if (file_exists($this->infoFile)) {
 				unlink($this->infoFile);
 			}
 		}
 
 		/**
 		 * Compress $string based on $extension
 		 *
 		 * @param string $string
 		 * @param string $extension
 		 */
 		function compressString(&$string, $extension)
 		{
 			if ($extension == 'js') {
 				$minifier =& $this->Application->makeClass('JsMinifyHelper');
 				/* @var $minifier JsMinifyHelper */
 
 				$string = $minifier->minify($string);
 			}
 			elseif ($extension == 'css') {
 				$minifier =& $this->Application->makeClass('CssMinifyHelper');
 				/* @var $minifier CssMinifyHelper */
 
 				$string = $minifier->minify($string);
 			}
 		}
 
 		/**
 		 * Get full paths on disk for each of given templates
 		 *
 		 * @param Array $templates
 		 * @return Array
 		 */
 		function _getTemplatePaths($templates)
 		{
 			$ret = Array ();
 			$reg_exp = '/^' . preg_quote($this->Application->BaseURL(), '/') . '(.*)/';
 
 			foreach ($templates as $template) {
 				if (!$template) {
 					continue;
 				}
 
 				if (preg_match($reg_exp, $template, $regs)) {
 					// full url (from current domain) to a file
 					$path = FULL_PATH . '/' . $regs[1];
 				}
 				else {
 					list ($path, $module_filename) = $this->Application->TemplatesCache->GetTemplatePaths($template);
 					$path .= DIRECTORY_SEPARATOR . $module_filename;
 				}
 
 				$ret[] = $path;
 			}
 
 			return $ret;
 		}
 	}
\ No newline at end of file
Index: branches/5.1.x/core/install.php
===================================================================
--- branches/5.1.x/core/install.php	(revision 13689)
+++ branches/5.1.x/core/install.php	(revision 13690)
@@ -1,1471 +1,1458 @@
 <?php
 /**
 * @version	$Id$
 * @package	In-Portal
 * @copyright	Copyright (C) 1997 - 2009 Intechnic. All rights reserved.
 * @license      GNU/GPL
 * In-Portal is Open Source software.
 * This means that this software may have been modified pursuant
 * 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.
 * See http://www.in-portal.org/license for copyright notices and details.
 */
 	ini_set('display_errors', 1);
 	error_reporting(E_ALL);
 
 	define('IS_INSTALL', 1);
 	define('ADMIN', 1);
 	define('FULL_PATH', realpath(dirname(__FILE__).'/..') );
 	define('REL_PATH', '/core');
 
 	// run installator
 	$install_engine = new kInstallator();
 	$install_engine->Init();
 	$install_engine->Run();
 	$install_engine->Done();
 
 	class kInstallator {
 
 		/**
 		 * Reference to kApplication class object
 		 *
 		 * @var kApplication
 		 */
 		var $Application = null;
 
 		/**
 		 * Connection to database
 		 *
 		 * @var kDBConnection
 		 */
 		var $Conn = null;
 
 		/**
 		 * XML file containing steps information
 		 *
 		 * @var string
 		 */
 		var $StepDBFile = '';
 
 		/**
 		 * Step name, that currently being processed
 		 *
 		 * @var string
 		 */
 		var $currentStep = '';
 
 		/**
 		 * Steps list (preset) to use for current installation
 		 *
 		 * @var string
 		 */
 		var $stepsPreset = '';
 
 		/**
 		 * Installtion steps to be done
 		 *
 		 * @var Array
 		 */
 		var $steps = Array (
 			'fresh_install'		=>	Array ('check_paths', 'db_config', 'select_license', /*'download_license',*/ 'select_domain', 'root_password', 'choose_modules', 'post_config', 'select_theme', 'security', 'finish'),
 			'clean_reinstall'	=>	Array ('check_paths', 'clean_db', 'db_config', 'select_license', /*'download_license',*/ 'select_domain', 'root_password', 'choose_modules', 'post_config', 'select_theme', 'security', 'finish'),
 			'already_installed'	=>	Array ('check_paths', 'install_setup'),
 
 			'upgrade'			=>	Array ('check_paths', 'install_setup', 'upgrade_modules', 'security', 'finish'),
 			'update_license'	=>	Array ('check_paths', 'install_setup', 'select_license', /*'download_license',*/ 'select_domain', 'security', 'finish'),
 			'db_reconfig'		=>	Array ('check_paths', 'install_setup', 'db_reconfig', 'security', 'finish'),
 			'fix_paths'			=>	Array ('check_paths', 'install_setup', 'fix_paths', 'security', 'finish'),
 		);
 
 		/**
 		 * Steps, that doesn't required admin to be logged-in to proceed
 		 *
 		 * @var Array
 		 */
 		var $skipLoginSteps = Array ('check_paths', 'select_license', /*'download_license',*/ 'select_domain', 'root_password', 'choose_modules', 'post_config', 'select_theme', 'security', 'finish', -1);
 
 		/**
 		 * Steps, on which kApplication should not be initialized, because of missing correct db table structure
 		 *
 		 * @var Array
 		 */
 		var $skipApplicationSteps = Array ('check_paths', 'clean_db', 'db_config', 'db_reconfig' /*, 'install_setup'*/); // remove install_setup when application will work separately from install
 
 		/**
 		 * Folders that should be writeable to continue installation. $1 - main writeable folder from config.php ("/system" by default)
 		 *
 		 * @var Array
 		 */
 		var $writeableFolders = Array (
 			'$1',
 			'$1/images',
 			'$1/images/pending',
 			'$1/images/emoticons', // for "In-Bulletin"
 			'$1/user_files',
 			'$1/cache',
 		);
 
 		/**
 		 * Contains last error message text
 		 *
 		 * @var string
 		 */
 		var $errorMessage = '';
 
 		/**
 		 * Base path for includes in templates
 		 *
 		 * @var string
 		 */
 		var $baseURL = '';
 
 		/**
 		 * Holds number of last executed query in the SQL
 		 *
 		 * @var int
 		 */
 		var $LastQueryNum = 0;
 
 		/**
 		 * Common tools required for installation process
 		 *
 		 * @var kInstallToolkit
 		 */
 		var $toolkit = null;
 
 		function Init()
 		{
 			include_once(FULL_PATH . REL_PATH . '/kernel/utility/multibyte.php');	// emulating multi-byte php extension
 			require_once(FULL_PATH . REL_PATH . '/install/install_toolkit.php'); // toolkit required for module installations to installator
 			$this->toolkit = new kInstallToolkit();
 			$this->toolkit->setInstallator($this);
 
 			$this->StepDBFile = FULL_PATH.'/'.REL_PATH.'/install/steps_db.xml';
 
 			$base_path = rtrim(preg_replace('/'.preg_quote(rtrim(REL_PATH, '/'), '/').'$/', '', str_replace('\\', '/', dirname($_SERVER['PHP_SELF']))), '/');
 			$this->baseURL = 'http://'.$_SERVER['HTTP_HOST'].$base_path.'/core/install/';
 
 			set_error_handler( Array(&$this, 'ErrorHandler') );
 
 			if (file_exists($this->toolkit->INIFile)) {
 				// if config.php found, then check his write permission too
 				$this->writeableFolders[] = $this->toolkit->defaultWritablePath . '/config.php';
 			}
 
 			if (!$this->toolkit->getSystemConfig('Misc', 'WriteablePath')) {
 				// set global writable folder when such setting is missing
 				$this->toolkit->setSystemConfig('Misc', 'WriteablePath', $this->toolkit->defaultWritablePath);
 				$this->toolkit->SaveConfig(true); // immediately save, because this path will be used in Application later
 			}
 
 			$this->currentStep = $this->GetVar('step');
 
 			// can't check login on steps where no application present anyways :)
 			$this->skipLoginSteps = array_unique(array_merge($this->skipLoginSteps, $this->skipApplicationSteps));
 
 			$this->SelectPreset();
 
 			if (!$this->currentStep) {
 				$this->SetFirstStep(); // sets first step of current preset
 			}
 
 			$this->InitStep();
 		}
 
 		function SetFirstStep()
 		{
 			reset($this->steps[$this->stepsPreset]);
 			$this->currentStep = current($this->steps[$this->stepsPreset]);
 		}
 
 		/**
 		 * Selects preset to proceed based on various criteria
 		 *
 		 */
 		function SelectPreset()
 		{
 			$preset = $this->GetVar('preset');
 			if ($this->toolkit->systemConfigFound()) {
 				// only at installation first step
 				$status = $this->CheckDatabase(false);
 				if ($status && $this->AlreadyInstalled()) {
 					// if already installed, then all future actions need login to work
 					$this->skipLoginSteps = Array ('check_paths', -1);
 					if (!$preset) {
 						$preset = 'already_installed';
 						$this->currentStep = '';
 					}
 				}
 			}
 			if ($preset === false) {
 				$preset = 'fresh_install'; // default preset
 			}
 
 			$this->stepsPreset = $preset;
 		}
 
 		function GetVar($name)
 		{
 			return array_key_exists($name, $_REQUEST) ? $_REQUEST[$name] : false;
 		}
 
 		function SetVar($name, $value)
 		{
 			$_REQUEST[$name] = $value;
 		}
 
 		/**
 		 * Performs needed intialization of data, that step requires
 		 *
 		 */
 		function InitStep()
 		{
 			$require_login = !in_array($this->currentStep, $this->skipLoginSteps);
 			$this->InitApplication($require_login);
 
 			if ($require_login) {
 				// step require login to proceed
 				if (!$this->Application->LoggedIn()) {
 					$this->stepsPreset = 'already_installed';
 					$this->currentStep = 'install_setup'; // manually set 2nd step, because 'check_paths' step doesn't contain login form
 //					$this->SetFirstStep();
 				}
 			}
 
 			switch ($this->currentStep) {
 				case 'check_paths':
 					$writeable_base = $this->toolkit->getSystemConfig('Misc', 'WriteablePath');
 					foreach ($this->writeableFolders as $folder_path) {
 						$file_path = FULL_PATH . str_replace('$1', $writeable_base, $folder_path);
 						if (file_exists($file_path) && !is_writable($file_path)) {
 							$this->errorMessage = '<br/>Installation can not continue until all required permissions are set correctly';
 							break;
 						}
 					}
 					break;
 
 				case 'clean_db':
 					// don't use Application, because all tables will be erased and it will crash
 					$sql = 'SELECT Path
 							FROM ' . TABLE_PREFIX . 'Modules';
 					$modules = $this->Conn->GetCol($sql);
 
 					foreach ($modules as $module_folder) {
 						$remove_file = '/' . $module_folder . 'install/remove_schema.sql';
 						if (file_exists(FULL_PATH . $remove_file)) {
 							$this->toolkit->RunSQL($remove_file);
 						}
 					}
 
 					$this->currentStep = $this->GetNextStep();
 					break;
 
 				case 'db_config':
 				case 'db_reconfig':
 					$fields = Array (
 						'DBType', 'DBHost', 'DBName', 'DBUser',
 						'DBUserPassword', 'DBCollation', 'TablePrefix'
 					);
 
 					// set fields
 					foreach ($fields as $field_name) {
 						$submit_value = $this->GetVar($field_name);
 
 						if ($submit_value !== false) {
 							$this->toolkit->setSystemConfig('Database', $field_name, $submit_value);
 						}
 						/*else {
 							$this->toolkit->setSystemConfig('Database', $field_name, '');
 						}*/
 					}
 			        break;
 
 				case 'download_license':
 					$license_source = $this->GetVar('license_source');
 					if ($license_source !== false && $license_source != 1) {
 						// previous step was "Select License" and not "Download from Intechnic" option was selected
 						$this->currentStep = $this->GetNextStep();
 					}
 					break;
 
 				case 'choose_modules':
 					// if no modules found, then proceed to next step
 					$modules = $this->ScanModules();
 					if (!$modules) {
 						$this->currentStep = $this->GetNextStep();
 					}
 					break;
 
 				case 'select_theme':
 					// put available theme list in database
 					$this->toolkit->rebuildThemes();
 					break;
 
 				case 'upgrade_modules':
 					// get installed modules from db and compare their versions to upgrade script
 					$modules = $this->GetUpgradableModules();
 					if (!$modules) {
 						$this->currentStep = $this->GetNextStep();
 					}
 					break;
 
 				case 'install_setup':
 					$next_preset = $this->Application->GetVar('next_preset');
 					if ($next_preset !== false) {
 						if ($this->Application->GetVar('login') == 'root') {
 							// verify "root" user using configuration settings
 							$login_event = new kEvent('u.current:OnLogin');
 							$this->Application->HandleEvent($login_event);
 
 							if ($login_event->status != erSUCCESS) {
 								$user =& $this->Application->recallObject('u.current');
 								/* @var $user UsersItem */
 
 								$this->errorMessage = $user->GetErrorMsg('ValidateLogin') . '. If you don\'t know your username or password, contact Intechnic Support';
 							}
 						}
 						else {
 							// non "root" user -> verify using licensing server
 							$url_params = Array (
 								'login=' . md5( $this->GetVar('login') ),
 								'password=' . md5( $this->GetVar('password') ),
 								'action=check',
 								'license_code=' . base64_encode( $this->toolkit->getSystemConfig('Intechnic', 'LicenseCode') ),
 								'version=' . '4.3.0',//$this->toolkit->GetMaxModuleVersion('core/'),
 								'domain=' . base64_encode($_SERVER['HTTP_HOST']),
 							);
 
 							$license_url = GET_LICENSE_URL . '?' . implode('&', $url_params);
 							$file_data = curl_post($license_url, '', null, 'GET');
 
 							if (substr($file_data, 0, 5) == 'Error') {
 								$this->errorMessage = substr($file_data, 6) . ' If you don\'t know your username or password, contact Intechnic Support';
 							}
 
 							if ($this->errorMessage == '') {
 								$user_id = USER_ROOT;
 								$session =& $this->Application->recallObject('Session');
 								$session->SetField('PortalUserId', $user_id);
 								$this->Application->SetVar('u.current_id', $user_id);
 								$this->Application->StoreVar('user_id', $user_id);
 							}
 						}
 
 						if ($this->errorMessage == '') {
 							// processed with redirect to selected step preset
 							if (!isset($this->steps[$next_preset])) {
 								$this->errorMessage = 'Preset "'.$next_preset.'" not yet implemented';
 							}
 							else {
 								$this->stepsPreset = $next_preset;
 							}
 						}
 					}
 					else {
 						// if preset was not choosen, then raise error
 						$this->errorMessage = 'Please select action to perform';
 					}
 					break;
 
 				case 'security':
 					// perform write check
 					if ($this->Application->GetVar('skip_security_check')) {
 						// administrator intensionally skips security checks
 						break;
 					}
 
 					$write_check = true;
 					$check_paths = Array ('/', '/index.php', $this->toolkit->defaultWritablePath . '/config.php', ADMIN_DIRECTORY . '/index.php');
 					foreach ($check_paths as $check_path) {
 						$path_check_status = $this->toolkit->checkWritePermissions(FULL_PATH . $check_path);
 
 						if (is_bool($path_check_status) && $path_check_status) {
 							$write_check = false;
 							break;
 						}
 					}
 
 					// script execute check
 					if (file_exists(WRITEABLE . '/install_check.php')) {
 						unlink(WRITEABLE . '/install_check.php');
 					}
 
 					$fp = fopen(WRITEABLE . '/install_check.php', 'w');
 					fwrite($fp, "<?php\n\techo 'OK';\n");
 					fclose($fp);
 
 					$curl_helper =& $this->Application->recallObject('CurlHelper');
 					/* @var $curl_helper kCurlHelper */
 
 					$output = $curl_helper->Send($this->Application->BaseURL(WRITEBALE_BASE) . 'install_check.php');
 					unlink(WRITEABLE . '/install_check.php');
 					$execute_check = ($output !== 'OK');
 
 					$directive_check = true;
 					$ini_vars = Array ('register_globals' => false, 'open_basedir' => true, 'allow_url_fopen' => false);
 					foreach ($ini_vars as $var_name => $var_value) {
 						$current_value = ini_get($var_name);
 
 						if (($var_value && !$current_value) || (!$var_value && $current_value)) {
 							$directive_check = false;
 							break;
 						}
 					}
 
 					if (!$write_check || !$execute_check || !$directive_check) {
 						$this->errorMessage = true;
 					}
 					/*else {
 						$this->currentStep = $this->GetNextStep();
 					}*/
 					break;
 			}
 
 			$this->PerformValidation(); // returns validation status (just in case)
 		}
 
 		/**
 		 * Validates data entered by user
 		 *
 		 * @return bool
 		 */
 		function PerformValidation()
 		{
 			if ($this->GetVar('step') != $this->currentStep) {
 				// just redirect from previous step, don't validate
 				return true;
 			}
 
 			$status = true;
 
 			switch ($this->currentStep) {
 				case 'db_config':
 				case 'db_reconfig':
 					// 1. check if required fields are filled
 					$section_name = 'Database';
 					$required_fields = Array ('DBType', 'DBHost', 'DBName', 'DBUser', 'DBCollation');
 					foreach ($required_fields as $required_field) {
 						if (!$this->toolkit->getSystemConfig($section_name, $required_field)) {
 							$status = false;
 							$this->errorMessage = 'Please fill all required fields';
 							break;
 						}
 					}
 					if (!$status) break;
 
 					// 2. check permissions, that use have in this database
 					$status = $this->CheckDatabase(($this->currentStep == 'db_config') && !$this->GetVar('UseExistingSetup'));
 					break;
 
 				case 'select_license':
 					$license_source = $this->GetVar('license_source');
 					if ($license_source == 2) {
 						// license from file -> file must be uploaded
 						$upload_error = $_FILES['license_file']['error'];
 						if ($upload_error != UPLOAD_ERR_OK) {
 							$this->errorMessage = 'Missing License File';
 						}
 					}
 					elseif (!is_numeric($license_source)) {
 						$this->errorMessage = 'Please select license';
 					}
 
 					$status = $this->errorMessage == '';
 					break;
 
 				case 'root_password':
 					// check, that password & verify password match
 					$password = $this->Application->GetVar('root_password');
 					$password_verify = $this->Application->GetVar('root_password_verify');
 
 					if ($password != $password_verify) {
 						$this->errorMessage = 'Passwords does not match';
 					}
 					elseif (mb_strlen($password) < 4) {
 						$this->errorMessage = 'Root Password must be at least 4 characters';
 					}
 
 					$status = $this->errorMessage == '';
 					break;
 
 				case 'choose_modules':
 					break;
 
 				case 'upgrade_modules':
 					$modules = $this->Application->GetVar('modules');
 					if (!$modules) {
 						$modules = Array ();
 						$this->errorMessage = 'Please select module(-s) to ' . ($this->currentStep == 'choose_modules' ? 'install' : 'upgrade');
 					}
 
 					// check interface module
 					$upgrade_data = $this->GetUpgradableModules();
 
 					if (array_key_exists('core', $upgrade_data) && !in_array('core', $modules)) {
 						// core can be upgraded, but isn't selected
 						$this->errorMessage = 'Please select "Core" as interface module';
 					}
 
 					$status = $this->errorMessage == '';
 					break;
 			}
 
 			return $status;
 		}
 
 		/**
 		 * Perform installation step actions
 		 *
 		 */
 		function Run()
 		{
 			if ($this->errorMessage) {
 				// was error during data validation stage
 				return ;
 			}
 
 			switch ($this->currentStep) {
 				case 'db_config':
 				case 'db_reconfig':
 					// store db configuration
 					$sql = 'SHOW COLLATION
 							LIKE \''.$this->toolkit->getSystemConfig('Database', 'DBCollation').'\'';
 					$collation_info = $this->Conn->Query($sql);
 					if ($collation_info) {
 						$this->toolkit->setSystemConfig('Database', 'DBCharset', $collation_info[0]['Charset']);
 
 						// database is already connected, that's why set collation on the fly
 						$this->Conn->Query('SET NAMES \''.$this->toolkit->getSystemConfig('Database', 'DBCharset').'\' COLLATE \''.$this->toolkit->getSystemConfig('Database', 'DBCollation').'\'');
 					}
 
 					$this->toolkit->SaveConfig();
 
 					if ($this->currentStep == 'db_config') {
 						if ($this->GetVar('UseExistingSetup')) {
 							// abort clean install and redirect to already_installed
 							$this->stepsPreset = 'already_installed';
 							break;
 						}
 
 						// import base data into new database, not for db_reconfig
 						$this->toolkit->RunSQL('/core/install/install_schema.sql');
 						$this->toolkit->RunSQL('/core/install/install_data.sql');
 
 						// create category using sql, because Application is not available here
 						$table_name = $this->toolkit->getSystemConfig('Database', 'TablePrefix') . 'IdGenerator';
 						$this->Conn->Query('UPDATE ' . $table_name . ' SET lastid = lastid + 1');
 						$resource_id = $this->Conn->GetOne('SELECT lastid FROM ' . $table_name);
 						if ($resource_id === false) {
 							$this->Conn->Query('INSERT INTO '.$table_name.' (lastid) VALUES (2)');
 							$resource_id = 2;
 						}
 
 						$fields_hash = Array (
 							'l1_Name' => 'Content', 'Filename' => 'Content', 'AutomaticFilename' => 0,
 							'CreatedById' => USER_ROOT, 'CreatedOn' => time(), 'ResourceId' => $resource_id - 1,
 							'l1_Description' => 'Content', 'Status' => 4,
 						);
 
 						$this->Conn->doInsert($fields_hash, $this->toolkit->getSystemConfig('Database', 'TablePrefix') . 'Category');
 
 						$this->toolkit->SetModuleRootCategory('Core', $this->Conn->getInsertID());
 
 						// set module "Core" version after install (based on upgrade scripts)
 						$this->toolkit->SetModuleVersion('Core', 'core/');
 
 						// for now we set "In-Portal" module version to "Core" module version (during clean install)
 						$this->toolkit->SetModuleVersion('In-Portal', 'core/');
 					}
 					break;
 
 				case 'select_license':
 					// reset memory cache, when application is first available (on fresh install and clean reinstall steps)
 					$this->Application->HandleEvent($event, 'adm:OnResetMemcache');
 
 					$license_source = $this->GetVar('license_source');
 					switch ($license_source) {
 						case 1: // Download from Intechnic
 
 							break;
 
 						case 2: // Upload License File
 							$file_data = array_map('trim', file($_FILES['license_file']['tmp_name']));
 							if ((count($file_data) == 3) && $file_data[1]) {
 								$modules_helper =& $this->Application->recallObject('ModulesHelper');
 								/* @var $modules_helper kModulesHelper */
 
 								if ($modules_helper->verifyLicense($file_data[1])) {
 									$this->toolkit->setSystemConfig('Intechnic', 'License', $file_data[1]);
 									$this->toolkit->setSystemConfig('Intechnic', 'LicenseCode', $file_data[2]);
 									$this->toolkit->SaveConfig();
 								}
 								else {
 									$this->errorMessage = 'Invalid License File';
 								}
 							}
 							else {
 								$this->errorMessage = 'Invalid License File';
 							}
 							break;
 
 						case 3: // Use Existing License
 							$license_hash = $this->toolkit->getSystemConfig('Intechnic', 'License');
 							if ($license_hash) {
 								$modules_helper =& $this->Application->recallObject('ModulesHelper');
 								/* @var $modules_helper kModulesHelper */
 
 								if (!$modules_helper->verifyLicense($license_hash)) {
 									$this->errorMessage = 'Invalid or corrupt license detected';
 								}
 							}
 							else {
 								// happens, when browser's "Back" button is used
 								$this->errorMessage = 'Missing License File';
 							}
 							break;
 
 						case 4: // Skip License (Local Domain Installation)
 							if ($this->toolkit->sectionFound('Intechnic')) {
 								// remove any previous license information
 								$this->toolkit->setSystemConfig('Intechnic', 'License');
 								$this->toolkit->setSystemConfig('Intechnic', 'LicenseCode');
 								$this->toolkit->SaveConfig();
 							}
 							break;
 					}
 					break;
 
 				case 'download_license':
 					$license_login = $this->GetVar('login');
 					$license_password = $this->GetVar('password');
 					$license_id = $this->GetVar('licenses');
 
 					if (strlen($license_login) && strlen($license_password) && !$license_id) {
 						// Here we determine weather login is ok & check available licenses
 						$url_params = Array (
 							'login=' . md5($license_login),
 							'password=' . md5($license_password),
 							'version=' . $this->toolkit->GetMaxModuleVersion('core/'),
 							'domain=' . base64_encode($_SERVER['HTTP_HOST']),
 						);
 
 						$license_url = GET_LICENSE_URL . '?' . implode('&', $url_params);
 						$file_data = curl_post($license_url, '', null, 'GET');
 						if (!$file_data) {
 							// error connecting to licensing server
 							$this->errorMessage = 'Unable to connect to the Intechnic server! Please try again later!';
 						}
 						else {
 							if (substr($file_data, 0, 5) == 'Error') {
 								// after processing data server returned error
 								$this->errorMessage = substr($file_data, 6);
 							}
 							else {
 								// license received
 								if (substr($file_data, 0, 3) == 'SEL') {
 									// we have more, then one license -> let user choose
 									$this->SetVar('license_selection', base64_encode( substr($file_data, 4) )); // we received html with radio buttons with names "licenses"
 									$this->errorMessage = 'Please select which license to use';
 								}
 								else {
 									// we have one license
 									$this->toolkit->processLicense($file_data);
 								}
 							}
 						}
 					}
 					else if (!$license_id) {
 						// licenses were not queried AND user/password missing
 						$this->errorMessage = 'Incorrect Username or Password. If you don\'t know your username or password, contact Intechnic Support';
 					}
 					else {
 						// Here we download license
 						$url_params = Array (
 							'license_id=' . md5($license_id),
 							'dlog=' . md5($license_login),
 							'dpass=' . md5($license_password),
 							'version=' . $this->toolkit->GetMaxModuleVersion('core/'),
 							'domain=' . base64_encode($_SERVER['HTTP_HOST']),
 						);
 
 						$license_url = GET_LICENSE_URL . '?' . implode('&', $url_params);
 						$file_data = curl_post($license_url, '', null, 'GET');
 
 						if (!$file_data) {
 							// error connecting to licensing server
 							$this->errorMessage = 'Unable to connect to the Intechnic server! Please try again later!';
 						}
 						else {
 							if (substr($file_data, 0, 5) == 'Error') {
 								// after processing data server returned error
 								$this->errorMessage = substr($file_data, 6);
 							}
 							else {
 								$this->toolkit->processLicense($file_data);
 							}
 						}
 					}
 					break;
 
 				case 'select_domain':
 					$modules_helper =& $this->Application->recallObject('ModulesHelper');
 					/* @var $modules_helper kModulesHelper */
 
 					$license_hash = $this->toolkit->getSystemConfig('Intechnic', 'License');
 					if ($license_hash) {
 						// when license present, then extract domain from it
 						$license_hash = base64_decode($license_hash);
 						list ( , , $license_keys) = $modules_helper->_ParseLicense($license_hash);
 						$license_domain = $license_keys[0]['domain'];
 					}
 					else {
 						// when license missing, then use current domain
 						$license_domain = $_SERVER['HTTP_HOST'];
 					}
 
 					$domain = $this->GetVar('domain') == 1 ? $_SERVER['HTTP_HOST'] : str_replace(' ', '', $this->GetVar('other'));
 
 					if ($domain != '') {
 						if (strstr($domain, $license_domain) || $modules_helper->_IsLocalSite($domain)) {
 							$this->toolkit->setSystemConfig('Misc', 'Domain', $domain);
 							$this->toolkit->SaveConfig();
 						}
 						else {
 							$this->errorMessage = 'Domain name entered does not match domain name in the license!';
 						}
 					}
 					else {
 						$this->errorMessage = 'Please enter valid domain!';
 					}
 					break;
 
 				case 'root_password':
 					// update root password in database
 					$password = md5( md5($this->Application->GetVar('root_password')) . 'b38');
 
 					$config_values = Array (
 						'RootPass' => $password,
 						'Site_Path' => BASE_PATH.'/', // set Site_Path (for SSL & old in-portal code)
 						'Backup_Path' => FULL_PATH . $this->toolkit->getSystemConfig('Misc', 'WriteablePath') . DIRECTORY_SEPARATOR . 'backupdata',
 						'Smtp_AdminMailFrom' => 'portal@' . $this->toolkit->getSystemConfig('Misc', 'Domain')
 					);
 
 					$site_timezone = ini_get('date.timezone') ? ini_get('date.timezone') : getenv('TZ');
 
 					if ($site_timezone) {
 						$config_values['Config_Site_Time'] = $site_timezone;
 					}
 
 					$this->toolkit->saveConfigValues($config_values);
 
 					// login as "root", when no errors on password screen
 					$this->Application->SetVar('login', 'root');
 					$this->Application->SetVar('password', $this->Application->GetVar('root_password'));
 
 					$login_event = new kEvent('u.current:OnLogin');
 					$this->Application->HandleEvent($login_event);
 
 					// import base language for core (english)
 					$this->toolkit->ImportLanguage('/core/install/english');
 
 					// make sure imported language is set as active in session, created during installation
 					$this->Application->Session->SetField('Language', 1);
 
 					// set imported language as primary
 					$lang =& $this->Application->recallObject('lang.-item', null, Array('skip_autoload' => true));
 					/* @var $lang LanguagesItem */
 
 					$lang->Load(1); // fresh install => ID=1
 					$lang->setPrimary(true); // for Front-End
 					break;
 
 				case 'choose_modules':
 					// run module install scripts
 					$modules = $this->Application->GetVar('modules');
 					if ($modules) {
 						foreach ($modules as $module) {
 							$install_file = MODULES_PATH.'/'.$module.'/install.php';
 							if (file_exists($install_file)) {
 								include_once($install_file);
 							}
 						}
 					}
 
 					// update category cache
 					$updater =& $this->Application->recallObject('kPermCacheUpdater');
 					/* @var $updater kPermCacheUpdater */
 
 					$updater->OneStepRun();
 					break;
 
 				case 'post_config':
 					$this->toolkit->saveConfigValues( $this->GetVar('config') );
 					break;
 
 				case 'select_theme':
 					// 1. mark theme, that user is selected
 					$theme_id = $this->GetVar('theme');
 					$theme_table = $this->Application->getUnitOption('theme', 'TableName');
 					$theme_idfield = $this->Application->getUnitOption('theme', 'IDField');
 
 					$sql = 'UPDATE ' . $theme_table . '
 							SET Enabled = 1, PrimaryTheme = 1
 							WHERE ' . $theme_idfield . ' = ' . $theme_id;
 					$this->Conn->Query($sql);
 
 					$this->toolkit->rebuildThemes(); // rescan theme to create structure after theme is enabled !!!
 
-					if ($this->Application->isModuleEnabled('In-Portal')) {
-						// 2. compile theme stylesheets (only In-Portal uses them)
-						$css_table = $this->Application->getUnitOption('css', 'TableName');
-						$css_idfield = $this->Application->getUnitOption('css', 'IDField');
-
-						$sql = 'SELECT LOWER(Name) AS Name, ' . $css_idfield . '
-								FROM ' . $css_table;
-						$css_hash = $this->Conn->GetCol($sql, $css_idfield);
-
-						$css_item =& $this->Application->recallObject('css', null, Array('skip_autoload' => true));
-						/* @var $css_item StyleshetsItem */
-
-						foreach ($css_hash as $stylesheet_id => $theme_name) {
-							$css_item->Load($stylesheet_id);
-							$css_item->Compile();
-
-							$sql = 'UPDATE ' . $theme_table . '
-									SET StylesheetId = ' . $stylesheet_id . '
-									WHERE LOWER(Name) = ' .  $this->Conn->qstr($theme_name);
-							$this->Conn->Query($sql);
-						}
-					}
-
 					// install theme dependent demo data
 					if ($this->Application->GetVar('install_demo_data')) {
 						$sql = 'SELECT Name
 								FROM ' . $theme_table . '
 								WHERE ' . $theme_idfield . ' = ' . $theme_id;
 						$theme_name = $this->Conn->GetOne($sql);
 
 						foreach ($this->Application->ModuleInfo as $module_name => $module_info) {
 							if ($module_name == 'In-Portal') {
 								continue;
 							}
 
 							$this->toolkit->RunSQL('/themes' . '/' . $theme_name . '/' . $module_info['TemplatePath'] . '_install/install_data.sql', '{ThemeId}', $theme_id);
 						}
 					}
 					break;
 
 				case 'upgrade_modules':
 					// get installed modules from db and compare their versions to upgrade script
 					$modules = $this->Application->GetVar('modules');
 					if ($modules) {
 						$upgrade_data = $this->GetUpgradableModules();
 
 						$start_from_module = $this->GetVar('continue_from_module');
 						$start_from_query = $this->GetVar('continue_from_query');
 						if (!$start_from_query) $start_from_query = 0;
 						foreach ($modules as $module_name) {
 							if ($start_from_module && $module_name != $start_from_module) {
 								continue;
 							}
 							else {
 								$start_from_module = false; //otherwise it will skip all modules after the one we start with!
 							}
 							$module_info = $upgrade_data[$module_name];
 							$upgrades_file = sprintf(UPGRADES_FILE, $module_info['Path'], 'sql');
 
 							$sqls = file_get_contents($upgrades_file);
 							$version_mark = preg_replace('/(\(.*?\))/', $module_info['FromVersion'], VERSION_MARK);
 
 							// get only sqls from next (relative to current) version to end of file
 							$start_pos = strpos($sqls, $version_mark);
 							$sqls = substr($sqls, $start_pos);
 
 							preg_match_all('/'.VERSION_MARK.'/s', $sqls, $regs);
 
 							if (!$start_from_module) {
 								$this->RunUpgrades($module_info['Path'], $regs[1], 'before');
 							}
 							if (!$this->toolkit->RunSQLText($sqls, null, null, $start_from_query)) {
 								$this->errorMessage .= '<input type="hidden" name="continue_from_module" value="'.$module_name.'">';
 								$this->errorMessage .= '<input type="hidden" name="continue_from_query" value="'.$this->LastQueryNum.'">';
 								$this->errorMessage .= '<br/>Click Continue button below to skip this query and go further<br/>';
 								$this->Done();
 							}
 							$start_from_query = 0; // so that next module start from the beggining
 
 							$this->toolkit->ImportLanguage('/' . $module_info['Path'] . 'install/english', true);
 							$this->RunUpgrades($module_info['Path'], $regs[1], 'after'); // upgrade script could operate resulting language pack
 
 							// after upgrade sqls are executed update version and upgrade language pack
 							$this->toolkit->SetModuleVersion($module_name, false, $module_info['ToVersion']);
 						}
 
 						// for now we set "In-Portal" module version to "Core" module version (during upgrade)
 						if (in_array('core', $modules)) {
 							$this->toolkit->SetModuleVersion('In-Portal', false, $upgrade_data['core']['ToVersion']);
 						}
 					}
 					break;
 
 				case 'fix_paths':
 					$this->toolkit->saveConfigValues( $this->Application->GetVar('config') );
 					break;
 
 				case 'finish':
 					// delete cache
 					$this->toolkit->deleteCache();
 					$this->toolkit->rebuildThemes();
 
+					// compile admin skin, so it will be available in 3 frames at once
+					$skin_helper =& $this->Application->recallObject('SkinHelper');
+					/* @var $skin_helper SkinHelper */
+
+					$skin =& $this->Application->recallObject('skin', null, Array ('skip_autoload' => true));
+					/* @var $skin kDBItem */
+
+					$skin->Load(1, 'IsPrimary');
+					$skin_helper->compile($skin);
+
 					// set installation finished mark
 					if ($this->Application->ConfigValue('InstallFinished') === false) {
 						$fields_hash = Array (
 							'VariableName'	=>	'InstallFinished',
 							'VariableValue'	=>	1,
 						);
 						$this->Conn->doInsert($fields_hash, TABLE_PREFIX.'ConfigurationValues');
 					}
 					break;
 			}
 
 			if ($this->errorMessage) {
 				// was error during run stage
 				return ;
 			}
 
 			$this->currentStep = $this->GetNextStep();
 			$this->InitStep(); // init next step (that will be shown now)
 
 			$this->InitApplication();
 
 			if ($this->currentStep == -1) {
 				// step after last step -> redirect to admin
 				$this->Application->Redirect('index', null, '', 'index.php');
 			}
 		}
 
 		/**
 		 * Run upgrade PHP scripts for module with specified path
 		 *
 		 * @param string $module_path
 		 * @param Array $versions
 		 * @param string $mode upgrade mode = {before,after}
 		 */
 		function RunUpgrades($module_path, $versions, $mode)
 		{
 			static $upgrade_classes = Array ();
 
 			$upgrades_file = sprintf(UPGRADES_FILE, $module_path, 'php');
 			if (!file_exists($upgrades_file) || !$versions) {
 				return ;
 			}
 
 			if (!isset($upgrade_classes[$module_path])) {
 				// save class name, because 2nd time
 				// (in after call $upgrade_class variable will not be present)
 				include_once $upgrades_file;
 				$upgrade_classes[$module_path] = $upgrade_class;
 			}
 
 			$upgrade_object = new $upgrade_classes[$module_path]();
 			if (method_exists($upgrade_object, 'setToolkit')) {
 				$upgrade_object->setToolkit($this->toolkit);
 			}
 
 			foreach ($versions as $version) {
 				$upgrade_method = 'Upgrade_'.str_replace(Array ('.', '-'), '_', $version);
 				if (method_exists($upgrade_object, $upgrade_method)) {
 					$upgrade_object->$upgrade_method($mode);
 				}
 			}
 		}
 
 		/**
 		 * Initialize kApplication
 		 *
 		 * @param bool $force initialize in any case
 		 */
 		function InitApplication($force = false)
 		{
 			if (($force || !in_array($this->currentStep, $this->skipApplicationSteps)) && !isset($this->Application)) {
 				// step is allowed for application usage & it was not initialized in previous step
 				global $start, $debugger, $dbg_options, $vars;
 				include_once(FULL_PATH.'/core/kernel/startup.php');
 
 				$this->Application =& kApplication::Instance();
 				$this->toolkit->Application =& kApplication::Instance();
 
 				$this->Application->Init();
 
 				$this->Conn =& $this->Application->GetADODBConnection();
 				$this->toolkit->Conn =& $this->Application->GetADODBConnection();
 			}
 		}
 
 		/**
 		 * Show next step screen
 		 *
 		 */
 		function Done($error_message = null)
 		{
 			if (isset($error_message)) {
 				$this->errorMessage = $error_message;
 			}
 
 			include_once (FULL_PATH.'/'.REL_PATH.'/install/incs/install.tpl');
 
 			if (isset($this->Application)) {
 				$this->Application->Done();
 			}
 
 			exit;
 		}
 
 		function ConnectToDatabase()
 		{
 			include_once FULL_PATH . '/core/kernel/db/db_connection.php';
 
 			$required_keys = Array ('DBType', 'DBUser', 'DBName');
 			foreach ($required_keys as $required_key) {
 				if (!$this->toolkit->getSystemConfig('Database', $required_key)) {
 					// one of required db connection settings missing -> abort connection
 					return false;
 				}
 			}
 
 			$this->Conn = new kDBConnection($this->toolkit->getSystemConfig('Database', 'DBType'), Array(&$this, 'DBErrorHandler'));
 			$this->Conn->Connect(
 				$this->toolkit->getSystemConfig('Database', 'DBHost'),
 				$this->toolkit->getSystemConfig('Database', 'DBUser'),
 				$this->toolkit->getSystemConfig('Database', 'DBUserPassword'),
 				$this->toolkit->getSystemConfig('Database', 'DBName')
 			);
 
 			// setup toolkit too
 			$this->toolkit->Conn =& $this->Conn;
 
 			return $this->Conn->errorCode == 0;
 		}
 
 		/**
 		 * Checks if core is already installed
 		 *
 		 * @return bool
 		 */
 		function AlreadyInstalled()
 		{
 			$table_prefix = $this->toolkit->getSystemConfig('Database', 'TablePrefix');
 
 			$sql = 'SELECT VariableValue
 					FROM ' . $table_prefix . 'ConfigurationValues
 					WHERE VariableName = "InstallFinished"';
 
 			return $this->TableExists('ConfigurationValues') && $this->Conn->GetOne($sql);
 		}
 
 		function CheckDatabase($check_installed = true)
 		{
 			// perform various check type to database specified
 			// 1. user is allowed to connect to database
 			// 2. user has all types of permissions in database
 
 
 			if (mb_strlen($this->toolkit->getSystemConfig('Database', 'TablePrefix')) > 7) {
 				$this->errorMessage = 'Table prefix should not be longer than 7 characters';
 				return false;
 			}
 
 			// connect to database
 			$status = $this->ConnectToDatabase();
 			if ($status) {
 				// if connected, then check if all sql statements work
 				$sql_tests[] = 'DROP TABLE IF EXISTS test_table';
 				$sql_tests[] = 'CREATE TABLE test_table(test_col mediumint(6))';
 				$sql_tests[] = 'LOCK TABLES test_table WRITE';
 				$sql_tests[] = 'INSERT INTO test_table(test_col) VALUES (5)';
 				$sql_tests[] = 'UPDATE test_table SET test_col = 12';
 				$sql_tests[] = 'UNLOCK TABLES';
 				$sql_tests[] = 'ALTER TABLE test_table ADD COLUMN new_col varchar(10)';
 				$sql_tests[] = 'SELECT * FROM test_table';
 				$sql_tests[] = 'DELETE FROM test_table';
 				$sql_tests[] = 'DROP TABLE IF EXISTS test_table';
 
 				foreach ($sql_tests as $sql_test) {
 					$this->Conn->Query($sql_test);
 					if ($this->Conn->getErrorCode() != 0) {
 						$status = false;
 						break;
 					}
 				}
 
 				if ($status) {
 					// if statements work & connection made, then check table existance
 					if ($check_installed && $this->AlreadyInstalled()) {
 						$this->errorMessage = 'An In-Portal Database already exists at this location';
 						return false;
 					}
 				}
 				else {
 					// user has insufficient permissions in database specified
 					$this->errorMessage = 'Permission Error: ('.$this->Conn->getErrorCode().') '.$this->Conn->getErrorMsg();
 					return false;
 				}
 			}
 			else {
 				// was error while connecting
 				if (!$this->Conn) return false;
 				$this->errorMessage = 'Connection Error: ('.$this->Conn->getErrorCode().') '.$this->Conn->getErrorMsg();
 				return false;
 			}
 
 			return true;
 		}
 
 		/**
 		 * Checks if all passed tables exists
 		 *
 		 * @param string $tables comma separated tables list
 		 * @return bool
 		 */
 		function TableExists($tables)
 		{
 			$prefix = $this->toolkit->getSystemConfig('Database', 'TablePrefix');
 
 			$all_found = true;
 			$tables = explode(',', $tables);
 			foreach ($tables as $table_name) {
 				$sql = 'SHOW TABLES LIKE "'.$prefix.$table_name.'"';
 				if (count($this->Conn->Query($sql)) == 0) {
 					$all_found = false;
 					break;
 				}
 			}
 
 			return $all_found;
 		}
 
 		/**
 		 * Returns modules list found in modules folder
 		 *
 		 * @return Array
 		 */
 		function ScanModules()
 		{
 			static $modules = null;
 
 			if (!isset($modules)) {
 				$modules = Array();
 				$fh = opendir(MODULES_PATH);
 				while ( ($sub_folder = readdir($fh)) ) {
 					$folder_path = MODULES_PATH . '/'.$sub_folder;
 					if ($sub_folder != '.' && $sub_folder != '..' && is_dir($folder_path)) {
 						// this is folder in MODULES_PATH directory
 						if (file_exists($folder_path.'/install.php') && file_exists($folder_path.'/install/install_schema.sql')) {
 							$install_order = trim( file_get_contents($folder_path . '/install/install_order.txt') );
 							$modules[$install_order] = $sub_folder;
 						}
 					}
 				}
 			}
 
 			// allows to control module install order
 			ksort($modules, SORT_NUMERIC);
 			return $modules;
 		}
 
 		/**
 		 * Virtually place module under "modules" folder or it won't be recognized during upgrade to 5.1.0 version
 		 *
 		 * @param string $name
 		 * @param string $path
 		 * @param string $version
 		 * @return string
 		 */
 		function getModulePath($name, $path, $version)
 		{
 			if ($name == 'Core') {
 				// don't transform path for Core module
 				return $path;
 			}
 
 			$version = $this->toolkit->ConvertModuleVersion($version);
 
 			if ($version < $this->toolkit->ConvertModuleVersion('5.1.0')) {
 				return 'modules/' . $path;
 			}
 
 			return $path;
 		}
 
 		/**
 		 * Returns list of modules, that can be upgraded
 		 *
 		 */
 		function GetUpgradableModules()
 		{
 			$ret = Array ();
 
 			foreach ($this->Application->ModuleInfo as $module_name => $module_info) {
 				if ($module_name == 'In-Portal') {
 					// don't show In-Portal, because it shares upgrade scripts with Core module
 					continue;
 				}
 
 				$module_info['Path'] = $this->getModulePath($module_name, $module_info['Path'], $module_info['Version']);
 				$upgrades_file = sprintf(UPGRADES_FILE, $module_info['Path'], 'sql');
 
 				if (!file_exists($upgrades_file)) {
 					// no upgrade file
 					continue;
 				}
 
 				$sqls = file_get_contents($upgrades_file);
 				$versions_found = preg_match_all('/'.VERSION_MARK.'/s', $sqls, $regs);
 				if (!$versions_found) {
 					// upgrades file doesn't contain version definitions
 					continue;
 				}
 
 				$to_version = end($regs[1]);
 				$this_version = $this->toolkit->ConvertModuleVersion($module_info['Version']);
 				if ($this->toolkit->ConvertModuleVersion($to_version) > $this_version) {
 					// destination version is greather then current
 					foreach ($regs[1] as $version) {
 						if ($this->toolkit->ConvertModuleVersion($version) > $this_version) {
 							$from_version = $version;
 							break;
 						}
 					}
 
 					$version_info = Array (
 						'FromVersion'	=>	$from_version,
 						'ToVersion'		=>	$to_version,
 					);
 
 					$ret[ strtolower($module_name) ] = array_merge_recursive2($module_info, $version_info);
 				}
 			}
 
 			return $ret;
 		}
 
 		/**
 		 * Returns content to show for current step
 		 *
 		 * @return string
 		 */
 		function GetStepBody()
 		{
 			$step_template = FULL_PATH.'/core/install/step_templates/'.$this->currentStep.'.tpl';
 			if (file_exists($step_template)) {
 				ob_start();
 				include_once ($step_template);
 				return ob_get_clean();
 			}
 
 			return '{step template "'.$this->currentStep.'" missing}';
 		}
 
 		/**
 		 * Parses step information file, cache result for current step ONLY & return it
 		 *
 		 * @return Array
 		 */
 		function &_getStepInfo()
 		{
 			static $info = Array('help_title' => null, 'step_title' => null, 'help_body' => null, 'queried' => false);
 
 			if (!$info['queried']) {
 				$fdata = file_get_contents($this->StepDBFile);
 
 				$parser = xml_parser_create();
 				xml_parse_into_struct($parser, $fdata, $values, $index);
 				xml_parser_free($parser);
 
 				foreach ($index['STEP'] as $section_index) {
 					$step_data =& $values[$section_index];
 
 					if ($step_data['attributes']['NAME'] == $this->currentStep) {
 						$info['step_title'] = $step_data['attributes']['TITLE'];
 						if (isset($step_data['attributes']['HELP_TITLE'])) {
 							$info['help_title'] = $step_data['attributes']['HELP_TITLE'];
 						}
 						else {
 							// if help title not set, then use step title
 							$info['help_title'] = $step_data['attributes']['TITLE'];
 						}
 						$info['help_body'] = trim($step_data['value']);
 						break;
 					}
 				}
 
 				$info['queried'] = true;
 			}
 
 			return $info;
 		}
 
 		/**
 		 * Returns particular information abou current step
 		 *
 		 * @param string $info_type
 		 * @return string
 		 */
 		function GetStepInfo($info_type)
 		{
 			$step_info =& $this->_getStepInfo();
 
 			if (isset($step_info[$info_type])) {
 				return $step_info[$info_type];
 			}
 
 			return '{step "'.$this->currentStep.'"; param "'.$info_type.'" missing}';
 		}
 
 		/**
 		 * Returns passed steps titles
 		 *
 		 * @param Array $steps
 		 * @return Array
 		 * @see kInstaller:PrintSteps
 		 */
 		function _getStepTitles($steps)
 		{
 			$fdata = file_get_contents($this->StepDBFile);
 
 			$parser = xml_parser_create();
 			xml_parse_into_struct($parser, $fdata, $values, $index);
 			xml_parser_free($parser);
 
 			$ret = Array ();
 			foreach ($index['STEP'] as $section_index) {
 				$step_data =& $values[$section_index];
 				if (in_array($step_data['attributes']['NAME'], $steps)) {
 					$ret[ $step_data['attributes']['NAME'] ] = $step_data['attributes']['TITLE'];
 				}
 			}
 
 			return $ret;
 		}
 
 		/**
 		 * Returns current step number in active steps_preset.
 		 * Value can't be cached, because same step can have different number in different presets
 		 *
 		 * @return int
 		 */
 		function GetStepNumber()
 		{
 			return array_search($this->currentStep, $this->steps[$this->stepsPreset]) + 1;
 		}
 
 		/**
 		 * Returns step name to process next
 		 *
 		 * @return string
 		 */
 		function GetNextStep()
 		{
 			$next_index = $this->GetStepNumber();
 			if ($next_index > count($this->steps[$this->stepsPreset]) - 1) {
 				return -1;
 			}
 
 			return $this->steps[$this->stepsPreset][$next_index];
 		}
 
 		/**
 		 * Returns step name, that was processed before this step
 		 *
 		 * @return string
 		 */
 		function GetPreviousStep()
 		{
 			$next_index = $this->GetStepNumber() - 1;
 			if ($next_index < 0) {
 				$next_index = 0;
 			}
 
 			return $this->steps[$this->stepsPreset][$next_index];
 		}
 
 		/**
 		 * Prints all steps from active steps preset and highlights current step
 		 *
 		 * @param string $active_tpl
 		 * @param string $passive_tpl
 		 * @return string
 		 */
 		function PrintSteps($active_tpl, $passive_tpl)
 		{
 			$ret = '';
 			$step_titles = $this->_getStepTitles($this->steps[$this->stepsPreset]);
 
 			foreach ($this->steps[$this->stepsPreset] as $step_name) {
 				$template = $step_name == $this->currentStep ? $active_tpl : $passive_tpl;
 				$ret .= sprintf($template, $step_titles[$step_name]);
 			}
 
 			return $ret;
 		}
 
 		/**
 		 * Installation error handler for sql errors
 		 *
 		 * @param int $code
 		 * @param string $msg
 		 * @param string $sql
 		 * @return bool
 		 * @access private
 		 */
 		function DBErrorHandler($code, $msg, $sql)
 		{
 			$this->errorMessage = 'Query: <br />'.htmlspecialchars($sql).'<br />execution result is error:<br />['.$code.'] '.$msg;
 			return true;
 		}
 
 		/**
 		 * Installation error handler
 		 *
 		 * @param int $errno
 		 * @param string $errstr
 		 * @param string $errfile
 		 * @param int $errline
 		 * @param Array $errcontext
 		 */
 		function ErrorHandler($errno, $errstr, $errfile = '', $errline = '', $errcontext = '')
 		{
 			if ($errno == E_USER_ERROR) {
 				// only react on user fatal errors
 				$this->Done($errstr);
 			}
 		}
 
 		/**
 		 * Checks, that given button should be visible on current installation step
 		 *
 		 * @param string $name
 		 * @return bool
 		 */
 		function buttonVisible($name)
 		{
 			$button_visibility = Array (
 				'continue' => $this->GetNextStep() != -1 || ($this->stepsPreset == 'already_installed'),
 				'refresh' => in_array($this->currentStep, Array ('check_paths', 'security')),
 				'back' => in_array($this->currentStep, Array (/*'select_license',*/ 'download_license', 'select_domain')),
 			);
 
 			if ($name == 'any') {
 				foreach ($button_visibility as $button_name => $button_visible) {
 					if ($button_visible) {
 						return true;
 					}
 				}
 
 				return false;
 			}
 
 			return array_key_exists($name, $button_visibility) ? $button_visibility[$name] : true;
 		}
 	}