Location: PHPKode > projects > JaxBlog > JaxBlog/app/common/database.php
<?php

// config.php MUST have already been brought in 
// via require_once() prior to calling this script.
require_once(FOLDER_RELATIVE_BASE . FOLDER_ADODB . 'adodb-exceptions.inc.php');
require_once(FOLDER_RELATIVE_BASE . FOLDER_ADODB . 'adodb.inc.php');

// Error message bank
function errorDatabaseUnavailable($code, $msg, $file, $line, $context) { header( 'Location: ../../user/user_story/user_error.php?errorcode=1001' ); die(); }

function databaseGetConnection($report_errors = false) {
	set_error_handler("errorDatabaseUnavailable");

	if (FLAG_DEBUG == 'Y' || $report_errors == true) {
		ini_set('display_errors', '0' or '1');
		error_reporting(E_ALL);
	}
	$connection = NewADOConnection(VENDOR);
	
	if (VENDOR == 'sqlite') {
		$connection->PConnect(FILE_SQLITE, USERNAME, PASSWORD);
	} else {
		$connection->PConnect(SERVER, USERNAME, PASSWORD, DATABASE);
	}
	restore_error_handler();

	return $connection;
}

function databaseGetValue($sql, $args) {
	$connection = databaseGetConnection();
	$rows = $connection->GetArray($sql, $args);
	if ($rows == null) return null;
	return $rows[0][0];
}

function databaseGetRows($sql, $args) {
	$connection = databaseGetConnection();
	$rows = $connection->GetArray($sql, $args);
	if ($rows == null) $rows = array();
	return $rows;
}

function databaseGetRow($sql, $args) {
	$connection = databaseGetConnection();
	$rows = $connection->GetArray($sql, $args);
	if (count($rows) == 0) return null;
	return $rows[0];
}

function databaseExecute($sql, $args) {
	$connection = databaseGetConnection();
	$connection->Execute($sql, $args);
}

function databaseExecuteReturnId($sql, $args, $sequence) {
	$connection = databaseGetConnection();
	$args[0] = $connection->GenId($sequence);
	$connection->Execute($sql, $args);
	return $args[0];
}

function databaseGetId($sequence) {
	$connection = databaseGetConnection();
	return $connection->GenId($sequence);
}

function databaseGetDate($databaseFieldName, $customDateFormat = false) {
	if (!$customDateFormat) {
		$key = 'DATABASE_GETDATE_' . $databaseFieldName;
		if (isset($_SESSION[$key])) return $_SESSION[$key];
		$value = databaseGetConnection()->SQLDate('Y-m-d H:i A', $databaseFieldName);
		$_SESSION[$key] = $value;
		return $value;
	} else {
		$value = databaseGetConnection()->SQLDate($customDateFormat, $databaseFieldName);
		return $value;
	}
}

function databaseSetDate($valueFromUser = null) {
	$timeSinceEpoch = null;
	if ($valueFromUser == null) {
		$timeSinceEpoch = time();
	} else {
		$valid = preg_match('/[0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2} (AM|PM)/', $valueFromUser);
		if (!$valid) {
			return 'null';
		}
		
		// Go from  2010-01-01 01:00 PM
		//      to  2010-01-01 13:00:00
		$halves = split(' ', $valueFromUser);
		$mdy = split('-', $halves[0]);
		$times = split(':', $halves[1]);
		if ($halves[2] == 'PM') {
			$times[0] = ((int) $times[0]) + 12;
			if  ($times[0] == 24) $times[0] = '00';
		}
		$timeSinceEpoch = "$mdy[0]-$mdy[1]-$mdy[2] $times[0]:$times[1]:00";
	}
	return databaseGetConnection()->DBTimeStamp($timeSinceEpoch);
}

function databaseFormatToReadableDate($db_date) {
	
	$halves = split(' ', $db_date);
	$mdy = split('-', $halves[0]);
	$times = split(':', $halves[1]);
	
	$months = array(
		'January',
		'February',
		'March',
		'April',
		'May',
		'June',
		'July',
		'August',
		'September',
		'October',
		'November',
		'December',
	);
	
	$month = $months[$mdy[1] - 1];
	$date = "$month $mdy[2], $mdy[0]";
	
	return $date;
}

function passwordScore($password) {
	
	$expressionAlpha   = '/[A-Za-z]+/';
	$expressionNumeric = '/[0-9]+/';
	
	$strength = -1;
	if (strlen($password) >  0 && (preg_match($expressionAlpha, $password) || preg_match($expressionNumeric, $password))) $strength++;
	if (strlen($password) >= 4 && (preg_match($expressionAlpha, $password) || preg_match($expressionNumeric, $password))) $strength++;
	if (strlen($password) >= 7 && preg_match($expressionAlpha, $password) && preg_match($expressionNumeric, $password)) $strength++;
	
	return $strength;
}

?>
Return current item: JaxBlog