<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use \App\Rules\Count;

class LikeController extends Controller
{
	public function __construct()
    {
        $this->middleware(function ($request, $next) {
            if(auth()->check()){

                //SET PERMISSION WHO CAN VISIT THIS CONTROLLER
                $allow = ['admin', 'member', 'reseller', 'priority', 'junior'];

                $level = auth()->user()->level;
                if(!in_array($level, $allow)){
                    abort(505);
                }
            }
            else{
                return redirect('/login');
            }
            return $next($request);
        });
    }

    public function likes($id)
    {
        if($id > 2 ){
            return back();
        }
        
        
        if($id < 1){
            return back();
        }
        
        if($id == 1){
            return back();
        }
        

 
        
        
        $a = \App\History::where('submitted_by', \Auth::User()->username)
                                            ->where('type', 'Like Server 1')
                                            ->whereDate('created_at', '=', date('Y-m-d'))->sum('total_submit');
                                            
        $b = \App\History::where('submitted_by', \Auth::User()->username)
                                        ->where('type', 'Like Server 2')
                                        ->whereDate('created_at', '=', date('Y-m-d'))->sum('total_submit');
        
        $sev1 = \App\History::where('submitted_by', \Auth::User()->username)
                                            ->where('type', 'Like Server 1')
                                            ->whereDate('created_at', '=', date('Y-m-d'))->count();
                                            
        $sev2 = \App\History::where('submitted_by', \Auth::User()->username)
                                            ->where('type', 'Like Server 2')
                                            ->whereDate('created_at', '=', date('Y-m-d'))->count();
        
     

        $pesan = '';
        return view('server.likes',compact('id', 'pesan', 'a', 'b', 'sev1', 'sev2'));
    }
    
    public function isJson($string) {
        return ((is_string($string) &&
        (is_object(json_decode($string)) ||
        is_array(json_decode($string))))) ? true : false;
    }
    
    public function getLikeCount($url){
        if(preg_match('/taken-by/', $url))
        {
            $url = $url;
            $url .= "&__a=1";
        }
        else
        {
            $url = $url."?__a=1";
        }
     
        $getContent = file_get_contents($url);
        $dataContent = json_decode($getContent, 1);
     
        if(!$this->isJson($getContent))
        {
            echo "Url tidak ditemukan.";
        }
        else
        {
            echo $dataContent['graphql']['shortcode_media']['edge_media_preview_like']['count'];
        }
    }
    
    
    
