<?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'];

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

    public function likes($id)
    {
        if($id > 1){
            return back();
        }
        $pesan = '';
        return view('server.likes',compact('id', 'pesan'));
    }
    
    public function order($id){
        if($id == 1){
            
            // $result = $this->curl('http://ivmedia.tk/apibayu.php','url='.request()->url.'&jumlah='.request()->jumlah);
            // $result = $this->curl('http://209.97.129.35/bowokntolzx.php','urlxx='.request()->url.'&jumlahlahx='.request()->jumlah);
            
            // $api = 'http://209.97.129.35/bowokntolzx.php';
            // $postdata = 'urlxx='.request()->url.'&jumlahlahx='.request()->jumlah;
            
            $username = "ivanz";
        	$password = "kntolz32";
        	$links = request()->url;
        	$jums = request()->jumlah;
        	$result = $this->proses("http://209.97.129.35/bowokntolzx.php","urlxx={$links}&jumlahlahx={$jums}",$username,$password);
        	
        	
            $type = 'Like Server 1';
            
        }else if($id == 2){
            $types = 100;
            $type = 'Like Server 2';
            // $result = $this->curlLikes('api=adfgkih6890kjhd568933nhg45360kh57h&action=add&link='.request()->url.'&type='.$types.'&quantity='.request()->jumlah, 'https://103.252.100.133/api2.php');
            $postdata = 'api=adfgkih6890kjhd568933nhg45360kh57h&action=add&link='.request()->url.'&type='.$types.'&quantity='.request()->jumlah;
            $api = 'https://103.252.100.133/api2.php';
        }else{
            $types = 101;
            $type = 'Like Server 3';
            $postdata = 'api=adfgkih6890kjhd568933nhg45360kh57h&action=add&link='.request()->url.'&type='.$types.'&quantity='.request()->jumlah;
            // $result = $this->curlLikes('api=adfgkih6890kjhd568933nhg45360kh57h&action=add&link='.request()->url.'&type='.$types.'&quantity='.request()->jumlah, 'https://103.252.100.133/api2.php');
            $api = 'https://103.252.100.133/api2.php';
            
        }
        
        
        if($id == 2 || $id == 3)
        {
            
            
            $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{
                        $delay = 15;
                    }
                    \App\Delay::where('user_id', auth()->user()->id)->update(['like_1' => date('Y-m-d H:i:s', strtotime(date('Y-m-d H:i:s') . $delay . ' minute'))]);
    
                $history = new \App\History;
                $history->submitted_by = auth()->user()->username;
                $history->target = request()->url;
                $history->type = $type;
                $history->total_submit = request()->jumlah;
                
                $history->ip = $_SERVER['REMOTE_ADDR'];
                $history->save();
            }
                else
            {
                $pesan = "GAGAL";
            }
            
            
            
            
            
        }else{
            
            
        	
        	
            
             
             $pesan = "SUKSES";
            $level = auth()->user()->level;
                if($level == 'admin'){
                    $delay = 1;
                }else{
                    $delay = 10;
                }
                \App\Delay::where('user_id', auth()->user()->id)->update(['like_4' => date('Y-m-d H:i:s', strtotime(date('Y-m-d H:i:s') . $delay . ' minute'))]);
    
            $history = new \App\History;
            $history->submitted_by = auth()->user()->username;
            $history->target = request()->url;
            $history->type = $type;
            $history->total_submit = request()->jumlah;
            
            $history->ip = $_SERVER['REMOTE_ADDR'];
            $history->save();
            
            
            
            
            
            
            
            
        }
        
            
            
            
            
            
            
            
    
        
            

        
        
        
        
        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);
    }
    
}
