<?php
/*															*/
/* File: misc.php											*/
/* (c) 2003,2004 by Łukasz Tomicki <tomicki(at)o2(dot)pl> 	*/
/* Most recent version available @ : http://tomicki.net/	*/
/*															*/
/*
/*  This program is free software; you can redistribute it and/or modify
/*  it under the terms of the GNU General Public License as published by
/*  the Free Software Foundation; either version 2 of the License, or
/*  (at your option) any later version.
/*
/*  This program is distributed in the hope that it will be useful,
/*  but WITHOUT ANY WARRANTY; without even the implied warranty of
/*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
/*  GNU Library General Public License for more details.
/*
/*  You should have received a copy of the GNU General Public License
/*  along with this program; if not, write to the Free Software
/*  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
/*															*/

function calculateAuthValue($a, $b)
{
	$m = ($b % $a);
	if (!$m)	$m = 1;
		
	$n = ($a % $b);
	if (!$n)	$n = 1;
		
	$y = ((($m + $b) % ($n + $a)) + ($b - $a)) * ($b + $a);
	return ( $y );
}

function addHTMLLinks($var)
{
	$mod_var = "";
	$offset = 0;
	$running = 1;
	$str_size = strlen($var);
	while($running)
	{
		$www = 0;
		$pos1 = strpos($var, "http://", $offset);
		$pos2 = strpos($var, "www", $offset);
		
		if ($pos1 === false)
			$pos1 = $str_size;
		if ($pos2 === false)
			$pos2 = $str_size;
		
		if ($pos1 < $pos2)
			$pos = $pos1;

		else
		{
			$pos = $pos2;
			$www = 1;
		}		
		
		if ($pos === $str_size)
		{
			$running = 0;
			$mod_var .= substr($var, $offset, $pos - $offset);
			continue;
		}
		
		$mod_var .= substr($var, $offset, $pos - $offset);
		$offset = $pos;
		$mod_var .= "<a href=\"";
		if ($www)
			$mod_var .= "http://";
			
		$pos1 = strpos($var, "</span>", $offset);
		$pos2 = strpos($var, "<br>", $offset);
		$pos3 = strpos($var, " ", $offset);
		if ($pos1 === false)
			$pos1 = $str_size;
		if ($pos2 === false)
			$pos2 = $str_size;
		if ($pos3 === false)
			$pos3 = $str_size;
		
		$pos = $pos1 < $pos2 ? $pos1 : $pos2;
		$pos = $pos < $pos3 ? $pos : $pos3; 
		
		if ($pos === false)
			$pos = $str_size;
			
		
		$addr = substr($var, $offset, $pos - $offset);
		$offset = $pos;
		$mod_var .= $addr;
		$mod_var .= "\">";
		$mod_var .= $addr;
		$mod_var .= "</a>";
	}
	
	if ($mod_var)
		return ( $mod_var );
}

function doBodyClean($var, $www = 1)
{
	$var = trim($var);
	$var = strip_tags($var, '<b><u><i>');
	$var = str_replace("\n", "<br>", $var);
	
	if ($www)
		$var = addHTMLLinks($var);
	
	return ( $var );
}

function doTitleClean($var)
{
	$var = trim($var);
	$var = str_replace("<", "&lt;", $var);
	$var = str_replace(">", "&gt;", $var);

	return ( $var ); 
}

function isPostNew($date)
{
	$rel_time = time() - strtotime($date);
	if (86400 <= $rel_time)
		return 0;
	
	return ((integer)($rel_time / 8640) + 1);
}

function postPerDay($created, $posts)
{
	$time = time() - strtotime($created);
	$days = (date("Y", $time) - 1970) * 365 + date("z", $time);
	$ppd = round($posts / ($days + 1), 3);
	
	return ( $ppd );
}

function longwords($data, $lnt)
{
	$data = explode(" ", $data);
	$data_num = count($data);
	
	for ($i = 0; $i < $data_num; ++$i) {
		if (strlen($data[$i]) > $lnt)
		return 1;
	}
	return 0;
}

function DisplayQuotedText($text)
{
	$text = strip_tags($text, '<b><u><i>');
	$data = explode("\r",$text);
	$size = count($data);
	for ($i = 0; $i < $size; ++$i)
	{
		if ($data[$i])
			echo ">", $data[$i], "\n";
	}
}

function getOptions($user, $dont_quote_replies = 0, $public_email = 0, $sign_posts = 0, $reply_notification = 0)
{
	global $_OPTION;
	
	$data = new UserOptions($user); 
	
	if ($dont_quote_replies)
		$data->Set($_OPTION['dont_quote_replies']);
	else 
		$data->Delete($_OPTION['dont_quote_replies']);
		
	if ($public_email)
		$data->Set($_OPTION['public_email']);	
	else 
		$data->Delete($_OPTION['public_email']);
		
	if ($sign_posts)
		$data->Set($_OPTION['sign_posts']);	
	else 
		$data->Delete($_OPTION['sign_posts']);
		
	if ($reply_notification)
		$data->Set($_OPTION['reply_notification']);	
	else 
		$data->Delete($_OPTION['reply_notification']);
	
	return ( $data->options );
}

function cutSignature($text)
{
	$position = strpos($text, "-- ");
	
	if (!$position)
		return ( $text );
		
	return ( substr( $text, 0, $position) );
}

function quoteText($text)
{
	$data = explode("<br>", $text);
	$text = "";
	$size = count ( $data );
	
	for ($i = 0; $i < $size; ++$i)
	{
		if ($data[$i][0] == '>')
		{
			$tmp = "<span class=\"quote1\">";
			$tmp .= $data[$i];
			$tmp .= "</span>";
			$data[$i] = $tmp;
		}
		$data[$i] .= "<br>";
		$text .= $data[$i];
	}
	return ( $text );
}

function sign($user, $text)
{
		$data = getUserData($user, "signature");
		if ($data['signature'])
		{
			$text .= "<br>";
			$text .= "<span class=\"signature\">";
			$text .= "-- ";
			$text .= "<br>";
			$text .= addslashes(addHTMLLinks($data['signature']));
			$text .= "</span>";
		}
		
		return ( $text );
}

?>