Ejemplo 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.
  *
  * @author Sami Barakat <s.m.barakat AT gmail DOT com>
  * @access public
  * @return string HTML output for search results
  *
  */
 function doSearch()
 {
     global $_CONF, $LANG01, $LANG09, $LANG31, $_TABLES, $_USER;
     $debug_info = '';
     $retval = '';
     $list_top = '';
     // Verify current user can perform requested search
     if (!$this->_isSearchAllowed()) {
         return $this->_getAccessDeniedMessage();
     }
     // Make sure there is a query string
     // Full text searches have a minimum word length of 3 by default
     if (empty($this->_query)) {
         if ((empty($this->_author) || $this->_author == 0) && (empty($this->_type) || $this->_type == 'all') && (empty($this->_topic) || $this->_topic == 'all') && (empty($this->_dateStart) || empty($this->_dateEnd))) {
             $retval = $this->showForm();
             $retval .= '<div style="margin-bottom:5px;border-bottom:1px solid #ccc;"></div><p>' . $LANG09[41] . '</p>' . LB;
             return $retval;
         }
     } elseif (strlen($this->_query) < 3) {
         $retval = $this->showForm();
         $retval .= '<div style="margin-bottom:5px;border-bottom:1px solid #ccc;"></div><p>' . $LANG09[41] . '</p>' . LB;
         return $retval;
     }
     // Build the URL strings
     $this->_searchURL = $_CONF['site_url'] . '/search.php?query=' . urlencode($this->_query) . (!empty($this->_keyType) ? '&amp;keyType=' . urlencode($this->_keyType) : '') . (!empty($this->_dateStart) ? '&amp;datestart=' . urlencode($this->_dateStart) : '') . (!empty($this->_dateEnd) ? '&amp;dateend=' . urlencode($this->_dateEnd) : '') . (!empty($this->_topic) ? '&amp;topic=' . urlencode($this->_topic) : '') . (!empty($this->_author) ? '&amp;author=' . urlencode($this->_author) : '') . (!empty($this->_searchDays) ? '&amp;st=' . urlencode($this->_searchDays) : '');
     $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['search_show_user'];
     $show_hits = $_CONF['search_show_hits'];
     $style = isset($_CONF['search_style']) ? $_CONF['search_style'] : 'google';
     if (!COM_isAnonUser()) {
         $userStyle = DB_getItem($_TABLES['userprefs'], 'search_result_format', 'uid=' . (int) $_USER['uid']);
         if ($userStyle != '') {
             $style = $userStyle;
         }
     }
     if ($style == 'table') {
         $obj->setStyle('table');
         //             Title        Name           Display     Sort   Format
         $obj->setField($LANG09[62], ROW_NUMBER, $show_num, false, '<b>%d.</b>');
         $obj->setField($LANG09[5], SQL_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;
     } else {
         if ($style == 'google') {
             $obj->setStyle('inline');
             $obj->setField('', ROW_NUMBER, $show_num, false, '<span style="font-size:larger; font-weight:bold;">%d.</span>');
             $obj->setField($LANG09[16], 'title', true, true, '<span style="font-size:larger; font-weight:bold;">%s</span><br/>');
             $obj->setField('', 'description', true, false, '%s<br/>');
             $obj->setField('', '_html', true, false, '<span style="color:green;">');
             $obj->setField($LANG09[18], 'uid', $show_user, true, $LANG01[104] . ' %s ');
             $obj->setField($LANG09[17], 'date', true, true, $LANG01[36] . ' %s');
             $obj->setField($LANG09[5], SQL_TITLE, $show_type, true, ' - %s');
             $obj->setField($LANG09[50], 'hits', $show_hits, true, ' - %s ' . $LANG09[50]);
             $obj->setField('', '_html', true, false, '</span>');
             $this->_wordlength = 50;
         }
     }
     $obj->setDefaultSort('date');
     $obj->setRowFunction(array($this, 'searchFormatCallBack'));
     // Start search timer
     $searchtimer = new timerobject();
     $searchtimer->setPercision(4);
     $searchtimer->startTimer();
     // Have plugins do their searches
     $page = isset($_REQUEST['page']) ? COM_applyFilter($_REQUEST['page'], true) : 1;
     $result_plugins = PLG_doSearch($this->_query, $this->_dateStart, $this->_dateEnd, $this->_topic, $this->_type, $this->_author, $this->_keyType, $page, 5);
     $result_plugins_comment = PLG_doSearchComment($this->_query, $this->_dateStart, $this->_dateEnd, $this->_topic, $this->_type, $this->_author, $this->_keyType, $page, 5);
     $result_plugins = array_merge($result_plugins, $result_plugins_comment);
     // Add core searches
     if ($this->_type == 'all' || $this->_type == 'stories') {
         $result_plugins[] = $this->_searchStories();
     }
     if ($this->_type == 'all' || $this->_type == 'comments') {
         $result_plugins[] = $this->_searchComments();
     }
     // Loop through all plugins separating the new API from the old
     $new_api = 0;
     $old_api = 0;
     $num_results = 0;
     if (!isset($_CONF['search_use_fulltext'])) {
         $_CONF['search_use_fulltext'] = false;
     }
     foreach ($result_plugins as $result) {
         if (is_a($result, 'SearchCriteria')) {
             $debug_info .= $result->getName() . " using APIv2, ";
             $type = $result->getType();
             if ($type == 'sql') {
                 if ($_CONF['search_use_fulltext'] == true && $result->getFTSQL() != '') {
                     $debug_info .= "search using FULLTEXT\n";
                     $sql = $result->getFTSQL();
                 } else {
                     $debug_info .= "search using LIKE\n";
                     $sql = $result->getSQL();
                 }
                 $sql = $this->_convertsql($sql);
                 $obj->setQuery($result->getLabel(), $result->getName(), $sql, $result->getRank());
                 $this->_url_rewrite[$result->getName()] = $result->UrlRewriteEnable() ? true : false;
             } else {
                 if ($type == 'text') {
                     $obj->setQueryText($result->getLabel(), $result->getName(), $this->_query, $result->getNumResults(), $result->getRank());
                 }
             }
             $new_api++;
         } else {
             if (is_a($result, 'Plugin') && $result->num_searchresults != 0) {
                 // Some backwards compatibility
                 $debug_info .= $result->plugin_name . " using APIv1, search using backwards compatibility\n";
                 // 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'));
                 //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'
                 $col_url = $this->_findColumn($result->searchheading, array('URL'));
                 //'Hits','Views'
                 $label = str_replace($LANG09[59], '', $result->searchlabel);
                 if ($result->num_itemssearched > 0) {
                     $_page = isset($_REQUEST['page']) ? COM_applyFilter($_REQUEST['page'], true) : 1;
                     if (isset($_REQUEST['results'])) {
                         $_per_page = COM_applyFilter($_REQUEST['results'], true);
                     } else {
                         $_per_page = $obj->getPerPage();
                     }
                     $obj->addTotalRank(3);
                     $pp = round(3 / $obj->getTotalRank() * $_per_page);
                     $offset = ($_page - 1) * $pp;
                     $limit = $pp;
                     $obj->addToTotalFound($result->num_itemssearched);
                     $counter = 0;
                     // Extract the results
                     foreach ($result->searchresults as $old_row) {
                         if ($counter >= $offset && $counter <= $offset + $limit) {
                             if ($col_date != -1) {
                                 // Convert the date back to a timestamp
                                 $date = $old_row[$col_date];
                                 $date = substr($date, 0, strpos($date, '@'));
                                 if ($date == '') {
                                     $date = $old_row[$col_date];
                                 } else {
                                     $date = strtotime($date);
                                 }
                             }
                             $api_results = array(SQL_NAME => $result->plugin_name, SQL_TITLE => $label, 'title' => $col_title == -1 ? $_CONF['search_no_data'] : $old_row[$col_title], 'description' => $col_desc == -1 ? $_CONF['search_no_data'] : $old_row[$col_desc], 'date' => $col_date == -1 ? '&nbsp;' : $date, 'uid' => $col_user == -1 ? '' : $old_row[$col_user], 'hits' => $col_hits == -1 ? '0' : str_replace(',', '', $old_row[$col_hits]), 'url' => $old_row[$col_url]);
                             $obj->addResult($api_results);
                         }
                         $counter++;
                     }
                 }
                 $old_api++;
             }
         }
     }
     // Find out how many plugins are on the old/new system
     $debug_info .= "\nAPIv1: {$old_api}\nAPIv2: {$new_api}";
     // Execute the queries
     $results = $obj->ExecuteQueries();
     // Searches are done, stop timer
     $searchtime = $searchtimer->stopTimer();
     $escquery = htmlspecialchars($this->_query);
     if ($this->_keyType == 'any') {
         $searchQuery = str_replace(' ', "</b>' " . $LANG09[57] . " '<b>", $escquery);
         $searchQuery = "<b>'{$searchQuery}'</b>";
     } else {
         if ($this->_keyType == 'all') {
             $searchQuery = str_replace(' ', "</b>' " . $LANG09[56] . " '<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);
     $searchText = "{$LANG09[25]} {$searchQuery}. ";
     $retval .= $this->showForm();
     if (count($results) == 0) {
         $retval .= '<div style="margin-bottom:5px;border-bottom:1px solid #ccc;"></div>';
         $retval .= $LANG09[74];
     } else {
         $retval .= $obj->getFormattedOutput($results, $LANG09[11], $list_top, '');
     }
     return $retval;
 }
Ejemplo n.º 2
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;
     } else {
         if ($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());
             } else {
                 if ($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);
             }
         } else {
             if (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') {
         $searchQuery = str_replace(' ', "</b>' " . $LANG09[57] . " '<b>", $escquery);
         $searchQuery = "<b>'{$searchQuery}'</b>";
     } else {
         if ($this->_keyType == 'all') {
             $searchQuery = str_replace(' ', "</b>' " . $LANG09[56] . " '<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;
         $retval = $obj->getFormattedOutput($results, $LANG09[11], $retval, '', $_CONF['search_show_sort'], $_CONF['search_show_limit']);
     }
     return $retval;
 }