<?php
/*
*********************************************************************************************************
* PHP Class
* Copyright (c) 2011 Giulio Calzolari <[email protected]> All Rights Reserved.
*
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
*********************************************************************************************************
* Original author name cannot be removed.
* Authors: Giulio Calzolari <[email protected]>
*********************************************************************************************************
*
*/
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;
} }
/*
var_dump($command_name);
echo "<br><br>";
var_dump($condition);
echo "<br><br>";
var_dump($command_result);
echo "<br><br>";
var_dump($match);
echo "<br><br>";
var_dump($value);
echo "<br><br>";
var_dump($MacAdr);
echo "<br><br>";
*/
if ($MacAdr) return $MacAdr;
else return "(none)";
}
function error($msg){
trigger_error("[Class MacAddress]: " . $msg , E_USER_ERROR);
echo "[Class MacAddress]: " . $msg ."\n";
} }
?>