2008-11-26 22:50:40 +00:00
< ? php
/**
* AgileBill - Open Billing Software
*
* This body of work is free software ; you can redistribute it and / or
* modify it under the terms of the Open AgileBill License
* License as published at http :// www . agileco . com / agilebill / license1 - 4. txt
2009-08-03 04:10:16 +00:00
*
* Originally authored by Tony Landis , AgileBill LLC
* Recent modifications by Deon George
*
* @ author Deon George < deonATleenooksDOTnet >
* @ copyright 2009 Deon George
* @ link http :// osb . leenooks . net
2008-11-26 22:50:40 +00:00
*
* @ link http :// www . agileco . com /
* @ copyright 2004 - 2008 Agileco , LLC .
* @ license http :// www . agileco . com / agilebill / license1 - 4. txt
2009-08-03 04:10:16 +00:00
* @ author Tony Landis < tony @ agileco . com >
2008-11-26 22:50:40 +00:00
* @ package AgileBill
2009-08-03 04:10:16 +00:00
* @ subpackage Install
2008-11-26 22:50:40 +00:00
*/
2009-08-03 04:10:16 +00:00
/**
* The main AgileBill Installer
*/
global $VAR , $_SERVER , $HTTP_SERVER_VARS ;
# Get the file path
if ( isset ( $_SERVER [ 'PATH_TRANSLATED' ]) && is_file ( $_SERVER [ 'PATH_TRANSLATED' ]))
$path = $_SERVER [ 'PATH_TRANSLATED' ];
elseif ( is_file ( $HTTP_SERVER_VARS [ 'SCRIPT_FILENAME' ]))
$path = $HTTP_SERVER_VARS [ 'SCRIPT_FILENAME' ];
else
2008-11-26 22:50:40 +00:00
$path = PATH_AGILE ;
2009-08-03 04:10:16 +00:00
$path = str_replace ( 'index.php' , '' , $path );
$path = str_replace ( '\\' , '/' , $path );
# Get the current IP & Domain
if ( isset ( $_SERVER )) {
2008-11-26 22:50:40 +00:00
$site_domain = @ $_SERVER [ 'HTTP_HOST' ];
2009-08-03 04:10:16 +00:00
$site_ip = @ $_SERVER [ 'SERVER_ADDR' ];
2008-11-26 22:50:40 +00:00
} else {
$server = getallheaders ();
$site_domain = $server [ 'Host' ];
2009-08-03 04:10:16 +00:00
$site_ip = false ;
2008-11-26 22:50:40 +00:00
}
# req writable files/paths
2009-08-03 04:10:16 +00:00
$_FILES = array (
array ( 'dir' , 'includes/smarty/templates/' ),
array ( 'dir' , 'includes/files/' ),
array ( 'dir' , 'includes/images/' )
);
# Define the default group to add all methods to
$VAR [ 'module_group' ] = array ( '1001' );
# Action handler
if ( ! empty ( $VAR [ 'do' ])) {
2008-11-26 22:50:40 +00:00
$do = $VAR [ 'do' ];
2009-08-03 04:10:16 +00:00
switch ( $VAR [ 'do' ]) {
# Check file permissions:
2008-11-26 22:50:40 +00:00
case 'step1' :
2009-08-03 04:10:16 +00:00
$files = test_files ( $_FILES );
2008-11-26 22:50:40 +00:00
$s_file = 'step1.php' ;
2009-08-03 04:10:16 +00:00
break ;
# Write the new config vars
2008-11-26 22:50:40 +00:00
case 'step2' :
2009-08-03 04:10:16 +00:00
$files = write_config ( $VAR );
2008-11-26 22:50:40 +00:00
$s_file = 'step2.php' ;
# get the license code
2009-08-03 04:10:16 +00:00
$filename = 'License.txt' ;
if ( is_file ( $filename )) {
$handle = fopen ( $filename , 'r' );
$license_agreement = fread ( $handle , filesize ( $filename ));
2008-11-26 22:50:40 +00:00
fclose ( $handle );
}
2009-08-03 04:10:16 +00:00
require_once ( PATH_MODULES . 'module/module.inc.php' );
$install = new module ;
$modules = $install -> getlist ();
break ;
# Test the database connection & select the modules to install
2008-11-26 22:50:40 +00:00
case 'step3' :
2009-08-03 04:10:16 +00:00
global $C_translate ;
define ( 'DEFAULT_LANGUAGE' , 'english' );
$C_translate = new CORE_translate ;
2008-11-26 22:50:40 +00:00
2009-08-03 04:10:16 +00:00
ob_start ();
$database = test_connection ();
$s_file = 'step3.php' ;
if ( $database === true ) {
require_once ( PATH_MODULES . 'module/module.inc.php' );
$dbinst = new module ;
# We first need to create our core account tables
$modules = $dbinst -> getlist ();
if ( isset ( $modules [ 'core' ])) {
$todo = array_keys ( $modules [ 'core' ]);
$action = true ;
while ( $action ) {
$action = false ;
foreach ( $todo as $index => $module ) {
if ( $dbinst -> install ( array ( 'install_name' => $module , 'module_group' => $VAR [ 'module_group' ], 'no-class' => true , 'no-data' => true , 'type' => 'core' ))) {
unset ( $todo [ $index ]);
$action = true ;
} else {
$action = $action ? $action : false ;
}
}
}
2008-11-26 22:50:40 +00:00
2009-08-03 04:10:16 +00:00
if ( count ( $todo ))
print_r ( 'There was a problem PRE-INSTALLING the CORE modules: [%s] didnt install' , implode ( '|' , $todo ));
}
2008-11-26 22:50:40 +00:00
2009-08-03 04:10:16 +00:00
# Now install our core and base modules
if ( isset ( $modules [ 'base' ]) || isset ( $modules [ 'core' ])) {
$todo = array ();
foreach ( array ( 'base' , 'core' ) as $type )
if ( isset ( $modules [ $type ]))
$todo = array_merge ( $todo , array_keys ( $modules [ $type ]));
$action = true ;
while ( $action ) {
$action = false ;
foreach ( $todo as $index => $module ) {
if ( $dbinst -> install ( array ( 'install_name' => $module , 'module_group' => $VAR [ 'module_group' ], 'no-class' => true , 'no-data' => false , 'type' => 'base' ))) {
unset ( $todo [ $index ]);
$action = true ;
} else {
$action = $action ? $action : false ;
}
}
}
2008-11-26 22:50:40 +00:00
2009-08-03 04:10:16 +00:00
if ( count ( $todo ))
print_r ( 'There was a problem INSTALLING the BASE modules: [%s] didnt install' , implode ( '|' , $todo ));
}
2008-11-26 22:50:40 +00:00
# check for user-defined modules
2009-08-03 04:10:16 +00:00
if ( is_array ( @ $VAR [ 'modules' ])) {
2008-11-26 22:50:40 +00:00
for ( $i = 0 ; $i < count ( $VAR [ 'modules' ]); $i ++ ) {
$n = $VAR [ 'modules' ][ $i ];
2009-08-03 04:10:16 +00:00
if ( ! empty ( $n )) {
$dbinst -> install ( array ( 'install_name' => $n , 'module_group' => $VAR [ 'module_group' ]));
2008-11-26 22:50:40 +00:00
}
}
}
# insert license key and code into setup table
2009-08-03 04:10:16 +00:00
$fields = array ( 'license_key' => @ $VAR [ 'license_key' ], 'license_code' => @ $VAR [ 'license_code' ]);
$db -> Execute ( sqlUpdate ( $db , 'setup' , $fields , '' ));
2008-11-26 22:50:40 +00:00
}
2009-08-03 04:10:16 +00:00
$dbinst -> tables_created = ob_get_flush ();
ob_end_clean ();
break ;
2008-11-26 22:50:40 +00:00
# select the database connection
case 'step4' :
$s_file = 'step4.php' ;
2009-08-03 04:10:16 +00:00
break ;
2008-11-26 22:50:40 +00:00
}
2009-08-03 04:10:16 +00:00
} else {
2008-11-26 22:50:40 +00:00
$do = 'step1' ;
2009-08-03 04:10:16 +00:00
$files = test_files ( $_FILES );
2008-11-26 22:50:40 +00:00
$s_file = 'step1.php' ;
}
# Load the template
include ( 'install/template.inc' );
2009-08-03 04:10:16 +00:00
/**
* Write the new config file
*/
function write_config ( $VAR ) {
# We only write the config file for the main setup.
if ( DEFAULT_SITE != 1 )
return true ;
2008-11-26 22:50:40 +00:00
2009-08-03 04:10:16 +00:00
# Get the config template
if ( $file = fopen ( 'install/config.inc' , 'r' )) {
2008-11-26 22:50:40 +00:00
$data = '' ;
2009-08-03 04:10:16 +00:00
while ( ! feof ( $file ))
2008-11-26 22:50:40 +00:00
$data .= fgetc ( $file );
2009-08-03 04:10:16 +00:00
2008-11-26 22:50:40 +00:00
fclose ( $file );
2009-08-03 04:10:16 +00:00
# Replace the user vars
$data = str_replace ( '%%PATH_AGILE%%' , $VAR [ 'path_agile' ], $data );
$data = str_replace ( '%%AGILE_DB_TYPE%%' , $VAR [ 'db_type' ], $data );
$data = str_replace ( '%%AGILE_DB_HOST%%' , $VAR [ 'db_host' ], $data );
$data = str_replace ( '%%AGILE_DB_DATABASE%%' , $VAR [ 'db_database' ], $data );
$data = str_replace ( '%%AGILE_DB_USERNAME%%' , $VAR [ 'db_username' ], $data );
$data = str_replace ( '%%AGILE_DB_PASSWORD%%' , $VAR [ 'db_password' ], $data );
$data = str_replace ( '%%AGILE_DB_PREFIX%%' , $VAR [ 'db_prefix' ], $data );
if ( ! defined ( 'DEFAULT_SITE' ))
define ( 'DEFAULT_SITE' , 1 );
$data = str_replace ( '%%AGILE_DEFAULT_SITE%%' , DEFAULT_SITE , $data );
# Write the new config file
if ( $file = fopen ( 'config.inc.php' , 'w' )) {
fputs ( $file , $data );
2008-11-26 22:50:40 +00:00
return true ;
2009-08-03 04:10:16 +00:00
2008-11-26 22:50:40 +00:00
} else {
return false ;
}
2009-08-03 04:10:16 +00:00
2008-11-26 22:50:40 +00:00
} else {
return false ;
}
}
2009-08-03 04:10:16 +00:00
/**
* Check file / directory permissions
*/
function test_files ( $_arr_perm ) {
# Check each file/dir
$_perm_err = false ;
2008-11-26 22:50:40 +00:00
2009-08-03 04:10:16 +00:00
# Check the config file
if ( DEFAULT_SITE == 1 && ( ! file_exists ( 'config.inc.php' ) || ! is_writable ( 'config.inc.php' )))
$_perm_err .= sprintf ( '<br/><br/><b>File Permission Problem Detected:</b><br/>The file `<u>%s</u>` must be set set to writable. [ERROR-1]' , 'config.inc.php' );
2008-11-26 22:50:40 +00:00
2009-08-03 04:10:16 +00:00
for ( $i = 0 ; $i < count ( $_arr_perm ); $i ++ ) {
if ( $_arr_perm [ $i ][ 0 ] == 'file' ) {
if ( ! file_exists ( $_arr_perm [ $i ][ 1 ]) || ! is_writable ( $_arr_perm [ $i ][ 1 ]))
$_perm_err .= sprintf ( '<br/><br/><b>File Permission Problem Detected:</b><br/>The file `<u>%s</u>` must be set set to writable. [ERROR-1]' , $_arr_perm [ $i ][ 1 ]);
2008-11-26 22:50:40 +00:00
2009-08-03 04:10:16 +00:00
} else {
if ( ! is_dir ( $_arr_perm [ $i ][ 1 ]) || ! is_writable ( $_arr_perm [ $i ][ 1 ]))
$_perm_err .= sprintf ( '<br/><br/><b>Directory Permission Problem Detected:</b><br/>The directory `<u>%s</u>` must be set set to writable. [ERROR-2]' , $_arr_perm [ $i ][ 1 ]);
2008-11-26 22:50:40 +00:00
}
}
2009-08-03 04:10:16 +00:00
if ( empty ( $_perm_err ))
return true ;
2008-11-26 22:50:40 +00:00
else
2009-08-03 04:10:16 +00:00
return $_perm_err ;
2008-11-26 22:50:40 +00:00
}
2009-08-03 04:10:16 +00:00
/**
* Check database connection
*/
function test_connection () {
if ( AGILE_DB_PREFIX == '' )
return '<br/><br/><b>Database Configration Problem Detected:</b><br/>You must return to Step 1 of the installation process and enter a database prefix.' ;
2008-11-26 22:50:40 +00:00
# Attempt to connect to the database
2009-08-03 04:10:16 +00:00
$db = & DB ();
$err = @ $db -> ErrorMsg ();
if ( ! empty ( $err )) {
if ( preg_match ( '/No Database Selected/' , $err ) || preg_match ( '/Unknown database/' , $err )) {
# Attempt to create the database
$result = $db -> Execute ( sprintf ( 'CREATE DATABASE %s' , AGILE_DB_DATABASE ));
if ( $result === false )
return sprintf ( '<br/><br/><b>Database Problem Detected:</b><br/>' .
'The database named `%s` does not exist on `%s`,' .
'and the user/pass provided does not have permission to create it. [ERROR-3]' , AGILE_DB_DATABASE , AGILE_DB_HOST );
2008-11-26 22:50:40 +00:00
else
$result = true ;
2009-08-03 04:10:16 +00:00
} elseif ( eregi ( 'Access denied' , $err ))
return sprintf ( '<br/><br/><b>Database Problem Detected:</b><br/>' .
'The database `%s` cannot be accessed with the user user/pass provided. [ERROR-4]' , AGILE_DB_DATABASE );
2008-11-26 22:50:40 +00:00
else
2009-08-03 04:10:16 +00:00
return sprintf ( '<br/><br/><b>Database Problem Detected:</b><br/>%s [ERROR-5]' , $err );
2008-11-26 22:50:40 +00:00
}
2009-08-03 04:10:16 +00:00
# Drop table
# $db->Execute(sprintf('DROP TABLE %s',AGILE_DB_DATABASE));
if ( defined ( 'DEFAULT_SITE' ) && DEFAULT_SITE == 1 ) {
# Attempt table creation:
$db = & DB ();
$dict = NewDataDictionary ( $db );
$flds = 'col1 C(32),col2 C(32)' ;
$sqlarray = $dict -> CreateTableSQL ( '1st_delete_this' , $flds );
$result = $db -> Execute ( $sqlarray [ 0 ]);
2008-11-26 22:50:40 +00:00
if ( $result === false ) {
echo $db -> ErrorMsg ();
2009-08-03 04:10:16 +00:00
return '<br/><br/><b>Database Problem Detected:</b><br/>You do not have the correct access to the database; unable to create tables. [ERROR-6]' ;
2008-11-26 22:50:40 +00:00
}
2009-08-03 04:10:16 +00:00
# Attempt index creation:
$db = & DB ();
$idxflds = 'co11,col2' ;
$sqlarray = $dict -> CreateIndexSQL ( 'id' , '1st_delete_this' , $idxflds );
$dict -> ExecuteSQLarray ( $sqlarray );
if ( $result === false )
return '<br/><br/><b>Database Problem Detected:</b><br/>You do not have the correct access to the database; unable to create indexes. [ERROR-7]' ;
2008-11-26 22:50:40 +00:00
2009-08-03 04:10:16 +00:00
# Database tests passed:
2008-11-26 22:50:40 +00:00
$db -> Execute ( 'DROP TABLE 1st_delete_this' );
}
return true ;
2009-08-03 04:10:16 +00:00
}
?>