system/classes/search.class.php
author Sami Barakat
Sun, 25 Oct 2009 22:01:38 +0000
branchHEAD
changeset 7391 0bee991937cc
parent 7387 39932e68c099
child 7392 3872f0c040e4
permissions -rw-r--r--
Added callback function to search API (feature request #0000985)
     1 <?php
     2 
     3 /* Reminder: always indent with 4 spaces (no tabs). */
     4 // +---------------------------------------------------------------------------+
     5 // | Geeklog 1.6.1                                                             |
     6 // +---------------------------------------------------------------------------+
     7 // | search.class.php                                                          |
     8 // |                                                                           |
     9 // | Geeklog search class.                                                     |
    10 // +---------------------------------------------------------------------------+
    11 // | Copyright (C) 2000-2009 by the following authors:                         |
    12 // |                                                                           |
    13 // | Authors: Tony Bibbs       - tony AT geeklog DOT net                       |
    14 // |          Dirk Haun        - dirk AT haun-online DOT de                    |
    15 // |          Sami Barakat     - s.m.barakat AT gmail DOT com                  |
    16 // +---------------------------------------------------------------------------+
    17 // |                                                                           |
    18 // | This program is free software; you can redistribute it and/or             |
    19 // | modify it under the terms of the GNU General Public License               |
    20 // | as published by the Free Software Foundation; either version 2            |
    21 // | of the License, or (at your option) any later version.                    |
    22 // |                                                                           |
    23 // | This program is distributed in the hope that it will be useful,           |
    24 // | but WITHOUT ANY WARRANTY; without even the implied warranty of            |
    25 // | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the             |
    26 // | GNU General Public License for more details.                              |
    27 // |                                                                           |
    28 // | You should have received a copy of the GNU General Public License         |
    29 // | along with this program; if not, write to the Free Software Foundation,   |
    30 // | Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.           |
    31 // |                                                                           |
    32 // +---------------------------------------------------------------------------+
    33 
    34 if (strpos(strtolower($_SERVER['PHP_SELF']), 'search.class.php') !== false) {
    35     die('This file can not be used on its own.');
    36 }
    37 
    38 require_once $_CONF['path_system'] . 'classes/plugin.class.php';
    39 require_once $_CONF['path_system'] . 'classes/searchcriteria.class.php';
    40 require_once $_CONF['path_system'] . 'classes/listfactory.class.php';
    41 
    42 /**
    43 * Geeklog Search Class
    44 *
    45 * @author Tony Bibbs, tony AT geeklog DOT net
    46 * @package net.geeklog.search
    47 *
    48 */
    49 class Search {
    50 
    51     // PRIVATE VARIABLES
    52     var $_query = '';
    53     var $_topic = '';
    54     var $_dateStart = null;
    55     var $_dateEnd = null;
    56     var $_author = '';
    57     var $_type = '';
    58     var $_keyType = '';
    59     var $_names = array();
    60     var $_url_rewrite = array();
    61     var $_append_query = array();
    62     var $_searchURL = '';
    63     var $_wordlength;
    64     var $_verbose = false; // verbose logging
    65 
    66     /**
    67     * Constructor
    68     *
    69     * Sets up private search variables
    70     *
    71     * @author Tony Bibbs, tony AT geeklog DOT net
    72     * @access public
    73     *
    74     */
    75     function Search()
    76     {
    77         global $_CONF, $_TABLES;
    78 
    79         // Set search criteria
    80         if (isset ($_GET['query'])) {
    81             $this->_query = strip_tags (COM_stripslashes ($_GET['query']));
    82         }
    83         if (isset ($_GET['topic'])) {
    84             $this->_topic = COM_applyFilter ($_GET['topic']);
    85         }
    86         if (isset ($_GET['datestart'])) {
    87             $this->_dateStart = COM_applyFilter ($_GET['datestart']);
    88         }
    89         if (isset ($_GET['dateend'])) {
    90             $this->_dateEnd = COM_applyFilter ($_GET['dateend']);
    91         }
    92         if (isset ($_GET['author'])) {
    93             $this->_author = COM_applyFilter($_GET['author']);
    94 
    95             // In case we got a username instead of uid, convert it.  This should
    96             // make custom themes for search page easier.
    97             if (!is_numeric($this->_author) && !preg_match('/^([0-9]+)$/', $this->_author) && $this->_author != '') {
    98                 $this->_author = DB_getItem($_TABLES['users'], 'uid', "username='" . addslashes ($this->_author) . "'");
    99             }
   100 
   101             if ($this->_author < 1) {
   102                 $this->_author = '';
   103             }
   104         }
   105         $this->_type = isset($_GET['type']) ? COM_applyFilter($_GET['type']) : 'all';
   106         $this->_keyType = isset($_GET['keyType']) ? COM_applyFilter($_GET['keyType']) : $_CONF['search_def_keytype'];
   107     }
   108 
   109     /**
   110     * Shows an error message to anonymous users
   111     *
   112     * This is called when anonymous users attempt to access search
   113     * functionality that has been locked down by the Geeklog admin.
   114     *
   115     * @author Tony Bibbs, tony AT geeklog DOT net
   116     * @access private
   117     * @return string HTML output for error message
   118     *
   119     */
   120     function _getAccessDeniedMessage()
   121     {
   122         global $_CONF, $LANG_LOGIN;
   123 
   124         $retval .= COM_startBlock ($LANG_LOGIN[1], '',
   125                         COM_getBlockTemplate ('_msg_block', 'header'));
   126         $login = new Template($_CONF['path_layout'] . 'submit');
   127         $login->set_file (array ('login'=>'submitloginrequired.thtml'));
   128         $login->set_var ( 'xhtml', XHTML );
   129         $login->set_var ('login_message', $LANG_LOGIN[2]);
   130         $login->set_var ('site_url', $_CONF['site_url']);
   131         $login->set_var ('site_admin_url', $_CONF['site_admin_url']);
   132         $login->set_var ('layout_url', $_CONF['layout_url']);
   133         $login->set_var ('lang_login', $LANG_LOGIN[3]);
   134         $login->set_var ('lang_newuser', $LANG_LOGIN[4]);
   135         $login->parse ('output', 'login');
   136         $retval .= $login->finish ($login->get_var('output'));
   137         $retval .= COM_endBlock (COM_getBlockTemplate ('_msg_block', 'footer'));
   138 
   139         return $retval;
   140     }
   141 
   142     /**
   143     * Determines if user is allowed to perform a search
   144     *
   145     * Geeklog has a number of settings that may prevent
   146     * the access anonymous users have to the search engine.
   147     * This performs those checks
   148     *
   149     * @author Tony Bibbs, tony AT geeklog DOT net
   150     * @access private
   151     * @return boolean True if search is allowed, otherwise false
   152     *
   153     */
   154     function _isSearchAllowed()
   155     {
   156         global $_USER, $_CONF;
   157 
   158         if (empty($_USER['username'])) {
   159             //check if an anonymous user is attempting to illegally access privilege search capabilities
   160             if (($this->_type != 'all') OR !empty($this->_dateStart) OR !empty($this->_dateEnd) OR ($this->_author > 0) OR !empty($topic)) {
   161                 if (($_CONF['loginrequired'] == 1) OR ($_CONF['searchloginrequired'] >= 1)) {
   162                     return false;
   163                 }
   164             } else {
   165                 if (($_CONF['loginrequired'] == 1) OR ($_CONF['searchloginrequired'] == 2)) {
   166                     return false;
   167                 }
   168             }
   169         }
   170 
   171         return true;
   172     }
   173 
   174     /**
   175     * Determines if user is allowed to use the search form
   176     *
   177     * Geeklog has a number of settings that may prevent
   178     * the access anonymous users have to the search engine.
   179     * This performs those checks
   180     *
   181     * @author Dirk Haun, dirk AT haun-online DOT de
   182     * @access private
   183     * @return boolean True if form usage is allowed, otherwise false
   184     *
   185     */
   186     function _isFormAllowed ()
   187     {
   188         global $_CONF, $_USER;
   189 
   190         if (empty($_USER['username']) AND (($_CONF['loginrequired'] == 1) OR ($_CONF['searchloginrequired'] >= 1))) {
   191             return false;
   192         }
   193 
   194         return true;
   195     }
   196 
   197     /**
   198     * Shows search form
   199     *
   200     * Shows advanced search page
   201     *
   202     * @author Tony Bibbs, tony AT geeklog DOT net
   203     * @access public
   204     * @return string HTML output for form
   205     *
   206     */
   207     function showForm ()
   208     {
   209         global $_CONF, $_TABLES, $LANG09;
   210 
   211         $retval = '';
   212 
   213         // Verify current user my use the search form
   214         if (!$this->_isFormAllowed()) {
   215             return $this->_getAccessDeniedMessage();
   216         }
   217 
   218         $retval .= COM_startBlock($LANG09[1],'advancedsearch.html');
   219         $searchform = new Template($_CONF['path_layout'].'search');
   220         $searchform->set_file (array ('searchform' => 'searchform.thtml',
   221                                       'authors'    => 'searchauthors.thtml'));
   222         $searchform->set_var( 'xhtml', XHTML );
   223         $searchform->set_var('search_intro', $LANG09[19]);
   224         $searchform->set_var('site_url', $_CONF['site_url']);
   225         $searchform->set_var('site_admin_url', $_CONF['site_admin_url']);
   226         $searchform->set_var('layout_url', $_CONF['layout_url']);
   227         $searchform->set_var('lang_keywords', $LANG09[2]);
   228         $searchform->set_var('lang_date', $LANG09[20]);
   229         $searchform->set_var('lang_to', $LANG09[21]);
   230         $searchform->set_var('date_format', $LANG09[22]);
   231         $searchform->set_var('lang_topic', $LANG09[3]);
   232         $searchform->set_var('lang_all', $LANG09[4]);
   233         $searchform->set_var('topic_option_list',
   234                             COM_topicList ('tid,topic', $this->_topic));
   235         $searchform->set_var('lang_type', $LANG09[5]);
   236         $searchform->set_var('lang_results', $LANG09[59]);
   237         $searchform->set_var('lang_per_page', $LANG09[60]);
   238 
   239         $searchform->set_var('lang_exact_phrase', $LANG09[43]);
   240         $searchform->set_var('lang_all_words', $LANG09[44]);
   241         $searchform->set_var('lang_any_word', $LANG09[45]);
   242         $searchform->set_var('lang_titles', $LANG09[69]);
   243 
   244         $escquery = htmlspecialchars($this->_query);
   245         $escquery = str_replace(array('{', '}'), array('&#123;', '&#125;'),
   246                                 $escquery);
   247         $searchform->set_var ('query', $escquery);
   248         $searchform->set_var ('datestart', $this->_dateStart);
   249         $searchform->set_var ('dateend', $this->_dateEnd);
   250 
   251         $phrase_selected = '';
   252         $all_selected = '';
   253         $any_selected = '';
   254         if ($this->_keyType == 'phrase') {
   255             $phrase_selected = 'selected="selected"';
   256         } else if ($this->_keyType == 'all') {
   257             $all_selected = 'selected="selected"';
   258         } else if ($this->_keyType == 'any') {
   259             $any_selected = 'selected="selected"';
   260         }
   261         $searchform->set_var ('key_phrase_selected', $phrase_selected);
   262         $searchform->set_var ('key_all_selected', $all_selected);
   263         $searchform->set_var ('key_any_selected', $any_selected);
   264 
   265         $options = '';
   266         $plugintypes = array('all' => $LANG09[4], 'stories' => $LANG09[6], 'comments' => $LANG09[7]);
   267         $plugintypes = array_merge($plugintypes, PLG_getSearchTypes());
   268         // Generally I don't like to hardcode HTML but this seems easiest
   269         foreach ($plugintypes as $key => $val) {
   270             $options .= "<option value=\"$key\"";
   271             if ($this->_type == $key)
   272                 $options .= ' selected="selected"';
   273             $options .= ">$val</option>".LB;
   274         }
   275         $searchform->set_var('plugin_types', $options);
   276 
   277         if ($_CONF['contributedbyline'] == 1) {
   278             $searchform->set_var('lang_authors', $LANG09[8]);
   279             $searchusers = array();
   280             $result = DB_query("SELECT DISTINCT uid FROM {$_TABLES['comments']}");
   281             while ($A = DB_fetchArray($result)) {
   282                 $searchusers[$A['uid']] = $A['uid'];
   283             }
   284             $result = DB_query("SELECT DISTINCT uid FROM {$_TABLES['stories']} WHERE (date <= NOW()) AND (draft_flag = 0)");
   285             while ($A = DB_fetchArray($result)) {
   286                 $searchusers[$A['uid']] = $A['uid'];
   287             }
   288 
   289             $inlist = implode(',', $searchusers);
   290 
   291             if (!empty ($inlist)) {
   292                 $sql = "SELECT uid,username,fullname FROM {$_TABLES['users']} WHERE uid IN ($inlist)";
   293                 if (isset ($_CONF['show_fullname']) && ($_CONF['show_fullname'] == 1)) {
   294                     /* Caveat: This will group all users with an emtpy fullname
   295                      *         together, so it's not exactly sorted by their
   296                      *         full name ...
   297                      */
   298                     $sql .= ' ORDER BY fullname,username';
   299                 } else {
   300                     $sql .= ' ORDER BY username';
   301                 }
   302                 $result = DB_query ($sql);
   303                 $options = '';
   304                 while ($A = DB_fetchArray($result)) {
   305                     $options .= '<option value="' . $A['uid'] . '"';
   306                     if ($A['uid'] == $this->_author) {
   307                         $options .= ' selected="selected"';
   308                     }
   309                     $options .= '>' . htmlspecialchars(COM_getDisplayName('', $A['username'], $A['fullname'])) . '</option>';
   310                 }
   311                 $searchform->set_var('author_option_list', $options);
   312                 $searchform->parse('author_form_element', 'authors', true);
   313             } else {
   314                 $searchform->set_var('author_form_element', '<input type="hidden" name="author" value="0"' . XHTML . '>');
   315             }
   316         } else {
   317             $searchform->set_var ('author_form_element',
   318                     '<input type="hidden" name="author" value="0"' . XHTML . '>');
   319         }
   320 
   321         // Results per page
   322         $options = '';
   323         $limits = explode(',', $_CONF['search_limits']);
   324         foreach ($limits as $limit) {
   325             $options .= "<option value=\"$limit\"";
   326             if ($_CONF['num_search_results'] == $limit) {
   327                 $options .= ' selected="selected"';
   328             }
   329             $options .= ">$limit</option>" . LB;
   330         }
   331         $searchform->set_var('search_limits', $options);
   332 
   333         $searchform->set_var('lang_search', $LANG09[10]);
   334         $searchform->parse('output', 'searchform');
   335 
   336         $retval .= $searchform->finish($searchform->get_var('output'));
   337         $retval .= COM_endBlock();
   338 
   339         return $retval;
   340     }
   341 
   342     /**
   343     * Performs search on all stories
   344     *
   345     * @access private
   346     * @return object plugin object
   347     *
   348     */
   349     function _searchStories()
   350     {
   351         global $_TABLES, $_DB_dbms, $LANG09;
   352 
   353         // Make sure the query is SQL safe
   354         $query = trim(addslashes($this->_query));
   355 
   356         $sql = "SELECT s.sid AS id, s.title AS title, s.introtext AS description, ";
   357         $sql .= "UNIX_TIMESTAMP(s.date) AS date, s.uid AS uid, s.hits AS hits, ";
   358         $sql .= "CONCAT('/article.php?story=',s.sid) AS url ";
   359         $sql .= "FROM {$_TABLES['stories']} AS s, {$_TABLES['users']} AS u ";
   360         $sql .= "WHERE (draft_flag = 0) AND (date <= NOW()) AND (u.uid = s.uid) ";
   361         $sql .= COM_getPermSQL('AND') . COM_getTopicSQL('AND') . COM_getLangSQL('sid', 'AND') . ' ';
   362 
   363         if (!empty($this->_topic)) {
   364             $sql .= "AND (s.tid = '$this->_topic') ";
   365         }
   366         if (!empty($this->_author)) {
   367             $sql .= "AND (s.uid = '$this->_author') ";
   368         }
   369 
   370         $search_s = new SearchCriteria('stories', $LANG09[65]);
   371 
   372         $columns = array('title' => 'title', 'introtext', 'bodytext');
   373         $sql .= $search_s->getDateRangeSQL('AND', 'date', $this->_dateStart, $this->_dateEnd);
   374         list($sql, $ftsql) = $search_s->buildSearchSQL($this->_keyType, $query, $columns, $sql);
   375 
   376         $search_s->setSQL($sql);
   377         $search_s->setFTSQL($ftsql);
   378         $search_s->setRank(5);
   379         $search_s->setURLRewrite(true);
   380 
   381         // Search Story Comments
   382         $sql = "SELECT c.cid AS id, c.title AS title, c.comment AS description, ";
   383         $sql .= "UNIX_TIMESTAMP(c.date) AS date, c.uid AS uid, ";
   384 
   385         // MSSQL has a problem when concatenating numeric values
   386         if ($_DB_dbms == 'mssql') {
   387             $sql .= "'/comment.php?mode=view&amp;cid=' + CAST(c.cid AS varchar(10)) AS url ";
   388         } else {
   389             $sql .= "CONCAT('/comment.php?mode=view&amp;cid=',c.cid) AS url ";
   390         }
   391 
   392         $sql .= "FROM {$_TABLES['users']} AS u, {$_TABLES['comments']} AS c ";
   393         $sql .= "LEFT JOIN {$_TABLES['stories']} AS s ON ((s.sid = c.sid) ";
   394         $sql .= COM_getPermSQL('AND',0,2,'s') . COM_getTopicSQL('AND',0,'s') . COM_getLangSQL('sid','AND','s') . ") ";
   395         $sql .= "WHERE (u.uid = c.uid) AND (s.draft_flag = 0) AND (s.commentcode >= 0) AND (s.date <= NOW()) ";
   396 
   397         if (!empty($this->_topic)) {
   398             $sql .= "AND (s.tid = '$this->_topic') ";
   399         }
   400         if (!empty($this->_author)) {
   401             $sql .= "AND (c.uid = '$this->_author') ";
   402         }
   403 
   404         $search_c = new SearchCriteria('comments', array($LANG09[65],$LANG09[66]));
   405 
   406         $columns = array('title' => 'c.title', 'comment');
   407         $sql .= $search_c->getDateRangeSQL('AND', 'c.date', $this->_dateStart, $this->_dateEnd);
   408         list($sql, $ftsql) = $search_c->buildSearchSQL($this->_keyType, $query, $columns, $sql);
   409 
   410         $search_c->setSQL($sql);
   411         $search_c->setFTSQL($ftsql);
   412         $search_c->setRank(2);
   413 
   414         return array($search_s, $search_c);
   415     }
   416 
   417     /**
   418     * Kicks off the appropriate search(es)
   419     *
   420     * Initiates the search engine and returns HTML formatted
   421     * results. It also provides support to plugins using a
   422     * search API. Backwards compatibility has been incorporated
   423     * in this function to allow legacy support to plugins using
   424     * the old API calls defined versions prior to Geeklog 1.5.1
   425     *
   426     * @access public
   427     * @return string HTML output for search results
   428     *
   429     */
   430     function doSearch()
   431     {
   432         global $_CONF, $LANG01, $LANG09, $LANG31;
   433 
   434         // Verify current user can perform requested search
   435         if (!$this->_isSearchAllowed())
   436         {
   437             return $this->_getAccessDeniedMessage();
   438         }
   439 
   440         // Make sure there is a query string
   441         // Full text searches have a minimum word length of 3 by default
   442         if ((empty($this->_query) && empty($this->_author) && empty($this->_topic)) || ($_CONF['search_use_fulltext'] && strlen($this->_query) < 3))
   443         {
   444             $retval = '<p>' . $LANG09[41] . '</p>' . LB;
   445             $retval .= $this->showForm();
   446 
   447             return $retval;
   448         }
   449 
   450         // Build the URL strings
   451         $this->_searchURL = $_CONF['site_url'] . '/search.php?query=' . urlencode($this->_query) .
   452             ((!empty($this->_keyType))    ? '&amp;keyType=' . $this->_keyType : '' ) .
   453             ((!empty($this->_dateStart))  ? '&amp;datestart=' . $this->_dateStart : '' ) .
   454             ((!empty($this->_dateEnd))    ? '&amp;dateend=' . $this->_dateEnd : '' ) .
   455             ((!empty($this->_topic))      ? '&amp;topic=' . $this->_topic : '' ) .
   456             ((!empty($this->_author))     ? '&amp;author=' . $this->_author : '' );
   457 
   458         $url = "{$this->_searchURL}&amp;type={$this->_type}&amp;mode=";
   459         $obj = new ListFactory($url.'search', $_CONF['search_limits'], $_CONF['num_search_results']);
   460         $obj->setRowFunction(array($this, 'searchFormatCallback'));
   461         $obj->setField('ID', 'id', false);
   462         $obj->setField('URL', 'url', false);
   463 
   464         $show_num  = $_CONF['search_show_num'];
   465         $show_type = $_CONF['search_show_type'];
   466         $show_user = $_CONF['contributedbyline'];
   467         $show_hits = !$_CONF['hideviewscount'];
   468         $style = isset($_CONF['search_style']) ? $_CONF['search_style'] : 'google';
   469 
   470         if ($style == 'table')
   471         {
   472             $obj->setStyle('table');
   473             //             Title        Name           Display     Sort   Format
   474             $obj->setField($LANG09[62], ROW_NUMBER,    $show_num,  false, '<b>%d.</b>');
   475             $obj->setField($LANG09[5],  SQL_TITLE,     $show_type, true,  '<b>%s</b>');
   476             $obj->setField($LANG09[16], 'title',       true,       true);
   477             $obj->setField($LANG09[63], 'description', true,       false);
   478             $obj->setField($LANG09[17], 'date',        true,       true);
   479             $obj->setField($LANG09[18], 'uid',         $show_user, true);
   480             $obj->setField($LANG09[50], 'hits',        $show_hits, true);
   481             $this->_wordlength = 7;
   482         }
   483         else if ($style == 'google')
   484         {
   485             $obj->setStyle('inline');
   486             $obj->setField('',          ROW_NUMBER,    $show_num,  false, '<b>%d.</b>');
   487             $obj->setField($LANG09[16], 'title',       true,       true,  '%s<br>');
   488             $obj->setField('',          'description', true,       false, '%s<br>');
   489             $obj->setField('',          '_html',       true,       false, '<span style="color:green;">');
   490             $obj->setField($LANG09[18], 'uid',         $show_user, true,  $LANG01[104].' %s ');
   491             $obj->setField($LANG09[17], 'date',        true,       true,  $LANG01[36].' %s');
   492             $obj->setField($LANG09[5],  SQL_TITLE,     $show_type, true,  ' - %s');
   493             $obj->setField($LANG09[50], 'hits',        $show_hits, true,  ' - %s '.$LANG09[50]);
   494             $obj->setField('',          '_html',       true,       false, '</span>');
   495             $this->_wordlength = 50;
   496         }
   497         $obj->setDefaultSort('hits');
   498 
   499         // Start search timer
   500         $searchtimer = new timerobject();
   501         $searchtimer->setPrecision(4);
   502         $searchtimer->startTimer();
   503 
   504         // Have plugins do their searches
   505         $page = isset($_GET['page']) ? COM_applyFilter($_GET['page'], true) : 1;
   506         $result_plugins = PLG_doSearch($this->_query, $this->_dateStart, $this->_dateEnd, $this->_topic, $this->_type, $this->_author, $this->_keyType, $page, 5);
   507 
   508         // Add core searches
   509         $result_plugins = array_merge($result_plugins, $this->_searchStories());
   510 
   511         // Loop through all plugins separating the new API from the old
   512         $new_api = 0;
   513         $old_api = 0;
   514         $num_results = 0;
   515 
   516         foreach ($result_plugins as $result)
   517         {
   518             if (is_a($result, 'SearchCriteria'))
   519             {
   520                 $debug_info = $result->getName() . " using APIv2";
   521 
   522                 if ($this->_type != 'all' && $this->_type != $result->getName())
   523                 {
   524                     if ($this->_verbose) {
   525                         $new_api++;
   526                         COM_errorLog("$debug_info. Skipped as type is not " . $this->_type);
   527                     }
   528                     continue;
   529                 }
   530 
   531                 $api_results = $result->getResults();
   532                 if (!empty($api_results)) {
   533                     $obj->addResultArray($api_results);
   534                 }
   535 
   536                 $api_callback_func = $result->getCallback();
   537                 if (!empty($api_callback_func))
   538                 {
   539                     $debug_info .= " with Callback Function " . $api_callback_func;
   540                     $obj->setCallback($result->getLabel(), $result->getName(), $api_callback_func, $result->getRank(), $result->getTotal());
   541                 }
   542                 else
   543                 {
   544                     if ($_CONF['search_use_fulltext'] == true && $result->getFTSQL() != '')
   545                     {
   546                         $debug_info .= " with SQL FULLTEXT. ";
   547                         $sql = $result->getFTSQL();
   548                     }
   549                     else
   550                     {
   551                         $debug_info .= " with SQL LIKE. ";
   552                         $sql = $result->getSQL();
   553                     }
   554 
   555                     $sql = $this->_convertsql($sql);
   556                     $debug_info .= "SQL = " . print_r($sql,1);
   557                     $obj->setQuery($result->getLabel(), $result->getName(), $sql, $result->getRank());
   558                 }
   559 
   560                 $this->_url_rewrite[ $result->getName() ] = $result->UrlRewriteEnable();
   561                 $this->_append_query[ $result->getName() ] = $result->AppendQueryEnable();
   562 
   563                 if ($this->_verbose) {
   564                     $new_api++;
   565                     COM_errorLog($debug_info);
   566                 }
   567             }
   568             else if (is_a($result, 'Plugin') && $result->num_searchresults != 0)
   569             {
   570                 // Some backwards compatibility
   571                 if ($this->_verbose) {
   572                     $old_api++;
   573                     $debug_info = $result->plugin_name . " using APIv1 with backwards compatibility.";
   574                     $debug_info .= " Count: " . $result->num_searchresults;
   575                     $debug_info .= " Headings: " . implode(",", $result->searchheading);
   576                     COM_errorLog($debug_info);
   577                 }
   578 
   579                 // Find the column heading names that closely match what we are looking for
   580                 // There may be issues here on different languages, but this _should_ capture most of the data
   581                 $col_title = $this->_findColumn($result->searchheading, array($LANG09[16],$LANG31[4],'Question', 'Site Page'));//Title,Subject
   582                 $col_desc = $this->_findColumn($result->searchheading, array($LANG09[63],'Answer'));
   583                 $col_date = $this->_findColumn($result->searchheading, array($LANG09[17]));//'Date','Date Added','Last Updated','Date & Time'
   584                 $col_user = $this->_findColumn($result->searchheading, array($LANG09[18],'Submited by'));
   585                 $col_hits = $this->_findColumn($result->searchheading, array($LANG09[50],$LANG09[23],'Downloads','Clicks'));//'Hits','Views'
   586 
   587                 $label = str_replace($LANG09[59], '', $result->searchlabel);
   588                 $num_results += $result->num_itemssearched;
   589 
   590                 // Extract the results
   591                 for ($i = 0; $i < 5; $i++)
   592                 {
   593                     // If the plugin does not repect the $perpage perameter force it here.
   594                     $j = ($i + ($page * 5)) - 5;
   595                     if ($j >= count($result->searchresults))
   596                         break;
   597 
   598                     $old_row = $result->searchresults[$j];
   599                     if ($col_date != -1)
   600                     {
   601                         // Convert the date back to a timestamp
   602                         $date = $old_row[ $col_date ];
   603                         $date = substr($date, 0, strpos($date, '@'));
   604                         if ($date == '')
   605                         {
   606                             $date = $old_row[$col_date];
   607                         }
   608                         else
   609                         {
   610                             $date = strtotime($date);
   611                         }
   612                     }
   613 
   614                     $api_results = array(
   615                                 SQL_NAME =>       $result->plugin_name,
   616                                 SQL_TITLE =>      $label,
   617                                 'title' =>        $col_title == -1 ? '<i>' . $LANG09[70] . '</i>' : $old_row[$col_title],
   618                                 'description' =>  $col_desc == -1 ? '<i>' . $LANG09[70] . '</i>' : $old_row[$col_desc],
   619                                 'date' =>         $col_date == -1 ? 'LF_NULL' : $date,
   620                                 'uid' =>          $col_user == -1 ? 'LF_NULL' : $old_row[$col_user],
   621                                 'hits' =>         $col_hits == -1 ? 'LF_NULL' : str_replace(',', '', $old_row[$col_hits])
   622                             );
   623                     preg_match('/href="([^"]+)"/i', $api_results['title'], $links);
   624                     $api_results['url'] = empty($links) ? '#' : $links[1];
   625 
   626                     $obj->addResult($api_results);
   627                 }
   628             }
   629         }
   630 
   631         // Find out how many plugins are on the old/new system
   632         if ($this->_verbose) {
   633             COM_errorLog("Search Plugins using APIv1: $old_api APIv2: $new_api");
   634         }
   635 
   636         // Execute the queries
   637         $results = $obj->ExecuteQueries();
   638 
   639         // Searches are done, stop timer
   640         $searchtime = $searchtimer->stopTimer();
   641 
   642         $escquery = htmlspecialchars($this->_query);
   643         $escquery = str_replace(array('{', '}'), array('&#123;', '&#125;'),
   644                                 $escquery);
   645         if ($this->_keyType == 'any')
   646         {
   647             $searchQuery = str_replace(' ', "</b>' " . $LANG09[57] . " '<b>", $escquery);
   648             $searchQuery = "'<b>$searchQuery</b>'";
   649         }
   650         else if ($this->_keyType == 'all')
   651         {
   652             $searchQuery = str_replace(' ', "</b>' " . $LANG09[56] . " '<b>", $escquery);
   653             $searchQuery = "'<b>$searchQuery</b>'";
   654         }
   655         else
   656         {
   657             $searchQuery = $LANG09[55] . " '<b>$escquery</b>'";
   658         }
   659 
   660         // Clean the query string so that sprintf works as expected
   661         $searchQuery = str_replace("%", "%%", $searchQuery);
   662 
   663         $retval = "{$LANG09[25]} $searchQuery. ";
   664         if (count($results) == 0)
   665         {
   666             $retval .= sprintf($LANG09[24], 0);
   667             $retval = '<p>' . $retval . '</p>' . LB;
   668             $retval .= '<p>' . $LANG09[13] . '</p>' . LB;
   669             $retval .= $this->showForm();
   670         }
   671         else
   672         {
   673             $retval .= $LANG09[64] . " ($searchtime {$LANG09[27]}). ";
   674             $retval .= str_replace("%", "%%", COM_createLink($LANG09[61], $url.'refine'));
   675             $retval = '<p>' . $retval . '</p>' . LB;
   676             $retval = $obj->getFormattedOutput($results, $LANG09[11], $retval, '', $_CONF['search_show_sort'], $_CONF['search_show_limit']);
   677         }
   678 
   679         return $retval;
   680     }
   681 
   682     /**
   683     * Callback function for the ListFactory class
   684     *
   685     * This function gets called by the ListFactory class and formats
   686     * each row accordingly for example pulling usernames from the
   687     * users table and displaying a link to their profile.
   688     *
   689     * @access public
   690     * @param array $row An array of plain data to format
   691     * @return array A reformatted version of the input array
   692     *
   693     */
   694     function searchFormatCallback( $preSort, $row )
   695     {
   696         global $_CONF, $LANG09;
   697 
   698         if ($preSort)
   699         {
   700             $row[SQL_TITLE] = is_array($row[SQL_TITLE]) ? implode($_CONF['search_separator'],$row[SQL_TITLE]) : $row[SQL_TITLE];
   701 
   702             if (is_numeric($row['uid']))
   703             {
   704                 if (empty($this->_names[ $row['uid'] ]))
   705                 {
   706                     $this->_names[ $row['uid'] ] = htmlspecialchars(COM_getDisplayName( $row['uid'] ));
   707                     if ($row['uid'] != 1)
   708                     {
   709                         $this->_names[$row['uid']] = COM_createLink($this->_names[ $row['uid'] ],
   710                                     $_CONF['site_url'] . '/users.php?mode=profile&amp;uid=' . $row['uid']);
   711                     }
   712                 }
   713                 $row['uid'] = $this->_names[ $row['uid'] ];
   714             }
   715         }
   716         else
   717         {
   718             $row[SQL_TITLE] = COM_createLink($row[SQL_TITLE], $this->_searchURL.'&amp;type='.$row[SQL_NAME].'&amp;mode=search');
   719 
   720             if ($row['url'] != '#')
   721             {
   722                 $row['url'] = ($row['url'][0] == '/' ? $_CONF['site_url'] : '') . $row['url'];
   723                 if (isset($this->_url_rewrite[$row[SQL_NAME]]) &&
   724                         $this->_url_rewrite[$row[SQL_NAME]]) {
   725                     $row['url'] = COM_buildUrl($row['url']);
   726                 }
   727                 if (isset($this->_append_query[$row[SQL_NAME]]) &&
   728                         $this->_append_query[$row[SQL_NAME]]) {
   729                     $row['url'] .= (strpos($row['url'],'?') ? '&amp;' : '?') . 'query=' . urlencode($this->_query);
   730                 }
   731             }
   732 
   733             $row['title'] = $this->_shortenText($this->_query, $row['title'], 8);
   734             $row['title'] = stripslashes(str_replace('$', '&#36;', $row['title']));
   735             $row['title'] = COM_createLink($row['title'], $row['url']);
   736 
   737             if ($row['description'] == 'LF_NULL') {
   738                 $row['description'] = '<i>' . $LANG09[70] . '</i>';
   739             } elseif ($row['description'] != '<i>' . $LANG09[70] . '</i>') {
   740                 $row['description'] = stripslashes($this->_shortenText($this->_query, PLG_replaceTags($row['description']), $this->_wordlength));
   741             }
   742 
   743             if ($row['date'] != 'LF_NULL') {
   744                 $dt = COM_getUserDateTimeFormat(intval($row['date']));
   745                 $row['date'] = $dt[0];
   746             }
   747 
   748             if ($row['hits'] != 'LF_NULL') {
   749                 $row['hits'] = COM_NumberFormat($row['hits']) . ' '; // simple solution to a silly problem!
   750             }
   751         }
   752 
   753         return $row;
   754     }
   755 
   756     /**
   757     * Shortens a long text string to only a few words
   758     *
   759     * Returns a shorter version of the in putted text centred
   760     * around the keyword. The keyword is highlighted in bold.
   761     * Adds '...' to the beginning or the end of the shortened
   762     * version depending where the text was cut. Works on a
   763     * word basis, so long words wont get cut.
   764     *
   765     * @access private
   766     * @param string $keyword The word to centre around
   767     * @param string $text The complete text string
   768     * @param int $num_words The number of words to display, best to use an odd number
   769     * @return string A short version of the text
   770     *
   771     */
   772     function _shortenText($keyword, $text, $num_words = 7)
   773     {
   774         $text = COM_getTextContent($text);
   775         $words = explode(' ', $text);
   776         $word_count = count($words);
   777         if ($word_count <= $num_words) {
   778             return COM_highlightQuery($text, $keyword, 'b');
   779         }
   780 
   781         $rt = '';
   782         $pos = $this->_stripos($text, $keyword);
   783         if ($pos !== false)
   784         {
   785             $pos_space = strpos($text, ' ', $pos);
   786             if (empty($pos_space))
   787             {
   788                 // Keyword at the end of text
   789                 $key = $word_count - 1;
   790                 $start = 0 - $num_words;
   791                 $end = 0;
   792                 $rt = '<b>...</b> ';
   793             }
   794             else
   795             {
   796                 $str = substr($text, $pos, $pos_space - $pos);
   797                 $m = (int) (($num_words - 1) / 2);
   798                 $key = $this->_arraySearch($keyword, $words);
   799                 if ($key === false) {
   800                     // Keyword(s) not found - show start of text
   801                     $key = 0;
   802                     $start = 0;
   803                     $end = $num_words - 1;
   804                 } elseif ($key <= $m) {
   805                     // Keyword at the start of text
   806                     $start = 0 - $key;
   807                     $end = $num_words - 1;
   808                     $end = ($key + $m <= $word_count - 1)
   809                          ? $key : $word_count - $m - 1;
   810                     $abs_length = abs($start) + abs($end) + 1;
   811                     if ($abs_length < $num_words) {
   812                         $end += ($num_words - $abs_length);
   813                     }
   814                 } else {
   815                     // Keyword in the middle of text
   816                     $start = 0 - $m;
   817                     $end = ($key + $m <= $word_count - 1)
   818                          ? $m : $word_count - $key - 1;
   819                     $abs_length = abs($start) + abs($end) + 1;
   820                     if ($abs_length < $num_words) {
   821                         $start -= ($num_words - $abs_length);
   822                     }
   823                     $rt = '<b>...</b> ';
   824                 }
   825             }
   826         }
   827         else
   828         {
   829             $key = 0;
   830             $start = 0;
   831             $end = $num_words - 1;
   832         }
   833 
   834         for ($i = $start; $i <= $end; $i++) {
   835             $rt .= $words[$key + $i] . ' ';
   836         }
   837         if ($key + $i != $word_count) {
   838             $rt .= ' <b>...</b>';
   839         }
   840 
   841         return COM_highlightQuery($rt, $keyword, 'b');
   842     }
   843 
   844     /**
   845     * Search array of words for keyword(s)
   846     *
   847     * @param   string  $needle    keyword(s), separated by spaces
   848     * @param   array   $haystack  array of words to search through
   849     * @return  mixed              index in $haystack or false when not found
   850     * @access  private
   851     *
   852     */
   853     function _arraySearch($needle, $haystack)
   854     {
   855         $keywords = explode(' ', $needle);
   856         $num_keywords = count($keywords);
   857 
   858         foreach ($haystack as $key => $value) {
   859             if ($this->_stripos($value, $keywords[0]) !== false) {
   860                 if ($num_keywords == 1) {
   861                     return $key;
   862                 } else {
   863                     $matched_all = true;
   864                     for ($i = 1; $i < $num_keywords; $i++) {
   865                         if ($this->_stripos($haystack[$key + $i], $keywords[$i]) === false) {
   866                             $matched_all = false;
   867                             break;
   868                         }
   869                     }
   870                     if ($matched_all) {
   871                         return $key;
   872                     }
   873                 }
   874             }
   875         }
   876 
   877         return false;
   878     }
   879 
   880     /**
   881     * Finds the similarities between heading names
   882     *
   883     * Returns the index of a heading that matches a
   884     * number of similar heading names. Used for backwards
   885     * compatibility in the doSearch() function.
   886     *
   887     * @access private
   888     * @param array $headings All the headings
   889     * @param array $find An array of alternative headings to find
   890     * @return int The index of the alternative heading
   891     *
   892     */
   893     function _findColumn( $headings, $find )
   894     {
   895         // We can't use normal for loops here as some of the
   896         // heading indexes start from 1, so foreach works better
   897         foreach ($find as $fh)
   898         {
   899             $j = 0;
   900             foreach ($headings as $h)
   901             {
   902                 if (preg_match("/$fh/i", $h) > 0) {
   903                     return $j;
   904                 }
   905                 $j++;
   906             }
   907         }
   908         return -1;
   909     }
   910 
   911     /**
   912     * Converts the MySQL CONCAT function to the MSSQL equivalent
   913     *
   914     * @access private
   915     * @param string $sql The SQL to convert
   916     * @return string MSSQL friendly SQL
   917     *
   918     */
   919     function _convertsql( $sql )
   920     {
   921         global $_DB_dbms;
   922         if ($_DB_dbms == 'mssql')
   923         {
   924             if (is_string( $sql ))
   925             {
   926                 $sql = preg_replace("/CONCAT\(([^\)]+)\)/ie", "preg_replace('/,?(\'[^\']+\'|[^,]+),/i', '\\\\1 + ', '\\1')", $sql);
   927             }
   928             else if (is_array( $sql ))
   929             {
   930                 $sql['mssql'] = preg_replace("/CONCAT\(([^\)]+)\)/ie", "preg_replace('/,?(\'[^\']+\'|[^,]+),/i', '\\\\1 + ', '\\1')", $sql['mssql']);
   931             }
   932         }
   933         return $sql;
   934     }
   935 
   936     /**
   937     * Helper function: Simulate stripos on PHP 4
   938     *
   939     * @param   string  $haystack  string to search in
   940     * @param   string  $needle    string to search for
   941     * @return  mixed              first pos of $needle in $haystack, or false
   942     *
   943     */
   944     function _stripos($haystack, $needle)
   945     {
   946         if (function_exists('stripos')) {
   947             return stripos($haystack, $needle);
   948         } else {
   949             return strpos(strtolower($haystack), strtolower($needle));
   950         }
   951     }
   952 }
   953 
   954 ?>