ACC SHELL

Path : /srv/www/vhosts/alfa-com/engine/
File Upload :
Current File : /srv/www/vhosts/alfa-com/engine/SQL.php

<?php
	// Author: Jakub Macek, CZ; Copyright: Poski.com s.r.o.; Code is 100% my work. Do not copy.

	class SQL
	{
		public static $connection = null;
		public static $tableNames = array();
	}

	function qconnect($dsn = null, $ignoreFailure = false)
	{
		if (!$dsn)
			$dsn = site()->dsn;
		if (preg_match('~^(.+?)://(.+?):(.+?)@(.+?)/(.+?)$~', $dsn, $matches))
		{
			if ($matches[1] == 'mysql')
			{
				$result = mysql_connect($matches[4], $matches[2], $matches[3]);
				if (!$result && $ignoreFailure)
					return false;
				if (!$result)
					die('database failure');
				mysql_select_db($matches[5], $result);
				mysql_query("SET NAMES 'utf8'", $result);
				if (!SQL::$connection)
					SQL::$connection = $result;
				return ($result ? true : false);
			}
		}
		else
			return false;
	}

	function qe($value)
	{
		if ($value === null)
			return 'NULL';
		return  mysql_real_escape_string($value, SQL::$connection);
	}

	function qq($value)
	{
		return "'".qe($value)."'";
	}

	function qi($identifier)
	{
		return '`' . $identifier . '`';
	}

	function qp($query, $args = array())
	{
		if (function_exists('lll'))
		{
			lll('storage', 'qp()', $query, Log::EVENT_STORAGE);
			if (DEV) lll(strtr($query, "\n", ' '), null, null, Log::EVENT_STORAGE, 'qp');
		}
		foreach (SQL::$tableNames as $k => $v)
			$query = str_replace('##'.$k, $v, $query);
		if (function_exists('site'))
		{
			$query = str_replace('##', site()->tablePrefix, $query);
			$query = str_replace('#L#', L, $query);
			$query = str_replace('#LL#', LL, $query);
		}
		$query = preg_replace('~(:[\w\d-_\.]+)~', '\1:', $query);
		foreach ($args as $k => $v)
			$query = str_replace('::' . $k . ':', qe($v), $query);
		foreach ($args as $k => $v)
			$query = str_replace(':' . $k . ':', qq($v), $query);
		return $query;
	}

	function q($query, $args = array())
	{
		$query = qp($query, $args);
		lll('storage', 'q()', $query, Log::EVENT_STORAGE);
		$r = mysql_query($query, SQL::$connection);
		if ($r === true)
			return true;
		if ($r === false)
			return error(mysql_error(SQL::$connection));
		return $r;
	}

	function qa($query, $args = array(), $index = 'oid')
	{
		$r = q($query, $args);
		if (is_resource($r))
		{
			$result = array();
			while ($row = mysql_fetch_assoc($r))
				if (isset($row[$index]))
					$result[$row[$index]] = $row;
				else
					$result[] = $row;
			mysql_free_result($r);
			return $result;
		}
		else
			return $r;
	}

	function qr($query, $args = array())
	{
		$r = q($query, $args);
		if (is_resource($r))
		{
			$result = mysql_fetch_assoc($r);
			mysql_free_result($r);
			return $result;
		}
		else
			return $r;
	}

	function qc($query, $args = array(), $column = null, $index = 'oid')
	{
		$r = q($query, $args);
		if (is_resource($r))
		{
			$result = array();
			if ($column === null)
			{
				while ($row = mysql_fetch_assoc($r))
					if (isset($row[$index]))
						$result[$row[$index]] = current($row);
					else
						$result[] = current($row);
			}
			else
			{
				while ($row = mysql_fetch_assoc($r))
					if (isset($row[$index]))
						$result[$row[$index]] = $row[$column];
					else
						$result[] = $row[$column];
			}
			mysql_free_result($r);
			return $result;
		}
		else
			return $r;
	}

	function qo($query, $args = array())
	{
		$r = qr($query, $args);
		if (is_array($r))
			return array_shift($r);
		else
			return $r;
	}

	function qupdate()
	{
		$args = func_get_args();
		$table = array_shift($args);
		$result = '';
		foreach ($args as $item)
			$result .= qi($item).' = :'.$item.',';
		return 'UPDATE `##'.$table.'` SET ' . rtrim($result, ',') . ' ';
	}

	function qupdatea($table, $args, $table_prefix = '##')
	{
		$result = '';
		foreach (array_unique($args) as $item)
			$result .= $result .= qi($item).' = :'.$item.',';
		return 'UPDATE '.qi($table_prefix.$table).' SET ' . rtrim($result, ',') . ' ';
	}
	
	function qupdatedirect($table, $fields, $key = 'oid', $table_prefix = '##')
	{
		$query = '';
		foreach ($fields as $k => $v)
			if ($k != $key)
				$query .= qi($k).' = :'.$k.',';
		$query = 'UPDATE '.qi($table_prefix.$table).' SET ' . rtrim($query, ',') . ' WHERE `'.$key.'` = :'.$key;
		return q($query, $fields);
	}

	function qinsert()
	{
		$one = '';
		$two = '';
		$args = func_get_args();
		$table = array_shift($args);
		foreach ($args as $item)
		{
			$one .= qi($item).',';
			$two .= ':'.$item.',';
		}
		return 'INSERT INTO '.qi('##'.$table).'('.rtrim($one, ',').') VALUES ('.rtrim($two, ',').')';
	}

	function qinserta($table, $args, $table_prefix = '##')
	{
		$one = '';
		$two = '';
		foreach (array_unique($args) as $item)
		{
			$one .= qi($item).',';
			$two .= ':'.$item.',';
		}
		return 'INSERT INTO '.qi($table_prefix.$table).'('.rtrim($one, ',').') VALUES ('.rtrim($two, ',').')';
	}

	function qinsertdirect($table, $fields, $table_prefix = '##')
	{
		$query = qinserta($table, array_keys($fields), $table_prefix);
		return q($query, $fields);
	}

?>

ACC SHELL 2018