<?php

  /*
  * A PHP Function which checks if the IP Address specified is a Proxy Server utilising the API provided by https://proxycheck.io
  * This function is covered under an MIT License.
  */

  $proxycheck_cache_dir = 'tmp/proxycheck/';
  
  if (!is_dir($proxycheck_cache_dir)) {
    mkdir($proxycheck_cache_dir, 0755, true);
  }

  function proxycheck_function($Visitor_IP) {
    $cache_file = $GLOBALS['proxycheck_cache_dir'] . md5($Visitor_IP) . '.cache';
    
    if (file_exists($cache_file)) {
      $cache_data = json_decode(file_get_contents($cache_file), true);
      if ($cache_data && (time() - $cache_data['timestamp']) < 86400) {
        return $cache_data['result'];
      }
    }

    // ------------------------------
    // SETTINGS
    // ------------------------------

    $API_Key = "80538i-120uf8-878579-wa4639"; // Supply your API key between the quotes if you have one
    $VPN = "1"; // Change this to 1 if you wish to perform VPN Checks on your visitors
    $TLS = "0"; // Change this to 1 to enable transport security, TLS is much slower though!
    $TAG = "1"; // Change this to 1 to enable tagging of your queries (will show within your dashboard)
    
    // If you would like to tag this traffic with a specific description place it between the quotes.
    // Without a custom tag entered below the domain and page url will be automatically used instead.
    $Custom_Tag = ""; // Example: $Custom_Tag = "My Forum Signup Page";

    // ------------------------------
    // END OF SETTINGS
    // ------------------------------

    // Setup the correct querying string for the transport security selected.
    if ( $TLS == 1 ) {
      $Transport_Type_String = "https://";
    } else {
      $Transport_Type_String = "http://";
    }
    
    // By default the tag used is your querying domain and the webpage being accessed
    // However you can supply your own descriptive tag or disable tagging altogether above.
    if ( $TAG == 1 && $Custom_Tag == "" ) {
      $Post_Field = "tag=" . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
    } else if ( $TAG == 1 && $Custom_Tag != "" ) {
      $Post_Field = $Custom_Tag;
    } else {
      $Post_Field = "";
    }
    
    // Performing the API query to proxycheck.io/v2/ using cURL
    $ch = curl_init($Transport_Type_String . 'proxycheck.io/v2/' . $Visitor_IP . '?key=' . $API_Key . '&vpn=' . $VPN);
    
    $curl_options = array(
      CURLOPT_CONNECTTIMEOUT => 30,
      CURLOPT_POST => 1,
      CURLOPT_POSTFIELDS => $Post_Field,
      CURLOPT_RETURNTRANSFER => true
    );
    
    curl_setopt_array($ch, $curl_options);
    $API_JSON_Result = curl_exec($ch);
    curl_close($ch);
    
    // Decode the JSON from our API
    $Decoded_JSON = json_decode($API_JSON_Result);

    // Check if the IP we're testing is a proxy server
    if ( isset($Decoded_JSON->$Visitor_IP->proxy) && $Decoded_JSON->$Visitor_IP->proxy == "yes" ) {
      // A proxy has been detected.
      $result = true;
    } else {
      // No proxy has been detected.
      $result = false;
    }
    
    // Cache result
    $cache_data = [
      'timestamp' => time(),
      'result' => $result
    ];
    file_put_contents($cache_file, json_encode($cache_data));
    
    return $result;
  }

  function proxycheck_get_provider($Visitor_IP) {
    // Check cache
    $cache_file = $GLOBALS['proxycheck_cache_dir'] . md5($Visitor_IP) . '_provider.cache';
    
    if (file_exists($cache_file)) {
      $cache_data = json_decode(file_get_contents($cache_file), true);
      // Expire cache after 24 hours to not waste space
      if ($cache_data && (time() - $cache_data['timestamp']) < 86400) {
        return $cache_data['result'];
      }
    }

    // ------------------------------
    // SETTINGS
    // ------------------------------

    $API_Key = "80538i-120uf8-878579-wa4639"; // Supply your API key between the quotes if you have one
    $VPN = "1"; // Change this to 1 if you wish to perform VPN Checks on your visitors
    $TLS = "0"; // Change this to 1 to enable transport security, TLS is much slower though!
    $TAG = "1"; // Change this to 1 to enable tagging of your queries (will show within your dashboard)
    
    // If you would like to tag this traffic with a specific description place it between the quotes.
    // Without a custom tag entered below the domain and page url will be automatically used instead.
    $Custom_Tag = ""; // Example: $Custom_Tag = "My Forum Signup Page";

    // ------------------------------
    // END OF SETTINGS
    // ------------------------------

    // Setup the correct querying string for the transport security selected.
    if ( $TLS == 1 ) {
      $Transport_Type_String = "https://";
    } else {
      $Transport_Type_String = "http://";
    }
    
    // By default the tag used is your querying domain and the webpage being accessed
    // However you can supply your own descriptive tag or disable tagging altogether above.
    if ( $TAG == 1 && $Custom_Tag == "" ) {
      $Post_Field = "tag=" . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
    } else if ( $TAG == 1 && $Custom_Tag != "" ) {
      $Post_Field = $Custom_Tag;
    } else {
      $Post_Field = "";
    }
    
    // Performing the API query to proxycheck.io/v2/ using cURL
    $ch = curl_init($Transport_Type_String . 'proxycheck.io/v2/' . $Visitor_IP . '?key=' . $API_Key . '&vpn=' . $VPN);
    
    $curl_options = array(
      CURLOPT_CONNECTTIMEOUT => 30,
      CURLOPT_POST => 1,
      CURLOPT_POSTFIELDS => $Post_Field,
      CURLOPT_RETURNTRANSFER => true
    );
    
    curl_setopt_array($ch, $curl_options);
    $API_JSON_Result = curl_exec($ch);
    curl_close($ch);
    
    // Decode the JSON from our API
    $Decoded_JSON = json_decode($API_JSON_Result);

    // Check if the IP we're testing is a proxy server and get provider info
    if ( isset($Decoded_JSON->$Visitor_IP->proxy) && $Decoded_JSON->$Visitor_IP->proxy == "yes" ) {

      // Get the operator
      if ( isset($Decoded_JSON->$Visitor_IP->operator->name) ) {
        $result = $Decoded_JSON->$Visitor_IP->operator->name;
      } else if ( isset($Decoded_JSON->$Visitor_IP->provider) ) {
        // Fallback to provider if operator name is not available
        $result = $Decoded_JSON->$Visitor_IP->provider;
      } else if ( isset($Decoded_JSON->$Visitor_IP->type) ) {
        // If neither operator name nor provider is available, find the type of it
        $result = ucfirst($Decoded_JSON->$Visitor_IP->type);
      } else {
        // Fallback to generic VPN if no specific info is available
        $result = "VPN";
      }
      
    } else {
      
      // No proxy has been detected.
      $result = false;
    }
    
    // Cache the result
    $cache_data = [
      'timestamp' => time(),
      'result' => $result
    ];
    file_put_contents($cache_file, json_encode($cache_data));
    
    return $result;
  }

?>
