fogproject  1.5.9
About: FOG is a Linux-based network computer cloning solution for Windows, Mac OSX and various Linux distributions that ties together a few open-source tools with a PHP-based web interface. FOG doesn’t use any boot disks, or CDs; everything is done via TFTP and PXE.
  Fossies Dox: fogproject-1.5.9.tar.gz  ("unofficial" and yet experimental doxygen-generated source code documentation)  

macaddress.class.php
Go to the documentation of this file.
1 <?php
22 class MACAddress extends FOGBase
23 {
29  private static $_pattern = '';
35  private $_msg = '';
41  private $_Host = null;
47  private $_MAC = null;
53  protected $MAC;
59  protected $tmpMAC;
65  public function __construct($mac)
66  {
71  self::$_pattern = sprintf(
72  '%s%s%s%s',
73  '/^(?:[[:xdigit:]]{2}([-:]))',
74  '(?:[[:xdigit:]]{2}\1){4}[[:xdigit:]]{2}$',
75  '|^(?:[[:xdigit:]]{12})$|^(?:[[:xdigit:]]',
76  '{4}([.])){2}[[:xdigit:]]{4}$/'
77  );
81  parent::__construct();
85  $this->tmpMAC = $mac;
91  $this->setMAC();
96  $this->_MAC = self::getClass('MACAddressAssociation')
97  ->set('mac', $this->__toString())
98  ->load('mac');
99  }
106  protected function setMAC()
107  {
108  try {
117  if ($this->tmpMAC instanceof MACAddressAssociation) {
118  $this->MAC = $this->tmpMAC->get('mac');
119  } elseif (is_array($this->tmpMAC)) {
120  $this->MAC = $this->tmpMAC[0];
121  } else {
122  $this->MAC = $this->tmpMAC;
123  }
124  $this->MAC = self::normalizeMAC($this->MAC);
128  if (!$this->isValid()) {
129  throw new Exception("#!im\n");
130  }
137  $splitter = str_split($this->MAC, 2);
141  foreach ((array) $splitter as &$split) {
142  $hwAddr .= chr(hexdec($split));
143  unset($split);
144  }
151  $this->_msg = sprintf(
152  '%s%s',
153  str_repeat(chr(255), 6),
154  str_repeat($hwAddr, 16)
155  );
156  } catch (Exception $e) {
157  self::$FOGCore->debug(
158  sprintf(
159  '%s MAC: %s',
160  $e->getMessage(),
161  $this->MAC
162  )
163  );
164  }
165  $hwAddr = '';
166  return $this;
167  }
175  protected static function normalizeMAC($mac)
176  {
180  $mac = array_values(
181  preg_grep(
182  self::$_pattern,
183  (array) $mac
184  )
185  );
190  return strtolower(
191  str_replace(
192  array(
193  ':',
194  '-',
195  '.'
196  ),
197  '',
198  $mac[0]
199  )
200  );
201  }
207  public function getMACPrefix()
208  {
226  return implode(
227  '-',
228  str_split(
229  substr(
230  $this->MAC,
231  0,
232  6
233  ),
234  2
235  )
236  );
237  }
243  public function __toString()
244  {
257  return implode(
258  ':',
259  str_split(
260  $this->MAC,
261  2
262  )
263  );
264  }
270  public function isValid()
271  {
276  return (bool) preg_match(
277  self::$_pattern,
278  $this->MAC
279  );
280  }
286  public function isPending()
287  {
288  return (bool) $this->_MAC->isPending();
289  }
295  public function isClientIgnored()
296  {
297  return (bool) $this->_MAC->isClientIgnored();
298  }
304  public function isPrimary()
305  {
306  return (bool) $this->_MAC->isPrimary();
307  }
313  public function isImageIgnored()
314  {
315  return (bool) $this->_MAC->isImageIgnored();
316  }
322  public function getHost()
323  {
324  return $this->_MAC->getHost();
325  }
334  public function wake($ip, $port = 9)
335  {
339  $ret = true;
343  $sock = socket_create(
344  AF_INET,
345  SOCK_DGRAM,
346  SOL_UDP
347  );
351  if ($sock == false) {
352  return false;
353  }
357  $set_opt = socket_set_option(
358  $sock,
359  SOL_SOCKET,
360  SO_BROADCAST,
361  true
362  );
366  if ($set_opt < 0) {
367  socket_close($sock);
368  return false;
369  }
373  $sendto = socket_sendto(
374  $sock,
375  $this->_msg,
376  strlen($this->_msg),
377  0,
378  $ip,
379  $port
380  );
384  if (!$sendto) {
385  $ret = false;
386  }
390  socket_close($sock);
394  return $ret;
395  }
396 }
MACAddress\getMACPrefix
getMACPrefix()
Definition: macaddress.class.php:207
FOGBase\getClass
static getClass($class, $data='', $props=false)
Definition: fogbase.class.php:439
MACAddressAssociation
Definition: macaddressassociation.class.php:23
MACAddress\__toString
__toString()
Definition: macaddress.class.php:243
MACAddress\isPrimary
isPrimary()
Definition: macaddress.class.php:304
$ret
$ret
Definition: bandwidth.php:112
$mac
$mac
Definition: hostinfo.php:151
MACAddress\$_pattern
static $_pattern
Definition: macaddress.class.php:29
MACAddress\$_MAC
$_MAC
Definition: macaddress.class.php:47
MACAddress\$_Host
$_Host
Definition: macaddress.class.php:41
MACAddress\isValid
isValid()
Definition: macaddress.class.php:270
MACAddress\$tmpMAC
$tmpMAC
Definition: macaddress.class.php:59
FOGBase
Definition: fogbase.class.php:25
MACAddress\isPending
isPending()
Definition: macaddress.class.php:286
MACAddress\wake
wake($ip, $port=9)
Definition: macaddress.class.php:334
MACAddress\normalizeMAC
static normalizeMAC($mac)
Definition: macaddress.class.php:175
MACAddress\$MAC
$MAC
Definition: macaddress.class.php:53
MACAddress\setMAC
setMAC()
Definition: macaddress.class.php:106
MACAddress
Definition: macaddress.class.php:23
MACAddress\$_msg
$_msg
Definition: macaddress.class.php:35
MACAddress\isClientIgnored
isClientIgnored()
Definition: macaddress.class.php:295
$ip
if(!(isset($_POST['ip']) &&is_string($_POST['ip']))) if(!(isset($_POST['file']) &&is_string($_POST['file']))) if(!(isset($_POST['lines']) &&is_numeric($_POST['lines']))) if(!(isset($_POST['reverse']) &&is_numeric($_POST['reverse']))) $ip
Definition: logtoview.php:149
MACAddress\getHost
getHost()
Definition: macaddress.class.php:322
MACAddress\isImageIgnored
isImageIgnored()
Definition: macaddress.class.php:313
MACAddress\__construct
__construct($mac)
Definition: macaddress.class.php:65