<?php
// Default assume their is no network connection
$isNetworked = false;
$hasInternet = false;
$regex_ip = '/(?<=inet )[^\s]+/';
$ipAddress = "";

// Check if we have an IP
$ifout = shell_exec('ifconfig eth0');

preg_match($regex_ip, $ifout, $ip, PREG_OFFSET_CAPTURE, 0);

if (isset($ip[0][0])) {

  $interface = "eth0";
  $isNetworked = true;
  $ipAddress = $ip[0][0];

}

// Check wlan if eth fails

if (!$isNetworked) {

  $ifout = shell_exec('ifconfig wlan0');
  preg_match($regex_ip, $ifout, $ip, PREG_OFFSET_CAPTURE, 0);

  if (isset($ip[0][0])) {

    $interface = "wlan0";
    $isNetworked = true;
    $ipAddress = $ip[0][0];
  } else {
    $ipAddress = "0.0.0.0";
  }

}

$ipoctets = explode(".",$ipAddress);

unset($ip);
unset($ifout);

// Check for internet connectivity

if ($isNetworked) {
$curl = curl_init();

curl_setopt_array($curl, array(
  CURLOPT_URL => "https://linetest.iplogin.ca/getIP.php",
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => "",
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 30,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => "GET",
  CURLOPT_POSTFIELDS => "",
));

$response = curl_exec($curl);
$err = curl_error($curl);

curl_close($curl);

if (!$err) {
  $hasInternet = true;
}

}



//echo "<pre>";
//echo $ipAddress." on ";
//echo $interface,"\n";
//if ($hasInternet) { echo "Internet Connected!"; } else { echo "No internet available. Please check your network."; }
//echo "</pre><hr/>";



