error_permission(); } define('MODE_ANY', ($bugsys->in['mode'] == 1)); define('MODE_ALL', ($bugsys->in['mode'] == 2)); define('MODE_RAW', ($bugsys->in['mode'] == 3)); $var = $db->query_first("SHOW VARIABLES LIKE 'ft_min_word_len'"); define('SEARCH_WORD_MIN', $var['Value']); $show['search'] = true; // ################################################################### if (empty($_REQUEST['do'])) { $_REQUEST['do'] = 'search'; } // ################################################################### if ($_REQUEST['do'] == 'process') { // ------------------------------------------------------------------- // handle keywords if ($bugsys->in['summary']) { $keywords = preg_split('#\s+#', $bugsys->in['summary']); // TODO - need to have some str to bool conversions foreach ($keywords AS $word) { if (strlen($word) < SEARCH_WORD_MIN) { continue; } if (MODE_ALL) { $querybuild['text'] .= " +$word"; } else { $querybuild['text'] .= " $word"; } if (!preg_match('#-(.+?)#', trim($word))) { $hilight .= " $word"; } } $hilight = preg_replace('#[^0-9a-zA-Z_ ]#', '', $hilight); $hilight = trim($hilight); $hilight = preg_replace('#\s#', '+', $hilight); $temp = trim($querybuild['text']); if (MODE_ALL OR MODE_RAW) { $bool_flag = ' IN BOOLEAN MODE'; } $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)"; } // ------------------------------------------------------------------- // reporter if ($bugsys->in['reporter']) { // force email or name?? make a distinction? // more elegant way to do this? probably $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')) . "%'"); if ($user['userid']) { $querybuild['reporter'] = "AND bug.userid = $user[userid] OR comment.userid = $user[userid]"; } } // ------------------------------------------------------------------- // product/component/version stuff if (is_array($bugsys->in['pcv_select'])) { foreach ($bugsys->in['pcv_select'] AS $pcv) { $pcv = parse_pcv_select($pcv, true); $products[] = $pcv['product']; $components[] = $pcv['component']; $versions[] = $pcv['version']; } $querybuild['pcv'] = "AND bug.product IN (" . implode(',', $products) . ") AND bug.component IN (" . implode(',', $components) . ") AND bug.version IN (" . implode(',', $versions) . ")"; } // ------------------------------------------------------------------- // severity, priority, status, resolution, assignedto // severity if ($bugsys->in['severity']) { $bugsys->input_clean('severity', TYPE_UINT); $querybuild['severity'] = "AND bug.severity IN (" . implode(',', $bugsys->in['severity']) . ")"; } // priority if ($bugsys->in['priority']) { $bugsys->input_clean('priority', TYPE_UINT); $querybuild['priority'] = "AND bug.priority IN (" . implode(',', $bugsys->in['priority']) . ")"; } // status if ($bugsys->in['status']) { $bugsys->input_clean('status', TYPE_UINT); $querybuild['status'] = "AND bug.status IN (" . implode(',', $bugsys->in['status']) . ")"; } // resolution if ($bugsys->in['resolution']) { $bugsys->input_clean('resolution', TYPE_UINT); $querybuild['resolution'] = "AND bug.resolution IN (" . implode(',', $bugsys->in['resolution']) . ")"; } // assignment if ($bugsys->in['assignedto']) { $bugsys->input_clean('assignedto', TYPE_UINT); $querybuild['assignedto'] = "AND bug.assignedto IN (" . implode(',', $bugsys->in['assignedto']) . ")"; } // ------------------------------------------------------------------- // date if ($bugsys->in['date']) { // now - (seconds/day * number of days) $dateline = time() - ($bugsys->input_clean('date', TYPE_INT) * 3600); $querybuild['date'] = "AND bug.dateline >= $dateline"; } // ------------------------------------------------------------------- // favoritess if (($bugsys->in['favorite'] == 1 OR $bugsys->in['favorite'] == 0) AND $bugsys->userinfo['userid']) { $favorites = $db->query("SELECT * FROM " . TABLE_PREFIX . "favorite WHERE userid = " . $bugsys->userinfo['userid']); while ($favorite = $db->fetch_array($favorites)) { $ids[] = $favorite['bugid']; } $querybuild['favorites'] = "AND bug.bugid " . ($bugsys->in['favorite'] == 0 ? "NOT IN" : "IN") . " (" . implode(', ', $ids) . ")"; } // ------------------------------------------------------------------- // sort by $sortby = array('bugid', 'severity', 'priority', 'status', 'resolution', 'dateline'); $orderby = array('ASC', 'DESC'); $bugsys->in['orderby'] = strtoupper($bugsys->in['orderby']); if (in_array($bugsys->in['sortby'], $sortby) AND in_array($bugsys->in['orderby'], $orderby)) { $sortclause = "ORDER BY " . $bugsys->in['sortby'] . ' ' . $bugsys->in['orderby']; } else if ($bugsys->in['sortby'] == 'relevance') { $sortclause = ''; } else { $sortclause = ''; } // ------------------------------------------------------------------- // custom fields $fields_fetch = $bugsys->db->query(" SELECT bugfield.* FROM " . TABLE_PREFIX . "bugfield AS bugfield LEFT JOIN " . TABLE_PREFIX . "bugfieldpermission AS permission ON (bugfield.fieldid = permission.fieldid) WHERE permission.mask <> 0 AND permission.usergroupid = {$bugsys->userinfo['usergroupid']} AND bugfield.cansearch = 1" ); while ($field = $bugsys->db->fetch_array($fields_fetch)) { if (!empty($bugsys->in["field$field[fieldid]"]) OR ($field['type'] == 'select_single' AND isset($bugsys->in["field$field[fieldid]"]))) { if ($field['type'] == 'input_checkbox') { $querybuild[] = "AND bugfieldvalue.field$field[fieldid] = " . (($bugsys->in["field$field[fieldid]"] == 1) ? 0 : 1); } else if ($field['type'] == 'input_text') { $querybuild[] = "AND bugfieldvalue.field$field[fieldid] LIKE '%" . $bugsys->in["field$field[fieldid]"] . "%'"; } else if ($field['type'] == 'select_single' AND $bugsys->in["field$field[fieldid]"] != -1) { $temp = unserialize($field['selects']); $querybuild[] = "AND bugfieldvalue.field$field[fieldid] = '" . trim($temp[ intval($bugsys->in["field$field[fieldid]"]) ]) . "'"; } } } // ------------------------------------------------------------------- // have to search something if (sizeof($querybuild) < 1) { $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)); } // ------------------------------------------------------------------- // do the search $query = " SELECT bug.*, comment.commentid FROM " . TABLE_PREFIX . "bug AS bug LEFT JOIN " . TABLE_PREFIX . "comment AS comment ON (bug.bugid = comment.bugid) LEFT JOIN " . TABLE_PREFIX . "bugvaluefill AS bugfieldvalue ON (bug.bugid = bugfieldvalue.bugid) WHERE bug.bugid <> 0 AND bug.product IN (#<'ONBITS:VIEW'>#) 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'>#))" : "") . ") " . implode("\n\t\t", $querybuild) . " GROUP BY bug.bugid $sortclause"; $runquery = str_replace(array("#<'ONBITS:VIEW'>#", "#<'ONBITS:HIDDEN'>#", "#<'ONBITS:OWNHIDDEN'>#"), array(fetch_on_bits('canviewbugs'), fetch_on_bits('canviewhidden'), fetch_on_bits('canviewonhidden')), $query); $search = $db->query($runquery); $numrows = $db->num_rows($search); if ($numrows < 1) { $message->error(_('No search results were returned that matched your criteria.')); } while ($result = $db->fetch_array($search)) { $ids[] = $result['bugid']; $results[] = $result; } if ($bugsys->userinfo['userid']) { $db->query(" REPLACE INTO " . TABLE_PREFIX . "search (userid, dateline, query, ids, orderby, hilight, resultcount) VALUES (" . $bugsys->userinfo['userid'] . ", " . TIMENOW . ", '" . $bugsys->escape($query) . "', '" . implode(',', $ids) . "', '" . $bugsys->escape($sortclause) . "', '" . $bugsys->escape($hilight) . "', " . sizeof($results) . " )" ); } $justprocess = true; $search = array('ids' => implode(',', $ids), 'orderby' => $sortclause); $_REQUEST['do'] = 'results'; } // ################################################################### if ($_REQUEST['do'] == 'search') { if ($bugsys->userinfo['userid'] AND !$bugsys->in['new']) { if ($cachedsearch = $db->query_first("SELECT * FROM " . TABLE_PREFIX . "search WHERE userid = " . $bugsys->userinfo['userid'])) { $_REQUEST['do'] = 'results'; } else { $newsearch = true; } } else { $newsearch = true; } if ($newsearch) { if (!is_array($bugsys->datastore['product'])) { $message->error(_('No products are setup, therefore there can be no bugs and thus search cannot function.')); } if (!is_array($bugsys->datastore['version'])) { $message->error(_('No versions have been added underneath your product(s), there can be no bugs and thus search cannot function.')); } $pcv_select = construct_pcv_select(); // ------------------------------------------------------------------- // custom fields $fields = construct_custom_fields(null, true); $i = 0; foreach ($fields AS $field) { if ($i % 2 == 0) { $customfields['left'] .= $field; } else { $customfields['right'] .= $field; } $i++; } // ------------------------------------------------------------------- // built-in fields $select['severity'] = construct_datastore_select('severity', 'severity', 'severityid'); $select['priority'] = construct_datastore_select('priority', 'priority', 'priorityid'); $select['status'] = construct_datastore_select('status', 'status', 'statusid'); $select['resolution'] = construct_datastore_select('resolution', 'resolution', 'resolutionid'); $select['dev'] = ''; foreach ($bugsys->datastore['assignto'] AS $dev) { $value = $dev['userid']; $label = construct_user_display($dev, false); eval('$select[dev] .= "' . $template->fetch('selectoption') . '";'); } eval('$template->flush("' . $template->fetch('search') . '");'); } } // ################################################################### if ($_REQUEST['do'] == 'results') { $show['cached'] = false; if ($bugsys->userinfo['userid'] AND !$justprocess) { $search = $cachedsearch; if ($search['dateline'] < TIMENOW - 900 OR $bugsys->in['rerun']) { $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'])); while ($bug = $db->fetch_array($research)) { $ids[] = $bug['bugid']; $results[] = $bug; } $search['ids'] = implode(',', $ids); $db->query("UPDATE " . TABLE_PREFIX . "search SET ids = '" . implode(',', $ids) . "', dateline = " . TIMENOW . ", resultcount = " . sizeof($results) . " WHERE userid = " . $bugsys->userinfo['userid']); } $show['cached'] = true; $hilight = $search['hilight']; } $pagination = new Pagination('p', 'pp'); $pagination->total = $search['resultcount']; $pagination->split_pages(); if (!$search['ids']) { $message->error(_('No bugs matched your search criteria. Please try again with different search requirements.')); } $search = $db->query("SELECT * FROM " . TABLE_PREFIX . "bug WHERE bugid IN ($search[ids]) $search[orderby] LIMIT " . $pagination->fetch_limit($pagination->page - 1) . ", " . $pagination->perpage); while ($bug = $db->fetch_array($search)) { $funct->exec_swap_bg($stylevar['alt_color'], ''); $bug['bgcolor'] = ($bugsys->userinfo['showcolors'] ? $bugsys->datastore['status']["$bug[status]"]['color'] : $funct->bgcolour); $bug['hiddendisplay'] = ((can_perform('canviewhidden', $bug['product']) OR (can_perform('canviewownhidden') AND $bug['userid'] == $bugsys->userinfo['userid'])) ? false : true); $bug['product'] = $bugsys->datastore['product']["$bug[product]"]['title']; $bug['version'] = $bugsys->datastore['version']["$bug[version]"]['version']; $bug['status'] = $bugsys->datastore['status']["$bug[status]"]['status']; $bug['resolution'] = $bugsys->datastore['resolution']["$bug[resolution]"]['resolution']; $bug['priority'] = $bugsys->datastore['priority']["$bug[priority]"]['priority']; $bug['severity'] = $bugsys->datastore['severity']["$bug[severity]"]['severity']; $bug['lastposttime'] = ($bug['hiddendisplay'] ? $bug['hiddenlastposttime'] : $bug['lastposttime']); $bug['lastpost'] = ($bug['hiddendisplay'] ? $bug['hiddenlastpostbyname'] : $bug['lastpostbyname']); $bug['lastposttime'] = $datef->format($bugsys->options['dateformat'], $bug['lastposttime']); $bug['urladd'] = "&hilight=$hilight"; eval('$bugs .= "' . $template->fetch('trackerhome_bits') . '";'); } $show['pagenav'] = ($pagination->pagecount > 1 ? true : false); $pagenav = $pagination->construct_page_nav('search.php'); eval('$template->flush("' . $template->fetch('search_results') . '");'); } /*=====================================================================*\ || ################################################################### || # $HeadURL$ || # $Id$ || ################################################################### \*=====================================================================*/ ?>