Project

General

Profile

Download (13 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2
/* $Id$ */
3
/*
4
	firewall_shaper.php
5
	Copyright (C) 2004, 2005 Scott Ullrich
6
	Copyright (C) 2008 Ermal Lu?i
7
	All rights reserved.
8

    
9
	Originally part of m0n0wall (http://m0n0.ch/wall)
10
	Copyright (C) 2003-2004 Manuel Kasper <mk@neon1.net>.
11
	All rights reserved.
12

    
13
	Redistribution and use in source and binary forms, with or without
14
	modification, are permitted provided that the following conditions are met:
15

    
16
	1. Redistributions of source code must retain the above copyright notice,
17
	   this list of conditions and the following disclaimer.
18

    
19
	2. Redistributions in binary form must reproduce the above copyright
20
	   notice, this list of conditions and the following disclaimer in the
21
	   documentation and/or other materials provided with the distribution.
22

    
23
	THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
24
	INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
25
	AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26
	AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
27
	OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28
	SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29
	INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30
	CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31
	ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32
	POSSIBILITY OF SUCH DAMAGE.
33
*/
34

    
35
require("guiconfig.inc");
36

    
37
$pgtitle = array("Firewall","Traffic Shape");
38

    
39
if (!is_array($config['shaper']['queue'])) {
40
	$config['shaper']['queue'] = array();
41
}
42

    
43
read_altq_config();
44
$tree = "<ul class=\"tree\" >";
45
if (is_array($altq_list_queues)) {
46
	foreach ($altq_list_queues as $altq) {
47
        	$tree .= $altq->build_tree();
48
        }
49
$tree .=  get_interface_list_to_show();
50
}
51
$tree .= "</ul>";
52

    
53
/* 
54
 * The whole logic in these code maybe can be specified.
55
 * If you find a better way contact me :).
56
 */
57

    
58
if ($_GET) {
59
	if ($_GET['queue'])
60
        	$qname = trim($_GET['queue']);
61
        if ($_GET['interface'])
62
                $interface = trim($_GET['interface']);
63
        if ($_GET['action'])
64
                $action = $_GET['action'];
65
}
66
if ($_POST) {
67
	if ($_POST['name'])
68
        	$qname = trim($_POST['name']);
69
        if ($_POST['interface'])
70
                $interface = trim($_POST['interface']);
71
	if ($_POST['parentqueue'])
72
		$parentqueue = trim($_POST['parentqueue']);
73
}
74

    
75
if ($interface) {
76
	$altq = $altq_list_queues[$interface];
77
	if ($altq) {
78
		$queue =& $altq->find_queue($interface, $qname);
79
		if ($queue) {
80
			if ($queue->GetEnabled())
81
				$can_enable = true;
82
			else
83
				$can_enable = false;
84
			if ($queue->CanHaveChilds() && $can_enable) {
85
				if ($queue->GetDefault() <> "")
86
					$can_add = false;
87
				else
88
					$can_add = true;
89
			} else
90
				$can_add = false;
91
		}
92
	} else $addnewaltq = true;
93
}
94

    
95
$dontshow = false;
96
$newqueue = false;
97
$output_form = "";
98

    
99
if ($_GET) {
100
	switch ($action) {
101
	case "delete":
102
			if ($queue) {
103
				$queue->delete_queue();
104
				write_config();
105
				touch($d_shaperconfdirty_path);
106
			}
107
			header("Location: firewall_shaper.php");
108
			exit;
109
		break;
110
	case "resetall":
111
			foreach ($altq_list_queues as $altq)
112
				$altq->delete_all();
113
			unset($altq_list_queues);
114
			$altq_list_queues = array();
115
			$tree = "<ul class=\"tree\" >";
116
			$tree .= get_interface_list_to_show();
117
			$tree .= "</ul>";
118
			unset($config['shaper']['queue']);
119
			unset($queue);
120
			unset($altq);
121
			$can_add = false;
122
			$can_enable = false;
123
			$dontshow = true;
124
			foreach ($config['filter']['rule'] as $key => $rule) {
125
				if (isset($rule['wizard']) && $rule['wizard'] == "yes")
126
					unset($config['filter']['rule'][$key]);
127
			}
128
			write_config();
129
			
130
			$retval = 0;
131
                        $savemsg = get_std_save_message($retval);
132

    
133
                        config_lock();
134
                        $retval = filter_configure();
135
                        config_unlock();
136

    
137
                        if (stristr($retval, "error") <> true)
138
	                        $savemsg = get_std_save_message($retval);
139
                        else
140
       	                	$savemsg = $retval;
141
			
142
			$output_form = $default_shaper_message;
143

    
144
		break;
145
	case "add":
146
			/* XXX: Find better way because we shouldn't know about this */
147
		if ($altq) {
148
	                switch ($altq->GetScheduler()) {
149
         	        case "PRIQ":
150
                	        $q = new priq_queue();
151
                        	break;
152
                        case "HFSC":
153
                         	$q = new hfsc_queue();
154
                        	break;
155
                        case "CBQ":
156
                                $q = new cbq_queue();
157
                        	break;
158
                        default:
159
                                /* XXX: Happens when sched==NONE?! */
160
				$q = new altq_root_queue();
161
                        	break;
162
        		}
163
		} else if ($addnewaltq) {
164
			$q = new altq_root_queue();
165
		} else 
166
			$input_errors[] = "Could not create new queue/discipline!";
167

    
168
			if ($q) {
169
				$q->SetInterface($interface);
170
				$output_form .= $q->build_form();
171
				$output_form .= "<input type=\"hidden\" name=\"parentqueue\" id=\"parentqueue\"";
172
				$output_form .= " value=\"".$qname."\">";
173
                unset($q);
174
				$newqueue = true;
175
			}
176
		break;
177
		case "show":
178
			if ($queue)  
179
                        $output_form .= $queue->build_form();
180
			else
181
					$input_errors[] = "Queue not found!";
182
		break;
183
		case "enable":
184
			if ($queue) {
185
					$queue->SetEnabled("on");
186
					$output_form .= $queue->build_form();
187
					write_config();
188
					touch($d_shaperconfdirty_path);
189
			} else
190
					$input_errors[] = "Queue not found!";
191
		break;
192
		case "disable":
193
			if ($queue) {
194
					$queue->SetEnabled("");
195
					$output_form .= $queue->build_form();
196
					write_config();
197
					touch($d_shaperconfdirty_path);
198
			} else
199
					$input_errors[] = "Queue not found!";
200
		break;
201
		default:
202
			$output_form .= "<p class=\"pgtitle\">" . $default_shaper_msg."</p>";
203
			$dontshow = true;
204
			break;
205
	}
206
} else if ($_POST) {
207
	unset($input_errors);
208

    
209
	if ($addnewaltq) {
210
		$altq =& new altq_root_queue();
211
		$altq->SetInterface($interface);
212
		
213
		switch ($altq->GetBwscale()) {
214
				case "Mb":
215
					$factor = 1000 * 1000;
216
					brak;
217
				case "Kb":
218
					$factor = 1000;
219
					break;
220
				case "b":
221
					$factor = 1;
222
					break;
223
				case "Gb":
224
					$factor = 1000 * 1000 * 1000;
225
					break;
226
				case "%": /* We don't use it for root_XXX queues. */
227
				default: /* XXX assume Kb by default. */
228
					$factor = 1000;
229
					break;
230
			} 
231
		$altq->SetAvailableBandwidth($altq->GetBandwidth() * $factor);
232
		$altq->ReadConfig($_POST);
233
		$altq->validate_input($_POST, &$input_errors);
234
		if (!$input_errors) {
235
			unset($tmppath);
236
			$tmppath[] = $altq->GetInterface();
237
			$altq->SetLink(&$tmppath);	
238
			$altq->wconfig();
239
			write_config();
240
			touch($d_shaperconfdirty_path);
241
			$can_enable = true;
242
			if ($queue->GetDefault() <> "")
243
                             $can_add = false;
244
                        else
245
                             $can_add = true;
246
		}
247
		$output_form .= $altq->build_form();
248

    
249
	} else if ($parentqueue) { /* Add a new queue */
250
		$qtmp =& $altq->find_queue($interface, $parentqueue);
251
		if ($qtmp) {
252
			$tmppath =& $qtmp->GetLink();
253
			array_push($tmppath, $qname);
254
			$tmp =& $qtmp->add_queue($interface, $_POST, $tmppath, &$input_errors);
255
			if (!$input_errors) {
256
				array_pop($tmppath);
257
				$tmp->wconfig();
258
				$can_enable = true;
259
				if ($tmp->CanHaveChilds() && $can_enable) {
260
					if ($queue->GetDefault() <> "")
261
                             			$can_add = false;
262
                        		else
263
                             			$can_add = true;
264
				} else
265
					$can_add = false;
266
				write_config();
267
				touch($d_shaperconfdirty_path);
268
				$can_enable = true;
269
				if ($altq->GetScheduler() != "PRIQ") /* XXX */
270
					if ($queue->GetDefault() <> "")
271
                                                $can_add = false;
272
                                        else
273
                                                $can_add = true;
274
			}
275
			$output_form .= $tmp->build_form();			
276
		} else
277
			$input_errors[] = "Could not add new queue.";
278
	} else if ($_POST['apply']) {
279
			write_config();
280

    
281
			$retval = 0;
282
			$savemsg = get_std_save_message($retval);
283
			
284
			config_lock();
285
			$retval = filter_configure();
286
			config_unlock();
287
			
288
			if (stristr($retval, "error") <> true)
289
					$savemsg = get_std_save_message($retval);
290
			else
291
					$savemsg = $retval;
292

    
293
			enable_rrd_graphing();
294

    
295
            unlink($d_shaperconfdirty_path);
296
			
297
			if ($queue) {
298
				$output_form .= $queue->build_form();
299
				$dontshow = false;
300
			}
301
			else {
302
				$output_form .= $default_shaper_message;
303
				$dontshow = true;
304
			}
305

    
306
	} else if ($queue) {
307
                $queue->validate_input($_POST, &$input_errors);
308
                if (!$input_errors) {
309
                            $queue->update_altq_queue_data($_POST);
310
                            $queue->wconfig();
311
							write_config();
312
				touch($d_shaperconfdirty_path);
313
				$dontshow = false;
314
                } 
315
				$output_form .= $queue->build_form();
316
	} else  {
317
		$output_form .= "<p class=\"pgtitle\">" . $default_shaper_msg."</p>";
318
		$dontshow = true;
319
	}
320
} else {
321
	$output_form .= "<p class=\"pgtitle\">" . $default_shaper_msg."</p>";
322
	$dontshow = true;
323
}
324
	
325

    
326
	
327

    
328
if (!$dontshow || $newqueue) {
329

    
330
$output_form .= "<tr><td width=\"22%\" valign=\"top\" class=\"vncellreq\">";
331
$output_form .= "Queue Actions";
332
$output_form .= "</td><td valign=\"top\" class=\"vncellreq\" width=\"78%\">";
333

    
334
$output_form .= "<input type=\"submit\" name=\"Submit\" value=\"" . gettext("Save") . "\" class=\"formbtn\" />";
335
if ($can_add || $addnewaltq) {
336
	$output_form .= "<a href=\"firewall_shaper.php?interface=";
337
	$output_form .= $interface; 
338
	if ($queue) {
339
		$output_form .= "&queue=" . $queue->GetQname();
340
	}
341
	$output_form .= "&action=add\">";
342
	$output_form .= "<input type=\"button\" class=\"formbtn\" name=\"add\" value=\"Add Queue\">";
343
	$output_form .= "</a>";
344
	$output_form .= "<a href=\"firewall_shaper.php?interface=";
345
	$output_form .= $interface . "&queue=";
346
	if ($queue) {
347
		$output_form .= "&queue=" . $queue->GetQname();
348
	}
349
	$output_form .= "&action=delete\">";
350
	$output_form .= "<input type=\"button\" class=\"formbtn\" name=\"delete\"";
351
	if ($queue)
352
		$output_form .= " value=\"Delete a queue\">";
353
	else
354
		$output_form .= " value=\"Disable shaper on interface\">";
355
	$output_form .= "</a>";  
356
}
357
$output_form .= "</td></tr>";
358
$output_form .= "</div>";
359
} 
360
else 
361
	$output_form .= "</div>";
362

    
363
$output = "<div id=\"shaperarea\" style=\"position:relative\">";
364
if (!$dontshow) {
365
if ($queue || $altq) {
366
	$output .= "<tr><td valign=\"top\" class=\"vncellreq\"><br>";
367
	$output .= "Enable/Disable";
368
	$output .= "</td><td class=\"vncellreq\">";
369
	$output .= " <input type=\"checkbox\" id=\"enabled\" name=\"enabled\"";
370
	if ($queue)
371
		if ($queue->GetEnabled())
372
       			$output .=  " CHECKED";
373
	else if ($altq)
374
		if ($altq->GetEnabled())
375
			$output .= " CHECKED";
376
	$output .= " ><span class=\"vexpl\"> Enable/Disable queue and its childs</span>";
377
	$output .= "</td></tr>";
378
}
379
}
380
$output .= $output_form;
381

    
382
$pgtitle = "Firewall: Shaper: By Interface View";
383

    
384
include("head.inc");
385
?>
386

    
387
<body link="#0000CC" vlink="#0000CC" alink="#0000CC" >
388
<link rel="stylesheet" type="text/css" media="all" href="./tree/tree.css" />
389
<script type="text/javascript" src="./tree/tree.js"></script>
390
<?php
391
if ($queue) {
392
        echo "<script type=\"text/javascript\">";
393
        echo $queue->build_javascript();
394
        echo "</script>";
395
}
396

    
397
include("fbegin.inc"); 
398
?>
399
<div id="inputerrors"></div>
400
<?php if ($input_errors) print_input_errors($input_errors); ?>
401

    
402
<form action="firewall_shaper.php" method="post" id="iform" name="iform">
403

    
404
<?php if ($savemsg) print_info_box($savemsg); ?>
405
<?php if (file_exists($d_shaperconfdirty_path)): ?><p>
406
<?php print_info_box_np("The traffic shaper configuration has been changed.<br>You must apply the changes in order for them to take effect.");?><br>
407
<?php endif; ?>
408
<table width="100%" border="0" cellpadding="0" cellspacing="0">
409
  <tr><td>
410
<?php
411
	$tab_array = array();
412
	$tab_array[0] = array("By Interface", true, "firewall_shaper.php");
413
	$tab_array[1] = array("By Queue", false, "firewall_shaper_queues.php");
414
	$tab_array[2] = array("EZ Shaper wizard", false, "wizard.php?xml=traffic_shaper_wizard.xml");
415
	display_top_tabs($tab_array);
416
?>
417
  </td></tr>
418
  <tr>
419
    <td>
420
	<div id="mainarea">
421
              <table class="tabcont" width="100%" border="0" cellpadding="0" cellspacing="0">
422
<?php if (count($altq_list_queues) > 0): ?>
423
                        <tr class="tabcont"><td width="25%" align="left">
424
                                <a href="firewall_shaper.php?action=resetall" >
425
                                        <input type="button" value="Remove Shaper" class="formbtn">
426
                                </a>
427
                        </td><td width="75%"> </td></tr>
428
<? endif; ?>
429
			<tr>
430
			<td width="25%" valign="top" algin="left">
431
			<?php
432
				echo $tree; 
433
			?>
434
			</td>
435
			<td width="75%" valign="top" align="center">
436
			<table>
437
			<?
438
				echo $output;
439
			?>	
440
			</table>
441

    
442
		      </td></tr>
443
                    </table>
444
		</div>
445
	  </td>
446
	</tr>
447
</table>
448
            </form>
449
<?php include("fend.inc"); 
450
?>
451
</body>
452
</html>
(55-55/189)