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

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

    // check whether we can display the delete/password options
    $showAll = false;
    if ($file->deleteHash == $loggedInHash)
    {
        $showAll = true;
    }

    // double check the owner for logged in user
    if (($showAll == false) && ($file))
    {
        if ($file->userId == $Auth->id)
        {
            $showAll = true;
        }
    }
}
$user = userPeer::loadUserById($Auth->id);

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

// only show this page if active file
if ($file->statusId != 1)
{
    // redirect to file in order to show error
    coreFunctions::redirect($file->getFullShortUrl());
}

// check file permissions, allow owners, non user uploads and admin/mods
if($file->userId != null)
{
	if((($file->userId != $Auth->id) && ($Auth->level_id < 10)))
	{
		// if this is a private file
		if(coreFunctions::getOverallPublicStatus($file->userId, $file->folderId, $file->id) == false)
		{
			$errorMsg = t("error_file_is_not_publicly_shared", "File is not publicly available.");
			coreFunctions::redirect(coreFunctions::getCoreSitePath() . "/error." . SITE_CONFIG_PAGE_EXTENSION . "?e=" . urlencode($errorMsg));
		}
	}
}

// setup page
define("PAGE_NAME", t("share_file_title", "File Information"));
define("PAGE_DESCRIPTION", t("file_information_description", "Information about") . ' ' . $file->originalFilename);
define("PAGE_KEYWORDS", strtolower($file->originalFilename) . t("file_information_meta_keywords", ", share, information, file, upload, download, site"));
define("TITLE_DESCRIPTION_LEFT", t("file_information_left_description", "Information about") . ' ' . $file->originalFilename);
define("TITLE_DESCRIPTION_RIGHT", t("share_files_title_page_description_right", ""));

