Skip to content
Snippets Groups Projects
index.php 6.12 KiB
Newer Older
$cacheVersion = date('Ymd') . 'v';
$search_engine_id = '015236299699564929946:nk1siew10ie';

Brett Bieber's avatar
Brett Bieber committed
$config_file = __DIR__ . '/../config.sample.php';
if (file_exists(__DIR__ . '/../config.inc.php')) {
    $config_file = __DIR__ . '/../config.inc.php';
}
Brett Bieber's avatar
Brett Bieber committed
require_once $config_file;
use UNL\Templates\Templates;

function renderTemplate($file, $params = array())
{
    ob_start();
    extract($params);
    include $file;
    return ob_get_clean();
function loadDefaultSections($page)
{
Jeff Sturek's avatar
Jeff Sturek committed
    $page->titlegraphic = '<a class="dcf-txt-h5" href="/">Search</a>';
    $page->contactinfo = renderTemplate('templates/local-footer.tpl.php');
    $page->affiliation = '';
Jeff Sturek's avatar
Jeff Sturek committed
$isEmbed = isset($_GET['embed']) && $_GET['embed'];
Jeff Sturek's avatar
Jeff Sturek committed
// While the site proper is skinned with a specific version, the templates used
// used in embed search need to be supported in legacy sites.
// ?embed=1 : 4.1
Jeff Sturek's avatar
Jeff Sturek committed
// ?embed=(not 1) : 5.3
Jeff Sturek's avatar
Jeff Sturek committed
if ($isEmbed && $_GET['embed'] === '1') {
    $pageTemplate = 'Fixed';
    $page = Templates::factory($pageTemplate, Templates::VERSION_4_1);
    $templatePath = 'templates/4.1/';
Jeff Sturek's avatar
Jeff Sturek committed
    $page->addStyleSheet('css/search.css?v=' . $cacheVersion);

    $localScriptUrl = './js/search.min.js?v=' . $cacheVersion;

    if (UNL_Search::$mode === 'debug') {
        $pageTemplate = 'Local';
        $localScriptUrl = './js/search.js';
    }
Jeff Sturek's avatar
Jeff Sturek committed
    $pageTemplate = 'AppLocal';
Jeff Sturek's avatar
Jeff Sturek committed
    $page = Templates::factory($pageTemplate, Templates::VERSION_5_3);
Jeff Sturek's avatar
Jeff Sturek committed
    $templatePath = 'templates/5.0/';

Jeff Sturek's avatar
Jeff Sturek committed
    $localScriptUrl = './js/search-tabs.min.js?v=' . $cacheVersion;

    if (UNL_Search::$mode === 'debug') {
        $pageTemplate = 'Local';
        $localScriptUrl = './js/search-tabs.js';
    }

Jeff Sturek's avatar
Jeff Sturek committed
    if (!$isEmbed) {
Jeff Sturek's avatar
Jeff Sturek committed
        if (file_exists(__DIR__ . '/wdn/templates_5.3')) {
Jeff Sturek's avatar
Jeff Sturek committed
            $page->setLocalIncludePath(__DIR__);
        }

        $page->doctitle = '<title>Search | University of Nebraska&ndash;Lincoln</title>';
        $page->head .= '<link rel="home" href="./" />';

Jeff Sturek's avatar
Jeff Sturek committed
        // Add WDN Deprecated Styles
Jeff Sturek's avatar
Jeff Sturek committed
        $page->head .= '<link rel="preload" href="/wdn/templates_5.3/css/deprecated.css" as="style" onload="this.onload=null;this.rel=\'stylesheet\'"> <noscript><link rel="stylesheet" href="/wdn/templates_5.3/css/deprecated.css"></noscript>';
Jeff Sturek's avatar
Jeff Sturek committed

Jeff Sturek's avatar
Jeff Sturek committed
        // no menu items, so hide mobile menu
        $page->addStyleDeclaration("#dcf-mobile-toggle-menu {display: none!important}");

        // in search, so hide search
        $page->addStyleDeclaration("#dcf-mobile-toggle-search, #dcf-search {display: none!important}");
    }

Jeff Sturek's avatar
Jeff Sturek committed
    $page->addStyleSheet('css/search-5.0.css?v=' . $cacheVersion);
Michael Fairchild's avatar
Michael Fairchild committed
$localResults = '';
if (isset($_GET['u']) && $scanned = UNL_Search::getScannedPage($_GET['u'])) {
    // Add local site search results
    // we're scrapping the navigation and other content from the originatting site.
        if (isset($scanned->titlegraphic)) {
            //require_once 'HTMLPurifier.auto.php';
            $config = HTMLPurifier_Config::createDefault();
            $config->set('Cache.SerializerPath', '/tmp');
Michael Fairchild's avatar
Michael Fairchild committed
            //Trick the purifier into accepting HTML5 elements/attributes
            $config->set('HTML.DefinitionID', 'html5-definitions'); // unqiue id
            $config->set('HTML.DefinitionRev', 1);
            if ($def = $config->maybeGetRawHTMLDefinition()) {
                //Allow everything to have a role
                $def->addAttribute('*', 'role', 'Text');
            }
            $purifier = new HTMLPurifier($config);
Jeff Sturek's avatar
Jeff Sturek committed
            $page->head .= '<link rel="home" href="'.htmlentities($_GET['u'], ENT_QUOTES).'" />';
            $page->titlegraphic = $purifier->purify(str_replace(array('<h1>', '</h1>'), '', $scanned->titlegraphic));
            $page->affiliation = '';
Jeff Sturek's avatar
Jeff Sturek committed
            foreach (array('contactinfo', 'affiliation') as $region) {
                if (isset($scanned->{$region}) && !empty($scanned->{$region})) {
                    $scannedContent = $scanned->{$region};
                    switch ($region) {
                        case 'contactinfo':
                            $scannedContent = preg_replace('#<h3>.*</h3>#', '', $scannedContent);
                            break;
                    }
                    $page->{$region} = $purifier->purify($scannedContent);
                }
    if (isset($_GET['cx'])) {
        // Use their custom search engine instead of the linked one.
        $search_engine_id = $_GET['cx'];
        // send the site value as the context to search on
        $context = $_GET['u'];
    $localResults = renderTemplate($templatePath . 'google-results.tpl.php', array(
Jeff Sturek's avatar
Jeff Sturek committed
        'title' => $page->titlegraphic, // leave for backwards compatibility for 4.1
        'id' => 'local_results',
    ));
} elseif (!$isEmbed) {
    // Default search for no referring site.
Jeff Sturek's avatar
Jeff Sturek committed
$initialQuery = json_encode(isset($_GET['q']) ? $_GET['q'] : '');
$context = json_encode($context);

$maincontent = '';
Michael Fairchild's avatar
Michael Fairchild committed
    $maincontent .= renderTemplate('templates/search-form.tpl.php', array('local_results' => $localResults));
Jeff Sturek's avatar
Jeff Sturek committed
if ($isEmbed || !empty($_GET['q'])) {
    $maincontent .= renderTemplate($templatePath . 'search-results.tpl.php', array(
        'isEmbed' => $isEmbed,
Jeff Sturek's avatar
Jeff Sturek committed
        'local_results' => $localResults,
        'localTitle' => $page->titlegraphic
Jeff Sturek's avatar
Jeff Sturek committed
    ));
}
Jeff Sturek's avatar
Jeff Sturek committed
$endScript = renderTemplate($templatePath . 'end-scripts.tpl.php', array(
    'localScriptUrl' => $localScriptUrl,
    'googleLoaderUrl' => 'https://cse.google.com/cse.js?cx=' . $search_engine_id,
    'initialQuery' => $initialQuery,
Jeff Sturek's avatar
Jeff Sturek committed
    'localContext' => $context,
    'cacheVersion' => $cacheVersion
Jeff Sturek's avatar
Jeff Sturek committed
if ($isEmbed) {
    $maincontent .= $endScript;
} else {
    $page->jsbody .= $endScript;
}

if (!$isEmbed) {
    $page->maincontentarea = $maincontent;
    echo $page;
$template = $templatePath . 'embed.tpl.php';

if (UNL_Search::$mode === 'debug') {
    $template = $templatePath . 'embed-debug.tpl.php';

echo renderTemplate($template, array(
    'head' => $page->head,
Jeff Sturek's avatar
Jeff Sturek committed
    'maincontent' => $maincontent,
    'cacheVersion' => $cacheVersion