index.php 4.59 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
16
17
function renderTemplate($file, $params = array())
{
    extract($params);
    include $file;
}

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

18
UNL_Templates::setCachingService(new UNL_Templates_CachingService_Null());
19
UNL_Templates::$options['version'] = 4.0;
20

21
22
23
24
25
26
27
28

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');
}
29

30
31
32
33
34
35
36
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();
}
37

38
$local_results = '';
39
$inlineJS = '';
Brett Bieber's avatar
Brett Bieber committed
40
$apiKey = UNL_Search::getJSAPIKey();
41
42
43
44
45
46
47
48
49
50
51
52
$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;
53
}
54
55
$page->addScript('//www.google.com/jsapi?' . http_build_query($params));
$page->addStyleSheet('css/search.css');
56

57
58
//u is referring site
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 && isset($scanned->titlegraphic)) { 
62
63
64
65
        require_once 'HTMLPurifier.auto.php';
        $config = HTMLPurifier_Config::createDefault();
        $config->set('Cache.SerializerPath', '/tmp');
        $purifier = new HTMLPurifier($config);
66

67
68
        $page->head        .= '<link rel="home" href="'.htmlentities($_GET['u'], ENT_QUOTES).'" />';
        $page->breadcrumbs  = $purifier->purify(UNL_Search::removeRelativePaths($scanned->breadcrumbs, $_GET['u']));
69
        $page->titlegraphic = $purifier->purify(str_replace(array('<h1>', '</h1>'), '',$scanned->titlegraphic));
70
71
        $page->navlinks     = $purifier->purify(UNL_Search::removeRelativePaths($scanned->navlinks, $_GET['u']));
        if (!empty($scanned->contactinfo)) {
72
            $page->contactinfo = $purifier->purify(preg_replace('#<h3>.*</h3>#', '', $scanned->contactinfo));
73
        }
74
75
76
        if (!empty($scanned->footercontent)) {
            $page->footercontent = $purifier->purify($scanned->footercontent);
        }
77
    }
78

79
80
    if (isset($_GET['cx'])) {
        // Use their custom search engine instead of the linked one.
81
        $context = $_GET['cx'];
82
83
    } else {
        // Auto-build a custom search engine
84
        $context = array('crefUrl' => UNL_Search::getLinkedCSEUrl($_GET['u']));
85
    }
86
87
88
89
90
91
92
93
94
95
    $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) {
96
    // Default search for no referring site.
97
    $page->titlegraphic = 'UNL Search';
Brett Bieber's avatar
Brett Bieber committed
98
99
100
    $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');
101
    $page->contactinfo   = preg_replace('#<h3>.*</h3>#', '', file_get_contents('http://www.unl.edu/ucomm/sharedcode/footerContactInfo.html'));
Brett Bieber's avatar
Brett Bieber committed
102
    $page->footercontent = file_get_contents('http://www.unl.edu/ucomm/sharedcode/footer.html');
103
}
104

105
106
107
if (isset($_GET['q'])) {
    $q = json_encode($_GET['q']);
    $inlineJS .= "var INITIAL_QUERY = {$q};\n";
108
109
}

110
$page->addScriptDeclaration($inlineJS);
111

112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
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 {
129
130
131
132
133
134
    if (UNL_Search::$mode === 'debug') {
        $template = 'templates/embed-debug.tpl.php';
    } else {
        $template = 'templates/embed.tpl.php';
    }
    renderTemplate($template, array(
135
136
137
        'head' => $page->head,
        'maincontent' => $maincontent
    ));
138
}