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']);
$db->query("DELETE FROM " . TABLE_PREFIX . "search WHERE userid = 0 AND dateline < " . (TIMENOW - 3600));
$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['product']))
{
foreach ($bugsys->in['product'] AS $prod)
{
$product = explode(',', $prod);
$product = $bugsys->clean($product, TYPE_UINT);
$products[] = $product[0];
$components[] = $product[1];
$versions[] = $product[2];
}
$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("DELETE FROM " . TABLE_PREFIX . "search WHERE userid = " . $bugsys->userinfo['userid'] . " AND name IS NULL");
}
$db->query("
INSERT 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) . "
)"
);
$searchid = $db->insert_id();
$justprocess = true;
$search = array('ids' => implode(',', $ids), 'orderby' => $sortclause);
$_POST['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 name IS NULL AND userid = " . $bugsys->userinfo['userid']))
{
$_POST['do'] = 'results';
$searchid = $cachedsearch['searchid'];
}
else
{
$newsearch = true;
}
}
else if ($bugsys->in['searchid'] AND !$bugsys->in['new'])
{
if ($cachedsearch = $db->query_first("SELECT * FROM " . TABLE_PREFIX . "search WHERE searchid = " . $bugsys->input_clean('searchid', TYPE_UINT) . " AND userid = " . $bugsys->userinfo['userid']))
{
$_POST['do'] = 'results';
$searchid = $cachedsearch['searchid'];
}
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.'));
}
$productSelect = ConstructProductSelect();
// -------------------------------------------------------------------
// 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'] == 'export')
{
if (!$bugsys->in['searchid'] AND $bugsys->userinfo['userid'])
{
$search = $db->query_first("SELECT * FROM " . TABLE_PREFIX . "search WHERE name IS NULL AND userid = " . $bugsys->userinfo['userid']);
}
else if ($bugsys->in['searchid'])
{
$search = $db->query_first("SELECT * FROM " . TABLE_PREFIX . "search WHERE searchid = " . $bugsys->input_clean('searchid', TYPE_UINT) . " AND userid = " . $bugsys->userinfo['userid']);
}
else
{
$message->error(_('The search results are trying to export are invalid. Please start over here and try again.'));
}
if (!$search)
{
$message->error(_('Your search has expired because it is older than one hour. Please start over here.'));
}
$bugs = $db->query("SELECT * FROM " . TABLE_PREFIX . "bug WHERE bugid IN ($search[ids]) $search[orderby]");
$xml = '
';
while ($bug = $db->fetch_array($bugs))
{
$xml .= "\n\t";
ProcessBugDataForDisplay(&$bug);
$xml .= "\n\t\t" . $bug['bugid'] . "";
$xml .= "\n\t\t" . $datef->format('r', $bug['dateline']) . "";
if ($bug['userid'])
{
$xml .= "\n\t\t" . construct_user_display($db->query_first("SELECT * FROM " . TABLE_PREFIX . "user WHERE userid = $bug[userid]"), false) . "";
}
$xml .= "\n\t\t" . $bug['summary'] . "";
$xml .= "\n\t\t" . $bug['product'] . "";
if ($bug['component'])
{
$xml .= "\n\t\t" . $bug['component'] . "";
}
$xml .= "\n\t\t" . $bug['version'] . "";
$xml .= "\n\t\t" . $bug['status'] . "";
if ($bug['assignedto'])
{
$xml .= "\n\t\t" . construct_user_display($bugsys->datastore['assignto']["$bug[assignedto]"], false) . "";
}
$xml .= "\n\t\t" . $bug['resolution'] . "";
$xml .= "\n\t\t" . $bug['severity'] . "";
$xml .= "\n\t\t" . $bug['priority'] . "";
$xml .= "\n\t";
}
$xml .= "\n";
$funct->download_file($xml, _('bugdar-search-results.xml'), true);
}
// ###################################################################
if ($_POST['do'] == 'results')
{
$show['cached'] = false;
if ($searchid 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 searchid = " . $bugsys->input_clean('searchid', TYPE_UINT));
}
$show['cached'] = true;
$hilight = $search['hilight'];
}
LoadPaginationFramework();
$pagination->setTotal($search['resultcount']);
$pagination->splitPages();
$sort = new ListSorter('search');
$bugs = '';
$search = $db->query("SELECT * FROM " . TABLE_PREFIX . "bug WHERE bugid IN ($search[ids]) $search[orderby] LIMIT " . $pagination->fetchLimit($pagination->getPage() - 1) . ", " . $pagination->getPerPage());
while ($bug = $db->fetch_array($search))
{
$funct->exec_swap_bg($stylevar['alt_color'], '');
ProcessBugDataForDisplay(&$bug, $funct->bgcolour);
$bugs .= $sort->constructRow($bug, "&hilight=$hilight");
}
$columnHeads = $sort->constructColumnHeaders(false);
$show['pagenav'] = ($pagination->getPageCount() > 1);
$pagenav = $pagination->constructPageNav('search.php?searchid=' . $searchid);
eval('$template->flush("' . $template->fetch('search_results') . '");');
}
/*=====================================================================*\
|| ###################################################################
|| # $HeadURL$
|| # $Id$
|| ###################################################################
\*=====================================================================*/
?>