<?php

// Variable intake and sanitization
$incomingurl = $_POST['url'];
$url = filter_var($incomingurl, FILTER_SANITIZE_URL);
$incomingctx = $_POST['ctx'];
$ctx = filter_var($incomingctx, FILTER_SANITIZE_STRING);
$incomingkeywords = $_POST['keywords'];
$keywords = filter_var($incomingkeywords, FILTER_SANITIZE_STRING);
$language = filter_input(INPUT_POST, 'language', FILTER_SANITIZE_STRING);
$incomingtitle = $_POST['title'];
$title = filter_var($incomingtitle, FILTER_SANITIZE_STRING);
$incomingsubject= $_POST['subject'];
$subject = filter_var($incomingsubject, FILTER_SANITIZE_STRING);
$incomingauthor = $_POST['author'];
$author = filter_var($incomingauthor, FILTER_SANITIZE_STRING);

// Global variables used for URL construction
$searchprefix = "/search/searchresults.aspx?ctx=";
$searchmiddle = "&type=Boolean&term=";
$searchsuffix = "&sort=RELEVANCE&limit=&query=&page=0";
$searchjoiner = "%20and%20";

// Display posted variables
echo '<strong>URL:</strong> '.$url.'<br />';
echo '<strong>CTX:</strong> '.$ctx.'<br />';
echo '<strong>Keyword(s):</strong> '.$keywords.'<br />';
echo '<strong>Title:</strong> '.$title.'<br />';
echo '<strong>Author:</strong> '.$author.'<br />';
echo '<strong>Subjects:</strong> '.$subject.'<br />';
echo '<strong>Langauge:</strong> '.$language.'<br />';

// Begin building the URL

// Langauge selection starts the URL
$la = 'la='.$language;

if (!empty($keywords)) {
    $kw = $searchjoiner.'kw='.$keywords;
}

if (!empty($subject)) {
    $su = $searchjoiner.'su='.$subject;
}

if (!empty($author)) {
    $au = $searchjoiner.'au='.$author;
}

// Title status determines end of URL
if (!empty($title)) {
    $urlend = $searchjoiner.'ti='.$title.$searchsuffix; 
} else {
    $urlend = $searchsuffix;
}

// Display the Search link
echo '<h3>Your Deep Link is:</h3>';
//echo $url.'/'.$ctx.'/';

$SearchURL = implode("", array_filter(array($url, $searchprefix, $ctx, $searchmiddle, $la, $au, $kw, $su, $urlend)));

echo '<a href="'.$SearchURL.'">'.$SearchURL.'</a><br /><br />';

?>