r887: Removing all the annoying calls to intval() in place of ISSO's cleaning framework
[bugdar.git] / search.php
1 <?php
2 /*=====================================================================*\
3 || ###################################################################
4 || # Bugdar [#]version[#]
5 || # Copyright ©2002-[#]year[#] Iris Studios, Inc.
6 || #
7 || # This program is free software; you can redistribute it and/or modify
8 || # it under the terms of the GNU General Public License as published by
9 || # the Free Software Foundation; version [#]gpl[#] of the License.
10 || #
11 || # This program is distributed in the hope that it will be useful, but
12 || # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 || # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 || # more details.
15 || #
16 || # You should have received a copy of the GNU General Public License along
17 || # with this program; if not, write to the Free Software Foundation, Inc.,
18 || # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
19 || ###################################################################
20 \*=====================================================================*/
21
22 $fetchtemplates = array(
23 'search',
24 'search_results',
25 'trackerhome_bits'
26 );
27
28 define('SVN', '$Id$');
29
30 $focus['search'] = 'focus';
31
32 require_once('./global.php');
33 require_once('./includes/functions_product.php');
34
35 if (!can_perform('cansearch'))
36 {
37 $message->error_permission();
38 }
39
40 define('MODE_ANY', ($bugsys->in['mode'] == 1));
41 define('MODE_ALL', ($bugsys->in['mode'] == 2));
42 define('MODE_RAW', ($bugsys->in['mode'] == 3));
43
44 $var = $db->query_first("SHOW VARIABLES LIKE 'ft_min_word_len'");
45 define('SEARCH_WORD_MIN', $var['Value']);
46
47 $show['search'] = true;
48
49 // ###################################################################
50
51 if (empty($_REQUEST['do']))
52 {
53 $_REQUEST['do'] = 'search';
54 }
55
56 // ###################################################################
57
58 if ($_REQUEST['do'] == 'process')
59 {
60 // -------------------------------------------------------------------
61 // handle keywords
62 if ($bugsys->in['summary'])
63 {
64 $keywords = preg_split('#\s+#', $bugsys->in['summary']);
65
66 // #*# need to have some str to bool conversions
67
68 foreach ($keywords AS $word)
69 {
70 if (strlen($word) < SEARCH_WORD_MIN)
71 {
72 continue;
73 }
74
75 if (MODE_ALL)
76 {
77 $querybuild['text'] .= " +$word";
78 }
79 else
80 {
81 $querybuild['text'] .= " $word";
82 }
83
84 if (!preg_match('#-(.+?)#', trim($word)))
85 {
86 $hilight .= " $word";
87 }
88 }
89
90 $hilight = preg_replace('#[^0-9a-zA-Z_ ]#', '', $hilight);
91 $hilight = trim($hilight);
92 $hilight = preg_replace('#\s#', '+', $hilight);
93
94 $temp = trim($querybuild['text']);
95
96 if (MODE_ALL OR MODE_RAW)
97 {
98 $bool_flag = ' IN BOOLEAN MODE';
99 }
100
101 $querybuild['text'] = "AND\n\t\t\t(\n\t\t\t\tMATCH (bug.summary) AGAINST ('$temp'$bool_flag)\n\t\t\t\tOR MATCH (comment.comment) AGAINST ('$temp'$bool_flag)\n\t\t\t)";
102 }
103
104 // -------------------------------------------------------------------
105 // reporter
106 if ($bugsys->in['reporter'])
107 {
108 // force email or name?? make a distinction?
109 // more elegant way to do this? probably
110 $user = $db->query_first("SELECT * FROM " . TABLE_PREFIX . "user WHERE email LIKE '%" . str_replace('%', '\%', $bugsys->in['reporter']) . "' OR displayname LIKE '%" . str_replace('%', '\%', $bugsys->in['reporter']) . "%'");
111 if ($user['userid'])
112 {
113 $querybuild['reporter'] = "AND bug.userid = $user[userid] OR comment.userid = $user[userid]";
114 }
115 }
116
117 // -------------------------------------------------------------------
118 // product/component/version stuff
119 if (is_array($bugsys->in['pcv_select']))
120 {
121 foreach ($bugsys->in['pcv_select'] AS $pcv)
122 {
123 $pcv = parse_pcv_select($pcv, true);
124 $products[] = $pcv['product'];
125 $components[] = $pcv['component'];
126 $versions[] = $pcv['version'];
127 }
128 $querybuild['pcv'] = "AND bug.productid IN (" . implode(',', $products) . ") AND bug.componentid IN (" . implode(',', $components) . ") AND bug.versionid IN (" . implode(',', $versions) . ")";
129 }
130
131 // -------------------------------------------------------------------
132 // severity, priority, status, resolution, assignedto
133
134 // severity
135 if ($bugsys->in['severity'])
136 {
137 $bugsys->input_clean('severity', TYPE_UINT);
138 $querybuild['severity'] = "AND bug.severity IN (" . implode(',', $bugsys->in['severity']) . ")";
139 }
140
141 // priority
142 if ($bugsys->in['priority'])
143 {
144 $bugsys->input_clean('priority', TYPE_UINT);
145 $querybuild['priority'] = "AND bug.priority IN (" . implode(',', $bugsys->in['priority']) . ")";
146 }
147
148 // status
149 if ($bugsys->in['status'])
150 {
151 $bugsys->input_clean('status', TYPE_UINT);
152 $querybuild['status'] = "AND bug.status IN (" . implode(',', $bugsys->in['status']) . ")";
153 }
154
155 // resolution
156 if ($bugsys->in['resolution'])
157 {
158 $bugsys->input_clean('resolution', TYPE_UINT);
159 $querybuild['resolution'] = "AND bug.resolution IN (" . implode(',', $bugsys->in['resolution']) . ")";
160 }
161
162 // assignment
163 if ($bugsys->in['assignedto'])
164 {
165 $bugsys->input_clean('assignedto', TYPE_UINT);
166 $querybuild['assignedto'] = "AND bug.assignedto IN (" . implode(',', $bugsys->in['assignedto']) . ")";
167 }
168
169 // -------------------------------------------------------------------
170 // date
171 if ($bugsys->in['date'])
172 {
173 // now - (seconds/day * number of days)
174 $dateline = time() - ($bugsys->input_clean('date', TYPE_INT)) * 3600);
175 $querybuild['date'] = "AND bug.dateline >= $dateline";
176 }
177
178 // -------------------------------------------------------------------
179 // favourites
180 if ($bugsys->in['favourite'] AND $bugsys->userinfo['userid'])
181 {
182 $favourites = $db->query("SELECT * FROM " . TABLE_PREFIX . "favourite WHERE userid = " . $bugsys->userinfo['userid']);
183 while ($favourite = $db->fetch_array($favourites))
184 {
185 $ids[] = $favourite['bugid'];
186 }
187 $querybuild['favourites'] = "AND bug.bugid IN (" . implode(', ', $ids) . ")";
188 }
189
190 // -------------------------------------------------------------------
191 // sort by
192 $sortby = array('bugid', 'severity', 'priority', 'status', 'resolution', 'dateline');
193 $orderby = array('ASC', 'DESC');
194 $bugsys->in['orderby'] = strtoupper($bugsys->in['orderby']);
195 if (in_array($bugsys->in['sortby'], $sortby) AND in_array($bugsys->in['orderby'], $orderby))
196 {
197 $sortclause = "ORDER BY " . $bugsys->in['sortby'] . ' ' . $bugsys->in['orderby'];
198 }
199 else if ($bugsys->in['sortby'] == 'relevance')
200 {
201 $sortclause = '';
202 }
203 else
204 {
205 $sortclause = '';
206 }
207
208 // -------------------------------------------------------------------
209 // custom fields
210 $fields_fetch = $bugsys->db->query("
211 SELECT bugfield.*
212 FROM " . TABLE_PREFIX . "bugfield AS bugfield
213 LEFT JOIN " . TABLE_PREFIX . "bugfieldpermission AS permission
214 ON (bugfield.fieldid = permission.fieldid)
215 WHERE permission.mask <> 0
216 AND permission.usergroupid = {$bugsys->userinfo['usergroupid']}
217 AND bugfield.cansearch = 1"
218 );
219 while ($field = $bugsys->db->fetch_array($fields_fetch))
220 {
221 if (!empty($bugsys->in["field$field[fieldid]"]) OR ($field['type'] == 'select_single' AND isset($bugsys->in["field$field[fieldid]"])))
222 {
223 if ($field['type'] == 'input_checkbox')
224 {
225 $querybuild[] = "AND bugfieldvalue.field$field[fieldid] = " . (($bugsys->in["field$field[fieldid]"] == 1) ? 0 : 1);
226 }
227 else if ($field['type'] == 'input_text')
228 {
229 $querybuild[] = "AND bugfieldvalue.field$field[fieldid] LIKE '%" . $bugsys->in["field$field[fieldid]"] . "%'";
230 }
231 else if ($field['type'] == 'select_single' AND $bugsys->in["field$field[fieldid]"] != -1)
232 {
233 $temp = unserialize($field['selects']);
234 $querybuild[] = "AND bugfieldvalue.field$field[fieldid] = '" . trim($temp[ intval($bugsys->in["field$field[fieldid]"]) ]) . "'";
235 }
236 }
237 }
238
239 // -------------------------------------------------------------------
240 // have to search something
241 if (count($querybuild) < 1)
242 {
243 $message->error(sprintf($lang->string('You have to enter some criteria to search for. Node that words less than %1$d characters are ignored by the search engine (and some other very common words, too).'), SEARCH_WORD_MIN));
244 }
245
246 // -------------------------------------------------------------------
247 // do the search
248 $query = "
249 SELECT bug.*, comment.commentid,
250 user1.displayname AS firstreport,
251 user2.displayname AS lastpost
252 FROM " . TABLE_PREFIX . "bug AS bug
253 LEFT JOIN " . TABLE_PREFIX . "comment AS comment
254 ON (bug.bugid = comment.bugid)
255 LEFT JOIN " . TABLE_PREFIX . "user AS user1
256 ON (bug.userid = user1.userid)
257 LEFT JOIN " . TABLE_PREFIX . "user AS user2
258 ON (bug.lastpostby = user2.userid)
259 LEFT JOIN " . TABLE_PREFIX . "bugvaluefill AS bugfieldvalue
260 ON (bug.bugid = bugfieldvalue.bugid)
261 WHERE bug.bugid <> 0
262 AND bug.productid IN (#<'ONBITS:VIEW'>#)
263 AND (!bug.hidden OR (bug.hidden AND bug.productid IN (#<'ONBITS:HIDDEN'>#)))
264 " . implode("\n\t\t", $querybuild) . "
265 GROUP BY bug.bugid
266 $sortclause";
267
268 $runquery = str_replace(array("#<'ONBITS:VIEW'>#", "#<'ONBITS:HIDDEN'>#"), array(fetch_on_bits('canviewbugs'), fetch_on_bits('canviewhidden')), $query);
269
270 $search = $db->query($runquery);
271
272 $numrows = $db->num_rows($search);
273
274 if ($numrows < 1)
275 {
276 $message->error($lang->string('No search results were returned that matched your criteria.'));
277 }
278
279 while ($result = $db->fetch_array($search))
280 {
281 $ids[] = $result['bugid'];
282 $results[] = $result;
283 }
284
285 if ($bugsys->userinfo['userid'])
286 {
287 $db->query("
288 REPLACE INTO " . TABLE_PREFIX . "search
289 (userid, dateline, query, ids, orderby, hilight)
290 VALUES
291 (" . $bugsys->userinfo['userid'] . ",
292 " . TIMENOW . ", '" . $bugsys->escape($query) . "',
293 '" . implode(',', $ids) . "', '" . $bugsys->escape($sortclause) . "',
294 '" . $bugsys->escape($hilight) . "'
295 )"
296 );
297 }
298
299 $justprocess = true;
300
301 $_REQUEST['do'] = 'results';
302 }
303
304 // ###################################################################
305
306 if ($_REQUEST['do'] == 'search')
307 {
308 if ($bugsys->userinfo['userid'] AND !$bugsys->in['new'])
309 {
310 if ($cachedsearch = $db->query_first("SELECT * FROM " . TABLE_PREFIX . "search WHERE userid = " . $bugsys->userinfo['userid']))
311 {
312 $_REQUEST['do'] = 'results';
313 }
314 else
315 {
316 $newsearch = true;
317 }
318 }
319 else
320 {
321 $newsearch = true;
322 }
323
324 if ($newsearch)
325 {
326 $pcv_select = construct_pcv_select();
327
328 // -------------------------------------------------------------------
329 // custom fields
330 $fields = construct_custom_fields(null, true);
331 $i = 0;
332 foreach ($fields AS $field)
333 {
334 if ($i % 2 == 0)
335 {
336 $customfields['left'] .= $field;
337 }
338 else
339 {
340 $customfields['right'] .= $field;
341 }
342 $i++;
343 }
344
345 // -------------------------------------------------------------------
346 // built-in fields
347 $select['severity'] = construct_datastore_select('severity', 'severity', 'severityid');
348 $select['priority'] = construct_datastore_select('priority', 'priority', 'priorityid');
349 $select['status'] = construct_datastore_select('status', 'status', 'statusid');
350 $select['resolution'] = construct_datastore_select('resolution', 'resolution', 'resolutionid');
351
352 $select['dev'] = '';
353 foreach ($bugsys->datastore['assignto'] AS $dev)
354 {
355 $value = $dev['userid'];
356 $label = construct_user_display($dev, false);
357 eval('$select[dev] .= "' . $template->fetch('selectoption') . '";');
358 }
359
360 eval('$template->flush("' . $template->fetch('search') . '");');
361 }
362 }
363
364 // ###################################################################
365
366 if ($_REQUEST['do'] == 'results')
367 {
368 $show['cached'] = false;
369 if ($bugsys->userinfo['userid'] AND !$justprocess)
370 {
371 $search = $cachedsearch;
372 if ($search['dateline'] < TIMENOW - 900 OR $bugsys->in['rerun'])
373 {
374 $search = $db->query(str_replace(array("#<'ONBITS:VIEW'>#", "#<'ONBITS:HIDDEN'>#"), array(fetch_on_bits('canviewbugs'), fetch_on_bits('canviewhidden')), $search['query']));
375 while ($bug = $db->fetch_array($search))
376 {
377 $ids[] = $bug['bugid'];
378 $results[] = $bug;
379 }
380 $db->query("UPDATE " . TABLE_PREFIX . "search SET ids = '" . implode(',', $ids) . "', dateline = " . TIMENOW . " WHERE userid = " . $bugsys->userinfo['userid']);
381 }
382 else
383 {
384 $search = $db->query("
385 SELECT bug.*, user1.displayname AS firstreport,
386 user2.displayname AS lastpost
387 FROM " . TABLE_PREFIX . "bug AS bug
388 LEFT JOIN " . TABLE_PREFIX . "user AS user1
389 ON (bug.userid = user1.userid)
390 LEFT JOIN " . TABLE_PREFIX . "user AS user2
391 ON (bug.lastpostby = user2.userid)
392 WHERE bug.bugid IN ($search[ids])
393 $search[orderby]"
394 );
395 while ($bug = $db->fetch_array($search))
396 {
397 $results[] = $bug;
398 }
399 }
400 $show['cached'] = true;
401 $hilight = $search['hilight'];
402 }
403
404 foreach ($results AS $bug)
405 {
406 $funct->exec_swap_bg($stylevar['alt_colour'], '');
407 $bug['bgcolour'] = ($bugsys->userinfo['showcolours'] ? $bugsys->datastore['status']["$bug[status]"]['color'] : $funct->bgcolour);
408 $bug['product'] = $bugsys->datastore['product']["$bug[productid]"]['title'];
409 $bug['version'] = $bugsys->datastore['version']["$bug[versionid]"]['version'];
410 $bug['status'] = $bugsys->datastore['status']["$bug[status]"]['status'];
411 $bug['resolution'] = $bugsys->datastore['resolution']["$bug[resolution]"]['resolution'];
412 $bug['priority'] = $bugsys->datastore['priority']["$bug[priority]"]['priority'];
413 $bug['severity'] = $bugsys->datastore['severity']["$bug[severity]"]['severity'];
414 $bug['lastposttime'] = (($bug['hiddendisplay']) ? $bug['hiddenlastposttime'] : $bug['lastposttime']);
415 $bug['lastpost'] = (($bug['hiddendisplay']) ? $bug['hiddenlastpost'] : $bug['lastpost']);
416 $bug['lastposttime'] = $datef->format($bugsys->options['dateformat'], $bug['lastposttime']);
417 $bug['urladd'] = "&amp;hilight=$hilight";
418 eval('$bugs .= "' . $template->fetch('trackerhome_bits') . '";');
419 }
420
421 eval('$template->flush("' . $template->fetch('search_results') . '");');
422 }
423
424 /*=====================================================================*\
425 || ###################################################################
426 || # $HeadURL$
427 || # $Id$
428 || ###################################################################
429 \*=====================================================================*/
430 ?>