<?php
$sub_menu = "550100";
include_once('./_common.php');

check_demo();

auth_check($auth[$sub_menu], 'w');

if ($is_admin != 'super')
    alert('최고관리자만 접근 가능합니다.');

check_token();

$upload_dir = G5_PATH.'/data/file/';

// 디렉토리가 없다면 생성.
@mkdir("$upload_dir", 0707);
@chmod("$upload_dir", 0707);


function file_upload($files, $upload_dir, $allow_type="", $limit_size="")
{
    $total_size = 0;
    $upfile = array();

    // 단일 파일이면 배열로..
    if (!is_array($files[tmp_name])) {
        $files[tmp_name] = array($files[tmp_name]);
        $files[name] = array($files[name]);
        $files[type] = array($files[type]);
        $files[size] = array($files[size]);
    }

    for ($i=0; $i<count($files[tmp_name]); $i++)
    {
        if (is_uploaded_file($files[tmp_name][$i]))
        {
            $upfile[$i][file_name] = get_file_name($upload_dir, $files[name][$i]);
            $upfile[$i][real_name] = $files[name][$i];
            //$upfile[$i]mime_type] = $files[type][$i];
            $upfile[$i][file_size] = $files[size][$i];
            $upfile[$i][extension]=strtolower(array_pop(explode('.', $files[name][$i])));

            // 허용타입 체크
            if ($allow_type) {
                if (!allow_file_type($upfile[$i][extension], $allow_type))
                    alert("허용하지 않는 파일형식 입니다.");
            }

            // 허용용량 체크 byte 단위로 세트...
            $total_size += $upfile[$i][file_size];

            if ($limit_size) {
                if ((int)$upfile[$i][file_size] > (int)($limit_size))
                    alert("파일용량이 허용치를 초과하였습니다.");
            }

            // 파일복사
            $tmp_upload_file = $upload_dir.$upfile[$i][file_name];

            if (!move_uploaded_file($files[tmp_name][$i], $tmp_upload_file))
                alert("파일명 : $upfile[real_name]\\n파일을 업로드할 수 없습니다.");

            @chmod($tmp_upload_file, 0606);

            $upfile[$i][image] = @getimagesize($tmp_upload_file);
        }
        else
        {
            $upfile[$i][file_name] = "";
        }
    }
    return $upfile;
}

// get unique filename
function get_file_name($upload_dir, $file_name)
{
global $g5;
    // 아래의 문자열이 들어간 파일은 -x 를 붙여서 웹경로를 알더라도 실행을 하지 못하도록 함
    $file_name = preg_replace("/\.(php|phtm|htm|cgi|pl|exe|jsp|asp|inc|swf)/i", "$0-x", $file_name);

    $file_name = abs(ip2long($_SERVER[REMOTE_ADDR])).'_'.substr(md5(uniqid($g5[server_time])),0,8).'_'.str_replace('%', '', urlencode($file_name));

    // duplicate check
    srand((double)microtime()*1000000);
    while (file_exists($upload_dir.$file_name)) {
        $seed = rand(100,999);
        $file_name=$seed."_".$file_name;
        if (!file_exists($upload_dir.$file_name)) break;
    }

    return $file_name;
}

function allow_file_type($file_type, $allow_type)
{
    $rtn = false;
    $allow_type_array = explode(",", $allow_type);
    if (in_array($file_type, $allow_type_array))
        $rtn = true;

    return $rtn;
}

$sql_file = "";

/* 업로드 시작 */
$upload = file_upload($_FILES['okone_file'],$upload_dir);

if(!$okone_file_chk){
	if($upload[0][file_name]){
		$okone_file_name = $upload[0][file_name];
		$okone_file_source = $upload[0][real_name];
		$okone_file_type = $upload[0][image][2];

		$sql = " update okone_basic
                set okone_company = '{$_POST['okone_company']}',
                okone_facebook = '{$_POST['okone_facebook']}',
                okone_twitter = '{$_POST['okone_twitter']}',
                okone_youtube = '{$_POST['okone_youtube']}',
                okone_map = '{$_POST['okone_map']}',
				 okone_file_name = '$okone_file_name',
				 okone_file_source = '$okone_file_source',
				 okone_file_type = '$okone_file_type' where idx='1'  ";
		sql_query($sql);

	} else {

		$sql = " update okone_basic
                set okone_company = '{$_POST['okone_company']}',
                okone_facebook = '{$_POST['okone_facebook']}',
                okone_twitter = '{$_POST['okone_twitter']}',
                okone_youtube = '{$_POST['okone_youtube']}',
                okone_map = '{$_POST['okone_map']}' where idx='1'  ";
		sql_query($sql);
	}
}else{
	if($upload[0][file_name]){
	$rs = sql_fetch("select okone_file_name from okone_basic ");
	@unlink($upload_dir.$rs['okone_file_name']);

		$okone_file_name = $upload[0][file_name];
		$okone_file_source = $upload[0][real_name];
		$okone_file_type = $upload[0][image][2];

		$sql = " update okone_basic
                set okone_company = '{$_POST['okone_company']}',
                okone_facebook = '{$_POST['okone_facebook']}',
                okone_twitter = '{$_POST['okone_twitter']}',
                okone_youtube = '{$_POST['okone_youtube']}',
                okone_map = '{$_POST['okone_map']}',
				 okone_file_name = '$okone_file_name',
				 okone_file_source = '$okone_file_source',
				 okone_file_type = '$okone_file_type' where idx='1'  ";
		sql_query($sql);

	} else {
		@unlink($upload_dir.$rs['okone_file_name']);
		$okone_file_name = '';
		$okone_file_source = '';
		$okone_file_type = '';
		$sql = " update okone_basic
                set okone_company = '{$_POST['okone_company']}',
                okone_facebook = '{$_POST['okone_facebook']}',
                okone_twitter = '{$_POST['okone_twitter']}',
                okone_youtube = '{$_POST['okone_youtube']}',
                okone_map = '{$_POST['okone_map']}',
				 okone_file_name = '$okone_file_name',
				 okone_file_source = '$okone_file_source',
				 okone_file_type = '$okone_file_type' where idx='1'  ";

    sql_query($sql);
	}

}

goto_url('./okone.php', false);
?>
