<?php

/*
** DGS Search
** $Id: cgi.php,v 1.4 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
*/

if (!function_exists('getFormData')) {
	function getFormData() {
		global $HTTP_GET_VARS;
		global $HTTP_POST_VARS;

		$requestType = getEnvVar('REQUEST_METHOD');

		switch (strtolower($requestType)) {
			case 'get':
				$data = $HTTP_GET_VARS;
				break;
			case 'post':
				$data = $HTTP_POST_VARS;
				break;
			default:
				settype($data, 'array');
		}

		if (function_exists('_getSpecificFormData')) {
			$data = _getSpecificFormData();
		}

		return $data;
	}
}

function processQueryStruct($data) {
	global $config;

   /* Clean up and set defaults if needed. */
   $sq['r'] = (!isset($data['r'])) ? (($config['results']) ? 10 : $config['results']) : $data['r']; //Set default for results per page.
   $sq['r'] = ($sq['r'] < 1) ? $config['maxResults'] : $sq['r'];
   $sq['o'] = (!isset($data['o']) || $data['o'] < 1) ? 0 : $data['o']; //Set default for offset.
   $sq['s'] = (!isset($data['s']) || $data['s'] < 1) ? 0 : $data['s']; //Set result set to 0 if we don't have a cached one.
   $sq['origQ'] = (isset($data['q']) && $data['q'] != '') ? $data['q'] : '';
   $sq['cleanQ'] = htmlentities($sq['origQ']);
   $sq['encQ'] = htmlentities(urlencode($sq['origQ']));

	//Rip our NOT requests out.
	$tmp = ereg_replace("[[:space:]][^-[:space:]][^[:space:]]+", '', ' ' . $sq['origQ']);
	$tmp = str_replace('-', '', $tmp);
	$sq['q']['not'] = split('[[:space:]]+', trim($tmp));
	
	//Explode the OR sections of the query.
	$tmp = ereg_replace("[[:space:]]-[^[:space:]]+", '', ' ' . $sq['origQ']);
	$tmp = str_replace('-', '', $tmp);
	$tmp = eregi_replace('^OR[[:space:]]+', '', ltrim($tmp));
	$tmp = eregi_replace('[[:space:]]+OR$', '', rtrim($tmp));
	$sq['q']['or'] = split('[[:space:]]+[Oo][Rr][[:space:]]+', trim($tmp));

	//Explode out our AND sections, replacing the old strings with arrays.
	foreach($sq['q']['or'] as $i => $tmp) {
		$tmp = eregi_replace('[[:space:]]+AND[[:space:]]+', ' ', $tmp);
		$tmp = eregi_replace('^AND[[:space:]]+', '', ltrim($tmp));
		$tmp = eregi_replace('[[:space:]]+AND$', '', rtrim($tmp));
		$sq['q']['or'][$i] = split('[[:space:]]+', trim($tmp));
	}

	if ($config['debug']) {
		printf("->Debug: processQuerystruct() - Struct \$sq follows:<BR>\n");
		if (function_exists('print_r')) {
			print_r($sq);
		} else {
			var_dump($sq);
		}
		printf("<BR>\n");
	}

	return $sq;
}

if (!function_exists('getEnvVar')) {
	function getEnvVar($envVar) {
		global $config;
		global $HTTP_ENV_VARS;
		global $HTTP_SERVER_VARS;
	 
		if ($config['debug'] > 1) {
			printf("<BR>->Debug: getEnvVar() - getenv(\"%s\"): %s<BR>\n", $envVar, getenv($envVar));
			printf("->Debug: getEnvVar() - \$GLOBALS[\"%s\"]: %s<BR>\n", $envVar, $GLOBALS[$envVar]);
			printf("->Debug: getEnvVar() - \$HTTP_ENV_VAR[\"%s\"]: %s<BR>\n", $envVar, $HTTP_ENV_VARS[$envVar]);
			printf("->Debug: getEnvVar() - \$HTTP_SERVER_VAR[\"%s\"]: %s<BR>\n", $envVar, $HTTP_SERVER_VARS[$envVar]);
		}
	 
		$retVal = $GLOBALS[$envVar];
		if (strlen($retVal) < 1) {
			$retVal = getenv($envVar);
			if (strlen($retVal) < 1) {
				$retVal = $HTTP_ENV_VARS[$envVar];
				if (strlen($retVal) < 1) {
					$retVal = $HTTP_SERVER_VARS[$envVar];
				}
			}
		}
	 
		return $retVal;
	}
}

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

?>
