index.php 4.94 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
65
$page->addScript('//www.google.com/jsapi?' . http_build_query($params));
$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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
    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).'" />';
            $page->breadcrumbs  = $purifier->purify(UNL_Search::removeRelativePaths($scanned->breadcrumbs, $_GET['u']));
            $page->titlegraphic = $purifier->purify(str_replace(array('<h1>', '</h1>'), '',$scanned->titlegraphic));
            $page->navlinks     = $purifier->purify(UNL_Search::removeRelativePaths($scanned->navlinks, $_GET['u']));
            if (!empty($scanned->leftcollinks)) {
                $page->leftcollinks = $purifier->purify($scanned->leftcollinks);
            }
            if (!empty($scanned->contactinfo)) {
                $page->contactinfo = $purifier->purify(preg_replace('#<h3>.*</h3>#', '', $scanned->contactinfo));
            }
            if (!empty($scanned->footercontent)) {
                $page->footercontent = $purifier->purify($scanned->footercontent);
            }
        } else {
            loadDefaultSections($page);
93
        }
94
    }
95

96
97
    if (isset($_GET['cx'])) {
        // Use their custom search engine instead of the linked one.
98
        $context = $_GET['cx'];
99
100
    } else {
        // Auto-build a custom search engine
101
        $context = array('crefUrl' => UNL_Search::getLinkedCSEUrl($_GET['u']));
102
    }
103
104
105
106
107
108
109
110
111
112
    $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) {
113
    // Default search for no referring site.
114
    loadDefaultSections($page);
115
}
116

117
118
119
if (isset($_GET['q'])) {
    $q = json_encode($_GET['q']);
    $inlineJS .= "var INITIAL_QUERY = {$q};\n";
120
121
}

122
$page->addScriptDeclaration($inlineJS);
123

124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
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 {
141
142
143
144
145
146
    if (UNL_Search::$mode === 'debug') {
        $template = 'templates/embed-debug.tpl.php';
    } else {
        $template = 'templates/embed.tpl.php';
    }
    renderTemplate($template, array(
147
148
149
        'head' => $page->head,
        'maincontent' => $maincontent
    ));
150
}