<?php

manfi();

function connectToDatabase(){
	require_once('db.php');
    $connection = new database();
        return $connection;

}


function manfi(){
    $connection = connectToDatabase();

    $id = $_REQUEST['id'];
    $count = $_REQUEST['count'];
    $users_unfollowed = $_REQUEST['users_unfollowed'];
    
    if($id!="" && $connection->checkParameters($id)){
    
    	$connection->q("update users set Coin_Follow=Coin_Follow-$count where ID='$id'");
    	
    	$users = array();
        $users = getCSVValues($users_unfollowed,",");
        
        for ($i = 0; $i < count($users); $i++) {
            $connection->q("update users set Coin_Follow=Coin_Follow+2 where ID='".$users[$i]."'");
        }
    	
    }else {
    	echo "null";
    }
    
}


function getCSVValues($string, $separator=",")
{
    $elements = explode($separator, $string);
    for ($i = 0; $i < count($elements); $i++) {
        $nquotes = substr_count($elements[$i], '"');
        if ($nquotes %2 == 1) {
            for ($j = $i+1; $j < count($elements); $j++) {
                if (substr_count($elements[$j], '"') %2 == 1) { // Look for an odd-number of quotes
                    // Put the quoted string's pieces back together again
                    array_splice($elements, $i, $j-$i+1,
                        implode($separator, array_slice($elements, $i, $j-$i+1)));
                    break;
                }
            }
        }
        if ($nquotes > 0) {
            // Remove first and last quotes, then merge pairs of quotes
            $qstr =& $elements[$i];
            $qstr = substr_replace($qstr, '', strpos($qstr, '"'), 1);
            $qstr = substr_replace($qstr, '', strrpos($qstr, '"'), 1);
            $qstr = str_replace('""', '"', $qstr);
        }
    }
    return $elements;
}


?>