<?php
if (!defined('PICONTROL')) exit();

function checkConnectionToFritzbox($address, $version)
{
	if (!class_exists('cURL'))
		(include_once LIBRARY_PATH.'curl/curl.class.php') or die('Error: 9x1001');
	
	$curl = new cURL($address.':49000/'.(($version == 5) ? 'tr64desc' : 'igddesc').'.xml');
	$curl->execute();
	
	if ($curl->getStatusCode() == 200)
		return true;
	
	return false;
}

function getURLFromVersion($version)
{
	switch ($version)
	{
		case 5:
			return 'upnp';
		case 6:
			return 'igdupnp';
		default:
			return 'upnp';
	}
}

function getNameFromConnectionStatus($status)
{
	switch ($status)
	{
		case 'Unconfigured':
			return 'Nicht konfiguriert';
		case 'Connecting':
			return 'Verbinde';
		case 'Authenticating':
			return 'Authentifizierung';
		case 'PendingDisconnect':
			return 'Ausstehende Trennung';
		case 'Disconnecting':
			return 'Trenne';
		case 'Disconnected':
			return 'Getrennt';
		case 'Connected':
			return 'Verbunden';
		default:
			return 'Nicht konfiguriert';
	}
}

function getOptionsForConfigFromUnit($unit)
{
	switch ($unit)
	{
		case 'mbyte':
			return array(
							'unit' => 'MB/s',
							'division' => 1048576
						);
		case 'mbit':
		default:
			return array(
							'unit' => 'MBit/s',
							'division' => 125000
						);
	}
}

function getPublicIP($address, $version)
{
	$ip = trim(shell_exec('curl "'.$address.':49000/'.getURLFromVersion($version).'/control/WANIPConn1" -H "Content-Type: text/xml; charset="utf-8"" -H "SoapAction:urn:schemas-upnp-org:service:WANIPConnection:1#GetExternalIPAddress" -d "<?xml version=\'1.0\' encoding=\'utf-8\'?> <s:Envelope s:encodingStyle=\'http://schemas.xmlsoap.org/soap/encoding/\' xmlns:s=\'http://schemas.xmlsoap.org/soap/envelope/\'> <s:Body> <u:GetExternalIPAddress xmlns:u="urn:schemas-upnp-org:service:WANIPConnection:1" /> </s:Body> </s:Envelope>" -s | grep -Eo \'\<[[:digit:]]{1,3}(\.[[:digit:]]{1,3}){3}\>\''));
	
	if (!filter_var($ip, FILTER_VALIDATE_IP))
		return false;
	
	return $ip;
}

function getConnectionStatus($address, $version)
{
	exec('curl "'.$address.':49000/'.getURLFromVersion($version).'/control/WANIPConn1" -H "Content-Type: text/xml; charset="utf-8"" -H "SoapAction:urn:schemas-upnp-org:service:WANIPConnection:1#GetStatusInfo" -d "<?xml version=\'1.0\' encoding=\'utf-8\'?> <s:Envelope s:encodingStyle=\'http://schemas.xmlsoap.org/soap/encoding/\' xmlns:s=\'http://schemas.xmlsoap.org/soap/envelope/\'> <s:Body> <u:GetStatusInfo xmlns:u="urn:schemas-upnp-org:service:WANIPConnection:1" /> </s:Body> </s:Envelope>" -s | sed -e \'s/<[^>]*>//g;/^$/d\'', $status);
	
	if (!is_array($status) || !is_numeric($status[2]))
		return false;
	
	$output = array('status' => getNameFromConnectionStatus($status[0]),
					'uptime' => $status[2]);
	
	return $output;
}

