PageRenderTime 28ms CodeModel.GetById 10ms app.highlight 14ms RepoModel.GetById 1ms app.codeStats 0ms

/gallery3/installer/installer.php

#
PHP | 268 lines | 207 code | 32 blank | 29 comment | 29 complexity | 3a10dfd72cacfd01ed82159bfd50d332 MD5 | raw file
  1<?php defined("SYSPATH") or die("No direct script access.");
  2/**
  3 * Gallery - a web based photo album viewer and editor
  4 * Copyright (C) 2000-2012 Bharat Mediratta
  5 *
  6 * This program is free software; you can redistribute it and/or modify
  7 * it under the terms of the GNU General Public License as published by
  8 * the Free Software Foundation; either version 2 of the License, or (at
  9 * your option) any later version.
 10 *
 11 * This program is distributed in the hope that it will be useful, but
 12 * WITHOUT ANY WARRANTY; without even the implied warranty of
 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 14 * General Public License for more details.
 15 *
 16 * You should have received a copy of the GNU General Public License
 17 * along with this program; if not, write to the Free Software
 18 * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA  02110-1301, USA.
 19 */
 20
 21class installer {
 22  static $mysqli;
 23
 24  static function already_installed() {
 25    return file_exists(VARPATH . "database.php");
 26  }
 27
 28  static function var_writable() {
 29    if (is_writable(VARPATH)) {
 30      return true;
 31    }
 32
 33    if (@mkdir(VARPATH)) {
 34      return true;
 35    }
 36
 37    return false;
 38  }
 39
 40  static function create_database_config($config) {
 41    $db_config_file = VARPATH . "database.php";
 42    ob_start();
 43    extract($config);
 44    include(DOCROOT . "installer/database_config.php");
 45    $output = ob_get_clean();
 46    return file_put_contents($db_config_file, $output) !== false;
 47  }
 48
 49  static function unpack_var() {
 50    if (!file_exists(VARPATH)) {
 51      mkdir(VARPATH);
 52      chmod(VARPATH, 0777);
 53    }
 54
 55    include(DOCROOT . "installer/init_var.php");
 56    return true;
 57  }
 58
 59  static function unpack_sql($config) {
 60    $prefix = $config["prefix"];
 61    $buf = null;
 62    foreach (file(DOCROOT . "installer/install.sql") as $line) {
 63      $buf .= trim($line);
 64      if (preg_match("/;$/", $buf)) {
 65        if (!mysql_query(self::prepend_prefix($prefix, $buf))) {
 66          return false;
 67        }
 68        $buf = "";
 69      }
 70    }
 71    return true;
 72  }
 73
 74  static function connect($config) {
 75    // We know that we have either mysql or mysqli.  By default we use mysql functions, so if
 76    // they're not defined then do the simplest thing which will work: remap them to their mysqli
 77    // counterparts.
 78    if (!function_exists("mysql_query")) {
 79      function mysql_connect($host, $user, $pass) {
 80        list ($host, $port) = explode(":", $host . ":");
 81        installer::$mysqli = new mysqli($host, $user, $pass, $port);
 82        // http://php.net/manual/en/mysqli.connect.php says to use mysqli_connect_error() instead of
 83        // $mysqli->connect_error because of bugs before PHP 5.2.9
 84        $error = mysqli_connect_error();
 85        return empty($error);
 86      }
 87      function mysql_query($query) {
 88        return installer::$mysqli->query($query);
 89      }
 90      function mysql_num_rows($result) {
 91        return $result->num_rows;
 92      }
 93      function mysql_error() {
 94        return installer::$mysqli->error;
 95      }
 96      function mysql_select_db($db) {
 97        return installer::$mysqli->select_db($db);
 98      }
 99    }
100
101    $host = empty($config["port"]) ? $config['host'] : "{$config['host']}:{$config['port']}";
102    return @mysql_connect($host, $config["user"], $config["password"]);
103  }
104
105  static function select_db($config) {
106    if (mysql_select_db($config["dbname"])) {
107      return true;
108    }
109
110    return mysql_query("CREATE DATABASE `{$config['dbname']}`") &&
111      mysql_select_db($config["dbname"]);
112  }
113
114  static function verify_mysql_version($config) {
115    return version_compare(installer::mysql_version($config), "5.0.0", ">=");
116  }
117
118  static function mysql_version($config) {
119    $result = mysql_query("SHOW VARIABLES WHERE variable_name = \"version\"");
120    $row = mysql_fetch_object($result);
121    return $row->Value;
122  }
123
124  static function db_empty($config) {
125    $query = "SHOW TABLES LIKE '{$config['prefix']}items'";
126    $results = mysql_query($query);
127    if ($results === false) {
128      $msg = mysql_error();
129      return $msg;
130    }
131    return mysql_num_rows($results) === 0;
132  }
133
134  static function create_admin($config) {
135    $salt = "";
136    for ($i = 0; $i < 4; $i++) {
137      $char = mt_rand(48, 109);
138      $char += ($char > 90) ? 13 : ($char > 57) ? 7 : 0;
139      $salt .= chr($char);
140    }
141    $password = substr(md5(time() . mt_rand()), 0, 6);
142    // Escape backslash in preparation for our UPDATE statement.
143    $hashed_password = str_replace("\\", "\\\\", $salt . md5($salt . $password));
144    $sql = self::prepend_prefix($config["prefix"],
145       "UPDATE {users} SET `password` = '$hashed_password' WHERE `id` = 2");
146    if (mysql_query($sql)) {
147    } else {
148      throw new Exception(mysql_error());
149    }
150
151    return array("admin", $password);
152  }
153
154  static function create_admin_session($config) {
155    $session_id = md5(time() . mt_rand());
156    $user_agent = $_SERVER["HTTP_USER_AGENT"];
157    $user_agent_len = strlen($user_agent);
158    $now = time();
159    $data = "session_id|s:32:\"$session_id\"";
160    $data .= ";user_agent|s:{$user_agent_len}:\"$user_agent\"";
161    $data .= ";user|i:2";
162    $data .= ";after_install|i:1";
163    $data .= ";last_activity|i:$now";
164    $data = base64_encode($data);
165    $sql = "INSERT INTO {sessions}(`session_id`, `last_activity`, `data`) " .
166      "VALUES('$session_id', $now, '$data')";
167    $sql = self::prepend_prefix($config["prefix"], $sql);
168    if (mysql_query($sql)) {
169      setcookie("g3sid", $session_id, 0, "/", "", false, false);
170    } else {
171      throw new Exception(mysql_error());
172    }
173  }
174
175  static function create_private_key($config) {
176    $key = md5(uniqid(mt_rand(), true)) . md5(uniqid(mt_rand(), true));
177    $sql = self::prepend_prefix($config["prefix"],
178       "INSERT INTO {vars} VALUES(NULL, 'gallery', 'private_key', '$key')");
179    if (mysql_query($sql)) {
180    } else {
181      throw new Exception(mysql_error());
182    }
183  }
184
185  static function prepend_prefix($prefix, $sql) {
186    return preg_replace("#{([a-zA-Z0-9_]+)}#", "`{$prefix}$1`", $sql);
187  }
188
189  static function check_environment() {
190    if (!function_exists("mysql_query") && !function_exists("mysqli_set_charset")) {
191      $errors[] = "Gallery 3 requires a MySQL database, but PHP doesn't have either the <a href=\"http://php.net/mysql\">MySQL</a> or the  <a href=\"http://php.net/mysqli\">MySQLi</a> extension.";
192    }
193
194    if (!preg_match("/^.$/u", "?")) {
195      $errors[] = "PHP is missing <a href=\"http://php.net/pcre\">Perl-Compatible Regular Expression</a> with UTF-8 support.";
196    } else if (!preg_match("/^\pL$/u", "?")) {
197      $errors[] = "PHP is missing <a href=\"http://php.net/pcre\">Perl-Compatible Regular Expression</a> with Unicode support.";
198    }
199
200    if (!(function_exists("spl_autoload_register"))) {
201      $errors[] = "PHP is missing <a href=\"http://php.net/spl\">Standard PHP Library (SPL)</a> support";
202    }
203
204    if (!(class_exists("ReflectionClass"))) {
205      $errors[] = "PHP is missing <a href=\"http://php.net/reflection\">reflection</a> support";
206    }
207
208    if (!(function_exists("filter_list"))) {
209      $errors[] = "PHP is missing the <a href=\"http://php.net/filter\">filter extension</a>";
210    }
211
212    if (!(extension_loaded("iconv"))) {
213      $errors[] = "PHP is missing the <a href=\"http://php.net/iconv\">iconv extension</a>";
214    }
215
216    if (!(extension_loaded("xml"))) {
217      $errors[] = "PHP is missing the <a href=\"http://php.net/xml\">XML Parser extension</a>";
218    }
219
220    if (!(extension_loaded("simplexml"))) {
221      $errors[] = "PHP is missing the <a href=\"http://php.net/simplexml\">SimpleXML extension</a>";
222    }
223
224    if (!extension_loaded("mbstring")) {
225      $errors[] = "PHP is missing the <a href=\"http://php.net/mbstring\">mbstring extension</a>";
226    } else if (ini_get("mbstring.func_overload") & MB_OVERLOAD_STRING) {
227      $errors[] = "The <a href=\"http://php.net/mbstring\">mbstring extension</a> is overloading PHP's native string functions.  Please disable it.";
228    }
229
230    if (!function_exists("json_encode")) {
231      $errors[] = "PHP is missing the <a href=\"http://php.net/manual/en/book.json.php\">JavaScript Object Notation (JSON) extension</a>.  Please install it.";
232    }
233
234    if (!ini_get("short_open_tag")) {
235      $errors[] = "Gallery requires <a href=\"http://php.net/manual/en/ini.core.php\">short_open_tag</a> to be on.  Please enable it in your php.ini.";
236    }
237
238    if (!function_exists("ctype_alpha")) {
239      $errors[] = "Gallery requires the <a href=\"http://php.net/manual/en/book.ctype.php\">PHP Ctype</a> extension.  Please install it.";
240    }
241
242    if (self::ini_get_bool("safe_mode")) {
243      $errors[] = "Gallery cannot function when PHP is in <a href=\"http://php.net/manual/en/features.safe-mode.php\">Safe Mode</a>.  Please disable safe mode.";
244    }
245
246    return @$errors;
247  }
248
249  /**
250   * Convert any possible boolean ini value to true/false.
251   *   On = on = 1 = true
252   *   Off = off = 0 = false
253   */
254  static function ini_get_bool($varname) {
255    $value = ini_get($varname);
256
257    if (!strcasecmp("on", $value) || $value == 1 || $value === true) {
258      return true;
259    }
260
261    if (!strcasecmp("off", $value) || $value == 0 || $value === false) {
262      return false;
263    }
264
265    return false;
266  }
267
268}