PageRenderTime 55ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

/installer/installer.php

https://github.com/ShakeNBake/gallery3
PHP | 222 lines | 173 code | 25 blank | 24 comment | 18 complexity | a9578567d172aca4bd5d7a930b61819f MD5 | raw file
Possible License(s): GPL-2.0
  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-2009 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. class installer {
  21. static $mysqli;
  22. static function already_installed() {
  23. return file_exists(VARPATH . "database.php");
  24. }
  25. static function var_writable() {
  26. if (is_writable(VARPATH)) {
  27. return true;
  28. }
  29. if (@mkdir(VARPATH)) {
  30. return true;
  31. }
  32. return false;
  33. }
  34. static function create_database_config($config) {
  35. $db_config_file = VARPATH . "database.php";
  36. ob_start();
  37. extract($config);
  38. include(DOCROOT . "installer/database_config.php");
  39. $output = ob_get_clean();
  40. return file_put_contents($db_config_file, $output) !== false;
  41. }
  42. static function unpack_var() {
  43. if (!file_exists(VARPATH)) {
  44. mkdir(VARPATH);
  45. chmod(VARPATH, 0777);
  46. }
  47. include(DOCROOT . "installer/init_var.php");
  48. return true;
  49. }
  50. static function unpack_sql($config) {
  51. $prefix = $config["prefix"];
  52. $buf = null;
  53. foreach (file(DOCROOT . "installer/install.sql") as $line) {
  54. $buf .= trim($line);
  55. if (preg_match("/;$/", $buf)) {
  56. if (!mysql_query(self::prepend_prefix($prefix, $buf))) {
  57. return false;
  58. }
  59. $buf = "";
  60. }
  61. }
  62. return true;
  63. }
  64. static function connect($config) {
  65. // We know that we have either mysql or mysqli. By default we use mysql functions, so if
  66. // they're not defined then do the simplest thing which will work: remap them to their mysqli
  67. // counterparts.
  68. if (!function_exists("mysql_query")) {
  69. function mysql_connect($host, $user, $pass) {
  70. installer::$mysqli = new mysqli($host, $user, $pass);
  71. // http://php.net/manual/en/mysqli.connect.php says to use mysqli_connect_error() instead of
  72. // $mysqli->connect_error because of bugs before PHP 5.2.9
  73. $error = mysqli_connect_error();
  74. return empty($error);
  75. }
  76. function mysql_query($query) {
  77. return installer::$mysqli->query($query);
  78. }
  79. function mysql_num_rows($result) {
  80. return $result->num_rows;
  81. }
  82. function mysql_error() {
  83. return installer::$mysqli->error;
  84. }
  85. function mysql_select_db($db) {
  86. return installer::$mysqli->select_db($db);
  87. }
  88. }
  89. return @mysql_connect($config["host"], $config["user"], $config["password"]);
  90. }
  91. static function select_db($config) {
  92. if (mysql_select_db($config["dbname"])) {
  93. return true;
  94. }
  95. return mysql_query("CREATE DATABASE {$config['dbname']}") &&
  96. mysql_select_db($config["dbname"]);
  97. }
  98. static function verify_mysql_version($config) {
  99. return version_compare(installer::mysql_version($config), "5.0.0", ">=");
  100. }
  101. static function mysql_version($config) {
  102. $result = mysql_query("SHOW VARIABLES WHERE variable_name = \"version\"");
  103. $row = mysql_fetch_object($result);
  104. return $row->Value;
  105. }
  106. static function db_empty($config) {
  107. $query = "SHOW TABLES IN {$config['dbname']} LIKE '{$config['prefix']}items'";
  108. return mysql_num_rows(mysql_query($query)) == 0;
  109. }
  110. static function create_admin($config) {
  111. $salt = "";
  112. for ($i = 0; $i < 4; $i++) {
  113. $char = mt_rand(48, 109);
  114. $char += ($char > 90) ? 13 : ($char > 57) ? 7 : 0;
  115. $salt .= chr($char);
  116. }
  117. $password = substr(md5(time() * rand()), 0, 6);
  118. // Escape backslash in preparation for our UPDATE statement.
  119. $hashed_password = str_replace("\\", "\\\\", $salt . md5($salt . $password));
  120. $sql = self::prepend_prefix($config["prefix"],
  121. "UPDATE {users} SET `password` = '$hashed_password' WHERE `id` = 2");
  122. if (mysql_query($sql)) {
  123. } else {
  124. throw new Exception(mysql_error());
  125. }
  126. return array("admin", $password);
  127. }
  128. static function create_admin_session($config) {
  129. $session_id = md5(time() * rand());
  130. $user_agent = $_SERVER["HTTP_USER_AGENT"];
  131. $user_agent_len = strlen($user_agent);
  132. $now = time();
  133. $data = "session_id|s:32:\"$session_id\"";
  134. $data .= ";user_agent|s:{$user_agent_len}:\"$user_agent\"";
  135. $data .= ";user|i:2";
  136. $data .= ";after_install|i:1";
  137. $data .= ";last_activity|i:$now";
  138. $data = base64_encode($data);
  139. $sql = "INSERT INTO {sessions}(`session_id`, `last_activity`, `data`) " .
  140. "VALUES('$session_id', $now, '$data')";
  141. $sql = self::prepend_prefix($config["prefix"], $sql);
  142. if (mysql_query($sql)) {
  143. setcookie("g3sid", $session_id, 0, "/", "", false, false);
  144. } else {
  145. throw new Exception(mysql_error());
  146. }
  147. }
  148. static function create_private_key($config) {
  149. $key = md5(uniqid(mt_rand(), true)) . md5(uniqid(mt_rand(), true));
  150. $sql = self::prepend_prefix($config["prefix"],
  151. "INSERT INTO {vars} VALUES(NULL, 'gallery', 'private_key', '$key')");
  152. if (mysql_query($sql)) {
  153. } else {
  154. throw new Exception(mysql_error());
  155. }
  156. }
  157. static function prepend_prefix($prefix, $sql) {
  158. return preg_replace("#{([a-zA-Z0-9_]+)}#", "{$prefix}$1", $sql);
  159. }
  160. static function check_environment() {
  161. if (!function_exists("mysql_query") && !function_exists("mysqli_set_charset")) {
  162. $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.";
  163. }
  164. if (!@preg_match("/^.$/u", utf8_encode("\xF1"))) {
  165. $errors[] = "PHP is missing <a href=\"http://php.net/pcre\">Perl-Compatible Regular Expression</a> support.";
  166. }
  167. if (!(function_exists("spl_autoload_register"))) {
  168. $errors[] = "PHP is missing <a href=\"http://php.net/spl\">Standard PHP Library (SPL)</a> support";
  169. }
  170. if (!(class_exists("ReflectionClass"))) {
  171. $errors[] = "PHP is missing <a href=\"http://php.net/reflection\">reflection</a> support";
  172. }
  173. if (!(function_exists("filter_list"))) {
  174. $errors[] = "PHP is missing the <a href=\"http://php.net/filter\">filter extension</a>";
  175. }
  176. if (!(extension_loaded("iconv"))) {
  177. $errors[] = "PHP is missing the <a href=\"http://php.net/iconv\">iconv extension</a>";
  178. }
  179. if (!(extension_loaded("simplexml"))) {
  180. $errors[] = "PHP is missing the <a href=\"http://php.net/simplexml\">SimpleXML extension</a>";
  181. }
  182. if (extension_loaded("mbstring") && (ini_get("mbstring.func_overload") & MB_OVERLOAD_STRING)) {
  183. $errors[] = "The <a href=\"http://php.net/mbstring\">mbstring extension</a> is overloading PHP's native string functions. Please disable it.";
  184. }
  185. if (!function_exists("json_encode")) {
  186. $errors[] = "PHP is missing the <a href=\"http://php.net/manual/en/book.json.php\">JavaScript Object Notation (JSON) extension</a>. Please install it.";
  187. }
  188. return @$errors;
  189. }
  190. }