|
Server : Apache/2.4.62 System : FreeBSD fbsdweb2.web.rcn.net 14.1-RELEASE FreeBSD 14.1-RELEASE releng/14.1-n267679-10e31f0946d8 GENERIC amd64 User : www ( 80) PHP Version : 8.3.8 Disable Function : NONE Directory : /domains/owens.enteract/inc/libs/validation/ |
Upload File : |
<?php
/**
* Validation class for handing and sanitizing user input
*
EXAMPLES
include_once("inc/libs/validation/validation.class.php");
ValidationUtils::getRequestVar("GET","test1","turkey");
ValidationUtils::getRequestVar("POST","test1","turkey");
ValidationUtils::getRequestVar("BOTH","test1","turkey");
ValidationUtils::getRequestVar("COOKIE","test1","turkey");
*/
class ValidationUtils{
public static function getRequestVar($postMethod,$indexName,$defaultValue="",$escape=true) {
$tryPost = false;
if (strtoupper($postMethod) == "BOTH") {
if (isset($_GET[$indexName])) {
if ($_GET[$indexName] != "") {
$value = $_GET[$indexName];
} else {
$tryPost = true;
}
} else {
$tryPost = true;
}
if ($tryPost == true){
if (isset($_POST[$indexName])) {
if ($_POST[$indexName] != "") {
$value = $_POST[$indexName];
} else {
return $defaultValue;
}
} else {
return $defaultValue;
}
}
} else if (strtoupper($postMethod) == "GET") {
if (isset($_GET[$indexName])) {
if ($_GET[$indexName] != "") {
$value = $_GET[$indexName];
} else {
return $defaultValue;
}
} else {
return $defaultValue;
}
} else if (strtoupper($postMethod) == "POST") {
if (isset($_POST[$indexName])) {
if ($_POST[$indexName] != "") {
$value = $_POST[$indexName];
} else {
return $defaultValue;
}
} else {
return $defaultValue;
}
} else {
if (isset($_COOKIE[$indexName])) {
if ($_COOKIE[$indexName] != "") {
$value = $_COOKIE[$indexName];
} else {
return $defaultValue;
}
} else {
return $defaultValue;
}
}
if (!get_magic_quotes_gpc()) {
if ($escape) {
return addslashes(trim($value));
}else{
return trim($value);
}
} else {
if ($escape) {
return trim($value);
}else{
return stripslashes(trim($value));
}
}
}
public static function mySQLDate($datein,$defaultDate=true,$dateTime=false,$euroStyleOutput=false){
//At this point Euro style is NOT expected coming in, but it can be output
$tempDate = $datein;
//Takes mm/dd/yyyy and converts to date mysql can use
$tempDate = str_replace(".","/",$tempDate);
$tempDate = str_replace("-","/",$tempDate);
//Swap the m/d for the Euro style date "dd/mm/yyyy"
$dateParts = explode("/", $tempDate);
if (count($dateParts) == 3){
//Split away the year from the time (if necessary)
if (strpos($dateParts[2]," ") > -1){
$yearParts = explode(" ", $dateParts[2]);
$year = intval($yearParts[0]);
$time = $yearParts[1];
$time = " " . $time;
}else{
$year = intval($dateParts[2]);
$time = "";
}
//Guess on their year if it's less than 4 digits
if ($year < 25) {
$year = "20" . $year;
}else if ($year <= 99){
$year = "19" . $year;
}
$tempDate = $dateParts[0] . "/" . $dateParts[1] . "/" . $year;
//Swap the m/d for the Euro style date "dd/mm/yyyy"
if ($euroStyleOutput == true){
$tempDate = $dateParts[1] . "/" . $dateParts[0] . "/" . $year;
}
$tempDate .= $time;
}
if ($tempDate == "" && $defaultDate == false){
if ($dateTime){
return "0000-00-00 00:00:00";
}else{
return "0000-00-00";
}
}else{
$date = new DateExt($tempDate);
if ($dateTime){
return $date->format("Y-m-d H:i:s");
}else{
return $date->format("Y-m-d");
}
}
}
public static function mySQLSafe($valuein){
return str_replace("'","\'",$valuein);
}
public static function stripHTMLTags($strIn){
$temp_str = str_replace("<","<",$strIn);
return str_replace(">",">",$temp_str);
}
public static function stripAllBreaks($str){
$tempStr = str_replace("<br>","",$str);
$tempStr = str_replace("<br />","",$tempStr);
$tempStr = str_replace("<p>","",$tempStr);
$tempStr = str_replace("</p>","",$tempStr);
return $tempStr;
}
public static function stripHiddenBreaks($str){
$tempStr = str_replace("\n","",$str);
$tempStr = str_replace("\r","",$tempStr);
return $tempStr;
}
public static function stripAll($str,$stripSpaces=false){
//Connectors are - and _
$regEx = "";
if ($stripSpaces){
$regEx = "[^A-Za-z0-9]";
}else{
$regEx = "[^A-Za-z0-9 ]";
}
return ereg_replace($regEx,"",$str);
}
public static function cleanJSString($str){
$tempStr = str_replace("'","'",$str);
$tempStr = str_replace("\"",""",$tempStr);
return $tempStr;
}
public static function isValidUSState($statein){
if (preg_match("/\\b(?:A[KLRZ]|C[AOT]|D[CE]|FL|GA|HI|I[ADLN]|K[SY]|LA|M[ADEINOST]|N[CDEHJMVY]|O[HKR]|PA|RI|S[CD]|T[NX]|UT|V[AT]|W[AIVY])\\b/", $statein)) {
return true;
} else {
return false;
}
}
public static function isValidEmail($emailin){
//Does not match email addresses using an IP address instead of a domain name.
//Matches top-level domains up to 7 characters long (.museum). Including longer top level increases the risk of false positives.
if (preg_match("/^[^0-9][-A-z0-9_]+([.][-A-z0-9_]+)*[@][-A-z0-9_]+([.][-A-z0-9_]+)*[.][A-z]{2,6}$/", $emailin)) {
return true;
} else {
return false;
}
}
public static function urlExists($url) {
if ($url == ""){return false;}
$file_headers = @get_headers($url);
if($file_headers[0] == 'HTTP/1.1 404 Not Found') {
return false;
}else {
return true;
}
}
/* Adds a protocol to a URL string if it doesn't exist */
public static function fullURL($url,$defaultProtocol = "http"){
//Buffer the url with a space so we can check for precise location of protocol (starting) without resorting to hacky evaluations
$bufferURL = " ".$url;
//We assume it starts with either http, https, ftp or ftps protocols
if ($url == ""){
return "";
}else if ((strpos($bufferURL,"http") == 1 || strpos($bufferURL,"ftp") == 1 || strpos($bufferURL,$defaultProtocol) == 1) && strpos($bufferURL,"://") > 3){
return $url;
}else if (strpos($bufferURL,"//") == 1){
return $url;
}else{
return $defaultProtocol . "://" . $url;
}
}
}
/**
* Format class for formatting data.
*
*/
class FormatUtils{
public static function getOrdinal($num){
// Special case "teenth"
if ( ($num / 10) % 10 != 1 ) {
// Handle 1st, 2nd, 3rd
switch( $num % 10 ){
case 1: return $num . 'st';
case 2: return $num . 'nd';
case 3: return $num . 'rd';
}
}
if ($num == 0){
return $num;
}else{
// Everything else is "nth"
return $num . 'th';
}
}
public static function boolToString($value,$yes,$no){
$boolVal = substr(strtolower($value),0,1);
if ($boolVal == "y" || $boolVal == "1" || $boolVal == "t"){
return $yes;
}else{
return $no;
}
}
public static function convertToHTMLBreaks($txt){
$txt = str_replace("\r\n","\n",$txt);
$txt = str_replace("\n\r","\n",$txt);
$txt = str_replace("\n","<br />",$txt);
return $txt;
}
public static function cleanPostURL($title){
$tempURL = strtolower(str_replace("'","",$title));
$tempURL = str_replace("!","",$tempURL);
$tempURL = str_replace(",","",$tempURL);
$tempURL = str_replace(".","",$tempURL);
$tempURL = str_replace(" ","_",$tempURL);
$tempURL = ereg_replace("[^a-z0-9_-]","",$tempURL);
return $tempURL;
}
public static function cleanYouTubeURL($link){
if ($link == ""){
return "";
}else{
$link = preg_replace('~
# Match non-linked youtube URL in the wild. (Rev:20130823)
https?:// # Required scheme. Either http or https.
(?:[0-9A-Z-]+\.)? # Optional subdomain.
(?: # Group host alternatives.
youtu\.be/ # Either youtu.be,
| youtube # or youtube.com or
(?:-nocookie)? # youtube-nocookie.com
\.com # followed by
\S* # Allow anything up to VIDEO_ID,
[^\w\s-] # but char before ID is non-ID char.
) # End host alternatives.
([\w-]{11}) # $1: VIDEO_ID is exactly 11 chars.
(?=[^\w-]|$) # Assert next char is non-ID or EOS.
(?! # Assert URL is not pre-linked.
[?=&+%\w.-]* # Allow URL (query) remainder.
(?: # Group pre-linked alternatives.
[\'"][^<>]*> # Either inside a start tag,
| </a> # or inside <a> element text contents.
) # End recognized pre-linked alts.
) # End negative lookahead assertion.
[?=&+%\w.-]* # Consume any URL (query) remainder.
~ix',
'http://www.youtube.com/watch?v=$1',
$link);
return $link;
}
}
public static function ipToNumber($ipString){
if ($ipString == "") {
return 0;
} else {
$ips = explode(".", $ipString);
return ($ips[3] + $ips[2] * 256 + $ips[1] * 256 * 256 + $ips[0] * 256 * 256 * 256);
}
}
public static function xmlFormat($xmlIn,$errors){
$xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<content>\n" . $xmlIn;
//Write out errors
if ($errors == "") {
$xml .= "\t<script_message>ok</script_message>\n";
} else {
$xml .= "\t<script_message>failed</script_message>\n";
$xml .= "\t<errors>".$errors."</errors>\n";
}
$xml .= "</content>";
return $xml;
}
}
?>