Ejemplo n.º 1
0
/**
 * Do step of gathering of the database configuration
 *
 * @param array $params
 *
 * @return bool
 */
function module_cfg_install_db(&$params)
{
    global $error, $lcSettings;
    global $report_uid, $reportFName;
    global $checkRequirements;
    global $requirements;
    global $isDBConnected;
    $pdoErrorMsg = '';
    $output = '';
    $clrNumber = 1;
    $requirements = doCheckRequirements();
    // Remove report file if it was created on the previous step
    if (@file_exists($reportFName)) {
        @unlink($reportFName);
        $report_uid = '';
    }
    $paramFields = array('mysqlhost' => array('title' => xtr('MySQL server name'), 'description' => xtr('Hostname or IP address of your MySQL server.'), 'required' => true), 'mysqlbase' => array('title' => xtr('MySQL database name'), 'description' => xtr('The name of the existing database to use (if the database does not exist on the server, you should create it to continue the installation).'), 'required' => true), 'mysqluser' => array('title' => xtr('MySQL username'), 'description' => xtr('MySQL username. The user must have full access to the database specified above.'), 'required' => true, 'disableAutofill' => true), 'mysqlpass' => array('title' => xtr('MySQL password'), 'description' => xtr('Password for the above MySQL username.'), 'required' => false, 'type' => 'password', 'disableAutofill' => true), 'demo' => array('title' => xtr('Install sample catalog'), 'description' => xtr('Specify whether you would like to setup sample categories and products?'), 'required' => false, 'type' => 'checkbox'), 'mysqlport' => array('title' => xtr('MySQL server port'), 'description' => xtr('If your database server is listening to a non-standard port, specify its number (e.g. 3306).'), 'required' => false, 'section' => 'advanced-mysql'), 'mysqlsock' => array('title' => xtr('MySQL server socket'), 'description' => xtr('If your database server is used a non-standard socket, specify it (e.g. /tmp/mysql-5.1.34.sock).'), 'required' => false, 'section' => 'advanced-mysql'), 'mysqlprefix' => array('title' => xtr('MySQL tables prefix'), 'description' => xtr('The prefix of the shop tables in database'), 'required' => true, 'section' => 'advanced-mysql'), 'xlite_http_host' => array('title' => xtr('Web server name'), 'description' => xtr('Hostname of your web server (E.g.: www.example.com).'), 'required' => true, 'section' => 'advanced-server'), 'xlite_https_host' => array('title' => xtr('Secure web server name'), 'description' => xtr('Hostname of your secure (HTTPS-enabled) web server (E.g.: secure.example.com). If omitted, it is assumed to be the same as the web server name.'), 'required' => false, 'section' => 'advanced-server'), 'xlite_web_dir' => array('title' => xtr('X-Cart web directory'), 'description' => xtr('Path to X-Cart files within the web space of your web server (E.g.: /shop).'), 'required' => false, 'section' => 'advanced-server'), 'date_default_timezone' => array('title' => xtr('Default time zone'), 'description' => xtr('By default, dates in this site will be displayed in the chosen time zone.'), 'select_data' => getTimeZones(true), 'required' => false, 'type' => 'select', 'section' => 'advanced-server'));
    // Initialize default values for parameters
    applySuggestedDefValues($paramFields);
    $messageText = '';
    $displayConfigForm = false;
    foreach ($paramFields as $fieldName => $fieldData) {
        // Prepare first step data if we came from the second step back
        if (isset($_POST['go_back']) && $_POST['go_back'] === '1') {
            if (empty($fieldData['step']) && isset($params[$fieldName])) {
                $paramFields[$fieldName]['def_value'] = $params[$fieldName];
                unset($params[$fieldName]);
            }
        }
        // Unset parameter if its empty
        if (isset($params[$fieldName])) {
            $params[$fieldName] = trim($params[$fieldName]);
            if (empty($params[$fieldName])) {
                unset($params[$fieldName]);
            }
        }
        // Check if all required parameters presented
        if (!isset($params[$fieldName])) {
            $displayConfigForm = $displayConfigForm || $fieldData['required'];
        }
    }
    // Display form to enter host data and database settings
    if ($displayConfigForm) {
        ob_start();
        foreach ($paramFields as $fieldName => $fieldData) {
            if (isset($fieldData['step']) && $fieldData['step'] != 1) {
                continue;
            }
            $fieldData['value'] = isset($params[$fieldName]) ? $params[$fieldName] : $fieldData['def_value'];
            displayFormElement($fieldName, $fieldData, $clrNumber);
            $clrNumber = $clrNumber == 2 ? 1 : 2;
        }
        $output = ob_get_contents();
        ob_end_clean();
        ?>

<input type="hidden" name="cfg_install_db_step" value="1" />

<?php 
        // Display second step: review parameters and enter additional data
    } else {
        // Now checking if database named $params[mysqlbase] already exists
        $checkError = false;
        $checkWarning = false;
        if (strstr($params['xlite_http_host'], ':')) {
            list($_host, $_port) = explode(':', $params['xlite_http_host']);
        } else {
            $_host = $params['xlite_http_host'];
        }
        if (!$_host) {
            fatal_error(xtr('The web server name and/or web drectory is invalid (:host). Press \'BACK\' button and review web server settings you provided', array(':host' => $_host)), 'params', 'wrong web server or webdir');
            $checkError = true;
            // Check if database settings provided are valid
        } else {
            $connection = dbConnect($params, $pdoErrorMsg);
            if (!$connection && preg_match('/SQLSTATE.*\\[1049\\].*' . preg_quote($params['mysqlbase']) . '/', $pdoErrorMsg)) {
                // The specified database not found, try to create
                x_install_log('The specified database "' . $params['mysqlbase'] . '" not found');
                $paramsNoDb = $params;
                unset($paramsNoDb['mysqlbase']);
                $pdoErrorMsg = null;
                $connection = dbConnect($paramsNoDb, $pdoErrorMsg);
                if ($connection) {
                    $pdoErrorMsg = null;
                    dbExecute('CREATE DATABASE `' . $params['mysqlbase'] . '`;', $pdoErrorMsg);
                    if (empty($pdoErrorMsg)) {
                        x_install_log('The database "' . $params['mysqlbase'] . '" successfully created.');
                        // Reconnect...
                        $connection = dbConnect($params, $pdoErrorMsg);
                    } else {
                        $connection = null;
                        x_install_log('The database "' . $params['mysqlbase'] . '" cannot be created: ' . $pdoErrorMsg);
                        fatal_error_extended(xtr('The database <i>:dbname</i> cannot be created automatically:pdoerr.<br /> Please go back, create it manually and then proceed with the installation process again.', array(':dbname' => $params['mysqlbase'], ':pdoerr' => ': ' . $pdoErrorMsg)), 'pdo', @$pdoErrorMsg, xtr('kb_note_mysql_issue'));
                        $checkError = true;
                    }
                }
            }
            if ($connection) {
                $isDBConnected = true;
                $requirements = doCheckRequirements();
                // Check MySQL version
                $mysqlVersionErr = $currentMysqlVersion = '';
                if (!checkMysqlVersion($mysqlVersionErr, $currentMysqlVersion, true)) {
                    fatal_error_extended($mysqlVersionErr . (!empty($currentMysqlVersion) ? '<br />(current version is ' . $currentMysqlVersion . ')' : ''), 'reqs', 'mysql version', xtr('kb_note_mysql_issue'));
                    $checkError = true;
                }
                // Check if config.php file is writeable
                if (!$checkError && !@is_writable(LC_DIR_CONFIG . constant('LC_CONFIG_FILE'))) {
                    fatal_error(xtr('Cannot open file \':filename\' for writing. To install the software, please correct the problem and start the installation again...', array(':filename' => constant('LC_CONFIG_FILE'))), 'file', 'config write failed');
                    $checkError = true;
                } elseif (!$checkError) {
                    // Check if X-Cart tables is already exists
                    $mystring = '';
                    $first = true;
                    $res = dbFetchAll('SHOW TABLES LIKE \'' . get_db_tables_prefix() . '%\'');
                    if (is_array($res)) {
                        foreach ($res as $row) {
                            if (in_array(get_db_tables_prefix() . 'products', $row)) {
                                warning_error(xtr('Installation Wizard has detected X-Cart tables'), 're-install');
                                $checkWarning = true;
                                break;
                            }
                        }
                    }
                }
            } elseif (!$checkError) {
                preg_match('/SQLSTATE.*\\[(\\d+)\\].*/', $pdoErrorMsg, $match);
                $code = intval(!empty($match[1]) ? $match[1] : 0);
                if ('1045' == $code) {
                    fatal_error_extended(xtr('pdo-error-1045', array(':pdoerr' => !empty($pdoErrorMsg) ? $pdoErrorMsg : '')), 'pdo', @$pdoErrorMsg, xtr('kb_note_mysql_issue'));
                } elseif ('1044' == $code) {
                    fatal_error_extended(xtr('pdo-error-1044', array(':dbuser' => $params['mysqluser'], ':dbname' => $params['mysqlbase'], ':pdoerr' => !empty($pdoErrorMsg) ? $pdoErrorMsg : '')), 'pdo', @$pdoErrorMsg, xtr('kb_note_mysql_issue'));
                } elseif ('2005' == $code) {
                    fatal_error_extended(xtr('pdo-error-2005', array(':pdoerr' => !empty($pdoErrorMsg) ? $pdoErrorMsg : '')), 'pdo', @$pdoErrorMsg, xtr('kb_note_mysql_issue'));
                } else {
                    fatal_error_extended(xtr('pdo-error-common', array(':pdoerr' => !empty($pdoErrorMsg) ? $pdoErrorMsg : '')), 'pdo', @$pdoErrorMsg, xtr('kb_note_mysql_issue'));
                }
                $checkError = true;
            }
        }
        if (!$checkError && !$checkWarning) {
            global $autoPost;
            $autoPost = true;
        } else {
            $output = '';
        }
        $error = $checkError;
    }
    ?>

<?php 
    echo $messageText;
    ?>

<table width="100%" border="0" cellpadding="10">

<?php 
    echo $output;
    ?>

</table>

<?php 
    return $displayConfigForm;
}
Ejemplo n.º 2
0
/**
 * Do step of gathering of the database configuration
 *
 * @param array $params
 *
 * @return bool
 */
