<?php
	require_once("../system.engine/system.core.php");
	
	$mPong = array();
	$a = &$mPong;
	$p = &$_POST;
	
	global $systemHandler;
	$systemExecution = $systemHandler->loadHandler("EXECUTION");
	$systemHash = $systemHandler->loadHandler("HASH");
	/* ### DO YOUR CODES ONLY FROM HERE ### */
	
	$mFriendId = $p["friendId"];
	$mUserId = $systemHandler->loadHandler("USER")->mUserId;
	
	if($systemExecution->runQuery("SELECT COUNT(id) AS requestsCount FROM web_friendrequests WHERE (sender_id = '%s' AND receiver_id = '%s') OR (sender_id = '%s' AND receiver_id = '%s')", array($mUserId, $mFriendId, $mFriendId, $mUserId))->fetch_object()->requestsCount == 0 && $mUserId !== $mFriendId) {
		$systemExecution->runQuery("INSERT INTO web_friendrequests (sender_id, receiver_id) VALUES ('%s', '%s')", array($mUserId, $mFriendId));
		$a["isValid"] = true;
	} else $a["isValid"] = false;
	
	/* ### DO NOT CHANGE ANYTHING FROM HERE ### */
	header("Content-Type: application/json");
	echo json_encode($mPong);
?>