<?php
/*
 * WiND - Wireless Nodes Database
 *
 * Copyright (C) 2005 Nikolaos Nikalexis <winner@cube.gr>
 * 
 * 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; version 2 dated June, 1991.
 *
 * 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, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 */

class hostmaster_ranges {

	var $tpl;
	
	function hostmaster_ranges() {
		
	}
	
	function form_search_ranges() {
		global $construct, $db;
		$form_search_ranges = new form(array('FORM_NAME' => 'form_search_ranges'));
		$form_search_ranges->data = array("0" => array("Field" => "ip", "fullField" => "ip"));
		$form_search_ranges->db_data('ip_ranges.status, ip_ranges.delete_req');
		$form_search_ranges->db_data_search();
		return $form_search_ranges;
	}

	function table_ip_ranges() {
		global $construct, $db;
		$form_search_ranges = $this->form_search_ranges();
		$where = $form_search_ranges->db_data_where(array('ip' => 'exclude'));
		$table_ip_ranges = new table(array('TABLE_NAME' => 'table_ip_ranges', 'FORM_NAME' => 'table_ip_ranges'));
		$s_ip = correct_ip($form_search_ranges->data[0]['value']);
		$where = ($where !=''?"(".$where.") AND ":"").
				($s_ip !=''?"ip_ranges.ip_start <= ".ip2long($s_ip)." AND ip_ranges.ip_end >= ".ip2long($s_ip)." AND ":"");
		if ($where!='') $where = substr($where, 0, -5);
		$table_ip_ranges->db_data(
			'ip_ranges.id, "" AS ip_range, ip_ranges.ip_start, ip_ranges.ip_end, ip_ranges.date_in, ip_ranges.status, ip_ranges.delete_req',
			'ip_ranges',
			$where);
		foreach( (array) $table_ip_ranges->data as $key => $value) {
			if ($key != 0) {
				$table_ip_ranges->data[$key]['ip_start'] = long2ip($table_ip_ranges->data[$key]['ip_start']);
				$table_ip_ranges->data[$key]['ip_end'] = long2ip($table_ip_ranges->data[$key]['ip_end']);
				$table_ip_ranges->data[$key]['ip_range'] = $table_ip_ranges->data[$key]['ip_start']." - ".$table_ip_ranges->data[$key]['ip_end'];
			}
		}
		$table_ip_ranges->db_data_multichoice('ip_ranges', 'id');
		for($i=1;$i<count($table_ip_ranges->data);$i++) {
			if (isset($table_ip_ranges->data[$i])) {
				$table_ip_ranges->info['EDIT'][$i] = makelink(array("page" => "hostmaster", "subpage" => "range", "iprange" => $table_ip_ranges->data[$i]['id']));
			}
		}
		$table_ip_ranges->info['EDIT_COLUMN'] = 'ip_range';
		$table_ip_ranges->info['MULTICHOICE_LABEL'] = 'delete';
		$table_ip_ranges->db_data_remove('id', 'ip_start', 'ip_end');
		$table_ip_ranges->db_data_translate('ip_ranges__status', 'ip_ranges__delete_req');
		return $table_ip_ranges;
	}

	function output() {
		if ($_SERVER['REQUEST_METHOD'] == 'POST' && method_exists($this, 'output_onpost_'.$_POST['form_name'])) return call_user_func(array($this, 'output_onpost_'.$_POST['form_name']));
		global $construct;
		$this->tpl['form_search_ranges'] = $construct->form($this->form_search_ranges(), __FILE__);
		$this->tpl['table_ranges'] = $construct->table($this->table_ip_ranges(), __FILE__);
		return template($this->tpl, __FILE__);
	}

	function output_onpost_table_ip_ranges() {
		global $db, $main;
		$ret = TRUE;
		foreach( (array) $_POST['id'] as $key => $value) {
			$ret = $ret && $db->del("ip_ranges", "id = '".$value."'");
		}
		if ($ret) {
			$main->message->set_fromlang('info', 'delete_success', makelink("",TRUE));
		} else {
			$main->message->set_fromlang('error', 'generic');		
		}
	}

}

?>
