index.php 5.13 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
24
function loadDefaultSections($page)
{
    $page->titlegraphic = 'UNL Search';
Michael Fairchild's avatar
Michael Fairchild committed
25
26
27
28
    $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 = '';
Michael Fairchild's avatar
Michael Fairchild committed
29
30
31

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

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

Tyler R Lemburg's avatar
Tyler R Lemburg committed
36
$localScriptUrl = './js/search.min.js?v=4.1.20';
37
38
$pageTemplate = 'Fixed';

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

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

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

51
    $page->doctitle = '<title>Search | University of Nebraska&ndash;Lincoln</title>';
52
    $page->head = '<link rel="home" href="./" />';
53
    $page->pagetitle = '';
Michael Fairchild's avatar
Michael Fairchild committed
54
    $page->breadcrumbs = renderTemplate('templates/breadcrumbs.tpl.php');
55
}
56

Michael Fairchild's avatar
Michael Fairchild committed
57
$localResults = '';
58
59
$context = '';

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

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

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

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

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

107
108
    if (isset($_GET['cx'])) {
        // Use their custom search engine instead of the linked one.
109
        $search_engine_id = $_GET['cx'];
110
    } else {
111
112
        // send the site value as the context to search on
        $context = $_GET['u'];
113
    }
114

Michael Fairchild's avatar
Michael Fairchild committed
115
    $localResults = renderTemplate('templates/google-results.tpl.php', array(
116
117
118
119
        'title' => $page->titlegraphic,
        'id' => 'local_results',
    ));
} elseif (!$isEmbed) {
120
    // Default search for no referring site.
121
    loadDefaultSections($page);
122
}
123

Michael Fairchild's avatar
Michael Fairchild committed
124
$maincontent = '';
125
if (!$isEmbed) {
Michael Fairchild's avatar
Michael Fairchild committed
126
    $maincontent .= renderTemplate('templates/search-form.tpl.php', array('local_results' => $localResults));
127
128
}

Michael Fairchild's avatar
Michael Fairchild committed
129
$maincontent .= renderTemplate('templates/search-results.tpl.php', array(
130
    'isEmbed' => $isEmbed,
Michael Fairchild's avatar
Michael Fairchild committed
131
    'local_results' => $localResults
132
133
));

134
135
136
137
138
$initialQuery = json_encode(isset($_GET['q']) ? $_GET['q'] : '');
$context = json_encode($context);

$maincontent .= renderTemplate('templates/end-scripts.tpl.php', array(
    'localScriptUrl' => $localScriptUrl,
139
    'googleLoaderUrl' => 'https://cse.google.com/cse.js?cx=' . $search_engine_id,
140
141
142
143
    'initialQuery' => $initialQuery,
    'localContext' => $context,
));

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

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

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

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