<?php
/**
 * Cotonti Social Functions
 */

defined('COT_CODE') or die('Wrong URL');

require_once cot_langfile('cotsocial', 'plug');

global $db_cot_social_friendslist, $db_cot_social_userwall, $db_x;
$db_cot_social_friendslist = (isset($db_cot_social_friendslist)) ? $db_cot_social_friendslist : $db_x . 'social_friendslist';
$db_cot_social_userwall = (isset($db_cot_social_userwall)) ? $db_cot_social_userwall : $db_x . 'social_userwall';

function cot_social_check_friend($userid, $friendid)
{

	global $db, $db_cot_social_friendslist;
	
	$sql1 = $db->query("SELECT * FROM $db_cot_social_friendslist WHERE userid = $userid AND friendid = $friendid LIMIT 1");
	$friendsExist1 = $sql1->rowCount();
	
	$sql2 = $db->query("SELECT * FROM $db_cot_social_friendslist WHERE userid = $friendid AND friendid = $userid LIMIT 1");
	$friendsExist2 = $sql2->rowCount();
	
	if($friendsExist1 > 0){$active = $sql1->fetch();}
	if($friendsExist2 > 0){$active = $sql2->fetch();}
	
	if($friendsExist1 > 0 || $friendsExist2 > 0)
	{
	
		return true;
		
	}
	else
	{
	
		return false;
		
	}
	
}

function cot_social_check_friend_status($userid, $friendid)
{

	global $db, $db_cot_social_friendslist;
	
	$sql1 = $db->query("SELECT * FROM $db_cot_social_friendslist WHERE userid = $userid AND friendid = $friendid LIMIT 1");
	$friendsExist1 = $sql1->rowCount();
	
	$sql2 = $db->query("SELECT * FROM $db_cot_social_friendslist WHERE userid = $friendid AND friendid = $userid LIMIT 1");
	$friendsExist2 = $sql2->rowCount();
	
	if($friendsExist1 > 0){$active = $sql1->fetch();}
	if($friendsExist2 > 0){$active = $sql2->fetch();}
	
	if(($friendsExist1 > 0 || $friendsExist2 > 0) && $active['status'] > 0)
	{
	
		return true;
		
	}
	else
	{
	
		return false;
		
	}
	
}

function cot_social_post($onuser, $fromuser, $parent, $time, $message)
{

	global $db, $db_cot_social_userwall;
	$error = 0;
	
	if(empty($onuser) || empty($message))
	{
		$error = true;
	}
	if(!cot_social_check_friend_status($onuser, $fromuser) && $onuser != $fromuser)
	{
		$error = true;
	}
	
	if(!$error)
	{

		$db->insert($db_cot_social_userwall, array(
			'onuser' => (int)$onuser,
			'fromuser' => (int)$fromuser,
			'parent' => (int)$parent,
			'date' => (int)$time,
			'message' => $message
			));
			
		cot_redirect(cot_url('users', 'm=details&id=' . $onuser, '', true));
		
	}
	else
	{
	
		cot_redirect(cot_url('users', 'm=details&id=' . $onuser, '', true));
		
	}

}

function cot_social_remove($id)
{

	global $db, $db_cot_social_userwall;
	$error = 0;

	$sql = $db->query("SELECT * FROM $db_cot_social_userwall WHERE id = $id");
	$data = $sql->fetch();
	
	$check = 0;
	
	if($data['onuser'] == $_SESSION['cot_user_id'] || $data['fromuser'] == $_SESSION['cot_user_id'])
	{
		$check = 1;
	}

	if($check > 0)
	{
	
		$db->delete($db_cot_social_userwall, "id='".$id."' LIMIT 1");
		cot_redirect(cot_url('users', 'm=details&id=' . $data['onuser'], '', true));
	
	}
	else
	{
	
		cot_redirect(cot_url('users', 'm=details&id=' . $data['onuser'], '', true));
		
	}
	
}

function cot_social_add_friend($userid, $friendid, $time)
{

	global $db, $db_cot_social_friendslist;
	
	if(!cot_social_check_friend_status($userid, $friendid))
	{
	
		$db->insert($db_cot_social_friendslist, array(
			'userid' => (int)$userid,
			'friendid' => (int)$friendid,
			'date' => (int)$time
			));
			
		cot_redirect(cot_url('users', 'm=details&id=' . $userid . '&action=sent', '', true));
		
	}
	else
	{
	
		cot_redirect(cot_url('users', 'm=details&id=' . $userid, '', true));
	
	}

	
}

function cot_social_remove_friend($userid, $friendid)
{

	global $db, $db_cot_social_friendslist;

	if(cot_social_check_friend_status($userid, $friendid))
	{
	
		$sql1 = $db->query("SELECT * FROM $db_cot_social_friendslist WHERE userid = $userid AND friendid = $friendid LIMIT 1");
		$execute1 = $sql1->rowCount();
		
		$sql2 = $db->query("SELECT * FROM $db_cot_social_friendslist WHERE userid = $friendid AND friendid = $userid LIMIT 1");
		$execute2 = $sql2->rowCount();
		if($execute1 > 0){$data = $sql1->fetch();}
		if($execute2 > 0){$data = $sql2->fetch();}
	
		$db->delete($db_cot_social_friendslist, "id='".$data['id']."' LIMIT 1");
		cot_redirect(cot_url('users', 'm=details&id=' . $userid, '', true));
		
	}
	else
	{
	
		cot_redirect(cot_url('users', 'm=details&id=' . $userid, '', true));
	
	}
	
}

function cot_social_accept_request($userid, $friendid)
{

	global $db, $db_cot_social_friendslist;
	
	if(cot_social_check_friend($userid, $friendid))
	{
	
		$sql = $db->query("SELECT * FROM $db_cot_social_friendslist WHERE userid = $userid AND friendid = $friendid");
		$data = $sql->fetch();
	
		if(!cot_social_check_friend_status($userid, $friendid) && $friendid != $data['friendid'])
		{
		
			$db->update($db_cot_social_friendslist, array('status' => '1'));
			cot_redirect(cot_url('users', 'm=details&id=' . $userid, '', true));
	
		}
		else
		{
		
			cot_redirect(cot_url('users', 'm=details&id=' . $userid, '', true));
		
		}
	
	}
	else
	{
	
		cot_redirect(cot_url('users', 'm=details&id=' . $userid, '', true));
	
	}
	
}

?>