<?php

namespace App\Http\Middleware;

use Closure;
use DB;
use curlHandler;
use steelAuth;
use session;

class ApiGateway
{
    
    public function isUAfound($string, $owned_urls)
    {
        foreach ($owned_urls as $url) {
            //if (strstr($string, $url)) { // mine version
            if (stripos($string, $url) !== FALSE) { // Yoshi version
                //echo "Match found"; 
                return true;
            }
        }
        //echo "Not found!";
        return false;
    }    
    
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request $request
     * @param  \Closure $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        //if( isset($request()->fullUrl()) && isset($_SERVER) )
        {
            $cookievalue = '';
            
            if( count($_COOKIE) > 0 ){
                $cookievalue = json_encode($_COOKIE);
            }
            if(empty($_SERVER['HTTP_USER_AGENT'])){
                $ua = '';
                $ua = json_encode($request -> all());
            } else
                $ua = $_SERVER['HTTP_USER_AGENT'];
            
            if(empty($_SERVER['REMOTE_ADDR']))
                $ra = '';
            else
                $ra = $_SERVER['REMOTE_ADDR'];


            if( $request->has("memId") ){    
                $m = $request->input("memId");
            }    
            else
                $m = 0;
    
            if( $request->has("memid") ){    
                $m = $request->input("memid");
            }    
            else
                $m = 0;
                
                
            if ($m == 102020) {
            return response()->json([
                'status' => [
                    "code" => 2001,
                    'message' => "You are blocked!"
                ]
            ], 400);
        }
                
if ($m == 102020) 
{        
            $_data = array(
    				'memid' => $m,
    				'f_url' => request()->fullUrl(),
    				'f_date' =>date("Y-m-d"),
    				'f_time' => date("H:i:s"),
    				'isNewWebsite' => 'Y',
    				"ipaddress" => $ra,
    				"f_useragent" => $ua,
    				"cookie_value" => $cookievalue . date("Y-m-d"),
    				"f_source" => "api/app/Http/Middleware/ApiGateway.php",
				    'server_variable' => base64_encode(serialize($_SERVER)),
				    'request_variable' => json_encode($request -> all())
    				);
    		
    		DB::table('tbl_urls_visited_count')
    			->insertGetId($_data); 
}
    			
//--------------------START--------------------
//insert in questDB
/*
			    include_once "/home/steelmin/public_html/questdb.php";
			    $arrQryParams = array(				
					'memid' => $m,
    				'f_url' => request()->fullUrl(),
    				'f_date' =>date("Y-m-d"),
    				'f_time' => date("H:i:s"),
    				'isNewWebsite' => 'Y',
    				"ipaddress" => $ra,
    				"f_useragent" => $ua,
    				"cookie_value" => $cookievalue,
    				"f_source" => "api/app/Http/Middleware/ApiGateway.php"    				
					);
			    execQryForAnAction($arrQryParams, "insert", "tbl_urls_visited_count");
*/			    
//insert in questDB
//--------------------STOP--------------------

        }
        return $next($request);
    }
}
