<?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="row">

	<?php if(COUNT($rs) > 0): ?>
	<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 class="table table-striped">
			<?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>
		<div class="input-group form-buttons"> <span class="input-group-btn">
				<input name="submitme" type="hidden" value="1"/>
				<button class="btn btn-default" tabindex="99" type="submit" name="submit" onClick="window.location='<?php echo WEB_ROOT; ?>/link_checker.<?php echo SITE_CONFIG_PAGE_EXTENSION; ?>';"><?php echo t("check_more", "check more"); ?></button>
			</span> 
		</div>
	</div>
	<?php else: ?>
    <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_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'"); ?></p>
        </div>
	</div>
	<div class="col-md-12 animated" data-animation="fadeInUp" data-animation-delay="900">
        <div class="form">
            <form method="post" action="<?php echo WEB_ROOT; ?>/link_checker.<?php echo SITE_CONFIG_PAGE_EXTENSION; ?>">
                <?php
                if (notification::isErrors())
                {
                    echo notification::outputErrors();
                }
                elseif (notification::isSuccess())
                {
                    echo notification::outputSuccess();
                }
                ?>
                <div class="input-group"> <span class="input-group-addon"><i class="fa fa-chevron-right"></i></span>
                    <textarea rows="8" class="form-control" id="file_urls" placeholder="<?php echo t('link_checker_list_of_file_download_urls', 'List of file download urls, 1 on each line.'); ?>" name="file_urls"></textarea>
                </div>
                <div class="input-group form-buttons"> <span class="input-group-btn">
                        <input name="submitme" type="hidden" value="1"/>
                        <button class="btn btn-default" tabindex="99" type="submit" name="submit"><i class="fa fa-check"></i> <?php echo t("check_files", "check files"); ?></button>
                    </span> 
                </div>
            </form>
        </div>
    </div>
	<?php endif; ?>
</div>
<?php
// include footer
require_once(SITE_TEMPLATES_PATH . '/partial/_footer.inc.php');
?>
