XOOPS  2.6.0
index.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 
13 
26 include dirname(dirname(__DIR__)) . DIRECTORY_SEPARATOR . 'mainfile.php';
27 
29 if (!$search->getConfig('enable_search')) {
30  header('Location: ' . XOOPS_URL . '/index.php');
31  exit();
32 }
33 
35 
36 $action = Request::getCmd('action', 'search');
37 $query = Request::getString('query', '');
38 $andor = Request::getWord('andor', 'AND');
39 $mid = Request::getInt('mid', 0);
40 $uid = Request::getInt('uid', 0);
41 $start = Request::getInt('start', 0);
42 $mids = Request::getArray('mids', array());
43 
44 $queries = array();
45 
46 if ($action == "results") {
47  if ($query == "") {
48  $xoops->redirect("index.php", 1, _MD_SEARCH_PLZENTER);
49  }
50 } else {
51  if ($action == "showall") {
52  if ($query == "" || empty($mid)) {
53  $xoops->redirect("index.php", 1, _MD_SEARCH_PLZENTER);
54  }
55  } else {
56  if ($action == "showallbyuser") {
57  if (empty($mid) || empty($uid)) {
58  $xoops->redirect("index.php", 1, _MD_SEARCH_PLZENTER);
59  }
60  }
61  }
62 }
63 
64 $gperm_handler = $xoops->getHandlerGroupperm();
65 $available_modules = $gperm_handler->getItemIds('module_read', $xoops->getUserGroups());
66 $available_plugins = \Xoops\Module\Plugin::getPlugins('search');
67 
68 if ($action == 'search') {
69  $xoops->header();
70 
71  /* @var $formHandler SearchSearchForm */
72  $formHandler = $search->getForm(null, 'search');
73  $form = $formHandler->getSearchFrom($andor, $queries, $mids, $mid);
74  $form->display();
75 
76  $xoops->footer();
77 }
78 if ($andor != "OR" && $andor != "exact" && $andor != "AND") {
79  $andor = "AND";
80 }
81 
82 $ignored_queries = array(); // holds kewords that are shorter than allowed minmum length
83 $queries_pattern = array();
85 if ($action != 'showallbyuser') {
86  if ($andor != "exact") {
87  //$temp_queries = preg_split('/[\s,]+/', $query);
88  $temp_queries = str_getcsv($query, ' ', '"');
89  foreach ($temp_queries as $q) {
90  $q = trim($q);
91  if (mb_strlen($q) >= $search->getConfig('keyword_min')) {
92  $queries[] = $myts->addSlashes($q);
93  $queries_pattern[] = '~(' . $q . ')~sUi';
94  } else {
95  $ignored_queries[] = $myts->addSlashes($q);
96  }
97  }
98  if (count($queries) == 0) {
99  $xoops->redirect('index.php', 2, sprintf(_MD_SEARCH_KEYTOOSHORT, $search->getConfig('keyword_min')));
100  }
101  } else {
102  $query = trim($query);
103  if (mb_strlen($query) < $search->getConfig('keyword_min')) {
104  $xoops->redirect('index.php', 2, sprintf(_MD_SEARCH_KEYTOOSHORT, $search->getConfig('keyword_min')));
105  }
106  $queries = array($myts->addSlashes($query));
107  $queries_pattern[] = '~(' . $myts->addSlashes($query) . ')~sUi';
108  }
109 }
110 
111 switch ($action) {
112  case "results":
113  $module_handler = $xoops->getHandlerModule();
114  $criteria = new CriteriaCompo();
115  $criteria->add(new Criteria('dirname', "('" . implode("','", array_keys($available_plugins)) . "')", 'IN'));
116  $modules = $module_handler->getObjectsArray($criteria, true);
117  if (empty($mids) || !is_array($mids)) {
118  unset($mids);
119  $mids = array_keys($modules);
120  }
121  $xoops->header('module:search/search.tpl');
122  $nomatch = true;
123  $xoops->tpl()->assign('search', true);
124  $xoops->tpl()->assign('queries', $queries);
125  $xoops->tpl()->assign('ignored_words', sprintf(_MD_SEARCH_IGNOREDWORDS, $search->getConfig('keyword_min')));
126  $xoops->tpl()->assign('ignored_queries', $ignored_queries);
127 
128  $modules_result = array();
129  foreach ($mids as $mid) {
130  $mid = intval($mid);
131  /* @var $module XoopsModule */
132  $module = $modules[$mid];
133  /* @var $plugin SearchPluginInterface */
134  $plugin = \Xoops\Module\Plugin::getPlugin($module->getVar('dirname'), 'search');
135  $results = $plugin->search($queries, $andor, 5, 0, null);
136  $count = count($results);
137  $mid = $module->getVar('mid');
138 
139  $res = array();
140  if (is_array($results) && $count > 0) {
141  $nomatch = false;
142  $modules_result[$mid]['name'] = $module->getVar('name');
143  if (XoopsLoad::fileExists($image = $xoops->path('modules/' . $module->getVar('dirname') . '/icons/logo_large.png'))) {
144  $modules_result[$mid]['image'] = $xoops->url($image);
145  } else {
146  $modules_result[$mid]['image'] = $xoops->url('images/icons/posticon2.gif');
147  }
148  $res = array();
149  for ($i = 0; $i < $count; ++$i) {
150  if (!preg_match("/^http[s]*:\/\//i", $results[$i]['link'])) {
151  $res[$i]['link'] = $xoops->url('modules/' . $module->getVar('dirname') . '/' . $results[$i]['link']);
152  } else {
153  $res[$i]['link'] = $results[$i]['link'];
154  }
155  $res[$i]['title'] = $myts->htmlspecialchars($results[$i]['title']);
156  $res[$i]['title_highligh'] = preg_replace($queries_pattern, "<span class='searchHighlight'>$1</span>", $myts->htmlspecialchars($results[$i]['title']));
157  if (!empty($results[$i]['uid'])) {
158  $res[$i]['uid'] = intval($results[$i]['uid']);
159  $res[$i]['uname'] = XoopsUser::getUnameFromId($results[$i]['uid'], true);
160  }
161  $res[$i]['time'] = !empty($results[$i]['time']) ? XoopsLocale::formatTimestamp(intval($results[$i]['time'])) : "";
162  $res[$i]['content'] = empty($results[$i]['content']) ? "" : preg_replace($queries_pattern, "<span class='searchHighlight'>$1</span>", $results[$i]['content']);
163  }
164  if ($count >= 5) {
165  $search_url = $search->url('index.php?query=' . urlencode(stripslashes(implode(' ', $queries))));
166  $search_url .= "&mid={$mid}&action=showall&andor={$andor}";
167  $modules_result[$mid]['search_url'] = htmlspecialchars($search_url);
168  }
169  }
170  if (count($res) > 0) {
171  $modules_result[$mid]['result'] = $res;
172  }
173  }
174  unset($results);
175  unset($module);
176 
177  $xoops->tpl()->assign('modules', $modules_result);
178 
179  /* @var $formHandler SearchSearchForm */
180  $formHandler = $search->getForm(null, 'search');
181  $form = $formHandler->getSearchFrom($andor, $queries, $mids, $mid);
182  $form->display();
183  break;
184 
185  case "showall":
186  case 'showallbyuser':
187  $xoops->header('module:search/search.tpl');
188  $xoops->tpl()->assign('search', true);
189  $xoops->tpl()->assign('queries', $queries);
190  $xoops->tpl()->assign('ignored_words', sprintf(_MD_SEARCH_IGNOREDWORDS, $search->getConfig('keyword_min')));
191  $xoops->tpl()->assign('ignored_queries', $ignored_queries);
192 
193  $module_handler = $xoops->getHandlerModule();
194  $module = $xoops->getModuleById($mid);
195  /* @var $plugin SearchPluginInterface */
196  $plugin = \Xoops\Module\Plugin::getPlugin($module->getVar('dirname'), 'search');
197  $results = $plugin->search($queries, $andor, 20, $start, $uid);
198 
199  $modules_result[$mid]['name'] = $module->getVar('name');
200  $modules_result[$mid]['image'] = $xoops->url('modules/' . $module->getVar('dirname') . '/icons/logo_large.png');
201 
202  $count = count($results);
203  if (is_array($results) && $count > 0) {
204  $next_results = $plugin->search($queries, $andor, 1, $start + 20, $uid);
205  $next_count = count($next_results);
206  $has_next = false;
207  if (is_array($next_results) && $next_count == 1) {
208  $has_next = true;
209  }
210  $xoops->tpl()->assign('sr_showing', sprintf(_MD_SEARCH_SHOWING, $start + 1, $start + $count));
211  $res = array();
212  for ($i = 0; $i < $count; ++$i) {
213  if (isset($results[$i]['image']) && $results[$i]['image'] != "") {
214  $res[$i]['image'] = $xoops->url('modules/' . $module->getVar('dirname') . '/' . $results[$i]['image']);
215  } else {
216  $res[$i]['image'] = $xoops->url('images/icons/posticon2.gif');
217  }
218  if (!preg_match("/^http[s]*:\/\//i", $results[$i]['link'])) {
219  $res[$i]['link'] = $xoops->url('modules/' . $module->getVar('dirname') . '/' . $results[$i]['link']);
220  } else {
221  $res[$i]['link'] = $results[$i]['link'];
222  }
223  $res[$i]['title'] = $myts->htmlspecialchars($results[$i]['title']);
224  if (isset($queries_pattern)) {
225  $res[$i]['title_highligh'] = preg_replace($queries_pattern, "<span class='searchHighlight'>$1</span>", $myts->htmlspecialchars($results[$i]['title']));
226  } else {
227  $res[$i]['title_highligh'] = $myts->htmlspecialchars($results[$i]['title']);
228  }
229  if (!empty($results[$i]['uid'])) {
230  $res[$i]['uid'] = @intval($results[$i]['uid']);
231  $res[$i]['uname'] = XoopsUser::getUnameFromId($results[$i]['uid'], true);
232  }
233  $res[$i]['time'] = !empty($results[$i]['time']) ? " (" . XoopsLocale::formatTimestamp(intval($results[$i]['time'])) . ")" : "";
234  $res[$i]['content'] = empty($results[$i]['content']) ? "" : preg_replace($queries_pattern, "<span class='searchHighlight'>$1</span>", $results[$i]['content']);
235  }
236  if (count($res) > 0) {
237  $modules_result[$mid]['result'] = $res;
238  }
239 
240  $search_url = $search->url('index.php?query=' . urlencode(stripslashes(implode(' ', $queries))));
241  $search_url .= "&mid={$mid}&action={$action}&andor={$andor}";
242  if ($action == 'showallbyuser') {
243  $search_url .= "&uid={$uid}";
244  }
245  if ($start > 0) {
246  $prev = $start - 20;
247  $search_url_prev = $search_url . "&start={$prev}";
248  $modules_result[$mid]['prev'] = htmlspecialchars($search_url_prev);
249  }
250  if (false != $has_next) {
251  $next = $start + 20;
252  $search_url_next = $search_url . "&start={$next}";
253  $modules_result[$mid]['next'] = htmlspecialchars($search_url_next);
254  }
255  $xoops->tpl()->assign('modules', $modules_result);
256  }
257 
258  /* @var $formHandler SearchSearchForm */
259  $formHandler = $search->getForm(null, 'search');
260  $form = $formHandler->getSearchFrom($andor, $queries, $mids, $mid);
261  $form->display();
262  break;
263 }
264 $xoops->footer();
$mid
Definition: index.php:39
$start
Definition: index.php:41
if(empty($image_id)) $image
Definition: image.php:37
static formatTimestamp($time, $format= 'l', $timeoffset=null)
Definition: Abstract.php:289
$action
Definition: index.php:36
if(!$dbm->isConnectable()) $res
static getInstance()
Definition: Xoops.php:160
$myts
Definition: index.php:84
const _MD_SEARCH_IGNOREDWORDS
Definition: main.php:37
$query
Definition: index.php:37
$i
Definition: index.php:14
const _MD_SEARCH_PLZENTER
Definition: main.php:20
$queries
Definition: index.php:44
const _MD_SEARCH_SHOWING
Definition: main.php:24
$form
Definition: xoops_code.php:21
$search
Definition: index.php:28
$gperm_handler
exit
Definition: browse.php:104
defined('DS') or define('DS' DIRECTORY_SEPARATOR
Definition: common.php:41
$queries_pattern
Definition: index.php:83
$available_modules
Definition: index.php:65
static fileExists($file)
Definition: xoopsload.php:506
static getUnameFromId($userid, $usereal=0)
Definition: user.php:124
$criteria
Definition: index.php:30
$module
Definition: main.php:52
if(!$xoops->isUser()) $uid
Definition: index.php:31
$modules
Definition: userinfo.php:185
$andor
Definition: index.php:38
$module_handler
Definition: main.php:55
if($action== 'search') if($andor!="OR"&&$andor!="exact"&&$andor!="AND") $ignored_queries
Definition: index.php:82
$mids
Definition: index.php:42
$count
Definition: index.php:36
$available_plugins
Definition: index.php:66
const _MD_SEARCH_KEYTOOSHORT
Definition: main.php:34
if(file_exists($mainfile)) elseif(file_exists(__DIR__. '/install/index.php')) $xoops
Definition: index.php:36