<?php

//echo '<pre>';
//print_r($_POST);
//print_r($_FILES);
//exit;
include_once('../../../../../common.php');
include_once(G5_LIB_PATH.'/naver_syndi.lib.php');
include_once(G5_CAPTCHA_PATH.'/captcha.lib.php');


$return = ['res'=>'false','msg'=>'오류','list'=>[]];

if(empty($_POST['act_type'])){ echo json_encode($return);exit; }
if(empty($_POST['bo_table'])){ echo json_encode($return);exit; }
if(empty($_POST['write_table'])){ echo json_encode($return);exit; }

$bo_table = trim($_POST['bo_table']);
$write_table = trim($_POST['write_table']);
$wr_id = empty($_POST['wr_id']) ? 0 : intval($_POST['wr_id']);

//파일삭제시
if(trim($_POST['act_type'])=='delete'){

  if(empty($_POST['bf_file'])){ echo json_encode($return);exit; }
  @unlink(G5_DATA_PATH.'/file/'.$bo_table.'/'.$_POST['bf_file']);
  if(preg_match("/\.({$config['cf_image_extension']})$/i", $_POST['bf_file'])) {
    delete_board_thumbnail($bo_table, $_POST['bf_file']);
  }
    
  sql_query(" delete from {$g5['board_file_table']} where bo_table = '{$bo_table}' and wr_id = '{$wr_id}' and bf_file = '{$_POST['bf_file']}' ");
    
  $return['res'] = 'true';
  $return['msg'] = '파일이 삭제 되었습니다.';
  echo json_encode($return);exit;
}

//이하는 파일추가시
$chars_array = array_merge(range(0,9), range('a','z'), range('A','Z'));
// 디렉토리가 없으면 생성
@mkdir(G5_DATA_PATH.'/file/'.$bo_table, G5_DIR_PERMISSION);
@chmod(G5_DATA_PATH.'/file/'.$bo_table, G5_DIR_PERMISSION);

if(count($_FILES['file']['name'])>0){
  $list = [];
  for($i=0;$i<count($_FILES['file']['name']);$i++){
    $tmp_file  = $_FILES['file']['tmp_name'][$i];
    $filename  = get_safe_filename($_FILES['file']['name'][$i]);
    $filename = preg_replace("/\.(php|pht|phtm|htm|cgi|pl|exe|jsp|asp|inc)/i", "$0-x", $filename);
    shuffle($chars_array);
    $shuffle = implode('', $chars_array);
    $filename = abs(ip2long($_SERVER['REMOTE_ADDR'])).'_'.substr($shuffle,0,8).'_'.replace_filename($filename);
    if(!get_magic_quotes_gpc()){ $filename = addslashes($filename); }
    $timg = @getimagesize($tmp_file);
    if(preg_match("/\.({$config['cf_image_extension']})$/i",$filename) || preg_match("/\.({$config['cf_flash_extension']})$/i",$filename)){
      if ($timg['2'] < 1 || $timg['2'] > 16){ continue; }
    }

    //파일확장자체크
    //$ext_arr = explode('|',$config['cf_image_extension']);
    $ext_arr = explode("|",$config['cf_image_extension']."|".$config['cf_movie_extension']."|hwp|xlsx|xls|zip|pdf|ppt|pptx|docx|doc|txt");
    $arr = explode('.',$filename);
    if(!in_array(end($arr),$ext_arr)){
      @unlink(G5_DATA_PATH.'/file/'.$bo_table.'/'.$filename);
      delete_board_thumbnail($bo_table,$filename);
      $return['msg'] = '지원하지 않는 파일형식 입니다.';
      echo json_encode($return);exit;
    }

    $f = [];
    $f['bf_source'] = $_FILES['file']['name'][$i];
    $f['bf_filesize'] = $_FILES['file']['size'][$i];
    $f['bf_datetime'] = G5_TIME_YMDHIS;
    $f['bf_file'] = $filename;
    $f['bf_width'] = $timg[0];
    $f['bf_height'] = $timg[1];
    $f['bf_type'] = $timg[2];
      
    $pinfo = pathinfo($f['bf_source']);
    $f['extension'] = $pinfo['extension'];
      
    if($f['bf_width'] > 0) {
        $f['view'] = '<img src="'.G5_DATA_URL.'/file/'.$bo_table.'/'.$filename.'" style="max-width:100%;" />';
    } else { 
        $f['view'] = "<div class=\"w_pd\"><a href=\"javascript:void(0);\" class=\"w_etc w_".$pinfo['extension']."\">".$pinfo['extension']."</a></div>";
        
        //$f['view'] = '<img src="'.G5_THEME_URL.'/image/folder.jpg" style="max-width:100%;" />';
    }
    $dest_file = G5_DATA_PATH.'/file/'.$bo_table.'/'.$filename;
    move_uploaded_file($tmp_file,$dest_file);
    chmod($dest_file, G5_FILE_PERMISSION);
    $list[] = $f;
  }
  $return['res'] = 'true';
  $return['msg'] = '업로드 완료';
  $return['list'] = $list;
  echo json_encode($return);exit;
}else{
  $return['msg'] = '파일을 선택하세요';
  echo json_encode($return);exit;
}



?>
