<?php

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

class ServiceController 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'];

                $level = auth()->user()->level;
                if(!in_array($level, $allow)){
                    abort(505);
                }
            }
            else{
                return redirect('/login');
            }
            return $next($request);
        });
    }
    
    public function comment()
    {
        return view('server.comments');
    }
    
    public function story()
    {
        return view('server.story');
    }
    
    public function live()
    {
        return view('server.liveviews');
    }
    
    public function likecomment()
    {
        return view('server.likecomment');
    }
    
    public function dataC()
    {
//         $level = auth()->user()->level;
//                 if($level == 'admin'){
//                     $delay = 1;
//                 }else{
//                     $delay = 15;
//                 }
        
//         \App\Delay::where('user_id', auth()->user()->id)->update(['comment' => date('Y-m-d H:i:s', strtotime(date('Y-m-d H:i:s') . $delay . ' minute'))]);
//     $as = explode("\n", request()->comment);
// 	$c = curl_init('https://api.instagram.com/oembed/?url='.trim(request()->url));
//         curl_setopt($c,CURLOPT_RETURNTRANSFER,1);
//         curl_setopt($c,CURLOPT_SSL_VERIFYPEER,0);
//         curl_setopt($c,CURLOPT_USERAGENT,"SEBASTIANW-PC 11/23.01.78.64 Recognized as Mozilla(AppleWebKit SPA)");
//         $x = curl_exec($c);
//         $xinfo = curl_getinfo($c);
//         curl_close($c);
        
//         if ($xinfo['http_code'] !== 200) {
//             $mediaid = null;
//         } else {
//             $mediaid = json_decode($x);
//             $mediaid = $mediaid->media_id;
//             $mediaid = explode("_", $mediaid);
//             $mediaid = $mediaid[0];
//         }
        
        
    	
//     	$id_media =  $mediaid;
    	
    	
    	
    	

// 		$ch=curl_init("http://209.97.129.35/bowoluvifan.php");
// 		curl_setopt($ch,CURLOPT_SSL_VERIFYPEER, false);
// 		curl_setopt($ch,CURLOPT_POST,1);
// 		curl_setopt($ch,CURLOPT_RETURNTRANSFER,TRUE);
// 		curl_setopt($ch,CURLOPT_POST,TRUE);
// 		curl_setopt($ch,CURLOPT_FOLLOWLOCATION,TRUE);
// 		curl_setopt($ch,CURLOPT_POSTFIELDS,"comment=$jumlah");
// 		curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);		
// 		curl_setopt($ch,CURLOPT_USERAGENT,"Haqny-PC 11/23.01.78.64 Recognized as Mozilla(AppleWebKit SPA)");
// 		curl_setopt($ch,CURLOPT_TIMEOUT,3);
// 		curl_exec($ch);
// 		curl_close($ch);
		
// 		$ch = curl_init();
//         curl_setopt($ch, CURLOPT_URL, $url);
//         curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
//         if($postdata !== ''){
//             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,5);
//         $result = curl_exec($ch);
//         curl_close($ch);
//         return $result;
            
            $arrays = explode("\n", request()->comment);
            
            

            
            foreach ($arrays as $array ) {
    

            $curl = curl_init();
            curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($curl, CURLOPT_URL, 'http://209.97.129.35/bowoluvifan.php?urlxx='.request()->url.'&komeng=@foreach($arrays as $array) {{$arrays}} @endforeach');
            $resp = curl_exec($curl);
            curl_close($curl);
            echo $resp;
    }
            
            
		
// 		$history = new \App\History;
//         $history->submitted_by = auth()->user()->username;
//         $history->target = request()->url;
//         $history->type = 'comment';
//         $history->total_submit = count(explode(PHP_EOL, request()->comment));
//         $history->ip = $_SERVER['REMOTE_ADDR'];
//         $history->save();
        
//         return redirect('/service/comment');   
    //     $username = "ivanz";
    // 	$password = "kntolz32";
    // 	$links = request()->url;
    // 	$coms = request()->comment;
    	
    // 	$result = $this->proses("http://209.97.129.35/bowoluvifan.php","urlxx=".$links."&komeng=".$coms,$username,$password);
    // 	dd($result);
    // 	if($result){
    // 	    echo 'sukses';
    // 	}else{
    // 	    echo 'gagal';
    // 	}

    }
    
    public function datalc(){
        $like = 'like_2';
        if(\Carbon\Carbon::now() < auth()->user()->delay->$like){
            return abort(505);
        }
        
        if(auth()->check()){
                

                $level = auth()->user()->level;
                if($level == 'admin'){
                    $delay = 2;
                }else{
                    $delay = 20;
                }
            }

        
        \App\Delay::where('user_id', auth()->user()->id)->update(['like_2' => date('Y-m-d H:i:s', strtotime(date('Y-m-d H:i:s') . $delay . ' minute'))]);
        
        $url = 'http://zero-seven19.date/api.php?user='.trim(request()->username);

        $ch = curl_init();
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_URL,$url);
        $result=curl_exec($ch);
        curl_close($ch);
        
        $jd = json_decode($result, true);
        
        set_time_limit(0);
        $c = curl_init('https://api.instagram.com/oembed/?url='.trim(request()->url));
        curl_setopt($c,CURLOPT_RETURNTRANSFER,1);
        curl_setopt($c,CURLOPT_SSL_VERIFYPEER,0);
        curl_setopt($c,CURLOPT_USERAGENT,"SEBASTIANW-PC 11/23.01.78.64 Recognized as Mozilla(AppleWebKit SPA)");
        $x = curl_exec($c);
        $xinfo = curl_getinfo($c);
        curl_close($c);
        
        
        if ($xinfo['http_code'] !== 200) {
            $mediaid = null;
        } else {
            $mediaid = json_decode($x);
            $mediaid = $mediaid->media_id;
            $mediaid = explode("_", $mediaid);
            $mediaid = $mediaid[0];
        }
        $data['userid']     = $jd['graphql']['user']['id'];
        $data['jumlah']     = request()->jumlah;
        $data['mediaid']    = $mediaid;
        
        $result = $this->curlLC($data);
        
        $history = new \App\History;
        $history->submitted_by = auth()->user()->username;
        $history->target = request()->url;
        $history->type = 'Like in Comment';
        $history->total_submit = request()->jumlah;
        
        $history->ip = $_SERVER['REMOTE_ADDR'];
        $history->save();
        
        session()->flash('result', $result);
        
        return redirect('/service/likecomment');
        
    }
    
    public function dataVS()
    {   
        $like = 'like_3';
        if(\Carbon\Carbon::now() < auth()->user()->delay->$like){
            return abort(505);
        }
        
        if(auth()->check()){
                

                $level = auth()->user()->level;
                if($level == 'admin'){
                    $delay = 1;
                }else{
                    $delay = 20;
                }
            }
        
        \App\Delay::where('user_id', auth()->user()->id)->update(['like_3' => date('Y-m-d H:i:s', strtotime(date('Y-m-d H:i:s') . $delay . ' minute'))]);
        
        $url = 'http://zero-seven19.date/api.php?user='.trim(request()->username);
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_URL,$url);
        $result=curl_exec($ch);
        curl_close($ch);
        
        $jd = json_decode($result, true);
        
        $history = new \App\History;
        $history->submitted_by = auth()->user()->username;
        $history->target = request()->username;
        $history->type = 'Story Views';
        $history->total_submit = request()->jumlah;
        
        $history->ip = $_SERVER['REMOTE_ADDR'];
        $history->save();
        
        $data['userid']     = $jd['graphql']['user']['id'];
        $data['jumlah']     = request()->jumlah;
        $data['token']      = 'HoDolon05161.uhz';
        
        $result = $this->curlVS($data);
        
        session()->flash('result', $result);
        
        return redirect('/service/story');
        
    }
    
    public function curlLC($data)
    {
        $urlServer = 'https://panel-fp.com/TambahPrivate/api/addlikeinkomen.php?mediaid='.$data['mediaid'].'&count='.$data['jumlah'].'&userid='.$data['userid'];
        $ca = curl_init();
        curl_setopt($ca, CURLOPT_URL, $urlServer);
        curl_setopt($ca, CURLOPT_RETURNTRANSFER, 1);
        
        $data = array(
                    'mediaid'  => $data['mediaid'],
                    'count' => $data['jumlah'],
                    'userid'=> $data['userid'],
                 );
        curl_setopt($ca,CURLOPT_TIMEOUT, 5);
        
        $za = curl_exec($ca);
        curl_close($ca);

        return $za;
    }
    
    public function curlVS($data)
    {
        $urlServer = 'https://panel-fp.com/TambahPrivate/api/addviewsSG.php?user_idz='.$data['userid'].'&jumlah='.$data['jumlah'].'&token_akses='.$data['token'];
        $ca = curl_init();
        curl_setopt($ca, CURLOPT_URL, $urlServer);
        curl_setopt($ca, CURLOPT_RETURNTRANSFER, 1);
        

        curl_setopt($ca,CURLOPT_TIMEOUT, 10);
        
        $za = curl_exec($ca);
        curl_close($ca);

        return $za;
    }
    
    public function curlC($data)
    {
        //CURL TARO SINI NANTI pakai parameter
        $urlServer = 'https://panel-fp.com/TambahPrivate/api/addkomen.php?mediaid='.$data['mediaid'];
        $ca = curl_init();
        curl_setopt($ca, CURLOPT_URL, $urlServer);
        
        curl_setopt($ca,CURLOPT_SSL_VERIFYPEER, false);
		curl_setopt($ca,CURLOPT_POST,1);
		curl_setopt($ca,CURLOPT_RETURNTRANSFER,TRUE);
		curl_setopt($ca,CURLOPT_POST,TRUE);
		curl_setopt($ca,CURLOPT_POSTFIELDS, $data['comment']);
		curl_setopt($ca, CURLOPT_RETURNTRANSFER,1);		
		curl_setopt($ca,CURLOPT_USERAGENT,"Haqny-PC 11/23.01.78.64 Recognized as Mozilla(AppleWebKit SPA)");
		
        
        curl_setopt($ca,CURLOPT_TIMEOUT, 10);
        
        $za = curl_exec($ca);
        curl_close($ca);

        return $za;
    }
    
    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);
    }
    
    
    
}

?>