<?php
//中文格式驗證
function judgeChinese($str) {
	if(!preg_match("/[\x{4e00}-\x{9fa5}]/u",$str)==true) {
		$resultvar=true;
	} else {
		$resultvar=false;
	}
	return  $resultvar;
}
//數字欄位判斷-0-9
function judgedigital($str) {
	if (preg_match('/^[0-9]+$/', $str)) {
		$resultvar=false;
	} else {
		$resultvar=true;
	}
	return  $resultvar;
}
//HTML標籤判斷
function judgeHtml($str) {
	if($str != strip_tags($str)) {
		$resultvar=true;
	} else {
		$resultvar=false;
	}
	return  $resultvar;
}
//E-MAIL欄位判斷
function judgeEmail($str) {
	if(!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/",$str)) {
		$resultvar=true;
	} else {
		$resultvar=false;
	}
	return  $resultvar;
}
//網址驗證
function judgeurl($str) {
	if (!preg_match("/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i", $str)) {
		$resultvar=true;
	} else {
		$resultvar=false;
	}
	return  $resultvar;
}
//時間格式驗證-新(完整時間格式)
function validateDate($date="", $format="") {
	//$format = 'Y-m-d H:i:s'
	$d = DateTime::createFromFormat($format, $date);
	return $d && $d->format($format) == $date;
	//var_dump(validateDate('2012-02-27 12:12:12')); # true
	//var_dump(validateDate('2012-02-30 12:12:12')); # false
}
//時間格式驗證-舊(只能驗證日期)
function isDate($str) {
	if(!preg_match("/^[0-9]{4}-[1-12]{2}-[1-31]{2}$/", $str)) {
		return false;
	}
	$__y = substr($str, 0, 4);
	$__m = substr($str, 5, 2);
	$__d = substr($str, 8, 2);
	//$var=checkdate($__m, $__d, $__y);    
	if(checkdate($__m, $__d, $__y)) {
		$resultvar=true;
	} else {
		$resultvar=false;
	}
	return $resultvar;
}
//身分證驗證格式
function chk_pid($id) {
	if(!$id)return false;
	$id = strtoupper(trim($id));
	//將英文字母全部轉成大寫，消除前後空白
	//檢查第一個字母是否為英文字，第二個字元1 2 A~D 其餘為數字共十碼
	$ereg_pattern= "^[A-Z]{1}[12ABCD]{1}[[:digit:]]{8}$";
	if(!preg_match("/".$ereg_pattern."/i", $id))return true;
	$wd_str="BAKJHGFEDCNMLVUTSRQPZWYX0000OI";
	//關鍵在這行字串
	$d1=strpos($wd_str, $id[0])%10;
	$sum=0;
	if($id[1]>='A')$id[1]=chr($id[1])-65;
	//第2碼非數字轉換依[4]說明處理
	for ($ii=1;$ii<9;$ii++)
	        $sum+= (int)$id[$ii]*(9-$ii);
	$sum += $d1 + (int)$id[9];
	if($sum%10 != 0)return true;
	return false;
}



//禁止上傳檔案類型
function neillibrary_donotuploadfiles($filevar="",$operate="") {
	switch($operate) {
		case "1":
		$types = ['application/octet-stream', 'application/msaccess','text/plain','text/javascript'];
		//不允許類型 
		break;
		case "2":
		$types = ['image/png', 'image/jpeg','image/gif','image/webp'];
		//允許類型 
		break;
	}
	if(!is_array($filevar['type'])) {
		//單檔
		if (($operate==1 && in_array($filevar['type'],$types))  ||   ($operate==2 && !in_array($filevar['type'],$types)) ) {
			$prohibit['name'].="<span class='glyphicon glyphicon-remove' aria-hidden='true'></span>".$filevar['name'].'<br />';
			$prohibit['harmful']=true;
		}
	} else {
		//陣列
		foreach($filevar['type'] as $key=> $val) {
			if (($operate==1 && in_array($val,$types))  ||   ($operate==2 && !in_array($val,$types)) ) {
				$prohibit['name'].="<span class='glyphicon glyphicon-remove' aria-hidden='true'></span>".$filevar['name'][$key].'<br />';
				$prohibit['harmful']=true;
			}
		}
	}
	
	return $prohibit;
}



?>