<?php
/*
	common.libs.php
	Version 1.0 Jason M. Knight, August 2008

	Just some handy constants, functions and objects
*/

function cleanString($string) {
	return htmlspecialchars(strip_tags($string));
}

function cleanPath($path) {
	return trim(str_replace(['\\', '%5C'],  '/', $path), '/');
}

function commaSplit($string) {
	return array_map('trim', explode(',', $string));
}

function isNotAction($target) {
	return !is_file($target . '.content.php');
}

function isValidEmail($address) {
	if (filter_var($address,FILTER_VALIDATE_EMAIL)==FALSE) {
		return false;
	}
	/* explode out local and domain */
	list($local,$domain)=explode('@',$address);
	
	$localLength=strlen($local);
	$domainLength=strlen($domain);
	
	return (
		/* check for proper lengths */
		($localLength>0 && $localLength<65) &&
		($domainLength>3 && $domainLength<256) &&
		(
			checkdnsrr($domain,'MX') ||
			checkdnsrr($domain,'A')
		)
	);
}

function newLine($count = 1) {
	do { echo CRLF; } while (--$count);
}

function safeDefine($index, $value) {
	if (!defined($index)) define($index, $value);
}

function safeName($name) {
	return preg_replace('/[^\w]/', '', $name);
}

function safePost($index) {
	return array_key_exists($index, $_POST) ? $_POST[$index] : false;
}

function safeGet($index) {
	return array_key_existS($index, $_GET) ? $_GET[$index] : false;
}

function safeArray($index, $array) {
	return array_key_exists($index, $array) ? $array[$index] : false;
}

final class Request {

	private static
		$data = [],
		$path = '';
	
	private static function set() {
		self::$path = parse_url(cleanPath($_SERVER['REQUEST_URI']), PHP_URL_PATH);
		if (strpos(self::$path, '..')) die('Hacking Attempt Detected, Aborting');
		self::$path = substr(self::$path, strlen(ROOT_HTTP) - 1);
		self::$data = empty(self::$path) ? [DEFAULT_ACTION] : explode('/', self::$path);
		foreach (self::$data as &$p) $p = urldecode($p);
	}
	
	public static function value($index = 0) {
		if (count(self::$data) == 0) self::set();
		return isset(self::$data[$index]) ? self::$data[$index] : false;
	} // Request::value
	
	public static function getPath() {
		if (count(self::$data) == 0) self::set();
		return self::$path;
	}
	
} // Request

define('CRLF', "\r\n");
define('SCRIPT_PATH', cleanPath(pathinfo($_SERVER['SCRIPT_NAME'], PATHINFO_DIRNAME)));
define('ROOT_HTTP', '/' . SCRIPT_PATH . (SCRIPT_PATH == '' ? '' : '/'));
define('ROOT_LOCAL', pathinfo($_SERVER['SCRIPT_FILENAME'], PATHINFO_DIRNAME) . '/');
define('HOST_PROTOCOL', 'http' . (
	isset($_SERVER['HTTPS']) && ($_SERVER['HTTPS'] != 'off') ? 's' : ''
) . '://');
define('HOST_HTTP', HOST_PROTOCOL . $_SERVER['HTTP_HOST']);
define('BASE_HTTP', HOST_HTTP . ROOT_HTTP);
define('PATH_HTTP', 
	'/' . parse_url(cleanPath($_SERVER['REQUEST_URI']), PHP_URL_PATH)
);
define('FULL_HTTP', HOST_HTTP . PATH_HTTP);
define('CANONICAL_PATH', Request::getPath());
define('CANONICAL_URI', HOST_PROTOCOL . WORKING_DOMAIN . '/' . CANONICAL_PATH);
define('CANONICAL_URIe', urlencode(CANONICAL_URI));