Source for file MAC.php
Documentation is available at MAC.php
<?php // -*- mode:php; tab-width:4; c-basic-offset:4; intent-tabs-mode:nil; -*-
* This file contains the MAC class
* Copyright (c) 2006 Andrew Teixeira
* LICENSE: This source file is subject to version 3.0 of the PHP license
* that is available through the world-wide-web at the following URI:
* http://www.php.net/license/3_0.txt. If you did not receive a copy of
* the PHP License and are unable to obtain it through the web, please
* send a note to license@php.net so we can mail you a copy immediately.
* @license http://www.opensource.org/licenses/bsd-license.php BSD License
* @link http://pear.php.net/package/Net_MAC
* @author Andrew Teixeira <ateixeira@gmail.com>
* $Id: MAC.php,v 1.6 2007/05/01 18:03:57 atex Exp $
* Require PEAR/Exception.php since we will be using PEAR Exceptions
require_once 'PEAR/Exception.php';
* Constant to represent the maximum length of a line in the
define('NET_MAC_LINE_MAXLENGTH', 256 );
* Error constant: signifies no problem (OK)
define('NET_MAC_ERROR_OK', 0 );
* Error constant: signifies a bad option being passed to a function
define('NET_MAC_ERROR_BADOPT', 1 );
* Error constant: signifies bad data being passed to a function
define('NET_MAC_ERROR_BADDATA', 2 );
* Error constant: signifies a bad database connection
define('NET_MAC_ERROR_BADDB', 3 );
* Error constant: signifies a bad manufacturers file
define('NET_MAC_ERROR_BADFILE', 4 );
* Extension of the main PEAR_Exception Class for use with the Net_MAC
* @license http://www.opensource.org/licenses/bsd-license.php BSD License
* @link http://pear.php.net/package/Net_MAC
* @author Andrew Teixeira <ateixeira@gmail.com>
* Class to validate and cleanly format Media Access Control (MAC)
* @license http://www.php.net/license/3_0.txt PHP License 3.0
* @link http://pear.php.net/package/Net_MAC
* @author Andrew Teixeira <ateixeira@gmail.com>
* The MAC address to work on.
* A database instance to use in looking up MAC-to-vendor
* The options to use while connecting to the database.
* The default constructor
* This is the default constructor that will create and populate a
* @param object &$db A valid instantiated {@link }
* http://pear.php.net/package/MDB2/ MDB2} object to use when
* adding/retrieving information from the database for MAC address
* @param array $options An array of options to use with the
* database in retrieving MAC address vendors. The associative
* array should have key/value pairs as follows:
* <li><b>tablename</b>: The name of the table where MAC address
* vendor information lives</li>
* <li><b>macaddrcol</b>: The name of the column containing the
* MAC address prefixes</li>
* <li><b>vendorcol</b>: The name of the column containing the
* <li><b>desccol</b>: The name of the column containing any
* extra descriptive information derived from the vendor list</li>
* @return void No return value. A {@link Net_MAC_Exception}
* Net_MAC_Exception} Exception object will be thrown if there is
* an error during construction so the constructor should be
* called from a try/catch block
if (!$db instanceof MDB2_Driver_Common ) {
if (!isset ($options['tablename'])) {
if (!isset ($options['macaddrcol'])) {
if (!isset ($options['vendorcol'])) {
if (!isset ($options['desccol'])) {
$this->_db->loadModule ('Extended');
} /* end default constructor */
* This function will check a MAC address to make sure it is
* @param string $input The string containing the MAC Address
* @param string $delimiter The string representing the delimiter
* to use when verifying the MAC Address
* @return string <b>true</b> if the MAC address is valid,
static function check($input, $delimiter = ':')
// Check for 6 octets without any punctuation
$retval = preg_match('/^([0-9a-fA-F][0-9a-fA-F]\Q'. $delimiter. '\E){5}([0-9a-fA-F][0-9a-fA-F]){1}$/', $input);
* This method will set the MAC address in the class.
* @param string $macaddr The string representing the MAC address
* @param string $delimiter The string representing the delimiter
* to use when verifying the MAC Address
* @return bool Returns <b>true</b> if the MAC address is set
* correctly, <b>false</b> otherwise
function setMAC($macaddr, $delimiter = ':')
$validMAC = self ::check ($macaddr, $delimiter);
} /* end method setMac */
* This function will format a MAC address into XX:XX:XX:XX:XX:XX
* format from whatever format is passed to the function. The
* delimiter (: in the example above) will be replaced with
* whatever string is passed to the $delimiter parameter
* @param string $input The string containing the MAC Address
* @param string $delimiter The string representing the delimiter
* to use when formatting the MAC Address
* @param bool $uppercase If set to true (default), the
* hexadecimal values in the MAC Address will be returned in
* uppercase. If false, the hexadecimal values will be returned
* @return string The formatted MAC Address or <b>false</b> on
static function format($input, $delimiter = ':', $uppercase = true )
/* Replace all characters not in a valid MAC address with
* nothing. We are going to be testing for a MAC address as
* XXXXXXXXXXXX instead of XX:XX:XX:XX:XX:XX
/* If $uppercase is true, set all the alpha characters to
* uppercase, otherwise set all the alpha characters to
// Check for 6 octets without any punctuation
if (!preg_match('/^([0-9a-fA-F][0-9a-fA-F]){6}$/', $macaddr)) {
// Add back in the $delimiter delimiters
$macaddr = preg_replace('/([0-9a-fA-F][0-9a-fA-F]){1}/', '$1' . $delimiter, $macaddr);
// Remove the trailing $delimiter
$macaddr = preg_replace('/' . $delimiter . '$/', '', $macaddr);
} /* end method format */
* Import a manufacturers' file to the database
* This method will parse a manufacturers' file, such as the one
* http://anonsvn.wireshark.org/wireshark/trunk/manuf}, containing
* a list of MAC address prefix-to-vendor relationships. If the
* $doReturn parameter is <b>false</b>, then the data will be
* imported into the database defined by the factory of this
* class. However, if $doReturn is <b>true</b>, then the return
* will be an associative array with the key being the MAC address
* prefix and the data being an associative array with the keys
* 'vendor' and 'description'.
* @param string $file The filename or URL of the manufacturers'
* @param bool $doReturn If <b>true</b>, an array will be
* returned, if <b>false</b>, the data will be imported into the
* database. (default: false)
* @return mixed If $doReturn is true, the method will return an
* array. Otherwise, the method will return <b>true</b> on
* success. A {@link Net_MAC_Exception Net_MAC_Exception}
* Exception object will be thrown on failure in either case.
$fp = @fopen($file, 'r');
// Prepare parameters for MDB2->buildManipSQL()
$fields, MDB2_AUTOQUERY_INSERT );
$query = $this->_db->prepare ($sql);
if ( preg_match('/^([0-9A-Fa-f][0-9A-Fa-f]:){2}([0-9A-Fa-f][0-9A-Fa-f]){1}/', $line) ) {
// Since the file is space-delimited, we now need to
// reconstruct the description field if it existed
for ($i = 3; $i < count($pieces); $i++ ) {
$desc .= $pieces[$i]. ' ';
if ( (isset ($pieces[0 ])) && (isset ($pieces[1 ])) ) {
$retArr[$pieces[0 ]] = array ('vendor' => $pieces[1 ],
$values = array ($pieces[0 ], $pieces[1 ], $desc);
$result = $query->execute ($values);
return ($doReturn) ? $retArr : true;
} /* end method importVendors */
* Finds the vendor for a MAC address
* This method will search through the database to find a vendor
* that matches the MAC address stored in the class using {@link }
* setMAC setMAC}. If the $macList parameter is set, the method
* will use the array stored in $macList as the data source to
* find the MAC vendor instead of the database. The array would
* have to be an array with the same characteristics as one
* returned from the {@link importVendors importVendors} method
* when using the $doReturn parameter.
* @param bool $getDescription If set to true, the return value
* will be an array with keys 'vendor' and 'description'.
* Normally the method will simply return the vendor name.
* @param array $macList An optional list of MAC-to-vendor
* relationships to search instead of using the
* database. (default: NULL)
* @return mixed Returns an associative array if $getDescription
* is <b>true</b>, returns a string with the vendor name if
* $getDescription is <b>false</b>. If the MAC vendor cannot be
* found in the vendor list, <b>false</b> is returned.
function findVendor($getDescription = false , $macList = NULL )
/* The manufacturers' list only uses the first 3 octets,
* so we need to get that portion of the stored MAC
// Prepare parameters for MDB2->buildManipSQL()
$fields = array ($macaddrcol, $vendorcol, $desccol);
$where = " $macaddrcol = '$macaddr'";
$fields, MDB2_AUTOQUERY_SELECT ,
$query = $this->_db->prepare ($sql);
$result = $query->execute ();
if ( (MDB2 ::isError ($result)) || ($result->numRows () == 0 ) ) {
$macInfo = $result->fetchRow (MDB2_FETCHMODE_ASSOC );
return array ($vendorcol => $macInfo[$vendorcol],
$desccol => $macInfo[$desccol]);
return $macInfo[$vendorcol];
} /* end method findVendor */
} /* end class Net_MAC */
Documentation generated on Mon, 11 Mar 2019 15:17:04 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.
|