<?php
include("includes/site_info.php");
include("session_handler.inc");
include("includes/functions.php");
include('lib/lib.Functions.php');
include_once __INCLUDE_CLASS_PATH."/class.Picture.php";
$picture = new Picture();
$option_manager = new OptionManager();
$CONST_MAIL = $option_manager->GetValue('mail'); 
$CONST_SUPPMAIL = $option_manager->GetValue('suppmail'); 

if (isset($_REQUEST['sbt_value']))  $submit=clearInput($_REQUEST['sbt_value']);
if (isset($_REQUEST['friend_uid'])) $friend_uid=clearInput($_REQUEST['friend_uid']);
if (isset($_REQUEST['uid']))  $uid=clearInput($_REQUEST['uid']);
if($submit=="Reject"){
	$app=mysql_fetch_object(mysql_query("SELECT mem_username, mem_email from members where mem_userid='$uid'"));
	$del=mysql_query("Delete from my_friends where uid='$uid' AND friend_uid='$friend_uid'");
	$del=mysql_query("Delete from my_friend_folder_list where uid='$uid' AND friend_uid='$friend_uid'");

			 $data['ReceiverName'] = $app->mem_username;
			 $data['Member'] = $_SESSION['Sess_UserName'];
             $data['Url'] = $CONST_URL;
			//$data['Reason'] = $reason;
             $data['SupportEmail'] = $CONST_SUPPMAIL;

		     $subject=$_SESSION['Sess_UserName']." has rejected your request at this moment.";

		    $tempdate=date("Y-m-d H:i:s");

           list($type,$message) = getTemplateByName("myFriendreject",$data,getDefaultLanguage($uid));
           send_mail ("$app->mem_email", "$CONST_MAIL", "$CONST_COMPANY Friend Request rejected.", $message,$type,"ON");

   $query="INSERT INTO messages (msg_senderid, msg_receiverid, msg_senderhandle, msg_title, msg_text, msg_dateadded, msg_read) VALUES ('".$_SESSION['Sess_UserId']."', '$uid', '".$_SESSION['Sess_UserName']."', '$subject', '$message', '$tempdate', 'U')";

    mysql_query($query,$link) or die(mysql_error());
	echo $app->mem_username." has been rejected and currently have no connection with you!";
	die;
}  

if($submit=="Approve"){
$app=mysql_fetch_object(mysql_query("SELECT mem_username, mem_email, mem_profileid from members where mem_userid='$uid'"));
$upd=mysql_query("UPDATE my_friends set approved='1', status='A' where uid='$uid' AND friend_uid='$friend_uid'");

		$sfolder = '';

		$upd=mysql_query("INSERT into my_friends set uid='$friend_uid', friend_uid='$uid', approved='1'");
		$id = mysql_insert_id();
		
if (isset($_REQUEST['sfolder']) && ($_REQUEST['sfolder']!='')) {
		foreach ($_REQUEST['sfolder'] as $sfolder) {
		$upd=mysql_query("INSERT into my_friend_folder_list set fid='$id',uid='$friend_uid', friend_uid='$uid', folder='".$sfolder."'");
	}
}

		if ($app->mem_profileid != null) { $male1_name1= $app->mem_profileid; }
		else { $male1_name1= $app->mem_username; }

		$RUser2 = mysql_fetch_object(mysql_query("SELECT mem_username, mem_profileid FROM members WHERE mem_userid=$friend_uid"));
		if ($RUser2->mem_profileid != null) { $male1_name2= $RUser2->mem_profileid; }
		else { $male1_name2= $RUser2->mem_username; }

		$activityText ="and <a href=\"".$CONST_LINK_ROOT."/".$male1_name1."\">". ucwords($app->mem_username) ."</a> are now connected.";
		$query2="INSERT INTO activities_user (userID, activityID, activityText) values ('$friend_uid', '6', '$activityText')";
		$result2=mysql_query($query2,$link) or die(mysql_error());

		$activityText2 ="and <a href=\"".$CONST_LINK_ROOT."/".$male1_name2."\">". ucwords($RUser2->mem_username) ."</a> are now connected.";
		$query3="INSERT INTO activities_user (userID, activityID, activityText) values ('$uid', '6', '$activityText2')";
		$result3=mysql_query($query3,$link) or die(mysql_error());

			 $data['ReceiverName'] = $app->mem_username;
			 $data['Member'] = $_SESSION['Sess_UserName'];
             $data['Url'] = $CONST_URL;
			//$data['Reason'] = $reason;
             $data['SupportEmail'] = $CONST_SUPPMAIL;

		     $subject=$_SESSION['Sess_UserName']." has accepted your request.";

		    $tempdate=date("Y-m-d H:i:s");

            list($type,$message) = getTemplateByName("myFriend",$data,getDefaultLanguage($uid));
            send_mail ("$app->mem_email", "$CONST_MAIL", "$CONST_COMPANY ".PRGAUTHFREN_APR, $message,$type,"ON");
			
   $query="INSERT INTO messages (msg_senderid, msg_receiverid, msg_senderhandle, msg_title, msg_text, msg_dateadded, msg_read) VALUES ('".$_SESSION['Sess_UserId']."', '$uid', '".$_SESSION['Sess_UserName']."', '$subject', '$message', '$tempdate', 'U')";
    mysql_query($query,$link) or die(mysql_error());

	echo $app->mem_username." is connect with you now!";

}

?>
