Skip to content
Snippets Groups Projects
Forked from Digital Experience Group / UNL_Search
115 commits behind the upstream repository.
index.php 4.92 KiB
<?php

$config_file = __DIR__ . '/../config.sample.php';
if (file_exists(__DIR__ . '/../config.inc.php')) {
    $config_file = __DIR__ . '/../config.inc.php';
}

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)
{
    $page->titlegraphic = 'UNL Search';
    $page->breadcrumbs = str_replace('Department', 'Search', $page->breadcrumbs);
    $page->navlinks = file_get_contents('http://www.unl.edu/ucomm/sharedcode/navigation.html');
    $page->contactinfo = renderTemplate('templates/local-footer.tpl.php');
    $page->affiliation = '';
}

$isEmbed = isset($_GET['embed']) && $_GET['embed'];

$localScriptUrl = './js/search.min.js';
$pageTemplate = 'Fixed';

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

$page = Templates::factory($pageTemplate, Templates::VERSION_4_1);

if (!$isEmbed) {
    if (file_exists(__DIR__ . '/wdn/templates_4.1')) {
        $page->setLocalIncludePath(__DIR__);
    }

    $page->doctitle = '<title>Search | University of Nebraska&ndash;Lincoln</title>';
    $page->head = '<link rel="home" href="./" />';
    $page->pagetitle = '';
    $page->breadcrumbs = renderTemplate('templates/breadcrumbs.tpl.php');
}

$localResults = '';
$context = '';

$page->addStyleSheet('css/search.css');

//u is referring site
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 (!$isEmbed) {
        if (isset($scanned->titlegraphic)) {
            //require_once 'HTMLPurifier.auto.php';
            $config = HTMLPurifier_Config::createDefault();
            $config->set('Cache.SerializerPath', '/tmp');
            $purifier = new HTMLPurifier($config);

            $page->head        .= '<link rel="home" href="'.htmlentities($_GET['u'], ENT_QUOTES).'" />';
            $page->titlegraphic = $purifier->purify(str_replace(array('<h1>', '</h1>'), '', $scanned->titlegraphic));
            $page->affiliation = '';

            foreach (array('breadcrumbs', 'navlinks', 'leftcollinks', 'contactinfo', 'affiliation') as $region) {
                if (isset($scanned->{$region}) && !empty($scanned->{$region})) {
                    $scannedContent = $scanned->{$region};
                    switch ($region) {
                        case 'breadcrumbs':
                        case 'navlinks':
                            $scannedContent = UNL_Search::removeRelativePaths($scannedContent, $_GET['u']);
                            break;
                        case 'contactinfo':
                            $scannedContent = preg_replace('#<h3>.*</h3>#', '', $scannedContent);
                            break;
                    }

                    $page->{$region} = $purifier->purify($scannedContent);
                }
            }
        } else {
            loadDefaultSections($page);
        }
    }

    if (isset($_GET['cx'])) {
        // Use their custom search engine instead of the linked one.
        $context = $_GET['cx'];
    } else {
        // Auto-build a custom search engine
        $context = array('crefUrl' => UNL_Search::getLinkedCSEUrl($_GET['u']));
    }

    $localResults = renderTemplate('templates/google-results.tpl.php', array(
        'title' => $page->titlegraphic,
        'id' => 'local_results',
    ));
} elseif (!$isEmbed) {
    // Default search for no referring site.
    loadDefaultSections($page);
}


$maincontent = '';
if (!$isEmbed) {
    $maincontent .= renderTemplate('templates/search-form.tpl.php', array('local_results' => $localResults));
}

$maincontent .= renderTemplate('templates/search-results.tpl.php', array(
    'isEmbed' => $isEmbed,
    'local_results' => $localResults
));

$initialQuery = json_encode(isset($_GET['q']) ? $_GET['q'] : '');
$context = json_encode($context);

$apiKey = UNL_Search::getJSAPIKey();
$params = array(
    'autoload' => json_encode(array('modules' => array(
        array(
            'name' => 'search',
            'version' => '1.0',
            'callback' => 'searchInit',
            'style' => '//www.google.com/cse/style/look/v2/default.css'
        ),
    ))),
);

if (!empty($apiKey)) {
    $params['key'] = $apiKey;
}

$maincontent .= renderTemplate('templates/end-scripts.tpl.php', array(
    'localScriptUrl' => $localScriptUrl,
    'googleLoaderUrl' => 'https://www.google.com/jsapi?' . http_build_query($params),
    'initialQuery' => $initialQuery,
    'localContext' => $context,
));

if (!$isEmbed) {
    $page->maincontentarea = $maincontent;
    echo $page;
    exit;
}

$template = 'templates/embed.tpl.php';

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

echo renderTemplate($template, array(
    'head' => $page->head,
    'maincontent' => $maincontent
));