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;
/**
* Path to config.php
*
* @var string
*/
var $INIFile = '';
/**
* XML file containing steps information
*
* @var string
*/
var $StepDBFile = '';
/**
* Parsed data from config.php
*
* @var Array
*/
var $systemConfig = Array ();
/**
* 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', 'root_password', 'choose_modules', 'finish'),
'already_installed' => Array ('installed_select', 'finish'),
);
/**
* Steps, on which kApplication should not be initialized, because of missing correct db table structure
*
* @var Array
*/
var $skipApplicationSteps = Array ('check_paths', 'db_config');
/**
* Folders that should be writeable to continue installation
*
* @var Array
*/
var $writeableFolders = Array ('/', '/system');
/**
* Contains last error message text
*
* @var string
*/
var $errorMessage = '';
/**
* Base path for includes in templates
*
* @var string
*/
var $baseURL = '';
function Init()
{
$this->INIFile = FULL_PATH.'/config.php';
$this->StepDBFile = FULL_PATH.'/'.REL_PATH.'/install/steps_db.xml';
set_error_handler( Array(&$this, 'ErrorHandler') );
if (file_exists($this->INIFile)) {
// if config.php found, then check his write permission too
$this->writeableFolders[] = '/config.php';
}
$this->currentStep = $this->GetVar('step');
if (!$this->stepsPreset) {
$this->stepsPreset = 'fresh_install';
}
if (!$this->currentStep) {
// first step of current preset
reset($this->steps[$this->stepsPreset]);
$this->currentStep = current($this->steps[$this->stepsPreset]);
}
$this->systemConfig = $this->ParseConfig(true);
$this->systemConfig['Misc']['WriteablePath'] = '/system'; // for development purposes
$this->InitStep();
$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/';
}
function GetVar($name)
{
return isset($_REQUEST[$name]) ? $_REQUEST[$name] : false;
}
/**
* Performs needed intialization of data, that step requires
*
*/
function InitStep()
{
$go_next = $this->GetVar('step') === false ? false : true;
$this->InitApplication();
switch ($this->currentStep) {
case 'check_paths':
foreach ($this->writeableFolders as $folder_path) {
$file_path = FULL_PATH.$folder_path;
if (!is_writable($file_path)) {
$go_next = false;
$this->errorMessage = 'Install cannot write to specified folder in the root directory of your installation';
break;
}
}
break;
case 'db_config':
$section_name = 'Database';
$fields = Array ('DBType', 'DBHost', 'DBName', 'DBUser', 'DBUserPassword', 'TablePrefix');
if (!isset($this->systemConfig[$section_name])) {
$this->systemConfig[$section_name] = Array ();
}
// set fields
foreach ($fields as $field_name) {
$submit_value = $this->GetVar($field_name);
if ($submit_value !== false) {
$this->systemConfig[$section_name][$field_name] = $submit_value;
}
elseif (!isset($this->systemConfig[$section_name][$field_name])) {
$this->systemConfig[$section_name][$field_name] = '';
}
}
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':
// 1. check if required fields are filled
$section_name = 'Database';
$required_fields = Array ('DBType', 'DBHost', 'DBName', 'DBUser');
foreach ($required_fields as $required_field) {
if (!$this->systemConfig[$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(true);
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 (strlen($password) < 4) {
$this->errorMessage = 'Root Password must be at least 4 characters';
}
$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':
// store db configuration
$this->SaveConfig();
// import base data into database
$this->RunSQL('/core/install/install_schema.sql');
$this->RunSQL('/core/install/install_data.sql');
break;
case 'root_password':
// update root password in database
$password = md5( md5($this->Application->GetVar('root_password')) . 'b38');
$sql = 'UPDATE '.TABLE_PREFIX.'ConfigurationValues
SET VariableValue = '.$this->Conn->qstr($password).'
WHERE VariableName = "RootPass"';
$this->Conn->Query($sql);
// import base language for core (english)
$this->ImportLanguage('/core/install/english');
break;
case 'choose_modules':
$modules = $this->Application->GetVar('modules');
foreach ($modules as $module) {
$install_file = MODULES_PATH.'/'.$module.'/install.php';
if (file_exists($install_file)) {
include_once($install_file);
}
}
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, '', 'admin/index.php');
}
}
function InitApplication()
{
if (!in_array($this->currentStep, $this->skipApplicationSteps) && !isset($this->Application)) {
// step is allowed for application usage & it was not initialized in previous step
global $debugger;
include_once(FULL_PATH.'/core/kernel/startup.php');
$this->Application =& kApplication::Instance();
$this->Application->Init();
$this->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();
echo 'SID: ['.$this->Application->GetSID().']
';
}
exit;
}
function GetModuleVersion($module_name)
{
return '0.1.1';
}
function ConnectToDatabase()
{
include_once FULL_PATH.'/core/kernel/db/db_connection.php';
$this->Conn = new kDBConnection($this->systemConfig['Database']['DBType'], Array(&$this, 'DBErrorHandler'));
$this->Conn->Connect($this->systemConfig['Database']['DBHost'], $this->systemConfig['Database']['DBUser'], $this->systemConfig['Database']['DBUserPassword'], $this->systemConfig['Database']['DBName']);
return $this->Conn->errorCode == 0;
}
function CheckDatabase($check_installed = false)
{
// 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 (strlen($this->systemConfig['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
$already_installed = $this->TableExists('ConfigurationAdmin,Category,Permissions');
if ($check_installed && $already_installed) {
// already installed at that location
$this->errorMessage = 'An In-Portal Database already exists at this location'; // : 'An In-Portal Database was not found at this location';
return false;
}
}
else {
// user has insufficient permissions in database specified
$db_error = 'Permission Error: ('.$this->Conn->getErrorCode().') '.$this->Conn->getErrorMsg();
return false;
}
}
else {
// was error while connecting
$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->systemConfig['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;
}
function RunSQL($filename, $replace_from = null, $replace_to = null)
{
if (!file_exists(FULL_PATH.$filename)) {
return ;
}
$sqls = file_get_contents(FULL_PATH.$filename);
$table_prefix = $this->systemConfig['Database']['TablePrefix'];
// add prefix to all tables
if (strlen($table_prefix) > 0) {
$replacements = Array ('CREATE TABLE ', 'INSERT INTO ', 'UPDATE ', 'ALTER TABLE ');
foreach ($replacements as $replacement) {
$sqls = str_replace($replacement, $replacement.$table_prefix, $sqls);
}
$sqls = str_replace('DROP TABLE ', 'DROP TABLE IF EXISTS '.$table_prefix, $sqls);
}
if (isset($replace_from) && isset($replace_to)) {
// replace something additionally, e.g. module root category
$sqls = str_replace($replace_from, $replace_to, $sqls);
}
$sqls = explode(";\n", $sqls);
foreach ($sqls as $sql) {
$sql = trim($sql);
if (!$sql || substr($sql, 0, 1) == '#') {
continue; // usually last line || comment
}
$this->Conn->Query($sql);
if ($this->Conn->getErrorCode() != 0) {
$this->errorMessage = 'Error: ('.$this->Conn->getErrorCode().') '.$this->Conn->getErrorMsg().'
Database Query:
'.htmlspecialchars($sql).''; $this->Done(); break; } } } function ImportLanguage($lang_file) { $lang_file = FULL_PATH.$lang_file.'.lang'; if (!file_exists($lang_file)) { return ; } $lang_xml =& $this->Application->recallObjectP('LangXML', null, Array(), false); // false - don't use temp tables $lang_xml->Parse($lang_file, '|0|1|2|', ''); } /** * Returns content to show for current step * * @return string */ function GetStepBody() { ob_start(); switch ($this->currentStep) { case 'check_paths': ?>
', print_r($s, true). ''; }*/ ?>