index.php 4.84 KB
Newer Older
1
2
<?php

Brett Bieber's avatar
Brett Bieber committed
3
4
5
6
$config_file = __DIR__ . '/../config.sample.php';
if (file_exists(__DIR__ . '/../config.inc.php')) {
    $config_file = __DIR__ . '/../config.inc.php';
}
7

Brett Bieber's avatar
Brett Bieber committed
8
require_once $config_file;
9

Michael Fairchild's avatar
Michael Fairchild committed
10
11
use UNL\Templates\Templates;

12
13
function renderTemplate($file, $params = array())
{
Michael Fairchild's avatar
Michael Fairchild committed
14
    ob_start();
15
16
    extract($params);
    include $file;
Michael Fairchild's avatar
Michael Fairchild committed
17
    return ob_get_clean();
18
19
}

20
21
22
function loadDefaultSections($page)
{
    $page->titlegraphic = 'UNL Search';
Michael Fairchild's avatar
Michael Fairchild committed
23
24
25
26
    $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 = '';
27
28
}

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

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

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

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

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

46
47
    $page->doctitle = '<title>Search | University of Nebraska&ndash;Lincoln</title>';
    $page->pagetitle = '';
Michael Fairchild's avatar
Michael Fairchild committed
48
    $page->breadcrumbs = renderTemplate('templates/breadcrumbs.tpl.php');
49
}
50

Michael Fairchild's avatar
Michael Fairchild committed
51
$localResults = '';
52
$inlineJS = '';
Brett Bieber's avatar
Brett Bieber committed
53
$apiKey = UNL_Search::getJSAPIKey();
54
55
56
57
58
59
60
61
62
63
64
65
$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;
66
}
67
$page->addScript(htmlspecialchars('//www.google.com/jsapi?' . http_build_query($params)));
68
$page->addStyleSheet('css/search.css');
69

70
//u is referring site
71
if (isset($_GET['u']) && $scanned = UNL_Search::getScannedPage($_GET['u'])) {
72
    // Add local site search results
73
    // we're scrapping the navigation and other content from the originatting site.
74
    if (!$isEmbed) {
75
        if (isset($scanned->titlegraphic)) {
76
            //require_once 'HTMLPurifier.auto.php';
77
78
79
            $config = HTMLPurifier_Config::createDefault();
            $config->set('Cache.SerializerPath', '/tmp');
            $purifier = new HTMLPurifier($config);
80

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

85
            foreach (array('breadcrumbs', 'navlinks', 'leftcollinks', 'contactinfo', 'affiliation') as $region) {
86
87
88
89
90
91
92
93
94
95
96
                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;
                    }
97

98
99
                    $page->{$region} = $purifier->purify($scannedContent);
                }
100
101
102
            }
        } else {
            loadDefaultSections($page);
103
        }
104
    }
105

106
107
    if (isset($_GET['cx'])) {
        // Use their custom search engine instead of the linked one.
108
        $context = $_GET['cx'];
109
110
    } else {
        // Auto-build a custom search engine
111
        $context = array('crefUrl' => UNL_Search::getLinkedCSEUrl($_GET['u']));
112
    }
113
114
115
    $context = json_encode($context);
    $inlineJS .= "var LOCAL_SEARCH_CONTEXT = {$context};\n";
    
Michael Fairchild's avatar
Michael Fairchild committed
116
    $localResults = renderTemplate('templates/google-results.tpl.php', array(
117
118
119
120
        'title' => $page->titlegraphic,
        'id' => 'local_results',
    ));
} elseif (!$isEmbed) {
121
    // Default search for no referring site.
122
    loadDefaultSections($page);
123
}
124

125
126
127
if (isset($_GET['q'])) {
    $q = json_encode($_GET['q']);
    $inlineJS .= "var INITIAL_QUERY = {$q};\n";
128
129
}

130
$page->addScriptDeclaration($inlineJS);
131

Michael Fairchild's avatar
Michael Fairchild committed
132
$maincontent = '';
133
if (!$isEmbed) {
Michael Fairchild's avatar
Michael Fairchild committed
134
    $maincontent .= renderTemplate('templates/search-form.tpl.php', array('local_results' => $localResults));
135
136
}

Michael Fairchild's avatar
Michael Fairchild committed
137
$maincontent .= renderTemplate('templates/search-results.tpl.php', array(
138
    'isEmbed' => $isEmbed,
Michael Fairchild's avatar
Michael Fairchild committed
139
    'local_results' => $localResults
140
141
142
143
144
));

if (!$isEmbed) {
    $page->maincontentarea = $maincontent;
    echo $page;
145
146
147
148
149
150
151
    exit;
}

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

if (UNL_Search::$mode === 'debug') {
    $template = 'templates/embed-debug.tpl.php';
152
}
153
154
155
156
157

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