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

require $config['BASE_DIR']. '/classes/filter.class.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();

$message = NULL;
if ( isset($_POST['user_id']) ) {
    if ( isset($_SESSION['uid']) ) {
        $uid        = intval($_SESSION['uid']);
        $filter     = new VFilter();
        $user_id    = $filter->get('user_id', 'INTEGER');
        if ( $uid == $user_id ) {
            $message    = 'Invalid request!?';
        } else {
            $sql        = "SELECT UID FROM video_subscribe WHERE UID = " .$user_id. " AND SUID = " .$uid. " LIMIT 1";
            $conn->execute($sql);
            if ( $conn->Affected_Rows() != 1 ) {
                $message    = 'You are not subscribed to this user!?';
            } else {
                $sql        = "DELETE FROM video_subscribe WHERE UID = " .$user_id. " AND SUID = " .$uid. " LIMIT 1";
                $conn->execute($sql);
                $sql        = "UPDATE signup SET popularity = popularity-1, points = points-2 WHERE UID = " .$uid. " LIMIT 1";
                $conn->execute($sql);
            }
        }
    } else {
        $message    = 'Please login remove subscriptions!';
    }
} else {
	$message = 'Invalid request!';
}

echo $message;
die();
?>
