<?php

/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */

// +-------------------------------------------------------------------+
// | WiFiDog Authentication Server                                     |
// | =============================                                     |
// |                                                                   |
// | The WiFiDog Authentication Server is part of the WiFiDog captive  |
// | portal suite.                                                     |
// +-------------------------------------------------------------------+
// | PHP version 5 required.                                           |
// +-------------------------------------------------------------------+
// | Homepage:     http://www.wifidog.org/                             |
// | Source Forge: http://sourceforge.net/projects/wifidog/            |
// +-------------------------------------------------------------------+
// | 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, contact:                         |
// |                                                                   |
// | Free Software Foundation           Voice:  +1-617-542-5942        |
// | 59 Temple Place - Suite 330        Fax:    +1-617-542-2652        |
// | Boston, MA  02111-1307,  USA       gnu@gnu.org                    |
// |                                                                   |
// +-------------------------------------------------------------------+
/**
 * @package    WiFiDogAuthServer
 * @author     Benoit Grégoire <bock@step.polymtl.ca>
 * @author     Max Horvath <max.horvath@maxspot.de>
 * @copyright  2005-2006 Benoit Grégoire, Technologies Coeus inc.
 * @copyright  2006 Max Horvath, maxspot GmbH
 * @version    Subversion $Id: Network.php 1157 2006-12-31 19:30:34Z benoitg $
 * @link       http://www.wifidog.org/
 */

require_once('classes/GenericObject.php');


/* Blacklist Status Codes */
define('BLACKLIST_ACTIVE',1);
define('BLACKLIST_DISABLED',0);

/**
 * Blacklist.
 *
 * A blacklist is a list of MAC addresses that are barred from network access
 *
 * @package    WiFiDogAuthServer
 * @author     Dylan Reeve <dylan@bunkermedia.co.nz>
 * @author     Max Horvath <max.horvath@maxspot.de>
 * @copyright  2005-2006 Benoit Grégoire, Technologies Coeus inc.
 * @copyright  2006 Max Horvath, maxspot GmbH
 * @copyright  2007 Dylan Reeve, Bunker Media Ltd.
 */

class Blacklist implements GenericObject
{

        /** Object cache for the object factory (getObject())*/
        private static $instanceArray = array();
        /**
     * The Blacklist Id
     *
     * @var string
     *
     * @access private
     */
        private $object;

        private $blacklist = array();

        /**
     * Get an instance of the object
     *
     *
     * @return mixed The Content object, or null if there was an error
     *               (an exception is also thrown)
     *
     * @see GenericObject
     * @static
     * @access public
     */
        public static function getObject()
        {
                if(!isset(self::$object))
                {
                        self::$object = new self();
                }
                return self::$object;
        }

        /**
     * Constructor
     *
     * @return void
     *
     * @access private
     */
        private function __construct()
        {

        }

        /**
     * Checks if a MAC address is blacklisted for a network.
     * 
     * @param string $network_id Network ID
     * @param string $mac Client MAC address
     * 
     * @return bool
     *
     * @access public
     */
        public function isBlacklisted($network_id, $mac) {
                $db = AbstractDb::getObject();
                
                $mac = $db->escapeString($mac);
                $network_id = $db->escapeString($network_id);
                
                $row = null;
                
                $sql = "SELECT * FROM blacklist_mac WHERE mac_addr = '$mac' AND network = 'network_id' AND status = ".BLACKLIST_ACTIVE;
                
                $row = $db->execSqlUniqueRes($sql, false);
                if ($row == null) {
                        return FALSE;
                } else {
                        return TRUE;
                }
        }
        
        
        /**
     * Add a blacklist entry
     *
     * @param string $network_id Network ID
     * @param string $mac MAC Address
     * @param string $user_id ID of user adding blacklist entry.
     * @param string $notes Optional Notes about blacklist addition (reason etc...)
     * @param string $message Optional Additional message to display to user.
     *
     * @return void
     */
        public function addBlacklistEntry($network_id, $mac, $user_id, $notes="", $message="") {
                $db = AbstractDb::getObject();
                
                $network_id = $db->escapeString($network_id);
                $mac = $db->escapeString($mac);
                $notes = $db->escapeString($notes);
                $user_id = $db->escapeString($user_id);
                
                $sql = "INSERT INTO blacklist_mac (mac_addr, add_time, status, network, notes, message, user_id)";
                $sql .= " VALUES ('$mac', CURRENT_TIMESTAMP, ".BLACKLIST_ACTIVE.", '$network_id', '$notes', '$message', '$user_id')";
                
                $db->execSqlUpdate($sql, false);
        }
        
        
        /**
     * Get a blacklist entry
     *
     * @param string $network_id Network ID
     * @param string $mac MAC Address
     * @param bool $activeOnly Optional Return only active blacklist entries - Default: TRUE
     *
     * @return mixed Row from SQL query.
     */
        public function getBlacklistEntry($network_id, $mac, $activeOnly=TRUE) {
                $db = AbstractDb::getObject();
                
                $mac = $db->escapeString($mac);
                $network_id = $db->escapeString($network_id);
                
                $sql = "SELECT * FROM blacklist_mac WHERE mac_addr = '$mac' AND network = 'network_id'";
                if ($activeOnly) {
                        $sql .= "AND status = ".BLACKLIST_ACTIVE;
                }
                                
                $row = $db->execSqlUniqueRes($sql, false);
                
                return $row;
        }
        
        
        /**
     * Remove a blacklist entry
     *
     * @param string $network_id Network ID
     * @param string $mac MAC Address
     *
     * @return bool
     */
        public function removeBlacklistEntry($network_id, $mac) {
                $db = AbstractDb::getObject();
                
                $entry = $this->getBlacklistEntry($network_id, $mac);
                if (!empty($entry['entry_id'])) {
                        $id = $entry['entry_id'];
                        $sql = "UPDATE blacklist_mac SET status = ".BLACKLIST_DISABLED.", remove_time = CURRENT_TIMESTAMP WHERE entry_id = $id";
                        $db->execSqlUpdate($sql, false);
                        
                        return TRUE;
                } else {
                        return FALSE;
                }
        }
        
        
        /**
     * Get blacklist entries
     *
     * @param string $network_id Optional Network ID
     * @param string $mac Optional MAC Address
     * @param string $status Optional Entry status
     *
     * @return bool
     */
        public function getBlacklist($network_id=null, $mac=null, $status=null) {
                $db = AbstractDb::getObject();
                
                $conditions = array();
                
                $sql = "SELECT * FROM blacklist_mac ";
                if ($network_id != null) {
                        $network_id = $db->escapeString($network_id);
                        $conditions[] = "network_id = '$network_id'";
                }
                
                if ($mac != null) {
                        $mac = $db->escapeString($mac);
                        $conditions[] = "mac_addr = '$mac'";
                }
                
                if ($status != null) {
                        $status = $db->escapeString($status);
                        $conditions[] = "status = $status";
                }
                
                if (!empty($conditions)) {
                        $sql .= "WHERE ".join(' AND ', $conditions);
                }
                
                $rows = array();
                $db->execSql($sql, $rows, false);
                
                return $rows;
        }
}

?>