* @link 	http://pioder.gim2przemysl.int.pl/
* @license	GNU GPL v3
**/
if ( !defined('IN_uF') )
{
	die('Hacking attempt');
}
class Post
{
	function NewPost($tid, $post, $uid)
	{
		#read last post
		$last = DataBase::new_id(POSTS_TABLE);
		#read last post in topic
		$sql = "SELECT * FROM ".POSTS_TABLE." WHERE t_id='$tid' ORDER BY tp_id DESC LIMIT 1;";
		$query = DataBase::sql_query($sql,GENERAL,'Could not last post information.');
		$result = DataBase::fetch($query);
		$forum = $result['f_id'];//forum id
		$moderate = Forum::ForumInformation($forum,'moderate');
		$tpid = $result['tp_id'];//post in topic id
		$tpid = $tpid+1;
		#
  		$time = time();
		#add new post
  		$sql = "INSERT INTO `".POSTS_TABLE."` VALUES ('$last','$tid', '$uid', '$post', '".$_SERVER['HTTP_USER_AGENT']."', '$time', '$tpid', '$forum','$moderate','".$_SERVER['REMOTE_ADDR']."')";
  		$query = DataBase::sql_query($sql,GENERAL,'Could not add new post.');
  		$result=User::UserInformation($uid,'posts');
  		$result = $result+1;
		TriggerStats($forum, 1);
		TriggerStats($tid, 2);
  		$sql="UPDATE ".USERS_TABLE." SET posts='$result' WHERE u_id='$uid' ";
  		$query = DataBase::sql_query($sql,GENERAL,'Could not update user information.');
  		return $tpid;
	}
	
	function EditPost($postid, $text)
	{
		$sql = "UPDATE `".POSTS_TABLE."` SET text='$text' WHERE `p_id`='$postid';";
		$query = DataBase::sql_query($sql,GENERAL,'Could not edit post.');
	}
	
	function NewTopic($posttext, $ntopic, $forum, $uid, $sticky)
	{
		//Select last topic
		$moderate = Forum::ForumInformation($forum,'moderate');
		$time = time();
		$lastt=DataBase::new_id(TOPICS_TABLE);
		$sql = "INSERT INTO ".TOPICS_TABLE." VALUES ('$lastt', '$forum', '0', '$sticky', '$ntopic', '$uid','$time', '', '')";
		$query = DataBase::sql_query($sql,GENERAL,'Could not add new topic');
		//add post
		//select last post
		$last = DataBase::new_id(POSTS_TABLE);
		//add post
  		$sql = "INSERT INTO ".POSTS_TABLE." VALUES ('$last','$lastt', '$uid', '$posttext','".$_SERVER['HTTP_USER_AGENT']."', '$time', '1', '$forum', '$moderate','".$_SERVER['REMOTE_ADDR']."');";
  		$query = DataBase::sql_query($sql,GENERAL,'Could not add new post.');
  		$sql = "SELECT * FROM ".USERS_TABLE."  WHERE u_id='$uid';";
  		$query = DataBase::sql_query($sql,GENERAL,'Could not obtain user information.');
  		$result = DataBase::fetch($query);
  		$result = $result['posts'];
  		$result = $result+1;
  		
		TriggerStats($forum, 1);
		TriggerStats($lastt, 2);
  		
  		$sql = "UPDATE ".USERS_TABLE." SET posts='$result' WHERE u_id='$uid' ";
  		$query = DataBase::sql_query($sql,GENERAL,'Could not update user information.');
  		return $lastt;
	}
	
	function SmilesShow()
	{
		$text ='';
		$result='';
		$sql = "SELECT * FROM ".SMILES_TABLE."";
		$query = DataBase::sql_query($sql,GENERAL,'Cold not obtain smiles information.');
		$i = 1;
		while($smile = DataBase::fetch($query))
		{
			$action = "insertSmile('".$smile['url']."','".$smile['smile']."')";
			$mouse = "this.style.cursor='hand';";
			$text = "\n".'
 '."\n";
			$result = $result.$text;
			if ($i%5==0)
			{ 
				$i = 1;
				$result=$result.'
';
			}
			else
			{
				$i +=1;
			}
		}
		return $result;
	}
	
	function SmilesReplace($text)
	{
		$sql = "SELECT * FROM ".SMILES_TABLE."";
		$query = DataBase::sql_query($sql,GENERAL,'Could not obtain emoticons information.');
		$i = 1;
		while($result = DataBase::fetch($query))
		{
			$smile[$i]['smile'] = $result['smile'];
			$smile[$i]['url'] = $result['url'];
			$i +=1;
		}
		$smile = (!isset($smile)) ? array() : $smile;
		$i = 1;
		for($i=1;$i<=count($smile);$i++)
		{
    			$text =   str_replace(' '.$smile[$i]['smile'],'
', $text);
			$text =   str_replace(' '.$smile[$i]['smile'],'
', $text);
		}
		return $text;
	}
}
?>