<?php
/* ValidateFormErrors class.

Error Codes:
1 	 Mandatory
2	 Email Regex
4	 Password validation. only [a-zA-Z0-9+-*@#%=?!_;./()]
		and must be atleast 5 characters long
8	Max chars 20
16	Max chars 30
32	Max chars 50
64  Max chars 100
128 Max Chars 255
256 Text, Numerics and White Space only 
512 Invalid characters only [a-zA-Z0-9+-*@#%=?!_;./()\r\n] inc carriage return and new lines (textareas)
1024 Max Chars 510


DEPRECIATED
16	 File Size
32	 Numeric Input
64	 Date - YYYY
128  Date - DDMMYYYY
256	 Latitude and Longitude - 51:34:27N or 51.57423, 0:08:36W or -0.14328.
512 Text only - [a-zA-Z] and white space.
1024 Text & numerics only - [a-zA-Z0-9].
2048 Maximum length in characters =< 30
4096 Maximum length in characters =< 50
8192 Binary uppercase Y or N
16384 Maximum length in characters =< 20
32768 Maximum length in characters =< 100
65536 Invalid charaters - only [ a-zA-Z0-9,-'.,_()/\%]
*/

class ValidateForm {
	
	var $aFormFields = array();
	
	function ValidateForm($aFormFields) {
		$this->aFormFields = $aFormFields;
	}
		
