<?php
 /**
 * @version $Id$
 * @package    Contact_Enhanced
 * @subpackage Install
 * @author     Douglas Machado {@link http://ideal.fok.com.br}
 * @author     Created on 04-Dec-09
 * @license		GNU/GPL, see license.txt
 * Contact Enhanced  is free software. This version may have been modified pursuant
 * to the GNU General Public License, and as distributed it includes or
 * is derivative of works licensed under the GNU General Public License or
 * other free or open source software licenses. */
@error_reporting(E_ALL);

// Help get past php timeouts if we made it that far
// Joomla 1.5 installer can be very slow and this helps avoid timeouts
@set_time_limit(300);
$kn_maxTime = @ini_get('max_execution_time');

$maxMem = trim(@ini_get('memory_limit'));
if ($maxMem) {
	$unit = strtolower($maxMem{strlen($maxMem) - 1});
	switch($unit) {
		case 'g':
			$maxMem	*=	1024;
		case 'm':
			$maxMem	*=	1024;
		case 'k':
			$maxMem	*=	1024;
	}
	if ($maxMem < 16000000) {
		@ini_set('memory_limit', '16M');
	}
	if ($maxMem < 32000000) {
		@ini_set('memory_limit', '32M');
	}
	if ($maxMem < 48000000) {
		@ini_set('memory_limit', '48M');
	}
}
ignore_user_abort(true);

/**
 * Check is a table field exists
 * @param string	Table name
 * @param string	Table field
 * @return bool
 */
function tableFieldExists($tblval, $tblfield){
	$database 	= & JFactory::getDBO();
	$database->setQuery( 'SHOW FIELDS FROM ' . $tblval );
	$fields = $database->loadResultArray();
	return in_array($tblfield,$fields);
}

/**
 * Check is a table exists
 * @param string	Table name
 * @return bool
 */
function tableExists($tblval){
	$database 	= & JFactory::getDBO();
	$tables		= $database->getTableList();
	return in_array($tblval,$tables);
}

/**
 * @param string File name
 */
function populate_db( $sqlfile='install.sql') {
	$errors		= array();
	$database 	= & JFactory::getDBO();
	$path		= JPATH_BASE.DS."components".DS."com_contact_enhanced".DS.'install'.DS;
	
	if(!file_exists($path . $sqlfile)){
		$errors[]	= JText::_('SQL FILE NOT FOUND!');
	}
	
	$mqr = @get_magic_quotes_runtime();
	@set_magic_quotes_runtime(0);
	$query = fread( fopen( $path . $sqlfile, 'r' ), filesize( $path . $sqlfile ) );
	@set_magic_quotes_runtime($mqr);
	$pieces  = split_sql($query);

	for ($i=0; $i<count($pieces); $i++) {
		$pieces[$i] = trim($pieces[$i]);
		if(!empty($pieces[$i]) && $pieces[$i] != "#") {
			$database->setQuery( $pieces[$i] );
			if (!$database->query()) {
				$errors[] = array ( $database->getErrorMsg(), $pieces[$i] );
			}
		}
	}
	return $errors;
}

/**
 * @param string
 */
function split_sql($sql) {
	$sql = trim($sql);
//	$sql = preg_replace("\n#[^\n]*\n", "\n", $sql); // was ereg_replace() //remove comments

	$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);
}