source: temp/test-xoops.ec-cube.net/html/search.php @ 405

Revision 405, 12.1 KB checked in by root, 20 years ago (diff)
Line 
1<?php
2// $Id: search.php,v 1.4 2005/08/03 12:39:11 onokazu Exp $
3//  ------------------------------------------------------------------------ //
4//                XOOPS - PHP Content Management System                      //
5//                    Copyright (c) 2000 XOOPS.org                           //
6//                       <http://www.xoops.org/>                             //
7//  ------------------------------------------------------------------------ //
8//  This program is free software; you can redistribute it and/or modify     //
9//  it under the terms of the GNU General Public License as published by     //
10//  the Free Software Foundation; either version 2 of the License, or        //
11//  (at your option) any later version.                                      //
12//                                                                           //
13//  You may not change or alter any portion of this comment or credits       //
14//  of supporting developers from this source code or any supporting         //
15//  source code which is considered copyrighted (c) material of the          //
16//  original comment or credit authors.                                      //
17//                                                                           //
18//  This program is distributed in the hope that it will be useful,          //
19//  but WITHOUT ANY WARRANTY; without even the implied warranty of           //
20//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            //
21//  GNU General Public License for more details.                             //
22//                                                                           //
23//  You should have received a copy of the GNU General Public License        //
24//  along with this program; if not, write to the Free Software              //
25//  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA //
26//  ------------------------------------------------------------------------ //
27
28$xoopsOption['pagetype'] = "search";
29
30include 'mainfile.php';
31$config_handler =& xoops_gethandler('config');
32$xoopsConfigSearch =& $config_handler->getConfigsByCat(XOOPS_CONF_SEARCH);
33
34if ($xoopsConfigSearch['enable_search'] != 1) {
35    header('Location: '.XOOPS_URL.'/index.php');
36    exit();
37}
38$action = "search";
39if (!empty($_GET['action'])) {
40  $action = $_GET['action'];
41} elseif (!empty($_POST['action'])) {
42  $action = $_POST['action'];
43}
44$query = "";
45if (!empty($_GET['query'])) {
46  $query = trim($_GET['query']);
47} elseif (!empty($_POST['query'])) {
48  $query = trim($_POST['query']);
49}
50$andor = "AND";
51if (!empty($_GET['andor'])) {
52  $andor = $_GET['andor'];
53} elseif (!empty($_POST['andor'])) {
54  $andor = $_POST['andor'];
55}
56$mid = $uid = $start = 0;
57if ( !empty($_GET['mid']) ) {
58  $mid = intval($_GET['mid']);
59} elseif ( !empty($_POST['mid']) ) {
60  $mid = intval($_POST['mid']);
61}
62if (!empty($_GET['uid'])) {
63  $uid = intval($_GET['uid']);
64} elseif (!empty($_POST['uid'])) {
65  $uid = intval($_POST['uid']);
66}
67if (!empty($_GET['start'])) {
68  $start = intval($_GET['start']);
69} elseif (!empty($_POST['start'])) {
70  $start = intval($_POST['start']);
71}
72if (!empty($_GET['mids'])) {
73  $mids = $_GET['mids'];
74} elseif (!empty($_POST['mids'])) {
75  $mids = $_POST['mids'];
76}
77
78$queries = array();
79
80if ($action == "results") {
81    if ($query == "") {
82         redirect_header("search.php",1,_SR_PLZENTER);
83        exit();
84    }
85} elseif ($action == "showall") {
86    if ($query == "" || empty($mid)) {
87        redirect_header("search.php",1,_SR_PLZENTER);
88        exit();
89    }
90} elseif ($action == "showallbyuser") {
91    if (empty($mid) || empty($uid)) {
92        redirect_header("search.php",1,_SR_PLZENTER);
93        exit();
94    }
95}
96
97$groups = is_object($xoopsUser) ? $xoopsUser -> getGroups() : XOOPS_GROUP_ANONYMOUS;
98$gperm_handler = & xoops_gethandler( 'groupperm' );
99$available_modules = $gperm_handler->getItemIds('module_read', $groups);
100
101if ($action == 'search') {
102    include XOOPS_ROOT_PATH.'/header.php';
103    include 'include/searchform.php';
104    $search_form->display();
105    include XOOPS_ROOT_PATH.'/footer.php';
106    exit();
107}
108
109if ( $andor != "OR" && $andor != "exact" && $andor != "AND" ) {
110    $andor = "AND";
111}
112
113$myts =& MyTextSanitizer::getInstance();
114if ($action != 'showallbyuser') {
115    if ( $andor != "exact" ) {
116        $ignored_queries = array(); // holds kewords that are shorter than allowed minmum length
117        $temp_queries = preg_split('/[\s,]+/', $query);
118        foreach ($temp_queries as $q) {
119            $q = trim($q);
120            if (strlen($q) >= $xoopsConfigSearch['keyword_min']) {
121                $queries[] = $myts->addSlashes($q);
122            } else {
123                $ignored_queries[] = $myts->addSlashes($q);
124            }
125        }
126        if (count($queries) == 0) {
127            redirect_header('search.php', 2, sprintf(_SR_KEYTOOSHORT, $xoopsConfigSearch['keyword_min']));
128            exit();
129        }
130    } else {
131        $query = trim($query);
132        if (strlen($query) < $xoopsConfigSearch['keyword_min']) {
133            redirect_header('search.php', 2, sprintf(_SR_KEYTOOSHORT, $xoopsConfigSearch['keyword_min']));
134            exit();
135        }
136        $queries = array($myts->addSlashes($query));
137    }
138}
139switch ($action) {
140    case "results":
141    $module_handler =& xoops_gethandler('module');
142    $criteria = new CriteriaCompo(new Criteria('hassearch', 1));
143    $criteria->add(new Criteria('isactive', 1));
144    $criteria->add(new Criteria('mid', "(".implode(',', $available_modules).")", 'IN'));
145    $modules =& $module_handler->getObjects($criteria, true);
146    if (empty($mids) || !is_array($mids)) {
147        unset($mids);
148        $mids = array_keys($modules);
149    }
150    include XOOPS_ROOT_PATH."/header.php";
151    echo "<h3>"._SR_SEARCHRESULTS."</h3>\n";
152    echo _SR_KEYWORDS.':';
153    if ($andor != 'exact') {
154        foreach ($queries as $q) {
155            echo ' <b>'.htmlspecialchars(stripslashes($q)).'</b>';
156        }
157        if (!empty($ignored_queries)) {
158            echo '<br />';
159            printf(_SR_IGNOREDWORDS, $xoopsConfigSearch['keyword_min']);
160            foreach ($ignored_queries as $q) {
161                echo ' <b>'.htmlspecialchars(stripslashes($q)).'</b>';
162            }
163        }
164    } else {
165        echo ' "<b>'.htmlspecialchars(stripslashes($queries[0])).'</b>"';
166    }
167    echo '<br />';
168    foreach ($mids as $mid) {
169        $mid = intval($mid);
170        if ( in_array($mid, $available_modules) ) {
171            $module =& $modules[$mid];
172            $results =& $module->search($queries, $andor, 5, 0);
173            echo "<h4>".$myts->makeTboxData4Show($module->getVar('name'))."</h4>";
174            $count = count($results);
175            if (!is_array($results) || $count == 0) {
176                echo "<p>"._SR_NOMATCH."</p>";
177            } else {
178                for ($i = 0; $i < $count; $i++) {
179                    if (isset($results[$i]['image']) && $results[$i]['image'] != "") {
180                        echo "<img src='modules/".$module->getVar('dirname')."/".$results[$i]['image']."' alt='".$myts->makeTboxData4Show($module->getVar('name'))."' />&nbsp;";
181                    } else {
182                        echo "<img src='images/icons/posticon2.gif' alt='".$myts->makeTboxData4Show($module->getVar('name'))."' width='26' height='26' />&nbsp;";
183                    }
184                    echo "<b><a href='modules/".$module->getVar('dirname')."/".$results[$i]['link']."'>".$myts->makeTboxData4Show($results[$i]['title'])."</a></b><br />\n";
185                    echo "<small>";
186                    $results[$i]['uid'] = intval($results[$i]['uid']);
187                    if ( !empty($results[$i]['uid']) ) {
188                        $uname = XoopsUser::getUnameFromId($results[$i]['uid']);
189                        echo "&nbsp;&nbsp;<a href='".XOOPS_URL."/userinfo.php?uid=".$results[$i]['uid']."'>".$uname."</a>\n";
190                    }
191                    echo $results[$i]['time'] ? " (". formatTimestamp(intval($results[$i]['time'])).")" : "";
192                    echo "</small><br />\n";
193                }
194                if ( $count == 5 ) {
195                    $search_url = XOOPS_URL.'/search.php?query='.urlencode(stripslashes(implode(' ', $queries)));
196                    $search_url .= "&mid=$mid&action=showall&andor=$andor";
197                    echo '<br /><a href="'.htmlspecialchars($search_url).'">'._SR_SHOWALLR.'</a></p>';
198                }
199            }
200        }
201        unset($results);
202        unset($module);
203    }
204    include "include/searchform.php";
205    $search_form->display();
206    break;
207    case "showall":
208    case 'showallbyuser':
209    include XOOPS_ROOT_PATH."/header.php";
210    $module_handler =& xoops_gethandler('module');
211    $module =& $module_handler->get($mid);
212    $results =& $module->search($queries, $andor, 20, $start, $uid);
213    $count = count($results);
214    if (is_array($results) && $count > 0) {
215        $next_results =& $module->search($queries, $andor, 1, $start + 20, $uid);
216        $next_count = count($next_results);
217        $has_next = false;
218        if (is_array($next_results) && $next_count == 1) {
219            $has_next = true;
220        }
221        echo "<h4>"._SR_SEARCHRESULTS."</h4>\n";
222        if ($action == 'showall') {
223            echo _SR_KEYWORDS.':';
224            if ($andor != 'exact') {
225                foreach ($queries as $q) {
226                    echo ' <b>'.htmlspecialchars(stripslashes($q)).'</b>';
227                }
228            } else {
229                echo ' "<b>'.htmlspecialchars(stripslashes($queries[0])).'</b>"';
230            }
231            echo '<br />';
232        }
233        //    printf(_SR_FOUND,$count);
234        //    echo "<br />";
235        printf(_SR_SHOWING, $start+1, $start + $count);
236        echo "<h5>".$myts->makeTboxData4Show($module->getVar('name'))."</h5>";
237        for ($i = 0; $i < $count; $i++) {
238            if (isset($results[$i]['image']) && $results[$i]['image'] != '') {
239                echo "<img src='modules/".$module->getVar('dirname')."/".$results[$i]['image']."' alt='".$myts->makeTboxData4Show($module->getVar('name'))."' />&nbsp;";
240            } else {
241                echo "<img src='images/icons/posticon2.gif' alt='".$myts->makeTboxData4Show($module->name())."' width='26' height='26' />&nbsp;";
242            }
243            echo "<b><a href='modules/".$module->getVar('dirname')."/".$results[$i]['link']."'>".$myts->makeTboxData4Show($results[$i]['title'])."</a></b><br />\n";
244            echo "<small>";
245            $results[$i]['uid'] = intval($results[$i]['uid']);
246            if ( !empty($results[$i]['uid']) ) {
247                $uname = XoopsUser::getUnameFromId($results[$i]['uid']);
248                echo "&nbsp;&nbsp;<a href='".XOOPS_URL."/userinfo.php?uid=".$results[$i]['uid']."'>".$uname."</a>\n";
249            }
250            echo $results[$i]['time'] ? " (". formatTimestamp(intval($results[$i]['time'])).")" : "";
251            echo "</small><br />\n";
252        }
253        echo '
254        <table>
255          <tr>
256        ';
257        $search_url = XOOPS_URL.'/search.php?query='.urlencode(stripslashes(implode(' ', $queries)));
258        $search_url .= "&mid=$mid&action=$action&andor=$andor";
259        if ($action=='showallbyuser') {
260            $search_url .= "&uid=$uid";
261        }
262        if ( $start > 0 ) {
263            $prev = $start - 20;
264            echo '<td align="left">
265            ';
266            $search_url_prev = $search_url."&start=$prev";
267            echo '<a href="'.htmlspecialchars($search_url_prev).'">'._SR_PREVIOUS.'</a></td>
268            ';
269        }
270        echo '<td>&nbsp;&nbsp;</td>
271        ';
272        if (false != $has_next) {
273            $next = $start + 20;
274            $search_url_next = $search_url."&start=$next";
275            echo '<td align="right"><a href="'.htmlspecialchars($search_url_next).'">'._SR_NEXT.'</a></td>
276            ';
277        }
278        echo '
279          </tr>
280        </table>
281        <p>
282        ';
283    } else {
284        echo '<p>'._SR_NOMATCH.'</p>';
285    }
286    include "include/searchform.php";
287    $search_form->display();
288    echo '</p>
289    ';
290    break;
291}
292include XOOPS_ROOT_PATH."/footer.php";
293?>
Note: See TracBrowser for help on using the repository browser.