This repository has been archived on 2024-04-08. You can view files and clone it, but cannot push or open issues or pull requests.
khosb/install/install.inc
2011-05-03 09:49:04 +10:00

329 lines
9.1 KiB
PHP

<?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
*
* 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
*
* @link http://www.agileco.com/
* @copyright 2004-2008 Agileco, LLC.
* @license http://www.agileco.com/agilebill/license1-4.txt
* @author Tony Landis <tony@agileco.com>
* @package AgileBill
* @subpackage Install
*/
/**
* 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
$path = PATH_AGILE;
$path = str_replace('index.php','',$path);
$path = str_replace('\\','/',$path);
# Get the current IP & Domain
if (isset($_SERVER)) {
$site_domain = @$_SERVER['HTTP_HOST'];
$site_ip = @$_SERVER['SERVER_ADDR'];
} else {
$server = getallheaders();
$site_domain = $server['Host'];
$site_ip = false;
}
# req writable files/paths
$_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'])) {
$do = $VAR['do'];
switch ($VAR['do']) {
# Check file permissions:
case 'step1':
$files = test_files($_FILES);
$s_file = 'step1.php';
break;
# Write the new config vars
case 'step2':
$files = write_config($VAR);
$s_file = 'step2.php';
# get the license code
$filename = 'License.txt';
if (is_file($filename)) {
$handle = fopen($filename,'r');
$license_agreement = fread($handle,filesize($filename));
fclose($handle);
}
require_once(PATH_MODULES.'module/module.inc.php');
$install = new module;
$modules = $install->getlist();
break;
# Test the database connection & select the modules to install
case 'step3':
global $C_translate;
define('DEFAULT_LANGUAGE','en');
$C_translate = new CORE_translate;
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;
}
}
}
if (count($todo))
print_r('There was a problem PRE-INSTALLING the CORE modules: [%s] didnt install',implode('|',$todo));
}
# 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;
}
}
}
if (count($todo))
print_r('There was a problem INSTALLING the BASE modules: [%s] didnt install',implode('|',$todo));
}
# check for user-defined modules
if (is_array(@$VAR['modules'])) {
for($i=0; $i<count($VAR['modules']); $i++) {
$n = $VAR['modules'][$i];
if (!empty($n)) {
$dbinst->install(array('install_name'=>$n,'module_group'=>$VAR['module_group']));
}
}
}
# insert license key and code into setup table
$fields = array('license_key'=> @$VAR['license_key'],'license_code'=> @$VAR['license_code']);
$db->Execute(sqlUpdate($db,'setup',$fields,''));
}
$dbinst->tables_created = ob_get_flush();
ob_end_clean();
break;
# select the database connection
case 'step4':
$s_file = 'step4.php';
break;
}
} else {
$do = 'step1';
$files = test_files($_FILES);
$s_file = 'step1.php';
}
# Load the template
include('install/template.inc');
/**
* 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;
# Get the config template
if ($file = fopen('install/config.inc','r')) {
$data = '';
while (! feof($file))
$data .= fgetc($file);
fclose($file);
# 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);
return true;
} else {
return false;
}
} else {
return false;
}
}
/**
* Check file/directory permissions
*/
function test_files($_arr_perm) {
# Check each file/dir
$_perm_err = false;
# 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');
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]);
} 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]);
}
}
if (empty($_perm_err))
return true;
else
return $_perm_err;
}
/**
* 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.';
# Attempt to connect to the database
$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);
else
$result = true;
} 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);
else
return sprintf('<br/><br/><b>Database Problem Detected:</b><br/>%s [ERROR-5]',$err);
}
# 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]);
if ($result === false) {
echo $db->ErrorMsg();
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]';
}
# 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]';
# Database tests passed:
$db->Execute('DROP TABLE 1st_delete_this');
}
return true;
}
?>