示例#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)
    }
示例#2
0
 public function Step3()
 {
     Kit::ClassLoader('install');
     set_time_limit(0);
     $fault = false;
     $fault_string = '';
     foreach ($_POST as $key => $post) {
         // $key should be like 1-2, 1-3 etc
         // Split $key on - character.
         $parts = explode('-', $key);
         if (count($parts) == 2) {
             $step_num = 'Step' . $parts[0];
             include_once 'install/database/' . $parts[0] . '.php';
             $response = $_SESSION[$step_num]->ValidateQuestion($parts[1], $post);
             if (!$response == true) {
                 // The upgrade routine for this step wasn't happy.
                 $fault = true;
                 $fault_string .= $response . "<br />\n";
             }
         }
     }
     if ($fault) {
         throw new Exception($fault_string);
     }
     $doBackup = Kit::GetParam('doBackup', $_POST, _CHECKBOX);
     if ($doBackup == 0) {
         throw new Exception(__('You MUST have a valid database backup to continue. Please take and verify a backup and upgrade again.'));
     }
     $sql_file = '';
     $sql = '';
     $i = 0;
     // Now loop over the entire upgrade. Run the SQLs and PHP interleaved.
     try {
         $dbh = PDOConnect::init();
         //$dbh->beginTransaction();
         for ($i = $_SESSION['upgradeFrom'] + 1; $i <= $_SESSION['upgradeTo']; $i++) {
             if (file_exists('install/database/' . $i . '.sql')) {
                 $delimiter = ';';
                 $sql_file = @file_get_contents('install/database/' . $i . '.sql');
                 $sql_file = Install::remove_remarks($sql_file);
                 $sql_file = Install::split_sql_file($sql_file, $delimiter);
                 foreach ($sql_file as $sql) {
                     $dbh->exec($sql);
                 }
             }
             if (file_exists('install/database/' . $i . '.php')) {
                 $stepName = 'Step' . $i;
                 if (!$_SESSION[$stepName]->Boot()) {
                     throw new Exception(__('Failed with %s', $stepName));
                 }
             }
         }
         //$dbh->commit();
     } catch (Exception $e) {
         //$dbh->rollBack();
         throw new Exception(sprintf(__('An error occurred running the upgrade. Please take a screen shot of this page and seek help. Statement number: %d. Error Message = [%s]. File = [%s]. SQL = [%s].'), $i, $e->getMessage(), $sql_file, $sql));
     }
     // Install files
     Media::installAllModuleFiles();
     // Delete install
     if (!unlink('install.php')) {
         $formFields[] = FormManager::AddMessage(__("Unable to delete install.php. Please ensure the webserver has permission to unlink this file and retry"));
     }
     $formFields[] = FormManager::AddMessage(__('The upgrade was a success!'));
     // Return a rendered form
     Theme::Set('form_fields', $formFields);
     return Theme::RenderReturn('form_render');
 }