From 88d5d2af84cde0ce1107551fd2a61455e493b7c8 Mon Sep 17 00:00:00 2001 From: thirdid Date: Wed, 27 Jan 2010 03:40:33 +0000 Subject: Changing directory 'includes' -> 'include' git-svn-id: http://svn.3rd-infantry-division.org/testing/branches/Bayonet CMS v2.1@465 c5b2fb0a-d05d-0410-98c8-828840a80ff6 --- include/classes.php | 131 ++++++++++ include/debug.php | 225 ++++++++++++++++++ include/functions.php | 647 ++++++++++++++++++++++++++++++++++++++++++++++++++ include/sql.class.php | 167 +++++++++++++ 4 files changed, 1170 insertions(+) create mode 100644 include/classes.php create mode 100644 include/debug.php create mode 100644 include/functions.php create mode 100644 include/sql.class.php (limited to 'include') diff --git a/include/classes.php b/include/classes.php new file mode 100644 index 0000000..0042c95 --- /dev/null +++ b/include/classes.php @@ -0,0 +1,131 @@ +request = $_POST; + echo "
\n"; + } + + public function __destruct() + { + echo "
\n"; + } + + function getKeyStates($keys) + { + $good = array(); + $bad = array(); + + if(!is_array($keys)) + return array(); + + foreach($keys as $key => $value) + { + if(!empty($value)) + $good[$key] = $value; + else + $bad[$key] = $value; + } + + $data = array( + 'set' => $good, + 'unset' => $bad + ); + + return $data; + } + + function verify($submit_key) + { + return $this->verifySubmit($submit_key); + } + + function verifySubmit($submit_key) + { + return isset($this->request[$submit_key]) ? true : false; + } + + function button($extern_name, $value, $text = "Button") + { + echo "\n"; + } + + function submitButton($extern_name, $value = "Submit") + { + echo "\n"; + } + + function reset($value = "Reset") + { + echo "\n"; + } + + function textField($extern_name, $value = NULL, $isPassword = false, $size = NULL, $max = NULL) + { + $type = 'text'; + if($isPassword) $type = 'password'; + + $value = filter_var($value, FILTER_SANITIZE_STRING); + echo "\n"; + } + + function radioButton($extern_name, $value, $isChecked = false) + { + if($isChecked) + { + echo "\n"; + } + else + { + echo "\n"; + } + } + + function checkBox($extern_name, $value, $isChecked = false) + { + if($isChecked) + { + echo "\n"; + } + else + { + echo "\n"; + } + } + + function dropDown($extern_name, $values = array('None'), $select = NULL) + { + $selectIterator = 1; + + echo "\n"; + } + + function textArea($extern_name, $rows = 10, $cols = 30, $value = NULL) + { + $value = filter_var($value, FILTER_SANITIZE_STRING); + echo "\n"; + } +} + +?> \ No newline at end of file diff --git a/include/debug.php b/include/debug.php new file mode 100644 index 0000000..29f4195 --- /dev/null +++ b/include/debug.php @@ -0,0 +1,225 @@ +. + */ + +define("RUDI_DEBUG",true); +define("RUDI_DEBUG_LEVEL",true); +define('NO_REPEAT',false); +define('REPEAT',true); +define('FORCE', true); + +static $last_message = NULL; +static $last_message_count = 0; + +static $log_message_last = NULL; +static $log_message_queue = array(); +static $log_message_pos = 0; + +function decho($message, $force = false) +{ + global $log_message_last, $log_message_queue, $log_message_pos, $config; + + if($force == false) + { + if($config['debug']['enabled'] == false || + $config['debug']['show_messages'] == false) return; + } + + + date_default_timezone_set($config['logs']['timezone']); + $timestamp = date('H:i:s T'); + if(!is_array($message) && !is_object($message)) + $message = "[$timestamp]: $message"; + else + { + $message = $message; + } + + //if(count($log_message_queue) >= 100) + // array_pop($log_message_queue); + + array_push($log_message_queue, $message); + + $log_message_pos++; +} + +function queuePrint($obj, $force = false) +{ + global $config; + + if($force == false) + { + if($config['debug']['enabled'] == false || + $config['debug']['show_messages'] == false) return; + } + + if(is_array($obj)) + { + $array_dump = print_r($obj,true); + if(empty($obj)) + { + echo "Array was empty
\n"; + return; + } + echo "
" . $array_dump . "