// include header
require_once(SITE_TEMPLATES_PATH . '/partial/_header.inc.php');
?>
<div class="animated" data-animation="fadeInUp" data-animation-delay="900">
    <table style="width:100%;">
        <tbody>
            <tr>
                <td>
                    <table class="table table-bordered table-striped">
                        <tbody>
                            <tr>
                                <td class="share-file-table-header">
                                    <?php echo UCWords(t('filename', 'filename')); ?>:
                                </td>
                                <td class="responsiveInfoTable">
                                    <?php echo validation::safeOutputToScreen($file->originalFilename, null, 70); ?>
                                    &nbsp;&nbsp;<a href="<?php echo validation::safeOutputToScreen($file->getFullShortUrl()); ?>" target="_blank">(<?php echo t('download', 'download'); ?>)</a>
                                    <?php if ($Auth->id != $file->userId): ?>
                                        &nbsp;&nbsp;<a href="<?php echo CORE_PAGE_WEB_ROOT . '/account_copy_file.php?f=' . $file->shortUrl; ?>">(<?php echo t('copy_into_your_account', 'copy file'); ?>)</a>
                                    <?php endif; ?>
                                </td>
                            </tr>
                            <tr>
                                <td class="share-file-table-header">
                                    <?php echo UCWords(t('filesize', 'filesize')); ?>:
                                </td>
                                <td class="responsiveInfoTable">
                                    <?php echo coreFunctions::formatSize($file->fileSize); ?>
                                </td>
                            </tr>
                        </tbody>
                    </table>
                    <?php if ((SITE_CONFIG_FORCE_FILES_PRIVATE == 'no') && ($file->isPublic == 1) && ($user->isPublic == 1) && coreFunctions::getUserFoldersPublicStatus($file->folderId) == true): ?>
                    <table class="table table-bordered table-striped">
                        <tbody>
                            <tr>
                                <td class="share-file-table-header">
                                    <?php echo UCWords(t('url', 'url')); ?>:
                                </td>
                                <td class="responsiveInfoTable">
                                    <a href="<?php echo validation::safeOutputToScreen($file->getFullShortUrl()); ?>" target="_blank"><?php echo validation::safeOutputToScreen($file->getFullShortUrl()); ?></a>
                                </td>
                            </tr>
                        </tbody>
                    </table>
                    <?php endif; ?>
                </td>
                <?php if ($file->getLargeIconPath()): ?>
                    <td class="share-file-last-cell animated reponsiveMobileHide"  data-animation="bounceIn" data-animation-delay="1300">
                        <img src="<?php echo $file->getLargeIconPath(); ?>" width="160" alt="<?php echo strtolower($file->extension); ?>"/>
                    </td>
                <?php endif; ?>

            </tr>
        </tbody>
    </table>
    <?php if ((SITE_CONFIG_FORCE_FILES_PRIVATE == 'no') && ($file->isPublic == 1) && ($user->isPublic == 1) && coreFunctions::getUserFoldersPublicStatus($file->folderId) == true): ?>
    <h2><?php echo t("download_urls", "download urls"); ?></h2>
    <table class="table table-bordered table-striped">
        <tbody>
            <tr>
                <td class="share-file-table-header">
                    <?php echo t('html_code', 'HTML Code'); ?>:
                </td>
                <td class="responsiveInfoTable">
                    <?php echo $file->getHtmlLinkCode(); ?>
                </td>
            </tr>
            <tr>
                <td class="share-file-table-header">
                    <?php echo UCWords(t('forum_code', 'forum code')); ?>:
                </td>
                <td class="responsiveInfoTable">
                    <?php echo $file->getForumLinkCode(); ?>
                </td>
            </tr>
        </tbody>
    </table>
    <h2><?php echo t("share", "share"); ?></h2>
    <table class="table table-bordered table-striped">
        <tbody>
            <tr>
                <td class="share-file-table-header">
                    <?php echo UCWords(t('share_file', 'share file')); ?>:
                </td>
                <td class="responsiveInfoTable">
                    <!-- AddThis Button BEGIN -->
                    <div class="addthis_toolbox addthis_default_style addthis_32x32_style">
                        <a class="addthis_button_preferred_1" addthis:url="<?php echo validation::safeOutputToScreen($file->getFullShortUrl()); ?>" addthis:title="<?php echo validation::safeOutputToScreen($file->originalFilename); ?>"></a>
                        <a class="addthis_button_preferred_2" addthis:url="<?php echo validation::safeOutputToScreen($file->getFullShortUrl()); ?>" addthis:title="<?php echo validation::safeOutputToScreen($file->originalFilename); ?>"></a>
                        <a class="addthis_button_preferred_3" addthis:url="<?php echo validation::safeOutputToScreen($file->getFullShortUrl()); ?>" addthis:title="<?php echo validation::safeOutputToScreen($file->originalFilename); ?>"></a>
                        <a class="addthis_button_preferred_4" addthis:url="<?php echo validation::safeOutputToScreen($file->getFullShortUrl()); ?>" addthis:title="<?php echo validation::safeOutputToScreen($file->originalFilename); ?>"></a>
                        <a class="addthis_button_preferred_5" addthis:url="<?php echo validation::safeOutputToScreen($file->getFullShortUrl()); ?>" addthis:title="<?php echo validation::safeOutputToScreen($file->originalFilename); ?>"></a>
                        <a class="addthis_button_preferred_6" addthis:url="<?php echo validation::safeOutputToScreen($file->getFullShortUrl()); ?>" addthis:title="<?php echo validation::safeOutputToScreen($file->originalFilename); ?>"></a>
                        <a class="addthis_button_preferred_7" addthis:url="<?php echo validation::safeOutputToScreen($file->getFullShortUrl()); ?>" addthis:title="<?php echo validation::safeOutputToScreen($file->originalFilename); ?>"></a>
                        <a class="addthis_button_preferred_8" addthis:url="<?php echo validation::safeOutputToScreen($file->getFullShortUrl()); ?>" addthis:title="<?php echo validation::safeOutputToScreen($file->originalFilename); ?>"></a>
                        <a class="addthis_button_preferred_9" addthis:url="<?php echo validation::safeOutputToScreen($file->getFullShortUrl()); ?>" addthis:title="<?php echo validation::safeOutputToScreen($file->originalFilename); ?>"></a>
                        <a class="addthis_button_compact" addthis:url="<?php echo validation::safeOutputToScreen($file->getFullShortUrl()); ?>" addthis:title="<?php echo validation::safeOutputToScreen($file->originalFilename); ?>"></a>
                        <a class="addthis_counter addthis_bubble_style" addthis:url="<?php echo validation::safeOutputToScreen($file->getFullShortUrl()); ?>" addthis:title="<?php echo validation::safeOutputToScreen($file->originalFilename); ?>"></a>
                    </div>
                    <script type="text/javascript" src="<?php echo _CONFIG_SITE_PROTOCOL; ?>://s7.addthis.com/js/250/addthis_widget.js#pubid=xa-4f10918d56581527"></script>
                    <!-- AddThis Button END -->
                </td>
            </tr>
        </tbody>
    </table>
    <?php endif; ?>
    <?php
    $canViewStats = $file->canViewStats();
    if (($canViewStats) || ($showAll == true))
    {
        ?>
        <h2><?php echo t("other_options", "other options"); ?></h2>
        <table class="table table-bordered table-striped">
            <tbody>
                <?php if ($canViewStats): ?>
                    <tr>
                        <td class="share-file-table-header">
                            <?php echo UCWords(t('statistics', 'statistics')); ?>:
                        </td>
                        <td class="responsiveInfoTable">
                            <a href="<?php echo $file->getStatisticsUrl(); ?>" target="_blank"><?php echo $file->getStatisticsUrl(); ?></a>
                        </td>
                    </tr>
                <?php endif; ?>
                <?php if ($showAll == true): ?>
                    <tr>
                        <td>
                            <?php echo UCWords(t('delete_file', 'delete file')); ?>:
                        </td>
                        <td class="responsiveInfoTable">
                            <a href="<?php echo $file->getDeleteUrl(); ?>" target="_blank"><?php echo $file->getDeleteUrl(); ?></a>
                        </td>
                    </tr>
                <?php endif ?>
            </tbody>
        </table>
    </div>
    <?php
}
?>
<?php
// include footer
require_once(SITE_TEMPLATES_PATH . '/partial/_footer.inc.php');
?>
