index.php 5.46 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

Michael Fairchild's avatar
Michael Fairchild committed
66
$localResults = '';
67
68
$context = '';

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

71
//u is referring site
72
if (isset($_GET['u']) && $scanned = UNL_Search::getScannedPage($_GET['u'])) {
73
    // Add local site search results
74
    // we're scrapping the navigation and other content from the originatting site.
75
    if (!$isEmbed) {
76
        if (isset($scanned->titlegraphic)) {
77
            //require_once 'HTMLPurifier.auto.php';
78
79
            $config = HTMLPurifier_Config::createDefault();
            $config->set('Cache.SerializerPath', '/tmp');
Michael Fairchild's avatar
Michael Fairchild committed
80
81
82
83
84
85
86
87
88
            
            //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');
            }
            
89
            $purifier = new HTMLPurifier($config);
90

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

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

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

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

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

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

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

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

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

153
154
155
if (!$isEmbed) {
    $page->maincontentarea = $maincontent;
    echo $page;
156
157
158
    exit;
}

159
$template = $templatePath . 'embed.tpl.php';
160
161

if (UNL_Search::$mode === 'debug') {
162
    $template = $templatePath . 'embed-debug.tpl.php';
163
}
164
165
166
167
168

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