
<?php
$_C00KIES = "cookies.txt";
$aVideos = array("http://www.youtube.com/watch?v=868NCW0jGrU");
$aUsers = array();
$aUsers[0] = array("somemail@gmail.com","password_here");
//$aUsers[1] = array("somemail@gmail.com","password_here");
for($x=0;$x<count($aVideos);$x++){
    for($y=0;$y<count($aUsers);$y++){
        if(file_exists($_C00KIES)){
            @unlink($_C00KIES);
        }
        $sYTLink = $aVideos[$x];
        $sUser = $aUsers[$y][0];
        $sPass = $aUsers[$y][1];
        if(false!==($iPos = strpos($sYTLink,"&"))){
            $sYTLink = substr($sYTLink,0,$iPos);
        }
        $aVID = explode("=",$sYTLink);
        if(!isset($aVID[1])){
            die("Invalid video");
        }
        $sVID = $aVID[1];
        // open gmail and get dynamic field values everytime we start a login attempt
        $ch = curl_init("https://accounts.google.com/ServiceLogin?continue=http%3A%2F%2Fwww.youtube.com%2Fsignin%3Faction_handle_sign​in%3Dtrue%26feature%3Dsign_in_button%26hl%3Dnl_NL%26next%3D%252F%26nomobiletemp%​3D1&passive=true&service=youtube&uilel=3&hl=nl_NL");
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        curl_setopt($ch, CURLOPT_COOKIEFILE, $_C00KIES);
        $sData = curl_exec($ch);
        // get login form
        $sPattern = "/<form(?=[^>]*novalidate id=\"gaia_loginform\")[^>].+<\/form>/Uis";
        preg_match($sPattern,$sData,$aData);
        if(isset($aData[0])){
            // get action
            $sPattern = "/action=\"([^\"]+)/is";
            preg_match($sPattern,$aData[0],$aTarget);
            $sTarget = $aTarget[1];
            // get all inputs
            $sPattern = "/<input[^>]+/is";
            preg_match_all($sPattern,$aData[0],$aInput);
            // go through the inputs and extract the vars and their values
            $sPost = "";
            for($y=0;$y<count($aInput[0]);$y++){
                if($y>0){
                    $sPost .= "&";
                }
                // get var names
                $sPattern = "/name=\"([^\"]+)/is";
                preg_match($sPattern,$aInput[0][$y],$aVar);
                if(!isset($aVar[1])){
                    // no name
                    continue;
                }
                $sPost .= $aVar[1]."=";
                // add pass
                if($aVar[1]=="Passwd"){
                    $sPost .= $sPass;
                    continue;
                }
                // add email/user
                if($aVar[1]=="Email"){
                    $sPost .= $sUser;
                    continue;
                }
                // get values
                $sPattern = "/value=\"([^\"]+)/is";
                preg_match($sPattern,$aInput[0][$y],$aVal);
                if(!isset($aVal[1]) || empty($aVal[1])){
                    // no value
                    continue;
                }
                $sPost .= $aVal[1];
            }
            // in case we are skipping an input with no name
            $sPost = str_replace("&&","&",$sPost);
            curl_setopt($ch, CURLOPT_URL, $sTarget);
            curl_setopt($ch, CURLOPT_POST, true);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $sPost);
            curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-type: application/x-www-form-urlencoded"));
            $sResponse = curl_exec($ch);
            // find the user style string if it is there, login was successful
            $iPos = strpos($sResponse,"refresh");
            if($iPos){
                // ok we have logged in, go to youtube and get the video
                curl_setopt($ch, CURLOPT_URL, $sYTLink);
                curl_setopt($ch, CURLOPT_POST, false);
                curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
                curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
                $sResponse = curl_exec($ch);
                preg_match('/\'PLAYBACK_ID\':\s+\"(.*)\"/',$sResponse,$aPID);
                preg_match('/yt\.setAjaxToken\(\'watch_actions_ajax\',\s+\"(.*)\"/',$sResponse,$aSessionToken);
                if(!isset($aPID[1])){
                    echo"No playback id found";
                }
                elseif(!isset($aSessionToken[1])){
                    echo"No session id found";
                }
                else{
                    // like this video
                    curl_setopt($ch, CURLOPT_URL, "http://www.youtube.com/watch_actions_ajax?action_like_video=1&video_id=".$sVID."&plid=".$aPID[1]);
                    curl_setopt($ch, CURLOPT_POST, true);
                    curl_setopt($ch, CURLOPT_POSTFIELDS, "session_token=".$aSessionToken[1]."&screen=".rawurlencode("h=800&w=1280&d=24"));
                    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
                    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
                    curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-type: application/x-www-form-urlencoded"));
                    $sResponse = curl_exec($ch);
                    curl_close($ch);
                    //echo $sResponse;
                }
            }
            else{
                echo "Login failed";
            }
        }
        else{
            die("Unable to find login form, terminating script\n");
        }
    }
}
?>