function module_cfg_install_db(&$params)
{
    global $error, $lcSettings;
    global $report_uid, $reportFName;
    global $checkRequirements;
    $pdoErrorMsg = '';
    $output = '';
    $clrNumber = 1;
    // Remove report file if it was created on the previous step
    if (@file_exists($reportFName)) {
        @unlink($reportFName);
        $report_uid = '';
    }
    $paramFields = array('xlite_http_host' => array('title' => xtr('Web server name'), 'description' => xtr('Hostname of your web server (E.g.: www.example.com).'), 'def_value' => $_SERVER['HTTP_HOST'], 'required' => true), 'xlite_https_host' => array('title' => xtr('Secure web server name'), 'description' => xtr('Hostname of your secure (HTTPS-enabled) web server (E.g.: secure.example.com). If omitted, it is assumed to be the same as the web server name.'), 'def_value' => $_SERVER['HTTP_HOST'], 'required' => true), 'xlite_web_dir' => array('title' => xtr('LiteCommerce web directory'), 'description' => xtr('Path to LiteCommerce files within the web space of your web server (E.g.: /shop).'), 'def_value' => preg_replace('/\\/install(\\.php)*/Ss', '', $_SERVER['REQUEST_URI']), 'required' => false), 'mysqlhost' => array('title' => xtr('MySQL server name'), 'description' => xtr('Hostname or IP address of your MySQL server.'), 'def_value' => 'localhost', 'required' => true), 'mysqlport' => array('title' => xtr('MySQL server port'), 'description' => xtr('If your database server is listening to a non-standard port, specify its number (e.g. 3306).'), 'def_value' => '', 'required' => false), 'mysqlsock' => array('title' => xtr('MySQL server socket'), 'description' => xtr('If your database server is used a non-standard socket, specify it (e.g. /tmp/mysql-5.1.34.sock).'), 'def_value' => '', 'required' => false), 'mysqlbase' => array('title' => xtr('MySQL database name'), 'description' => xtr('The name of the existing database to use (if the database does not exist on the server, you should create it to continue the installation).'), 'def_value' => '', 'required' => true), 'mysqluser' => array('title' => xtr('MySQL username'), 'description' => xtr('MySQL username. The user must have full access to the database specified above.'), 'def_value' => '', 'required' => true), 'mysqlpass' => array('title' => xtr('MySQL password'), 'description' => xtr('Password for the above MySQL username.'), 'def_value' => '', 'required' => false, 'type' => 'password'), 'demo' => array('title' => xtr('Install sample catalog'), 'description' => xtr('Specify whether you would like to setup sample categories and products?'), 'def_value' => '1', 'required' => false, 'type' => 'checkbox'));
    $messageText = '';
    $displayConfigForm = false;
    foreach ($paramFields as $fieldName => $fieldData) {
        // Prepare first step data if we came from the second step back
        if (isset($_POST['go_back']) && $_POST['go_back'] === '1') {
            if (empty($fieldData['step']) && isset($params[$fieldName])) {
                $paramFields[$fieldName]['def_value'] = $params[$fieldName];
                unset($params[$fieldName]);
            }
        }
        // Unset parameter if its empty
        if (isset($params[$fieldName]) && strlen(trim($params[$fieldName])) == 0) {
            unset($params[$fieldName]);
        }
        // Check if all required parameters presented
        if (!isset($params[$fieldName])) {
            $displayConfigForm = $displayConfigForm || $fieldData['required'];
        }
    }
    // Display form to enter host data and database settings
    if ($displayConfigForm) {
        ob_start();
        foreach ($paramFields as $fieldName => $fieldData) {
            if (isset($fieldData['step']) && $fieldData['step'] != 1) {
                continue;
            }
            $fieldData['value'] = isset($params[$fieldName]) ? $params[$fieldName] : $fieldData['def_value'];
            displayFormElement($fieldName, $fieldData, $clrNumber);
            $clrNumber = $clrNumber == 2 ? 1 : 2;
        }
        $output = ob_get_contents();
        ob_end_clean();
        ?>

<input type="hidden" name="cfg_install_db_step" value="1" />

<?php 
        // Display second step: review parameters and enter additional data
    } else {
        // Now checking if database named $params[mysqlbase] already exists
        $checkError = false;
        $checkWarning = false;
        if (strstr($params['xlite_http_host'], ':')) {
            list($_host, $_port) = explode(':', $params['xlite_http_host']);
        } else {
            $_host = $params['xlite_http_host'];
        }
        if (!$_host) {
            fatal_error(xtr('The web server name and/or web drectory is invalid (:host). Press \'BACK\' button and review web server settings you provided', array(':host' => $_host)));
            $checkError = true;
            // Check if database settings provided are valid
        } else {
            $connection = dbConnect($params, $pdoErrorMsg);
            if ($connection) {
                // Check MySQL version
                $mysqlVersionErr = $currentMysqlVersion = '';
                if (!checkMysqlVersion($mysqlVersionErr, $currentMysqlVersion, true)) {
                    fatal_error($mysqlVersionErr . (!empty($currentMysqlVersion) ? '<br />(current version is ' . $currentMysqlVersion . ')' : ''));
                    $checkError = true;
                }
                // Check if config.php file is writeable
                if (!$checkError && !@is_writable(LC_DIR_CONFIG . constant('LC_CONFIG_FILE'))) {
                    fatal_error(xtr('Cannot open file \':filename\' for writing. To install the software, please correct the problem and start the installation again...', array(':filename' => constant('LC_CONFIG_FILE'))));
                    $checkError = true;
                } elseif (!$checkError) {
                    // Check if LiteCommerce tables is already exists
                    $mystring = '';
                    $first = true;
                    $res = dbFetchAll('SHOW TABLES LIKE \'xlite_%\'');
                    if (is_array($res)) {
                        foreach ($res as $row) {
                            if (in_array('xlite_products', $row)) {
                                warning_error(xtr('Installation Wizard has detected LiteCommerce tables'));
                                $checkWarning = true;
                                break;
                            }
                        }
                    }
                }
            } else {
                fatal_error(xtr('Can\'t connect to MySQL server specified:pdoerr<br /> Press \'BACK\' button and review MySQL server settings you provided.', array(':pdoerr' => !empty($pdoErrorMsg) ? ': ' . $pdoErrorMsg : '')));
                $checkError = true;
            }
        }
        if (!$checkError && !$checkWarning) {
            global $autoPost;
            $autoPost = true;
        } else {
            $output = '';
        }
        $error = $checkError;
    }
    ?>

<?php 
    echo $messageText;
    ?>

<table width="100%" border="0" cellpadding="10">

<?php 
    echo $output;
    ?>

</table>

<?php 
    return $displayConfigForm;
}