Example #1
0
/**
 * return error message as string, or true indicating success
 * requires setup to be written first.
 */
function setup_database()
{
    $conn = DB_Helper::getInstance(false);
    $db_exists = checkDatabaseExists($conn, $_POST['db_name']);
    if (!$db_exists) {
        if (@$_POST['create_db'] == 'yes') {
            try {
                $conn->query("CREATE DATABASE {{{$_POST['db_name']}}}");
            } catch (DbException $e) {
                throw new RuntimeException(getErrorMessage('create_db', $e->getMessage()));
            }
        } else {
            throw new RuntimeException('The provided database name could not be found. Review your information or specify that the database should be created in the form below.');
        }
    }
    // create the new user, if needed
    if (@$_POST['alternate_user'] == 'yes') {
        $user_list = getUserList($conn);
        if ($user_list) {
            $user_exists = in_array(strtolower(@$_POST['eventum_user']), $user_list);
            if (@$_POST['create_user'] == 'yes') {
                if (!$user_exists) {
                    $stmt = "GRANT SELECT, UPDATE, DELETE, INSERT, ALTER, DROP, CREATE, INDEX ON {{{$_POST['db_name']}}}.* TO ?@'%' IDENTIFIED BY ?";
                    try {
                        $conn->query($stmt, array($_POST['eventum_user'], $_POST['eventum_password']));
                    } catch (DbException $e) {
                        throw new RuntimeException(getErrorMessage('create_user', $e->getMessage()));
                    }
                }
            } else {
                if (!$user_exists) {
                    throw new RuntimeException('The provided MySQL username could not be found. Review your information or specify that the username should be created in the form below.');
                }
            }
        }
    }
    // check if we can use the database
    try {
        $conn->query("USE {{{$_POST['db_name']}}}");
    } catch (DbException $e) {
        throw new RuntimeException(getErrorMessage('select_db', $e->getMessage()));
    }
    // set sql mode (sad that we rely on old bad mysql defaults)
    $conn->query("SET SQL_MODE = ''");
    // check the CREATE and DROP privileges by trying to create and drop a test table
    $table_list = getTableList($conn);
    if (!in_array('eventum_test', $table_list)) {
        try {
            $conn->query('CREATE TABLE eventum_test (test char(1))');
        } catch (DbException $e) {
            throw new RuntimeException(getErrorMessage('create_test', $e->getMessage()));
        }
    }
    try {
        $conn->query('DROP TABLE eventum_test');
    } catch (DbException $e) {
        throw new RuntimeException(getErrorMessage('drop_test', $e->getMessage()));
    }
    // if requested. drop tables first
    if (@$_POST['drop_tables'] == 'yes') {
        $queries = get_queries(APP_PATH . '/upgrade/drop.sql');
        foreach ($queries as $stmt) {
            try {
                $conn->query($stmt);
            } catch (DbException $e) {
                throw new RuntimeException(getErrorMessage('drop_table', $e->getMessage()));
            }
        }
    }
    // setup database with upgrade script
    $buffer = array();
    try {
        $dbmigrate = new DbMigrate(APP_PATH . '/upgrade');
        $dbmigrate->setLogger(function ($e) use(&$buffer) {
            $buffer[] = $e;
        });
        $dbmigrate->patch_database();
        $e = false;
    } catch (Exception $e) {
    }
    global $tpl;
    $tpl->assign('db_result', implode("\n", $buffer));
    if ($e) {
        $upgrade_script = APP_PATH . '/upgrade/update-database.php';
        $error = array('Database setup failed on upgrade:', "<tt>{$e->getMessage()}</tt>", '', "You may want run update script <tt>{$upgrade_script}</tt> manually");
        throw new RuntimeException(implode('<br/>', $error));
    }
    // write db name now that it has been created
    $setup = array();
    $setup['database'] = $_POST['db_name'];
    // substitute the appropriate values in config.php!!!
    if (@$_POST['alternate_user'] == 'yes') {
        $setup['username'] = $_POST['eventum_user'];
        $setup['password'] = $_POST['eventum_password'];
    }
    Setup::save(array('database' => $setup));
}
Example #2
0
#!/usr/bin/php
<?php 
/**
 * Tool for helping Eventum upgrades.
 *
 * See our Wiki for documentation:
 * https://github.com/eventum/eventum/wiki/Upgrading
 */
define('INSTALL_PATH', __DIR__ . '/..');
define('CONFIG_PATH', INSTALL_PATH . '/config');
// avoid init.php redirecting us to setup if not configured yet
$setup_path = CONFIG_PATH . '/setup.php';
if (!file_exists($setup_path) || !filesize($setup_path) || !is_readable($setup_path)) {
    error_log("ERROR: Can't get setup.php in '" . CONFIG_PATH . "'");
    error_log('Did you forgot to copy config from old install? Is file readable?');
    exit(1);
}
require_once INSTALL_PATH . '/init.php';
try {
    $dbmigrate = new DbMigrate(INSTALL_PATH . '/upgrade');
    $dbmigrate->patch_database();
} catch (Exception $e) {
    echo $e->getMessage(), "\n";
    exit(1);
}