<?php
// setup page
define("PAGE_NAME", t("link_checker_page_name", "Link Checker"));
define("PAGE_DESCRIPTION", t("link_checker_meta_description", "Check multiple download links at the same time."));
define("PAGE_KEYWORDS", t("link_checker_meta_keywords", "link, checker, copyright, infringement, file, hosting"));
define("TITLE_DESCRIPTION_LEFT", t("link_checker_title_page_description_left", ""));
define("TITLE_DESCRIPTION_RIGHT", t("link_checker_title_page_description_right", ""));

// send report if submitted
$rs = array();
$totalFound = 0;
$totalActive = 0;
$totalDisabled = 0;
if ((int) $_REQUEST['submitme'])
{
    $file_urls = trim($_REQUEST['file_urls']);
    if (strlen($file_urls) == 0)
    {
        notification::setError(t("link_checker_error_please_enter_1_or_more_urls", "Please enter 1 or more file urls."));
    }
	else
	{
		// standardise
		$file_urls = str_replace(array("\n\r", "\r\n", "\r", "\n\n"), "\n", $file_urls);
		$file_urls = str_replace(array("\n\r", "\r\n", "\r", "\n\n"), "\n", $file_urls);
        $file_urls_arr = explode("\n", $file_urls);
	}

	// make sure we do no more than 200
	if (notification::isErrors() == false)
    {
		if(COUNT($file_urls_arr) > 200)
		{
			notification::setError(t("link_checker_error_only_200_allowed", "Please enter less than 200 urls to check at once."));
		}
	}
	
    // check the urls
    if (notification::isErrors() == false)
    {
		// standardise
		foreach($file_urls_arr AS $file_url)
		{
			// get short url
			$rs[$file_url] = 'not found';
			$file_url_parts = parse_url($file_url);
			if(isset($file_url_parts['path']))
			{
				$path = $file_url_parts['path'];
				$pathParts = explode("/", $path);
				$shortUrl = trim(end($pathParts));
				
				// if this looks like a filename, try one back
				if(strpos($shortUrl, '.') !== false)
				{
					array_pop($pathParts);
					$shortUrl = trim(end($pathParts));
				}
				
				if(strlen($shortUrl))
				{
					$file = file::loadByShortUrl($shortUrl);
					if($file)
					{
						// active
						if($file->statusId == 1)
						{
							$rs[$file_url] = 'active';
							$totalActive++;
						}
						else
						{
							$rs[$file_url] = 'disabled';
							$totalDisabled++;
						}
						$totalFound++;
					}
				}
			}
		}
    }
}

// include header
require_once(SITE_TEMPLATES_PATH . '/partial/_header.inc.php');
?>
<div class="contentPageWrapper">

	<?php if(COUNT($rs) > 0): ?>
	<div class="pageSectionMain ui-corner-all">
        <div class="pageSectionMainInternal">
            <div id="pageHeader">
                <h2><?php echo t("link_checker_page_name", "Link Checker"); ?></h2>
            </div>
			<div class="col-md-12 animated" data-animation="fadeInUp" data-animation-delay="900">
				<div class="col-md-12 form-description">
					<p><?php echo t("link_checker_result", "Found [[[TOTAL_FOUND]]] link(s), [[[TOTAL_ACTIVE]]] active and [[[TOTAL_DISABLED]]] disabled.", array('TOTAL_FOUND'=>$totalFound, 'TOTAL_ACTIVE'=>$totalActive, 'TOTAL_DISABLED'=>$totalDisabled)); ?></p>
				</div>
			</div>
			<div class="col-md-12 animated" data-animation="fadeInUp" data-animation-delay="900">
				<table>
					<?php foreach($rs AS $url=>$status): ?>
					<tr>
						<td><?php echo validation::safeOutputToScreen($url); ?></td>
						<td style="text-align: center; width: 120px;">
						<?php
							switch($status)
							{
								case 'active':
									echo '<span style="color: green;">';
									echo validation::safeOutputToScreen(UCWords($status));
									echo '</span>';
									break;
								case 'disabled':
									echo '<span style="color: red;">';
									echo validation::safeOutputToScreen(UCWords($status));
									echo '</span>';
									break;
								default:
									echo '<span style="color: gray;">';
									echo validation::safeOutputToScreen(UCWords($status));
									echo '</span>';
									break;
							}
						?>
						</td>
					</tr>
					<?php endforeach; ?>
				</table>
				
				<form id="form-join" class="international" method="get" action="<?php echo WEB_ROOT; ?>/link_checker.<?php echo SITE_CONFIG_PAGE_EXTENSION; ?>" id="form-join">
					<ul>
					<li class="field-container">
						<input tabindex="99" type="submit" name="submit" value="<?php echo t("check_more", "check more"); ?>" class="submitInput" />
					</li>
					</ul>
				</form>
			</div>
		</div>
    </div>
	<?php include_once(SITE_TEMPLATES_PATH . '/partial/_banner_right_content.inc.php'); ?>
    <div class="clear"><!-- --></div>
	<?php else: ?>

	<?php
	if (notification::isErrors())
	{
		echo notification::outputErrors();
	}
	elseif (notification::isSuccess())
	{
		echo notification::outputSuccess();
	}
	?>
    <!-- form -->
    <div class="pageSectionMain ui-corner-all">
        <div class="pageSectionMainInternal">
            <div id="pageHeader">
                <h2><?php echo t("link_checker_page_name", "Link Checker"); ?></h2>
            </div>
            <div class="introText"><?php echo t("link_checker_intro", "Use this page to check whether multiple download links are still active. Enter up to 200 urls below, a new one on each line and click 'Check Files'"); ?><br/><br/>
                <form id="form-join" class="international" method="post" action="<?php echo WEB_ROOT; ?>/link_checker.<?php echo SITE_CONFIG_PAGE_EXTENSION; ?>" id="form-join">
                    <ul class="reportFileRow">
                        <li class="field-container">
                            <label for="file_urls">
                                <span class="field-name"><?php echo t('link_checker_list_of_file_download_urls', 'List of file download urls, 1 on each line.'); ?>:
                                </span>
                                <textarea rows="8" id="file_urls" name="file_urls" style="width: 100%;"></textarea>
                            </label>
                        </li>
                       
                        <li class="field-container">
                            <span class="field-name"></span>
                            <input name="submitme" type="hidden" value="1"/>
                            <input tabindex="99" type="submit" name="submit" value="<?php echo t("check_files", "check files"); ?>" class="submitInput" />
                        </li>
                    </ul>
                </form>
                <div class="clear"></div>
            </div>
        </div>
    </div>
	<?php include_once(SITE_TEMPLATES_PATH . '/partial/_banner_right_content.inc.php'); ?>
    <div class="clear"><!-- --></div>

	<?php endif; ?>
</div>
<?php
// include footer
require_once(SITE_TEMPLATES_PATH . '/partial/_footer.inc.php');
?>
