<?php
 
namespace App\Http\Controllers;
 
use Illuminate\Http\Request;
use \App\Rules\Count;
use DB;
 
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)
    {
        $level = auth()->user()->level;
        
        // if($id == 3){
        //     if($level !== 'admin'){
        //         return back();
        //     }
        // }
       
        if($id > 3 ){
            return back();
        }
        
 
       
        if($id < 1 ){
            return back();
        }
       
       
       
       
       
        $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();
       
        $ceksisa = \App\History::where('submitted_by', \Auth::User()->username)
                                            ->where('type', 'Like Server 3')
                                            ->whereDate('created_at', '=', date('Y-m-d'))->sum('total_submit');
                                           
        $pesan = '';
        return view('server.likes',compact('id', 'pesan', 'sev1', 'sev2', 'ceksisa'));
    }
   
    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){
       
        $url = request()->url;
        if($url == NULL){
            $pesan = 'Mohon isi form url terlebih dahulu !';
            session()->flash('nulx', $pesan);
            return redirect('service/like/'.$id);
        }
       
        $jmlsub = request()->jumlah;
        if($jmlsub == NULL){
            $pesan = 'Mohon isi form jumlah submit terlebih dahulu !';
            session()->flash('nulx', $pesan);
            return redirect('service/like/'.$id);
        }
       
        if(\Auth::User()->username == 'Demo'){
            $pesan = "SUKSES";
            session()->flash('result', $pesan);
            return redirect('service/like/'.$id);
        }
       
       
       
        $level = auth()->user()->level;
       
        $dcode = 'minute';
      if($level == 'admin'){
            $delay = 0;
        }else if($level == 'reseller' || $level == 'member'){
$delayxx = \App\History::where('submitted_by', \Auth::User()->username)->where('type', 'Like Server '. $id)->whereDate('created_at', '=', date('Y-m-d'))->count();
          if($delayxx <= 3){
                 $delay = 50;
             }else if($delayxx < 4){
                $delay = 150;
             }else{
                 $delay = 4;
                 $dcode = 'hour';
        }
        }else if($level == 'priority'){
          
          if($id == 1){
            $delay = 50;    
          }else{
              $delayxx = \App\History::where('submitted_by', \Auth::User()->username)->where('type', 'Like Server '. $id)->whereDate('created_at', '=', date('Y-m-d'))->count();
              if($delayxx <= 1){
                    $delay = 20;
                }else if($delayxx < 2){
                    $delay = 50;
                }else{
                    $delay = 1;
                    $dcode = 'hour';
                }
          }
        }else{
          $delay = 50;
        }
        
        
 
        // if($level == 'admin'){
        //     $delay = 0;
        // }else if($level == 'priority'){
        //   $delay = 15;
        // }elseif($level == 'reseller'){
        //     $delayxx = \App\History::where('submitted_by', \Auth::User()->username)
        //                                             ->where('type', 'Like Server '. $id)
        //                                             ->whereDate('created_at', '=', date('Y-m-d'))->count();
                                                   
        //     if($delayxx > 15){
        //         $dcode = 'hour';
        //         $delay = 1;
        //     }else{
        //         if($jmlsub <= 1000){
        //             $delay = 5;
        //         }else{
        //             $delay = 8;
        //         }
        //     }
        // }else{
        // $delayxx = \App\History::where('submitted_by', \Auth::User()->username)
        //                                             ->where('type', 'Like Server '. $id)
        //                                             ->whereDate('created_at', '=', date('Y-m-d'))->count();
        //     if($delayxx < 2){
        //         $delay = 8;
        //     }else if($delayxx < 10){
        //         $delay = 10;
        //     }else if($delayxx < 15){
        //         $delay = 20;
        //     }else if($delayxx < 20){
        //         $delay = 60;
        //         $delay = 3;
        //         $dcode = 'hour';
        //     }
           
        //     if($jmlsub > 1000){
        //         $delay = $delay * 2;
        //     }
         
        // }
       
 
 
 
        error_reporting(0);
       
        $c=curl_init('https://api.instagram.com/oembed/?url='.trim($url));
        curl_setopt($c,CURLOPT_RETURNTRANSFER,1);
        curl_setopt($c,CURLOPT_SSL_VERIFYPEER,0);
        curl_setopt($c,CURLOPT_USERAGENT,"Mozilla/5.0 (Windows NT 6.1; rv:21.0) Gecko/20100101 Firefox/21.0");
        $x=curl_exec($c);
        $mama=curl_getinfo($c);
        curl_close($c);
        preg_match('#provider_url#',$x);
        $obj = json_decode($x,true);
        $media = $obj['media_id'];
        $authid = $obj['author_id'];
       
        $cc=curl_init($url);
        curl_setopt($cc,CURLOPT_RETURNTRANSFER,1);
        curl_setopt($cc,CURLOPT_SSL_VERIFYPEER,0);
        curl_setopt($cc,CURLOPT_USERAGENT,"Mozilla/5.0 (Windows NT 6.1; rv:21.0) Gecko/20100101 Firefox/21.0");
        $x1=curl_exec($cc);
        $mama1=curl_getinfo($cc);
        curl_close($cc);
        preg_match('/\"edge_media_preview_like\"\:\s?\{\"count\"\:\s?([0-9]+)/',$x1,$l);
        $ses_data['start'] =  $l[1];
         
        $ses_data['target'] = request()->url;
       
        session()->flash('ses_data', $ses_data);
       
        // $result = $this->curl($apiku,'url='.request()->url.'&jumlah='.request()->jumlah.'&server=like'.$id);
        
       
       
       
        $cekhisto = DB::table('users_history')->where('target', request()->url)->count();
           
        if($id == 1 || $id == 2 || $id == 3){
           
           
            if($id == 1){
                /*
                === | UBAI | ===
                $jeda = 'like_5';
                $type = 'Like Server 1';
                $max = 200;
                $types = 112;
                */
            
                    $jeda = 'like_5';
           
                $max = 100;
                $type = 'Like Server 1';
                $types = 306;
            }else if($id == 2){
                if($level == 'priority' || $level == 'member' || $level == 'reseller' ){
                    $jeda = 'like_5';
                }else{
                    $jeda = 'like_1';
                }
                $max = 100;
                $type = 'Like Server 2';
                $types = 306;
               
            }else{
                $max = 300;
                $jeda = 'like_6';
                $type = 'Like Server 3';
                
            }
           
            if($id == 1){
                if($level == 'priority'){
                    $limsev = 3;
                }else if($level == 'member'){
                    $limsev = 10;
                }else if($level == 'reseller'){
                    $limsev = 10;
                }
            }else{
                if($level == 'priority'){
                    $limsev = 3;
                }else if($level == 'member'){
                    $limsev = 10;
                }else if($level == 'reseller'){
                    $limsev = 10;
                }
            }
           
            if($id == 1){
                $apikey = "7edd7db27bbde12fc861332c77438b09"; // API Key Anda
                $fitur = "Likess"; // Fitur Layanan, tersedia: Likes, Followers, Comment
                $postdata = "apikey=$apikey&fitur=$fitur&target=".request()->url."&jumlah=".request()->jumlah;
                
                
                /*
                $key = '2019gantidiaivanbangsatbocahtolol';
                $api = 'https://followers-gratis.com/api.php';
               
                $postdata = 'api='.$key.'&action=add&link='.request()->url.'&type='.$types.'&quantity='.request()->jumlah;
                */
            }else if($id == 2){
                $key = 'Ah8Zf93do6Krt0cPOYNMeVE5BXwnUL';
                $api = 'https://mentalispanel.id/api/json.php';
               
                $postdata = 'api_key='.$key.'&action=order&data='.request()->url.'&service=3681000&quantity='.request()->jumlah;
            }else{
                // $postdata = 'url='.request()->url.'&jumlah='.request()->jumlah;
                $api_key = "1acb7c1494d750d39139ddffa5ab2c5c";
                $pid = 1621;
                $post_link = request()->url;
                $post_quantity = request()->jumlah;
                
                $order_postdata = "key=$api_key&action=add&service=$pid&link=$post_link&quantity=$post_quantity";	
            }
 
 
            $sev = \App\History::where('submitted_by', \Auth::User()->username)
                                                ->where('type', 'Like Server '. $id)
                                                ->whereDate('created_at', '=', date('Y-m-d'))->count();
   
            if($level == 'priority' || $level == 'member' || $level == 'reseller' ){
                if($sev > $limsev){
                    session()->flash('ses_lim', 'Maaf Saat Ini Kamu Limit Server Sudah Habis Harap Tunggu Esok Pagi Untuk Submit Ulang !');
                    return redirect()->back();
                }
            }
   
           
 
            // if($id == 1){
            //     $result = $this->curl('http://curl.pengikut.id/ivlike1.php','urlxx='.request()->url.'&jumlahlahx='.request()->jumlah);
            // }else{
            //     $result = $this->curl('http://curl.pengikut.id/ivlike2.php','urlxx='.request()->url.'&jumlahlahx='.request()->jumlah);
            // }
           
            if($id == 1){
                $ch = curl_init();
                curl_setopt($ch, CURLOPT_URL, "https://panel.pengikut.id/api/order.php");
                curl_setopt($ch, CURLOPT_POST, 1);
                curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata);
                curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
                curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
                $chresult = curl_exec($ch);
                curl_close($ch);
                $json_result = json_decode($chresult, true);
                
                
                	$order_id = $json_result['id'];
                	$statusorder = "Pending";
                
                /*
                $ch = curl_init('https://followers-gratis.com/api.php');
           
                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)');
                $results = curl_exec($ch);
                if (curl_errno($ch)) {
                    $results = curl_error($ch);
                }
                       
                curl_close($ch);
               
                $order = json_encode($results);
                $ambil= json_decode($results,true);
                
                $order_id=$ambil['info']['order']['order_id'];
                $statusorder = $ambil['info']['order']['status'];
                */
            }else if($id == 2){
                $ch = curl_init('https://mentalispanel.id/api/json.php');
           
                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)');
                $results = curl_exec($ch);
                if (curl_errno($ch)) {
                    $results = curl_error($ch);
                }
                       
                curl_close($ch);
               
                $order = json_encode($results);
                $ambil= json_decode($results,true);
                
                $order_id=$ambil['data']['id'];
                $statusorder = 'Pending';
            }else{
                $ch = curl_init();
                curl_setopt($ch, CURLOPT_URL, "http://smmlite.com/api/v2");
                curl_setopt($ch, CURLOPT_POST, 1);
                curl_setopt($ch, CURLOPT_POSTFIELDS, $order_postdata);
                curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
                curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
                $chresult = curl_exec($ch);
                curl_close($ch);
                $order_data = json_decode($chresult, true);
                
                $order_id = $order_data['order'];
                $statusorder = 'Pending';
                
                
            }
            // else{
            //     $result = $this->curl('http://curl.pengikut.id/ivlike2.php','urlxx='.request()->url.'&jumlahlahx='.request()->jumlah);
            // }
            
            // DELAY
           if($id == 1 || $id == 2){
               if(isset($order_id)){
                    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();
                        }
           
                    \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 . $dcode))]);
                        $pesan = "SUKSES";
                    
                    // SUB SERVER DB
                    $user_id = auth()->user()->id;
                    $cek = DB::table('subserver')->where('user_id', $user_id)->where('date', date('Y-m-d'))->get();
                    $data = DB::table('subserver')->where('user_id', $user_id)->where('date', date('Y-m-d'))->first();
                    if(count($cek) == 0){
                      if($id == 1){
                        $ins = DB::table('subserver')
                          ->insert([
                            'user_id' => $user_id,
                            'server1' => 1,
                            'server2' => 0,
                            'date' => date('Y-m-d')
                          ]);
                      }else{
                        $ins = DB::table('subserver')
                          ->insert([
                            'user_id' => $user_id,
                            'server1' => 0,
                            'server2' => 1,
                            'date' => date('Y-m-d')
                          ]);
                      }
                    }else{
                      if($id == 1){
                        $ins = DB::table('subserver')
                          ->where('user_id', $user_id)
                          ->where('date', date('Y-m-d'))
                          ->update([
                            'server1' => $data->server1 + 1,
                          ]);
                      }else{
                        $ins = DB::table('subserver')
                          ->where('user_id', $user_id)
                          ->where('date', date('Y-m-d'))
                          ->update([
                            'server2' => $data->server2 + 1,
                          ]);
                      }
                    }
                   
                    $ins = DB::table('cekhistory')
                                ->insert([
                                    'o_id' => $order_id,
                                    'target' => $url,
                                    'status' => $statusorder,
                                ]);
                   
                   
                   
                    $history = new \App\History;
                    $history->submitted_by = auth()->user()->username;
                    $history->target = request()->url;
                    $history->type = $type;
                   
                   
                    $history->total_submit = request()->jumlah;
                    $history->status = $statusorder;
                    $history->order_id = $order_id;
                    $history->start = $ses_data['start'];
                   
                    $history->ip = $_SERVER['REMOTE_ADDR'];
                    $history->save();
                   }else{
                       $pesan = 'GAGAL';
                   }
           }else{
               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();
                }
   
                \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 . $dcode))]);
                $pesan = "SUKSES";
               
                $history = new \App\History;
                $history->submitted_by = auth()->user()->username;
                $history->target = request()->url;
                $history->type = $type;
               
               
                $history->total_submit = request()->jumlah;
                $history->status = $statusorder;
                $history->order_id = $order_id;
                $history->start = $ses_data['start'];
               
                $history->ip = $_SERVER['REMOTE_ADDR'];
                $history->save();
           }
        }
       
        // if($id == 3){
           
        //     $cek = \App\History::where('submitted_by', \Auth::User()->username)
        //                                     ->where('type', 'Like Server 3')
        //                                     ->whereDate('created_at', '=', date('Y-m-d'))->sum('total_submit');
                                           
        //     if(request()->jumlah >= 2000){
        //         \App\Delay::where('user_id', auth()->user()->id)->update(['like_6' => date('Y-m-d H:i:s', strtotime(date('Y-m-d H:i:s') . 24 . ' hour'))]);
        //         return back();
        //     }
                                           
        //     if($cek <= 2000){                      
           
        //         $post_link = request()->url;
        //         $post_quantity = request()->jumlah;
        //         $order_postdata = "key=53155f07429d66115dd199a5fda0769c&action=add&service=450&link=$post_link&quantity=$post_quantity";
        //         $ch = curl_init();
        //         curl_setopt($ch, CURLOPT_URL, 'https://justanotherpanel.com/api/v2');
        //         curl_setopt($ch, CURLOPT_POST, 1);
        //         curl_setopt($ch, CURLOPT_POSTFIELDS, $order_postdata);
        //         curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        //         curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        //         $chresult = curl_exec($ch);
        //         curl_close($ch);
        //         $order_data = json_decode($chresult, true);
               
        //         $poid = $order_data['order'];
               
        //         $history = new \App\History;
        //         $history->submitted_by = auth()->user()->username;
        //         $history->target = request()->url;
        //         $history->type = 'Like Server 3';
        //         $history->total_submit = request()->jumlah;
        //         $history->start = $ses_data['start'];
               
        //         $history->ip = $_SERVER['REMOTE_ADDR'];
        //         $history->save();
               
        //         $cek = \App\History::where('submitted_by', \Auth::User()->username)
        //                                     ->where('type', 'Like Server 3')
        //                                     ->whereDate('created_at', '=', date('Y-m-d'))->sum('total_submit');
               
        //         if($cek >= 2000){
        //             \App\Delay::where('user_id', auth()->user()->id)->update(['like_6' => date('Y-m-d H:i:s', strtotime(date('Y-m-d H:i:s') . 24 . ' hour'))]);
        //         }
           
        //         $pesan = 'SUKSES';
        //     }else{
        //         $pesan = 'LIMIT';
        //     }
        // }
           
       
       
       
       
        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);
 
    }
   
    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_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_TIMEOUT,10);
        $result = curl_exec($ch);
        curl_close($ch);
        return $result;
    }
   
    public function ubai($postdata){
       
       
        $ch = curl_init('https://followers-gratis.com/api.php');
           
        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)');
        $results = curl_exec($ch);
        if (curl_errno($ch)) {
            $results = curl_error($ch);
        }
               
        curl_close($ch);
     
    }
   
    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);
    }
   
}