<?php
/**************************************************************************
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.
@Authors: Ryan Thompson(hide@address.com)
***************************************************************************/
class setup {
var $databases = array('mysql'=>array('MySQL','mysql_connect'),
'pgsql'=>array('PostgreSQL','pg_connect'));
var $replace = '';
var $debug_select;
/*!
@function create_table()
@author Ryan Thompson
@abstract Creates Tables for O
@version 0.1
@params $service - Location of service
@params $file - File containing table information
@return TRUE/FALSE
@since 06-11-2003
*/
function create_table($service, $file)
{
GLOBAL $O, $db;
include($O->dir ."/$service/config/tables/$file");
//Seeing as how all filenames are name after the table with which they contain information for
//Thats how we get the name.
$temp = explode('.', $file);
$table_name = $temp[0];
//$tables = $db->tables();
if(in_array($table_name, $db->tables()))
{
return;
} else {
$sql = $db->create_table($table_name, $fields);
$db->query($sql);
return TRUE;
}
}
/*!
@function drop_tables()
@author Ryan Thompson
@abstract Drops all existing tables from database
@version 0.1
@params $table_list - A list of tables to drop
@return TRUE/FALSE
@since 06-11-2003
*/
function drop_tables($table_list)
{
GLOBAL $O, $db;
//We need a workaround to handle PostgreSQL sequences.
//If the table list value is a sequence skip it. It will be dropped when the table that owns it drops.
if($db->db_type == 'pgsql')
{
foreach($table_list AS $key => $value)
{
if(substr($value, '-4', 4) != '_seq')
{
$db->drop_table($value);
}
}
return;
} else {
foreach($table_list AS $key => $value)
{
$db->drop_table($value, $table_list);
}
return;
}
}
/*!
@function config_access()
@author Ryan Thompson
@abstract Sets a session for 30 minutes to edit
@version 0.1
@params $root_url
@params $db_type
@return $session
@since 03-11-2003
*/
function config_access($db)
{
GLOBAL $db;
$id = uniqid('ocfg_');
setcookie('o_config', $id);
$now = time();
$exp = time() + 1800;
$sql = "INSERT INTO o_sessions(session_id, user_id, login_date, expires) VALUES ('$id','0','$now','$exp')";
$db->query($sql);
return;
}
/*!
@function verify_password()
@author Ryan Thompson
@abstract Simply check to see if db password matches
@version 0.1
@params $confirm - Confirmation password
@return TRUE/FALSE
@since 11-10-2003
*/
function verify_password($confirm)
{
if($this->replace['DB_PASS'] == $confirm)
{
return TRUE;
} else {
return FALSE;
}
}
/*!
@function guess_root_dir()
@author Ryan Thompson
@abstract O attempts to determine the root directory it's running in.
@version 0.1
@param $config_dir Current working directory
@param $os Server Operating System
@return $root_dir
@since 10-10-2003
*/
function guess_root_dir($config_dir, $os)
{
//We have to get one directory out. Therefore we have to treat Windows and Linux differently -Ryan
//??Do we need to treat Mac differently to? -Ryan
if($os == 'Linux' || $os == 'Unix')
{
$root_dir = explode('/', $config_dir);
$trash = array_pop($root_dir);
$root_dir = implode('/', $root_dir);
} else {
//I don't know how Windows returns getcwd() Does it create '\\' or just '\' -Ryan
//Till this is figured out this remains untouched
}
return $root_dir;
}
/*!
@function get_support
@author Ryan Thompson
@abstract Determine what O can and can't support
@version 0.1
@params $check -What to check for
@return TRUE/FALSE -At the moment create object variable with select box for debugging
@since 04-11-2003
*/
function get_support($check = 'system')
{
switch($check)
{
case 'system':
$version = '4.1.2';
$this->warning = 'Setup has determined you are using PHP version '. PHP_VERSION;
$this->warning .= '<br />O uses several functions that didn\'t exists prior to version 4.1.2.
There are no guarantees to stability if you choose to continue';
break;
case 'debug':
$version = '4.3.0';
$this->debug_select ="<select name=\"debug\">\n
<option value=\"FALSE\">Disabled</option>\n
<option value=\"TRUE\">Enabled</option>\n
</select>\n
*For development purposes ONLY\n";
$this->warning = 'The debugging option requires at least version 4.3.0';
break;
}
if(version_compare(PHP_VERSION, $version, '>='))
{
return TRUE;
} else {
return FALSE;
}
}
/*!
@function guess_root_url
@author Ryan Thompson
@abstract O attempts to determine the Root URL including subdirectories if they exist
@version 0.1
@return $root_url The url that will point to the ROOT of O on the current system less http://
@since 10-10-2003
*/
function guess_root_url()
{
//Nowhere to we add http:// this is added dynamically incase user runs ssh
$host = $_SERVER['HTTP_HOST'];
//We're going to try to get a subdirectory out of this.
$self = explode('/', $_SERVER['PHP_SELF']);
array_pop($self);
array_pop($self);
$self = implode('/', $self);
return $host .$self;
}
/*!
@function get_supported_dbs
@author Ryan Thompson
@abstract Look for databases supported by current server.
@version 0.1
@return $db_select
@since 10-10-2003
*/
function get_supported_dbs()
{
$db_select = "<select name=\"db_type\">\n";
foreach($this->databases AS $key => $value)
{
//If php was compiled with database functions then we're going to
//assume it's available. Just check for the databases connect function
if(function_exists($value[1]))
{
$db_select .= "<option value=\"$key\">{$value['0']}</option>\n";
}
}
$db_select .= "</select>\n";
return $db_select;
}
/*!
@function download_config
@author Ryan Thompson
@abstract Function to allow user to download config.inc.php
@version 0.1
@return $config_file #may not need
@since 10-10-2003
*/
function download_config($config_file)
{
//Temporary. Find a way to create the file then download it as config.inc.php
$temp_file = tempnam('/tmp', 'o_');
$fp = fopen($temp_file, 'w');
$config_file = str_replace('<','<', $config_file);
fwrite($fp, $config_file);
fclose($fp);
$size = filesize($temp_file);
header("Content-Length: ".$size);
header("Content-disposition: attachment; filename=$temp_file");
header("Content-type: application/octet-stream");
header('Pragma: no-cache');
header('Expires: 0');
$fp = fopen($temp_file, 'r');
fpassthru($fp);
fclose($fp);
unlink($temp_file);
}
/*!
@function set_values()
@author Ryan Thompson
@abstract Sets values for configuration file
@version 0.1
@since 10-10-2003
*/
function set_values($config_values)
{
//These are static. Set them later.
$this->replace['SITENAME'] = $config_values['sitename'];
$this->replace['SITELOGO'] = $config_values['sitelogo'];
$this->replace['SSL'] = 'FALSE';
$this->replace['CONFIG_DIR'] = getcwd();
if(empty($config_values['debug']))
{
$this->replace['DEBUG'] = 'FALSE';
} else {
$this->replace['DEBUG'] = $config_values['debug'];
}
$this->replace['DB_TYPE'] = $config_values['db_type'];
$this->replace['DB_HOST'] = $config_values['db_host'];
$this->replace['DB_NAME'] = $config_values['db_name'];
$this->replace['DB_USER'] = $config_values['db_user'];
$this->replace['DB_PASS'] = $config_values['db_pass'];
$this->replace['CONFIG_ACCESS'] = $config_values['config_access'];
$this->replace['ROOT_URL'] = $config_values['root_url'];
$this->replace['ROOT_DIR'] = $config_values['root_dir'];
$this->replace['TMP_DIR'] = $config_values['tmp_dir'];
return TRUE;
}
/*!
@function create_config()
@author Ryan Thompson
@abstract Replaces data in template with config data
@version 0.1
@return $config_file
@since 11-10-2003
*/
function create_config()
{
$template = $this->retrieve_template();
$template = str_replace('\n','<br />', $template);
foreach($this->replace AS $key=>$value)
{
$template = str_replace("{". $key ."}", $value, $template);
}
/*
We're keeping it in an array right now to make it simpler to edit based
on users selection for retrieving the file. (ie do we need \n or <br />)
for new lines
*/
return $template;
}
/*!
@function retrieve_template()
@author Ryan Thompson
@abstract Replaces data in template with config data
@version 0.1
@return $template
@since 11-10-2003
*/
function retrieve_template()
{
return file($this->replace['ROOT_DIR'] .'/config/config.inc.tpl');
}
/*!
@function retrieve_template()
@author Ryan Thompson
@abstract Replaces data in template with config data
@version 0.1
@return $template
@since 11-10-2003
*/
function db_test_connection()
{
$host = $this->replace['DB_HOST'];
$port = 5432;
$pass = $this->replace['DB_PASS'];
$user = $this->replace['DB_USER'];
$dbname = $this->replace['DB_NAME'];
switch($this->replace['DB_TYPE'])
{
case "mysql":
if(!@mysql_connect($host, $user, $pass))
{
return FALSE;
} else {
return TRUE;
}
break;
case "pgsql":
if(!@pg_connect("host=$host port=$port dbname=$dbname user=$user password=$pass"))
{
return FALSE;
} else {
return TRUE;
}
break;
}
}
/*!
@function write_file()
@author Ryan Thompson
@abstract Writes config.inc.php
@version 0.1
@params $template
@return TRUE
@since 11-10-2003
*/
function write_file($template)
{
if(!$fp = fopen($this->replace['ROOT_DIR'] ."/config.inc.php", 'w'))
{
echo "FAILED";
} else {
fputs($fp, '<?php');
foreach($template AS $value)
{
fputs($fp, $value);
}
fclose($fp);
}
}
/*!
@function text_search()
@author Ryan Thompson
@abstract Looks for text in o_text and adds any that are missing
@version 0.1
@params $text - Array containing id_text and message
@params $service - Service that text belongs to
@params $language - Language of text
@return TRUE
@since 27-10-2003
*/
function text_search($text, $service, $language)
{
GLOBAL $O, $db;
$text = explode('=>', $text);
$id_text = trim($text[0]);
$message = trim($text[1]);
$sql = "SELECT * FROM o_text WHERE id_text='$id_text' AND service='$service' AND language='$language'";
$db->query($sql);
if($db->num_rows == 0)
{
if(strlen($id_text) != 0)
{
$sql = "INSERT INTO o_text (id_text, service, language, messages)
VALUES ('$id_text','$service','$language','$message')";
$db->query($sql);
}
} else {
return TRUE;
}
return;
}
/*!
@function find_changes()
@author Ryan Thompson
@abstract Looks to see what changes need to be made
@version 0.1
@params $table - Table to modify
@params $change - Changes to be made
@return TRUE
@since 27-10-2003
*/
function find_changes($table, $change)
{
GLOBAL $O, $db;
$sql = "SELECT value FROM o_info WHERE name='version'";
$db->query($sql);
$db->fetch_results();
$installed_version = $db->record['value'];
if(!is_array($change))
{
return FALSE;
}
foreach($change AS $key => $value)
{
//Assume current version was properly upgraded.
//If version change was made in is greater then the currently running version then we upgrade it
if($value['version'] > $installed_version)
{
$this->make_change($table, $key, $value);
}
}
}
/*!
@function make_change()
@author Ryan Thompson
@abstract Makes changes to database
@version 0.1
@params $table - Table to modify
@params $field - Field to alter
@params $data - Data required for change
@return TRUE
@since 27-10-2003
*/
function make_change($table, $field, $data)
{
GLOBAL $O, $db;
switch($data['alter'])
{
case 'drop': //Drop existing field from table
$sql = "ALTER TABLE $table DROP $field";
break;
case 'rename': //Rename field with possible change
$sql = "ALTER TABLE $table CHANGE $field {$data['defn']}";
break;
case 'change': //Redefine attributes of field
$sql = "ALTER TABLE $table CHANGE $field $field {$data['defn']}";
break;
case 'add': //Add new field to table
$sql = "ALTER TABLE $table ADD $field {$data['defn']}";
break;
case 'key': //Modify key of field (Either making or drop key)
}
$db->query($sql);
}
/*!
@function get_language_support()
@author Ryan Thompson
@abstract Checks for available and enabled languages
@version 0.1
@params $type
@return $langs
@since 11-10-2003
*/
function get_language_support($type)
{
GLOBAL $O, $db;
switch($type)
{
case 0:
$name = "available_languages[]";
break;
case 1:
$name = "installed_languages[]";
break;
}
$sql = "SELECT * FROM o_languages WHERE enabled='$type'";
$db->query($sql);
$langs = "\n<select name=\"$name\" size=\"5\" multiple=\"multiple\">\n";
while($db->fetch_results())
{
$langs .= "<option value=\"{$db->record['lg_id']}\">{$db->record['language']}</option>\n";
}
$langs .= "</select>\n";
return $langs;
}
}