<?php

/**
 * This class has been taken from Otter CMS project.
 * Credits goes out to ottercms.com's team.
 */
class Validator {

	var $errors; // A variable to store a list of error messages
	var $allowBlanks = false; // If set to true, blank are also permitted

	// Validate something's been entered
	// NOTE: Only this method does nothing to prevent SQL injection
	// use with addslashes() command
	function validateEmptyGeneral($theInput,$description = ''){
		if ($theInput == ''){return true;}		
		if (trim($theInput) != "") {
			return true;
		}else{
			$this->errors[] = $description;
			return false;
		}
	}

	function validateGeneral($theInput,$description = ''){
	
		if ($this->allowBlanks && $theInput == ''){return true;}
		
		if (trim($theInput) != "") {
			return true;
		}else{
			$this->errors[] = $description;
			return false;
		}
	}

	
	// Validate text only
	function validateTextOnly($theInput,$description = ''){
	
		if ($this->allowBlanks && $theInput == ''){return true;}
			
		$result = ereg ("^[A-Za-z0-9_\!@' ]+$", $theInput );
		if ($result){
			return true;
		}else{
			$this->errors[] = $description;
			return false; 
		}
	}

	// Validate text only
	function validateEmptyTextOnly($theInput,$description = ''){
	
		if (trim($theInput) == ''){return true;}
			
		$result = ereg ("^[A-Za-z0-9_\!@' ]+$", $theInput );
		if ($result){
			return true;
		}else{
			$this->errors[] = $description;
			return false; 
		}
	}

	// Validate text only, no spaces allowed
	function validateTextOnlyNoSpaces($theInput,$description = ''){

		if ($this->allowBlanks && $theInput == ''){return true;}

		$result = ereg ("^[A-Za-z0-9_]+$", $theInput );
		if ($result){
			return true;
		}else{
			$this->errors[] = $description;
			return false; 
		}
	}
		
	// Validate email address
	function validateEmail($theInput,$description = ''){
	
		if ($this->allowBlanks && $theInput == ''){return true;}
		
		$result = ereg ("^[^@ ]+@[^@ ]+\.[^@ \.]+$", $theInput );
		if ($result){
			return true;
		}else{
			$this->errors[] = $description;
			return false; 
		}
			
	}
	
	// Validate numbers only
	function validateNumber($theInput,$description = ''){
	
		if ($this->allowBlanks && $theInput == ''){return true;}
	
		if (is_numeric($theInput)) {
			return true; // The value is numeric, return true
		}else{ 
			$this->errors[] = $description; // Value not numeric! Add error description to list of errors
			return false; // Return false
		}
	}
	
	// Validate date
	function validateDate($theInput,$description = ''){
		
		if ($this->allowBlanks && $theInput == ''){return true;}
		
		if (strtotime($thedate) === -1 || $thedate == '') {
			$this->errors[] = $description;
			return false;
		}else{
			return true;
		}
	}
	
	// Check whether any errors have been found (i.e. validation has returned false)
	// since the object was created
	function foundErrors() {
		if (count($this->errors) > 0){
			return true;
		}else{
			return false;
		}
	}

	// Return a string containing a list of errors found,
	// Seperated by a given deliminator
	function listErrors($delim = ' '){
		if (count($this->errors) > 0){return implode($delim,$this->errors);}
	}

	// Return a string containing a list of errors found,
	// Seperated by a <LI> html tag
	function listDotErrors(){
		return "<li>".implode("<li>",$this->errors);
	}

	
	// Manually add something to the list of errors
	function addError($description){
		$this->errors[] = $description;
	}

	// Allow blank values to validate as true	
	function allowBlanks($bool){
		$this->allowBlanks = $bool;
	}
		
}

?>