    public function order($id){
        
            
        if($id == 1){
            $jmlsub = request()->jumlah;
            $level = auth()->user()->level;
                if($level == 'admin'){
                    $delay = 1;
                }else{
                    
                    
                        if($id == 1){
                            if($jmlsub < 1000){
                                $delay = 20;
                            }else{
                                 $delay = 30;
                            }
                        
                        }else{
                            if($jmlsub < 250){
                                $delay = 20;
                            }else{
                                $delay = 30;
                            }
                        }

                }
                $delays = 40;
                
            if($id == 1){
                $type = 'Like Server 1';
                $jeda = 'like_5';
                $max = 2000;
                $apiku = 'http://209.97.179.175/curl/ivantololbgstzkntlx.php';
            }else{
                $type = 'Like Server 2';
                $jeda = 'like_1';
                $max = 500;
                $apiku = 'http://45.55.59.66/curl/ivanzselowwwxxxx.php';
            }
            
            $sev1 = \App\History::where('submitted_by', \Auth::User()->username)
                                            ->where('type', 'Like Server 1')
                                            ->whereDate('created_at', '=', date('Y-m-d'))->count();
            
            
            $a = \App\History::where('submitted_by', \Auth::User()->username)
                                            ->where('type', 'Like Server 1')
                                            ->whereDate('created_at', '=', date('Y-m-d'))->sum('total_submit');
                                            
            $b = \App\History::where('submitted_by', \Auth::User()->username)
                                            ->where('type', 'Like Server 2')
                                            ->whereDate('created_at', '=', date('Y-m-d'))->sum('total_submit');
            if($level == 'priority'){
                $tsisa = 2000;
            }else{
                $tsisa = 5000;
            }
            $sisanya = $tsisa - $a;
            
            if($level == 'member'){
                if($sev1 > 10){
                    session()->flash('ses_lim', 'Maaf Saat Ini Kamu Limit Server Sudah Habis Harap Tunggu Esok Pagi Untuk Submit Ulang !');
                    return redirect()->back();
                }
            }
            
            $ceksisanya = $sisanya - request()->jumlah;
            if($level == 'priority'){
                if($sisanya > 0){
                    if(request()->jumlah > $sisanya){
                        session()->flash('ses_lim', 'Jumlah yang Anda masukkan melebihi sisa batas limit Anda !');
                        return redirect()->back();
                    }else if($ceksisanya < 0){
                        session()->flash('ses_lim', 'Jumlah yang Anda masukkan melebihi sisa batas limit Anda !');
                        return redirect()->back();
                    }
                }
            }else{
                if($sisanya > 0){
                    if(request()->jumlah > $sisanya){
                        session()->flash('ses_lim', 'Jumlah yang Anda masukkan melebihi sisa batas limit Anda !');
                        return redirect()->back();
                    }
                }
            }
            
            
            
            
            \App\Delay::where('user_id', auth()->user()->id)->update(['like_9' => date('Y-m-d H:i:s', strtotime(date('Y-m-d H:i:s') . $delays . ' minute'))]);
            
            if($a >= $tsisa){
                \App\Delay::where('user_id', auth()->user()->id)->update([$jeda => date('Y-m-d H:i:s', strtotime(date('Y-m-d H:i:s') . 3 . ' hour'))]);
            }else{
                \App\Delay::where('user_id', auth()->user()->id)->update([$jeda => date('Y-m-d H:i:s', strtotime(date('Y-m-d H:i:s') . $delay . ' minute'))]);
            }
                
                if(request()->jumlah > $max){
                    \App\Delay::where('user_id', auth()->user()->id)->update([$jeda => date('Y-m-d H:i:s', strtotime(date('Y-m-d H:i:s') . 24 . ' hour'))]);
                    return back();
                }
            
            
            
            // $result = $this->curl('http://ivmedia.tk/apibayu.php','url='.request()->url.'&jumlah='.request()->jumlah);
            $url = request()->url;
            error_reporting(0);
            if(preg_match('/taken-by/', $url))
            
                {
                    $url = $url;
                    $url .= "&__a=1";
                }
                else
                {
                    $url = $url."?__a=1";
                }
             
                $getContent = file_get_contents($url);
                $dataContent = json_decode($getContent, 1);
             
                if(!$this->isJson($getContent))
                {
                    $ses_data['start'] =  0;
                }
                else
                {
                  $ses_data['start'] =  $dataContent['graphql']['shortcode_media']['edge_media_preview_like']['count'];
                }
            
             
            $ses_data['target'] = request()->url;
            
            session()->flash('ses_data', $ses_data);
            
            $result = $this->curl($apiku,'urlxx='.request()->url.'&jumlahlahx='.request()->jumlah);
        
            
            // $api = 'http://167.99.202.150/kuy/bowokntolzx.php';
            // $postdata = 'urlxx='.request()->url.'&jumlahlahx='.request()->jumlah;
            $history = new \App\History;
            $history->submitted_by = auth()->user()->username;
            $history->target = request()->url;
            $history->type = $type;
            $history->total_submit = request()->jumlah;
            $history->start = $ses_data['start'];
            
            $history->ip = $_SERVER['REMOTE_ADDR'];
            $history->save();
            
        //     $username = "ivanz";
        // 	$password = "kntolz32";
        // 	$links = request()->url;
        // 	$jums = request()->jumlah;
        // 	$result = $this->proses("http://167.99.202.150/kuy/newkntlzzz.php","urlxx={$links}&jumlahlahx={$jums}",$username,$password);
        	
        	$pesan = "SUKSES";
            
                
                
            
            
        // }else if($id == 2 || $id == 3 || $id == 4 || $id == 5 || $id == 6){
        }else if($id == 2){
            
            // $result = $this->curlLikes('api=adfgkih6890kjhd568933nhg45360kh57h&action=add&link='.request()->url.'&type='.$types.'&quantity='.request()->jumlah, 'https://103.252.100.133/api2.php');
            
            if($id == 2){
                $type = 'Like Server 2';
                $api = 'https://followers-gratis.com/api.php';
                $types = 120;
            }
            
            
            
            $key = '2019gantidiaivanbangsatbocahtolol';
            
            $postdata = 'api='.$key.'&action=add&link='.request()->url.'&type='.$types.'&quantity='.request()->jumlah;
            // $postdata = 'api='.$key.'&url='.request()->url.'&jumlah='.request()->jumlah.'&service=120';
            
            error_reporting(0);
                if(preg_match('/taken-by/', $url))
                
                    {
                        $url = $url;
                        $url .= "&__a=1";
                    }
                    else
                    {
                        $url = $url."?__a=1";
                    }
                 
                    $getContent = file_get_contents($url);
                    $dataContent = json_decode($getContent, 1);
                 
                    if(!$this->isJson($getContent))
                    {
                        $ses_data['start'] =  0;
                    }
                    else
                    {
                      $ses_data['start'] =  $dataContent['graphql']['shortcode_media']['edge_media_preview_like']['count'];
                    }
            
            
            
            
            
        }else if($id == 4){
            
            $history = new \App\History;
            $history->submitted_by = auth()->user()->username;
            $history->target = request()->url;
            $history->type = 'Like Server 4';
            $history->total_submit = request()->jumlah;
            
            $history->ip = $_SERVER['REMOTE_ADDR'];
            $history->save();
            
            $username = "ivanz";
        	$password = "kntolz32";
        	$links = request()->url;
        	$jums = request()->jumlah;
        	$result = $this->proses("http://167.99.202.150/kuy/ivankntlz69.php","urlxx={$links}&jumlahlahx={$jums}",$username,$password);
        	
            // $result = $this->curlLikes('api=adfgkih6890kjhd568933nhg45360kh57h&action=add&link='.request()->url.'&type='.$types.'&quantity='.request()->jumlah, 'https://103.252.100.133/api2.php');
            
                    	$pesan = "SUKSES";
            $level = auth()->user()->level;
                if($level == 'admin'){
                    $delay = 1;
                }else{
                    // if(request()->jumlah <= 50){
                    //             $delay = 2;
                    //         }else if(request()->jumlah <= 125){
                    //             $delay = 3;
                    //         }else if(request()->jumlah <= 250){
                    //             $delay = 6;
                    //         }else{
                    //             $delay = 12;
                    // }
                    if(request()->jumlah <= 600){
                                $delay = 20;
                            }else{
                                $delay = 30;
                            
                    }
                    
                    
                    
                }
                $delays = 40;
                \App\Delay::where('user_id', auth()->user()->id)->update(['like_5' => date('Y-m-d H:i:s', strtotime(date('Y-m-d H:i:s') . $delay . ' minute'))]);
                \App\Delay::where('user_id', auth()->user()->id)->update(['like_9' => date('Y-m-d H:i:s', strtotime(date('Y-m-d H:i:s') . $delays . ' minute'))]);
                
                if(request()->jumlah > 1100){
                    \App\Delay::where('user_id', auth()->user()->id)->update(['like_5' => date('Y-m-d H:i:s', strtotime(date('Y-m-d H:i:s') . 24 . ' hour'))]);
                    return back();
                    
                }
        	
            
        }else{
            $pesan = "GAGAL";
        }
        
        
        
        if( $id == 2)
        {
            $b = \App\History::where('submitted_by', \Auth::User()->username)
                    ->where('type', 'Like Server 2')
                    ->whereDate('created_at', '=', date('Y-m-d'))->sum('total_submit');
            if($level == 'priority'){
                $tsisa = 2000;
            }else{
                $tsisa = 7500;
            }
            $sisanya = $tsisa - $b;
            
            $sev2 = \App\History::where('submitted_by', \Auth::User()->username)
                                            ->where('type', 'Like Server 2')
                                            ->whereDate('created_at', '=', date('Y-m-d'))->count();
                                            
                                            
            if($level == 'member'){
                if($sev2 > 8){
                    session()->flash('ses_lim', 'Maaf Saat Ini Kamu Limit Server Sudah Habis Harap Tunggu Esok Pagi Untuk Submit Ulang !');
                    return redirect()->back();
                }
            }
            
            
            
            $ceksisanya = $sisanya - request()->jumlah;
            if($level == 'priority'){
                if($sisanya > 0){
                    if(request()->jumlah > $sisanya){
                        session()->flash('ses_lim', 'Jumlah yang Anda masukkan melebihi sisa batas limit Anda !');
                        return redirect()->back();
                    }else if($ceksisanya < 0){
                        session()->flash('ses_lim', 'Jumlah yang Anda masukkan melebihi sisa batas limit Anda !');
                        return redirect()->back();
                    }
                }
            }else{
                if($sisanya > 0){
                    if(request()->jumlah > $sisanya){
                        session()->flash('ses_lim', 'Jumlah yang Anda masukkan melebihi sisa batas limit Anda !');
                        return redirect()->back();
                    }
                }
            }
            
            
            
            
            $ch = curl_init($api);
    		
    		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($ch, CURLOPT_POST, 1);
            curl_setopt($ch, CURLOPT_HEADER, 0);
            curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    		curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
    		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        		
        		
            curl_setopt($ch, CURLOPT_POST, true);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata);
        
    		
    		curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)');
            $result = curl_exec($ch);
            if (curl_errno($ch)) {
                $result = curl_error($ch);
            }
    				
