Ejemplo n.º 1
0
    public function Step3()
    {
        // Have we been told to create a new database
        $this->db_create = Kit::GetParam('db_create', _POST, _INT);
        // Check all parameters have been specified
        $this->db_admin_user = Kit::GetParam('admin_username', _POST, _PASSWORD);
        $this->db_admin_pass = Kit::GetParam('admin_password', _POST, _PASSWORD);
        $this->new_db_host = Kit::GetParam('host', _POST, _STRING);
        $this->new_db_user = Kit::GetParam('db_username', _POST, _PASSWORD);
        $this->new_db_pass = Kit::GetParam('db_password', _POST, _PASSWORD);
        $this->new_db_name = Kit::GetParam('db_name', _POST, _PASSWORD);
        $this->existing_db_host = Kit::GetParam('existing_host', _POST, _STRING);
        $this->existing_db_user = Kit::GetParam('existing_db_username', _POST, _PASSWORD);
        $this->existing_db_pass = Kit::GetParam('existing_db_password', _POST, _PASSWORD);
        $this->existing_db_name = Kit::GetParam('existing_db_name', _POST, _PASSWORD);
        // If an administrator user name / password has been specified then we should create a new DB
        if ($this->db_create == 1) {
            // Check details for a new database
            if ($this->new_db_host == '') {
                throw new Exception(__('Please provide a database host. This is usually localhost.'));
            }
            if ($this->new_db_user == '') {
                throw new Exception(__('Please provide a user for the new database.'));
            }
            if ($this->new_db_pass == '') {
                throw new Exception(__('Please provide a password for the new database.'));
            }
            if ($this->new_db_name == '') {
                throw new Exception(__('Please provide a name for the new database.'));
            }
            if ($this->db_admin_user == '') {
                throw new Exception(__('Please provide an admin user name.'));
            }
            // Try to create the new database
            // Try and connect using these details and create the new database
            try {
                $dbh = PDOConnect::connect($this->new_db_host, $this->db_admin_user, $this->db_admin_pass);
            } catch (Exception $e) {
                throw new Exception(sprintf(__('Could not connect to MySQL with the administrator details. Please check and try again. Error Message = [%s]'), $e->getMessage()));
            }
            // Try to create the new database
            try {
                $dbh = PDOConnect::init();
                $dbh->exec(sprintf('CREATE DATABASE `%s`', $this->new_db_name));
            } catch (Exception $e) {
                throw new Exception(sprintf(__('Could not create a new database with the administrator details [%s]. Please check and try again. Error Message = [%s]'), $this->db_admin_user, $e->getMessage()));
            }
            // Try to create the new user
            try {
                $dbh = PDOConnect::init();
                // Create the user and grant privileges
                if ($this->new_db_host == 'localhost') {
                    $dbh->exec(sprintf('GRANT ALL PRIVILEGES ON `%s`.* to %s@%s IDENTIFIED BY %s', $this->new_db_name, $dbh->quote($this->new_db_user), $dbh->quote($this->new_db_host), $dbh->quote($this->new_db_pass)));
                } else {
                    $dbh->exec(sprintf("GRANT ALL PRIVILEGES ON `%s`.* to %s@%% IDENTIFIED BY %s", $this->new_db_name, $dbh->quote($this->new_db_user), $dbh->quote($this->new_db_pass)));
                }
                // Flush
                $dbh->exec('FLUSH PRIVILEGES');
            } catch (Exception $e) {
                throw new Exception(sprintf(__('Could not create a new user with the administrator details. Please check and try again. Error Message = [%s]'), $e->getMessage()));
            }
            // Set our DB details
            $this->existing_db_host = $this->new_db_host;
            $this->existing_db_user = $this->new_db_user;
            $this->existing_db_pass = $this->new_db_pass;
            $this->existing_db_name = $this->new_db_name;
            // Close the connection
            PDOConnect::close();
        } else {
            // Check details for a new database
            if ($this->existing_db_host == '') {
                throw new Exception(__('Please provide a database host. This is usually localhost.'));
            }
            if ($this->existing_db_user == '') {
                throw new Exception(__('Please provide a user for the existing database.'));
            }
            if ($this->existing_db_pass == '') {
                throw new Exception(__('Please provide a password for the existing database.'));
            }
            if ($this->existing_db_name == '') {
                throw new Exception(__('Please provide a name for the existing database.'));
            }
        }
        // Try and make a connection with this database
        try {
            $dbh = PDOConnect::connect($this->existing_db_host, $this->existing_db_user, $this->existing_db_pass, $this->existing_db_name);
        } catch (Exception $e) {
            throw new Exception(sprintf(__('Could not connect to MySQL with the administrator details. Please check and try again. Error Message = [%s]'), $e->getMessage()));
        }
        // We should have a database that we can access and populate with our tables.
        $sql_files = array('structure.sql', 'data.sql');
        $sqlStatementCount = 0;
        $sql_file = '';
        $sql = '';
        try {
            $dbh = PDOConnect::init();
            foreach ($sql_files as $filename) {
                $delimiter = ';';
                $sql_file = @file_get_contents('install/master/' . $filename);
                $sql_file = Install::remove_remarks($sql_file);
                $sql_file = Install::split_sql_file($sql_file, $delimiter);
                foreach ($sql_file as $sql) {
                    $sqlStatementCount++;
                    $dbh->exec($sql);
                }
            }
        } catch (Exception $e) {
            throw new Exception(sprintf(__('An error occurred populating the database. Statement number: %d. Error Message = [%s]. File = [%s]. SQL = [%s].'), $sqlStatementCount, $e->getMessage(), $sql_file, $sql));
        }
        // Write out a new settings.php
        $fh = fopen('settings.php', 'wt');
        if (!$fh) {
            throw new Exception(__('Unable to write to settings.php. We already checked this was possible earlier, so something changed.'));
        }
        // Generate a secret key for various reasons
        $secretKey = Install::gen_secret();
        // Escape the password before we write it to disk
        $dbh = PDOConnect::init();
        $existing_db_pass = addslashes($this->existing_db_pass);
        $settings = <<<END
<?php

/*
 * Xibo - Digital Signage - http://www.xibo.org.uk
 *
 * This file is part of Xibo - and is automatically generated by the installer
 *
 * You should not need to edit this file, unless your SQL connection details have changed.
 */

defined('XIBO') or die(__("Sorry, you are not allowed to directly access this page.") . "<br />" . __("Please press the back button in your browser."));

global \$dbhost;
global \$dbuser;
global \$dbpass;
global \$dbname;

\$dbhost = '{$this->existing_db_host}';
\$dbuser = '******';
\$dbpass = '******';
\$dbname = '{$this->existing_db_name}';

define('SECRET_KEY', '{$secretKey}');

END;
        if (!fwrite($fh, $settings)) {
            throw new Exception(__('Unable to write to settings.php. We already checked this was possible earlier, so something changed.'));
        }
        fclose($fh);
        // If we get here, we want to move on to the next step.
        // This is handled by the calling function (i.e. there is no output from this call, we just reload and move on)
    }