\n"; + //WriteLog($array_dump,BAYONET_LOG_INFO); + } + elseif(is_object($obj)) + { + ob_start(); + var_dump($obj); + $obj_dump = ob_get_contents(); + ob_end_clean(); + //htmlentities($obj_dump,ENT_QUOTES); + //WriteLog($obj_dump,BAYONET_LOG_INFO); + echo "
" . $obj_dump . "

\n"; + } + elseif(is_string($obj)) + { + $message = wordwrap($obj,80,'
'); + echo $obj . "
\n"; + //WriteLog($message,BAYONET_LOG_INFO); + } + else + { + echo "queuePrint: Invalid type of " . gettype($obj) . "
\n"; + } +} + +function logQueueFlush($force = false) +{ + error_reporting(0); + global $log_message_queue, $config; + + if($force == false) + { + if($config['debug']['show_messages'] == false) return; + } + + $messageCount = 0; + static $log_message_last_count = 0; + static $next = false; + + OpenContent(); + echo "
Bayonet Debug Messages
"; + echo "
"; + + if(count($log_message_queue) < 1) + { + echo "

No messages

"; + echo "
"; + return false; + } + + echo "" . count($log_message_queue) . " messages received.

\n"; + foreach($log_message_queue as $message) + { + if($message != $log_message_queue[$messageCount - 1]) + { + if($force) + queuePrint($message, true); + else + queuePrint($message); + } + elseif($message == $log_message_queue[$messageCount - 1]) + { + $log_message_last_count++; + if($config['debug']['repeat_messages'] == true) + { + //echo "{$messageCount}: $message
\n"; + if($force) + queuePrint($message,true); + else + queuePrint($message); + } + if($config['debug']['repeat_messages'] == false) + { + if($log_message_queue[$messageCount + 1] != $message) + { + $next = true; + } + } + + } + + if($next == true) + { + queuePrint("Last message recieved $log_message_last_count times
\n"); + $log_message_last_count = 0; + $next = false; + } + + ++$messageCount; + } + + echo ""; + CloseContent(); + error_reporting(1); +} + +/* +function decho($message, $flag = REPEAT) +{ + global $last_message, $last_message_count; + if($last_message == $message) + { + if($flag == NO_REPEAT) + ++$last_message_count; + else + $last_message_count = $last_message_count; + return; + } + elseif($last_message != $message) + { + if($last_message_count > 0 && !is_array($last_message) && !is_object($last_message)) + { + WriteLog("Receieved previous message $last_message_count times\n",BAYONET_LOG_INFO); + } + else + { + if(is_array($message)) + { + $array_dump = print_r($message,true); + WriteLog($array_dump,BAYONET_LOG_INFO); + } + elseif(is_object($message)) + { + ob_start(); + var_dump($message); + $obj_dump = ob_get_contents(); + ob_end_clean(); + //htmlentities($obj_dump,ENT_QUOTES); + WriteLog($obj_dump,BAYONET_LOG_INFO); + } + elseif(is_string($message)) + { + $message = wordwrap($message,80,'
'); + WriteLog($message,BAYONET_LOG_INFO); + } + } + + $last_message_count = 0; + } + + $last_message = $message; + +} +*/ + + +?> \ No newline at end of file diff --git a/include/functions.php b/include/functions.php new file mode 100644 index 0000000..3f1c3d2 --- /dev/null +++ b/include/functions.php @@ -0,0 +1,647 @@ +. + */ + +/** + * bbcode_format() + * + * Modified public domain code from www.phpit.net + * + * @param mixed $str + * @return + */ + +include_once 'classes.php'; + +function bbcode_format ($str) +{ + $str = htmlentities($str); + $str = strip_tags($str); + //$str = wordwrap($str,100,"\n",true); + + $simple_search = array( + '/\[b\](.*?)\[\/b\]/is', + '/\[i\](.*?)\[\/i\]/is', + '/\[u\](.*?)\[\/u\]/is', + '/\[s\](.+?)\[\/s\]/is', + '/\[hr\]/is', + '/\[pi\](.*?)\[\/pi\]/is', + '/\[title\](.*?)\[\/title\]/is', + '/\[article\](.*?)\[\/article\]/is', + '/\[section\](.*?)\[\/section\]/is', + '/\[code\](.*?)\[\/code\]/is', + '/\[quote\](.*?)\[\/quote\]/is', + '/\[quote\=(.*?)\](.*?)\[\/quote\]/is', + '/\[url\](.*?)\[\/url\]/is', + '/\[url\=(.*?)\](.*?)\[\/url\]/is', + '/\[align\=(.*?)\](.*?)\[\/align\]/is', + '/\[size\=(.*?)\](.*?)\[\/size\]/is', + '/\[img\=(.*?)\](.*?)\[\/img\]/is', + '/\[img align\=(.+?)\](.+?)\[\/img\]/is', + '/\[mail\](.*?)\[\/mail\]/is', + '/\[mail\=(.*?)\](.*?)\[\/mail\]/is', + '/\[list\](.*?)\[\/list\]/is' + ); + + $simple_replace = array( + '$1', + '$1', + '$1', + '$1', + '
', + '

