<?php
/**
 * Made By Frody: ffrody@gmail.com (H.S.Choi)
 *
 * 본인 이외에 복제/사용 금지
 */
namespace Frody\Util;

Class MultiFileUpload {
  static public function swfFormFileInfo($fileids, $target_dir, $info_unlink="D"){
    if( !preg_match("/\/$/",$target_dir) ) {
      $target_dir = $target_dir . "/";
    }
    $k = 0;
    $upfiles = array();
    foreach( (array)$fileids as $cid => $fileid ) {
      if (file_exists($target_dir . $fileid . ".info")) {
        $fp = fopen($target_dir . $fileid . ".info", "r");
        while (($line = fgets($fp, 4096)) !== false) {
          $tmp = explode(":", $line);
          $upfiles[$k][$tmp[0]] = chop($tmp[1]);
        }
        if( $upfiles[$k]['error'] ){ //등록시 오류가 발생한 경우 제외
          unset($upfiles[$k]);
          continue;
        }
        $upfiles[$k]['tmp_text'] = $fileid;
        $upfiles[$k]['tmp_name'] = $target_dir . $fileid;
        fclose($fp);
        $k++;
        if ($info_unlink == "D") { // 같은 자리에서 계속 등록을 할 경우 중복등록 방지용으로 삭제를 한다.
          unlink($target_dir . $fileid . ".info");
        }
      }
    }
    return $upfiles;
  }

  static function swfInfoSaveFile($ufile){
    $fileinfo["tmp_name"] = $ufile["tmp_name"];
    $fileinfo["name"]     = $ufile["name"];
    $fileinfo["size"]     = $ufile["size"];
    $fileinfo["type"]     = $ufile["type"];
    $fileinfo['width']    = 0;
    $fileinfo['height']   = 0;
    $fileinfo["error"]   = $ufile["error"];
    if( strpos($ufile['type'], "image") !== false ){
      $iinfo = getimagesize($ufile["tmp_name"]);
      $fileinfo['width'] = $iinfo[0];
      $fileinfo['height'] = $iinfo[1];
    }
    return $fileinfo;
  }

  static function swfFormSaveFile($ufile, $target_dir, $temp_id=""){
    if( !preg_match("/\/$/",$target_dir) ) {
      $target_dir = $target_dir . "/";
    }
    if( $temp_id ){
      $fileid = $temp_id;
    }else{
      $fileid = uniqid("tmpfile_");
    }
    @move_uploaded_file($ufile["tmp_name"], $target_dir . $fileid);
    $ufile['width'] = 0;
    $ufile['height'] = 0;
    if( strpos($ufile['type'], "image") !== false ){
      $iinfo = getimagesize($target_dir . $fileid);
      $ufile['width'] = $iinfo[0];
      $ufile['height'] = $iinfo[1];
    }
    $fp = fopen($target_dir . $fileid . ".info","w+");
    fputs($fp, "name:".   $ufile["name"]."\r\n");
    fputs($fp, "size:".   $ufile["size"]."\r\n");
    fputs($fp, "type:".   $ufile["type"]."\r\n");
    fputs($fp, "width:".  $ufile["width"]."\r\n");
    fputs($fp, "height:". $ufile["height"]."\r\n");
    fputs($fp, "error:".  $ufile["error"]."\r\n");
    fclose($fp);
    return $fileid;
  }

  static function swfMoveUploadedFile($src, $tgt){
    rename($src, $tgt);
  }

  static function swfSaveFile($ufile, $sfile){
    @move_uploaded_file($ufile["tmp_name"], $sfile);
    $upfiles['name'] = $ufile['name'];
    $upfiles['size'] = $ufile['size'];
    return json_encode($upfiles);
  }

  static function swfCleanDir($target_dir){
    if( !is_dir($target_dir) ) return;
    $dp = opendir($target_dir);
    if( !preg_match("/\/$/",$target_dir) ) {
      $target_dir = $target_dir . "/";
    }
    while(false !== ($file = readdir($dp))) {
      if ($file != "." && $file != "..") {
        $ftime = filectime($target_dir.$file);
        if( $ftime < time()-3600*24 ){
          @unlink($target_dir.$file);
        }
      }
    }
  }

  static public function mime_type( $file_ext ){
    switch ( $file_ext )
    {
      case "doc" :    $mime_type = "application/msword";
        break;
      case "xls" :
      case "xlw" :
      case "xla" :
      case "xlc" :
      case "xlm" :
      case "xlt" :    $mime_type= "application/vnd.ms-excel";
          break;
      case "ppt" :
      case "pps" :
      case "pot" :    $mime_type= "application/vnd.ms-powerpoint";
          break;
      case "mpp" :    $mime_type= "application/vnd.ms-project";
          break;
      case "mdb" :    $mime_type= "application/x-msaccess";
          break;
      case "pub" :    $mime_type= "application/x-mspublisher";
          break;
      case "scd" :    $mime_type= "application/x-msschedule";
          break;
      case "hlp" :    $mime_type= "application/winhlp";
          break;
      case "crd" :    $mime_type= "application/x-mscardfile";
          break;
      case "clp" :    $mime_type= "application/x-msclip";
          break;
      case "m13" :
      case "m14" :    $mime_type= "application/x-msmediaview ";
          break;
      case "wmf" :    $mime_type= "application/x-msmetafile";
          break;
      case "mny" :    $mime_type= "application/x-msmoney";
          break;
      case "trm" :    $mime_type= "application/x-msterminal";
          break;
      case "wri" :    $mime_type= "application/x-mswrite";
          break;
      case "bin" :
      case "exe" :
      case "com" :    $mime_type= "application/octet-stream";
          break;
      case "asd" :
      case "asn" :    $mime_type= "application/astound";
          break;
      case "lcc" :    $mime_type= "application/fastman";
          break;
      case "hqx" :    $mime_type= "application/mac-binhex40";
          break;
      case "mbd" :    $mime_type= "application/mbedlet";
          break;
      case "oda" :    $mime_type= "application/oda";
          break;
      case "pdf" :    $mime_type= "application/pdf";
          break;
      case "ai" :
      case "eps" :
      case "ps" :     $mime_type= "application/postscript";
          break;
      case "rtf" :    $mime_type= "application/rtf";
          break;
      case "smp" :    $mime_type= "application/studiom";
          break;
      case "tbt" :    $mime_type= "application/timbuktu";
          break;
      case "js" :     $mime_type= "application/x-javascript";
          break;
      case "asp" :    $mime_type= "application/x-asap";
          break;
      case "csh" :    $mime_type= "application/x-csh";
          break;
      case "dot" :    $mime_type= "application/x-dot";
          break;
      case "dvi" :    $mime_type= "application/x-dvi";
          break;
      case "etc" :    $mime_type= "application/x-earthtime";
          break;
      case "evy" :    $mime_type= "application/x-envoy";
          break;
      case "xll" :    $mime_type= "application/x-excel";
          break;
      case "gtar" :   $mime_type= "application/x-gtar";
          break;
      case "hdf" :    $mime_type= "application/x-hdf";
          break;
      case "latex" :  $mime_type= "application/x-latex";
          break;
      case "fm" :     $mime_type= "application/x-maker";
          break;
      case "mi" :
      case "mif" :    $mime_type= "application/x-mif";
          break;
      case "mocha" :
      case "moc" :    $mime_type= "application/x-mocha";
          break;
      case "ins" :    $mime_type= "application/x-NET-Install";
          break;
      case "nc" :
      case "cdf" :    $mime_type= "application/x-netcdf";
          break;
      case "proxy" :  $mime_type= "application/x-ns-proxy-autoconfig";
          break;
      case "css" :    $mime_type= "application/x-pointplus";
          break;
      case "slc" :    $mime_type= "application/x-salsa";
          break;
      case "sh" :     $mime_type= "application/x-sh";
          break;
      case "shar" :   $mime_type= "application/x-shar";
          break;
      case "spr" :
      case "sprite" : $mime_type= "application/x-sprite";
          break;
      case "tar" :    $mime_type= "application/x-tar";
          break;
      case "tcl" :    $mime_type= "application/x-tcl";
          break;
      case "tex" :    $mime_type= "application/x-tex";
          break;
      case "texi" :
      case "texinfo" :        $mime_type= "application/x-texinfo";
          break;
      case "tbp" :    $mime_type= "application/x-timbuktu";
          break;
      case "tki" :
      case "tkined" : $mime_type= "application/x-tkined";
          break;
      case "man" :    $mime_type= "application/x-troff-man";
          break;
      case "me" :     $mime_type= "application/x-troff-me";
          break;
      case "ms" :     $mime_type= "application/x-troff-ms";
          break;
      case "t" :
      case "tr" :
      case "roff" :   $mime_type= "application/x-troff";
          break;
      case "src" :    $mime_type= "application/x-wais-source";
          break;
      case "zip" :    $mime_type= "application/zip";
          break;
      case "hwp" :    $mime_type= "application/hwp";
          break;
      case "au" :
      case "snd" :    $mime_type= "audio/basic";
          break;
      case "es" :
      case "esl" :    $mime_type= "audio/echospeech";
          break;
      case "midi" :
      case "mid" :    $mime_type= "audio/midi";
          break;
      case "aif" :
      case "aiff" :
      case "aifc" :   $mime_type= "audio/x-aiff";
          break;
      case "wav" :    $mime_type= "audio/x-wav";
          break;
      case "ra" :
      case "ram" :    $mime_type= "audio/x-pn-realaudio";
          break;
      case "pac" :    $mime_type= "audio/x-pac";
          break;
      case "pae" :    $mime_type= "audio/x-epac";
          break;
      case "fif" :    $mime_type= "image/fif";
          break;
      case "gif" :    $mime_type= "image/gif";
          break;
      case "ief" :    $mime_type= "image/ief";
          break;
      case "ifs" :    $mime_type= "image/ifs";
          break;
      case "jpg" :
      case "jpe" :
      case "jpeg" :   $mime_type= "image/jpeg";
          break;
      case "png" :    $mime_type= "image/png";
          break;
      case "tif" :
      case "tiff" :   $mime_type= "image/tiff";
          break;
      case "dwg" :
      case "svf" :    $mime_type= "image/vnd";
          break;
      case "wi" :     $mime_type= "image/wavelet";
          break;
      case "bmp" :    $mime_type= "image/bmp";
          break;
      case "ras" :    $mime_type= "image/x-cmu-raster";
          break;
      case "pnm" :    $mime_type= "image/x-portable-anymap";
          break;
      case "pbm" :    $mime_type= "image/x-portable-bitmap";
          break;
      case "pgm" :    $mime_type= "image/x-portable-graymap";
          break;
      case "ppm" :    $mime_type= "image/x-portable-pixmap";
          break;
      case "rgb" :    $mime_type= "image/x-rgb";
          break;
      case "xbm" :    $mime_type= "image/x-xbitmap";
          break;
      case "xpm" :    $mime_type= "image/x-xpixmap";
          break;
      case "xwd" :    $mime_type= "image/x-xwindowdump";
          break;

      case "htm" :
      case "html" :   $mime_type= "text/html";
          break;
      case "txt" :    $mime_type= "text/plain";
          break;
      case "rtx" :    $mime_type= "text/richtext";
          break;
      case "tsv" :    $mime_type= "text/tab-separated-values";
          break;
      case "etx" :    $mime_type= "text/x-setext";
          break;
      case "talk" :   $mime_type= "text/x-speech";
          break;
      case "fvi" :    $mime_type= "video/isivideo";
          break;
      case "mpg" :
      case "mpe" :
      case "mpeg" :   $mime_type= "video/mpeg";
          break;
      case "avi" :    $mime_type= "video/msvideo";
          break;
      case "qt" :
      case "mov" :    $mime_type= "video/quicktime";
          break;
      case "viv" :
      case "vivo" :   $mime_type= "video/vivo";
          break;
      case "wv" :     $mime_type= "video/wavelet";
          break;
      case "movie" :  $mime_type= "video/x-sgi-movie";
          break;
      case "svr" :    $mime_type= "x-world/x-svr";
          break;
      case "wrl" :    $mime_type= "x-world/x-vrml";
          break;
      case "vrt" :    $mime_type= "x-world/x-vrt";
          break;

      case "ice" :    $mime_type= "x-conference/x-cooltalk";
          break;
      case "gz" :     $mime_type= "x-gzip";
          break;
      case "z" :      $mime_type= "x-compress";
          break;
      case "uue" :
      case "uu" :     $mime_type= "x-uuencode";
          break;
      case "map" :    $mime_type= "magnus-internal/imagemap";
          break;
      case "shtml" :  $mime_type= "magnus-internal/parsed-html";
          break;
      case "bat" :
      case "cgi" :    $mime_type= "magnus-internal/cgi";
          break;
      default :
          $mime_type= "application/octet-stream";
    }
    return $mime_type;
  }
}
?>