Finishing re-add, addremove didn't work last time. Integrated with Enano's template engine properly.
<?php
/***********************************************************************
Copyright (C) 2002-2008 PunBB.org
This file is part of PunBB.
PunBB 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.
PunBB is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston,
MA 02111-1307 USA
************************************************************************/
// Make sure we have built in support for MySQL
if (!function_exists('mysql_connect'))
exit('This PHP environment doesn\'t have MySQL support built in. MySQL support is required if you want to use a MySQL database to run this forum. Consult the PHP documentation for further assistance.');
class DBLayer
{
var $prefix;
var $link_id;
var $query_result;
var $saved_queries = array();
var $num_queries = 0;
function DBLayer($db_host, $db_username, $db_password, $db_name, $db_prefix, $p_connect)
{
$this->prefix = $db_prefix;
if ($p_connect)
$this->link_id = @mysql_pconnect($db_host, $db_username, $db_password);
else
$this->link_id = @mysql_connect($db_host, $db_username, $db_password);
if ($this->link_id)
{
if (@mysql_select_db($db_name, $this->link_id))
return $this->link_id;
else
error('Unable to select database. MySQL reported: '.mysql_error(), __FILE__, __LINE__);
}
else
error('Unable to connect to MySQL server. MySQL reported: '.mysql_error(), __FILE__, __LINE__);
// Setup the client-server character set (UTF-8)
if (!defined('PUN_NO_SET_NAMES'))
mysql_query('SET NAMES \'utf8\'', $this->link_id) or error(__FILE__, __LINE__);
}
function start_transaction()
{
return;
}
function end_transaction()
{
return;
}
function query($sql, $unbuffered = false)
{
if (strlen($sql) > 140000)
exit('Insane query. Aborting.');
if (defined('PUN_SHOW_QUERIES'))
$q_start = get_microtime();
if ($unbuffered)
$this->query_result = @mysql_unbuffered_query($sql, $this->link_id);
else
$this->query_result = @mysql_query($sql, $this->link_id);
if ($this->query_result)
{
if (defined('PUN_SHOW_QUERIES'))
$this->saved_queries[] = array($sql, sprintf('%.5f', get_microtime() - $q_start));
++$this->num_queries;
return $this->query_result;
}
else
{
if (defined('PUN_SHOW_QUERIES'))
$this->saved_queries[] = array($sql, 0);
return false;
}
}
function query_build($query, $unbuffered = false)
{
$sql = '';
if (isset($query['SELECT']))
{
$sql = 'SELECT '.$query['SELECT'].' FROM '.(isset($query['PARAMS']['NO_PREFIX']) ? '' : $this->prefix).$query['FROM'];
if (isset($query['JOINS']))
{
foreach ($query['JOINS'] as $cur_join)
$sql .= ' '.key($cur_join).' '.(isset($query['PARAMS']['NO_PREFIX']) ? '' : $this->prefix).current($cur_join).' ON '.$cur_join['ON'];
}
if (!empty($query['WHERE']))
$sql .= ' WHERE '.$query['WHERE'];
if (!empty($query['GROUP BY']))
$sql .= ' GROUP BY '.$query['GROUP BY'];
if (!empty($query['HAVING']))
$sql .= ' HAVING '.$query['HAVING'];
if (!empty($query['ORDER BY']))
$sql .= ' ORDER BY '.$query['ORDER BY'];
if (!empty($query['LIMIT']))
$sql .= ' LIMIT '.$query['LIMIT'];
}
else if (isset($query['INSERT']))
{
$sql = 'INSERT INTO '.(isset($query['PARAMS']['NO_PREFIX']) ? '' : $this->prefix).$query['INTO'];
if (!empty($query['INSERT']))
$sql .= ' ('.$query['INSERT'].')';
$sql .= ' VALUES('.$query['VALUES'].')';
}
else if (isset($query['UPDATE']))
{
$query['UPDATE'] = (isset($query['PARAMS']['NO_PREFIX']) ? '' : $this->prefix).$query['UPDATE'];
if (isset($query['PARAMS']['LOW_PRIORITY']))
$query['UPDATE'] = 'LOW_PRIORITY '.$query['UPDATE'];
$sql = 'UPDATE '.$query['UPDATE'].' SET '.$query['SET'];
if (!empty($query['WHERE']))
$sql .= ' WHERE '.$query['WHERE'];
}
else if (isset($query['DELETE']))
{
$sql = 'DELETE FROM '.(isset($query['PARAMS']['NO_PREFIX']) ? '' : $this->prefix).$query['DELETE'];
if (!empty($query['WHERE']))
$sql .= ' WHERE '.$query['WHERE'];
}
else if (isset($query['REPLACE']))
{
$sql = 'REPLACE INTO '.(isset($query['PARAMS']['NO_PREFIX']) ? '' : $this->prefix).$query['INTO'];
if (!empty($query['REPLACE']))
$sql .= ' ('.$query['REPLACE'].')';
$sql .= ' VALUES('.$query['VALUES'].')';
}
return $this->query($sql, $unbuffered);
}
function result($query_id = 0, $row = 0)
{
return ($query_id) ? @mysql_result($query_id, $row) : false;
}
function fetch_assoc($query_id = 0)
{
return ($query_id) ? @mysql_fetch_assoc($query_id) : false;
}
function fetch_row($query_id = 0)
{
return ($query_id) ? @mysql_fetch_row($query_id) : false;
}
function num_rows($query_id = 0)
{
return ($query_id) ? @mysql_num_rows($query_id) : false;
}
function affected_rows()
{
return ($this->link_id) ? @mysql_affected_rows($this->link_id) : false;
}
function insert_id()
{
return ($this->link_id) ? @mysql_insert_id($this->link_id) : false;
}
function get_num_queries()
{
return $this->num_queries;
}
function get_saved_queries()
{
return $this->saved_queries;
}
function free_result($query_id = false)
{
return ($query_id) ? @mysql_free_result($query_id) : false;
}
function escape($str)
{
if (is_array($str))
return '';
else if (function_exists('mysql_real_escape_string'))
return mysql_real_escape_string($str, $this->link_id);
else
return mysql_escape_string($str);
}
function error()
{
$result['error_sql'] = @current(@end($this->saved_queries));
$result['error_no'] = @mysql_errno($this->link_id);
$result['error_msg'] = @mysql_error($this->link_id);
return $result;
}
function close()
{
if ($this->link_id)
{
if ($this->query_result)
@mysql_free_result($this->query_result);
return @mysql_close($this->link_id);
}
else
return false;
}
function table_exists($table_name)
{
$result = $this->query('SHOW TABLES LIKE \''.$this->escape($table_name).'\'');
return $this->num_rows($result) > 0;
}
function field_exists($table_name, $field_name)
{
$result = $this->query('SHOW COLUMNS FROM '.$table_name.' LIKE \''.$this->escape($field_name).'\'');
return $this->num_rows($result) > 0;
}
function index_exists($table_name, $index_name)
{
$exists = false;
$result = $this->query('SHOW INDEX FROM '.$table_name);
while ($cur_index = $this->fetch_assoc($result))
{
if ($cur_index['Key_name'] == $index_name)
{
$exists = true;
break;
}
}
return $exists;
}
function add_field($table_name, $field_name, $field_type, $allow_null, $default_value = null, $after_field = null)
{
if ($this->field_exists($table_name, $field_name))
return;
if ($default_value !== null && !is_int($default_value) && !is_float($default_value))
$default_value = '\''.$this->escape($default_value).'\'';
$this->query('ALTER TABLE '.$table_name.' ADD '.$field_name.' '.$field_type.($allow_null ? ' ' : ' NOT NULL').($default_value !== null ? ' DEFAULT '.$default_value : ' ').($after_field != null ? ' AFTER '.$after_field : '')) or error(__FILE__, __LINE__);
}
function drop_field($table_name, $field_name)
{
if (!$this->field_exists($table_name, $field_name))
return;
$this->query('ALTER TABLE '.$table_name.' DROP '.$field_name) or error(__FILE__, __LINE__);
}
}