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

10
11
12
13
14
15
function renderTemplate($file, $params = array())
{
    extract($params);
    include $file;
}

16
17
18
19
20
21
22
23
24
25
function loadDefaultSections($page)
{
    $page->titlegraphic = 'UNL Search';
    $page->breadcrumbs   = str_replace('Department', 'Search', $page->breadcrumbs);
    $page->navlinks      = file_get_contents('http://www.unl.edu/ucomm/sharedcode/navigation.html');
    $page->leftcollinks  = file_get_contents('http://www.unl.edu/ucomm/sharedcode/relatedLinks.html');
    $page->contactinfo   = preg_replace('#<h3>.*</h3>#', '', file_get_contents('http://www.unl.edu/ucomm/sharedcode/footerContactInfo.html'));
    $page->footercontent = file_get_contents('http://www.unl.edu/ucomm/sharedcode/footer.html');
}

26
27
$isEmbed = isset($_GET['embed']) && $_GET['embed'];

28
UNL_Templates::setCachingService(new UNL_Templates_CachingService_Null());
29
UNL_Templates::$options['version'] = 4.0;
30

31
32
33
34
35
36
37
38

if (UNL_Search::$mode === 'debug') {
    $page = UNL_Templates::factory('Local');
    $page->addScript('js/search.js');
} else {
    $page = UNL_Templates::factory('Fixed');
    $page->addScript('js/search.min.js');
}
39

40
41
42
43
44
45
46
if (!$isEmbed) {
    $page->doctitle = '<title>Search | University of Nebraska&ndash;Lincoln</title>';
    $page->pagetitle = '';
    ob_start();
    renderTemplate('templates/breadcrumbs.tpl.php');
    $page->breadcrumbs = ob_get_clean();
}
47

48
$local_results = '';
49
$inlineJS = '';
Brett Bieber's avatar
Brett Bieber committed
50
$apiKey = UNL_Search::getJSAPIKey();
51
52
53
54
55
56
57
58
59
60
61
62
$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;
63
}
64
$page->addScript(htmlspecialchars('//www.google.com/jsapi?' . http_build_query($params)));
65
$page->addStyleSheet('css/search.css');
66

67
68
//u is referring site
if (isset($_GET['u']) && $scanned = UNL_Search::getScannedPage($_GET['u'])) { 
69
    // Add local site search results
70
    // we're scrapping the navigation and other content from the originatting site.
71
72
73
74
75
76
77
78
    if (!$isEmbed) {
        if (isset($scanned->titlegraphic)) { 
            require_once 'HTMLPurifier.auto.php';
            $config = HTMLPurifier_Config::createDefault();
            $config->set('Cache.SerializerPath', '/tmp');
            $purifier = new HTMLPurifier($config);
    
            $page->head        .= '<link rel="home" href="'.htmlentities($_GET['u'], ENT_QUOTES).'" />';
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
            $page->titlegraphic = $purifier->purify(str_replace(array('<h1>', '</h1>'), '', $scanned->titlegraphic));
            
            foreach (array('breadcrumbs', 'navlinks', 'leftcollinks', 'contactinfo', 'footercontent') as $region) {
                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;
                    }
                    
                    $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
110
111
112
113
114
115
116
117
118
    $context = json_encode($context);
    $inlineJS .= "var LOCAL_SEARCH_CONTEXT = {$context};\n";
    
    ob_start();
    renderTemplate('templates/google-results.tpl.php', array(
        'title' => $page->titlegraphic,
        'id' => 'local_results',
    ));
    $local_results = ob_get_clean();
} elseif (!$isEmbed) {
119
    // Default search for no referring site.
120
    loadDefaultSections($page);
121
}
122

123
124
125
if (isset($_GET['q'])) {
    $q = json_encode($_GET['q']);
    $inlineJS .= "var INITIAL_QUERY = {$q};\n";
126
127
}

128
$page->addScriptDeclaration($inlineJS);
129

130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
ob_start();

if (!$isEmbed) {
    renderTemplate('templates/search-form.tpl.php', array('local_results' => $local_results));
}

renderTemplate('templates/search-results.tpl.php', array(
    'isEmbed' => $isEmbed,
    'local_results' => $local_results
));

$maincontent = ob_get_clean();

if (!$isEmbed) {
    $page->maincontentarea = $maincontent;
    echo $page;
} else {
147
148
149
150
151
152
    if (UNL_Search::$mode === 'debug') {
        $template = 'templates/embed-debug.tpl.php';
    } else {
        $template = 'templates/embed.tpl.php';
    }
    renderTemplate($template, array(
153
154
155
        'head' => $page->head,
        'maincontent' => $maincontent
    ));
156
}