index.php 5.4 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
function renderTemplate($file, $params = array())
{
Michael Fairchild's avatar
Michael Fairchild committed
16
    ob_start();
17
18
    extract($params);
    include $file;
Michael Fairchild's avatar
Michael Fairchild committed
19
    return ob_get_clean();
20
21
}

22
23
function loadDefaultSections($page)
{
Jeff Sturek's avatar
Jeff Sturek committed
24
    $page->titlegraphic = '<a class="dcf-txt-h5" href="/">Search</a>';
Michael Fairchild's avatar
Michael Fairchild committed
25
26
    $page->contactinfo = renderTemplate('templates/local-footer.tpl.php');
    $page->affiliation = '';
27
28
}

29
$localScriptUrl = './js/search.min.js?v=20190116';
30

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

Jeff Sturek's avatar
Jeff Sturek committed
36
$isEmbed = isset($_GET['embed']) && $_GET['embed'];
37

Jeff Sturek's avatar
Jeff Sturek committed
38
39
40
41
42
43
44
45
46
// 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
if ($isEmbed && $_GET['embed'] === '1') {
    $pageTemplate = 'Fixed';
    $page = Templates::factory($pageTemplate, Templates::VERSION_4_1);
    $templatePath = 'templates/4.1/';
    $page->addStyleSheet('css/search.css?v=20180901');
47

48
} else {
Jeff Sturek's avatar
Jeff Sturek committed
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
    $pageTemplate = 'App';
    $page = Templates::factory($pageTemplate, Templates::VERSION_5);
    $templatePath = 'templates/5.0/';

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

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

        // 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}");
    }

    $page->addStyleSheet('css/search-5.0.css?v=20180901');
69
}
70

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

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

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

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

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

Jeff Sturek's avatar
Jeff Sturek committed
98
            foreach (array('contactinfo', 'affiliation') as $region) {
99
100
101
102
103
104
105
                if (isset($scanned->{$region}) && !empty($scanned->{$region})) {
                    $scannedContent = $scanned->{$region};
                    switch ($region) {
                        case 'contactinfo':
                            $scannedContent = preg_replace('#<h3>.*</h3>#', '', $scannedContent);
                            break;
                    }
106

107
108
                    $page->{$region} = $purifier->purify($scannedContent);
                }
109
110
111
            }
        } else {
            loadDefaultSections($page);
112
        }
113
    }
114

115
116
    if (isset($_GET['cx'])) {
        // Use their custom search engine instead of the linked one.
117
        $search_engine_id = $_GET['cx'];
118
    } else {
119
120
        // send the site value as the context to search on
        $context = $_GET['u'];
121
    }
122

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

Jeff Sturek's avatar
Jeff Sturek committed
132
133
134
$initialQuery = json_encode(isset($_GET['q']) ? $_GET['q'] : '');
$context = json_encode($context);

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

Jeff Sturek's avatar
Jeff Sturek committed
140
141
142
143
144
145
if ($isEmbed || !empty($_GET['q'])) {
    $maincontent .= renderTemplate($templatePath . 'search-results.tpl.php', array(
        'isEmbed' => $isEmbed,
        'local_results' => $localResults
    ));
}
146

Jeff Sturek's avatar
Jeff Sturek committed
147
$endScript = renderTemplate($templatePath . 'end-scripts.tpl.php', array(
148
    'localScriptUrl' => $localScriptUrl,
149
    'googleLoaderUrl' => 'https://cse.google.com/cse.js?cx=' . $search_engine_id,
150
    'initialQuery' => $initialQuery,
Jeff Sturek's avatar
Jeff Sturek committed
151
    'localContext' => $context
152
153
));

Jeff Sturek's avatar
Jeff Sturek committed
154
155
156
157
158
159
if ($isEmbed) {
    $maincontent .= $endScript;
} else {
    $page->jsbody .= $endScript;
}

160
161
162
if (!$isEmbed) {
    $page->maincontentarea = $maincontent;
    echo $page;
163
164
165
    exit;
}

166
$template = $templatePath . 'embed.tpl.php';
167
168

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

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