<?php
/* setup page */
define("PAGE_NAME", t("delete_file_page_name", "Delete File"));
define("PAGE_DESCRIPTION", t("delete_file_meta_description", "Delete File"));
define("PAGE_KEYWORDS", t("delete_file_meta_keywords", "delete, remove, file"));
define("TITLE_DESCRIPTION_LEFT", t("file_delete_title_page_description_left", "Please confirm whether to delete the file below."));
define("TITLE_DESCRIPTION_RIGHT", t("file_delete_title_page_description_right", ""));

// load file
$file = null;
if (isset($_REQUEST['_page_url']))
{
    // figure out the delete hash
    $deleteHash = '';
    foreach ($_REQUEST AS $k => $item)
    {
        if (strlen($k) == 32)
        {
            $deleteHash = $k;
        }
    }

    // only keep the initial part if there's a forward slash
    $shortUrl = current(explode("/", str_replace("~d", "", $_REQUEST['_page_url'])));
    $file = file::loadByShortUrl($shortUrl);

    // check it's active
    if ($file->deleteHash != $deleteHash)
    {
        $file = null;
    }
}

// do we have a return page
$returnAccount = false;
if ((isset($_REQUEST['returnAccount'])) && ((int) $_REQUEST['returnAccount'] == 1))
{
    $returnAccount = true;
}

/* load file details */
if (!$file)
{
    /* if no file found, redirect to home page */
    coreFunctions::redirect(WEB_ROOT . "/index." . SITE_CONFIG_PAGE_EXTENSION);
}

/* delete file if submitted */
if ((int) $_REQUEST['delete'])
{
    // reomve file
    $file->removeByUser();

    // redirect to confirmation page
    $resultMsg = t('file_permanently_removed', 'File permanently removed.');
    if ($file->errorMsg)
    {
        $resultMsg = $file->errorMsg;
    }

    if ($returnAccount)
    {
        coreFunctions::redirect(CORE_WEB_ROOT . '/account_home.' . SITE_CONFIG_PAGE_EXTENSION . '?s=' . urlencode($resultMsg));
    }

    coreFunctions::redirect(WEB_ROOT . "/error." . SITE_CONFIG_PAGE_EXTENSION . '?e=' . urlencode($resultMsg));
}

// get file path
$filePath = file::getFileDomainAndPath($file->id);

// include header
require_once(SITE_TEMPLATES_PATH . '/partial/_header.inc.php');
?>
<div class="animated" data-animation="fadeInUp" data-animation-delay="900">
    <form method="post" action="<?php echo $file->getDeleteUrl($returnAccount, true); ?>" id="form-join" AUTOCOMPLETE="off">
        <?php
        if (notification::isErrors())
        {
            echo notification::outputErrors();
        }
        ?>
        <h2><?php echo t('file', 'File'); ?>: <a href="<?php echo _CONFIG_SITE_PROTOCOL; ?>://<?php echo $filePath; ?>/<?php echo $file->shortUrl; ?>" target="_blank"><?php echo $file->originalFilename; ?></a> (<?php echo coreFunctions::formatSize($file->fileSize); ?>)</h2>
        <input name="delete" type="hidden" value="1"/>
        <input name="submitme" type="hidden" value="1"/>
        <input name="returnAccount" type="hidden" value="<?php echo (int) $returnAccount; ?>"/>
        <div class="col-md-6">
            <div class="input-group form-buttons col-md-5 delete-file-button" style="float:left;"><span class="input-group-btn">
                    <button class="btn btn-default" tabindex="99" type="submit" style="width:100%; height:60px;" name="submit"><i class="fa fa-check"></i> <?php echo t("delete_file", "Delete File"); ?></button>
                </span> 
            </div>
            <div class="input-group form-buttons col-md-5 delete-file-button" style="float:left; margin-left:5px;"><span class="input-group-btn">
                    <button class="btn btn-default" tabindex="100" type="reset" name="reset" style="width:100%; background-color:#ccc; border-color:#ccc; height:60px;" onClick="window.location = '<?php echo $returnAccount ? (CORE_WEB_ROOT . '/account_home.' . SITE_CONFIG_PAGE_EXTENSION) : WEB_ROOT; ?>';"><i class="fa fa-times"></i> <?php echo t("cancel", "Cancel"); ?></button>
                </span> 
            </div>   
        </div>  
    </form>
</div>
<?php
// include footer
require_once(SITE_TEMPLATES_PATH . '/partial/_footer.inc.php');
?>
