<?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            = 'Failed to subscribe!';
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    = 'You cannot subscribe to yourself!';
        } 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 already subscribed to this user!';
            } else {
                $sql        = "INSERT INTO video_subscribe (UID, SUID, subscribe_date)  VALUES (" .$user_id. "," .$uid. ", '" .date('Y-m-d'). "')";
                $conn->execute($sql);
                $sql        = "UPDATE signup SET popularity = popularity+1 WHERE UID = " .$user_id. " LIMIT 1";
                $conn->execute($sql);
                $sql        = "UPDATE signup SET points = points+1 WHERE UID = " .$uid. " LIMIT 1";
                $conn->execute($sql);
                $message    = 'Subscribed successfully!';
            }
        }
    } else {
        $message    = 'Please login to subscribe to users!';
    }
}

echo $message;
die();
?>