$1

', + '

$1

', + '

$1

', + '

$1

', + '
$1
', + '
$1
', + '$1 wrote:
$2
', + '$1', + '$2', + '
$2', + '$2', + '$2', + '', + '$1', + '$2', + '
  • $1
  • ' + ); + + $str = preg_replace ($simple_search, $simple_replace, $str); + $str = nl2br($str); + return $str; +} + +/** + * BBcode 2 HTML was written by WAY2WEB.net + */ +function BBCode($Text) + { + // Replace any html brackets with HTML Entities to prevent executing HTML or script + // Don't use strip_tags here because it breaks [url] search by replacing & with amp + $Text = str_replace("<", "<", $Text); + $Text = str_replace(">", ">", $Text); + + // Convert new line chars to html
    tags + $Text = nl2br($Text); + + // Set up the parameters for a URL search string + $URLSearchString = " a-zA-Z0-9\:\/\-\?\&\.\=\_\~\#\'"; + // Set up the parameters for a MAIL search string + $MAILSearchString = $URLSearchString . " a-zA-Z0-9\.@"; + + // Perform URL Search + $Text = preg_replace("/\[url\]([$URLSearchString]*)\[\/url\]/", '$1', $Text); + $Text = preg_replace("(\[url\=([$URLSearchString]*)\](.+?)\[/url\])", '$2', $Text); + //$Text = preg_replace("(\[url\=([$URLSearchString]*)\]([$URLSearchString]*)\[/url\])", '$2', $Text); + + // Perform MAIL Search + $Text = preg_replace("(\[mail\]([$MAILSearchString]*)\[/mail\])", '$1', $Text); + $Text = preg_replace("/\[mail\=([$MAILSearchString]*)\](.+?)\[\/mail\]/", '$2', $Text); + + // Check for bold text + $Text = preg_replace("(\[b\](.+?)\[\/b])is",'$1',$Text); + + // Check for Italics text + $Text = preg_replace("(\[i\](.+?)\[\/i\])is",'$1',$Text); + + // Check for Underline text + $Text = preg_replace("(\[u\](.+?)\[\/u\])is",'$1',$Text); + + // Check for strike-through text + $Text = preg_replace("(\[s\](.+?)\[\/s\])is",'$1',$Text); + + // Check for over-line text + $Text = preg_replace("(\[o\](.+?)\[\/o\])is",'$1',$Text); + + // Check for colored text + $Text = preg_replace("(\[color=(.+?)\](.+?)\[\/color\])is","$2",$Text); + + // Check for sized text + $Text = preg_replace("(\[size=(.+?)\](.+?)\[\/size\])is","$2",$Text); + + // Check for list text + $Text = preg_replace("/\[list\](.+?)\[\/list\]/is", '' ,$Text); + $Text = preg_replace("/\[list=1\](.+?)\[\/list\]/is", '' ,$Text); + $Text = preg_replace("/\[list=i\](.+?)\[\/list\]/s", '' ,$Text); + $Text = preg_replace("/\[list=I\](.+?)\[\/list\]/s", '' ,$Text); + $Text = preg_replace("/\[list=a\](.+?)\[\/list\]/s", '' ,$Text); + $Text = preg_replace("/\[list=A\](.+?)\[\/list\]/s", '' ,$Text); + $Text = str_replace("[*]", "
  • ", $Text); + + // Check for font change text + $Text = preg_replace("(\[font=(.+?)\](.+?)\[\/font\])","$2",$Text); + + // Declare the format for [code] layout + $CodeLayout = ' + + + + + + +
    Code:
    $1
    '; + // Check for [code] text + $Text = preg_replace("/\[code\](.+?)\[\/code\]/is","$CodeLayout", $Text); + // Declare the format for [php] layout + $phpLayout = ' + + + + + + +
    Code:
    $1
    '; + // Check for [php] text + $Text = preg_replace("/\[php\](.+?)\[\/php\]/is",$phpLayout, $Text); + + // Declare the format for [quote] layout + $QuoteLayout = ' + + + + + + +
    Quote:
    $1
    '; + + // Check for [quote] text + $Text = preg_replace("/\[quote\](.+?)\[\/quote\]/is","$QuoteLayout", $Text); + + // Images + // [img]pathtoimage[/img] + $Text = preg_replace("/\[img\](.+?)\[\/img\]/", '', $Text); + + //[img=align]image source[/img] + $Text = preg_replace("(\[img align\=(.+?)\](.+?)\[\/img\])is","",$Text); + + // [img=widthxheight]image source[/img] + $Text = preg_replace("/\[img\=([0-9]*)x([0-9]*)\](.+?)\[\/img\]/", '', $Text); + + // Alignment + // [align=type]text[/align] + $Text = preg_replace("(\[align=(.+?)\](.+?)\[\/align\])is","
    $2
    ",$Text); + + return $Text; + } + +function articleHeading($text){ + + // Set the content-type + header('Content-type: image/png'); + + //$text = $_GET['text']; + + $im = imagecreatefrompng('images/news_header.png'); // open image + imagealphablending($im, true); // setting alpha blending on + imagesavealpha($im, true); // save alphablending setting (important) + + // Create some colors + $black = imagecolorallocate($im, 0, 0, 0); + + + // Replace path by your own font path + //$font = 'TrajanPro-Regular.otf'; + //$font = 'TrajanPro-Bold.otf'; + $font = 'BrushScriptStd.otf'; + //$font = 'TRATS__.TTF'; + //$text = strtoupper($text); + + // Add the text + imagettftext($im, 18, 0, 0, 17, $black, $font, $text); + + // Using imagepng() results in clearer text compared with imagejpeg() + imagepng($im); + imagedestroy($im); +} + +function LinkList($array) +{ + if(!is_array($array)) + { + ReportError("List was not an array"); + return; + } + + echo "
      "; + foreach($array as $text => $link) + { + echo "
    • $text
    • "; + } + echo "
    "; +} + +/** + * LinkModule() + * + * Helper function to link to Bayonet modules. + * + * @param mixed $module_name + * @param mixed $link_name + * @return + */ +function LinkModule($module_name,$args = NULL,$link_name) +{ + + return "{$link_name}"; +} + +/** + * LinkModuleFile() + * + * Helper function to link to Bayonet internal module files. + * + * @param mixed $module_name + * @param mixed $link_name + * @return + */ + function LinkModuleFile($module_name, $file_name, $link_name) + { + return "{$link_name}"; + } + +/** + * LinkPage() + * + * Helper function to link to Bayonet pages. + * + * @param mixed $page_id + * @param mixed $page_name + * @return + */ +function LinkPage($page_id,$page_name) +{ + return "{$page_name}"; +} + +/** + * LinkInternal() + * + * Helper function to link to a relative Bayonet path. + * + * @param mixed $name + * @param string $rel_path + * @param string $file + * @return + */ +function LinkInternal($name,$file,$rel_path = "./") +{ + return "{$name}"; +} + +if(!defined("CALLED_FROM_ADMIN")) +{ + /** + * OpenTable() + * Begins a Bayonet site table. + * @return + */ + function OpenTable($width = "100%") + { + //width="100%" is important. Otherwise all of our tables will be text width. + echo "\n"; + } + + /** + * CloseTable() + * Closes a Bayonet site table. + * @return + */ + function CloseTable() + { + echo "
    "; + } + + /** + * OpenContent() + * Begins a Bayonet site table. + * @return + */ + function OpenContent() + { + echo "
    "; + echo "
    "; + } + + /** + * CloseContent() + * Closes a Bayonet site table. + * @return + */ + function CloseContent() + { + echo "
    "; + echo "
    "; + } +} + +function OpenBlock($title = 'New Block') +{ + OpenContent(); + echo "
    {$title}
    "; + echo "
    "; +} + +function CloseBlock() +{ + echo "
    "; + CloseContent(); +} + +static $error_stack_messages = array(); //global stack of errors accumulated throughout execution +function push_error_stack($message) +{ + global $error_stack_messages; + + if(count($error_stack_messages) >= 100) + array_pop($error_stack_messages); + + array_push($error_stack_messages, $message); +} + +function fatal_error_handler($buffer) { + if (ereg("(error:)(.+)(/","",$regs[2]); + //ReportError($err); + } + return $buffer; +} + +function handle_error ($errno, $errstr, $errfile, $errline) +{ + //decho("Warning: $errfile:$errline, $errstr"); + push_error_stack("Error Number: $errno
    Error: $errstr
    In File: $errfile
    Line: $errline"); + if($errno == FATAL || $errno == ERROR){ + push_error_stack($errstr); + ob_end_flush(); + exit(0); + } +} + +/** + * ReportError() + * + * This function should be called in the event that an error has occured. + * + * @param mixed $message - automatically logged + * @return + */ +function ReportError($message) +{ + //WriteLog($message,BAYONET_LOG_ERROR); + OpenContent(); + echo "
    Error Message
    {$message}
    "; + CloseContent(); +} + +/** + * ReportHack() + * + * This function should be called in the event that we are confirming + * a hacking attempt. + * + * @param mixed $message - automatically logged + * @return void + */ +function ReportHack($message) +{ + //WriteLog($message,BAYONET_LOG_HACK); + OpenContent(); + echo "
    Hacking Attempt
    {$message}
    "; + CloseContent(); +} + +/** + * PageRedirect() + * + * performs an http redirect + * + * @param $delay + * @param $link + */ +function PageRedirect($delay, $link) +{ + echo ""; +} + + +/** + * array_dump() + * + * Useful against $_POST and $_GET variables, for dumping data to + * a log file, or to stdout. The return value is a single string, + * with each array key => value pair delimited by a character. + * The default character is a pipe -> '|'. It is assumed that if + * wanted to have a newline character inserted, just replace $spacer + * with '\n'. + * + * @param mixed $array + * @param string $spacer + * @return + */ +function array_dump($array, $spacer = '|') +{ + $retval = NULL; + foreach($array as $challenge => $answer) + { + $retval .= " $challenge => $answer " . $spacer; + } + + return $retval; +} + +/** + * WriteLog() + * + * This function can be accessed directly, however, anything that is + * passed to ReportError() or ReportHack(), or decho() will be logged in their + * appropriate log file. + * + * Change made: added checks to see if the log files exist before opening + * + * @param mixed $message + * @param mixed $flag + * @return + */ +define('BAYONET_LOG_HACK','bayonet_log_hack'); +define('BAYONET_LOG_ERROR','bayonet_log_error'); +define('BAYONET_LOG_WARN','bayonet_log_warn'); +define('BAYONET_LOG_INFO','bayonet_log_info'); + +function WriteLogBayonet($message,$flag) +{ + global $config; + $enabled = $config['logs']['enabled']; + + if(!$enabled) + { + return false; + } + + $dir = $config['logs']['dir']; + date_default_timezone_set($config['logs']['timezone']); + $varstr = array_dump($_GET); + $type = NULL; + $ip = $_SERVER['REMOTE_ADDR']; + $hostname = gethostbyaddr($ip); + $executed = $_SERVER['PHP_SELF']; + $timestamp = date('Y-M-d H:i:s T'); + + + $message = str_replace("\n",'',$message); + $message = str_replace("
    ",'',$message); + + switch($flag) + { + case BAYONET_LOG_HACK: + if(!file_exists($dir.'hacks.log')){ + break; + } + $fp = fopen($dir.'hacks.log','a'); + $type = 'HACK'; + $full_message = "TIMESTAMP: {$timestamp}\n\t\tIP: {$ip}\n\t\tHOSTNAME: {$hostname}\n\t\tACTION: {$type} of {$executed}\n\t\tDEFERRAL: {$message}\n\t\tVAR: ({$varstr})\n\n"; + break; + case BAYONET_LOG_ERROR: + if(!file_exists($dir.'error.log')){ + break; + } + $fp = fopen($dir.'error.log','a'); + $type = 'ERROR'; + $full_message = "({$timestamp}) - {$type} - {$message} - ({$varstr})\n"; + break; + case BAYONET_LOG_WARN: + if(!file_exists($dir.'warn.log')){ + break; + } + $fp = fopen($dir.'warn.log','a'); + $type = 'WARN'; + $full_message = "({$timestamp}) - {$type} - {$message} - ({$varstr})\n"; + break; + case BAYONET_LOG_INFO: + if(!file_exists($dir.'info.log')){ + break; + } + $fp = fopen($dir.'info.log','a'); + $type = 'INFO'; + $full_message = "({$timestamp}) - {$type} - {$message}\n"; + break; + default: + echo 'To log something, you need to define a log to write to.
    '; + return; + } + if(file_exists($fp)){ + fwrite($fp,$full_message); + fclose($fp); + }else{ + //echo "could not write to file because file does not exist.
    "; + } +} + +/** + * UnderConstruction() + * + * Displays a site-wide message across the page header. + * + * @param mixed $message + * @param mixed $flag Acceptable flags are BAYONET_SITE, and BAYONET_SECTION + * @return + */ +define('BAYONET_SITE','bayonet_site'); +define('BAYONET_SECTION','bayonet_section'); +function UnderConstruction($message = NULL, $flag = BAYONET_SITE) +{ + $timestamp = date("Y-M-d h:m:s"); + OpenTable(); + switch($flag) + { + case BAYONET_SITE: + echo "Site is currently under construction : $timestamp"; + break; + case BAYONET_SECTION: + echo "Section currently under construction : $timestamp"; + } + + if(!is_null($message)) + { + echo "$message"; + } + + CloseTable(); + echo "
    "; +} + +/** + * valid_result() + * + * Determine if a mysqli result is valid. + * Can be used on normal objects to check if they are empty. + * + * @param mixed $p_result + * @return + */ +function valid_result($p_result) +{ + if(is_object($p_result) && count($p_result) <= 1) + return false; + else + return true; +} + +/** + * GetBlocks() + * + * Includes all directories listed in blocks/ and uses the bayonet_blocks + * MySQL table to determine the order of the blocks displayed. + * + * @return + */ + +define('BLOCK_LEFT', 0); +define('BLOCK_RIGHT', 1); + +function GetBlocks($position = BLOCK_LEFT) +{ + global $config; + global $db; + + $query = sprintf("SELECT block_id, active, weight, position, dir_name, title FROM bayonet_blocks WHERE active = 1 AND position = %d ORDER BY weight", (int)$position); + $result = $db->Query($query); + + + + $blocks = $db->Fetch($result); + if(empty($blocks)) return; + + foreach($blocks as $block) + { + $load = 'blocks/'.$block['dir_name'].'/index.php'; + if(file_exists($load)) + { + OpenBlock($block['title']); + include_once $load; + CloseBlock(); + decho("'{$block['dir_name']}' block loaded"); + } + else + { + ReportError("Failed to load block, '{$block['dir_name']}'. Check block config."); + } + if($config['blocks']['spacer']) echo "
    "; + } +} +?> \ No newline at end of file diff --git a/include/sql.class.php b/include/sql.class.php new file mode 100644 index 0000000..c32de63 --- /dev/null +++ b/include/sql.class.php @@ -0,0 +1,167 @@ +. + */ + +static $db_queries = 0; +static $db_connections = 0; +static $db_frees = 0; +static $db_fetches = 0; + +class Bayonet_SQL +{ + protected $hostname; + public function Connect($hostname, $username, $passwd) + { + global $db_connections; + $db_connections++; + + $this->hostname = $hostname; + + decho("Connecting ('$hostname')"); + return ($GLOBALS['___mysqli_ston'] = mysqli_connect($hostname, $username, $passwd)); + } + + public function Disconnect($link) + { + decho("Disconnecting ('$link' from '$this->hostname')"); + return mysqli_close($GLOBALS['___mysqli_ston']); + } + + public function Stat() + { + return mysqli_stat($GLOBALS['___mysqli_ston']); + } + + public function Select_db($db) + { + decho("Selecting database ('$db')"); + return mysqli_select_db($GLOBALS['___mysqli_ston'], $db); + } + + private function validResult($p_result) + { + if($this->Rows($p_result) > 0) + return true; + else + return false; + } + + public function Query($str) + { + global $db_queries; + $db_queries++; + return mysqli_query($GLOBALS['___mysqli_ston'], $str); + } + + public function Free($result) + { + global $db_frees; + $db_frees++; + @((mysqli_free_result($result) || (is_object($result) && (get_class($result) == "mysqli_result"))) ? true : false); + } + + public function Fetch($result) + { + return $this->FetchArray($result); + } + + public function FetchArray($p_result) + { + global $db_fetches; + $db_fetches++; + decho('Fetching result'); + + if(!$this->validResult($p_result)) + { + decho('Result was not valid.'); + return array(); + } + + while ($row = mysqli_fetch_array($p_result, MYSQLI_ASSOC)) + { + $result[] = $row; + } + $this->Free($p_result); + + return is_array($result) ? $result : array(); + } + + public function FetchObject($p_result, $class, $no_array = false) + { + global $db_fetches; + $db_fetches++; + + decho("Fetching object result"); + + if(!$this->validResult($p_result)) + { + decho('Result was not valid.'); + return (object)array(); + } + + while ($row = mysqli_fetch_object($p_result, $class)) + { + if($no_array == true) + (object)$result = $row; + else + (object)$result[] = $row; + } + + $this->Free($p_result); + + // TODO: Test for objects inside of $result array + if($no_array == true) { return is_object($result) ? $result : (object)$nothing; } + return is_array($result) ? $result : (object)array(); + } + + public function FetchAssoc($result) + { + return $this->FetchArray($result); + } + + public function FetchRow($p_result) + { + global $db_fetches; + $db_fetches++; + + decho("Fetching single row"); + + if(!$this->validResult($p_result)) + { + decho('Result was not valid.'); + return array(); + } + + while ($row = mysqli_fetch_assoc($p_result)) { + $result = $row; + } + + $this->Free($p_result); + + return is_array($result) ? $result : array(); + } + + public function Rows($result) + { + decho("Fetching number of rows"); + + return mysqli_num_rows($result); + } +} + +?> -- cgit