<?php
	/*****************************************************************************
	IP Reg, a PHP/MySQL IPAM tool
	Copyright (C) 2007-2009 Wietse Warendorff
	
	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 3 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, see <http://www.gnu.org/licenses/>.
	
	For more information, visit http://sourceforge.net/projects/ipreg,
	or contact me at wietsew@users.sourceforge.net
	*****************************************************************************/
	
	// strip mac address to 12 char string
	function strip_mac($mac) {
		// strip chars we don't need
		$mac = preg_replace("|[^a-fA-F0-9]|", "", $mac);
		
		// capitalize (just because it looks better eh)
		$mac = strtoupper($mac);
		
		// and return
		return ($mac);
	}
	
	// rebuild mac address
	function write_mac($mac) {
		// check string length
		if (strlen($mac)!=12) {
			// if the MAC is empty, or for whatever reason incorrect, just return
			return $mac;
		} else {
			// count to 12...
			for($i=0;$i<12;$i++) {
				// ... 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++) {
				// ... and replace user preference with pieces
				$user_mac = preg_replace("/x/", ${"mac".$i}, $user_mac, 1);
			}
			
			// and return
			return $user_mac;
		}
	}
	
	// redirect page
	function header_location($location) {
		// send header
		header("location: " . $location);
		
		// exit to be sure
		exit;
	}
	
	// sanitize input
	function sanitize($input) {
		// trim whitespaces
		$input = @trim($input);
		
		// magic quotes enabled?
		if(get_magic_quotes_gpc()) {
			// strip slashes
			$input = stripslashes($input);
		}
		
		// convert to utf-8
		iconv("UTF-8", "UTF-8", $input);
		
		// convert special chars
		$input = htmlentities($input,ENT_QUOTES,'UTF-8');
		
		// make sql ready
		$input = mysql_real_escape_string($input);
		
		// and return
		return $input;
	}
?>