<?php
require_once (dirname(__FILE__).'/purifier/HTMLPurifier.auto.php');

function isNumber($int){
	if(preg_match('/^[0-9]+$/', $int)){
		return $int;
	} else {
		die ('Error: 數字欄必備輸入數字');
	}
}
function isDate($date){
	if(preg_match('/(\d{4})-(\d{2})-(\d{2})/', $date)){
		return $date;
	} else {
		die ('Error: 日期欄不是有較日期');
	}
}

function isEmail($email){
	if(preg_match("/^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/", $email)) { 
		return $email; 
	} else {
		die ('Error: 電郵錯誤');
	}
}

function txt_only($str){
 	$del_char = array('!','@','#','$','%','^','&','*','+','=','[',']','\\','\'','{','}','|','\"',':','<','>','?','~','(',')');  // '_', '/', '-', '.', ',' enabled
	foreach($del_char as $v):
		$str = str_replace($v,"", $str); 
	endforeach;  
	return trim($str);
}

function htmlEncode($str){
	$str = trim($str);
	$data = htmlspecialchars($str, ENT_QUOTES);
	return $data;
}
function checkBoolean($str){
	if($str == 'true' || $str == 'false'){
		return $str;
	} else {
		die ('Error: Is not a Boolean value.');
	}
}

//filter script tags
function htmlPurify($dirty_html){
	$config = HTMLPurifier_Config::createDefault();
	$config->set('Attr.EnableID', true);
	$purifier = new HTMLPurifier($config);
	$clean_html = $purifier->purify($dirty_html);
	
	return $clean_html;
}

?>