index.php 4.83 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
if (UNL_Search::$mode === 'debug') {
Michael Fairchild's avatar
Michael Fairchild committed
32
    $page = Templates::factory('Local', Templates::VERSION_4_1);
33
34
    $page->addScript('js/search.js');
} else {
Michael Fairchild's avatar
Michael Fairchild committed
35
    $page = Templates::factory('Fixed', Templates::VERSION_4_1);
36
37
    $page->addScript('js/search.min.js');
}
38

39
40
41
if (!$isEmbed) {
    $page->doctitle = '<title>Search | University of Nebraska&ndash;Lincoln</title>';
    $page->pagetitle = '';
Michael Fairchild's avatar
Michael Fairchild committed
42
    $page->breadcrumbs = renderTemplate('templates/breadcrumbs.tpl.php');
43
}
44

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

64
65
//u is referring site
if (isset($_GET['u']) && $scanned = UNL_Search::getScannedPage($_GET['u'])) { 
66
    // Add local site search results
67
    // we're scrapping the navigation and other content from the originatting site.
68
69
    if (!$isEmbed) {
        if (isset($scanned->titlegraphic)) { 
70
            //require_once 'HTMLPurifier.auto.php';
71
72
73
74
75
            $config = HTMLPurifier_Config::createDefault();
            $config->set('Cache.SerializerPath', '/tmp');
            $purifier = new HTMLPurifier($config);
    
            $page->head        .= '<link rel="home" href="'.htmlentities($_GET['u'], ENT_QUOTES).'" />';
76
            $page->titlegraphic = $purifier->purify(str_replace(array('<h1>', '</h1>'), '', $scanned->titlegraphic));
77
            $page->affiliation = '';
78
            
79
            foreach (array('breadcrumbs', 'navlinks', 'leftcollinks', 'contactinfo', 'affiliation') as $region) {
80
81
82
83
84
85
86
87
88
89
90
91
92
93
                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);
                }
94
95
96
            }
        } else {
            loadDefaultSections($page);
97
        }
98
    }
99

100
101
    if (isset($_GET['cx'])) {
        // Use their custom search engine instead of the linked one.
102
        $context = $_GET['cx'];
103
104
    } else {
        // Auto-build a custom search engine
105
        $context = array('crefUrl' => UNL_Search::getLinkedCSEUrl($_GET['u']));
106
    }
107
108
109
    $context = json_encode($context);
    $inlineJS .= "var LOCAL_SEARCH_CONTEXT = {$context};\n";
    
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
120
121
if (isset($_GET['q'])) {
    $q = json_encode($_GET['q']);
    $inlineJS .= "var INITIAL_QUERY = {$q};\n";
122
123
}

124
$page->addScriptDeclaration($inlineJS);
125

Michael Fairchild's avatar
Michael Fairchild committed
126
$maincontent = '';
127
if (!$isEmbed) {
Michael Fairchild's avatar
Michael Fairchild committed
128
    $maincontent .= renderTemplate('templates/search-form.tpl.php', array('local_results' => $localResults));
129
130
}

Michael Fairchild's avatar
Michael Fairchild committed
131
$maincontent .= renderTemplate('templates/search-results.tpl.php', array(
132
    'isEmbed' => $isEmbed,
Michael Fairchild's avatar
Michael Fairchild committed
133
    'local_results' => $localResults
134
135
136
137
138
139
));

if (!$isEmbed) {
    $page->maincontentarea = $maincontent;
    echo $page;
} else {
140
141
142
143
144
    if (UNL_Search::$mode === 'debug') {
        $template = 'templates/embed-debug.tpl.php';
    } else {
        $template = 'templates/embed.tpl.php';
    }
Michael Fairchild's avatar
Michael Fairchild committed
145
    echo renderTemplate($template, array(
146
147
148
        'head' => $page->head,
        'maincontent' => $maincontent
    ));
149
}