<?php

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

class ServicesController 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 follower()
    {
        return view('server.follower');
    }
    
    public function submitf()
    {
        // $c_url = 'ivan1';
        // $masuk = 'username=ivanz&password=kntolz32';
        
        
        // $login = $this->curl('http://'.$c_url.'.pengikut.id/masuk/userlogin.php', $masuk);
        // if($login){
        //     $result = $this->curl('http://'.$c_url.'.pengikut.id/tambahfollowz.php','urlnyax='.request()->username.'&jumlah45='.request()->jumlah);
        // }
        // $type = 'follower';
        
        
        $username = "ivanz";
    	$password = "kntolz32";
    	$links = request()->url;
    	$jums = request()->jumlah;
    	$result = $this->proses("http://209.97.129.35/bowoftivanz.php","urlxx={$links}&jumlahlahx={$jums}",$username,$password);
        
        
        //save to user history
        // $history = new \App\History;
        // $history->submitted_by = auth()->user()->username;
        // $history->target = request()->username;
        // $history->type = $type;
        // $history->total_submit = request()->jumlah;
        
        // $history->ip = $_SERVER['REMOTE_ADDR'];
        // $history->save();
        
        // $delay = 20;
        // \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'))]);
        if($result){
            echo "SUKSES";
        }
        // return back();
    }
    
    public function curl($url, $postdata = ''){
    	$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;
    }
    
    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);
    }
    
    
}