guildmaster/src_joomla_1.0/install.php

94 lines
1.8 KiB
PHP

<?php
function com_install() {
global $mainframe;
global $database;
global $mosConfig_absolute_path;
// Taken from the upcoming Joomla 1.5
$url = $mosConfig_absolute_path.'/administrator/components/com_guildmaster/install.sql';
if ($raw_data = file_get_contents($url)) {
$queries = splitSQL($raw_data);
if (count($queries) == 0) {
/*
* No queries to process
*/
return 0;
}
/*
* Process each query in the $queries array (split out of sql file).
*/
foreach ($queries as $query) {
$query = trim($query);
if ($query != '' && $query {
0 }
!= '#') {
$database->setQuery($query);
if (!$database->query()) {
echo 'SQL Error - '.$database->stderr(true);
return false;
}
}
}
} else {
echo "Error reading install.sql file!";
return false;
}
}
/**
* Splits contents of a sql file into array of discreet queries
* queries need to be delimited with end of statement marker ';'
* @param string
* @return array
*/
function splitSql($sql) {
$sql = trim($sql);
$sql = preg_replace("/\n\#[^\n]*/", '', "\n".$sql);
$buffer = array ();
$ret = array ();
$in_string = false;
for ($i = 0; $i < strlen($sql) - 1; $i ++) {
if ($sql[$i] == ";" && !$in_string) {
$ret[] = substr($sql, 0, $i);
$sql = substr($sql, $i +1);
$i = 0;
}
if ($in_string && ($sql[$i] == $in_string) && $buffer[1] != "\\") {
$in_string = false;
}
elseif (!$in_string && ($sql[$i] == '"' || $sql[$i] == "'") && (!isset ($buffer[0]) || $buffer[0] != "\\")) {
$in_string = $sql[$i];
}
if (isset ($buffer[1])) {
$buffer[0] = $buffer[1];
}
$buffer[1] = $sql[$i];
}
if (!empty ($sql)) {
$ret[] = $sql;
}
return ($ret);
}
?>