<?php
class MacAddress
{
private $mac='';
function __construct($mac)
{
$m = trim($mac) ;
if (strpos($m,'-')){ $this->mac = str_replace('-','',$m); }
elseif (strpos($m,'.')){ $this->mac = str_replace('.','',$m); }
elseif (strpos($m,':')){ $this->mac = str_replace(':','',$m); }
else { $this->error('No valid MAC ADDRESS : '.$m); }
}
function output($sep = ':',$digit = 2, $output = 'normal') {
$out = "";
for ($i=1; $i<strlen($this->mac)+1; $i++){
$out .= $this->mac[$i-1];
if (@($i % $digit) == 0 ) $out .= $sep;
}
switch ($output) {
case 'normal':
return rtrim($out,$sep);
break;
case 'upper':
return strtoupper(rtrim ($out,$sep));
break;
case 'lower':
return strtolower(rtrim ($out,$sep));
break;
default:
$this->error('No valid output option : '.$out);
} }
public function GetMacAddr($ifname = 'eth0') {
switch ($_SERVER["OS"]) {
case 'FreeBSD':
$command_name = "/sbin/ifconfig $ifname ";
$condition = "/ether [0-9A-F:]*/i";
$linefeed = "\n";
break;
case 'Windows_NT':
$command_name = "ipconfig /all ";
$condition = "/Physical address[0-9 a-f :-]*|Indirizzo fisico[0-9 a-f :-]*/i";
$linefeed = "<br>";
break;
default:
$command_name = "/sbin/ifconfig $ifname | grep HWadd";
$condition = "/HWaddr (\S+)/i";
$linefeed = "\n";
break;
}
exec($command_name , $command_result);
$MacAdr = "";
foreach($command_result as $value) {
if(preg_match($condition, $value, $match)) {
$MacAdr = substr($value, -17);
break;
} }
if ($MacAdr) return $MacAdr;
else return "(none)";
}
function error($msg){
trigger_error("[Class MacAddress]: " . $msg , E_USER_ERROR);
echo "[Class MacAddress]: " . $msg ."\n";
} }
?>