$errors));
    } else {
      $content = render("get_db_info.html.php");
    }
    break;
  case "save_db_info":
    $config = array("host" => $_POST["dbhost"],
                    "user" => $_POST["dbuser"],
                    "password" => $_POST["dbpass"],
                    "dbname" => $_POST["dbname"],
                    "prefix" => $_POST["prefix"],
                    "type" => function_exists("mysqli_set_charset") ? "mysqli" : "mysql");
    if (!installer::connect($config)) {
      $content = render("invalid_db_info.html.php");
    } else if (!installer::select_db($config)) {
      $content = render("missing_db.html.php");
    } else if (!installer::db_empty($config)) {
      $content = render("db_not_empty.html.php");
    } else if (!installer::unpack_var()) {
      $content = oops("Unable to create files inside the var directory");
    } else if (!installer::unpack_sql($config)) {
      $content = oops("Failed to create tables in your database:" . mysql_error());
    } else if (!installer::create_database_config($config)) {
      $content = oops("Couldn't create var/database.php");
    } else {
      try {
        list ($user, $password) = installer::create_admin($config);
        installer::create_admin_session($config);
        $content = render("success.html.php", array("user" => $user, "password" => $password));
        installer::create_private_key($config);
      } catch (Exception $e) {
        $content = oops($e->getMessage());
      }
    }
    break;
  }
}
include("views/install.html.php");
function render($view, $args=array()) {
  ob_start();
  extract($args);
  include(DOCROOT . "installer/views/" . $view);
  return ob_get_clean();
}
function oops($error) {
  return render("oops.html.php", array("error" => $error));
}
function check_environment() {
  if (!function_exists("mysql_query") && !function_exists("mysqli_set_charset")) {
    $errors[] = "Gallery 3 requires a MySQL database, but PHP doesn't have either the MySQL or the  MySQLi extension.";
  }
  if (!@preg_match("/^.$/u", utf8_encode("\xF1"))) {
    $errors[] = "PHP is missing Perl-Compatible Regular Expression support.";
  }
  if (!(function_exists("spl_autoload_register"))) {
    $errors[] = "PHP is missing Standard PHP Library (SPL) support";
  }
  if (!(class_exists("ReflectionClass"))) {
    $errors[] = "PHP is missing reflection support";
  }
  if (!(function_exists("filter_list"))) {
    $errors[] = "PHP is missing the filter extension";
  }
  if (!(extension_loaded("iconv"))) {
    $errors[] = "PHP is missing the iconv extension";
  }
  if (extension_loaded("mbstring") && (ini_get("mbstring.func_overload") & MB_OVERLOAD_STRING)) {
    $errors[] = "The mbstring extension is overloading PHP's native string functions.  Please disable it.";
  }
  if (!function_exists("json_encode")) {
    $errors[] = "PHP is missing the JavaScript Object Notation (JSON) extension.  Please install it.";
  }
  return @$errors;
}