XOOPS 2.5.6  Final
 All Classes Namespaces Files Functions Variables Pages
search.php
Go to the documentation of this file.
1 <?php
2 /*
3  You may not change or alter any portion of this comment or credits
4  of supporting developers from this source code or any supporting source code
5  which is considered copyrighted (c) material of the original comment or credit authors.
6 
7  This program is distributed in the hope that it will be useful,
8  but WITHOUT ANY WARRANTY; without even the implied warranty of
9  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10 */
11 
27 include dirname(__FILE__) . DIRECTORY_SEPARATOR . 'mainfile.php';
28 
29 xoops_loadLanguage('search');
30 
33 
34 if ($xoopsConfigSearch['enable_search'] != 1) {
35  header('Location: ' . XOOPS_URL . '/index.php');
36  exit();
37 }
38 $action = "search";
39 if (!empty($_GET['action'])) {
40  $action = trim(strip_tags($_GET['action']));
41 } else if (!empty($_POST['action'])) {
42  $action = trim(strip_tags($_POST['action']));
43 }
44 $query = "";
45 if (!empty($_GET['query'])) {
46  $query = trim(strip_tags($_GET['query']));
47 } else if (!empty($_POST['query'])) {
48  $query = trim(strip_tags($_POST['query']));
49 }
50 $andor = "AND";
51 if (!empty($_GET['andor'])) {
52  $andor = trim(strip_tags($_GET['andor']));
53 } else if (!empty($_POST['andor'])) {
54  $andor = trim(strip_tags($_POST['andor']));
55 }
56 $mid = $uid = $start = 0;
57 if (!empty($_GET['mid'])) {
58  $mid = intval($_GET['mid']);
59 } else if (!empty($_POST['mid'])) {
60  $mid = intval($_POST['mid']);
61 }
62 if (!empty($_GET['uid'])) {
63  $uid = intval($_GET['uid']);
64 } else if (!empty($_POST['uid'])) {
65  $uid = intval($_POST['uid']);
66 }
67 if (!empty($_GET['start'])) {
68  $start = intval($_GET['start']);
69 } else if (!empty($_POST['start'])) {
70  $start = intval($_POST['start']);
71 }
72 
73 $queries = array();
74 
75 if ($action == "results") {
76  if ($query == "") {
77  redirect_header("search.php", 1, _SR_PLZENTER);
78  exit();
79  }
80 } else if ($action == "showall") {
81  if ($query == "" || empty($mid)) {
82  redirect_header("search.php", 1, _SR_PLZENTER);
83  exit();
84  }
85 } else if ($action == "showallbyuser") {
86  if (empty($mid) || empty($uid)) {
87  redirect_header("search.php", 1, _SR_PLZENTER);
88  exit();
89  }
90 }
91 
92 $groups = is_object($xoopsUser) ? $xoopsUser->getGroups() : XOOPS_GROUP_ANONYMOUS;
94 $available_modules = $gperm_handler->getItemIds('module_read', $groups);
95 if ($action == 'search') {
96  include $GLOBALS['xoops']->path('header.php');
97  include $GLOBALS['xoops']->path('include/searchform.php');
98  $search_form->display();
99  include $GLOBALS['xoops']->path('footer.php');
100  exit();
101 }
102 if ($andor != "OR" && $andor != "exact" && $andor != "AND") {
103  $andor = "AND";
104 }
105 
107 if ($action != 'showallbyuser') {
108  if ($andor != "exact") {
109  $ignored_queries = array(); // holds kewords that are shorter than allowed minmum length
110  $temp_queries = preg_split('/[\s,]+/', $query);
111  foreach ($temp_queries as $q) {
112  $q = trim($q);
113  if (strlen($q) >= $xoopsConfigSearch['keyword_min']) {
114  $queries[] = $myts->addSlashes($q);
115  } else {
116  $ignored_queries[] = $myts->addSlashes($q);
117  }
118  }
119  if (count($queries) == 0) {
120  redirect_header('search.php', 2, sprintf(_SR_KEYTOOSHORT, $xoopsConfigSearch['keyword_min']));
121  exit();
122  }
123  } else {
124  $query = trim($query);
125  if (strlen($query) < $xoopsConfigSearch['keyword_min']) {
126  redirect_header('search.php', 2, sprintf(_SR_KEYTOOSHORT, $xoopsConfigSearch['keyword_min']));
127  exit();
128  }
129  $queries = array($myts->addSlashes($query));
130  }
131 }
132 switch ($action) {
133  case "results":
134  $module_handler =& xoops_gethandler('module');
135  $criteria = new CriteriaCompo(new Criteria('hassearch', 1));
136  $criteria->add(new Criteria('isactive', 1));
137  $criteria->add(new Criteria('mid', "(" . implode(',', $available_modules) . ")", 'IN'));
138  $modules = $module_handler->getObjects($criteria, true);
139  $mids = isset($_REQUEST['mids']) ? $_REQUEST['mids'] : array();
140  if (empty($mids) || ! is_array($mids)) {
141  unset($mids);
142  $mids = array_keys($modules);
143  }
144  $xoopsOption['xoops_pagetitle'] = _SR_SEARCHRESULTS . ': ' . implode(' ', $queries);
145  include $GLOBALS['xoops']->path('header.php');
146  $nomatch = true;
147  echo "<h3>" . _SR_SEARCHRESULTS . "</h3>\n";
148  echo _SR_KEYWORDS . ':';
149  if ($andor != 'exact') {
150  foreach ($queries as $q) {
151  echo ' <strong>' . htmlspecialchars(stripslashes($q)) . '</strong>';
152  }
153  if (!empty($ignored_queries)) {
154  echo '<br />';
155  printf(_SR_IGNOREDWORDS, $xoopsConfigSearch['keyword_min']);
156  foreach ($ignored_queries as $q) {
157  echo ' <strong>' . htmlspecialchars(stripslashes($q)) . '</strong>';
158  }
159  }
160  } else {
161  echo ' "<strong>' . htmlspecialchars(stripslashes($queries[0])) . '</strong>"';
162  }
163  echo '<br />';
164  foreach ($mids as $mid) {
165  $mid = intval($mid);
166  if (in_array($mid, $available_modules)) {
167  $module = $modules[$mid];
168  $results = $module->search($queries, $andor, 5, 0);
169  $count = count($results);
170  if (is_array($results) && $count > 0) {
171  $nomatch = false;
172  echo "<h4>" . $module->getVar('name') . "</h4>";
173  for($i = 0; $i < $count; $i++) {
174  if (isset($results[$i]['image']) && $results[$i]['image'] != "") {
175  echo "<img src='modules/" . $module->getVar('dirname') . "/" . $results[$i]['image'] . "' alt='" . $module->getVar('name') . "' />&nbsp;";
176  } else {
177  echo "<img style='width:26px; height:26px;' src='images/icons/posticon2.gif' alt='" . $module->getVar('name') . "' />&nbsp;";
178  }
179  if (!preg_match("/^http[s]*:\/\//i", $results[$i]['link'])) {
180  $results[$i]['link'] = "modules/" . $module->getVar('dirname') . "/" . $results[$i]['link'];
181  }
182  echo "<strong><a href='" . $results[$i]['link'] . "' title=''>" . $myts->htmlspecialchars($results[$i]['title']) . "</a></strong><br />\n";
183  echo "<span class='x-small'>";
184  $results[$i]['uid'] = @intval($results[$i]['uid']);
185  if (!empty($results[$i]['uid'])) {
186  $uname = XoopsUser::getUnameFromId($results[$i]['uid']);
187  echo "&nbsp;&nbsp;<a href='" . XOOPS_URL . "/userinfo.php?uid=" . $results[$i]['uid'] . "' title=''>" . $uname . "</a>\n";
188  }
189  echo !empty($results[$i]['time']) ? " (" . formatTimestamp(intval($results[$i]['time'])) . ")" : "";
190  echo "</span><br />\n";
191  }
192  if ($count >= 5) {
193  $search_url = XOOPS_URL . '/search.php?query=' . urlencode(stripslashes(implode(' ', $queries)));
194  $search_url .= "&mid={$mid}&action=showall&andor={$andor}";
195  echo '<p><a href="' . htmlspecialchars($search_url) . '" title="' . _SR_SHOWALLR . '">' . _SR_SHOWALLR . '</a></p>';
196  }
197  }
198  }
199  unset($results);
200  unset($module);
201  }
202  if ($nomatch) {
203  echo "<p>" . _SR_NOMATCH . "</p>";
204  }
205  include $GLOBALS['xoops']->path('include/searchform.php');
206  $search_form->display();
207  break;
208 
209  case "showall":
210  case 'showallbyuser':
211  include $GLOBALS['xoops']->path('header.php');
212  $module_handler =& xoops_gethandler('module');
213  $module =& $module_handler->get($mid);
214  $results = $module->search($queries, $andor, 20, $start, $uid);
215  $count = count($results);
216  if (is_array($results) && $count > 0) {
217  $next_results =& $module->search($queries, $andor, 1, $start + 20, $uid);
218  $next_count = count($next_results);
219  $has_next = false;
220  if (is_array($next_results) && $next_count == 1) {
221  $has_next = true;
222  }
223  echo "<h4>" . _SR_SEARCHRESULTS . "</h4>\n";
224  if ($action == 'showall') {
225  echo _SR_KEYWORDS . ':';
226  if ($andor != 'exact') {
227  foreach ($queries as $q) {
228  echo ' <strong>' . htmlspecialchars(stripslashes($q)) . '</strong>';
229  }
230  } else {
231  echo ' "<strong>' . htmlspecialchars(stripslashes($queries[0])) . '</strong>"';
232  }
233  echo '<br />';
234  }
235  printf(_SR_SHOWING, $start + 1, $start + $count);
236  echo "<h5>" . $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', "n") . "/" . $results[$i]['image'] . "' alt='" . $module->getVar('name') . "' />&nbsp;";
240  } else {
241  echo "<img style='width:26px; height:26px;' src='images/icons/posticon2.gif' alt='" . $module->getVar("name") . "' />&nbsp;";
242  }
243  if (!preg_match("/^http[s]*:\/\//i", $results[$i]['link'])) {
244  $results[$i]['link'] = "modules/" . $module->getVar('dirname') . "/" . $results[$i]['link'];
245  }
246  echo "<strong><a href='" . $results[$i]['link'] . "'>" . $myts->htmlspecialchars($results[$i]['title']) . "</a></strong><br />\n";
247  echo "<span class='x-small'>";
248  $results[$i]['uid'] = @intval($results[$i]['uid']);
249  if (!empty($results[$i]['uid'])) {
250  $uname = XoopsUser::getUnameFromId($results[$i]['uid']);
251  echo "&nbsp;&nbsp;<a href='" . XOOPS_URL . "/userinfo.php?uid=" . $results[$i]['uid'] . "'>" . $uname . "</a>\n";
252  }
253  echo !empty($results[$i]['time']) ? " (" . formatTimestamp(intval($results[$i]['time'])) . ")" : "";
254  echo "</span><br />\n";
255  }
256  echo '<table><tr>';
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  $search_url_prev = $search_url . "&start={$prev}";
266  echo '<a href="' . htmlspecialchars($search_url_prev) . '">' . _SR_PREVIOUS . '</a></td>';
267  }
268  echo '<td>&nbsp;&nbsp;</td>';
269  if (false != $has_next) {
270  $next = $start + 20;
271  $search_url_next = $search_url . "&start={$next}";
272  echo '<td align="right"><a href="' . htmlspecialchars($search_url_next) . '">' . _SR_NEXT . '</a></td>';
273  }
274  echo '</tr></table>';
275  } else {
276  echo '<p>' . _SR_NOMATCH . '</p>';
277  }
278  include $GLOBALS['xoops']->path('include/searchform.php');
279  $search_form->display();
280  break;
281 }
282 include $GLOBALS['xoops']->path('footer.php');
283 ?>