2009-01-09 18:33:48 +00:00
< ? php defined ( " SYSPATH " ) or die ( " No direct script access. " );
/**
* Gallery - a web based photo album viewer and editor
2010-03-03 10:15:34 -08:00
* Copyright ( C ) 2000 - 2010 Bharat Mediratta
2009-01-09 18:33:48 +00:00
*
* 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 .
*/
2009-06-13 17:06:34 -07:00
2009-01-10 20:47:38 +00:00
class installer {
2009-06-13 17:06:34 -07:00
static $mysqli ;
2009-01-19 08:59:50 +00:00
static function already_installed () {
return file_exists ( VARPATH . " database.php " );
}
2009-01-20 00:54:02 +00:00
static function var_writable () {
if ( is_writable ( VARPATH )) {
return true ;
2009-01-09 18:33:48 +00:00
}
2009-01-20 00:54:02 +00:00
if ( @ mkdir ( VARPATH )) {
return true ;
2009-01-10 20:47:38 +00:00
}
2009-01-20 00:54:02 +00:00
return false ;
2009-01-11 23:08:23 +00:00
}
2009-01-20 00:54:02 +00:00
static function create_database_config ( $config ) {
$db_config_file = VARPATH . " database.php " ;
ob_start ();
extract ( $config );
include ( DOCROOT . " installer/database_config.php " );
$output = ob_get_clean ();
return file_put_contents ( $db_config_file , $output ) !== false ;
}
2009-01-14 03:38:51 +00:00
2009-01-20 00:54:02 +00:00
static function unpack_var () {
2009-09-10 21:10:20 -07:00
if ( ! file_exists ( VARPATH )) {
mkdir ( VARPATH );
chmod ( VARPATH , 0777 );
}
2009-01-19 03:31:13 +00:00
include ( DOCROOT . " installer/init_var.php " );
2009-01-20 00:54:02 +00:00
return true ;
}
2009-01-14 04:12:02 +00:00
2009-02-27 19:26:21 +00:00
static function unpack_sql ( $config ) {
$prefix = $config [ " prefix " ];
2009-06-01 00:22:21 -04:00
$buf = null ;
2009-01-20 00:54:02 +00:00
foreach ( file ( DOCROOT . " installer/install.sql " ) as $line ) {
2009-09-10 21:22:07 -07:00
$buf .= trim ( $line );
2009-01-19 03:31:13 +00:00
if ( preg_match ( " /; $ / " , $buf )) {
2009-02-27 19:26:21 +00:00
if ( ! mysql_query ( self :: prepend_prefix ( $prefix , $buf ))) {
2009-01-20 00:54:02 +00:00
return false ;
2009-01-19 03:31:13 +00:00
}
$buf = " " ;
}
2009-01-14 03:38:51 +00:00
}
2009-01-20 00:54:02 +00:00
return true ;
}
2009-01-14 03:38:51 +00:00
2009-01-20 00:54:02 +00:00
static function connect ( $config ) {
2009-06-28 19:23:19 -07:00
// We know that we have either mysql or mysqli. By default we use mysql functions, so if
// they're not defined then do the simplest thing which will work: remap them to their mysqli
// counterparts.
if ( ! function_exists ( " mysql_query " )) {
function mysql_connect ( $host , $user , $pass ) {
2010-02-14 17:07:31 -08:00
list ( $host , $port ) = explode ( " : " , $host . " : " );
2009-11-12 14:42:37 -08:00
installer :: $mysqli = new mysqli ( $host , $user , $pass , $port );
2009-06-28 19:23:19 -07:00
// http://php.net/manual/en/mysqli.connect.php says to use mysqli_connect_error() instead of
// $mysqli->connect_error because of bugs before PHP 5.2.9
$error = mysqli_connect_error ();
return empty ( $error );
}
function mysql_query ( $query ) {
return installer :: $mysqli -> query ( $query );
}
function mysql_num_rows ( $result ) {
return $result -> num_rows ;
}
function mysql_error () {
return installer :: $mysqli -> error ;
}
function mysql_select_db ( $db ) {
return installer :: $mysqli -> select_db ( $db );
}
}
2009-11-12 14:42:37 -08:00
$host = empty ( $config [ " port " ]) ? $config [ 'host' ] : " { $config [ 'host' ] } : { $config [ 'port' ] } " ;
return @ mysql_connect ( $host , $config [ " user " ], $config [ " password " ]);
2009-01-20 00:54:02 +00:00
}
2009-01-14 04:12:02 +00:00
2009-01-20 00:54:02 +00:00
static function select_db ( $config ) {
if ( mysql_select_db ( $config [ " dbname " ])) {
return true ;
2009-01-19 03:31:13 +00:00
}
2009-01-19 19:05:22 +00:00
2010-01-06 10:11:03 -08:00
return mysql_query ( " CREATE DATABASE ` { $config [ 'dbname' ] } ` " ) &&
2009-01-20 00:54:02 +00:00
mysql_select_db ( $config [ " dbname " ]);
}
2009-08-30 13:43:29 -07:00
static function verify_mysql_version ( $config ) {
return version_compare ( installer :: mysql_version ( $config ), " 5.0.0 " , " >= " );
}
static function mysql_version ( $config ) {
2009-08-28 15:17:07 -07:00
$result = mysql_query ( " SHOW VARIABLES WHERE variable_name = \" version \" " );
2009-08-30 21:12:35 -07:00
$row = mysql_fetch_object ( $result );
2009-08-30 15:18:20 -07:00
return $row -> Value ;
2009-08-28 15:17:07 -07:00
}
2009-01-20 00:54:02 +00:00
static function db_empty ( $config ) {
2010-01-06 10:11:03 -08:00
$query = " SHOW TABLES LIKE ' { $config [ 'prefix' ] } items' " ;
$results = mysql_query ( $query );
if ( $results === false ) {
$msg = mysql_error ();
return $msg ;
}
return mysql_num_rows ( $results ) === 0 ;
2009-01-14 03:38:51 +00:00
}
2009-01-19 03:31:13 +00:00
static function create_admin ( $config ) {
$salt = " " ;
for ( $i = 0 ; $i < 4 ; $i ++ ) {
$char = mt_rand ( 48 , 109 );
$char += ( $char > 90 ) ? 13 : ( $char > 57 ) ? 7 : 0 ;
$salt .= chr ( $char );
}
$password = substr ( md5 ( time () * rand ()), 0 , 6 );
2009-09-01 21:12:42 -07:00
// Escape backslash in preparation for our UPDATE statement.
$hashed_password = str_replace ( " \\ " , " \\ \\ " , $salt . md5 ( $salt . $password ));
2009-02-27 19:26:21 +00:00
$sql = self :: prepend_prefix ( $config [ " prefix " ],
2009-02-28 06:37:28 +00:00
" UPDATE { users} SET `password` = ' $hashed_password ' WHERE `id` = 2 " );
2009-02-27 19:26:21 +00:00
if ( mysql_query ( $sql )) {
2009-01-14 03:38:51 +00:00
} else {
2009-02-15 09:14:03 +00:00
throw new Exception ( mysql_error ());
2009-01-19 03:31:13 +00:00
}
2009-01-14 03:38:51 +00:00
2009-01-19 03:31:13 +00:00
return array ( " admin " , $password );
2009-01-14 03:38:51 +00:00
}
2009-02-17 07:03:40 +00:00
2009-03-08 21:21:09 +00:00
static function create_admin_session ( $config ) {
$session_id = md5 ( time () * rand ());
$user_agent = $_SERVER [ " HTTP_USER_AGENT " ];
$user_agent_len = strlen ( $user_agent );
$now = time ();
$data = " session_id|s:32: \" $session_id\ " " ;
$data .= " ;user_agent|s: { $user_agent_len } : \" $user_agent\ " " ;
$data .= " ;user|i:2 " ;
2009-03-09 00:03:04 +00:00
$data .= " ;after_install|i:1 " ;
2009-03-08 21:21:09 +00:00
$data .= " ;last_activity|i: $now " ;
$data = base64_encode ( $data );
2009-05-29 18:04:54 -07:00
$sql = " INSERT INTO { sessions}(`session_id`, `last_activity`, `data`) " .
" VALUES(' $session_id ', $now , ' $data ') " ;
2009-03-08 21:21:09 +00:00
$sql = self :: prepend_prefix ( $config [ " prefix " ], $sql );
if ( mysql_query ( $sql )) {
setcookie ( " g3sid " , $session_id , 0 , " / " , " " , false , false );
} else {
throw new Exception ( mysql_error ());
}
}
2009-02-27 19:26:21 +00:00
static function create_private_key ( $config ) {
2009-02-17 07:03:40 +00:00
$key = md5 ( uniqid ( mt_rand (), true )) . md5 ( uniqid ( mt_rand (), true ));
2009-02-27 19:26:21 +00:00
$sql = self :: prepend_prefix ( $config [ " prefix " ],
2009-05-27 16:15:00 -07:00
" INSERT INTO { vars} VALUES(NULL, 'gallery', 'private_key', ' $key ') " );
2009-02-27 19:26:21 +00:00
if ( mysql_query ( $sql )) {
2009-02-17 07:03:40 +00:00
} else {
throw new Exception ( mysql_error ());
}
}
2009-02-27 19:26:21 +00:00
static function prepend_prefix ( $prefix , $sql ) {
2009-02-28 06:37:28 +00:00
return preg_replace ( " # { ([a-zA-Z0-9_]+)}# " , " { $prefix } $ 1 " , $sql );
2009-02-27 19:26:21 +00:00
}
2009-09-17 07:29:37 -07:00
static function check_environment () {
2010-06-06 19:04:15 -07:00
if ( ! function_exists ( " mysql_query " ) && ! function_exists ( " mysqli_set_charset " )) {
$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. " ;
}
2009-09-17 07:29:37 -07:00
2010-06-06 19:04:15 -07:00
if ( !@ preg_match ( " /^. $ /u " , utf8_encode ( " \xF1 " ))) {
$errors [] = " PHP is missing <a href= \" http://php.net/pcre \" >Perl-Compatible Regular Expression</a> support. " ;
}
2009-09-17 07:29:37 -07:00
2010-06-06 19:04:15 -07:00
if ( ! ( function_exists ( " spl_autoload_register " ))) {
$errors [] = " PHP is missing <a href= \" http://php.net/spl \" >Standard PHP Library (SPL)</a> support " ;
}
2009-09-17 07:29:37 -07:00
2010-06-06 19:04:15 -07:00
if ( ! ( class_exists ( " ReflectionClass " ))) {
$errors [] = " PHP is missing <a href= \" http://php.net/reflection \" >reflection</a> support " ;
}
2009-09-17 07:29:37 -07:00
2010-06-06 19:04:15 -07:00
if ( ! ( function_exists ( " filter_list " ))) {
$errors [] = " PHP is missing the <a href= \" http://php.net/filter \" >filter extension</a> " ;
}
2009-09-17 07:29:37 -07:00
2010-06-06 19:04:15 -07:00
if ( ! ( extension_loaded ( " iconv " ))) {
$errors [] = " PHP is missing the <a href= \" http://php.net/iconv \" >iconv extension</a> " ;
}
2009-09-17 07:29:37 -07:00
2010-06-06 19:04:15 -07:00
if ( ! ( extension_loaded ( " simplexml " ))) {
$errors [] = " PHP is missing the <a href= \" http://php.net/simplexml \" >SimpleXML extension</a> " ;
}
2009-09-17 07:29:37 -07:00
2010-06-06 19:04:15 -07:00
if ( ! extension_loaded ( " mbstring " )) {
$errors [] = " PHP is missing the <a href= \" http://php.net/mbstring \" >mbstring extension</a> " ;
} else if ( ini_get ( " mbstring.func_overload " ) & MB_OVERLOAD_STRING ) {
$errors [] = " The <a href= \" http://php.net/mbstring \" >mbstring extension</a> is overloading PHP's native string functions. Please disable it. " ;
}
2009-09-17 07:29:37 -07:00
2010-06-06 19:04:15 -07:00
if ( ! function_exists ( " json_encode " )) {
$errors [] = " PHP is missing the <a href= \" http://php.net/manual/en/book.json.php \" >JavaScript Object Notation (JSON) extension</a>. Please install it. " ;
}
2009-09-17 07:29:37 -07:00
2010-06-06 19:06:24 -07:00
if ( ! ini_get ( " short_open_tag " )) {
$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. " ;
}
2010-06-06 19:04:15 -07:00
return @ $errors ;
}
2009-09-17 07:29:37 -07:00
2009-06-01 00:22:21 -04:00
}