            curl_close($ch);
            
            
            
            $order = json_encode($result);
            
            $ambil=	json_decode($result,true);
            
            
            
            
            if(isset($ambil['info']['order']['order_id']))
            {
                
                $pesan = "SUKSES";
                $level = auth()->user()->level;
                    if($level == 'admin'){
                        $delay = 1;
                    }else{
                
                            if(request()->jumlah <= 1000){
                                $delay = 20;    
                            }else{
                                $delay = 30;
                            }
                            
                            
                           
                        
                    }
                    
                // if($id == 2){
                //     $jeda = 'like_1';
                // }else if($id == 3){
                //     $jeda = 'like_6';
                // }else if($id == 5){
                //     $jeda = 'like_7';
                // }else if($id == 6){
                //     $jeda = 'like_8'; 
                // }else{
                //     $pesan = "GAGAL";
                // }
                
                if($id == 2){
                    $jeda = 'like_1';
                }else if($id == 3){
                    $jeda = 'like_6';
                }else if($id == 4){
                    $jeda = 'like_7';
                }else if($id == 5){
                    $jeda = 'like_8';
                }else if($id == 6){
                    $jeda = 'like_9';
                    $delay = 16;
                }else{
                    $pesan = "GAGAL";
                }
                
                $level = auth()->user()->level;
                if($level == 'priority'){
                    $jeda = 'like_5';
                }
                
                
                    
                
                if($b >= $tsisa){
                    \App\Delay::where('user_id', auth()->user()->id)->update([$jeda => date('Y-m-d H:i:s', strtotime(date('Y-m-d H:i:s') . 4 . ' hour'))]);
                }else{
                    \App\Delay::where('user_id', auth()->user()->id)->update([$jeda => date('Y-m-d H:i:s', strtotime(date('Y-m-d H:i:s') . $delay . ' minute'))]);
                }
                \App\Delay::where('user_id', auth()->user()->id)->update(['like_9' => date('Y-m-d H:i:s', strtotime(date('Y-m-d H:i:s') . 25 . ' minute'))]);
                // if($id == 2 || $id == 3){
                //     if(request()->jumlah > 600){
                //     \App\Delay::where('user_id', auth()->user()->id)->update([$jeda => date('Y-m-d H:i:s', strtotime(date('Y-m-d H:i:s') . 24 . ' hour'))]);
                //     return back();
                //     }
                // }else if($id == 5){
                //     if(request()->jumlah > 600){
                //     \App\Delay::where('user_id', auth()->user()->id)->update([$jeda => date('Y-m-d H:i:s', strtotime(date('Y-m-d H:i:s') . 24 . ' hour'))]);
                //     return back();
                //     }
                // }else{
                //     if(request()->jumlah > 2000){
                //     \App\Delay::where('user_id', auth()->user()->id)->update([$jeda => date('Y-m-d H:i:s', strtotime(date('Y-m-d H:i:s') . 24 . ' hour'))]);
                //     return back();
                //     }
                // }
                
                if(request()->jumlah > 1200){
                    \App\Delay::where('user_id', auth()->user()->id)->update([$jeda => date('Y-m-d H:i:s', strtotime(date('Y-m-d H:i:s') . 24 . ' hour'))]);
                    return back();
                }
                
                
    
                $history = new \App\History;
                $history->submitted_by = auth()->user()->username;
                $history->target = request()->url;
                $history->type = $type;
                $history->total_submit = request()->jumlah;
                $history->start = $ses_data['start'];
                
                $history->ip = $_SERVER['REMOTE_ADDR'];
                $history->save();
                
            }
                else
            {
                $pesan = "GAGAL";
            }
            
            
            
            
            
        }
        
        
        
        
        
        session()->flash('result', $pesan);
        
        return redirect('service/like/'.$id);
	}
	
	public function curlLikes($postdata, $api)
	{
	    $ch = curl_init($api);
		
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
		curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    		
    		
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata);
    
		
		curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)');
        $result = curl_exec($ch);
        if (curl_errno($ch)) {
            $result = curl_error($ch);
        }
				
        curl_close($ch);
        
        // $order = json_encode($result);
        // $ambil=	json_decode($order,true);
        
        // if(isset($ambil['info']['order']['order_id']))
        // {
        //     $order_id=$ambil['info']['order']['order_id'];
        //     if($order_id > 0)
        //     {
        //         dd($order_id);
        //     }
        // }else{
        //     $gagal = "GAGAL";
        //     dd($gagal);
        // }
	}
	
	public function curl($url, $postdata = ''){
    	$ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
        
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata);
        
        // curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookie.txt');
        // curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie.txt');

        // curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        // curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_TIMEOUT,10);
        $result = curl_exec($ch);
        curl_close($ch);
        return $result;
    }
    
    function proses($url,$params,$username,$password){
            
    	 $ch = curl_init();
         curl_setopt($ch, CURLOPT_POST, 1);
         curl_setopt($ch, CURLOPT_POSTFIELDS, "username=$username&password=$password");
         curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
         curl_setopt($ch, CURLOPT_COOKIEJAR, "-");
         curl_setopt($ch, CURLOPT_COOKIEFILE, "-");
         $result = curl_exec($ch);
         curl_setopt($ch, CURLOPT_URL, $url);
         curl_setopt($ch, CURLOPT_POST, 1);
         curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
         $result = curl_exec($ch);
         curl_close($ch);
    }
    
}
