<?php

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

function getTitle($file) {
	global $config;

	$title = getHtmlTitle($file);
	if ($title)
		return $title;
	$endPath = explode($config['fsBase'], $file);
	$endPath = str_replace('\\', '/', $endPath[1]);

  	return ($config['urlBase'] . $endPath);
}

function getHtmlTitle($file) {
	global $config;

	$warn = $config['warn'];

	if ($warn) {
		$buffer = file($file); //Without @ will show SAFE MODE warnings.
	} else {
		$buffer = @file($file); //Hide warnings.
	}

	if (!is_array($buffer))
		return;

	$buffer = implode(' ', $buffer);
	$lowBuffer = strtolower($buffer);

	/* Locate where <TITLE> is located in html file. */
	$lBound = strpos($lowBuffer, '<title>') + 7; //7 is the lengh of <TITLE>.

	if ($lBound < 1)
		return;

	/* Locate where </TITLE> is located in html file. */
	$uBound = strpos($lowBuffer, '</title>', $lBound);

	if ($uBound < $lBound)
		return;

	/* Clean HTML and PHP tags out of $title with the madness below. */
	$title = ereg_replace("[\t\n\r]", '', substr($buffer, $lBound, $uBound - $lBound));
	$title = trim(strip_tags($title));

	if (strlen($title) < 1) //A blank title is worthless.
		return;

	return $title;
}

function getMetaDesc(&$content) {
	global $config;

	$lowBuffer = strtolower($content);

	/* Locate where <META is located in html file. */
	$lBound = strpos($lowBuffer, '<meta');

	if ($lBound < 1)
		return false;

	/* Locate where </HEAD is located in html file. */
	$uBound = strpos($lowBuffer, '</head', $lBound);

	if ($uBound < $lBound)
		return false;

	/* Clean HTML and PHP tags out of $desc with the madness below. */
	$desc = ereg_replace("[\t\r\n]", '', substr($content, $lBound, $uBound - $lBound));
	$desc = eregi_replace('^.*<META[[:space:]]+NAME[[:space:]]*=[[:space:]]*\"?description\"?[[:space:]]+CONTENT[[:space:]]*=[[:space:]]*\"?([^\">]*).*$', '\\1', $desc);
	$desc = trim(strip_tags($desc));

	if (strlen($desc) < 1) //A blank desc is worthless.
		return false;

	return $desc;
}

function strimatch($string1, $string2) {
	for($i = 0; $i < strlen($string1) && $i < strlen($string2); $i++) {
		if (strtolower($string1[$i]) != strtolower($string2[$i])) {
			break;
		}
	}

	return substr($string1, 0, $i);
}

function strmatch($string1, $string2) {
	for($i = 0; $i < strlen($string1) && $i < strlen($string2); $i++) {
		if ($string1[$i] != $string2[$i]) {
			break;
		}
	}

	return substr($string1, 0, $i);
}

function escapeSQL($text) {
	if (function_exists('mysql_escape_string'))
		$text = mysql_escape_string($text);
	else {
		$text = str_replace("\\", "\\\\", $text);
		$text = str_replace("'", "\\'", $text);
		$text = str_replace("\"", "\\\"", $text);
		$text = str_replace("\n", "\\n", $text);
		$text = str_replace("\r", "\\r", $text);
		$text = str_replace("\t", "\\t", $text);
		$text = str_replace("\b", "\\b", $text);
	}
	$text = str_replace("_", "\\_", $text);
	$text = str_replace("%", "\\%", $text);
	$text = str_replace(";", "\\;", $text);

	return $text;
}

function dgs_in_array($needle, $haystack, $strict = false) {
	if (function_exists('in_array'))
		return in_array($needle, $haystack, $strict);

	if (!is_array($haystack))
		return false;

	reset($haystack);
	foreach($haystack as $i => $value) {
		if ($value == $needle && (!$strict || gettype($value) == gettype($needle))) {
			return true;
		}
	}

	return false;
}

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

?>
