<?
/*********************************** 팝업관리 *************************************/
//팝업 등록하기
function insertPopup(){
	// 테이블 지정
	$tbl = $GLOBALS["_conf_tbl"]["popup"];

	//이미지파일 처리
	if ($_FILES[photo_file][error] == 0){
		//확장자 검사후 파일이름 생성
		$filename = $_FILES[photo_file][name];
		$attach_ext = explode(".",$filename);
		$extension = $attach_ext[sizeof($attach_ext)-1];
		$extension = strtolower($extension);		    
		$filerename = md5(mktime()) . "." . $extension;
		$filesize = $_FILES[photo_file][size];
		$filetype = $_FILES[photo_file][type];
			
		// 파일 확장자 검사
		if(!strcmp($extension,"htm") ||!strcmp($extension,"html") ||!strcmp($extension,"phtml") ||!strcmp($extension,"php") ||!strcmp($extension,"php3") ||!strcmp($extension,"php4") ||!strcmp($extension,"inc") ||!strcmp($extension,"pl") ||!strcmp($extension,"cgi")){
			jsMsg("not allowed file extension");
			jsHistory("-1");
		}
		
		if (is_uploaded_file($_FILES[photo_file][tmp_name])) {	
			move_uploaded_file ($_FILES[photo_file][tmp_name],$GLOBALS["_SITE"]["UPLOADED_DATA"] . "/popup/".$filerename);
		}
	}

	// 테이블에 입력
	$sql = "INSERT INTO ".$tbl." set 
		subject='".mysql_real_escape_string($_POST[subject])."',
		contents='".mysql_real_escape_string($_POST[contents])."',
		width='".mysql_real_escape_string($_POST[width])."',
		height='".mysql_real_escape_string($_POST[height])."',
		p_mode='".mysql_real_escape_string($_POST[p_mode]=="P"?"P":"L")."',
		p_type='".mysql_real_escape_string($_POST[p_type]=="IMG"?"IMG":"HTML")."',
		p_image='".$filerename."',
		p_url='".mysql_real_escape_string($_POST[p_url])."',
		p_target='".mysql_real_escape_string($_POST[p_target]=="O"?"O":"B")."',
		s_date='".mysql_real_escape_string($_POST[s_date])."',
		e_date='".mysql_real_escape_string($_POST[e_date])."',
		pop_top='".mysql_real_escape_string($_POST[pop_top])."',
		pop_left='".mysql_real_escape_string($_POST[pop_left])."',				
		w_date=now()
	";

	$rs = mysql_query($sql, $GLOBALS[dblink]);
	$total = mysql_affected_rows($GLOBALS[dblink]);

	if($total > 0){
		return true;
	}else{
		return false;
	}
}



//팝업 수정하기
function editPopup($idx){
	// 테이블 지정
	$tbl = $GLOBALS["_conf_tbl"]["popup"];

	//이미지파일 처리
	$arrInfo = getArticleInfo($tbl, $idx);



	if ($_FILES[photo_file][error] == 0){
		//확장자 검사후 파일이름 생성
		$filename = $_FILES[photo_file][name];
		$attach_ext = explode(".",$filename);
		$extension = $attach_ext[sizeof($attach_ext)-1];
		$extension = strtolower($extension);		    
		$filerename = md5(mktime()) . "." . $extension;
		$filesize = $_FILES[photo_file][size];
		$filetype = $_FILES[photo_file][type];
			
		// 파일 확장자 검사
		if(!strcmp($extension,"htm") ||!strcmp($extension,"html") ||!strcmp($extension,"phtml") ||!strcmp($extension,"php") ||!strcmp($extension,"php3") ||!strcmp($extension,"php4") ||!strcmp($extension,"inc") ||!strcmp($extension,"pl") ||!strcmp($extension,"cgi")){
			jsMsg("not allowed file extension");
			jsHistory("-1");
		}
		
		if (is_uploaded_file($_FILES[photo_file][tmp_name])) {	
			unlink($GLOBALS["_SITE"]["UPLOADED_DATA"] . "/popup/".$arrInfo["list"][0][p_image]);
			move_uploaded_file ($_FILES[photo_file][tmp_name],$GLOBALS["_SITE"]["UPLOADED_DATA"] . "/popup/".$filerename);
		}
	}else{
		$filerename = $arrInfo["list"][0][p_image];
	}

	// 테이블에 입력
	$sql = "UPDATE ".$tbl." set 
		subject='".mysql_real_escape_string($_POST[subject])."',
		contents='".mysql_real_escape_string($_POST[contents])."',
		width='".mysql_real_escape_string($_POST[width])."',
		height='".mysql_real_escape_string($_POST[height])."',
		p_mode='".mysql_real_escape_string($_POST[p_mode]=="P"?"P":"L")."',
		p_type='".mysql_real_escape_string($_POST[p_type]=="IMG"?"IMG":"HTML")."',
		p_image='".$filerename."',
		p_url='".mysql_real_escape_string($_POST[p_url])."',
		p_target='".mysql_real_escape_string($_POST[p_target]=="O"?"O":"B")."',
		s_date='".mysql_real_escape_string($_POST[s_date])."',
		e_date='".mysql_real_escape_string($_POST[e_date])."',
		pop_top='".mysql_real_escape_string($_POST[pop_top])."',
		pop_left='".mysql_real_escape_string($_POST[pop_left])."'		
		WHERE idx='".mysql_real_escape_string($_POST[idx])."'
	";

	$rs = mysql_query($sql, $GLOBALS[dblink]);
	$total = mysql_affected_rows($GLOBALS[dblink]);

	if($rs){
		return true;
	}else{
		return false;
	}

}

//팝업 삭제하기
function deletePopup($idx){
	// 테이블 지정
	$tbl = $GLOBALS["_conf_tbl"]["popup"];

	//이미지파일 처리
	$arrInfo = getArticleInfo($tbl, $idx);

	//게시판 테이블에서 삭제
	$sql = "DELETE FROM ".$tbl." 
		WHERE idx='".mysql_real_escape_string($_POST[idx])."'
	";

	$rs = mysql_query($sql, $GLOBALS[dblink]);
	$total = mysql_affected_rows($GLOBALS[dblink]);

	if($total > 0){
		@unlink($GLOBALS["_SITE"]["UPLOADED_DATA"] . "/popup/".$arrInfo["list"][0][p_image]);
		return true;
	}else{
		return false;
	}
}

//팝업 가져오기
function getActivePopup(){
	$tbl = $GLOBALS["_conf_tbl"]["popup"];

	$sql  = "SELECT * ";
	$sql .= "FROM ".$tbl." ";
	$sql .= "WHERE s_date <= curdate() ";
	$sql .= "AND e_date >= curdate() ";
	$rs = mysql_query($sql, $GLOBALS[dblink]);
	$total_rs = mysql_num_rows($rs);
	
	if($total_rs > 0){
			$list['total'] = $total_rs;
			for($i=0; $i < $total_rs; $i++){
					$list['list'][$i] = mysql_fetch_assoc($rs);
			}
	}else{
			$list['total'] = 0;
	}
	return $list;
}
?>