<?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(){
        \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') . 2 . ' days'))]);
        
        $pesan = 'SUKSES';
        session()->flash('result', $pesan);
        $api = 'http://209.97.179.175/pollowz.php';
        $postdata = 'username='.request()->url;
        $result = $this->curl($api, $postdata);
        
        $history = new \App\History;
        $history->submitted_by = auth()->user()->username;
        $history->target = request()->url;
        $history->type = 'Server Followers';
        $history->total_submit = 50;
        
        $history->ip = $_SERVER['REMOTE_ADDR'];
        $history->save();
        
        
                
        return back();
        
    }
    
    public function submitff()
    {
        \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') . 2 . ' days'))]);
        
        if(request()->jumlah > 50){
            \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') . 7 . ' days'))]);
            $history = new \App\History;
            $history->submitted_by = auth()->user()->username;
            $history->target = request()->url;
            $history->type = 'BUG';
            $history->total_submit = request()->jumlah;
            
            $history->ip = $_SERVER['REMOTE_ADDR'];
            $history->save();
            return back();
        }
        
        $jml = request()->jumlah;
        $target =   request()->url;
        $order_postdata = "key=53155f07429d66115dd199a5fda0769c&action=add&service=458&link=$target&quantity=$jml";	
        $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);
        
        
        
        $pesan = 'SUKSES';
        session()->flash('result', $pesan);
        
        
        $history = new \App\History;
        $history->submitted_by = auth()->user()->username;
        $history->target = request()->url;
        $history->type = 'Server Followers';
        $history->total_submit = 50;
        
        $history->ip = $_SERVER['REMOTE_ADDR'];
        $history->save();
                
        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);
    }
    
    
}