<?php
function ConnectDB($data){
	$link = odbc_connect($data['host'],$data['user'],$data['pass']);
	if($link){
		odbc_exec($link,"set names 'UTF8'");
		return $link;
	}
}

function ConnectMYSQL($data){
	$connect = mysql_connect($data['host'],$data['user'],$data['pass']);
	mysql_query('SET NAMES utf8');
	if($connect){
		//mysql_select_db('eageonline', $connect);
		return $connect;
	}
}

function JSLINK($url){
	echo "<script>window.location.href='".$url."';</script>";
	exit();
}

function JSALERT($msg){
	echo "<script>alert('".$msg."');</script>";
}

function JSReload(){
	echo "<script>top.window.location.href = top.window.location.href;</script>";
}

function JSTOPLINK($url){
	echo "<script>top.window.location.href='".$url."';</script>";
	exit();
}



function detectPhpSymbol($data){

	$symbolHack = array("<?php", "?>", "<?", "&lt;?",'\"',"&gt;","&lt;");
	$antiHack = array("&#60;?php", "?&#62;", "&#60;?", "&#60;?",'"',">","<");
	$cleanData = str_replace($symbolHack, $antiHack, $data);
	
	return $cleanData;
}
function translatePhpSymbol($data){

	$symbolHack = array("<?php", "?>", "<?", "&lt;?", "&#60;?php", "?&#62;", "&#60;?");
	$antiHack = array("< ?php", "? >", "< ?", "< ?", "< ?php", "? >", "< ?", );
	$cleanData = str_replace($symbolHack, $antiHack, $data);
	
	return $cleanData;
}

function CheckSpecialChar($data){
	$allow  = array('a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','0','1','2','3','4','5','6','7','8','9');
	$count  = strlen($data);
	$string = strtolower($data);
 	
	for($i=0;$i<$count;$i++){
		$str = substr($string,$i,1);
		if(in_array($str,$allow)){
			$status = "Y";
		}else{
			$status = "N";
			$i = $count;
		}
	}
	return $status;
}

function CheckCompanyName($data){
	$allow  = array('a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','0','1','2','3','4','5','6','7','8','9',' ');
	$count  = strlen($data);
	$string = strtolower($data);
 	
	for($i=0;$i<$count;$i++){
		$str = substr($string,$i,1);
		if(in_array($str,$allow)){
			$status = "Y";
		}else{
			$status = "N";
			$i = $count;
		}
	}
	return $status;
}



function check_email_address($email){
	return eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $email);
}

function SQL_INJECT($data){

	$cleanData	=	htmlentities($data , ENT_QUOTES);

	return $cleanData;
}

function SQL_INJECT_CN($data){

	$cleanData	=	str_replace("'","&#39;",$data);
	$cleanData	=	str_replace('"','&quot;',$cleanData);
	$cleanData	=	str_replace('`','&#96;',$cleanData);
	return $cleanData;
}


function IMAGE($img,$w,$h){ //,$t
	$max_width  = $w; 
	$max_height = $h; 
	list($width, $height) = getimagesize($img); 
	$ratioh = $max_height/$height; 
	$ratiow = $max_width/$width; 
	$ratio = min($ratioh, $ratiow); 
	// New dimensions 
	$width = intval($ratio*$width); 
	$height = intval($ratio*$height);
	//echo " width='".$width."' height='".$height."' ";
	/*
	if($t=='w'){
		return $width;	
	}else{
		return $height;
	}
	*/
	$return['width']	= $width;
	$return['height']	= $height;
	
	return $return;
}
function UploadImage($img, $width, $height, $ftp_server, $ftp_user_name, $ftp_user_pass){
	if($img["name"]){
		//////////////upload image///////////////////
		$allowedExts = array("gif", "jpeg", "jpg", "png","GIF","JPG","JPEG","PNG");
		$temp = explode(".", $img["name"]);
		$extension = end($temp);
		if ((($img["type"] == "image/gif")
		|| ($img["type"] == "image/jpeg")
		|| ($img["type"] == "image/jpg")
		|| ($img["type"] == "image/pjpeg")
		|| ($img["type"] == "image/x-png")
		|| ($img["type"] == "image/png"))
		&& ($img["size"] < 51200000)
		&& in_array($extension, $allowedExts)){
			if ($img["error"] > 0){
				$msg = "Image error. ".$img["error"]."<br>";
			}else{
					$NEW_FILE_NAME	=	date('YmdHis').rand(11111,99999).'.'.$temp[1];
					$target_dir = '//yanwojy/images/upload/ori/';							
					$conn_id = ftp_connect($ftp_server);
					
					// login with username and password
					$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
					
					// check if exist
					if (!ftp_nlist($conn_id, $target_dir.$NEW_FILE_NAME)){
						// upload a file
						if (ftp_put($conn_id, $target_dir.$NEW_FILE_NAME, $img["tmp_name"], FTP_BINARY)){
							
							$imageurl = 'http://www.yanwojy.com/images/upload/ori/'.$NEW_FILE_NAME;
							
							$file = $imageurl; 
							$target					=	IMAGE($imageurl,$width,$height);
							$target_width			=	$target['width']; //IMAGE($imageurl,'700','500','w');
							$target_height 			=	$target['height']; //IMAGE($imageurl,'700','500','h');
							$target_layer			=	imagecreatetruecolor($target_width,$target_height);
							$source_properties 		= 	getimagesize($file);
							
							switch(strtolower($temp[1])) {
								case 'jpeg':
								case 'jpg':
										$image_resource_id = imagecreatefromjpeg($imageurl);
								break;

								case 'png':
										$image_resource_id = imagecreatefrompng($imageurl);
								break;

								case 'gif':
										$image_resource_id = imagecreatefromgif($imageurl);
								break;
							}
							$imageurl = 'http://www.yanwojy.com/images/upload/'.$NEW_FILE_NAME;
							
							imagecopyresampled($target_layer,$image_resource_id,0,0,0,0,$target_width,$target_height, $source_properties[0],$source_properties[1]);
							
							//imagejpeg($target_layer);
							imagejpeg($target_layer,"images/upload/".$NEW_FILE_NAME);
						}else{
							$msg = "There was a problem while uploading your image"; //error_get_last();
						}
					}else{
						$msg = $img["name"]." already exists.";
					}
					
					// close the connection
					ftp_close($conn_id);
				//}
			}
		}else{
			$msg = "Invalid Image file: max file size is 500kb";
		}
	}
	
	$return['imageurl'] = $imageurl;
	$return['msg'] = $msg;
	
	return $return;
}

?>