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

45
$local_results = '';
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
70
71
72
73
74
75
    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).'" />';
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
            $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);
                }
93
94
95
            }
        } else {
            loadDefaultSections($page);
96
        }
97
    }
98

99
100
    if (isset($_GET['cx'])) {
        // Use their custom search engine instead of the linked one.
101
        $context = $_GET['cx'];
102
103
    } else {
        // Auto-build a custom search engine
104
        $context = array('crefUrl' => UNL_Search::getLinkedCSEUrl($_GET['u']));
105
    }
106
107
108
    $context = json_encode($context);
    $inlineJS .= "var LOCAL_SEARCH_CONTEXT = {$context};\n";
    
Michael Fairchild's avatar
Michael Fairchild committed
109
    $local_results = renderTemplate('templates/google-results.tpl.php', array(
110
111
112
113
        'title' => $page->titlegraphic,
        'id' => 'local_results',
    ));
} elseif (!$isEmbed) {
114
    // Default search for no referring site.
115
    loadDefaultSections($page);
116
}
117

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

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

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

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

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