<?php
defined('_VALID') or die('Restricted Access!');

require $config['BASE_DIR']. '/classes/filter.class.php';
require $config['BASE_DIR']. '/include/compat/json.php';
require $config['BASE_DIR']. '/include/adodb/adodb.inc.php';
require $config['BASE_DIR']. '/include/dbconn.php';
require $config['BASE_DIR']. '/include/sessions.php';

disableRegisterGlobals();

$response = array('status' => 0, 'msg' => 'Successfully blocked user!');
if ( isset($_POST['user_id']) ) {
    if ( isset($_SESSION['uid']) ) {
        $filter     = new VFilter();
        $uid        = intval($_SESSION['uid']);
        $user_id    = $filter->get('user_id', 'INTEGER');
        if ( $uid == $user_id ) {
            $response['msg'] = 'You cannot block/unblock yourself!?';
        } else {
            $sql        = "SELECT UID FROM users_blocks WHERE UID = " .$uid. " AND BID = " .$user_id. " LIMIT 1";
            $conn->execute($sql);
            if ( $conn->Affected_Rows() == 1 ) {
                $response['msg'] = 'This user is already blocked!';
            } else {
                $sql    = "INSERT INTO users_blocks (UID, BID) VALUES (" .$uid. ", " .$user_id. ")";
                $conn->execute($sql);
                $sql    = "UPDATE signup SET popularity = popularity-3 WHERE UID = " .$user_id. " LIMIT 1";
                $conn->execute($sql);
                $response['status'] = 1;
            }
        }
    } else {
        $response['msg'] = 'Please login to block users!';
    }
}

echo json_encode($response);
die();
?>
