Index: trunk/core/install.php =================================================================== diff -u -N -r6694 -r6707 --- trunk/core/install.php (.../install.php) (revision 6694) +++ trunk/core/install.php (.../install.php) (revision 6707) @@ -1,48 +1,48 @@ 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 * @@ -55,14 +55,14 @@ * @var string */ var $currentStep = ''; - + /** * Steps list (preset) to use for current installation * * @var string */ var $stepsPreset = ''; - + /** * Installtion steps to be done * @@ -71,84 +71,84 @@ var $steps = Array( 'fresh_install' => Array ('check_paths', 'db_config', 'root_password', 'choose_modules', 'finish'), 'already_installed' => Array ('install_setup'), - + 'upgrade' => Array ('install_setup',/* ..., */ 'finish'), 'db_reconfig' => Array ('install_setup',/* ..., */ 'finish'), 'fix_paths' => Array ('install_setup',/* ..., */ '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', 'install_setup'); // remove install_setup when application will work separately from install - + /** * 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'; - + $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->INIFile)) { // if config.php found, then check his write permission too $this->writeableFolders[] = '/config.php'; } - + $this->systemConfig = $this->ParseConfig(true); $this->systemConfig['Misc']['WriteablePath'] = '/system'; // for development purposes - + $this->currentStep = $this->GetVar('step'); - + $this->SelectPreset(); - + if (!$this->currentStep) { // first step of current preset reset($this->steps[$this->stepsPreset]); $this->currentStep = current($this->steps[$this->stepsPreset]); } - + $this->InitStep(); } - + /** * Selects preset to proceed based on various criteria * */ function SelectPreset() { $preset = $this->GetVar('preset'); - + if ($preset === false) { $preset = 'fresh_install'; // default preset - + if (file_exists($this->INIFile)) { // only at installation first step $status = $this->CheckDatabase(false); @@ -157,23 +157,23 @@ } } } - + $this->stepsPreset = $preset; } - + function GetVar($name) { return isset($_REQUEST[$name]) ? $_REQUEST[$name] : false; } - + /** * Performs needed intialization of data, that step requires * */ function InitStep() { $this->InitApplication(); - + switch ($this->currentStep) { case 'check_paths': foreach ($this->writeableFolders as $folder_path) { @@ -184,19 +184,19 @@ } } 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; } @@ -205,15 +205,15 @@ } } break; - + case 'choose_modules': // if no modules found, then proceed to next step $modules = $this->ScanModules(); if (!$modules) { $this->currentStep = $this->GetNextStep(); } break; - + case 'install_setup': if ($this->stepsPreset == 'already_installed') { // if preset was not choosen, then raise error @@ -223,7 +223,7 @@ // if preset was choosen, then check root password entered $user_name = $this->GetVar('user_name'); $user_password = $this->GetVar('user_password'); - + if ($user_name == 'root') { $sql = 'SELECT VariableValue FROM '.$this->systemConfig['Database']['TablePrefix'].' @@ -240,10 +240,10 @@ } break; } - + $this->PerformValidation(); // returns validation status (just in case) } - + /** * Validates data entered by user * @@ -255,9 +255,9 @@ // 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 @@ -271,30 +271,30 @@ } } if (!$status) break; - + // 2. check permissions, that use have in this database $status = $this->CheckDatabase(); 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 * @@ -305,29 +305,29 @@ // 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) { @@ -338,36 +338,36 @@ } 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 + // 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 * @@ -377,32 +377,32 @@ 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; } - + /** * Checks if core is already installed * @@ -412,14 +412,14 @@ { return $this->TableExists('ConfigurationAdmin'); //,Category,Permissions'); } - + 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 (strlen($this->systemConfig['Database']['TablePrefix']) > 7) { $this->errorMessage = 'Table prefix should not be longer than 7 characters'; return false; @@ -466,10 +466,10 @@ $this->errorMessage = 'Connection Error: ('.$this->Conn->getErrorCode().') '.$this->Conn->getErrorMsg(); return false; } - + return true; } - + /** * Checks if all passed tables exists * @@ -479,7 +479,7 @@ function TableExists($tables) { $prefix = $this->systemConfig['Database']['TablePrefix']; - + $all_found = true; $tables = explode(',', $tables); foreach ($tables as $table_name) { @@ -489,20 +489,20 @@ 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 '); @@ -511,14 +511,14 @@ } $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) == '#') { @@ -532,18 +532,18 @@ } } } - + 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 =& $this->Application->recallObjectP('LangXML', null, Array(), false); // false - don't use temp tables $lang_xml->Parse($lang_file, '|0|1|2|', ''); } - + /** * Returns modules list found in modules folder * @@ -552,7 +552,7 @@ function ScanModules() { static $modules = null; - + if (!isset($modules)) { $modules = Array(); $fh = opendir(MODULES_PATH); @@ -570,10 +570,10 @@ } } } - + return $modules; } - + /** * Returns content to show for current step * @@ -587,10 +587,10 @@ 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 * @@ -599,17 +599,17 @@ 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'])) { @@ -623,13 +623,13 @@ break; } } - + $info['queried'] = true; } - + return $info; } - + /** * Returns particular information abou current step * @@ -639,14 +639,14 @@ 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 * @@ -657,22 +657,22 @@ 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 @@ -683,7 +683,7 @@ { return array_search($this->currentStep, $this->steps[$this->stepsPreset]) + 1; } - + /** * Returns step name to process next * @@ -695,10 +695,10 @@ 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 * @@ -710,10 +710,10 @@ 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 * @@ -725,15 +725,15 @@ { $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; } - + function ParseConfig($parse_section = false) { if (!file_exists($this->INIFile)) { @@ -754,7 +754,7 @@ if ($ln == 1 && $line != '<'.'?'.'php die() ?'.">\n") { $resave = true; } - + $ln++; $line = trim($line); $line = eregi_replace(';[.]*','',$line); @@ -779,7 +779,7 @@ } } } - + if ($resave) { $fp = fopen($this->INIFile, 'w'); reset($contents); @@ -792,12 +792,12 @@ return $retval; } - + function SaveConfig() { $fp = fopen($this->INIFile, 'w'); fwrite($fp,'<'.'?'.'php die() ?'.">\n\n"); - + foreach ($this->systemConfig as $section_name => $section_data) { fwrite($fp, '['.$section_name."]\n"); foreach ($section_data as $key => $value) { @@ -807,7 +807,7 @@ } fclose($fp); } - + /** * Installation error handler for sql errors * @@ -822,7 +822,7 @@ $this->errorMessage = 'Query:
'.htmlspecialchars($sql).'
execution result is error:
['.$code.'] '.$msg; return true; } - + /** * Installation error handler * @@ -840,11 +840,11 @@ } } } - + /*function print_pre($s) { echo '
', print_r($s, true). '
'; }*/ - - + + ?> \ No newline at end of file