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']); // #*# 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->in['reporter']) . "' OR displayname LIKE '%" . str_replace('%', '\%', $bugsys->in['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"; } // ------------------------------------------------------------------- // favourites if (($bugsys->in['favourite'] == 1 OR $bugsys->in['favourite'] == 0) AND $bugsys->userinfo['userid']) { $favourites = $db->query("SELECT * FROM " . TABLE_PREFIX . "favourite WHERE userid = " . $bugsys->userinfo['userid']); while ($favourite = $db->fetch_array($favourites)) { $ids[] = $favourite['bugid']; } $querybuild['favourites'] = "AND bug.bugid " . ($bugsys->in['favourite'] == 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($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)); } // ------------------------------------------------------------------- // do the search $query = " SELECT bug.*, comment.commentid, user1.displayname AS firstreport, user2.displayname AS lastpost FROM " . TABLE_PREFIX . "bug AS bug LEFT JOIN " . TABLE_PREFIX . "comment AS comment ON (bug.bugid = comment.bugid) LEFT JOIN " . TABLE_PREFIX . "user AS user1 ON (bug.userid = user1.userid) LEFT JOIN " . TABLE_PREFIX . "user AS user2 ON (bug.lastpostby = user2.userid) 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'>#))) " . implode("\n\t\t", $querybuild) . " GROUP BY bug.bugid $sortclause"; $runquery = str_replace(array("#<'ONBITS:VIEW'>#", "#<'ONBITS:HIDDEN'>#"), array(fetch_on_bits('canviewbugs'), fetch_on_bits('canviewhidden')), $query); $search = $db->query($runquery); $numrows = $db->num_rows($search); if ($numrows < 1) { $message->error($lang->string('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) VALUES (" . $bugsys->userinfo['userid'] . ", " . TIMENOW . ", '" . $bugsys->escape($query) . "', '" . implode(',', $ids) . "', '" . $bugsys->escape($sortclause) . "', '" . $bugsys->escape($hilight) . "' )" ); } $justprocess = true; $_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) { $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']) { $search = $db->query(str_replace(array("#<'ONBITS:VIEW'>#", "#<'ONBITS:HIDDEN'>#"), array(fetch_on_bits('canviewbugs'), fetch_on_bits('canviewhidden')), $search['query'])); while ($bug = $db->fetch_array($search)) { $ids[] = $bug['bugid']; $results[] = $bug; } $db->query("UPDATE " . TABLE_PREFIX . "search SET ids = '" . implode(',', $ids) . "', dateline = " . TIMENOW . " WHERE userid = " . $bugsys->userinfo['userid']); } else { $search = $db->query(" SELECT bug.*, user1.displayname AS firstreport, user2.displayname AS lastpost FROM " . TABLE_PREFIX . "bug AS bug LEFT JOIN " . TABLE_PREFIX . "user AS user1 ON (bug.userid = user1.userid) LEFT JOIN " . TABLE_PREFIX . "user AS user2 ON (bug.lastpostby = user2.userid) WHERE bug.bugid IN ($search[ids]) $search[orderby]" ); while ($bug = $db->fetch_array($search)) { $results[] = $bug; } } $show['cached'] = true; $hilight = $search['hilight']; } foreach ($results AS $bug) { $funct->exec_swap_bg($stylevar['alt_colour'], ''); $bug['bgcolour'] = ($bugsys->userinfo['showcolours'] ? $bugsys->datastore['status']["$bug[status]"]['color'] : $funct->bgcolour); $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['hiddenlastpost'] : $bug['lastpost']); $bug['lastposttime'] = $datef->format($bugsys->options['dateformat'], $bug['lastposttime']); $bug['urladd'] = "&hilight=$hilight"; eval('$bugs .= "' . $template->fetch('trackerhome_bits') . '";'); } eval('$template->flush("' . $template->fetch('search_results') . '");'); } /*=====================================================================*\ || ################################################################### || # $HeadURL$ || # $Id$ || ################################################################### \*=====================================================================*/ ?>