2 /*=====================================================================*\
3 || ###################################################################
4 || # Bugdar [#]version[#]
5 || # Copyright 2002-[#]year[#] Blue Static
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.
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
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 \*=====================================================================*/
22 $fetchtemplates = array(
32 define('SVN', '$Id$');
34 $focus['search'] = 'focus';
36 require_once('./global.php');
37 require_once('./includes/functions_product.php');
38 require_once('./includes/class_sort.php');
39 require_once('./includes/api_bug.php');
40 require_once('./includes/class_api_error.php');
42 APIError(array($message, 'error'));
44 if (!can_perform('cansearch'))
46 $message->error_permission();
49 define('MODE_ANY', ($bugsys->in
['mode'] == 1));
50 define('MODE_ALL', ($bugsys->in
['mode'] == 2));
51 define('MODE_RAW', ($bugsys->in
['mode'] == 3));
53 $var = $db->query_first("SHOW VARIABLES LIKE 'ft_min_word_len'");
54 define('SEARCH_WORD_MIN', $var['Value']);
56 $db->query("DELETE FROM " . TABLE_PREFIX
. "search WHERE userid = 0 AND dateline < " . (TIMENOW
- 3600));
58 $show['search'] = true
;
60 // ###################################################################
62 if (empty($_REQUEST['do']))
64 $_REQUEST['do'] = 'search';
67 // ###################################################################
69 if ($_REQUEST['do'] == 'process')
71 // -------------------------------------------------------------------
73 if ($bugsys->in
['summary'])
75 $keywords = preg_split('#\s+#', $bugsys->in
['summary']);
77 // TODO - need to have some str to bool conversions
79 foreach ($keywords AS $word)
81 if (strlen($word) < SEARCH_WORD_MIN
)
88 $querybuild['text'] .= " +$word";
92 $querybuild['text'] .= " $word";
95 if (!preg_match('#-(.+?)#', trim($word)))
101 $hilight = preg_replace('#[^0-9a-zA-Z_ ]#', '', $hilight);
102 $hilight = trim($hilight);
103 $hilight = preg_replace('#\s#', '+', $hilight);
105 $temp = trim($querybuild['text']);
107 if (MODE_ALL
OR MODE_RAW
)
109 $bool_flag = ' IN BOOLEAN MODE';
112 $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)";
115 // -------------------------------------------------------------------
117 if ($bugsys->in
['reporter'])
119 // force email or name?? make a distinction?
120 // more elegant way to do this? probably
121 $user = $db->query_first("SELECT * FROM " . TABLE_PREFIX
. "user WHERE email LIKE '%" . str_replace('%', '\%', $bugsys->input_escape('reporter')) . "' OR displayname LIKE '%" . str_replace('%', '\%', $bugsys->input_escape('reporter')) . "%'");
124 $querybuild['reporter'] = "AND bug.userid = $user[userid] OR comment.userid = $user[userid]";
128 // -------------------------------------------------------------------
129 // product/component/version stuff
130 if (is_array($bugsys->in
['product']))
132 foreach ($bugsys->in
['product'] AS $prod)
134 $product = explode(',', $prod);
135 $product = $bugsys->clean($product, TYPE_UINT
);
136 $products[] = $product[0];
137 $components[] = $product[1];
138 $versions[] = $product[2];
140 $querybuild['pcv'] = "AND bug.product IN (" . implode(',', $products) . ") AND bug.component IN (" . implode(',', $components) . ") AND bug.version IN (" . implode(',', $versions) . ")";
143 // -------------------------------------------------------------------
144 // severity, priority, status, resolution, assignedto
147 if ($bugsys->in
['severity'])
149 $bugsys->input_clean('severity', TYPE_UINT
);
150 $querybuild['severity'] = "AND bug.severity IN (" . implode(',', $bugsys->in
['severity']) . ")";
154 if ($bugsys->in
['priority'])
156 $bugsys->input_clean('priority', TYPE_UINT
);
157 $querybuild['priority'] = "AND bug.priority IN (" . implode(',', $bugsys->in
['priority']) . ")";
161 if ($bugsys->in
['status'])
163 $bugsys->input_clean('status', TYPE_UINT
);
164 $querybuild['status'] = "AND bug.status IN (" . implode(',', $bugsys->in
['status']) . ")";
168 if ($bugsys->in
['resolution'])
170 $bugsys->input_clean('resolution', TYPE_UINT
);
171 $querybuild['resolution'] = "AND bug.resolution IN (" . implode(',', $bugsys->in
['resolution']) . ")";
175 if ($bugsys->in
['assignedto'])
177 $bugsys->input_clean('assignedto', TYPE_UINT
);
178 $querybuild['assignedto'] = "AND bug.assignedto IN (" . implode(',', $bugsys->in
['assignedto']) . ")";
181 // -------------------------------------------------------------------
183 if ($bugsys->in
['date'])
185 // now - (seconds/day * number of days)
186 $dateline = time() - ($bugsys->input_clean('date', TYPE_INT
) * 3600);
187 $querybuild['date'] = "AND bug.dateline >= $dateline";
190 // -------------------------------------------------------------------
192 $bugsys->input_clean('favorite', TYPE_INT
);
193 if ($bugsys->in
['favorite'] != 0 AND $bugsys->userinfo
['userid'])
195 $favorites = $db->query("SELECT * FROM " . TABLE_PREFIX
. "favorite WHERE userid = " . $bugsys->userinfo
['userid']);
196 while ($favorite = $db->fetch_array($favorites))
198 $ids[] = $favorite['bugid'];
200 $querybuild['favorites'] = "AND bug.bugid " . ($bugsys->in
['favorite'] > 0 ?
"IN" : "NOT IN") . " (" . implode(', ', $ids) . ")";
203 // -------------------------------------------------------------------
205 $sortby = array('bugid', 'severity', 'priority', 'status', 'resolution', 'dateline');
206 $orderby = array('ASC', 'DESC');
207 $bugsys->in
['orderby'] = strtoupper($bugsys->in
['orderby']);
208 if (in_array($bugsys->in
['sortby'], $sortby) AND in_array($bugsys->in
['orderby'], $orderby))
210 $sortclause = "ORDER BY " . $bugsys->in
['sortby'] . ' ' . $bugsys->in
['orderby'];
212 else if ($bugsys->in
['sortby'] == 'relevance')
221 // -------------------------------------------------------------------
223 $fields_fetch = $bugsys->db
->query("
225 FROM " . TABLE_PREFIX
. "bugfield AS bugfield
226 LEFT JOIN " . TABLE_PREFIX
. "bugfieldpermission AS permission
227 ON (bugfield.fieldid = permission.fieldid)
228 WHERE permission.mask <> 0
229 AND permission.usergroupid = {$bugsys->userinfo['usergroupid']}
230 AND bugfield.cansearch = 1"
232 while ($field = $bugsys->db
->fetch_array($fields_fetch))
234 if (!empty($bugsys->in
["field$field[fieldid]"]) OR ($field['type'] == 'select_single' AND isset($bugsys->in
["field$field[fieldid]"])))
236 if ($field['type'] == 'input_checkbox' AND $bugsys->input_clean("field$field[fieldid]", TYPE_INT
) != 0)
238 $querybuild[] = "AND bugfieldvalue.field$field[fieldid] = " . (($bugsys->in
["field$field[fieldid]"] > 0) ?
1 : 0);
240 else if ($field['type'] == 'input_text')
242 $querybuild[] = "AND bugfieldvalue.field$field[fieldid] LIKE '%" . $bugsys->in
["field$field[fieldid]"] . "%'";
244 else if ($field['type'] == 'select_single' AND $bugsys->in
["field$field[fieldid]"] != -1)
246 $temp = unserialize($field['selects']);
247 $querybuild[] = "AND bugfieldvalue.field$field[fieldid] = '" . trim($temp[ intval($bugsys->in
["field$field[fieldid]"]) ]) . "'";
252 // -------------------------------------------------------------------
253 // have to search something
254 if (sizeof($querybuild) < 1)
256 $message->error(sprintf(_('You have to enter some criteria to search for. Note that words less than %1$d characters are ignored by the search engine (and some other very common words, too).'), SEARCH_WORD_MIN
));
259 // -------------------------------------------------------------------
262 SELECT bug.*, comment.commentid
263 FROM " . TABLE_PREFIX
. "bug AS bug
264 LEFT JOIN " . TABLE_PREFIX
. "comment AS comment
265 ON (bug.bugid = comment.bugid)
266 LEFT JOIN " . TABLE_PREFIX
. "bugvaluefill AS bugfieldvalue
267 ON (bug.bugid = bugfieldvalue.bugid)
269 AND bug.product IN (#<'ONBITS:VIEW'>#)
270 AND (!bug.hidden OR (bug.hidden AND bug.product IN (#<'ONBITS:HIDDEN'>#))" . (can_perform('canviewownhidden') ?
" OR (bug.hidden AND bug.userid = " . $bugsys->userinfo
['userid'] . " AND bug.product IN (#<'ONBITS:OWNHIDDEN'>#))" : "") . ")
271 " . implode("\n\t\t", $querybuild) . "
275 $runquery = str_replace(array("#<'ONBITS:VIEW'>#", "#<'ONBITS:HIDDEN'>#", "#<'ONBITS:OWNHIDDEN'>#"), array(fetch_on_bits('canviewbugs'), fetch_on_bits('canviewhidden'), fetch_on_bits('canviewonhidden')), $query);
277 $search = $db->query($runquery);
279 $numrows = $db->num_rows($search);
283 $message->error(_('No search results were returned that matched your criteria.'));
286 while ($result = $db->fetch_array($search))
288 $ids[] = $result['bugid'];
289 $results[] = $result;
292 if ($bugsys->userinfo
['userid'])
294 $db->query("DELETE FROM " . TABLE_PREFIX
. "search WHERE userid = " . $bugsys->userinfo
['userid'] . " AND name IS NULL");
298 INSERT INTO " . TABLE_PREFIX
. "search
299 (userid, dateline, query, ids, orderby, hilight, resultcount)
301 (" . $bugsys->userinfo
['userid'] . ",
302 " . TIMENOW
. ", '" . $bugsys->escape($query) . "',
303 '" . implode(',', $ids) . "', '" . $bugsys->escape($sortclause) . "',
304 '" . $bugsys->escape($hilight) . "',
305 " . sizeof($results) . "
309 $searchid = $db->insert_id();
312 $search = array('ids' => implode(',', $ids), 'orderby' => $sortclause);
314 $_POST['do'] = 'results';
317 // ###################################################################
319 if ($_REQUEST['do'] == 'search')
321 if ($bugsys->in
['searchid'] AND !$bugsys->in
['new'])
323 if ($cachedsearch = $db->query_first("SELECT * FROM " . TABLE_PREFIX
. "search WHERE searchid = " . $bugsys->input_clean('searchid', TYPE_UINT
) . " AND userid = " . $bugsys->userinfo
['userid']))
325 $_POST['do'] = 'results';
326 $searchid = $cachedsearch['searchid'];
333 else if ($bugsys->userinfo
['userid'] AND !$bugsys->in
['new'])
335 if ($cachedsearch = $db->query_first("SELECT * FROM " . TABLE_PREFIX
. "search WHERE name IS NULL AND userid = " . $bugsys->userinfo
['userid']))
337 $_POST['do'] = 'results';
338 $searchid = $cachedsearch['searchid'];
352 if (!is_array($bugsys->datastore
['product']))
354 $message->error(_('No products are setup, therefore there can be no bugs and thus search cannot function.'));
357 if (!is_array($bugsys->datastore
['version']))
359 $message->error(_('No versions have been added underneath your product(s), there can be no bugs and thus search cannot function.'));
362 $productSelect = ConstructProductSelect();
364 // -------------------------------------------------------------------
366 $fields = construct_custom_fields(null
, true
, false
, true
);
368 foreach ($fields AS $field)
372 $customfields['left'] .= $field;
376 $customfields['right'] .= $field;
381 // -------------------------------------------------------------------
383 $select['severity'] = construct_datastore_select('severity', 'severity', 'severityid');
384 $select['priority'] = construct_datastore_select('priority', 'priority', 'priorityid');
385 $select['status'] = construct_datastore_select('status', 'status', 'statusid');
386 $select['resolution'] = construct_datastore_select('resolution', 'resolution', 'resolutionid');
389 if ($bugsys->userinfo
['userid'])
391 $searchesFetch = $db->query("SELECT * FROM " . TABLE_PREFIX
. "search WHERE name IS NOT NULL AND userid = " . $bugsys->userinfo
['userid']);
392 while ($search = $db->fetch_array($searchesFetch))
394 $value = $search['searchid'];
395 $label = $search['name'];
396 eval('$searches .= "' . $template->fetch('selectoption') . '";');
401 foreach ($bugsys->datastore
['assignto'] AS $dev)
403 $value = $dev['userid'];
404 $label = construct_user_display($dev, false
);
405 eval('$select[dev] .= "' . $template->fetch('selectoption') . '";');
408 eval('$template->flush("' . $template->fetch('search') . '");');
412 // ###################################################################
414 if ($_REQUEST['do'] == 'update')
416 $search = $db->query_first("SELECT * FROM " . TABLE_PREFIX
. "search WHERE searchid = " . $bugsys->input_clean('searchid', TYPE_UINT
) . " AND userid = " . $bugsys->userinfo
['userid']);
419 $message->error_permission();
422 $productSelect = ConstructProductSelect();
424 $show['update'] = true
;
426 // -------------------------------------------------------------------
428 $fields = construct_custom_fields(null
, true
, false
, true
);
430 foreach ($fields AS $field)
434 $customfields['left'] .= $field;
438 $customfields['right'] .= $field;
443 // -------------------------------------------------------------------
445 $select['severity'] = construct_datastore_select('severity', 'severity', 'severityid', 0, 0);
446 $select['priority'] = construct_datastore_select('priority', 'priority', 'priorityid', 0, 0);
447 $select['status'] = construct_datastore_select('status', 'status', 'statusid', 0, 0);
448 $select['resolution'] = construct_datastore_select('resolution', 'resolution', 'resolutionid', 0, 0);
454 eval('$select[dev] .= "' . $template->fetch('selectoption') . '";');
456 foreach ($bugsys->datastore
['assignto'] AS $dev)
458 $value = $dev['userid'];
459 $label = construct_user_display($dev, false
);
460 eval('$select[dev] .= "' . $template->fetch('selectoption') . '";');
463 eval('$template->flush("' . $template->fetch('search_update') . '");');
466 // ###################################################################
468 if ($_POST['do'] == 'doupdate')
470 $search = $db->query_first("SELECT * FROM " . TABLE_PREFIX
. "search WHERE searchid = " . $bugsys->input_clean('searchid', TYPE_UINT
) . " AND userid = " . $bugsys->userinfo
['userid']);
473 $message->error_permission();
476 // find all the bugs that we can edit
478 SELECT * FROM " . TABLE_PREFIX
. "bug
479 WHERE bugid IN ($search[ids])
480 AND product IN (" . fetch_on_bits('canviewbugs') . ")
483 product IN (" . fetch_on_bits('caneditother') . ")
485 (userid = " . $bugsys->userinfo
['userid'] . " AND product IN (" . fetch_on_bits('caneditown') . "))
488 while ($bug = $db->fetch_array($bugs))
490 if (!((can_perform('caneditown', $bug['product']) AND $bugsys->userinfo
['userid'] == $bug['userid']) OR (can_perform('caneditother', $bug['product']) AND $bugsys->userinfo
['userid'] != $bug['userid'])) AND !can_perform('canpostcomments', $bug['product']))
495 // TODO - need to add logging for custom fields
497 $api = new BugApi($bugsys);
498 $log = new Logging();
499 $log->set_bugid($bug['bugid']);
500 $log->add_data(true
, $bug, $log->getCommonFields());
502 if ($bugsys->in
['status'] AND can_perform('canchangestatus', $bug['product']))
504 $api->set('status', $bugsys->in
['status']);
506 if ($bugsys->in
['priority'] AND can_perform('canchangestatus', $bug['product']))
508 $api->set('priority', $bugsys->in
['priority']);
510 if ($bugsys->in
['severity'])
512 $api->set('severity', $bugsys->in
['severity']);
514 if ($bugsys->in
['resolution'] AND can_perform('canchangestatus', $bug['product']))
516 $api->set('resolution', $bugsys->in
['resolution']);
518 if ($bugsys->in
['assignedto'] AND can_perform('canassign', $bug['product']))
520 $api->set('severity', $bugsys->in
['severity']);
522 if ($bugsys->in
['product'])
524 $product = explode(',', $bugsys->in
['product']);
525 $api->set('product', $product[0]);
526 $api->set('component', $product[1]);
527 $api->set('version', $product[2]);
530 $log->add_data(false
, $api->values
, $log->getCommonFields());
532 process_custom_fields($bug['bugid'], $message);
535 $log->update_history();
538 $message->redirect(_('The specified bugs have been updated and you will now return to your search results.'), 'search.php?do=results&searchid=' . $bugsys->in
['searchid']);
541 // ###################################################################
543 if ($_REQUEST['do'] == 'export')
545 if (!$bugsys->in
['searchid'] AND $bugsys->userinfo
['userid'])
547 $search = $db->query_first("SELECT * FROM " . TABLE_PREFIX
. "search WHERE name IS NULL AND userid = " . $bugsys->userinfo
['userid']);
549 else if ($bugsys->in
['searchid'])
551 $search = $db->query_first("SELECT * FROM " . TABLE_PREFIX
. "search WHERE searchid = " . $bugsys->input_clean('searchid', TYPE_UINT
) . " AND userid = " . $bugsys->userinfo
['userid']);
555 $message->error(_('The search results are trying to export are invalid. Please start over <a href="search.php?new=1">here</a> and try again.'));
560 $message->error(_('Your search has expired because it is older than one hour. Please start over <a href="search.php?new=1">here</a>.'));
563 $bugs = $db->query("SELECT * FROM " . TABLE_PREFIX
. "bug WHERE bugid IN ($search[ids]) $search[orderby]");
565 $xml = '<?xml version="1.0" encoding="' . $language['charset'] . '"?>
567 <bugdarExport user="' . $bugsys->unsanitize(construct_user_display($bugsys->userinfo
, false
)) . '" date="' . $datef->format('r', TIMENOW
) . '" searchid="' . $search['searchid'] . '">';
569 while ($bug = $db->fetch_array($bugs))
573 ProcessBugDataForDisplay(&$bug);
575 $xml .= "\n\t\t<id>" . $bug['bugid'] . "</id>";
576 $xml .= "\n\t\t<dateReported>" . $datef->format('r', $bug['dateline']) . "</dateReported>";
579 $xml .= "\n\t\t<reporter>" . construct_user_display($db->query_first("SELECT * FROM " . TABLE_PREFIX
. "user WHERE userid = $bug[userid]"), false
) . "</reporter>";
581 $xml .= "\n\t\t<summary>" . $bug['summary'] . "</summary>";
582 $xml .= "\n\t\t<product>" . $bug['product'] . "</product>";
583 if ($bug['component'])
585 $xml .= "\n\t\t<component>" . $bug['component'] . "</component>";
587 $xml .= "\n\t\t<version>" . $bug['version'] . "</version>";
588 $xml .= "\n\t\t<status>" . $bug['status'] . "</status>";
589 if ($bug['assignedto'])
591 $xml .= "\n\t\t<assignedTo>" . construct_user_display($bugsys->datastore
['assignto']["$bug[assignedto]"], false
) . "</assignedTo>";
593 $xml .= "\n\t\t<resolution>" . $bug['resolution'] . "</resolution>";
594 $xml .= "\n\t\t<severity>" . $bug['severity'] . "</severity>";
595 $xml .= "\n\t\t<priority>" . $bug['priority'] . "</priority>";
597 $xml .= "\n\t</bug>";
600 $xml .= "\n</bugdarExport>";
602 $funct->download_file($xml, _('bugdar-search-' . $search['searchid'] . '.xml'), true
);
605 // ###################################################################
607 if ($_POST['do'] == 'dosave')
609 $search = $db->query_first("SELECT * FROM " . TABLE_PREFIX
. "search WHERE searchid = " . $bugsys->input_clean('searchid', TYPE_UINT
) . " AND userid = " . $bugsys->userinfo
['userid']);
612 $message->add_error(_('The search does not exist.'));
616 $message->add_error(sprintf(_('This search has already been named "%1$s".'), $search['name']));
618 if (empty($bugsys->in
['name']))
620 $message->add_error(_('The name cannot be empty.'));
623 if (!$message->items
)
625 $db->query("UPDATE " . TABLE_PREFIX
. "search SET name = '" . $bugsys->input_escape('name') . "' WHERE searchid = " . $bugsys->in
['searchid']);
626 $message->redirect(_('Your search has been saved.'), 'search.php?searchid=' . $search['searchid']);
630 $message->error_list_process();
631 $_REQUEST['do'] = 'save';
632 $show['errors'] = true
;
636 // ###################################################################
638 if ($_REQUEST['do'] == 'save')
640 if (!$bugsys->userinfo
['userid'])
642 $message->error_permission();
647 eval('$template->flush("' . $template->fetch('search_save') . '");');
650 // ###################################################################
652 if ($_POST['do'] == 'results')
654 $show['cached'] = false
;
655 if ($searchid AND !$justprocess)
657 $search = $cachedsearch;
658 if ($search['dateline'] < TIMENOW
- 900 OR $bugsys->in
['rerun'])
660 $research = $db->query(str_replace(array("#<'ONBITS:VIEW'>#", "#<'ONBITS:HIDDEN'>#", "#<'ONBITS:OWNHIDDEN'>#"), array(fetch_on_bits('canviewbugs'), fetch_on_bits('canviewhidden'), fetch_on_bits('canviewownhidden')), $search['query']));
661 while ($bug = $db->fetch_array($research))
663 $ids[] = $bug['bugid'];
666 $search['ids'] = implode(',', $ids);
667 $db->query("UPDATE " . TABLE_PREFIX
. "search SET ids = '" . implode(',', $ids) . "', dateline = " . TIMENOW
. ", resultcount = " . sizeof($results) . " WHERE searchid = " . $search['searchid']);
669 $show['cached'] = true
;
670 $hilight = $search['hilight'];
675 $message->error(_('No bugs matched your search criteria. Please <a href="search.php?new=1">try again</a> with different search requirements.'));
678 LoadPaginationFramework();
679 $pagination->setTotal($search['resultcount']);
680 $pagination->splitPages();
682 $sort = new ListSorter('search');
684 $show['save'] = ($bugsys->userinfo
['userid'] AND !$search['name']);
685 $show['update'] = can_perform('caneditother');
688 $search = $db->query("SELECT * FROM " . TABLE_PREFIX
. "bug WHERE bugid IN ($search[ids]) $search[orderby] LIMIT " . $pagination->fetchLimit($pagination->getPage() - 1) . ", " . $pagination->getPerPage());
689 while ($bug = $db->fetch_array($search))
691 $funct->exec_swap_bg($stylevar['alt_color'], '');
692 ProcessBugDataForDisplay(&$bug, $funct->bgcolour
);
693 $bugs .= $sort->constructRow($bug, "&hilight=$hilight");
696 $columnHeads = $sort->constructColumnHeaders(false
);
697 $show['pagenav'] = ($pagination->getPageCount() > 1);
698 $pagenav = $pagination->constructPageNav('search.php?searchid=' . $searchid);
700 eval('$template->flush("' . $template->fetch('search_results') . '");');
703 /*=====================================================================*\
704 || ###################################################################
707 || ###################################################################
708 \*=====================================================================*/