index.php 4.92 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
32
33
$localScriptUrl = './js/search.min.js';
$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');
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
66
            $config = HTMLPurifier_Config::createDefault();
            $config->set('Cache.SerializerPath', '/tmp');
            $purifier = new HTMLPurifier($config);
67

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

72
            foreach (array('breadcrumbs', 'navlinks', 'leftcollinks', 'contactinfo', 'affiliation') as $region) {
73
74
75
76
77
78
79
80
81
82
83
                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;
                    }
84

85
86
                    $page->{$region} = $purifier->purify($scannedContent);
                }
87
88
89
            }
        } else {
            loadDefaultSections($page);
90
        }
91
    }
92

93
94
    if (isset($_GET['cx'])) {
        // Use their custom search engine instead of the linked one.
95
        $context = $_GET['cx'];
96
97
    } else {
        // Auto-build a custom search engine
98
        $context = array('crefUrl' => UNL_Search::getLinkedCSEUrl($_GET['u']));
99
    }
100

Michael Fairchild's avatar
Michael Fairchild committed
101
    $localResults = renderTemplate('templates/google-results.tpl.php', array(
102
103
104
105
        'title' => $page->titlegraphic,
        'id' => 'local_results',
    ));
} elseif (!$isEmbed) {
106
    // Default search for no referring site.
107
    loadDefaultSections($page);
108
}
109

110

Michael Fairchild's avatar
Michael Fairchild committed
111
$maincontent = '';
112
if (!$isEmbed) {
Michael Fairchild's avatar
Michael Fairchild committed
113
    $maincontent .= renderTemplate('templates/search-form.tpl.php', array('local_results' => $localResults));
114
115
}

Michael Fairchild's avatar
Michael Fairchild committed
116
$maincontent .= renderTemplate('templates/search-results.tpl.php', array(
117
    'isEmbed' => $isEmbed,
Michael Fairchild's avatar
Michael Fairchild committed
118
    'local_results' => $localResults
119
120
));

121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
$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,
));

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

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

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

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