Aleksander Machniak
2015-09-04 98a61c74eedd5cc8be9c2dc9b62b3a7fce30cd1d
installer/rcube_install.php
@@ -28,11 +28,13 @@
  var $failures = 0;
  var $config = array();
  var $configured = false;
  var $legacy_config = false;
  var $last_error = null;
  var $email_pattern = '([a-z0-9][a-z0-9\-\.\+\_]*@[a-z0-9]([a-z0-9\-][.]?)*[a-z0-9])';
  var $bool_config_props = array();
  var $obsolete_config = array('db_backend', 'double_auth');
  var $local_config = array('db_dsnw', 'default_host', 'support_url', 'des_key', 'plugins');
  var $obsolete_config = array('db_backend', 'db_max_length', 'double_auth');
  var $replaced_config = array(
    'skin_path'            => 'skin',
    'locale_string'        => 'language',
@@ -42,6 +44,8 @@
    'pagesize'             => 'mail_pagesize',
    'default_imap_folders' => 'default_folders',
    'top_posting'          => 'reply_mode',
    'keep_alive'           => 'refresh_interval',
    'min_keep_alive'       => 'min_refresh_interval',
  );
  // list of supported database drivers
@@ -97,9 +101,11 @@
    else {
      if ($config = $this->load_config_file(RCUBE_CONFIG_DIR . 'main.inc.php')) {
        $this->config = array_merge($this->config, $config);
        $this->legacy_config = true;
      }
      if ($config = $this->load_config_file(RCUBE_CONFIG_DIR . 'db.inc.php')) {
        $this->config = array_merge($this->config, $config);
        $this->legacy_config = true;
      }
    }
