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 | |||
12 | /** |
||
13 | * @copyright The XUUPS Project http://sourceforge.net/projects/xuups/ |
||
14 | * @license http://www.fsf.org/copyleft/gpl.html GNU public license |
||
15 | * @package Publisher |
||
16 | * @subpackage Action |
||
17 | * @since 1.0 |
||
18 | * @author trabis <[email protected]> |
||
19 | * @author Taiwen Jiang <[email protected]> |
||
20 | */ |
||
21 | |||
22 | use Xmf\Request; |
||
23 | use XoopsModules\Publisher; |
||
24 | use XoopsModules\Publisher\Constants; |
||
25 | |||
26 | require_once __DIR__ . '/header.php'; |
||
27 | xoops_loadLanguage('search'); |
||
28 | //Checking general permissions |
||
29 | /** @var \XoopsConfigHandler $configHandler */ |
||
30 | $configHandler = xoops_getHandler('config'); |
||
31 | $xoopsConfigSearch = $configHandler->getConfigsByCat(XOOPS_CONF_SEARCH); |
||
32 | if (empty($xoopsConfigSearch['enable_search'])) { |
||
33 | redirect_header(PUBLISHER_URL . '/index.php', 2, _NOPERM); |
||
34 | } |
||
35 | |||
36 | /** @var \XoopsModules\Publisher\Helper $helper */ |
||
37 | $helper = \XoopsModules\Publisher\Helper::getInstance(); |
||
38 | $groups = $GLOBALS['xoopsUser'] ? $GLOBALS['xoopsUser']->getGroups() : XOOPS_GROUP_ANONYMOUS; |
||
39 | $grouppermHandler = $helper->getHandler('GroupPerm'); |
||
40 | $module_id = $helper->getModule()->mid(); |
||
41 | |||
42 | //Checking permissions |
||
43 | if (!$helper->getConfig('perm_search') || !$grouppermHandler->checkRight('global', Constants::PUBLISHER_SEARCH, $groups, $module_id)) { |
||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
44 | redirect_header(PUBLISHER_URL, 2, _NOPERM); |
||
45 | } |
||
46 | |||
47 | $GLOBALS['xoopsConfig']['module_cache'][$module_id] = 0; |
||
48 | $GLOBALS['xoopsOption']['template_main'] = 'publisher_search.tpl'; |
||
49 | require_once $GLOBALS['xoops']->path('header.php'); |
||
50 | |||
51 | $module_info_search = $helper->getModule()->getInfo('search'); |
||
52 | require_once PUBLISHER_ROOT_PATH . '/' . $module_info_search['file']; |
||
53 | |||
54 | $limit = 10; //$helper->getConfig('idxcat_perpage'); |
||
55 | $uid = 0; |
||
56 | $queries = []; |
||
57 | $andor = Request::getString('andor', '', 'POST'); |
||
58 | $start = Request::getInt('start', 0, 'POST'); |
||
59 | $category = Request::getArray('category', [], 'POST'); |
||
60 | $username = Request::getString('uname', '', 'POST'); |
||
61 | $searchin = Request::getArray('searchin', [], 'POST'); |
||
62 | $sortby = Request::getString('sortby', '', 'POST'); |
||
63 | $term = Request::getString('term', '', 'POST'); |
||
64 | |||
65 | if (empty($category) || (is_array($category) && in_array('all', $category, true))) { |
||
66 | $category = []; |
||
67 | } else { |
||
68 | $category = !is_array($category) ? explode(',', $category) : $category; |
||
69 | $category = array_map('intval', $category); |
||
70 | } |
||
71 | |||
72 | $andor = in_array(mb_strtoupper($andor), ['OR', 'AND', 'EXACT'], true) ? mb_strtoupper($andor) : 'OR'; |
||
73 | $sortby = in_array(mb_strtolower($sortby), ['itemid', 'datesub', 'title', 'categoryid'], true) ? mb_strtolower($sortby) : 'itemid'; |
||
74 | |||
75 | if ($term && 'none' !== Request::getString('submit', 'none', 'POST')) { |
||
76 | $next_search['category'] = implode(',', $category); |
||
77 | $next_search['andor'] = $andor; |
||
78 | $next_search['term'] = $term; |
||
79 | $query = trim($term); |
||
80 | |||
81 | if ('EXACT' !== $andor) { |
||
82 | $ignored_queries = []; // holds keywords that are shorter than allowed minimum length |
||
83 | $temp_queries = preg_split("/[\s,]+/", $query); |
||
84 | foreach ($temp_queries as $q) { |
||
85 | $q = trim($q); |
||
86 | if (mb_strlen($q) >= $xoopsConfigSearch['keyword_min']) { |
||
87 | $queries[] = $myts->addSlashes($q); |
||
88 | } else { |
||
89 | $ignored_queries[] = $myts->addSlashes($q); |
||
90 | } |
||
91 | } |
||
92 | // unset($q); |
||
93 | if (0 == count($queries)) { |
||
94 | redirect_header(PUBLISHER_URL . '/search.php', 2, sprintf(_SR_KEYTOOSHORT, $xoopsConfigSearch['keyword_min'])); |
||
95 | } |
||
96 | } else { |
||
97 | if (mb_strlen($query) < $xoopsConfigSearch['keyword_min']) { |
||
98 | redirect_header(PUBLISHER_URL . '/search.php', 2, sprintf(_SR_KEYTOOSHORT, $xoopsConfigSearch['keyword_min'])); |
||
99 | } |
||
100 | $queries = [$myts->addSlashes($query)]; |
||
101 | } |
||
102 | |||
103 | $uname_required = false; |
||
104 | $search_username = trim($username); |
||
105 | $next_search['uname'] = $search_username; |
||
106 | if (!empty($search_username)) { |
||
107 | $uname_required = true; |
||
108 | $search_username = $myts->addSlashes($search_username); |
||
109 | if (!$result = $GLOBALS['xoopsDB']->query('SELECT uid FROM ' . $GLOBALS['xoopsDB']->prefix('users') . ' WHERE uname LIKE ' . $GLOBALS['xoopsDB']->quoteString("%$search_username%"))) { |
||
110 | redirect_header(PUBLISHER_URL . '/search.php', 1, _CO_PUBLISHER_ERROR); |
||
111 | } |
||
112 | $uid = []; |
||
113 | while (false !== ($row = $GLOBALS['xoopsDB']->fetchArray($result))) { |
||
114 | $uid[] = $row['uid']; |
||
115 | } |
||
116 | } else { |
||
117 | $uid = 0; |
||
118 | } |
||
119 | |||
120 | $next_search['sortby'] = $sortby; |
||
121 | $next_search['searchin'] = implode('|', $searchin); |
||
122 | |||
123 | $extra = ''; |
||
124 | if (!empty($time)) { |
||
125 | $extra = ''; |
||
126 | } |
||
127 | |||
128 | if ($uname_required && (!$uid || (is_array($uid) && count($uid) < 1))) { |
||
129 | $results = []; |
||
130 | } else { |
||
131 | $results = $module_info_search['func']($queries, $andor, $limit, $start, $uid, $category, $sortby, $searchin, $extra); |
||
132 | } |
||
133 | |||
134 | if (count($results) < 1) { |
||
135 | $results[] = ['text' => _SR_NOMATCH]; |
||
136 | } |
||
137 | |||
138 | $xoopsTpl->assign('results', $results); |
||
139 | |||
140 | if (count($next_search) > 0) { |
||
141 | $items = []; |
||
142 | foreach ($next_search as $para => $val) { |
||
143 | if (!empty($val)) { |
||
144 | $items[] = "{$para}={$val}"; |
||
145 | } |
||
146 | } |
||
147 | if (count($items) > 0) { |
||
148 | $paras = implode('&', $items); |
||
149 | } |
||
150 | unset($next_search, $para, $val, $items); |
||
151 | } |
||
152 | $search_url = PUBLISHER_URL . '/search.php?' . $paras; |
||
153 | |||
154 | if (count($results)) { |
||
155 | $next = $start + $limit; |
||
156 | $queries = implode(',', $queries); |
||
157 | $search_url_next = $search_url . "&start={$next}"; |
||
158 | $search_next = '<a href="' . htmlspecialchars($search_url_next, ENT_QUOTES | ENT_HTML5) . '">' . _SR_NEXT . '</a>'; |
||
159 | $xoopsTpl->assign('search_next', $search_next); |
||
160 | } |
||
161 | if ($start > 0) { |
||
162 | $prev = $start - $limit; |
||
163 | $search_url_prev = $search_url . "&start={$prev}"; |
||
164 | $search_prev = '<a href="' . htmlspecialchars($search_url_prev, ENT_QUOTES | ENT_HTML5) . '">' . _SR_PREVIOUS . '</a>'; |
||
165 | $xoopsTpl->assign('search_prev', $search_prev); |
||
166 | } |
||
167 | |||
168 | unset($results); |
||
169 | $search_info = _SR_KEYWORDS . ': ' . $myts->htmlSpecialChars($term); |
||
170 | if ($uname_required) { |
||
171 | if ($search_info) { |
||
172 | $search_info .= '<br>'; |
||
173 | } |
||
174 | $search_info .= _CO_PUBLISHER_UID . ': ' . $myts->htmlSpecialChars($search_username); |
||
175 | } |
||
176 | $xoopsTpl->assign('search_info', $search_info); |
||
177 | } |
||
178 | |||
179 | /* type */ |
||
180 | $typeSelect = '<select name="andor">'; |
||
181 | $typeSelect .= '<option value="OR"'; |
||
182 | if ('OR' === $andor) { |
||
183 | $typeSelect .= ' selected="selected"'; |
||
184 | } |
||
185 | $typeSelect .= '>' . _SR_ANY . '</option>'; |
||
186 | $typeSelect .= '<option value="AND"'; |
||
187 | if ('AND' === $andor) { |
||
188 | $typeSelect .= ' selected="selected"'; |
||
189 | } |
||
190 | $typeSelect .= '>' . _SR_ALL . '</option>'; |
||
191 | $typeSelect .= '<option value="EXACT"'; |
||
192 | if ('EXACT' === $andor) { |
||
193 | $typeSelect .= ' selected="selected"'; |
||
194 | } |
||
195 | $typeSelect .= '>' . _SR_EXACT . '</option>'; |
||
196 | $typeSelect .= '</select>'; |
||
197 | |||
198 | /* category */ |
||
199 | /** @var Publisher\CategoryHandler $categoryHandler */ |
||
200 | $categoryHandler = $helper->getHandler('Category'); |
||
201 | $categories = $categoryHandler->getCategoriesForSearch(); |
||
202 | |||
203 | $categorySelect = '<select name="category[]" size="5" multiple="multiple">'; |
||
204 | $categorySelect .= '<option value="all"'; |
||
205 | if (empty($category) || 0 == count($category)) { |
||
206 | $categorySelect .= 'selected="selected"'; |
||
207 | } |
||
208 | $categorySelect .= '>' . _ALL . '</option>'; |
||
209 | foreach ($categories as $id => $cat) { |
||
210 | $categorySelect .= '<option value="' . $id . '"'; |
||
211 | if (in_array($id, $category, true)) { |
||
212 | $categorySelect .= 'selected="selected"'; |
||
213 | } |
||
214 | $categorySelect .= '>' . $cat . '</option>'; |
||
215 | } |
||
216 | unset($id, $cat); |
||
217 | $categorySelect .= '</select>'; |
||
218 | |||
219 | /* scope */ |
||
220 | $searchSelect = ''; |
||
221 | $searchSelect .= '<input type="checkbox" name="searchin[]" value="title"'; |
||
222 | if (in_array('title', $searchin, true)) { |
||
223 | $searchSelect .= ' checked'; |
||
224 | } |
||
225 | $searchSelect .= '>' . _CO_PUBLISHER_TITLE . ' '; |
||
226 | $searchSelect .= '<input type="checkbox" name="searchin[]" value="subtitle"'; |
||
227 | if (in_array('subtitle', $searchin, true)) { |
||
228 | $searchSelect .= ' checked'; |
||
229 | } |
||
230 | $searchSelect .= '>' . _CO_PUBLISHER_SUBTITLE . ' '; |
||
231 | $searchSelect .= '<input type="checkbox" name="searchin[]" value="summary"'; |
||
232 | if (in_array('summary', $searchin, true)) { |
||
233 | $searchSelect .= ' checked'; |
||
234 | } |
||
235 | $searchSelect .= '>' . _CO_PUBLISHER_SUMMARY . ' '; |
||
236 | $searchSelect .= '<input type="checkbox" name="searchin[]" value="text"'; |
||
237 | if (in_array('body', $searchin, true)) { |
||
238 | $searchSelect .= ' checked'; |
||
239 | } |
||
240 | $searchSelect .= '>' . _CO_PUBLISHER_BODY . ' '; |
||
241 | $searchSelect .= '<input type="checkbox" name="searchin[]" value="keywords"'; |
||
242 | if (in_array('meta_keywords', $searchin, true)) { |
||
243 | $searchSelect .= ' checked'; |
||
244 | } |
||
245 | $searchSelect .= '>' . _CO_PUBLISHER_ITEM_META_KEYWORDS . ' '; |
||
246 | $searchSelect .= '<input type="checkbox" name="searchin[]" value="all"'; |
||
247 | if (empty($searchin) || in_array('all', $searchin, true)) { |
||
248 | $searchSelect .= ' checked'; |
||
249 | } |
||
250 | $searchSelect .= '>' . _ALL . ' '; |
||
251 | |||
252 | /* sortby */ |
||
253 | $sortbySelect = '<select name="sortby">'; |
||
254 | $sortbySelect .= '<option value="itemid"'; |
||
255 | if ('itemid' === $sortby || empty($sortby)) { |
||
256 | $sortbySelect .= ' selected="selected"'; |
||
257 | } |
||
258 | $sortbySelect .= '>' . _NONE . '</option>'; |
||
259 | $sortbySelect .= '<option value="datesub"'; |
||
260 | if ('datesub' === $sortby) { |
||
261 | $sortbySelect .= ' selected="selected"'; |
||
262 | } |
||
263 | $sortbySelect .= '>' . _CO_PUBLISHER_DATESUB . '</option>'; |
||
264 | $sortbySelect .= '<option value="title"'; |
||
265 | if ('title' === $sortby) { |
||
266 | $sortbySelect .= ' selected="selected"'; |
||
267 | } |
||
268 | $sortbySelect .= '>' . _CO_PUBLISHER_TITLE . '</option>'; |
||
269 | $sortbySelect .= '<option value="categoryid"'; |
||
270 | if ('categoryid' === $sortby) { |
||
271 | $sortbySelect .= ' selected="selected"'; |
||
272 | } |
||
273 | $sortbySelect .= '>' . _CO_PUBLISHER_CATEGORY . '</option>'; |
||
274 | $sortbySelect .= '</select>'; |
||
275 | |||
276 | $xoopsTpl->assign('type_select', $typeSelect); |
||
277 | $xoopsTpl->assign('searchin_select', $searchSelect); |
||
278 | $xoopsTpl->assign('category_select', $categorySelect); |
||
279 | $xoopsTpl->assign('sortby_select', $sortbySelect); |
||
280 | $xoopsTpl->assign('search_term', htmlspecialchars($term, ENT_QUOTES)); |
||
281 | $xoopsTpl->assign('search_user', $username); |
||
282 | |||
283 | $xoopsTpl->assign('modulename', $helper->getModule()->name()); |
||
284 | $xoopsTpl->assign('module_dirname', $helper->getDirname()); |
||
285 | |||
286 | if ($xoopsConfigSearch['keyword_min'] > 0) { |
||
287 | $xoopsTpl->assign('search_rule', sprintf(_SR_KEYIGNORE, $xoopsConfigSearch['keyword_min'])); |
||
288 | } |
||
289 | |||
290 | require $GLOBALS['xoops']->path('footer.php'); |
||
291 |