Project

General

Profile

Download (12.4 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
if (!is_array($config['shaper']['queue'])) {
38
	$config['shaper']['queue'] = array();
39
}
40

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

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

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

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

    
90
$dontshow = false;
91
$newqueue = false;
92
$output_form = "";
93

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

    
128
                        config_lock();
129
                        $retval = filter_configure();
130
                        config_unlock();
131

    
132
                        if (stristr($retval, "error") <> true)
133
	                        $savemsg = get_std_save_message($retval);
134
                        else
135
       	                	$savemsg = $retval;
136
			
137
			$output_form = $default_shaper_message;
138

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

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

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

    
241
	} else if ($parentqueue) { /* Add a new queue */
242
		$qtmp =& $altq->find_queue($interface, $parentqueue);
243
		if ($qtmp) {
244
			$tmppath =& $qtmp->GetLink();
245
			array_push($tmppath, $qname);
246
			$tmp =& $qtmp->add_queue($interface, $_POST, &$tmppath, &$input_errors);
247
			if (!$input_errors) {
248
				array_pop($tmppath);
249
				$tmp->wconfig();
250
				$can_enable = true;
251
				if ($tmp->CanHaveChilds() && $can_enable)
252
					$can_add = true;
253
				else
254
					$can_add = false;
255
				write_config();
256
				touch($d_shaperconfdirty_path);
257
				$can_enable = true;
258
				if ($altq->GetScheduler() != "PRIQ") /* XXX */
259
					$can_add = true;
260
			}
261
			$output_form .= $tmp->build_form();			
262
		} else
263
			$input_errors[] = "Could not add new queue.";
264
	} else if ($_POST['apply']) {
265
			write_config();
266

    
267
			$retval = 0;
268
			$savemsg = get_std_save_message($retval);
269
			
270
			config_lock();
271
			$retval = filter_configure();
272
			config_unlock();
273
			
274
			if (stristr($retval, "error") <> true)
275
					$savemsg = get_std_save_message($retval);
276
			else
277
					$savemsg = $retval;
278

    
279
			enable_rrd_graphing();
280

    
281
            unlink($d_shaperconfdirty_path);
282
			
283
			if ($queue) {
284
				$output_form .= $queue->build_form();
285
				$dontshow = false;
286
			}
287
			else {
288
				$output_form .= $default_shaper_message;
289
				$dontshow = true;
290
			}
291

    
292
	} else if ($queue) {
293
                $queue->validate_input($_POST, &$input_errors);
294
                if (!$input_errors) {
295
                            $queue->update_altq_queue_data($_POST);
296
                            $queue->wconfig();
297
							write_config();
298
				touch($d_shaperconfdirty_path);
299
				$dontshow = false;
300
                } 
301
				$output_form .= $queue->build_form();
302
	} else  {
303
		$output_form .= "<p class=\"pgtitle\">" . $default_shaper_msg."</p>";
304
		$dontshow = true;
305
	}
306
} else {
307
	$output_form .= "<p class=\"pgtitle\">" . $default_shaper_msg."</p>";
308
	$dontshow = true;
309
}
310
	
311

    
312
	
313

    
314
if (!$dontshow || $newqueue) {
315

    
316
$output_form .= "<tr><td width=\"22%\" valign=\"top\" class=\"vncellreq\">";
317
$output_form .= "Queue Actions";
318
$output_form .= "</td><td valign=\"top\" class=\"vncellreq\" width=\"78%\">";
319

    
320
$output_form .= "<input type=\"submit\" name=\"Submit\" value=\"" . gettext("Save") . "\" class=\"formbtn\" />";
321
if ($can_add || $addnewaltq) {
322
	$output_form .= "<a href=\"firewall_shaper.php?interface=";
323
	$output_form .= $interface; 
324
	if ($queue) {
325
		$output_form .= "&queue=" . $queue->GetQname();
326
	}
327
	$output_form .= "&action=add\">";
328
	$output_form .= "<input type=\"button\" class=\"formbtn\" name=\"add\" value=\"Add Queue\">";
329
	$output_form .= "</a>";
330
	$output_form .= "<a href=\"firewall_shaper.php?interface=";
331
	$output_form .= $interface . "&queue=";
332
	if ($queue) {
333
		$output_form .= "&queue=" . $queue->GetQname();
334
	}
335
	$output_form .= "&action=delete\">";
336
	$output_form .= "<input type=\"button\" class=\"formbtn\" name=\"delete\"";
337
	if ($queue)
338
		$output_form .= " value=\"Delete a queue\">";
339
	else
340
		$output_form .= " value=\"Disable shaper on interface\">";
341
	$output_form .= "</a>";  
342
}
343
$output_form .= "</td></tr>";
344
$output_form .= "</div>";
345
} 
346
else 
347
	$output_form .= "</div>";