@@ -113,6 +119,28 @@
  {
    if (is_readable($file)) {
      include $file;
      // read comments from config file
      if (function_exists('token_get_all')) {
        $tokens = token_get_all(file_get_contents($file));
        $in_config = false;
        $buffer = '';
        for ($i=0; $i < count($tokens); $i++) {
          $token = $tokens[$i];
          if ($token[0] == T_VARIABLE && $token[1] == '$config' || $token[1] == '$rcmail_config') {
            $in_config = true;
            if ($buffer && $tokens[$i+1] == '[' && $tokens[$i+2][0] == T_CONSTANT_ENCAPSED_STRING) {
              $propname = trim($tokens[$i+2][1], "'\"");
              $this->comments[$propname] = $buffer;
              $buffer = '';
              $i += 3;
            }
          }
          else if ($in_config && $token[0] == T_COMMENT) {
            $buffer .= strtr($token[1], array('\n' => "\n"));
          }
        }
      }
      // deprecated name of config variable
      if (is_array($rcmail_config)) {
@@ -155,6 +183,18 @@
      $is_default = !isset($_POST["_$prop"]);
      $value      = !$is_default || $this->bool_config_props[$prop] ? $_POST["_$prop"] : $default;
      // always disable installer
      if ($prop == 'enable_installer')
        $value = false;
      // reset useragent to default (keeps version up-to-date)
      if ($prop == 'useragent' && stripos($value, 'Roundcube Webmail/') !== false)
        $value = $this->defaults[$prop];
      // generate new encryption key, never use the default value
      if ($prop == 'des_key' && $value == $this->defaults[$prop])
        $value = $this->random_key(24);
      // convert some form data
      if ($prop == 'debug_level' && !$is_default) {
        if (is_array($value)) {
@@ -164,7 +204,7 @@
          $value = $val;
        }
      }
      else if ($which == 'db' && $prop == 'db_dsnw' && !empty($_POST['_dbtype'])) {
      else if ($prop == 'db_dsnw' && !empty($_POST['_dbtype'])) {
        if ($_POST['_dbtype'] == 'sqlite')
          $value = sprintf('%s://%s?mode=0646', $_POST['_dbtype'], $_POST['_dbname']{0} == '/' ? '/' . $_POST['_dbname'] : $_POST['_dbname']);
        else if ($_POST['_dbtype'])
@@ -209,7 +249,9 @@
      }
      // skip this property
      if (!array_key_exists($prop, $this->defaults) || ($value == $this->defaults[$prop])) {
      if (($value == $this->defaults[$prop]) && !in_array($prop, $this->local_config)
          || in_array($prop, array_merge($this->obsolete_config, array_keys($this->replaced_config)))
          || preg_match('/^db_(table|sequence)_/', $prop)) {
        continue;
      }
@@ -218,19 +260,31 @@
      $config[$prop] = $value;
    }
    // sort by option name
    ksort($config);
    $out = "<?php\n\n";
    $out .= "/* Local configuration for Roundcube Webmail */\n\n";
    foreach ($config as $prop => $value) {
      // @TODO: copy option descriptions from defaults.inc.php file?
      $out .= "\$config['$prop'] = " . rcube_install::_dump_var($value, $prop) . ";\n";
      // copy option descriptions from existing config or defaults.inc.php
      $out .= $this->comments[$prop];
      $out .= "\$config['$prop'] = " . rcube_install::_dump_var($value, $prop) . ";\n\n";
    }
    $out .= "\n?>";
    return $out;
  }
  /**
   * save generated config file in RCUBE_CONFIG_DIR
   *
   * @return boolean True if the file was saved successfully, false if not
   */
  function save_configfile($config)
  {
    if (is_writable(RCUBE_CONFIG_DIR)) {
      return file_put_contents(RCUBE_CONFIG_DIR . 'config.inc.php', $config);
    }
    return false;
  }
  /**
   * Check the current configuration for missing properties
@@ -352,7 +406,7 @@
   *
   * @param rcube_db Database object
   *
   * @return boolean True if the schema is up-to-date, false if not or an error occured
   * @return boolean True if the schema is up-to-date, false if not or an error occurred
   */
  function db_schema_check($DB)
  {
@@ -405,6 +459,52 @@
    return $schema;
  }
  /**
   * Try to detect some file's mimetypes to test the correct behavior of fileinfo
   */
  function check_mime_detection()
  {
    $files = array(
      'installer/images/roundcube_logo.png' => 'image/png',
      'program/resources/blank.tif' => 'image/tiff',
      'skins/larry/images/buttons.gif' => 'image/gif',
      'skins/larry/README' => 'text/plain',
    );
    $errors = array();
    foreach ($files as $path => $expected) {
      $mimetype = rcube_mime::file_content_type(INSTALL_PATH . $path, basename($path));
      if ($mimetype != $expected) {
        $errors[] = array($path, $mimetype, $expected);
      }
    }
    return $errors;
  }
  /**
   * Check the correct configuration of the 'mime_types' mapping option
   */
  function check_mime_extensions()
  {
    $types = array(
      'application/zip'   => 'zip',
      'application/x-tar' => 'tar',
      'application/pdf'   => 'pdf',
      'image/gif'     => 'gif',
      'image/svg+xml' => 'svg',
    );
    $errors = array();
    foreach ($types as $mimetype => $expected) {
      $ext = rcube_mime::get_mime_extensions($mimetype);
      if (!in_array($expected, (array) $ext)) {
        $errors[] = array($mimetype, $ext, $expected);
      }
    }
    return $errors;
  }
  /**
   * Getter for the last error message
@@ -490,10 +590,13 @@
   * @param string Test name
   * @param string Error message
   * @param string URL for details
   * @param bool   Do not count this failure
   */
  function fail($name, $message = '', $url = '')
  function fail($name, $message = '', $url = '', $optional=false)
  {
    $this->failures++;
    if (!$optional) {
      $this->failures++;
    }
    echo Q($name) . ':&nbsp; <span class="fail">NOT OK</span>';
    $this->_showhint($message, $url);
@@ -621,7 +724,8 @@
    // read schema file from /SQL/*
    $fname = INSTALL_PATH . "SQL/$engine.initial.sql";
    if ($sql = @file_get_contents($fname)) {
      $this->exec_sql($sql, $DB);
      $DB->set_option('table_prefix', $this->config['db_prefix']);
      $DB->exec_script($sql);
    }
    else {
      $this->fail('DB Schema', "Cannot read the schema file: $fname");
@@ -652,63 +756,6 @@
      . " 2>&1", $result);
    return !$result;
  }
  /**
   * Execute the given SQL queries on the database connection
   *
   * @param string SQL queries to execute
   * @param object rcube_db Database connection
   * @return boolen True on success, False on error
   */
  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) == '')
        continue;
      $buff .= $line . "\n";
      if (preg_match('/(;|^GO)$/', trim($line))) {
        $DB->query($buff);
        $buff = '';
        if ($DB->is_error())
          break;
      }
    }
    return !$DB->is_error();
  }
  /**
   * 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;
  }