summaryrefslogtreecommitdiff
path: root/installer/cli.php
blob: a853f67bf086df286a3ee0145705a5b9f52fa354 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
<?php defined("SYSPATH") or die("No direct script access.");
/**
 * Gallery - a web based photo album viewer and editor
 * Copyright (C) 2000-2008 Bharat Mediratta
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or (at
 * your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA  02110-1301, USA.
 */
/**
 * Command line parameters:
 * -h     Database host          (default: localhost)
 * -u     Database user          (default: root)
 * -p     Database user password (default: )
 * -d     Database name          (default: gallery3)
 * -x     Table prefix           (default: )
 */
if (installer::already_installed()) {
  print "Gallery 3 is already installed.\n";
  return;
}

$config = parse_cli_params();
if (!installer::connect($config)) {
  oops("Unable to connect to the database.\n" . mysql_error() . "\n");
} else if (!installer::select_db($config)) {
  oops("Database {$config['dbname']} doesn't exist and can't be created.  " .
       "Please create the database by hand.");
} else if (!installer::db_empty($config)) {
  oops("Database {$config['dbname']} already has tables in it. " .
       "Please specify an empty database.\n");
} else if (!installer::unpack_var()) {
  oops("Unable to create files inside the 'var' directory");
} else if (!installer::unpack_sql($config)) {
  oops("Failed to create database tables\n" . mysql_error());
} else if (!installer::create_database_config($config)) {
  oops("Couldn't create var/database.php");
} else {
  system("chmod -R 777 " . VARPATH);
  try {
    list ($user, $password) = installer::create_admin($config);
    print "Your Gallery has been successfully installed!\n";
    print "We've created an account for you to use:\n";
    print "  username: $user\n";
    print "  password: $password\n";
    print "\n";

    installer::create_private_key($config);
    exit(0);
  } catch (Exception $e) {
    oops($e->getMessage());
  }
}

function oops($message) {
  print "Oops! Something went wrong during the installation:\n\n";

  print "==> " . $message;
  print "\n";
  print "For help you can try:\n";
  print "  * The Gallery3 FAQ   - http://codex.gallery2.org/Gallery3:FAQ\n";
  print "  * The Gallery Forums - http://gallery.menalto.com/forum\n";
  print "\n\n** INSTALLATION FAILED **\n";
  exit(1);
}

function parse_cli_params() {
  $config = array("host" => "localhost",
                  "user" => "root",
                  "password" => "",
                  "dbname" => "gallery3",
                  "prefix" => "",
                  "type" => function_exists("mysqli_set_charset") ? "mysqli" : "mysql");

  $argv = $_SERVER["argv"];
  for ($i = 1; $i < count($argv); $i++) {
    switch (strtolower($argv[$i])) {
    case "-d":
      $config["dbname"] = $argv[++$i];
      break;
    case "-h":
      $config["host"] = $argv[++$i];
      break;
    case "-u":
      $config["user"] = $argv[++$i];
      break;
    case "-p":
      $config["password"] = $argv[++$i];
      break;
    case "-x":
      $config["prefix"] = $argv[++$i];
      break;
    }
  }

  return $config;
}