Improved MAC address formatting
This commit is contained in:
		
							parent
							
								
									02980bbad5
								
							
						
					
					
						commit
						20b54f5b27
					
				| 
						 | 
					@ -7,44 +7,45 @@ Copyright (C) 2011-2023 Thomas Hooge
 | 
				
			||||||
SPDX-License-Identifier: GPL-3.0-or-later
 | 
					SPDX-License-Identifier: GPL-3.0-or-later
 | 
				
			||||||
*****************************************************************************/
 | 
					*****************************************************************************/
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					function strip_mac($mac, $caps=true) {
 | 
				
			||||||
    // strip mac address to 12 char string
 | 
					    // strip mac address to 12 char string
 | 
				
			||||||
function strip_mac($mac) {
 | 
					 | 
				
			||||||
    // strip chars we don't need
 | 
					    // strip chars we don't need
 | 
				
			||||||
    $mac = preg_replace("|[^a-fA-F0-9]|", "", $mac);
 | 
					    $mac = preg_replace('/[^a-fA-F0-9]/', '', $mac);
 | 
				
			||||||
 | 
					    if ($caps) {
 | 
				
			||||||
    // capitalize (just because it looks better eh)
 | 
					 | 
				
			||||||
        $mac = strtoupper($mac);
 | 
					        $mac = strtoupper($mac);
 | 
				
			||||||
 | 
					    } else {
 | 
				
			||||||
    // and return
 | 
					        $mac = strtolower($mac);
 | 
				
			||||||
    return ($mac);
 | 
					    }
 | 
				
			||||||
 | 
					    return $mac;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// rebuild mac address
 | 
					function write_mac($mac, $user_mac='xx:xx:xx:xx:xx:xx') {
 | 
				
			||||||
function write_mac($mac) {
 | 
					    // rebuild mac address using user supplied format
 | 
				
			||||||
    // check string length
 | 
					
 | 
				
			||||||
    if (strlen($mac) != 12) {
 | 
					    if (strlen($mac) != 12) {
 | 
				
			||||||
        // if the MAC is empty, or for whatever reason incorrect, just return
 | 
					        // if the MAC is empty, or for whatever reason incorrect, just return
 | 
				
			||||||
        return $mac;
 | 
					        return $mac;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    // check format of user mac: count upper or lower char
 | 
				
			||||||
 | 
					    $chars = count_chars($user_mac, 1);
 | 
				
			||||||
 | 
					    if (array_key_exists(88, $chars) and $chars[88] == 12) {
 | 
				
			||||||
 | 
					        $pattern = '/X/';
 | 
				
			||||||
 | 
					        $mac = strtoupper($mac);
 | 
				
			||||||
 | 
					    } elseif (array_key_exists(120, $chars) and $chars[120] == 12) {
 | 
				
			||||||
 | 
					        $pattern = '/x/';
 | 
				
			||||||
 | 
					        $mac = strtolower($mac);
 | 
				
			||||||
    } else {
 | 
					    } else {
 | 
				
			||||||
        // count to 12...
 | 
					        // invalid format
 | 
				
			||||||
        for ($i=0; $i<12; $i++) {
 | 
					        return $mac;
 | 
				
			||||||
            // ... and strip mac to pieces
 | 
					 | 
				
			||||||
            ${"mac".$i} = $mac{$i};
 | 
					 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        // get user preference
 | 
					 | 
				
			||||||
        $user_mac = $_SESSION['suser_mac'];
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        // count to 12 again...
 | 
					 | 
				
			||||||
    for($i=0; $i<12; $i++) {
 | 
					    for($i=0; $i<12; $i++) {
 | 
				
			||||||
            // ... and replace user preference with pieces
 | 
					        $user_mac = preg_replace($pattern, $mac[$i], $user_mac, 1);
 | 
				
			||||||
            $user_mac = preg_replace("/x/", ${"mac".$i}, $user_mac, 1);
 | 
					 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        // and return
 | 
					 | 
				
			||||||
    return $user_mac;
 | 
					    return $user_mac;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
// redirect page
 | 
					// redirect page
 | 
				
			||||||
function header_location($location) {
 | 
					function header_location($location) {
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
							
								
								
									
										3
									
								
								node.php
								
								
								
								
							
							
						
						
									
										3
									
								
								node.php
								
								
								
								
							| 
						 | 
					@ -231,7 +231,7 @@ $sth = $dbh->prepare($sql);
 | 
				
			||||||
$sth->execute([$id]);
 | 
					$sth->execute([$id]);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
$node = $sth->fetch(PDO::FETCH_OBJ);
 | 
					$node = $sth->fetch(PDO::FETCH_OBJ);
 | 
				
			||||||
$node->mac = write_mac($node->mac);
 | 
					$node->mac = write_mac($node->mac, $_SESSION['suser_mac']);
 | 
				
			||||||
$smarty->assign("node", $node);
 | 
					$smarty->assign("node", $node);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// nat
 | 
					// nat
 | 
				
			||||||
| 
						 | 
					@ -281,6 +281,7 @@ $sql = "SELECT node_id AS id, node_ip AS ip, node_mac AS mac,
 | 
				
			||||||
$sth = $dbh->prepare($sql);
 | 
					$sth = $dbh->prepare($sql);
 | 
				
			||||||
$sth->execute([$id]);
 | 
					$sth->execute([$id]);
 | 
				
			||||||
$node = $sth->fetch(PDO::FETCH_OBJ);
 | 
					$node = $sth->fetch(PDO::FETCH_OBJ);
 | 
				
			||||||
 | 
					$node->mac = write_mac($node->mac, $_SESSION['suser_mac']);
 | 
				
			||||||
$node->flags = explode(',', $node->flags);
 | 
					$node->flags = explode(',', $node->flags);
 | 
				
			||||||
$smarty->assign("node", $node);
 | 
					$smarty->assign("node", $node);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in New Issue