예제 #1
0
 /**
  * check if database is online and return current version
  *
  * this should be, if user-id is not set (checking always might
  * be too performance intensive)
  */
 public function getVersion()
 {
     global $sql_obj;
     if (!is_object($sql_obj)) {
         $sql_obj = new sql_class($this->dbhost, $this->user, $this->pass, $this->dbname);
     }
     if (!$sql_obj->connect()) {
         ### can't connect db... ###
         log_message("Can't connect database");
         return NULL;
     }
     if (!$sql_obj->selectdb()) {
         ### can't select... ###
         log_message("Can't select database");
         return NULL;
     }
     ### get version ###
     $prefix = confGet('DB_TABLE_PREFIX');
     $sql_command = "select * from {$prefix}db where updated is NULL";
     if ($sql_obj->execute($sql_command)) {
         $row = $sql_obj->fetchArray();
         return $row;
     } else {
         trigger_error("Can't selection version row from db: {$sql_command}");
         return NULL;
     }
 }
예제 #2
0
/**
* proceed with installation
* - returns true on success
*/
function step_02_proceed()
{
    global $g_form_fields, $sql_obj;
    echo "<h2>Proceeding...</h2>";
    $f_db_type = $g_form_fields['db_type']['value'];
    $f_hostname = $g_form_fields['hostname']['value'];
    $f_db_name = $g_form_fields['db_name']['value'];
    $f_db_username = $g_form_fields['db_username']['value'];
    $f_db_password = $g_form_fields['db_password']['value'];
    $f_db_table_prefix = $g_form_fields['db_table_prefix']['value'];
    $f_user_admin_name = $g_form_fields['user_admin_name']['value'];
    $f_user_admin_password = $g_form_fields['user_admin_password']['value'];
    $f_continue_on_sql_errors = $g_form_fields['continue_on_sql_errors']['value'];
    require_once dirname(__FILE__) . "/../db/db_" . $f_db_type . "_class.php";
    print_testStart("checking mysql connecting to '{$f_hostname}'...");
    $sql_obj = new sql_class($f_hostname, $f_db_username, $f_db_password, $f_db_name);
    if ($sql_obj->error == false) {
        # Connection DB
        if (!$sql_obj->connect()) {
            $hint = 'This could be a problem with incorrect setup of your sql-server. <a href="http://www.streber-pm.org/1176">Read more...</a>';
            print_testResult(RESULT_FAILED, "mySQL-Error[" . __LINE__ . "]:<pre>" . $sql_obj->error . "</pre><br>{$hint}");
            return false;
        } else {
            print_testResult(RESULT_GOOD, $sql_obj->error);
        }
    } else {
        print_testResult(RESULT_FAILED, $sql_obj->error);
        return false;
    }
    print_testStart("Make sure to not overwrite existing streber-db called '{$f_db_name}'");
    ### db does NOT exists ###
    if (!$sql_obj->selectdb()) {
        print_testResult(RESULT_GOOD, $sql_obj->error);
        ### create new database ###
        print_testStart("create database");
        if (!$sql_obj->execute("CREATE DATABASE IF NOT EXISTS {$f_db_name} charset utf8 COLLATE utf8_general_ci")) {
            print_testResult(RESULT_FAILED, "<pre>" . $sql_obj->error . "</pre>");
            return false;
        } else {
            if (!$sql_obj->selectdb()) {
                print_testResult(RESULT_FAILED, $sql_obj->error);
                return false;
            } else {
                print_testResult(RESULT_GOOD, 'Database ' . $f_db_name . ' created.');
            }
        }
    } else {
        print_testResult(RESULT_PROBLEM, "DB '{$f_db_name}' already exists");
        ### check version of existing database ###
        print_testStart("checking version of existing database");
        if ($sql_obj->execute("SELECT * FROM {$f_db_table_prefix}db \r\n           \t\tWHERE `updated` IS NULL ORDER BY `version` ASC")) {
            $count = 0;
            $db_version = NULL;
            $streber_version_required = NULL;
            while ($row = $sql_obj->fetchArray()) {
                $db_version = $row['version'];
                $streber_version_required = $row['version_streber_required'];
                $count++;
            }
            /**
             * there should be excactly one row with updated == NULL. Otherwise we a have a problem
             */
            if ($count < 1) {
                /* Ugh oh. Lets see if we can get the row with the highest
                   version instead. */
                print_testResult(RESULT_PROBLEM, "Streber has detected a problem with db-version but is attempting to work around it.\n");
                if ($sql_obj->execute("SELECT * FROM {$f_db_table_prefix}db ORDER BY `version` DESC LIMIT 1")) {
                    while ($row = $sql_obj->fetchArray()) {
                        $db_version = $row['version'];
                        $streber_version_required = $row['version_streber_required'];
                        $count++;
                    }
                }
                if ($count < 1) {
                    print_testResult(RESULT_FAILED, "Streber is unable to detect your current installed version.<br/>\n" . "You can work around this by manually adding this information to the db table in your Streber database.");
                    return false;
                }
                print_testResult(RESULT_PROBLEM, "Taking best guess at currently installed version.\n");
            }
            if ($count > 1) {
                /* Doh. It appears that our user is the victim of an installer bug
                 * found in older versions of Streber (we hope). */
                print_testResult(RESULT_PROBLEM, "Streber has detected a problem with db-version but is now fixing. Upgrade history lost.");
                $sql_obj->execute("TRUNCATE TABLE {$f_db_table_prefix}db");
                $sql_obj->execute("INSERT INTO {$f_db_table_prefix}db SET version = " . $db_version . ", version_streber_required = " . $streber_version_required . ", id = 1, updated = ");
            }
            if ($db_version < confGet('STREBER_VERSION')) {
                ### update ###
                print_testResult(RESULT_PROBLEM, "version is {$db_version}. Upgrading...");
                $result = upgrade(array('db_type' => $f_db_type, 'hostname' => $f_hostname, 'db_username' => $f_db_username, 'db_password' => $f_db_password, 'db_table_prefix' => $f_db_table_prefix, 'db_name' => $f_db_name, 'db_version' => $db_version, 'continue_on_sql_errors' => $f_continue_on_sql_errors));
                return $result;
            } else {
                if ($streber_version_required > confGet('STREBER_VERSION')) {
                    print_testResult(RESULT_PROBLEM, "version is {$db_version}. It's requires Version " . confGet('STREBER_VERSION') . " of Streber. Current Version is " . confGet('STREBER_VERSION') . ". Please download and install the latest version.");
                    return false;
                } else {
                    $filename = '../' . confGet('DIR_SETTINGS') . confGet('FILE_DB_SETTINGS');
                    print_testStart("writing configuration file '{$filename}'...");
                    $write_ok = writeSettingsFile($filename, array('DB_TYPE' => $f_db_type, 'HOSTNAME' => $f_hostname, 'DB_USERNAME' => $f_db_username, 'DB_PASSWORD' => $f_db_password, 'DB_TABLE_PREFIX' => $f_db_table_prefix, 'DB_NAME' => $f_db_name, 'DB_VERSION' => confGet('STREBER_VERSION')));
                    if ($write_ok) {
                        print_testResult(RESULT_GOOD, "Current database (version {$db_version}) looks fine. Installation finished with database setting rewritten to file. Please view " . getStreberWikiLink('installation', 'Installation Guide') . " on how to fix unsolved problems.");
                    } else {
                        print_testResult(RESULT_PROBLEM, "Current database (version {$db_version}) looks fine. Installation finished with no change (unable to rewrite database setting to file). Please view " . getStreberWikiLink('installation', 'Installation Guide') . " on how to fix unsolved problems.");
                    }
                    return true;
                }
            }
            print_testResult(RESULT_PROBLEM, "Installation aborted due to unknown reason.");
            return false;
        } else {
            print_testResult(RESULT_GOOD, "Could not query streber-db version. Assuming fresh installation");
        }
    }
    /**
     * fresh installation
     */
    ### creating database-structure ###
    print_testStart("creating table structure...");
    $filename = "./create_structure_v" . confGet('DB_CREATE_DUMP_VERSION') . ".sql";
    $upgradeFromVersion = confGet('DB_CREATE_DUMP_VERSION');
    if (!file_exists($filename)) {
        $filenames = glob("./create_structure_v*.sql");
        if ($filenames) {
            rsort($filenames);
            print_testResult(RESULT_PROBLEM, "Required file {$filename} is missing, trying to use {$filenames['0']} instead and then upgrade.");
            $filename = $filenames[0];
            preg_match("/create_structure_v(.*)\\.sql/", $filename, $matches);
            $upgradeFromVersion = $matches[1];
        } else {
            print_testResult(RESULT_FAILED, "Getting sql-code failed. This is an internal error. Look at " . getStreberWikiLink('installation', 'Installation Guide') . " for clues. ");
            return false;
        }
    }
    if (!parse_mysql_dump($filename, $f_db_table_prefix, $sql_obj)) {
        print_testResult(RESULT_FAILED, "SQL-Error[" . __LINE__ . "]:<br><pre>" . $sql_obj->error . "</pre>");
        return false;
    }
    print_testResult(RESULT_GOOD);
    ### upgrade
    if ($upgradeFromVersion != confGet('STREBER_VERSION')) {
        print_testStart("updating to latest version...");
        $result = upgrade(array('db_type' => $f_db_type, 'hostname' => $f_hostname, 'db_username' => $f_db_username, 'db_password' => $f_db_password, 'db_table_prefix' => $f_db_table_prefix, 'db_name' => $f_db_name, 'continue_on_sql_errors' => $f_continue_on_sql_errors, 'db_version' => $upgradeFromVersion));
        if (!$result) {
            print_testResult(RESULT_FAILED, "Upgrading failed. This is an internal error. Look at " . getStreberWikiLink('installation', 'Installation Guide') . " for clues. ");
            return false;
        }
        print_testResult(RESULT_GOOD);
    }
    ### create db-version entry ###
    print_testStart("add db-version entry");
    $db_version = confGet('STREBER_VERSION');
    $streber_version_required = confGet('STREBER_VERSION');
    $str_query = "INSERT into {$f_db_table_prefix}db (id,version,version_streber_required,created) VALUES(1,'{$db_version}','{$streber_version_required}',NOW() )";
    if (!$sql_obj->execute($str_query)) {
        print_testResult(RESULT_FAILED, "SQL-Error[" . __LINE__ . "]:<pre>" . $sql_obj->error . "</pre>");
        return false;
    } else {
        print_testResult(RESULT_GOOD);
    }
    ### create admin entry entry ###
    print_testStart("add admin-user entry 1/2");
    $password_md5 = md5($f_user_admin_password);
    $str_query = "INSERT into {$f_db_table_prefix}person\r\n                          (id,\r\n                          name,\r\n                          nickname,\r\n                          password,\r\n                          user_rights,\r\n                          can_login,\r\n                          profile,\r\n                          language,\r\n                          ip_address,\r\n                          office_email\r\n                          )\r\n                          VALUES(\r\n                          1,\r\n                          '{$f_user_admin_name}',\r\n                          '{$f_user_admin_name}',\r\n                          '{$password_md5}',\r\n                          268435455, /* all rights */\r\n                          1,\r\n                          1,\r\n                          '" . confGet('DEFAULT_LANGUAGE') . "',\r\n                          '',\r\n                          '" . $g_form_fields["site_email"]["value"] . "'\r\n                          )";
    if (!$sql_obj->execute($str_query)) {
        print_testResult(RESULT_FAILED, "SQL-Error[" . __LINE__ . "]:<br><pre>" . $sql_obj->error . "</pre>");
        return false;
    } else {
        print_testResult(RESULT_GOOD);
    }
    ### create admin entry entry ###
    print_testStart("add admin-user entry 2/2");
    $str_query = "INSERT into {$f_db_table_prefix}item\r\n                          (id,\r\n                          type,\r\n                          state,\r\n                          created_by,\r\n                          modified_by\r\n                          )\r\n                          VALUES(\r\n                          1,\r\n                          " . ITEM_PERSON . ",\r\n                          " . ITEM_STATE_OK . ",\r\n                          1,\r\n                          1\r\n                          )";
    if (!$sql_obj->execute($str_query)) {
        print_testResult(RESULT_FAILED, "SQL-Error[" . __LINE__ . "]:<br><pre>" . $sql_obj->error . "</pre>");
        return false;
    } else {
        print_testResult(RESULT_GOOD);
    }
    ### settings-directory already exists? ###
    if (!file_exists('../' . confGet('DIR_SETTINGS'))) {
        print_testStart("try to create " . confGet('DIR_SETTINGS') . "...");
        if (!mkdir('../' . confGet('DIR_SETTINGS'))) {
            print_testResult(RESULT_FAILED, "could not create directory. This could be a file permission problem...");
        } else {
            print_testResult(RESULT_GOOD);
        }
    }
    /* Write general site settings */
    $filename = "../" . confGet("DIR_SETTINGS") . confGet("SITE_SETTINGS");
    print_testStart("writing configuration file '" . $filename . "'...");
    $settings = array("APP_NAME" => $g_form_fields["site_name"]["value"], "EMAIL_ADMINISTRATOR" => $g_form_fields["site_email"]["value"], 'APP_TITLE_HEADER' => $g_form_fields["site_name"]["value"] . "<span class=extend>PM</span>");
    $write_ok = writeSettingsFile($filename, $settings);
    if (!$write_ok) {
        print_testResult(RESULT_FAILED, "can not write '" . $filename . "'. Please create it with this content:<br><pre>&lt;?php" . buildSettingsFile($settings) . "?&gt;</pre>");
        return false;
    } else {
        print_testResult(RESULT_GOOD);
    }
    /* Write database settings */
    $filename = '../' . confGet('DIR_SETTINGS') . confGet('FILE_DB_SETTINGS');
    print_testStart("writing configuration file '{$filename}'...");
    $settings = array('DB_TYPE' => $f_db_type, 'HOSTNAME' => $f_hostname, 'DB_USERNAME' => $f_db_username, 'DB_PASSWORD' => $f_db_password, 'DB_TABLE_PREFIX' => $f_db_table_prefix, 'DB_NAME' => $f_db_name, 'DB_VERSION' => confGet('STREBER_VERSION'));
    $write_ok = writeSettingsFile($filename, $settings);
    if (!$write_ok) {
        print_testResult(RESULT_FAILED, "can not write '{$filename}'. Please create it with this content:<br><pre>&lt;?php" . buildSettingsFile($settings) . "?&gt;</pre>");
        return false;
    } else {
        print_testResult(RESULT_GOOD);
    }
    ### tmp-directory already exists? ###
    if (!file_exists('../' . confGet('DIR_TEMP'))) {
        print_testStart("try to create directory of tempory files " . confGet('DIR_TEMP') . "...");
        if (!mkdir('../' . confGet('DIR_TEMP'))) {
            print_testResult(RESULT_FAILED, "could not create directory. This could be a file permission problem...");
        } else {
            print_testResult(RESULT_GOOD);
        }
    }
    return true;
}