Exemplo n.º 1
0
 /**
  * Kicks off the appropriate search(es)
  * Initiates the search engine and returns HTML formatted
  * results. It also provides support to plugins using a
  * search API. Backwards compatibility has been incorporated
  * in this function to allow legacy support to plugins using
  * the old API calls defined versions prior to Geeklog 1.5.1
  *
  * @return string HTML output for search results
  */
 public function doSearch()
 {
     global $_CONF, $LANG01, $LANG09, $LANG31;
     // Verify current user can perform requested search
     if (!$this->_isSearchAllowed()) {
         return SEC_loginRequiredForm();
     }
     // When full text searches are enabled, make sure the min. query length
     // is 3 characters. Otherwise, make sure at least one of query string,
     // author, or topic is not empty.
     if (empty($this->_query) && empty($this->_author) && empty($this->_topic) || $_CONF['search_use_fulltext'] && strlen($this->_query) < 3) {
         $retval = '<p>' . $LANG09[41] . '</p>' . LB;
         $retval .= $this->showForm();
         return $retval;
     }
     // Build the URL strings
     $this->_searchURL = $_CONF['site_url'] . '/search.php?query=' . urlencode($this->_query) . (!empty($this->_keyType) ? '&amp;keyType=' . $this->_keyType : '') . (!empty($this->_dateStart) ? '&amp;datestart=' . $this->_dateStart : '') . (!empty($this->_dateEnd) ? '&amp;dateend=' . $this->_dateEnd : '') . (!empty($this->_topic) ? '&amp;topic=' . $this->_topic : '') . (!empty($this->_author) ? '&amp;author=' . $this->_author : '') . ($this->_titlesOnly ? '&amp;title=true' : '');
     $url = "{$this->_searchURL}&amp;type={$this->_type}&amp;mode=";
     $obj = new ListFactory($url . 'search', $_CONF['search_limits'], $_CONF['num_search_results']);
     $obj->setField('ID', 'id', false);
     $obj->setField('URL', 'url', false);
     $show_num = $_CONF['search_show_num'];
     $show_type = $_CONF['search_show_type'];
     $show_user = $_CONF['contributedbyline'];
     $show_hits = !$_CONF['hideviewscount'];
     $style = isset($_CONF['search_style']) ? $_CONF['search_style'] : 'google';
     if ($style === 'table') {
         $obj->setStyle('table');
         //             Title        Name            Display     Sort   Format
         $obj->setField($LANG09[62], LF_ROW_NUMBER, $show_num, false, '<b>%d.</b>');
         $obj->setField($LANG09[5], LF_SOURCE_TITLE, $show_type, true, '<b>%s</b>');
         $obj->setField($LANG09[16], 'title', true, true);
         $obj->setField($LANG09[63], 'description', true, false);
         $obj->setField($LANG09[17], 'date', true, true);
         $obj->setField($LANG09[18], 'uid', $show_user, true);
         $obj->setField($LANG09[50], 'hits', $show_hits, true);
         $this->_wordlength = 7;
     } elseif ($style === 'google') {
         $sort_uid = $this->_author == '' ? true : false;
         $sort_date = empty($this->_dateStart) || empty($this->_dateEnd) || $this->_dateStart != $this->_dateEnd ? true : false;
         $sort_type = $this->_type == 'all' ? true : false;
         $obj->setStyle('inline');
         $obj->setField('', LF_ROW_NUMBER, $show_num, false, '<b>%d.</b>');
         $obj->setField($LANG09[16], 'title', true, true, '%s<br' . XHTML . '>');
         $obj->setField('', 'description', true, false, '%s<br' . XHTML . '>');
         $obj->setField('', '_html', true, false, '<span class="searchresult-byline">');
         $obj->setField($LANG09[18], 'uid', $show_user, $sort_uid, $LANG01[104] . ' %s ');
         $obj->setField($LANG09[17], 'date', true, $sort_date, $LANG01[36] . ' %s');
         $obj->setField($LANG09[5], LF_SOURCE_TITLE, $show_type, $sort_type, ' - %s');
         $obj->setField($LANG09[50], 'hits', $show_hits, true, ' - %s ' . $LANG09[50]);
         $obj->setField('', '_html', true, false, '</span>');
         $this->_wordlength = 50;
     }
     // get default sort order
     $default_sort = explode('|', $_CONF['search_def_sort']);
     $obj->setDefaultSort($default_sort[0], $default_sort[1]);
     // set this only now, for compatibility with PHP 4
     $obj->setRowFunction(array($this, 'searchFormatCallback'));
     // Start search timer
     $searchTimer = new timerobject();
     $searchTimer->setPrecision(4);
     $searchTimer->startTimer();
     // Have plugins do their searches
     $page = isset($_GET['page']) ? COM_applyFilter($_GET['page'], true) : 1;
     $result_plugins = PLG_doSearch($this->_query, $this->_dateStart, $this->_dateEnd, $this->_topic, $this->_type, $this->_author, $this->_keyType, $page, 5);
     // Add core searches
     $result_plugins = array_merge($result_plugins, $this->_searchStories());
     // Loop through all plugins separating the new API from the old
     $new_api = 0;
     $old_api = 0;
     $num_results = 0;
     foreach ($result_plugins as $result) {
         if (is_a($result, 'SearchCriteria')) {
             $debug_info = $result->getName() . ' using APIv2';
             if ($this->_type != 'all' && $this->_type != $result->getName()) {
                 if ($this->_verbose) {
                     $new_api++;
                     COM_errorLog($debug_info . '. Skipped as type is not ' . $this->_type);
                 }
                 continue;
             }
             $api_results = $result->getResults();
             if (!empty($api_results)) {
                 $obj->addResultArray($api_results);
             }
             $api_callback_func = $result->getCallback();
             if (!empty($api_callback_func)) {
                 $debug_info .= ' with Callback Function.';
                 $obj->setCallback($result->getLabel(), $result->getName(), $api_callback_func, $result->getRank(), $result->getTotal());
             } elseif ($result->getSQL() != '' || $result->getFTSQL() != '') {
                 if ($_CONF['search_use_fulltext'] == true && $result->getFTSQL() != '') {
                     $sql = $result->getFTSQL();
                 } else {
                     $sql = $result->getSQL();
                 }
                 $sql = $this->_convertsql($sql);
                 $debug_info .= ' with SQL = ' . print_r($sql, 1);
                 $obj->setQuery($result->getLabel(), $result->getName(), $sql, $result->getRank());
             }
             $this->_url_rewrite[$result->getName()] = $result->UrlRewriteEnable();
             $this->_append_query[$result->getName()] = $result->AppendQueryEnable();
             if ($this->_verbose) {
                 $new_api++;
                 COM_errorLog($debug_info);
             }
         } elseif (is_a($result, 'Plugin') && $result->num_searchresults != 0) {
             // Some backwards compatibility
             if ($this->_verbose) {
                 $old_api++;
                 $debug_info = $result->plugin_name . ' using APIv1 with backwards compatibility.';
                 $debug_info .= ' Count: ' . $result->num_searchresults;
                 $debug_info .= ' Headings: ' . implode(',', $result->searchheading);
                 COM_errorLog($debug_info);
             }
             // Find the column heading names that closely match what we are looking for
             // There may be issues here on different languages, but this _should_ capture most of the data
             $col_title = $this->_findColumn($result->searchheading, array($LANG09[16], $LANG31[4], 'Question', 'Site Page'));
             //Title,Subject
             $col_desc = $this->_findColumn($result->searchheading, array($LANG09[63], 'Answer'));
             $col_date = $this->_findColumn($result->searchheading, array($LANG09[17]));
             //'Date','Date Added','Last Updated','Date & Time'
             $col_user = $this->_findColumn($result->searchheading, array($LANG09[18], 'Submited by'));
             $col_hits = $this->_findColumn($result->searchheading, array($LANG09[50], $LANG09[23], 'Downloads', 'Clicks'));
             //'Hits','Views'
             $label = str_replace($LANG09[59], '', $result->searchlabel);
             $num_results += $result->num_itemssearched;
             // Extract the results
             for ($i = 0; $i < 5; $i++) {
                 // If the plugin does not repect the $perpage perameter force it here.
                 $j = $i + $page * 5 - 5;
                 if ($j >= count($result->searchresults)) {
                     break;
                 }
                 $old_row = $result->searchresults[$j];
                 if ($col_date != -1) {
                     // Convert the date back to a timestamp
                     $date = $old_row[$col_date];
                     $date = substr($date, 0, strpos($date, '@'));
                     $date = $date == '' ? $old_row[$col_date] : strtotime($date);
                 }
                 $api_results = array(LF_SOURCE_NAME => $result->plugin_name, LF_SOURCE_TITLE => $label, 'title' => $col_title == -1 ? '<i>' . $LANG09[70] . '</i>' : $old_row[$col_title], 'description' => $col_desc == -1 ? '<i>' . $LANG09[70] . '</i>' : $old_row[$col_desc], 'date' => $col_date == -1 ? '&nbsp;' : $date, 'uid' => $col_user == -1 ? '&nbsp;' : $old_row[$col_user], 'hits' => $col_hits == -1 ? '0' : str_replace(',', '', $old_row[$col_hits]));
                 preg_match('/href="([^"]+)"/i', $api_results['title'], $links);
                 $api_results['url'] = empty($links) ? '#' : $links[1];
                 $obj->addResult($api_results);
             }
         }
     }
     // Find out how many plugins are on the old/new system
     if ($this->_verbose) {
         COM_errorLog('Search Plugins using APIv1: ' . $old_api . ' APIv2: ' . $new_api);
     }
     // Execute the queries
     $results = $obj->ExecuteQueries();
     // Searches are done, stop timer
     $searchtime = $searchTimer->stopTimer();
     $escquery = htmlspecialchars($this->_query);
     $escquery = str_replace(array('{', '}'), array('&#123;', '&#125;'), $escquery);
     if ($this->_keyType == 'any' or $this->_keyType == 'all') {
         $words = array_unique(explode(' ', $escquery));
         $words = array_filter($words);
         // filter out empty strings
         $escquery = implode(' ', $words);
         if ($this->_keyType == 'any') {
             $lang_search_op = $LANG09[57];
         } elseif ($this->_keyType == 'all') {
             $lang_search_op = $LANG09[56];
         }
         $searchQuery = str_replace(' ', "</b>' " . $lang_search_op . " '<b>", $escquery);
         $searchQuery = "<b>'{$searchQuery}'</b>";
     } else {
         $searchQuery = $LANG09[55] . " '<b>{$escquery}</b>'";
     }
     // Clean the query string so that sprintf works as expected
     $searchQuery = str_replace('%', '%%', $searchQuery);
     $retval = "{$LANG09[25]} {$searchQuery}. ";
     if (count($results) == 0) {
         $retval .= sprintf($LANG09[24], 0);
         $retval = '<p>' . $retval . '</p>' . LB;
         $retval .= '<p>' . $LANG09[13] . '</p>' . LB;
         $retval .= $this->showForm();
     } else {
         $retval .= $LANG09[64] . " ({$searchtime} {$LANG09[27]}). ";
         $retval .= str_replace('%', '%%', COM_createLink($LANG09[61], $url . 'refine'));
         $retval = '<p>' . $retval . '</p>' . LB;
         if (version_compare($_CONF['supported_version_theme'], '2.0.0', '>=')) {
             // Make GET params array
             $params_arr = array();
             $params_arr['query'] = urlencode($this->_query);
             if (!empty($this->_keyType)) {
                 $params_arr['keyType'] = $this->_keyType;
             }
             if (!empty($this->_dateStart)) {
                 $params_arr['datestart'] = $this->_dateStart;
             }
             if (!empty($this->_dateEnd)) {
                 $params_arr['dateend'] = $this->_dateEnd;
             }
             if (!empty($this->_topic)) {
                 $params_arr['topic'] = $this->_topic;
             }
             if (!empty($this->_author)) {
                 $params_arr['author'] = $this->_author;
             }
             if ($this->_titlesOnly) {
                 $params_arr['title'] = true;
             }
             $params_arr['type'] = $this->_type;
             $params_arr['mode'] = 'search';
             $retval = $obj->getFormattedOutput2($results, $LANG09[11], $retval, '', $_CONF['search_show_sort'], $_CONF['search_show_limit'], $params_arr);
         } else {
             $retval = $obj->getFormattedOutput($results, $LANG09[11], $retval, '', $_CONF['search_show_sort'], $_CONF['search_show_limit']);
         }
     }
     return $retval;
 }