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

3
4
$search_engine_id = '015236299699564929946:nk1siew10ie';

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

Brett Bieber's avatar
Brett Bieber committed
10
require_once $config_file;
11

Michael Fairchild's avatar
Michael Fairchild committed
12
13
use UNL\Templates\Templates;

14
15
16
17
18
19
20
21
22
23
24
$isEmbed = isset($_GET['embed']) && $_GET['embed'];

// 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
// ?embed=5.0 : 5.0
$templatePath = 'templates/4.1/';
if ($isEmbed && $_GET['embed'] === '5.0') {
  $templatePath = 'templates/5.0/';
}

25
26
function renderTemplate($file, $params = array())
{
Michael Fairchild's avatar
Michael Fairchild committed
27
    ob_start();
28
29
    extract($params);
    include $file;
Michael Fairchild's avatar
Michael Fairchild committed
30
    return ob_get_clean();
31
32
}

33
34
35
function loadDefaultSections($page)
{
    $page->titlegraphic = 'UNL Search';
Michael Fairchild's avatar
Michael Fairchild committed
36
    $page->breadcrumbs = str_replace('Department', 'Search', $page->breadcrumbs);
37
    $page->navlinks = file_get_contents('https://www.unl.edu/ucomm/sharedcode/navigation.html');
Michael Fairchild's avatar
Michael Fairchild committed
38
39
    $page->contactinfo = renderTemplate('templates/local-footer.tpl.php');
    $page->affiliation = '';
Michael Fairchild's avatar
Michael Fairchild committed
40
41
42

    //Convert relative paths to absolute
    $page->navlinks = UNL_Search::removeRelativePaths($page->navlinks, 'https://www.unl.edu/');
43
44
}

Tyler R Lemburg's avatar
Tyler R Lemburg committed
45
$localScriptUrl = './js/search.min.js?v=4.1.20';
46
47
$pageTemplate = 'Fixed';

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

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

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

60
    $page->doctitle = '<title>Search | University of Nebraska&ndash;Lincoln</title>';
61
    $page->head = '<link rel="home" href="./" />';
62
    $page->pagetitle = '';
Michael Fairchild's avatar
Michael Fairchild committed
63
    $page->breadcrumbs = renderTemplate('templates/breadcrumbs.tpl.php');
64
65
66
}

if ($isEmbed && $_GET['embed'] === '5.0') {
67
68
69
  $page->addStyleSheet('css/search-5.0.css?v=20180901');
} else {
  $page->addStyleSheet('css/search.css?v=20180901');
70
}
71

Michael Fairchild's avatar
Michael Fairchild committed
72
$localResults = '';
73
74
$context = '';

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

Michael Fairchild's avatar
Michael Fairchild committed
85
86
87
88
89
90
91
            //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');
            }
92

93
            $purifier = new HTMLPurifier($config);
94

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

99
            foreach (array('breadcrumbs', 'navlinks', 'leftcollinks', 'contactinfo', 'affiliation') as $region) {
100
101
102
103
104
105
106
107
108
109
110
                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;
                    }
111

112
113
                    $page->{$region} = $purifier->purify($scannedContent);
                }
114
115
116
            }
        } else {
            loadDefaultSections($page);
117
        }
118
    }
119

120
121
    if (isset($_GET['cx'])) {
        // Use their custom search engine instead of the linked one.
122
        $search_engine_id = $_GET['cx'];
123
    } else {
124
125
        // send the site value as the context to search on
        $context = $_GET['u'];
126
    }
127

128
    $localResults = renderTemplate($templatePath . 'google-results.tpl.php', array(
129
130
131
132
        'title' => $page->titlegraphic,
        'id' => 'local_results',
    ));
} elseif (!$isEmbed) {
133
    // Default search for no referring site.
134
    loadDefaultSections($page);
135
}
136

Michael Fairchild's avatar
Michael Fairchild committed
137
$maincontent = '';
138
if (!$isEmbed) {
Michael Fairchild's avatar
Michael Fairchild committed
139
    $maincontent .= renderTemplate('templates/search-form.tpl.php', array('local_results' => $localResults));
140
141
}

142
$maincontent .= renderTemplate($templatePath . 'search-results.tpl.php', array(
143
    'isEmbed' => $isEmbed,
Michael Fairchild's avatar
Michael Fairchild committed
144
    'local_results' => $localResults
145
146
));

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

150
$maincontent .= renderTemplate($templatePath . 'end-scripts.tpl.php', array(
151
    'localScriptUrl' => $localScriptUrl,
152
    'googleLoaderUrl' => 'https://cse.google.com/cse.js?cx=' . $search_engine_id,
153
154
155
156
    'initialQuery' => $initialQuery,
    'localContext' => $context,
));

157
158
159
if (!$isEmbed) {
    $page->maincontentarea = $maincontent;
    echo $page;
160
161
162
    exit;
}

163
$template = $templatePath . 'embed.tpl.php';
164
165

if (UNL_Search::$mode === 'debug') {
166
    $template = $templatePath . 'embed-debug.tpl.php';
167
}
168
169
170
171
172

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