348

    
349
$output = "<div id=\"shaperarea\" style=\"position:relative\">";
350
if (!$dontshow) {
351
if ($queue || $altq) {
352
	$output .= "<tr><td valign=\"top\" class=\"vncellreq\"><br>";
353
	$output .= "Enable/Disable";
354
	$output .= "</td><td class=\"vncellreq\">";
355
	$output .= " <input type=\"checkbox\" id=\"enabled\" name=\"enabled\"";
356
	if ($queue)
357
		if ($queue->GetEnabled())
358
       			$output .=  " CHECKED";
359
	else if ($altq)
360
		if ($altq->GetEnabled())
361
			$output .= " CHECKED";
362
	$output .= " ><span class=\"vexpl\"> Enable/Disable queue and its childs</span>";
363
	$output .= "</td></tr>";
364
}
365
}
366
$output .= $output_form;
367

    
368
$pgtitle = "Firewall: Shaper: By Interface View";
369

    
370
include("head.inc");
371
?>
372

    
373
<body link="#0000CC" vlink="#0000CC" alink="#0000CC" >
374
<link rel="stylesheet" type="text/css" media="all" href="./tree/tree.css" />
375
<script type="text/javascript" src="./tree/tree.js"></script>
376
<?php
377
if ($queue) {
378
        echo "<script type=\"text/javascript\">";
379
        echo $queue->build_javascript();
380
        echo "</script>";
381
}
382

    
383
include("fbegin.inc"); 
384
?>
385
<div id="inputerrors"></div>
386
<?php if ($input_errors) print_input_errors($input_errors); ?>
387

    
388
<form action="firewall_shaper.php" method="post" id="iform" name="iform">
389

    
390
<?php if ($savemsg) print_info_box($savemsg); ?>
391
<?php if (file_exists($d_shaperconfdirty_path)): ?><p>
392
<?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>
393
<?php endif; ?>
394
<table width="100%" border="0" cellpadding="0" cellspacing="0">
395
  <tr><td>
396
<?php
397
	$tab_array = array();
398
	$tab_array[0] = array("By Interface", true, "firewall_shaper.php");
399
	$tab_array[1] = array("By Queue", false, "firewall_shaper_queues.php");
400
	$tab_array[2] = array("EZ Shaper wizard", false, "wizard.php?xml=traffic_shaper_wizard.xml");
401
	display_top_tabs($tab_array);
402
?>
403
  </td></tr>
404
  <tr>
405
    <td>
406
	<div id="mainarea">
407
              <table class="tabcont" width="100%" border="0" cellpadding="0" cellspacing="0">
408
<?php if (count($altq_list_queues) > 0): ?>
409
                        <tr class="tabcont"><td width="25%" align="left">
410
                                <a href="firewall_shaper.php?action=resetall" >
411
                                        <input type="button" value="Remove Shaper" class="formbtn">
412
                                </a>
413
                        </td><td width="75%"> </td></tr>
414
<? endif; ?>
415
			<tr>
416
			<td width="25%" valign="top" algin="left">
417
			<?php
418
				echo $tree; 
419
			?>
420
			</td>
421
			<td width="75%" valign="top" align="center">
422
			<table>
423
			<?
424
				echo $output;
425
			?>	
426
			</table>
427

    
428
		      </td></tr>
429
                    </table>
430
		</div>
431
	  </td>
432
	</tr>
433
</table>
434
            </form>
435
<?php include("fend.inc"); 
436
?>
437
</body>
438
</html>
(55-55/187)