+ /**
+ * Compare the local database schema with the reference schema
+ * required for this version of Roundcube
+ *
+ * @param boolean True if the schema schould be updated
+ * @return boolean True if the schema is up-to-date, false if not or an error occured
+ */
+ function db_schema_check($DB, $update = false)
+ {
+ if (!$this->configured)
+ return false;
+
+ // read reference schema from mysql.initial.sql
+ $db_schema = $this->db_read_schema(INSTALL_PATH . 'SQL/mysql.initial.sql');
+ $errors = array();
+
+ // check list of tables
+ $existing_tables = $DB->list_tables();
+
+ foreach ($db_schema as $table => $cols) {
+ $table = !empty($this->config['db_table_'.$table]) ? $this->config['db_table_'.$table] : $table;
+ if (!in_array($table, $existing_tables)) {
+ $errors[] = "Missing table '".$table."'";
+ }
+ else { // compare cols
+ $db_cols = $DB->list_cols($table);
+ $diff = array_diff(array_keys($cols), $db_cols);
+ if (!empty($diff))
+ $errors[] = "Missing columns in table '$table': " . join(',', $diff);
+ }
+ }
+
+ return !empty($errors) ? $errors : false;
+ }
+
+ /**
+ * Utility function to read database schema from an .sql file
+ */
+ private function db_read_schema($schemafile)
+ {
+ $lines = file($schemafile);
+ $table_block = false;
+ $schema = array();
+ foreach ($lines as $line) {
+ if (preg_match('/^\s*create table `?([a-z0-9_]+)`?/i', $line, $m)) {
+ $table_block = $m[1];
+ }
+ else if ($table_block && preg_match('/^\s*`?([a-z0-9_-]+)`?\s+([a-z]+)/', $line, $m)) {
+ $col = $m[1];
+ if (!in_array(strtoupper($col), array('PRIMARY','KEY','INDEX','UNIQUE','CONSTRAINT','REFERENCES','FOREIGN'))) {
+ $schema[$table_block][$col] = $m[2];
+ }
+ }
+ }
+
+ return $schema;
+ }
+