From b78b5f23cd80feff31036a771115ceb654c29492 Mon Sep 17 00:00:00 2001
From: Kevin Abel <kevin.abel.0@gmail.com>
Date: Thu, 17 Apr 2014 17:25:13 -0500
Subject: [PATCH] Implement the 4.0 template and embedded search in the
 controller

---
 www/index.php | 163 +++++++++++++++++++++++++-------------------------
 1 file changed, 83 insertions(+), 80 deletions(-)

diff --git a/www/index.php b/www/index.php
index 237db7c..2c36116 100644
--- a/www/index.php
+++ b/www/index.php
@@ -1,55 +1,63 @@
 <?php
 
 $config_file = __DIR__ . '/../config.sample.php';
-
 if (file_exists(__DIR__ . '/../config.inc.php')) {
     $config_file = __DIR__ . '/../config.inc.php';
 }
+
 require_once $config_file;
 
+function renderTemplate($file, $params = array())
+{
+    extract($params);
+    include $file;
+}
+
+$isEmbed = isset($_GET['embed']) && $_GET['embed'];
+
 UNL_Templates::setCachingService(new UNL_Templates_CachingService_Null());
+UNL_Templates::$options['version'] = 4.0;
 
-UNL_Templates::$options['version'] = 3.1;
+$page = UNL_Templates::factory('Fixed');
 
-$template = 'Fixed';
+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();
+}
 
-if (isset($_GET['format'])
-    && $_GET['format'] == 'mobile') {
-    $template = 'Mobile';
+if (UNL_Search::$mode === 'debug') {
+    $page->addScript('js/search.js');
+} else {
+    $page->addScript('js/search.min.js');
 }
 
-$page = UNL_Templates::factory('Fixed');
-$page->doctitle = '<title>UNL | Search</title>';
-$page->pagetitle = '';
-$page->breadcrumbs = '
-<ul>
-    <li><a href="http://www.unl.edu/" title="University of Nebraska&ndash;Lincoln">UNL</a></li>
-    <li>Search</li>
-</ul>';
 $local_results = '';
+$inlineJS = '';
 $apiKey = UNL_Search::getJSAPIKey();
-$page->addScript('http://www.google.com/jsapi' . (empty($apiKey) ? '' : '?key=' . $apiKey));
-$page->head .= '
-<link rel="stylesheet" href="http://www.google.com/cse/style/look/default.css" type="text/css" />
-<link rel="stylesheet" type="text/css" href="http://directory.unl.edu/css/result_lists.css" />
-<link rel="stylesheet" type="text/css" href="searchCSS.css" />
-';
-if ($template == 'Mobile') {
-    $page->head .='<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0" >
-    <script type="text/javascript">var mobileSearch = true;</script>';
-} else {
-    $page->head .='<script type="text/javascript">var mobileSearch = false;</script>';
+$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;
 }
-$page->addScript('searchFunc.js');
+$page->addScript('//www.google.com/jsapi?' . http_build_query($params));
+$page->addStyleSheet('css/search.css');
 
-if (isset($_GET['u']) //u is referring site
-    && $_GET['u'] !== 'http://www.unl.edu/'
-    && preg_match('/^https?/', $_GET['u'])
-    && filter_var($_GET['u'], FILTER_VALIDATE_URL, FILTER_FLAG_SCHEME_REQUIRED)
-    && $scanned = @file_get_contents($_GET['u'])) {
+//u is referring site
+if (isset($_GET['u']) && $scanned = UNL_Search::getScannedPage($_GET['u'])) { 
     // Add local site search results
-    $scanned = new UNL_Templates_Scanner($scanned);
-    if (isset($scanned->navlinks)) { //we're scrapping the navigation and other content from the originatting site.
+    // we're scrapping the navigation and other content from the originatting site.
+    if (!$isEmbed && isset($scanned->navlinks)) { 
         require_once 'HTMLPurifier.auto.php';
         $config = HTMLPurifier_Config::createDefault();
         $config->set('Cache.SerializerPath', '/tmp');
@@ -63,7 +71,8 @@ if (isset($_GET['u']) //u is referring site
             $page->leftcollinks = $purifier->purify($scanned->leftcollinks);
         }
         if (!empty($scanned->contactinfo)) {
-            $page->contactinfo = $purifier->purify($scanned->contactinfo);
+            
+            $page->contactinfo = $purifier->purify(preg_replace('#<h3>.*</h3>#', '', $scanned->contactinfo));
         }
         if (!empty($scanned->footercontent)) {
             $page->footercontent = $purifier->purify($scanned->footercontent);
@@ -72,62 +81,56 @@ if (isset($_GET['u']) //u is referring site
 
     if (isset($_GET['cx'])) {
         // Use their custom search engine instead of the linked one.
-        $context = '"'.htmlentities($_GET['cx'], ENT_QUOTES).'"';
+        $context = $_GET['cx'];
     } else {
         // Auto-build a custom search engine
-        $context = '{crefUrl :"http://www1.unl.edu/search/linkedcse/?u='.$_GET['u'].'"}';
+        $context = array('crefUrl' => UNL_Search::getLinkedCSEUrl($_GET['u']));
     }
-    $page->addScriptDeclaration('
-        UNL_Search.do_local_search = true;
-        UNL_Search.local_search_context = '.$context.';
-    ');
-    $local_results = '
-    <h3 class="sec_header">'.$page->titlegraphic.' Results</h3>
-    <div id="local_results" class="google-results"></div>';
-} else {
+    $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) {
     // Default search for no referring site.
-    $page->titlegraphic = '<h1>UNL Search</h1>';
-    $page->addScriptDeclaration('UNL_Search.do_local_search = false;');
+    $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   = file_get_contents('http://www.unl.edu/ucomm/sharedcode/footerContactInfo.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');
+}
 
+if (isset($_GET['q'])) {
+    $q = json_encode($_GET['q']);
+    $inlineJS .= "var INITIAL_QUERY = {$q};\n";
 }
 
+$page->addScriptDeclaration($inlineJS);
 
-$page->maincontentarea = '
-<div id="searchform">
-  <form action="./" method="get">
-      <fieldset>
-          <label for="q">Search</label>
-          <input class="search-query" type="text" name="q" id="search_q" title="search" />';
-if (!empty($local_results)) {
-    $page->maincontentarea .= '<input type="hidden" name="u" value="'.htmlentities($_GET['u'], ENT_QUOTES).'" />';
+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 {
+    renderTemplate('templates/embed.tpl.php', array(
+        'head' => $page->head,
+        'maincontent' => $maincontent
+    ));
 }
-$page->maincontentarea .= '
-      <input class="search-button" title="search" type="submit" value="Search" />
-      </fieldset>
-  </form>
-  <noscript>
-    <form action="http://www.googlesyndicatedsearch.com/u/UNL1" method="get">
-        <input style="width:90%" type="text" name="q" />
-        <input type="submit" value="Search" />
-    </form>
-  </noscript>
-</div>
-<div class="grid8 first" id="search_results">
-    '.$local_results.'
-    <h3 class="sec_header">UNL Web</h3>
-    <div id="unl_results" class="google-results"></div>
-</div>
-<div class="grid4" id="directory_results">
-    <h3 class="sec_header">UNL Directory</h3>
-    <div id="ppl_results"></div>
-    <a href="http://www1.unl.edu/wdn/wiki/About_Peoplefinder">About the UNL Directory</a>
-</div>
-';
-echo $page;
-
-?>
-- 
GitLab