<?php
/***************************************************************
* Database-2.0.php                                             *
* Copyright ｩ2009 Quoord Systems Ltd. All Rights Reserved.     *
* Created by Dragooon (http://smf-media.com)                   *
****************************************************************
* This file or any content of the file should not be           *
* redistributed in any form of matter. This file is a part of  *
* Tapatalk package and should not be used and distributed      *
* in any form not approved by Quoord Systems Ltd.              *
* http://tapatalk.com | http://taptatalk.com/license.html      *
****************************************************************
* Provides DB abstraction for SMF 2.0                          *
***************************************************************/

if (!defined('SMF'))
	die('Hacking Attempt,,,');

// The main DB class for SMF 2.0
class MobDatabase
{
	// Some variables that we use for storing temporary data
	var $sql;
	var $result;
	var $params = array();

	// Constructor function, does almost nothing. Have it here just for future
	function MobDatabase()
	{
		global $smcFunc;

		$this->smcFunc = &$smcFunc;
	}

	// Performs a query
	function query($sql, $params = array(), $security_override = false)
	{
		global $db_prefix, $user_info, $smcFunc;

		if (empty($sql))
			return false;

		// Set this in global space
		$this->params = $params;
		$this->sql = $sql;

		// Filter out the column
		$this->sql = $this->filter_columns($this->sql);

		// Security override?
		if ($security_override)
			$this->params = 'security_override';

		$this->result = $smcFunc['db_query']('mob_query', $this->sql, $this->params);
	}

	// Returns the data from the result
	function fetch_assoc()
	{
		return $this->smcFunc['db_fetch_assoc']($this->result);
	}

	// Returns the number of rows from the result
	function num_rows()
	{
		return $this->smcFunc['db_num_rows']($this->result);
	}

	// Returns the result in another wau
	function fetch_row()
	{
		return $this->smcFunc['db_fetch_row']($this->result);
	}

	// Return the INSERT ID of the last INSERT
	function insert_id($table = '', $field = '')
	{
		return $this->smcFunc['db_insert_id']($table, $field);
	}

	// Performs a database insert
	function insert($table, $columns, $data, $ignore = false)
	{
		global $context, $db_prefix;

		if (empty($table) || empty($columns) || empty($data))
			return false;

		$insert = $ignore ? 'INSERT IGNORE' : 'INSERT';

		$columns_2 = array();

		// OK Convert them to a proper SMF 2.0 compatible format
		$count = count($columns);
		foreach ($columns as $k => $v)
			$columns_2[$v] = is_int($data[$k]) ? 'int' : 'string';
		$data_2 = array();

		// Convert the data now
		foreach ($data as $v)
			$data_2[] = $v;

		$this->smcFunc['db_insert']($ignore ? 'ignore' : '', $table, $columns_2, $data_2, null);
	}

	// Frees up the result
	function free_result()
	{
		$this->smcFunc['db_free_result']($this->result);
	}

	// This filters the 1.1 columns to 2.0
	function filter_columns($db_string)
	{
		return str_replace(
			array(
				'ID_MSG_MODIFIED', 'ID_MEMBER_FROM', 'ID_MEMBER', 'dateRegistered', 'realName', 'memberName', 'ID_TOPIC', 'ID_MSG', 'ID_FIRST_MSG', 'posterName', 'groupName',
				'ID_GROUP', 'minPosts', 'additionalGroups', 'ID_BOARD', 'ID_CAT', 'boardOrder', 'childLevel', 'catOrder',
				'boardOder', 'isSticky', 'isLocked', 'ID_LAST_MSG', 'posterTime', 'numReplies', 'numViews', 'ID_ATTACH',
				'attachmentType', 'emailAddress', 'passwordSalt', 'lastLogin', 'deletedBySender', 'ID_PM', 'fromName', 'ID_THUMB', 'onlineColor', 'logTime', 'showOnline',
			),
			array(
				'id_msg_modified', 'id_member_from', 'id_member', 'date_registered', 'real_name', 'member_name', 'id_topic', 'id_msg', 'id_first_msg', 'poster_name', 'group_name',
				'id_group', 'min_posts', 'additional_groups', 'id_board', 'id_cat', 'board_order', 'child_level', 'cat_order',
				'board_order', 'is_sticky', 'is_locked', 'id_last_msg', 'poster_time', 'num_replies', 'num_views', 'id_attach',
				'attachment_type', 'email_address', 'password_salt', 'last_login', 'deleted_by_sender', 'id_pm', 'from_name', 'id_thumb', 'online_color', 'log_time', 'show_online'
			), $db_string
		);
	}
}