function getNetworkTraffic($address, $version)
{
	exec('curl "'.$address.':49000/'.getURLFromVersion($version).'/control/WANCommonIFC1" -H "Content-Type: text/xml; charset="utf-8"" -H "SoapAction:urn:schemas-upnp-org:service:WANCommonInterfaceConfig:1#GetAddonInfos" -d "<?xml version=\'1.0\' encoding=\'utf-8\'?> <s:Envelope s:encodingStyle=\'http://schemas.xmlsoap.org/soap/encoding/\' xmlns:s=\'http://schemas.xmlsoap.org/soap/envelope/\'> <s:Body> <u:GetAddonInfos xmlns:u="urn:schemas-upnp-org:service:WANCommonInterfaceConfig:1" /> </s:Body> </s:Envelope>" -s | sed -e \'s/<[^>]*>//g;/^$/d\'', $traffic);
	
	if (!is_array($traffic) || !is_numeric($traffic[4]) || !is_numeric($traffic[5]))
		return false;
	
	$output = array('sent' => $traffic[4],
					'received' => $traffic[5]);
	
	return $output;
}

function getCurrentBandwidth($address, $version)
{
	exec('curl "'.$address.':49000/'.getURLFromVersion($version).'/control/WANCommonIFC1" -H "Content-Type: text/xml; charset="utf-8"" -H "SoapAction:urn:schemas-upnp-org:service:WANCommonInterfaceConfig:1#GetAddonInfos" -d "<?xml version=\'1.0\' encoding=\'utf-8\'?> <s:Envelope s:encodingStyle=\'http://schemas.xmlsoap.org/soap/encoding/\' xmlns:s=\'http://schemas.xmlsoap.org/soap/envelope/\'> <s:Body> <u:GetAddonInfos xmlns:u="urn:schemas-upnp-org:service:WANCommonInterfaceConfig:1" /> </s:Body> </s:Envelope>" -s | sed -e \'s/<[^>]*>//g;/^$/d\'', $bandwidth);
	
	if (!is_array($bandwidth) || !is_numeric($bandwidth[0]) || !is_numeric($bandwidth[1]))
		return false;
	
	$output = array('up' => $bandwidth[0],
					'down' => $bandwidth[1]);
	
	return $output;
}

function getMaxBandwidth($address, $version)
{
	exec('curl "'.$address.':49000/'.getURLFromVersion($version).'/control/WANCommonIFC1" -H "Content-Type: text/xml; charset="utf-8"" -H "SoapAction:urn:schemas-upnp-org:service:WANCommonInterfaceConfig:1#GetCommonLinkProperties" -d "<?xml version=\'1.0\' encoding=\'utf-8\'?> <s:Envelope s:encodingStyle=\'http://schemas.xmlsoap.org/soap/encoding/\' xmlns:s=\'http://schemas.xmlsoap.org/soap/envelope/\'> <s:Body> <u:GetCommonLinkProperties xmlns:u="urn:schemas-upnp-org:service:WANCommonInterfaceConfig:1" /> </s:Body> </s:Envelope>" -s | sed -e \'s/<[^>]*>//g;/^$/d\'', $bandwidth);
	
	if (!is_array($bandwidth) || !is_numeric($bandwidth[1]) || !is_numeric($bandwidth[2]))
		return false;
	
	$output = array('up' => $bandwidth[1],
					'down' => $bandwidth[2]);
	
	return $output;
}

function getModelName($address, $version)
{
	$modelName = trim(shell_exec('curl "'.$address.':49000/'.(($version == 5) ? 'tr64desc' : 'igddesc').'.xml" -s | grep modelName | sed -n 1p | sed -e \'s/<[^>]*>//g;/^$/d\''));
	
	if (!is_string($modelName))
		return false;
	
	return $modelName;
}

function convertToUnit($size, $unit, $current = 'bit')
{
	if ($unit == 'mbit')
	{
		if ($current == 'byte')
			$size *= 8;
		
		return number_format(round($size/1000/1000, 2), 2, ',', '').' MBit/s';
	}
	elseif ($unit == 'mbyte')
	{
		if ($current == 'bit')
			$size /= 8;
		
		return number_format(round($size/1024/1024, 2), 2, ',', '').' MB/s';
	}
	
	return false;
}

function getPercentageFromBandwidth($current, $max)
{
	if ($max == 0)
		return 0;
	
	$percentage = round(($current*8) / $max, 2) * 100;
	
	if ($percentage > 100)
		return 100;
	
	return $percentage;
}
?>