
<%    /**
     * ********************************************************************************
     * 프로그램명	: write_file_upload1.jsp 프로그램설명	: 제출 파일 업로드관련 작 성 자	: 기현테크(jskim) 작
     * 성 일	: 2003. 5.28 최신변경일 : 2013. 4.24
     * *********************************************************************************
     */
%>
<%@ page contentType="text/html; charset=UTF-8"%>

<%@ page import="java.net.*"%>
<%@ page import="kr.co.kihyun.lang.Encoder"%>
<%@ page import="kr.co.kihyun.lang.MString"%>
<%@ page import="java.util.ResourceBundle"%>
<%@ page import="kr.co.kihyun.moumi.Moumi"%>
<%@ include file="/totsys/common/inc/sec/secure.inc.jsp"%>
<%            try {
        /**
         * **** Parameter *****
         */
        String execMode = "";
        String svrFilename = "";
        String usrFilename = "";
        String boardGroupID = "";
        String mode = "";
        String docID = "";
        String reportID = "";
        String boardID = "";
        String path = "";
        String currentFileSize = "";

        execMode = request.getParameter("execMode");
        svrFilename = URLDecoder.decode(MString.checkNull(request.getParameter("svrFilename")), "UTF-8");
        usrFilename = URLDecoder.decode(MString.checkNull(request.getParameter("usrFilename")), "UTF-8");
        boardGroupID = request.getParameter("boardGroupID");
        mode = MString.checkNull(request.getParameter("mode"));
        docID = MString.checkNull(request.getParameter("docID"));
        reportID = MString.checkNull(request.getParameter("reportID"));
        boardID = MString.checkNull(request.getParameter("boardID"));
        path = MString.checkNull(request.getParameter("path"));
        String MSG = request.getParameter("MSG");
        //System.out.print("MSG::::" + MSG);
        String maxSize = request.getParameter("maxSize");
        if (maxSize == null || maxSize.equals("")) {
            maxSize = "1";
        }

        String tmpMsg = "";
        String pageFlag = clearXSS(request.getParameter("pageFlag"), "1");
        if (pageFlag.equals("1")) {
            tmpMsg = "- 관련자료 선택 후 추가버튼";
        } else if (pageFlag.equals("2")) {
            tmpMsg = "- 파일로 제출할 경우에만 추가하세요";
        }

        // 파일이 업로드가 성공하면 서블릿에서 업로드된 파일 크기(byte)를 파라미터로 받는다. (2014.09.02 by youngjun cho.)
        currentFileSize = MString.checkNull(request.getParameter("currentFileSize"));
%>
<% if (MSG != null) {%>
<script>
    var msg = '<%=MSG%>';
    if (msg == "-99") {
        alert("파일사이즈가 제한용량(" +<%=maxSize%> + "Mega Byte) 보다 크기때문에 업로드할수 없습니다");
    } else if (msg == "-77") {
        alert("파일명의 길이가 너무 길어 처리할수 없습니다.");
    } else if (msg == "-88") {
        alert("파일 업로드중 에러가 발생했습니다.");
    } else if (msg == "-66") {
        alert("총 파일사이즈가 제한용량(" +<%=maxSize%> + "Mega Byte) 보다 크기때문에 업로드할수 없습니다");
    } else if(msg=="-33") {
        alert("확장자가 없는 파일입니다.");   //7.위험한 형식 파일 업로드(MultipartRequest)_CWE-434 : Add by KWON,HAN
    } else if(msg=="-55") {
        alert("확장자가 [exe, bat, sh]는 업로드가 제한된 파일입니다.");   //7.위험한 형식 파일 업로드(MultipartRequest)_CWE-434 : Add by KWON,HAN
    } else if(msg=="-44") {
        alert("디렉토리 경로에 특수문자(.. 또는 /)가 포함되어 있습니다.");   //3.디렉토리 경로 조작(getParameter)_CWE-22/23/36 : Add by KWON,HAN
    }
</script>
<% }%>
<%
    String chkExt = "";
    if (execMode != "init") {
        chkExt = checkext(svrFilename, "");
        if (chkExt.equals("false")) {
            execMode = "error";
        }

        chkExt = checkext(usrFilename, "");
        if (chkExt.equals("false")) {
            execMode = "error";
        }
    }
%>
<html>
    <head>
        <META http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <link rel="stylesheet" href="/totsys/common/css/text.css" type="text/css">
        <script src="/totsys/common/js/rollover.js"></script>
        <script type="text/javascript" src="/totsys/common/js/jquery-1.12.4.min.js"></script>
        <script language="javascript">
<!--

            <%if ("csv".equals(mode)) {%>
    parent.viewPage.location = '/totsys/sysadm/org/file_view.jsp?fileName=<%=svrFilename%>';
            <%}%>

    function checkVal() {
        var frm = document.upFrm;
        var filename = frm.appendFile.value;
        var tempFileName = "";
        var refreshFileName = "";

        if (filename == "") {
            alert('<%=Moumi.getMessageBundle().getString("moumi.message.popup.imageSelect")%>');
            return;
        }
            <%if (!"csv".equals(mode)) {%>
        if (num > 4) {
            alert('<%=Moumi.getMessageBundle().getString("moumi.message.popup.attachPossible")%>');
            return;
        }
            <%} else if ("csv".equals(mode)) {%>
        if (num > 0) {
            alert('<%=Moumi.getMessageBundle().getString("moumi.message.popup.userRegistPossible")%>');
            return;
        }
            <%}%>

        // 각 파일들의 크기를 split 하여 배열에 저장한다. (2014.09.02 by youngjun cho.)
        fileSizeList = fileSizes.split(";", num);

        var accumulateFileSize = 0; // 누적 파일 크기

        // 첨부가능한 파일 갯수만큼 loop 를 돌면서 누적 파일 크기를 계산한다.
        for (i = 0; i < num; i++) {
            accumulateFileSize = accumulateFileSize + parseInt(fileSizeList[i]);
        }

        // 업로드 서블릿에 전달할 누적 파일 크기를 hidden value 에 저장한다.
        frm.accumulateFileSize.value = accumulateFileSize;

        //alert("accumulateFileSize : " + accumulateFileSize);

        frm.action = '/servlet/kr.co.kihyun.beans.totsys.board.HttpFileUpload_1?maxSize=<%=maxSize%>';
        frm.submit();
    }

    function exec(execMode) {

        if (execMode == "up") {
            //파일 업로드 모드
            checkFile('<%= svrFilename%>', '<%= usrFilename%>', '<%= currentFileSize%>');

            this.location = './write_file_upload_1.jsp?boardGroupID=<%= boardGroupID%>&execMode=init&docID=<%=docID%>&boardID=<%=boardID%>&mode=<%=mode%>&svrFilename=<%=svrFilename%>&usrFilename=<%=usrFilename%>';

        } else if (execMode == "del") {
            //파일 삭제 모드
            alert('<%=Moumi.getMessageBundle().getString("moumi.message.popup.fileDelete")%>');
            <%if ("csv".equals(mode)) {%>
            this.location = './write_file_upload_1.jsp?boardGroupID=<%= boardGroupID%>&execMode=init&docID=<%=docID%>&boardID=<%=boardID%>&mode=<%=mode%>&reportID=<%=reportID%>';
            <%}%>
        } else if (execMode == "init") {
            //초기 실행 모드
            //alert('<%=Moumi.getMessageBundle().getString("moumi.message.popup.fileAttachStart")%>');
            //this.location='./write_file_upload.jsp?boardGroupID=<%= boardGroupID%>&execMode=init';
        } else if (execMode == "jump") {
            this.location = './write_file_upload_1.jsp?boardGroupID=<%= boardGroupID%>&execMode=init&docID=<%=docID%>&boardID=<%=boardID%>&mode=<%=mode%>&svrFilename=<%=svrFilename%>&usrFilename=<%=usrFilename%>';
        } else {
            alert('<%=Moumi.getMessageBundle().getString("moumi.message.popup.wrongValue")%>');
            return;
        }
    }

    //-->
        </script>

        <script language="javascript">
    num = 0;
    usrFilenames = "";
    svrFilenames = "";
    fileInfos = "";
    userFilePath = "";

    fileSizes = "";

    function checkFile(Filename, Filename2, FileSize) {
            <%if ("sysadm".equals(mode) || "csv".equals(mode)) {%>
        frm = parent.document.multiUpload;
            <%} else if ("docRegist".equals(mode)) {%>
        frm = parent.opener.document.dform;
            <%} else {%>
        frm = parent.document.dform;
            <%}%>
        mkFileList(Filename, Filename2, 'add', FileSize);
        num++;
        frm.fileCount.value = num;
    }

    function mkFileList(Filename, Filename2, size, FileSize, mode) {
            <%if ("sysadm".equals(mode) || "csv".equals(mode)) {%>
        frm = parent.document.multiUpload;
            <%} else if ("docRegist".equals(mode)) {%>
        frm = parent.opener.document.dform;
            <%} else {%>
        frm = parent.document.dform;
            <%}%>
        num = frm.fileCount.value;
        usrFilenames = frm.usrFilenames.value;
        svrFilenames = frm.svrFilenames.value;
        fileInfos = frm.fileInfos.value;

        // 부모 페이지 form 에 "fileSizes" element 가 없으면 생성한다. (2014.09.02 by youngjun cho.)
        // element 가 존재하면(업로드를 한번이라도 한 경우) 부모 페이지 form 의 "fileSizes" 값을 가져온다.
        if (!frm.fileSizes) {
            $(frm).append('<input type="hidden" name="fileSizes" />');
        } else {
            fileSizes = frm.fileSizes.value;
        }

        var tmpSvrFilenames = "";
        var tmpUsrFilenames = "";
        var tmpFileInfos = "";
        //var nList4 = "";
        var tmpFileSizes = "";

        if (!mode) {
            mode = "add";
        }

        subFilename = Filename;
        subFilename2 = cut_str(1, Filename2, "/");
        subinfo = size;
        subFileSize = FileSize;

        usrFilenameList = usrFilenames.split(";", num);
        svrFilenameList = svrFilenames.split(";", num);
        fileInfoList = fileInfos.split(";", num);
        fileSizeList = fileSizes.split(";", num);


        if (mode == "add") {
            if (svrFilenames && usrFilenames) {
                for (i = 0; i < num; i++) {
                    tmpSvrFilenames += svrFilenameList[i] + ";";
                    tmpUsrFilenames += usrFilenameList[i] + ";";
                    tmpFileInfos += fileInfoList[i] + ";";

                    tmpFileSizes += fileSizeList[i] + ";";
                }
                tmpSvrFilenames += subFilename;
                tmpUsrFilenames += subFilename2;
                tmpFileInfos += subinfo;

                tmpFileSizes += subFileSize;
            } else {
                tmpSvrFilenames = subFilename;
                tmpUsrFilenames = subFilename2;
                tmpFileInfos = subinfo;

                tmpFileSizes += subFileSize;
            }
        } else {
            for (i = 0; i < num; i++) {
                if (subFilename == svrFilenameList[i])
                    continue;
                else {
                    tmpSvrFilenames += svrFilenameList[i] + ";";
                    tmpUsrFilenames += usrFilenameList[i] + ";";
                    tmpFileInfos += fileInfoList[i] + ";";
                    tmpFileSizes += fileSizeList[i] + ";";
                }
            }
        }

        usrFilenames = tmpUsrFilenames;
        svrFilenames = tmpSvrFilenames;
        fileInfos = tmpFileInfos;
        fileSizes = tmpFileSizes;

        frm.fileCount.value = num;
        frm.usrFilenames.value = usrFilenames;
        frm.svrFilenames.value = svrFilenames;
        frm.fileInfos.value = fileInfos;
        frm.fileSizes.value = fileSizes;
    }

    function cut_str(state, value, separator) {

        if (!separator) {
            separator = ".";
        }

        if (state == 0) {
            return value.substring(0, value.lastIndexOf(separator));
        }

        if (state == 1) {
            return value.substring(value.lastIndexOf(separator) + 1, value.length);
        }
    }

    function delForm() {
            <%if ("sysadm".equals(mode) || "csv".equals(mode)) {%>
        frm = parent.document.multiUpload;
            <%} else if ("docRegist".equals(mode)) {%>
        frm = parent.opener.document.dform;
            <%} else {%>
        frm = parent.document.dform;
            <%}%>
        num = frm.fileCount.value;
        usrFilenames = frm.usrFilenames.value;
        svrFilenames = frm.svrFilenames.value;
        fileInfos = frm.fileInfos.value;

        if (!frm.fileSizes) {
            $(frm).append('<input type="hidden" name="fileSizes" />');
        } else {
            fileSizes = frm.fileSizes.value;
        }

        var list = document.delFrm.fileList;
        var a = list.options[list.selectedIndex].value;
        var no = -1;

        usrFilenameList = usrFilenames.split(";", num);
        svrFilenameList = svrFilenames.split(";", num);
        fileSizeList = fileSizes.split(";", num);

        for (i = 0; i < num; i++) {
            if (a == svrFilenameList[i]) {
                no = i;
                mkFileList(svrFilenameList[i], usrFilenameList[i], 0, fileSizeList[i], "delete");
            }
        }

        if (no != -1) {
            <%if ("sysadm".equals(mode) || "csv".equals(mode)) {%>
            frm = parent.document.multiUpload;
            <%} else if ("docRegist".equals(mode)) {%>
            frm = parent.opener.document.dform;
            <%} else {%>
            frm = parent.document.dform;
            <%}%>

            dname = "uForm_" + no;
            //iname = "previewImg_" + no;
            //tname = "content_" + no;

            //eval (dname + ".style.visibility = \"hidden\";" );
            //eval (dname + ".style.display    = \"none\";" );

            //frm[iname].src = "";
            //frm[tname].value = "";
            //frm[iname].width = 0;
            //frm[iname].height = 0;

            //if (no < (num - 1)){
            //	reArrange(no, num);
            //}

            if (num > 0) {
                num--;
            }

            frm.fileCount.value = num;
            frm.usrFilenames.value = usrFilenames;
            frm.svrFilenames.value = svrFilenames;
            frm.fileInfos.value = fileInfos;
            frm.fileSizes.value = fileSizes;

            frm2 = document.delFrm;
            frm2.submit();
        }
    }

    function ProcKeyDown() {
        keyPressed = String.fromCharCode(window.event.keyCode);
        if ((keyPressed == "/r") || (keyPressed == "/n")) {
        }
    }
        </script>

    </head>

    <body style='' onload="exec('<%= execMode%>');">
        <table cellSpacing="0" cellPadding="8" width="100%" align="left" border="0">
            <tr>
                <td width="380">
                    <!-- form upFrm -->
                    <form action='/servlet/kr.co.kihyun.beans.totsys.board.HttpFileUpload_1'
                          name='upFrm' enctype='multipart/form-data' method='post'>
                        <input type="hidden" name="boardGroupID" value="<%= boardGroupID%>">
                        <input type="hidden" name="mode" value="<%= mode%>">
                        <input type="hidden" name="docID" value="<%= docID%>">
                        <input type="hidden" name="reportID" value="<%= reportID%>">
                        <input type="hidden" name="boardID" value="<%= boardID%>">
                        <input type="hidden" name="maxSize" value="<%=maxSize%>">
                        <input type="hidden" name="accumulateFileSize" value="0">
                        <%--<table width="100%" cellSpacing="0" cellPadding="0" border="1">--%>
                        <table width="400" cellSpacing="0" cellPadding="0" border="0">
                            <tr>
                                <td align="left" valign="middle">
                                    <input type="file" style="width: 100%" name="appendFile" class="inputtxt" onchange="checkVal();">
                                </td>
                            </tr>
                            <tr>
                                <td style="padding: 10px 0 0 0;">
                                    <font style="font-family: 돋움; font-size: 12px; color: #666;" color=565656><%=tmpMsg%>(파일용량 <%=maxSize%>M 제한)</font>
                                </td>
                            </tr>
                        </table>
                    </form>
                </td>
                <td valign="top">
                    <table width="100%" border=0 cellSpacing="0" cellPadding="0">
                        <tr>
                            <!-- form delFrm -->
                        <form action='/servlet/kr.co.kihyun.beans.totsys.board.HttpFileDelete_1' name='delFrm' method='post'>
                            <input type="hidden" name="boardGroupID" value="<%= boardGroupID%>" class=inputtxt>
                            <input type="hidden" name="mode" value="<%= mode%>">
                            <input type="hidden" name="docID" value="<%= docID%>">
                            <input type="hidden" name="reportID" value="<%= reportID%>">
                            <input type="hidden" name="boardID" value="<%= boardID%>">
                            <input type="hidden" name="maxSize" value="<%=maxSize%>">
                            <td width="300" align="left">
                                <!--파일 업로드된 리스트 박스-->
                                <select name='fileList' style="width: 100%" size=3 class="inputtxt">
                                    <option selected value="-1">
                                        ----------------- <%=Moumi.getMessageBundle().getString("moumi.message.tot_doc.relationDoc")%> -----------------
                                    </option>
                                    <script>
                                        <%
                                            if ("sysadm".equals(mode) || "csv".equals(mode)) {%>
                                        frm = parent.document.multiUpload;
                                        <%} else if ("docRegist".equals(mode)) {%>
                                        frm = parent.opener.document.dform;
                                        <%} else {%>
                                        frm = parent.document.dform;
                                        <%}%>
                                        num = frm.fileCount.value;
                                        usrFilenames = frm.usrFilenames.value;
                                        svrFilenames = frm.svrFilenames.value;
                                        fileInfos = frm.fileInfos.value;

                                        if (!frm.fileSizes) {
                                            $(frm).append('<input type="hidden" name="fileSizes" />');
                                        } else {
                                            fileSizes = frm.fileSizes.value;
                                        }

                                        //alert(num+"\n"+usrFilenames+"\n"+svrFilenames+"\n"+fileInfos);
                                        usrFilenameList = usrFilenames.split(";", num);
                                        svrFilenameList = svrFilenames.split(";", num);
                                        fileSizeList = fileSizes.split(";", num);

                                        for (i = 0; i < num && i < 5; i++)
                                        {
                                            //alert("i="+i+"::"+svrFilenameList[i]+"::"+usrFilenameList[i].substr(0, 20));
                                            if (svrFilenameList[i] == '')
                                                continue;
                                            //System.out.println("i="+i+"::"+svrFilenameList[i]+"::"+usrFilenameList[i].substr(0, 20));
                                            document.write("<option value='" + svrFilenameList[i] + "'>" + usrFilenameList[i].substr(0, 20) + "</option>");
                                        }
                                    </script>
                                </select>
                            </td>
                            <td valign="top">
                                <a href="#">
                                    <img src="./images/bt_file_del.gif" style="margin-left: 10px;" border="0" name='delBtn' value='<%=Moumi.getMessageBundle().getString("moumi.message.button_small.delete")%>' onClick='delForm();' align="absmiddle" />
                                </a>
                            </td>
                        </form>
            </tr>
        </table>
    </td>
</tr>
</table>
</body>
</html>
<%
    } catch (Exception ex) {
        ex.printStackTrace();
    }
%>
