Example #1
0
 /**
  * Export configuration (config.inc.php)
  * @param DatabaseSqlite3 &$db Database object
  * @return boolean
  */
 public function exportConfiguration(&$db)
 {
     Log::debug('Export configuration: roundcube');
     // If MariaDB is enabled
     if (Config::read('mariadb') === 'enabled') {
         // Create database object
         $mdb = new DatabaseMariaDb();
         if ($mdb === false) {
             return false;
         }
         // Open database connection
         $mdb->open();
         if ($mdb === false) {
             return false;
         }
         // Detect if the Roundcube table ('roundcubemail') has already been created and privileges already set
         $mdb->query('SHOW DATABASES LIKE "roundcubemail"');
         $mdb->next_row();
         if ($mdb->row === false) {
             // Create database
             $mdb->query('CREATE DATABASE roundcubemail CHARACTER SET = "utf8" COLLATE = "utf8_unicode_ci"');
             // Set privileges
             $mdb->query("GRANT ALL PRIVILEGES ON roundcubemail.* TO roundcubemail@'localhost' IDENTIFIED BY '" . Config::read('roundcube|dbPassword') . "'");
             $mdb->query('FLUSH PRIVILEGES');
             // Select database
             $mdb->query('USE roundcubemail');
             // Load initial SQL database scheme
             $sql = file_get_contents('/usr/share/roundcubemail/SQL/mysql.initial.sql');
             // Remove comments
             $patterns = array('/^-- .*$/m', '/^\\/\\*.*$/m', '/\\/\\*(.*)\\*\\//u');
             $sql = preg_replace($patterns, '', $sql);
             // Convert to a single line of multiple statements
             $sql = str_replace("\n", ' ', preg_replace(array('/,\\n/', '/\\n\\)/m', '/\\)\\n/', '/\\(\\n/'), array(', ', ') ', ') ', '( '), $sql));
             // Execute
             $mdb->conn->exec($sql);
         } else {
             // Select table
             $mdb->query('USE roundcubemail');
             // Reset password of roundcubemail user, in case this is a re-run of the setup, this avoids stale passwords
             $mdb->query("SET PASSWORD FOR roundcubemail@'localhost' = PASSWORD('" . Config::read('roundcube|dbPassword') . "')");
         }
     }
     // Smarty template
     $smarty = TemplateFactory::create();
     if ($smarty === false) {
         return false;
     }
     // Assign variables
     $smarty->assign('USER', 'roundcubemail');
     $smarty->assign('PASSWORD', Config::read('roundcube|dbPassword'));
     $smarty->assign('DESKEY', Config::read('roundcube|desKey'));
     // Generate config.inc.php
     $rc = $this->saveConfigFile(Config::read('roundcube|configFile'), 'configincphp', 'roundcube.tpl', $smarty, '<' . '?php ');
     if ($rc === false) {
         return false;
     }
     // Restrict permissions and ownership
     chmod(Config::read('roundcube|configFile'), 0640);
     chgrp(Config::read('roundcube|configFile'), 'apache');
     // Generate roundcubemail.conf
     $rc = $this->saveConfigFile(Config::read('apache|directoryConfD') . '/roundcubemail.conf', 'roundcubemailconf', 'roundcube.tpl', $smarty);
     if ($rc === false) {
         return false;
     } else {
         return true;
     }
 }