diff options
Diffstat (limited to 'installer')
-rw-r--r-- | installer/config.php | 12 | ||||
-rw-r--r-- | installer/rcube_install.php | 32 | ||||
-rw-r--r-- | installer/test.php | 8 |
3 files changed, 47 insertions, 5 deletions
diff --git a/installer/config.php b/installer/config.php index d6846edc7..b9a051ba0 100644 --- a/installer/config.php +++ b/installer/config.php @@ -301,6 +301,18 @@ echo '<label for="cfgdbpass">Database password (omit for sqlite)</label><br />'; ?> </dd> + +<dt class="propname">db_prefix</dt> +<dd> +<?php + +$input_prefix = new html_inputfield(array('name' => '_db_prefix', 'size' => 20, 'id' => "cfgdbprefix")); +echo $input_prefix->show($RCI->getprop('db_prefix')); + +?> +<div>Optional prefix that will be added to database object names (tables and sequences).</div> +</dd> + </dl> </fieldset> diff --git a/installer/rcube_install.php b/installer/rcube_install.php index 2e1298707..32b6a5dd7 100644 --- a/installer/rcube_install.php +++ b/installer/rcube_install.php @@ -372,7 +372,7 @@ class rcube_install $existing_tables = $DB->list_tables(); foreach ($db_schema as $table => $cols) { - $table = !empty($this->config['db_table_'.$table]) ? $this->config['db_table_'.$table] : $table; + $table = $this->config['db_prefix'] . $table; if (!in_array($table, $existing_tables)) { $errors[] = "Missing table '".$table."'"; } @@ -655,6 +655,7 @@ class rcube_install */ function exec_sql($sql, $DB) { + $sql = $this->fix_table_names($sql, $DB); $buff = ''; foreach (explode("\n", $sql) as $line) { if (preg_match('/^--/', $line) || trim($line) == '') @@ -674,6 +675,35 @@ class rcube_install /** + * Parse SQL file and fix table names according to db_prefix + * Note: This need to be a complete database initial file + */ + private function fix_table_names($sql, $DB) + { + if (empty($this->config['db_prefix'])) { + return $sql; + } + + // replace table names + if (preg_match_all('/CREATE TABLE (\[dbo\]\.|IF NOT EXISTS )?[`"\[\]]*([^`"\[\] \r\n]+)/i', $sql, $matches)) { + foreach ($matches[2] as $table) { + $real_table = $this->config['db_prefix'] . $table; + $sql = preg_replace("/([^a-zA-Z0-9_])$table([^a-zA-Z0-9_])/", "\\1$real_table\\2", $sql); + } + } + // replace sequence names + if ($DB->db_provider == 'postgres' && preg_match_all('/CREATE SEQUENCE (IF NOT EXISTS )?"?([^" \n\r]+)/i', $sql, $matches)) { + foreach ($matches[2] as $sequence) { + $real_sequence = $this->config['db_prefix'] . $sequence; + $sql = preg_replace("/([^a-zA-Z0-9_])$sequence([^a-zA-Z0-9_])/", "\\1$real_sequence\\2", $sql); + } + } + + return $sql; + } + + + /** * Handler for Roundcube errors */ function raise_error($p) diff --git a/installer/test.php b/installer/test.php index 340fe267d..fb3e7e937 100644 --- a/installer/test.php +++ b/installer/test.php @@ -24,7 +24,7 @@ else if (!$read_main) { } echo '<br />'; -if ($read_db && !empty($RCI->config['db_table_users'])) { +if ($read_db && !empty($RCI->config['db_dsnw'])) { $RCI->pass('db.inc.php'); } else if ($read_db) { @@ -171,7 +171,7 @@ else if ($db_working && $_POST['updatedb']) { // test database if ($db_working) { - $db_read = $DB->query("SELECT count(*) FROM {$RCI->config['db_table_users']}"); + $db_read = $DB->query("SELECT count(*) FROM {$RCI->config['db_prefix']}users"); if ($DB->is_error()) { $RCI->fail('DB Schema', "Database not initialized"); echo '<p><input type="submit" name="initdb" value="Initialize database" /></p>'; @@ -195,11 +195,11 @@ if ($db_working) { if ($db_working) { // write test $insert_id = md5(uniqid()); - $db_write = $DB->query("INSERT INTO {$RCI->config['db_table_session']} (sess_id, created, ip, vars) VALUES (?, ".$DB->now().", '127.0.0.1', 'foo')", $insert_id); + $db_write = $DB->query("INSERT INTO {$RCI->config['db_prefix']}session (sess_id, created, ip, vars) VALUES (?, ".$DB->now().", '127.0.0.1', 'foo')", $insert_id); if ($db_write) { $RCI->pass('DB Write'); - $DB->query("DELETE FROM {$RCI->config['db_table_session']} WHERE sess_id=?", $insert_id); + $DB->query("DELETE FROM {$RCI->config['db_prefix']}session WHERE sess_id=?", $insert_id); } else { $RCI->fail('DB Write', $RCI->get_error()); |