[ Index ] |
|
Code source de GeekLog 1.4.1 |
1 <?php 2 3 /* Reminder: always indent with 4 spaces (no tabs). */ 4 // +---------------------------------------------------------------------------+ 5 // | Geeklog 1.4 | 6 // +---------------------------------------------------------------------------+ 7 // | search.php | 8 // | | 9 // | Geeklog search class. | 10 // +---------------------------------------------------------------------------+ 11 // | Copyright (C) 2000-2006 by the following authors: | 12 // | | 13 // | Authors: Tony Bibbs - tony AT geeklog DOT net | 14 // | Dirk Haun - dirk AT haun-online DOT de | 15 // +---------------------------------------------------------------------------+ 16 // | | 17 // | This program is free software; you can redistribute it and/or | 18 // | modify it under the terms of the GNU General Public License | 19 // | as published by the Free Software Foundation; either version 2 | 20 // | of the License, or (at your option) any later version. | 21 // | | 22 // | This program is distributed in the hope that it will be useful, | 23 // | but WITHOUT ANY WARRANTY; without even the implied warranty of | 24 // | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 25 // | GNU General Public License for more details. | 26 // | | 27 // | You should have received a copy of the GNU General Public License | 28 // | along with this program; if not, write to the Free Software Foundation, | 29 // | Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. | 30 // | | 31 // +---------------------------------------------------------------------------+ 32 // 33 // $Id: search.class.php,v 1.57 2006/10/07 08:38:24 dhaun Exp $ 34 35 if (strpos ($_SERVER['PHP_SELF'], 'search.class.php') !== false) { 36 die ('This file can not be used on its own.'); 37 } 38 39 require_once ($_CONF['path_system'] . 'classes/plugin.class.php'); 40 41 /** 42 * Geeklog Search Class 43 * 44 * @author Tony Bibbs <tony AT geeklog DOT net> 45 * @package net.geeklog.search 46 * 47 */ 48 class Search { 49 /** 50 * @access private 51 * @var string 52 */ 53 var $_query = ''; 54 55 /** 56 * @access private 57 * @var string 58 */ 59 var $_topic = ''; 60 61 /** 62 * @access private 63 * @var string 64 */ 65 var $_dateStart = null; 66 67 /** 68 * @access private 69 * @var string 70 */ 71 var $_dateEnd = null; 72 73 /** 74 * @access private 75 * @var integer 76 */ 77 var $_author = null; 78 79 /** 80 * @access private 81 * @var string 82 */ 83 var $_type = ''; 84 85 /** 86 * @access private 87 * @var string 88 */ 89 var $_keyType = ''; 90 91 /** 92 * @access private 93 * @var integer 94 */ 95 var $_page = 0; 96 97 /** 98 * @access private 99 * @var integer 100 */ 101 var $_per_page = 10; 102 103 /** 104 * Constructor 105 * 106 * Sets up private search variables 107 * 108 * @author Tony Bibbs <tony AT geeklog DOT net> 109 * @access public 110 * 111 */ 112 function Search() 113 { 114 global $_CONF; 115 116 // Set search criteria 117 if (isset ($_REQUEST['query'])) { 118 $this->_query = strip_tags (COM_stripslashes ($_REQUEST['query'])); 119 } 120 if (isset ($_REQUEST['topic'])) { 121 $this->_topic = COM_applyFilter ($_REQUEST['topic']); 122 } 123 if (isset ($_REQUEST['datestart'])) { 124 $this->_dateStart = COM_applyFilter ($_REQUEST['datestart']); 125 } 126 if (isset ($_REQUEST['dateend'])) { 127 $this->_dateEnd = COM_applyFilter ($_REQUEST['dateend']); 128 } 129 if (isset ($_REQUEST['author'])) { 130 $this->_author = COM_applyFilter ($_REQUEST['author']); 131 } 132 if (isset ($_REQUEST['type'])) { 133 $this->_type = COM_applyFilter ($_REQUEST['type']); 134 } 135 if (empty ($this->_type)) { 136 $this->_type = 'all'; 137 } 138 if (isset ($_REQUEST['keyType'])) { 139 $this->_keyType = COM_applyFilter ($_REQUEST['keyType']); 140 } 141 if (isset ($_REQUEST['results'])) { 142 $this->_per_page = COM_applyFilter ($_REQUEST['results'], true); 143 } 144 if ($this->_per_page < 1) { 145 if (isset ($_CONF['num_search_results']) && 146 ($_CONF['num_search_results'] > 0)) { 147 $this->_per_page = $_CONF['num_search_results']; 148 } else { 149 $this->_per_page = 10; 150 } 151 } 152 if (isset ($_REQUEST['page'])) { 153 $this->_page = COM_applyFilter ($_REQUEST['page'], true); 154 } 155 if ($this->_page < 1) { 156 $this->_page = 1; 157 } 158 159 // In case we got a username instead of uid, convert it. This should 160 // make custom themes for search page easier. 161 $this->_convertAuthor(); 162 } 163 164 /** 165 * Converts a username to a uid 166 * 167 * @author Tony Bibbs <tony AT geeklog DOT net 168 * @access private 169 * 170 */ 171 function _convertAuthor() 172 { 173 global $_TABLES; 174 175 if (is_numeric ($this->_author) && 176 preg_match ('/^([0-9]+)$/', $this->_author)) { 177 return; 178 } 179 180 if (!empty ($this->_author)) { 181 $this->_author = DB_getItem ($_TABLES['users'], 'uid', 182 "username='" . addslashes ($this->_author) . "'"); 183 } 184 } 185 186 /** 187 * Return the user's username or full name for display, depending 188 * on the $_CONF['show_fullname'] config.php setting 189 * 190 * @author Dirk Haun <dirk AT haun-online DOT de 191 * @access private 192 * 193 */ 194 function _displayName ($username, $fullname) 195 { 196 return COM_getDisplayName('', $username, $fullname); 197 } 198 199 /** 200 * Performs search on all stories 201 * 202 * @author Tony Bibbs <tony AT geeklog DOT net> 203 * @access private 204 * @return object plugin object 205 * 206 */ 207 function _searchStories() 208 { 209 global $_CONF, $_TABLES, $_USER, $_GROUPS, $LANG09; 210 211 $urlQuery = urlencode ($this->_query); 212 213 if ($this->_type == 'all' OR $this->_type == 'stories') { 214 215 $select = "SELECT u.username,u.fullname,s.uid,sid,title,introtext,bodytext,hits,UNIX_TIMESTAMP(date) AS day,'story' AS type"; 216 $sql = " FROM {$_TABLES['stories']} AS s,{$_TABLES['users']} AS u WHERE (draft_flag = 0) AND (date <= NOW()) AND (u.uid = s.uid) "; 217 218 if (!empty ($this->_query)) { 219 if($this->_keyType == 'phrase') { 220 // do an exact phrase search (default) 221 $mywords[] = $this->_query; 222 $mysearchterm = addslashes ($this->_query); 223 $sql .= "AND (introtext LIKE '%$mysearchterm%' "; 224 $sql .= "OR bodytext LIKE '%$mysearchterm%' "; 225 $sql .= "OR title LIKE '%$mysearchterm%') "; 226 } elseif($this->_keyType == 'all') { 227 // must contain ALL of the keywords 228 $mywords = explode(' ', $this->_query); 229 $sql .= 'AND '; 230 $tmp = ''; 231 foreach ($mywords AS $mysearchterm) { 232 $mysearchterm = addslashes (trim ($mysearchterm)); 233 $tmp .= "(introtext LIKE '%$mysearchterm%' OR "; 234 $tmp .= "bodytext LIKE '%$mysearchterm%' OR "; 235 $tmp .= "title LIKE '%$mysearchterm%') AND "; 236 } 237 $tmp = substr($tmp, 0, strlen($tmp) - 4); 238 $sql .= $tmp; 239 } 240 elseif($this->_keyType == 'any') { 241 // must contain ANY of the keywords 242 $mywords = explode(' ', $this->_query); 243 $sql .= 'AND '; 244 $tmp = ''; 245 foreach ($mywords AS $mysearchterm) { 246 $mysearchterm = addslashes (trim ($mysearchterm)); 247 $tmp .= "(introtext LIKE '%$mysearchterm%' OR "; 248 $tmp .= "bodytext LIKE '%$mysearchterm%' OR "; 249 $tmp .= "title LIKE '%$mysearchterm%') OR "; 250 } 251 $tmp = substr($tmp, 0, strlen($tmp) - 3); 252 $sql .= "($tmp)"; 253 } else { 254 $mywords[] = $this->_query; 255 $mysearchterm = addslashes ($this->_query); 256 $sql .= "AND (introtext LIKE '%$mysearchterm%' "; 257 $sql .= "OR bodytext LIKE '%$mysearchterm%' "; 258 $sql .= "OR title LIKE '%$mysearchterm%') "; 259 } 260 } 261 if (!empty($this->_dateStart) AND !empty($this->_dateEnd)) { 262 $delim = substr($this->_dateStart, 4, 1); 263 if (!empty($delim)) { 264 $DS = explode($delim, $this->_dateStart); 265 $DE = explode($delim, $this->_dateEnd); 266 $startdate = mktime(0,0,0,$DS[1],$DS[2],$DS[0]); 267 $enddate = mktime(23,59,59,$DE[1],$DE[2],$DE[0]); 268 $sql .= "AND (UNIX_TIMESTAMP(date) BETWEEN '$startdate' AND '$enddate') "; 269 } 270 } 271 if (!empty($this->_topic)) { 272 $sql .= "AND (tid = '$this->_topic') "; 273 } 274 if (!empty($this->_author) && ($this->_author > 0)) { 275 $sql .= "AND (s.uid = '$this->_author') "; 276 } 277 $sql .= COM_getPermSQL ('AND') . COM_getTopicSQL ('AND'); 278 $sql .= COM_getLangSQL ('sid', 'AND'); 279 $sql .= ' GROUP BY date, username, fullname, u.uid, s.uid, sid, title, hits , s.introtext, s.bodytext ORDER BY date DESC '; 280 $l = ($this->_per_page * $this->_page) - $this->_per_page; 281 $sql .= 'LIMIT ' . $l . ',' . $this->_per_page; 282 283 $result_stories = DB_query ($select . $sql); 284 $result_count = DB_query ('SELECT COUNT(*)' . $sql); 285 $B = DB_fetchArray ($result_count, true); 286 287 $story_results = new Plugin(); 288 $story_results->searchlabel = $LANG09[53]; 289 $story_results->addSearchHeading($LANG09[16]); 290 $story_results->addSearchHeading($LANG09[17]); 291 if ($_CONF['contributedbyline'] == 1) { 292 $story_results->addSearchHeading($LANG09[18]); 293 } 294 if ($_CONF['hideviewscount'] == 0) { 295 $story_results->addSearchHeading($LANG09[23]); 296 } 297 $story_results->num_searchresults = 0; 298 $story_results->num_itemssearched = $B[0]; 299 $story_results->supports_paging = true; 300 301 // NOTE if any of your data items need to be links then add them 302 // here! Make sure data elements are in an array and in the same 303 // order as your headings above! 304 while ($A = DB_fetchArray($result_stories)) { 305 // get rows 306 $A['title'] = str_replace ('$', '$', $A['title']); 307 $thetime = COM_getUserDateTimeFormat ($A['day']); 308 if (empty ($urlQuery)) { 309 $articleUrl = COM_buildUrl ($_CONF['site_url'] 310 . '/article.php?story=' . $A['sid']); 311 } else { 312 $articleUrl = $_CONF['site_url'] . '/article.php?story=' 313 . $A['sid'] . '&query=' . urlencode ($urlQuery); 314 } 315 $author = $this->_displayName ($A['username'], $A['fullname']); 316 if ($A['uid'] == 1) { 317 $profile = $author; 318 } else { 319 $profile = '<a href="' . $_CONF['site_url'] 320 . '/users.php?mode=profile&uid=' . $A['uid'] 321 . '">' . $author . '</a>'; 322 } 323 $row = array ('<a href="' . $articleUrl . '">' 324 . stripslashes ($A['title']) . '</a>', 325 $thetime[0]); 326 if ($_CONF['contributedbyline'] == 1) { 327 $row[] = $profile; 328 } 329 if ($_CONF['hideviewscount'] == 0) { 330 $row[] = COM_NumberFormat ($A['hits']); 331 } 332 $story_results->addSearchResult ($row); 333 $story_results->num_searchresults++; 334 } 335 } else { 336 $story_results = new Plugin(); 337 $story_results->searchlabel = $LANG09[53]; 338 $story_results->num_itemssearched = 0; 339 } 340 341 return $story_results; 342 } 343 344 /** 345 * Performs search on all comments 346 * 347 * @author Tony Bibbs <tony AT geeklog DOT net> 348 * @access private 349 * @return object plugin object 350 * 351 */ 352 function _searchComments() 353 { 354 global $_CONF, $_TABLES, $_USER, $_GROUPS, $LANG09; 355 356 if ($this->_type == 'all' OR $this->_type == 'comments') { 357 358 $stsql = COM_getPermSQL ('AND', 0, 2, $_TABLES['stories']); 359 $stsql .= COM_getTopicSQL ('AND'); 360 $stsql .= COM_getLangSQL ('sid', 'AND', $_TABLES['stories']); 361 362 $stwhere = ''; 363 364 if (empty ($_USER['uid']) || ($_USER['uid'] == 1)) { 365 $stwhere .= "({$_TABLES['stories']}.perm_anon IS NOT NULL)"; 366 } else { 367 $stwhere .= "({$_TABLES['stories']}.owner_id IS NOT NULL AND {$_TABLES['stories']}.perm_owner IS NOT NULL) OR "; 368 $stwhere .= "({$_TABLES['stories']}.group_id IS NOT NULL AND {$_TABLES['stories']}.perm_group IS NOT NULL) OR "; 369 $stwhere .= "({$_TABLES['stories']}.perm_members IS NOT NULL)"; 370 } 371 372 $mysearchterm = addslashes ($this->_query); 373 $select = "SELECT {$_TABLES['users']}.username,{$_TABLES['users']}.fullname,{$_TABLES['stories']}.sid,{$_TABLES['comments']}.title,comment,pid,cid,{$_TABLES['comments']}.uid,{$_TABLES['comments']}.sid AS qid,type as comment_type,UNIX_TIMESTAMP({$_TABLES['comments']}.date) as day,'comment' as type"; 374 $sql = " FROM {$_TABLES['users']},{$_TABLES['comments']} "; 375 $sql .= "LEFT JOIN {$_TABLES['stories']} ON (({$_TABLES['stories']}.sid = {$_TABLES['comments']}.sid)" . $stsql . ") "; 376 $sql .= "WHERE "; 377 $sql .= " {$_TABLES['users']}.uid = {$_TABLES['comments']}.uid AND "; 378 $sql .= "({$_TABLES['stories']}.draft_flag = 0) AND ({$_TABLES['stories']}.commentcode = 0) AND ({$_TABLES['stories']}.date <= NOW()) "; 379 380 if (!empty ($this->_query)) { 381 if($this->_keyType == 'phrase') { 382 // do an exact phrase search (default) 383 $mywords[] = $this->_query; 384 $mysearchterm = addslashes ($this->_query); 385 $sql .= "AND (comment LIKE '%$mysearchterm%' "; 386 $sql .= "OR {$_TABLES['comments']}.title LIKE '%$mysearchterm%') "; 387 } elseif($this->_keyType == 'all') { 388 // must contain ALL of the keywords 389 $mywords = explode(' ', $this->_query); 390 $sql .= 'AND '; 391 $tmp = ''; 392 foreach ($mywords AS $mysearchterm) { 393 $mysearchterm = addslashes (trim ($mysearchterm)); 394 $tmp .= "(comment LIKE '%$mysearchterm%' OR "; 395 $tmp .= "{$_TABLES['comments']}.title LIKE '%$mysearchterm%') AND "; 396 } 397 $tmp = substr($tmp, 0, strlen($tmp) - 4); 398 $sql .= $tmp; 399 } else if ($this->_keyType == 'any') { 400 // must contain ANY of the keywords 401 $mywords = explode(' ', $this->_query); 402 $sql .= 'AND '; 403 $tmp = ''; 404 foreach ($mywords AS $mysearchterm) { 405 $mysearchterm = addslashes (trim ($mysearchterm)); 406 $tmp .= "(comment LIKE '%$mysearchterm%' OR "; 407 $tmp .= "{$_TABLES['comments']}.title LIKE '%$mysearchterm%') OR "; 408 } 409 $tmp = substr($tmp, 0, strlen($tmp) - 3); 410 $sql .= "($tmp)"; 411 } else { 412 $mywords[] = $this->_query; 413 $mysearchterm = addslashes ($this->_query); 414 $sql .= "AND (comment LIKE '%$mysearchterm%' "; 415 $sql .= "OR {$_TABLES['comments']}.title LIKE '%$mysearchterm%') "; 416 } 417 } 418 419 if (!empty($this->_dateStart) && !empty($this->_dateEnd)) { 420 $delim = substr($this->_dateStart, 4, 1); 421 if (!empty($delim)) { 422 $DS = explode($delim, $this->_dateStart); 423 $DE = explode($delim, $this->_dateEnd); 424 $startdate = mktime(0,0,0,$DS[1],$DS[2],$DS[0]); 425 $enddate = mktime(23,59,59,$DE[1],$DE[2],$DE[0]); 426 $sql .= "AND (UNIX_TIMESTAMP({$_TABLES['comments']}.date) BETWEEN '$startdate' AND '$enddate') "; 427 } 428 } 429 if (!empty($this->_author)) { 430 $sql .= "AND ({$_TABLES['comments']}.uid = '$this->_author') "; 431 } 432 $sql .= 'AND (' . $stwhere . ') '; 433 $sql .= " GROUP BY {$_TABLES['comments']}.date, {$_TABLES['comments']}.title, {$_TABLES['comments']}.comment, {$_TABLES['comments']}.pid, {$_TABLES['comments']}.cid, {$_TABLES['comments']}.uid, {$_TABLES['comments']}.sid, {$_TABLES['comments']}.type, {$_TABLES['stories']}.sid, {$_TABLES['users']}.fullname, {$_TABLES['users']}.username ORDER BY {$_TABLES['comments']}.date DESC "; 434 $l = ($this->_per_page * $this->_page) - $this->_per_page; 435 $sql .= 'LIMIT ' . $l . ',' . $this->_per_page; 436 437 $result_comments = DB_query ($select . $sql); 438 $result_count = DB_query ('SELECT COUNT(*)' . $sql); 439 $B = DB_fetchArray ($result_count, true); 440 441 $comment_results = new Plugin(); 442 $comment_results->searchlabel = $LANG09[54]; 443 $comment_results->addSearchHeading($LANG09[16]); 444 $comment_results->addSearchHeading($LANG09[17]); 445 $comment_results->addSearchHeading($LANG09[18]); 446 $comment_results->num_searchresults = 0; 447 $comment_results->num_itemssearched = $B[0]; 448 $comment_results->supports_paging = true; 449 450 if (!empty ($this->_query)) { 451 $querystring = '&query=' . $this->_query; 452 } else { 453 $querystring = ''; 454 } 455 456 // NOTE if any of your data items need to be links then add them 457 // here! Make sure data elements are in an array and in the same 458 // order as your headings above! 459 $names = array (); 460 while ($A = DB_fetchArray($result_comments)) { 461 $A['title'] = str_replace('$','$',$A['title']); 462 $A['title'] = '<a href="' . $_CONF['site_url'] 463 . '/comment.php?mode=view&cid=' . $A['cid'] 464 . $querystring . '">' . stripslashes ($A['title']) 465 . '</a>'; 466 $thetime = COM_getUserDateTimeFormat ($A['day']); 467 if (empty ($names[$A['uid']])) { 468 $names[$A['uid']] = COM_getDisplayName ($A['uid']); 469 } 470 $author = $names[$A['uid']]; 471 if ($A['uid'] == 1) { 472 $profile = $author; 473 } else { 474 $profile = '<a href="' . $_CONF['site_url'] 475 . '/users.php?mode=profile&uid=' . $A['uid'] 476 . '">' . $author . '</a>'; 477 } 478 $row = array ($A['title'], $thetime[0], $profile); 479 $comment_results->addSearchResult($row); 480 $comment_results->num_searchresults++; 481 } 482 } else { 483 $comment_results = new Plugin(); 484 $comment_results->searchlabel = $LANG09[54]; 485 $comment_results->num_itemssearched = 0; 486 } 487 488 return $comment_results; 489 } 490 491 function _showPager($resultPage, $pages, $extra='') 492 { 493 global $_CONF, $LANG09; 494 495 $urlQuery = urlencode($this->_query); 496 $pager = ''; 497 if ($pages > 1) { 498 if ($resultPage > 1) { 499 $previous = $resultPage - 1; 500 $pager .= ' <a href="' . $_CONF['site_url'] . '/search.php?query=' . $urlQuery . '&keyType=' . $this->_keyType . '&page=' . $previous . '&type=' . $this->_type . '&topic=' . $this->_topic . '&mode=search' . $extra . '">' . $LANG09[47] . '</a> '; 501 } 502 if ($pages <= 20) { 503 $startPage = 1; 504 $endPage = $pages; 505 } else { 506 $startPage = $resultPage - 10; 507 if ($startPage < 1) { 508 $startPage = 1; 509 } 510 $endPage = $resultPage + 9; 511 if ($endPage > $pages) { 512 $endPage = $pages; 513 } 514 } 515 for ($i = $startPage; $i <= $endPage; $i++) { 516 if ($i == $resultPage) { 517 $pager .= " <b>$i</b> "; 518 } else { 519 $pager .= " <a href = {$_CONF['site_url']}/search.php?query=$urlQuery&keyType=$this->_keyType&page=$i&type=$this->_type&topic=$this->_topic&mode=search$extra>$i</a> "; 520 } 521 } 522 if ($resultPage < $pages) { 523 $next = $resultPage+1; 524 $pager .= " <a href = {$_CONF['site_url']}/search.php?query=$urlQuery&keyType=$this->_keyType&page=$next&type=$this->_type&topic=$this->_topic&mode=search$extra>{$LANG09[46]}</a> "; 525 } 526 } 527 return $pager; 528 } 529 530 /** 531 * Gets formatted output of all searches 532 * 533 * @author Tony Bibbs <tony AT geeklog DOT net> 534 * @access private 535 * @param integer $nrows_plugins Total number of search results 536 * @param integer $total_plugins Total number of plugins 537 * @param array $result_plugins Array of plugin results 538 * @param string $searchtime Elapsed search time 539 * @return string HTML output 540 * 541 */ 542 function _formatResults($nrows_plugins, $total_plugins, $result_plugins, $searchtime) 543 { 544 global $_CONF, $_USER, $LANG09; 545 546 $retval = ''; 547 548 $searchmain = new Template ($_CONF['path_layout'] . 'search'); 549 $searchmain->set_file(array ('searchresults' => 'searchresults.thtml')); 550 $searchmain->set_var ('num_matches', ''); 551 552 if ($this->_keyType == 'any') { 553 $searchQuery = str_replace(' ', "</b>' " . $LANG09[57] . " '<b>",$this->_query); 554 $searchQuery = "<b>'$searchQuery'</b>"; 555 } else { 556 if ($this->_keyType == 'all') { 557 $searchQuery = str_replace(' ', "</b>' " . $LANG09[56] . " '<b>",$this->_query); 558 $searchQuery = "<b>'$searchQuery'</b>"; 559 } else { 560 $searchQuery = $LANG09[55] . " '<b>$this->_query</b>'"; 561 } 562 } 563 $searchmain->set_var('lang_matchesfor', $LANG09[25] . " $searchQuery."); 564 $searchmain->set_var('num_items_searched', 0); 565 $searchmain->set_var('lang_itemsin', $LANG09[26]); 566 $searchmain->set_var('search_time', $searchtime); 567 $searchmain->set_var('lang_seconds', $LANG09[27]); 568 $searchmain->set_var('lang_refine_search', $LANG09[61]); 569 570 // Print plugins search results 571 reset($result_plugins); 572 $cur_plugin = new Plugin(); 573 $searchresults = new Template($_CONF['path_layout'] . 'search'); 574 575 $maxdisplayed = 0; 576 $totalfound = 0; 577 $searchblocks = ''; 578 for ($i = 1; $i <= count ($result_plugins); $i++) { 579 $displayed = 0; 580 $searchresults->set_file (array ( 581 'searchheading' => 'searchresults_heading.thtml', 582 'searchrows' => 'searchresults_rows.thtml', 583 'searchblock' => 'searchblock.thtml', 584 'headingcolumn' => 'headingcolumn.thtml', 585 'resultrow' => 'resultrow.thtml', 586 'resulttitle' => 'resultcolumn.thtml', 587 'resultcolumn' => 'resultcolumn.thtml' 588 )); 589 if ($i == 1) { 590 $searchresults->set_var ('data_cols', ''); 591 $searchresults->set_var ('headings', ''); 592 } 593 $cur_plugin = current ($result_plugins); 594 $start_results = (($this->_per_page * $this->_page) - $this->_per_page) + 1; 595 if ($cur_plugin->supports_paging) { 596 $start_results = 1; 597 $end_results = $cur_plugin->num_searchresults; 598 $totalfound += $cur_plugin->num_searchresults; 599 } else { 600 // this plugin doesn't know about paging - fake it 601 if ($cur_plugin->num_searchresults < $start_results) { 602 $cur_plugin->num_searchresults = 0; 603 } else if ($cur_plugin->num_searchresults >= $start_results) { 604 $end_results = ($start_results + $this->_per_page) - 1; 605 if ($end_results > $cur_plugin->num_searchresults) { 606 $end_results = $cur_plugin->num_searchresults; 607 } 608 $totalfound += ($end_results - $start_results) + 1; 609 } else { 610 $start_results = 1; 611 $end_results = $cur_plugin->num_searchresults; 612 $totalfound += $cur_plugin->num_searchresults; 613 } 614 } 615 if ($cur_plugin->num_searchresults > 0) { 616 // Clear out data columns from previous result block 617 $searchresults->set_var('data_cols',''); 618 $searchresults->set_var('start_block_results',COM_startBlock($cur_plugin->searchlabel)); 619 $searchresults->set_var('headings',''); 620 $searchresults->set_var('label', '#'); 621 $searchresults->parse('headings','headingcolumn',true); 622 for ($j = 1; $j <= $cur_plugin->num_searchheadings; $j++) { 623 $searchresults->set_var('label', $cur_plugin->searchheading[$j]); 624 $searchresults->parse('headings','headingcolumn',true); 625 } 626 $searchresults->set_var('results',''); 627 $resultNumber = 0; 628 for ($j = $start_results; $j <= $end_results; $j++) { 629 $columns = $cur_plugin->searchresults[$j - 1]; 630 if ($cur_plugin->supports_paging) { 631 $searchresults->set_var('data', (($this->_per_page * $this->_page) - $this->_per_page) + $j . '.'); 632 } else { 633 $searchresults->set_var('data', $j . '.'); 634 } 635 $searchresults->parse('data_cols','resultcolumn',true); 636 for ($x = 1; $x <= count($columns); $x++) { 637 $searchresults->set_var('data', current($columns)); 638 $searchresults->parse('data_cols','resultcolumn',true); 639 next($columns); 640 } 641 $resultNumber++; 642 $searchresults->set_var ('cssid', ($resultNumber % 2) + 1); 643 $searchresults->parse ('results', 'resultrow', true); 644 $searchresults->set_var ('data_cols', ''); 645 $displayed++; 646 } 647 if ($cur_plugin->num_searchresults == 0) { 648 $searchresults->set_var('results', 649 '<tr><td colspan="4" align="center"><br>' 650 . $LANG09[31] . '</td></tr>'); 651 } 652 $searchresults->set_var('end_block', COM_endBlock()); 653 $searchblocks .= $searchresults->parse('tmpoutput','searchblock'); 654 } 655 next($result_plugins); 656 if ($displayed > $maxdisplayed) { 657 $maxdisplayed = $displayed; 658 } 659 } 660 661 if ($maxdisplayed == 0) { 662 $searchblocks .= '<p>' . $LANG09[13] . '</p>' . LB; 663 } 664 665 $searchmain->set_var ('search_blocks', $searchblocks); 666 667 $searchUrl = $_CONF['site_url'] . '/search.php?mode=search'; 668 $queryUrl = ''; 669 if (!empty ($this->_query)) { 670 $urlQuery = urlencode ($this->_query); 671 $queryUrl .= '&query=' . $urlQuery; 672 } 673 $queryUrl .= '&keyType=' . $this->_keyType 674 . '&type=' . $this->_type; 675 if (!empty ($this->_dateStart)) { 676 $queryUrl .= '&datestart=' . $this->_dateStart; 677 } 678 if (!empty ($this->_dateEnd)) { 679 $queryUrl .= '&dateend=' . $this->_dateEnd; 680 } 681 if (!empty ($this->_topic)) { 682 $queryUrl .= '&topic=' . $this->_topic; 683 } 684 if (!empty ($this->_author) && ($this->_author > 0)) { 685 $queryUrl .= '&author=' . $this->_author; 686 } 687 $queryUrl .= '&results=' . $this->_per_page; 688 if ($this->_page > 1) { 689 if ($maxdisplayed >= $this->_per_page) { 690 $numpages = $this->_page + 1; 691 } else { 692 $numpages = $this->_page; 693 } 694 } else { 695 if ($maxdisplayed >= $this->_per_page) { 696 $numpages = 2; 697 } else { 698 $numpages = 1; 699 } 700 } 701 if ($numpages > $this->_page) { 702 $next = '<a href="' . $searchUrl . $queryUrl . '&page=' 703 . ($this->_page + 1) . '">' . $LANG09[58] . '</a>'; 704 } else { 705 $next = $LANG09[58]; 706 } 707 $searchUrl .= $queryUrl; 708 $paging = COM_printPageNavigation ($searchUrl, $this->_page, $numpages, 709 'page=', false, '', $next); 710 $searchmain->set_var ('search_pager', $paging); 711 $searchmain->set_var ('google_paging', $paging); 712 $tmpTxt = sprintf ($LANG09[24], $totalfound); 713 $searchmain->set_var ('lang_found', $tmpTxt); 714 if (($totalfound == 0) && ($this->_page == 1)) { 715 $searchmain->set_var ('refine_url', ''); 716 $searchmain->set_var ('start_refine_anchortag', ''); 717 $searchmain->set_var ('end_refine_anchortag', ''); 718 $searchmain->set_var ('refine_search', ''); 719 } else { 720 $refineUrl = $_CONF['site_url'] . '/search.php?mode=refine' 721 . $queryUrl; 722 $refineLink = '<a href="' . $refineUrl . '">'; 723 $searchmain->set_var ('refine_url', $refineUrl); 724 $searchmain->set_var ('start_refine_anchortag', $refineLink); 725 $searchmain->set_var ('end_refine_anchortag', '</a>'); 726 $searchmain->set_var ('refine_search', 727 $refineLink . $LANG09[61] . '</a>'); 728 } 729 $retval .= $searchmain->parse ('output', 'searchresults'); 730 731 if (($totalfound == 0) && ($this->_page == 1)) { 732 $searchObj = new Search(); 733 $retval .= $searchObj->showForm (); 734 } 735 736 return $retval; 737 } 738 739 /** 740 * Determines if any advanced search criteria were supplied 741 * 742 * Geeklog allows admins to lock down the access anonymous users have 743 * to the search page. This method helps facilitate checking if an 744 * anonymous user is attempting to illegally access privilege search 745 * capabilities 746 * 747 * @author Tony Bibbs <tony AT geeklog DOT net> 748 * @access private 749 * @return boolean True if advanced criteria were supplied otherwise false 750 * 751 */ 752 function _hasAdvancedCriteria() 753 { 754 if (($this->_type != 'all') OR !empty($this->_dateStart) OR !empty($this->_dateEnd) OR 755 ($this->_author > 0) OR !empty($topic)) { 756 return true; 757 } 758 return false; 759 } 760 761 /** 762 * Shows an error message to anonymous users 763 * 764 * This is called when anonymous users attempt to access search 765 * functionality that has been locked down by the Geeklog admin. 766 * 767 * @author Tony Bibbs <tony AT geeklog DOT net> 768 * @access private 769 * @returns string HTML output for error message 770 * 771 */ 772 function _getAccessDeniedMessage() 773 { 774 global $_CONF, $LANG_LOGIN; 775 776 $retval .= COM_startBlock ($LANG_LOGIN[1], '', 777 COM_getBlockTemplate ('_msg_block', 'header')); 778 $login = new Template($_CONF['path_layout'] . 'submit'); 779 $login->set_file (array ('login'=>'submitloginrequired.thtml')); 780 $login->set_var ('login_message', $LANG_LOGIN[2]); 781 $login->set_var ('site_url', $_CONF['site_url']); 782 $login->set_var ('lang_login', $LANG_LOGIN[3]); 783 $login->set_var ('lang_newuser', $LANG_LOGIN[4]); 784 $login->parse ('output', 'login'); 785 $retval .= $login->finish ($login->get_var('output')); 786 $retval .= COM_endBlock (COM_getBlockTemplate ('_msg_block', 'footer')); 787 788 return $retval; 789 } 790 791 /** 792 * Determines if user is allowed to perform a search 793 * 794 * Geeklog has a number of settings that may prevent 795 * the access anonymous users have to the search engine. 796 * This performs those checks 797 * 798 * @author Tony Bibbs <tony AT geeklog DOT net> 799 * @access private 800 * @return boolean True if search is allowed, otherwise false 801 * 802 */ 803 function _isSearchAllowed() 804 { 805 global $_USER, $_CONF; 806 807 if (empty($_USER['username'])) { 808 809 if ($this->_hasAdvancedCriteria ()) { 810 811 if (($_CONF['loginrequired'] == 1) OR ($_CONF['searchloginrequired'] >= 1)) { 812 return false; 813 } 814 815 } else { 816 817 if (($_CONF['loginrequired'] == 1) OR ($_CONF['searchloginrequired'] == 2)) { 818 return false; 819 } 820 821 } 822 823 } 824 825 return true; 826 } 827 828 /** 829 * Determines if user is allowed to use the search form 830 * 831 * Geeklog has a number of settings that may prevent 832 * the access anonymous users have to the search engine. 833 * This performs those checks 834 * 835 * @author Dirk Haun <Dirk AT haun-online DOT de> 836 * @access private 837 * @return boolean True if form usage is allowed, otherwise false 838 * 839 */ 840 function _isFormAllowed () 841 { 842 global $_CONF, $_USER; 843 844 if (empty($_USER['username']) AND (($_CONF['loginrequired'] == 1) OR ($_CONF['searchloginrequired'] >= 1))) { 845 return false; 846 } 847 848 return true; 849 } 850 851 /** 852 * Shows search form 853 * 854 * Shows advanced search page 855 * 856 * @author Tony Bibbs <tony AT geeklog DOT net> 857 * @access public 858 * @return string HTML output for form 859 * 860 */ 861 function showForm () 862 { 863 global $_CONF, $_TABLES, $LANG09; 864 865 $retval = ''; 866 867 // Verify current user my use the search form 868 if (!$this->_isFormAllowed()) { 869 return $this->_getAccessDeniedMessage(); 870 } 871 872 $retval .= COM_startBlock($LANG09[1],'advancedsearch.html'); 873 $searchform = new Template($_CONF['path_layout'].'search'); 874 $searchform->set_file (array ('searchform' => 'searchform.thtml', 875 'authors' => 'searchauthors.thtml')); 876 $searchform->set_var('search_intro', $LANG09[19]); 877 $searchform->set_var('site_url', $_CONF['site_url']); 878 $searchform->set_var('lang_keywords', $LANG09[2]); 879 $searchform->set_var('lang_date', $LANG09[20]); 880 $searchform->set_var('lang_to', $LANG09[21]); 881 $searchform->set_var('date_format', $LANG09[22]); 882 $searchform->set_var('lang_topic', $LANG09[3]); 883 $searchform->set_var('lang_all', $LANG09[4]); 884 $searchform->set_var('topic_option_list', 885 COM_topicList ('tid,topic', $this->_topic)); 886 $searchform->set_var('lang_type', $LANG09[5]); 887 $searchform->set_var('lang_stories', $LANG09[6]); 888 $searchform->set_var('lang_comments', $LANG09[7]); 889 $searchform->set_var('lang_links', $LANG09[39]); 890 $searchform->set_var('lang_results', $LANG09[59]); 891 $searchform->set_var('lang_per_page', $LANG09[60]); 892 893 $searchform->set_var('lang_exact_phrase', $LANG09[43]); 894 $searchform->set_var('lang_all_words', $LANG09[44]); 895 $searchform->set_var('lang_any_word', $LANG09[45]); 896 897 $searchform->set_var ('query', htmlspecialchars ($this->_query)); 898 $searchform->set_var ('datestart', $this->_dateStart); 899 $searchform->set_var ('dateend', $this->_dateEnd); 900 $searchform->set_var ($this->_per_page . '_selected', 901 'selected="selected"'); 902 903 $phrase_selected = ''; 904 $all_selected = ''; 905 $any_selected = ''; 906 if ($this->_keyType == 'phrase') { 907 $phrase_selected = 'selected="selected"'; 908 } else if ($this->_keyType == 'all') { 909 $all_selected = 'selected="selected"'; 910 } else if ($this->_keyType == 'any') { 911 $any_selected = 'selected="selected"'; 912 } 913 $searchform->set_var ('key_phrase_selected', $phrase_selected); 914 $searchform->set_var ('key_all_selected', $all_selected); 915 $searchform->set_var ('key_any_selected', $any_selected); 916 917 $plugintypes = PLG_getSearchTypes(); 918 $pluginoptions = ''; 919 $plugin_selected = false; 920 // Generally I don't like to hardcode HTML but this seems easiest 921 for ($i = 0; $i < count ($plugintypes); $i++) { 922 $pluginoptions .= '<option value="' . key ($plugintypes) . '"'; 923 if ($this->_type == key ($plugintypes)) { 924 $pluginoptions .= ' selected="selected"'; 925 $plugin_selected = true; 926 } 927 $pluginoptions .= '>' . current ($plugintypes) . '</option>' . LB; 928 next($plugintypes); 929 } 930 $searchform->set_var('plugin_types', $pluginoptions); 931 932 $all_selected = ''; 933 $stories_selected = ''; 934 $comments_selected = ''; 935 if (!$plugin_selected) { 936 if ($this->_type == 'stories') { 937 $stories_selected = 'selected="selected"'; 938 } else if ($this->_type == 'comments') { 939 $comments_selected = 'selected="selected"'; 940 } else { 941 $all_selected = 'selected="selected"'; 942 } 943 } 944 $searchform->set_var ('type_all_selected', $all_selected); 945 $searchform->set_var ('stories_selected', $stories_selected); 946 $searchform->set_var ('comments_selected', $comments_selected); 947 948 if ($_CONF['contributedbyline'] == 1) { 949 $searchform->set_var('lang_authors', $LANG09[8]); 950 $searchusers = array(); 951 $result = DB_query("SELECT DISTINCT uid FROM {$_TABLES['comments']}"); 952 while ($A = DB_fetchArray($result)) { 953 $searchusers[$A['uid']] = $A['uid']; 954 } 955 $result = DB_query("SELECT DISTINCT uid FROM {$_TABLES['stories']} WHERE (date <= NOW()) AND (draft_flag = 0)"); 956 while ($A = DB_fetchArray($result)) { 957 $searchusers[$A['uid']] = $A['uid']; 958 } 959 960 $inlist = implode(',', $searchusers); 961 962 if (!empty ($inlist)) { 963 $sql = "SELECT uid,username,fullname FROM {$_TABLES['users']} WHERE uid IN ($inlist)"; 964 if (isset ($_CONF['show_fullname']) && 965 ($_CONF['show_fullname'] == 1)) { 966 /* Caveat: This will group all users with an emtpy fullname 967 * together, so it's not exactly sorted by their 968 * full name ... 969 */ 970 $sql .= ' ORDER BY fullname,username'; 971 } else { 972 $sql .= ' ORDER BY username'; 973 } 974 $result = DB_query ($sql); 975 $useroptions = ''; 976 while ($A = DB_fetchArray($result)) { 977 $useroptions .= '<option value="' . $A['uid'] . '"'; 978 if ($A['uid'] == $this->_author) { 979 $useroptions .= ' selected="selected"'; 980 } 981 $useroptions .= '>' . $this->_displayName ($A['username'], 982 $A['fullname']) . '</option>'; 983 } 984 $searchform->set_var('author_option_list', $useroptions); 985 $searchform->parse('author_form_element', 'authors', true); 986 } else { 987 $searchform->set_var('author_form_element', '<input type="hidden" name="author" value="0">'); 988 } 989 } else { 990 $searchform->set_var ('author_form_element', 991 '<input type="hidden" name="author" value="0">'); 992 } 993 $searchform->set_var('lang_search', $LANG09[10]); 994 $searchform->parse('output', 'searchform'); 995 996 $retval .= $searchform->finish($searchform->get_var('output')); 997 $retval .= COM_endBlock(); 998 999 return $retval; 1000 } 1001 1002 /** 1003 * Kicks off the appropriate search(es) 1004 * 1005 * @author Tony Bibbs <tony AT geeklog DOT net> 1006 * @access public 1007 * 1008 */ 1009 function doSearch() 1010 { 1011 global $_CONF; 1012 1013 // Verify current user can perform requested search 1014 if (!$this->_isSearchAllowed()) { 1015 return $this->_getAccessDeniedMessage(); 1016 } 1017 1018 // Start search timer 1019 $searchtimer = new timerobject(); 1020 $searchtimer->setPercision(4); 1021 $searchtimer->startTimer(); 1022 1023 // Do searches 1024 $this->story_results = $this->_searchStories(); 1025 $this->comment_results = $this->_searchComments(); 1026 1027 // Have plugins do their searches 1028 list($nrows_plugins, $total_plugins, $result_plugins) = PLG_doSearch($this->_query, $this->_dateStart, $this->_dateEnd, $this->_topic, $this->_type, $this->_author, $this->_keyType, $this->_page, $this->_per_page); 1029 1030 // Add the core GL object search results to plugin results 1031 $nrows_plugins += $this->story_results->num_searchresults; 1032 $nrows_plugins += $this->comment_results->num_searchresults; 1033 $total_plugins += $this->story_results->num_itemssearched; 1034 $total_plugins += $this->comment_results->num_itemssearched; 1035 1036 // Move GL core objects to front of array 1037 array_unshift ($result_plugins, $this->story_results, 1038 $this->comment_results); 1039 1040 // Searches are done, stop timer 1041 $searchtime = $searchtimer->stopTimer(); 1042 1043 // Format results 1044 $retval = $this->_formatResults ($nrows_plugins, $total_plugins, 1045 $result_plugins, $searchtime); 1046 1047 return $retval; 1048 } 1049 1050 } 1051 1052 ?>
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
Généré le : Wed Nov 21 12:27:40 2007 | par Balluche grâce à PHPXref 0.7 |
![]() |