<?
require_once('include/global.php');
require_once('include/file.php');
require_once('conf.inc.php');

// set
$_mf_attachs_size = $_GET['mf_attachs_size'];

// 멀티업로드의 경우 mf_add_attach_names: ||gitlab.png|1725588952.8972|0||IMG_3672.JPG|1725588952.9724|0||IMG_3716.JPG|1725588953.2261|0
//  - 앞에 || 을 삭제하기 위해서 추가
if ($_GET['type'] === 'json') {
    if (mb_substr($_GET['mf_add_attach_names'], 0,2) === '||') {
        $_GET['mf_add_attach_names'] = mb_substr($_GET['mf_add_attach_names'], 2);
        $_GET['mf_add_attach_names'] = explode("||", $_GET['mf_add_attach_names']);
    }
}

// proc
for ($i=0; $i<sizeof($_GET['mf_add_attach_names']); $i++)
{
    $_ex = explode("|", $_GET['mf_add_attach_names'][$i]);
    $_mf_attach_name = trim($_ex[0]);
    $upload_tmp_name = trim($_ex[1]);
    $_mf_attach_size = trim($_ex[2]);

    if (strlen($_mf_attach_name) > 0 && strlen($upload_tmp_name) > 0 && (strlen($_mf_attach_size) > 0 || $_GET['type'] === 'json'))
    {
        // [2024-09-06] type이 json이고 file_name이 ALL이 아니면 개별삭제, file_name이 ALL이면 전체 삭제
        if (($_GET['type'] === 'json' && ($_GET['file_name'] === $_mf_attach_name || $_GET['file_name'] === 'ALL')) || $_GET['type'] === null) {
            // file del
            #$UPLOAD_ROOT = get_mail_dir().'/tmp';
            $UPLOAD_ROOT = $UPLOAD_ROOT_NEW;
            $upload_dir = $UPLOAD_DIR[BIGFILE_TMP];
            $errorMsg = file_del($upload_tmp_name, "", $upload_dir);

            // javascript
            $_mf_attach_name = addslashes($_mf_attach_name);
            //$_mf_attachs_size = $_mf_attachs_size - $_mf_attach_size;
            //$_mf_attachs_size_h = filesize_human($_mf_attachs_size);

	        if ($_GET['type'] === 'json' && $errorMsg !== null) {
		        echo response_json(array(
			        'error' => str_replace('\n', '  ', $errorMsg),
		        ));
		        exit;
	        }
            ?>
            <script language="JavaScript">
                <!--
                if (parent.addAttachDelSuccess == null && opener.addAttachDelSuccess != null)
                {
                    parentFunc = opener.addAttachDelSuccess;
                } else
                {
                    parentFunc = parent.addAttachDelSuccess;
                }

                if (parentFunc)
                {
                    parentFunc('<?= $_mf_attach_name ?>', '<?= $upload_tmp_name ?>', '<?= $_mf_attachs_size_h ?>', '<?= $_mf_attachs_size ?>', '<?= $_mf_attach_size ?>', '<?= $errorMsg ?>');
                } else
                {
                    alert("올바른 URL로 접근하여 주십시오");
                }
                //-->
            </script>
        <?
        }	// if()
    }	// if()
}	// for()
require_once('include/global_end.php');
?>