<?php
/*
	common.lib.php
	
	Just some handy functions we might need.
*/

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

function emptyPost($index) {
	return !isset($_POST[$index]) || empty($_POST[$index]);
}

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')
		)
	);
}