<?php
global $czpf_plugin;

function send_error_die($msg){
	die(json_encode(array('R'=>'ERR','MSG'=>$msg)));
}

if(!is_user_logged_in()){
	send_error_die('You are not logged in.');
}

if(!isset($_REQUEST['tmp_login_user']) && !is_numeric($_REQUEST['tmp_login_user']) && !is_numeric($_REQUEST['tmp_site_user']) ){
	send_error_die('Error in data');
}


$login_user_lists = get_user_meta($_REQUEST['tmp_login_user'], 'friends_list', true);
$site_user_lists = get_user_meta($_REQUEST['tmp_site_user'], 'friends_list', true);

$tmp_login_user_lists = '';
$tmp_site_user_lists = '';

if(!empty($login_user_lists[$_REQUEST['tmp_site_user']])){
	$tmp_login_user_lists = $login_user_lists[$_REQUEST['tmp_site_user']];
}

if(!empty($site_user_lists[$_REQUEST['tmp_login_user']])){
	$tmp_site_user_lists = $site_user_lists[$_REQUEST['tmp_login_user']];
}

$status = '';

if(empty($tmp_login_user_lists)){
	$login_user_lists[$_REQUEST['tmp_site_user']] = 'pending';
	$site_user_lists[$_REQUEST['tmp_login_user']] = 'requests';
	$status = 'true';
} else if($tmp_login_user_lists == 'requests'){
	$login_user_lists[$_REQUEST['tmp_site_user']] = 'ok';
	$site_user_lists[$_REQUEST['tmp_login_user']] = 'ok';
	$status = 'true';
} else if($tmp_login_user_lists == 'pending' || $tmp_login_user_lists == 'ok' ){
	unset($login_user_lists[$_REQUEST['tmp_site_user']]);
	unset($site_user_lists[$_REQUEST['tmp_login_user']]);
	$status = 'false';
}

update_user_meta($_REQUEST['tmp_login_user'], 'friends_list', $login_user_lists);
update_user_meta($_REQUEST['tmp_site_user'], 'friends_list', $site_user_lists);

die(json_encode(array('R'=>'OK','status'=>$status,'MSG'=>__('Save done','rhc'))));









?>