'; } if (isset($GLOBALS['googleAnalyticsCode']) && $GLOBALS['googleAnalyticsCode'] != '') { addGoogleAnalyticsTracking($GLOBALS['googleAnalyticsCode']); } //-------------------------------------------------- // Set location variables function setLocationVariables($scriptFilePath) { //-------------------------------------------------- // Generate an organised folder array based on // the current page the website visitor is // viewing. The human version takes the name // out of camel notation (spaces). $GLOBALS['tplFolder'] = str_replace('\\', '/', realpath($scriptFilePath)); $GLOBALS['tplFolder'] = preg_replace('/^' . preg_quote(ROOT, '/') . '\/?(([^\/]*\/)*).*$/', '\1', $GLOBALS['tplFolder']); $GLOBALS['tplFolder'] = explode('/', $GLOBALS['tplFolder']); $GLOBALS['tplFolder'] = array_pad($GLOBALS['tplFolder'], 3, ''); $GLOBALS['tplFolderHuman'] = array(); foreach ($GLOBALS['tplFolder'] as $f) { $GLOBALS['tplFolderHuman'][] = ucfirst(preg_replace('/([a-z])([A-Z])/', '\1 \2', $f)); } //-------------------------------------------------- // Generate the page ID for the body tag and // any scripts which require it $GLOBALS['tplPageId'] = ''; foreach ($GLOBALS['tplFolder'] as $f) { $GLOBALS['tplPageId'] .= ucfirst($f); } if ($GLOBALS['tplPageId'] == '') { $GLOBALS['tplPageId'] = 'Home'; } $GLOBALS['tplPageId'] = 'p' . $GLOBALS['tplPageId']; //-------------------------------------------------- // Generate the page title $GLOBALS['tplPageTitle'] = implode(' | ', $GLOBALS['tplFolderHuman']); $GLOBALS['tplPageTitle'] = preg_replace('/^[ \|]*(.*?)[ \|]*$/', '\1', $GLOBALS['tplPageTitle']); if ($GLOBALS['tplPageTitle'] != '') { $GLOBALS['tplPageTitle'] = ' | ' . $GLOBALS['tplPageTitle']; } } if (!$GLOBALS['useUrlRewrite']) { setLocationVariables($_SERVER['SCRIPT_FILENAME']); } //-------------------------------------------------- // Return submitted values function returnSubmittedValue($variable, $method = 'request', $validation = 0) { //-------------------------------------------------- // Get value $value = ''; $method = strtolower($method); if ($method == 'post') { if (isset($_POST[$variable])) { $value = $_POST[$variable]; } } elseif ($method == 'cookie') { if (isset($_COOKIE[$variable])) { $value = $_COOKIE[$variable]; } } elseif ($method == 'request') { if (isset($_REQUEST[$variable])) { $value = $_REQUEST[$variable]; } } else { if (isset($_GET[$variable])) { $value = $_GET[$variable]; } } //-------------------------------------------------- // Remove bad characters if ($GLOBALS['pageCharset'] == 'ISO-8859-1') { $badCharacters = array(chr(145), chr(146), chr(147), chr(148), chr(150), chr(151)); $goodCharacters = array("'", "'", '"', '"', '-', '--'); $value = str_replace($badCharacters, $goodCharacters, $value); } //-------------------------------------------------- // Strip slashes (IF NESS) if (ini_get('magic_quotes_gpc')) { $value = stripslashesdeep($value); } //-------------------------------------------------- // Validation + string manipulation if ($validation & 1) $value = trim($value); if ($validation & 2) $value = strip_tags($value); if ($validation & 4) $value = strtolower($value); if ($validation & 8) $value = intval($value); //-------------------------------------------------- // Return value return $value; } //-------------------------------------------------- // Check that an email address is valid function isemail($email) { if (preg_match('/^\w[-.+\'\w]*@(\w[-._\w]*\.[a-zA-Z]{2,}.*)$/', $email, $matches)) { if (function_exists('checkdnsrr')) { if (checkdnsrr($matches[1] . '.', 'MX')) return true; // If a 'mail exchange' record exists if (checkdnsrr($matches[1] . '.', 'A')) return true; // Mail servers can fall back on 'A' records } else { return true; // For Windows } } return false; } //-------------------------------------------------- // Take a postcode and format it correctly, an // invalid postcode will return NULL function formatBritishPostcode($postcode) { //-------------------------------------------------- // Clean up the user input $postcode = strtoupper($postcode); $postcode = preg_replace('/[^A-Z0-9]/', '', $postcode); $postcode = preg_replace('/([A-Z0-9]{3})$/', ' \1', $postcode); $postcode = trim($postcode); //-------------------------------------------------- // Check that the submitted value is a valid // British postcode: AN NAA | ANN NAA | AAN NAA | // AANN NAA | ANA NAA | AANA NAA if (preg_match('/^[a-z](\d[a-z\d]?|[a-z]\d[a-z\d]?) \d[a-z]{2}$/i', $postcode)) { return $postcode; } else { return NULL; } } //-------------------------------------------------- // A recursive function for running stripslashes // on all items of a variable / array. function stripslashesdeep($value) { return (is_array($value) ? array_map('stripslashesdeep', $value) : stripslashes($value)); } //-------------------------------------------------- // Quick functions used to convert text into a safe // form of HTML/XML/CSV without having to write the // full native function in the script. function html($text) { return htmlentities($text, ENT_QUOTES, $GLOBALS['pageCharset']); } function htmlDecode($text) { return html_entity_decode($text, ENT_QUOTES, $GLOBALS['pageCharset']); } function xml($text) { $text = str_replace('&', '&', $text); $text = str_replace('"', '"', $text); $text = str_replace("'", ''', $text); $text = str_replace('>', '>', $text); $text = str_replace('<', '<', $text); return $text; } function csv($text) { return str_replace('"', '""', $text); } function head($text) { return preg_replace('/(\r|\n)/', '', $text); } //-------------------------------------------------- // A two quick text formatting function which // converts camel notation text into or from human function camel2human($text) { return ucfirst(preg_replace('/([a-z])([A-Z])/', '\1 \2', $text)); } function human2camel($text) { $text = ucwords(strtolower($text)); $text = preg_replace('/[^a-zA-Z0-9]/', '', $text); if (strlen($text) > 0) { // Min of 1 char $text[0] = strtolower($text[0]); } return $text; } //-------------------------------------------------- // Convert a UNIX time-stamp into human text function unix2human($unix) { //-------------------------------------------------- // Maths $sec = $unix % 60; $unix -= $sec; $minSeconds = $unix % 3600; $unix -= $minSeconds; $min = ($minSeconds / 60); $hourSeconds = $unix % 86400; $unix -= $hourSeconds; $hour = ($hourSeconds / 3600); $daySeconds = $unix % 604800; $unix -= $daySeconds; $day = ($daySeconds / 86400); $week = ($unix / 604800); //-------------------------------------------------- // Text $output = ''; if ($week > 0) $output .= ', ' . $week . ' week' . ($week != 1 ? 's' : ''); if ($day > 0) $output .= ', ' . $day . ' day' . ($day != 1 ? 's' : ''); if ($hour > 0) $output .= ', ' . $hour . ' hour' . ($hour != 1 ? 's' : ''); if ($min > 0) $output .= ', ' . $min . ' minute' . ($min != 1 ? 's' : ''); if ($sec > 0 || $output == '') { $output .= ', ' . $sec . ' second' . ($sec != 1 ? 's' : ''); } //-------------------------------------------------- // Grammar $output = substr($output, 2); $output = preg_replace('/, ([^,]+)$/', ' and $1', $output); //-------------------------------------------------- // Return the output return $output; } //-------------------------------------------------- // Function to convert a file-size into something // human readable function fileSize2human($size) { $units = array('B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'); foreach ($units as $unit) { if ($size >= 1024 && $unit != 'YB') { $size = ($size / 1024); } else { return round($size, 0) . $unit; } } } //-------------------------------------------------- // Function to send the user onto another page. // This takes into IE6 into consideration when // redirecting from a HTTPS connection to the // standard HTTP function redirect($url, $httpResponseCode = 302) { $htmlNext = '
Goto next page
'; if (isset($GLOBALS['createDebugOutput']) && $GLOBALS['createDebugOutput'] && ob_get_length() > 0) { ob_end_flush(); exit($htmlNext); } if ($GLOBALS['tplHttpsUsed'] && substr($url, 0, 7) == 'http://') { if (isset($_SERVER['HTTP_USER_AGENT']) && strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE 6') !== false) { header('Refresh: 0; URL=' . head($url)); exit(''); } } header('Location: ' . head($url), true, $httpResponseCode); exit($htmlNext); } //-------------------------------------------------- // Function to cut a string to X words, function cutStringLength($text, $words) { $text = strip_tags($text); $text = explode(' ', $text, $words + 1); if (count($text) > $words) { $dumpData = array_pop($text); } return implode(' ', $text); } //-------------------------------------------------- // Replace a variable in a URL function urlReplaceVariable($variable, $value = NULL, $url = NULL) { //-------------------------------------------------- // Split if ($url === NULL) { $path = (isset($_SERVER['SCRIPT_URL']) ? $_SERVER['SCRIPT_URL'] : ''); $query = (isset($_SERVER['QUERY_STRING']) ? $_SERVER['QUERY_STRING'] : ''); } else { if (($pos = strpos($url, '?')) !== false) { $path = substr($url, 0, $pos); $query = substr($url, ($pos + 1)); } else { $path = $url; $query = ''; } } //-------------------------------------------------- // Remove or replace variable if (preg_match('/(.*)(^|&)' . preg_quote($variable, '/') . '=[^&]*(&|$)(.*)/', $query, $m)) { if ($value !== NULL) { $query = $m[1] . $m[2] . $variable . '=' . urlencode($value) . $m[3] . $m[4]; } else { $query = $m[1] . ($m[1] != '' && $m[4] != '' ? $m[2] : '') . ($m[1] != '' && $m[2] == '' && $m[4] != '' ? $m[3] : '') . $m[4]; } } else if ($value !== NULL) { $query .= ($query == '' ? '' : '&') . $variable . '=' . urlencode($value); } //-------------------------------------------------- // Return return $path . ($query == '' ? '' : '?') . $query; } //-------------------------------------------------- // Database if (is_file(ROOT . '/a/php/database.php')) { require_once(ROOT . '/a/php/database.php'); if (isset($GLOBALS['dbHost'])) { // Some sites might not have the db config yet. $db = new database($GLOBALS['dbHost'], $GLOBALS['dbUser'], $GLOBALS['dbPass'], $GLOBALS['dbName']); $GLOBALS['db'] =& $db; // Effectively does nothing, but shows where the GLOBALS is set in a site wide-search and replace } } //-------------------------------------------------- // Error if (is_file(ROOT . '/a/php/error.php')) { require_once(ROOT . '/a/php/error.php'); } //-------------------------------------------------- // Debug if (isset($GLOBALS['createDebugOutput']) && $GLOBALS['createDebugOutput']) { require_once(ROOT . '/a/php/debug.php'); } //-------------------------------------------------- // System - site specific functions if (is_file(ROOT . '/a/php/system.php')) { require_once(ROOT . '/a/php/system.php'); } ?>