	function CheckFields($sName = "", $sValue, $bCheck) {
		//print "NAME:" . $sName . " - VAL:" . $sValue . " - Check:" . $bCheck . "\n\n";
		$aErrors = array();
		$aBits = array('bMandatory','bEmailRegex','bValidPassword','bMaxChar20','bMaxChar30','bMaxChar50','bMaxChar100','bMaxChar255','bTextNumeralSpaceOnly','bInvalidChars1','bMaxChar510');
		$iNumBits = count($aBits);
		$iBinary =  sprintf("%0" . $iNumBits . "b",$bCheck);
		for($i = 0; $i < $iNumBits; $i++) {
			$$aBits[$i] = substr($iBinary, (0-(1+$i)), 1);
		}

		# The last logic check is for file uploads, when empty it still produces an array with error code 4
		# Size test is to ensure error is returned if user inputs gibberish with CORRECT extension into upload input
		if($bMandatory == TRUE) {
			if($sValue === "" || $sValue === FALSE || $sValue === NULL || (($sValue['error']==4 || (@array_key_exists("size", $sValue) && $sValue['size'] == 0)) && is_array($sValue))) {
				//print "NAME:" . $sName . " - VAL:" . $sValue . "\n\n";
				$aErrors[] = "Empty field";
			}
		}
	
		if($bEmailRegex == TRUE) {
			if(!eregi("^[a-z0-9\._-]+@+[a-z0-9\._-]+\.+[a-z]{2,4}$", $sValue) && $sValue) {
				$aErrors[] = "Email invalid";
			}
		}
	
		if($bValidPassword == TRUE) {
			
			// Check valid characters
			if(!eregi("^[a-zA-Z0-9\+-\*\@\#\%\=\?\!_\;\.\/\(\)]*$", $sValue) && $sValue) {
				$aErrors[] = "Invalid characters used in Password. Allowed characters a-z A-Z 0-9 +-*@#%=?!_;./()";
			}		
			if(strlen($sValue) < 5) {
				$aErrors[] = "Password must be at least 5 characters long.";
			}
			//if(!preg_match( '/[A-Z]/', $sValue)) {
			//	$aErrors[] = "Password must contain at least 1 upper case letter.";
			//}
			//if(!preg_match( '/[0-9]/', $sValue)) {
			//	$aErrors[] = "Password must contain at least 1 numeral.";
			//}
		}

		if($bMaxChar20 == TRUE) {
			if(strlen($sValue) > 20 && $sValue) {
				$aErrors[] = "Too many characters maximum of 20 characters allowed";
			}
		}
		
		if($bMaxChar30 == TRUE) {
			if(strlen($sValue) > 30 && $sValue) {
				$aErrors[] = "Too many characters maximum of 30 characters allowed";
			}
		}
		
		if($bMaxChar50 == TRUE) {
			if(strlen($sValue) > 50 && $sValue) {
				$aErrors[] = "Too many characters maximum of 50 characters allowed";
			}
		}
		
		if($bMaxChar100 == TRUE) {
			if(strlen($sValue) > 100 && $sValue) {
				$aErrors[] = "Too many characters maximum of 100 characters allowed";
			}
		}
		
		if($bMaxChar255 == TRUE) {
			if(strlen($sValue) > 255 && $sValue) {
				$aErrors[] = "Too many characters maximum of 255 characters allowed";
			}
		}

		if($bTextNumeralSpaceOnly == TRUE) {
			if(!eregi("^[ a-zA-Z0-9]*$", $sValue) && $sValue) {
				$aErrors[] = "Invalid characters used - only alphanumeric and white space allowed ";
			}
		}

		if($bInvalidChars1 == TRUE) {
			if(!eregi("^[ a-zA-Z0-9\+-\*\@\#\%\=\?\!_\;\.\/\(\)\n\r]*$", $sValue) && $sValue) {
				$aErrors[] = "Invalid characters used - allowed characters a-z A-Z 0-9 +-*@#%=?!_;.,/()";
			}
		}
		
		if($bMaxChar510 == TRUE) {
			if(strlen($sValue) > 510 && $sValue) {
				$aErrors[] = "Too many characters maximum of 510 characters allowed";
			}
		}

		
		
		
		
		
		
		
		
		
		
		
		if($bValidateFileSize == TRUE) {
			if($bMandatory || ($sValue != '' && $sValue)) {
				eval("global \$".$sName."_maxsize;");
				eval("\$iMaxFileSize = \$".$sName."_maxsize;");
				if($iMaxFileSize < $sValue['size']) {
					$aErrors[] = "File too large";
				}
			}
		}
	
		if($bNumericInput == TRUE) {
			if(!is_numeric($sValue) && $sValue) {
				$aErrors[] = "Non numeric";
			}
		}

		if($bDateYYYY == TRUE) {
			if(!eregi("^[0-9]{1,4}$", $sValue) && $sValue) {
				$aErrors[] = "Invalid characters used";
			}
		}

		if($bDateDDMMYYYY == TRUE) {
			list($iDay,$iMonth,$iYear) = explode("/",$sValue);
			if((checkDate($iMonth,$iDay,$iYear) == FALSE) || (strlen($iYear) != 4) && $sValue) {
				$aErrors[] = "invalid_date";
			}
		}
		
		if($bLatitudeLongitude == TRUE) {
			if(!eregi("^([0-9]{1,2}:[0-9]{1,2}:[0-9]{1,2}[NSWEnswe]{1})|([-+]?[0-9]{1,3}\.[0-9]{1,6})$", $sValue) && $sValue) {
				$aErrors[] = "Latitude or Longitude invalid";
			}
		}



		if($bYorN == TRUE) {
			if($sValue != "Y" && $sValue != "N") {
				$aErrors[] = "Invalid input requires Y (Yes) or N (No)";
			}
		}


		
		if($bPhone == TRUE) {
			$iVal = eregi_replace("\(|\)|\-| |\+","",$sValue);
			$bError = FALSE;
			for ($i=0;$i<strlen($iVal);$i++) {
				$c = substr($iVal,$i,1);
				if ($c < "0" || $c > "9" && $sValue) {
					$bError = TRUE;
				}
			} 
			if($bError == TRUE) {
				$aErrors[] = "Phone number invalid";
			}
		}
		
		return $aErrors; 
	}
	
	// Validate html form input fields. Maping to field type array in global settings. 
	function Check($form, $RC) {
		$aErrors = array();
		foreach($this->aFormFields[$form] as $key => $val) {
			if($field_result = ValidateForm::CheckFields($key, $RC->GetVar($key), $val)) {
				$this->aFormFieldErrors[$key] = $field_result[0];
				$err = TRUE;
			}
		}
		if($err) {
			return false;
		} 
		return true;
	}
}
?>