index.php 5.34 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
$localScriptUrl = './js/search.min.js?v=4.1';
32
33
$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
    $page->doctitle = '<title>Search | University of Nebraska&ndash;Lincoln</title>';
47
    $page->head = '<link rel="home" href="./" />';
48
    $page->pagetitle = '';
Michael Fairchild's avatar
Michael Fairchild committed
49
    $page->breadcrumbs = renderTemplate('templates/breadcrumbs.tpl.php');
50
}
51

Michael Fairchild's avatar
Michael Fairchild committed
52
$localResults = '';
53
54
$context = '';

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

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

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

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

94
95
                    $page->{$region} = $purifier->purify($scannedContent);
                }
96
97
98
            }
        } else {
            loadDefaultSections($page);
99
        }
100
    }
101

102
103
    if (isset($_GET['cx'])) {
        // Use their custom search engine instead of the linked one.
104
        $context = $_GET['cx'];
105
106
    } else {
        // Auto-build a custom search engine
107
        $context = array('crefUrl' => UNL_Search::getLinkedCSEUrl($_GET['u']));
108
    }
109

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

119

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

Michael Fairchild's avatar
Michael Fairchild committed
125
$maincontent .= renderTemplate('templates/search-results.tpl.php', array(
126
    'isEmbed' => $isEmbed,
Michael Fairchild's avatar
Michael Fairchild committed
127
    'local_results' => $localResults
128
129
));

130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
$initialQuery = json_encode(isset($_GET['q']) ? $_GET['q'] : '');
$context = json_encode($context);

$apiKey = UNL_Search::getJSAPIKey();
$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;
}

$maincontent .= renderTemplate('templates/end-scripts.tpl.php', array(
    'localScriptUrl' => $localScriptUrl,
    'googleLoaderUrl' => 'https://www.google.com/jsapi?' . http_build_query($params),
    'initialQuery' => $initialQuery,
    'localContext' => $context,
));

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

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

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

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