<?php

/*
** DGS Search
** $Id: search.php,v 1.7 2006/01/24 07:08:09 sella Exp $
** Copyright (c) 2000-2001 Digital Genesis Software, LLC. All Rights Reserved.
** Released under the GPL Version 2 License.
** http://www.digitalgenesis.com
*/

require_once('config/config.php');
require_once('libs/dgsutil/cgi.php');
require_once('libs/dgsutil/text.php');
require_once('libs/dgsutil/misc.php');
require_once('libs/dgsutil/debug.php');
require_once('libs/dgsutil/dgssearch.php');
require_once('libs/dgsutil/dgsindex.php');
require_once('libs/dgsutil/filesystem.php');
require_once('libs/index/indexer.php');
require_once('libs/third-party/porter_stemming.php');

function search(&$retVal, $sq, $timer) {
	global $config;
	$modules = $config['searchModules'];
	$installBase = $config['installBase'];
	$retVal = array();

	if ($config['debug'])
		printf(">Debug: search()<BR>\n");

	reset($modules);
	while (list(, $module) = each($modules)) {
		$retVal = $module($retVal, $sq, $timer);
		if (is_string($retVal)) {
			printf("Error: Search module '%s' had a fatal error. Details below:<BR>\n<BR>\n%s<BR>\n", $module, $retVal);
			return false;
		}
	}

	return true;
}

function display($retVal, $sq, $timer) {
	global $config;
	$modules = $config['displayModules'];
	$installBase = $config['installBase'];
	$fileSeparator = $config['fileSeparator'];

	if ($config['debug'])
		printf(">Debug: display()<BR>\n");

	reset($modules);
	while (list(, $module) = each($modules)) {
		$error = $module($retVal, $sq, $timer);
		if (is_string($error)) {
			printf("Error: Display module '%s' had a fatal error. Details below:<BR>\n<BR>\n%s<BR>\n", $module, $error);
			break;
		}
	}

	return true;
}

/* Start of Main */

set_magic_quotes_runtime(0); //Disable Evil Magic Quotes

$data = getFormData();
if ($data['debug']) {
	if (!remoteDebug($data['debug'], $data['option']))
		return;
}

/* Hack for Search/Browse for SellaNMS */
if (!strcmp($data['function'], 'Browse') || strlen($data['q']) < 1) {
	$database[0] = $db_func['browse'];
} else {
	$database[0] = $db_func['search'];
}

$timer = getTime();

if ($config['debug'])
	printf("Debug: main() - Calling processConfig()<BR>\n");

$state = _processConfig($data);
if ($state) {
	//Store query in a simplified format for boolean search.
	$sq = processQueryStruct($data);

	/* Search for results. */
	if ($sq['q']) {
		if ($config['debug'])
			printf("Debug: main() - Calling search(\$retVal, \$sq, \$timer)<BR>\n");
		if (!search($retVal, $sq, $timer)) {
			if ($config['debug']) {
				printf("Debug: main() - Searched returned in error.<BR>\n");
			}
		}
	} else {
		if ($config['debug'])
			printf("Debug: main() - Skipping search. No query given.<BR>\n");
	}

	/* Display our results. */
	if ($config['debug'])
		printf("Debug: main() - Calling display(\$retVal, \$sq, \$timer)<BR>\n");
	display($retVal, $sq, $timer);
}

if ($config['debug'])
	printf("Debug: main() - Total Time: %.1f<BR>\n", getTime() - $timer);

/* End of Main */

/*
** Local Variables:
** c-basic-offset: 3
** tab-width: 3
** End:
** vim:
** vim: noet ts=3 sw=3
*/

?>
