Project

General

Profile

« Previous | Next » 

Revision 6aaec445

Added by Phil Davis about 10 years ago

Code style firewall shaper

View differences:

usr/local/www/firewall_shaper_vinterface.php
46 46
require_once("filter.inc");
47 47
require_once("shaper.inc");
48 48

  
49
if($_GET['reset'] <> "") {
49
if ($_GET['reset'] <> "") {
50 50
	mwexec("/usr/bin/killall -9 pfctl");
51 51
	exit;
52 52
}
......
55 55
$shortcut_section = "trafficshaper-limiters";
56 56

  
57 57
read_dummynet_config();
58
/* 
58
/*
59 59
 * The whole logic in these code maybe can be specified.
60 60
 * If you find a better way contact me :).
61 61
 */
62 62

  
63 63
if ($_GET) {
64
	if ($_GET['queue'])
65
        	$qname = htmlspecialchars(trim($_GET['queue']));
66
        if ($_GET['pipe'])
67
                $pipe = htmlspecialchars(trim($_GET['pipe']));
68
        if ($_GET['action'])
69
                $action = htmlspecialchars($_GET['action']);
64
	if ($_GET['queue']) {
65
		$qname = htmlspecialchars(trim($_GET['queue']));
66
	}
67
	if ($_GET['pipe']) {
68
		$pipe = htmlspecialchars(trim($_GET['pipe']));
69
	}
70
	if ($_GET['action']) {
71
		$action = htmlspecialchars($_GET['action']);
72
	}
70 73
}
71 74
if ($_POST) {
72
	if ($_POST['name'])
73
        	$qname = htmlspecialchars(trim($_POST['name']));
74
	else if ($_POST['newname'])
75
        	$qname = htmlspecialchars(trim($_POST['newname']));
76
        if ($_POST['pipe'])
77
        	$pipe = htmlspecialchars(trim($_POST['pipe']));
78
	else
75
	if ($_POST['name']) {
76
		$qname = htmlspecialchars(trim($_POST['name']));
77
	} else if ($_POST['newname']) {
78
		$qname = htmlspecialchars(trim($_POST['newname']));
79
	}
80
	if ($_POST['pipe']) {
81
		$pipe = htmlspecialchars(trim($_POST['pipe']));
82
	} else {
79 83
		$pipe = htmlspecialchars(trim($qname));
80
	if ($_POST['parentqueue'])
84
	}
85
	if ($_POST['parentqueue']) {
81 86
		$parentqueue = htmlspecialchars(trim($_POST['parentqueue']));
87
	}
82 88
}
83 89

  
84 90
if ($pipe) {
85 91
	$dnpipe = $dummynet_pipe_list[$pipe];
86 92
	if ($dnpipe) {
87 93
		$queue =& $dnpipe->find_queue($pipe, $qname);
88
	} else $addnewpipe = true;
94
	} else {
95
		$addnewpipe = true;
96
	}
89 97
}
90 98

  
91 99
$dontshow = false;
......
94 102

  
95 103
if ($_GET) {
96 104
	switch ($action) {
97
	case "delete":
98
		if ($queue) {
99
			if (is_array($config['filter']['rule'])) {
100
				foreach ($config['filter']['rule'] as $rule) {
101
					if ($rule['dnpipe'] == $queue->GetQname() || $rule['pdnpipe'] == $queue->GetQname())
102
						$input_errors[] = gettext("This pipe/queue is referenced in filter rules, please remove references from there before deleting.");
105
		case "delete":
106
			if ($queue) {
107
				if (is_array($config['filter']['rule'])) {
108
					foreach ($config['filter']['rule'] as $rule) {
109
						if ($rule['dnpipe'] == $queue->GetQname() || $rule['pdnpipe'] == $queue->GetQname()) {
110
							$input_errors[] = gettext("This pipe/queue is referenced in filter rules, please remove references from there before deleting.");
111
						}
112
					}
103 113
				}
114
				if (!$input_errors) {
115
					$queue->delete_queue();
116
					if (write_config()) {
117
						mark_subsystem_dirty('shaper');
118
					}
119
					header("Location: firewall_shaper_vinterface.php");
120
					exit;
121
				}
122
				$output_form .= $queue->build_form();
123
			} else {
124
				$input_errors[] = sprintf(gettext("No queue with name %s was found!"),$qname);
125
				$output_form .= $dn_default_shaper_msg;
126
				$dontshow = true;
104 127
			}
105
			if (!$input_errors) {
106
				$queue->delete_queue();
107
				if (write_config())
128
			break;
129
		case "resetall":
130
			foreach ($dummynet_pipe_list as $dn) {
131
				$dn->delete_queue();
132
			}
133
			unset($dummynet_pipe_list);
134
			$dummynet_pipe_list = array();
135
			unset($config['dnshaper']['queue']);
136
			unset($queue);
137
			unset($pipe);
138
			$can_add = false;
139
			$can_enable = false;
140
			$dontshow = true;
141
			foreach ($config['filter']['rule'] as $key => $rule) {
142
				if (isset($rule['dnpipe'])) {
143
					unset($config['filter']['rule'][$key]['dnpipe']);
144
				}
145
				if (isset($rule['pdnpipe'])) {
146
					unset($config['filter']['rule'][$key]['pdnpipe']);
147
				}
148
			}
149
			if (write_config()) {
150
				$retval = 0;
151
				$retval = filter_configure();
152
				$savemsg = get_std_save_message($retval);
153

  
154
				if (stristr($retval, "error") <> true) {
155
					$savemsg = get_std_save_message($retval);
156
				} else {
157
					$savemsg = $retval;
158
				}
159
			} else {
160
				$savemsg = gettext("Unable to write config.xml (Access Denied?)");
161
			}
162
			$output_form = $dn_default_shaper_message;
163

  
164
			break;
165
		case "add":
166
			if ($dnpipe) {
167
				$q = new dnqueue_class();
168
				$q->SetPipe($pipe);
169
				$output_form .= "<input type=\"hidden\" name=\"parentqueue\" id=\"parentqueue\"";
170
				$output_form .= " value=\"".$pipe."\" />";
171
			} else if ($addnewpipe) {
172
				$q = new dnpipe_class();
173
				$q->SetQname($pipe);
174
			} else {
175
				$input_errors[] = gettext("Could not create new queue/discipline!");
176
			}
177

  
178
			if ($q) {
179
				$output_form .= $q->build_form();
180
				$newjavascript = $q->build_javascript();
181
				unset($q);
182
				$newqueue = true;
183
			}
184
			break;
185
		case "show":
186
			if ($queue) {
187
			   $output_form .= $queue->build_form();
188
			} else {
189
				$input_errors[] = gettext("Queue not found!");
190
			}
191
			break;
192
		case "enable":
193
			if ($queue) {
194
				$queue->SetEnabled("on");
195
				$output_form .= $queue->build_form();
196
				$queue->wconfig();
197
				if (write_config()) {
108 198
					mark_subsystem_dirty('shaper');
109
				header("Location: firewall_shaper_vinterface.php");
110
				exit;
199
				}
200
			} else {
201
				$input_errors[] = gettext("Queue not found!");
111 202
			}
112
			$output_form .= $queue->build_form();
113
		} else {
114
			$input_errors[] = sprintf(gettext("No queue with name %s was found!"),$qname);
203
			break;
204
		case "disable":
205
			if ($queue) {
206
				$queue->SetEnabled("");
207
				$output_form .= $queue->build_form();
208
				$queue->wconfig();
209
				if (write_config()) {
210
					mark_subsystem_dirty('shaper');
211
				}
212
			} else {
213
				$input_errors[] = gettext("Queue not found!");
214
			}
215
			break;
216
		default:
115 217
			$output_form .= $dn_default_shaper_msg;
116 218
			$dontshow = true;
117
		}
118
		break;
119
	case "resetall":
120
		foreach ($dummynet_pipe_list as $dn)
121
			$dn->delete_queue();
122
		unset($dummynet_pipe_list);
123
		$dummynet_pipe_list = array();
124
		unset($config['dnshaper']['queue']);
125
		unset($queue);
126
		unset($pipe);
127
		$can_add = false;
128
		$can_enable = false;
129
		$dontshow = true;
130
		foreach ($config['filter']['rule'] as $key => $rule) {
131
			if (isset($rule['dnpipe']))
132
				unset($config['filter']['rule'][$key]['dnpipe']);
133
			if (isset($rule['pdnpipe']))
134
				unset($config['filter']['rule'][$key]['pdnpipe']);
135
		}
136
		if (write_config()) {
137
			$retval = 0;
138
			$retval = filter_configure();
139
			$savemsg = get_std_save_message($retval);
140

  
141
			if (stristr($retval, "error") <> true)
142
				$savemsg = get_std_save_message($retval);
143
			else
144
				$savemsg = $retval;
145
		} else
146
			$savemsg = gettext("Unable to write config.xml (Access Denied?)");
147
		$output_form = $dn_default_shaper_message;
148

  
149
		break;
150
	case "add":
151
		if ($dnpipe) {
152
			$q = new dnqueue_class();
153
			$q->SetPipe($pipe);
154
			$output_form .= "<input type=\"hidden\" name=\"parentqueue\" id=\"parentqueue\"";
155
			$output_form .= " value=\"".$pipe."\" />";
156
		} else if ($addnewpipe) {
157
			$q = new dnpipe_class();
158
			$q->SetQname($pipe);
159
		} else 
160
			$input_errors[] = gettext("Could not create new queue/discipline!");
161

  
162
		if ($q) {
163
			$output_form .= $q->build_form();
164
			$newjavascript = $q->build_javascript();
165
			unset($q);
166
			$newqueue = true;
167
		}
168
		break;
169
	case "show":
170
		if ($queue)  
171
                       $output_form .= $queue->build_form();
172
		else
173
			$input_errors[] = gettext("Queue not found!");
174
		break;
175
	case "enable":
176
		if ($queue) {
177
			$queue->SetEnabled("on");
178
			$output_form .= $queue->build_form();
179
			$queue->wconfig();
180
			if (write_config())
181
				mark_subsystem_dirty('shaper');
182
		} else
183
			$input_errors[] = gettext("Queue not found!");
184
		break;
185
	case "disable":
186
		if ($queue) {
187
			$queue->SetEnabled("");
188
			$output_form .= $queue->build_form();
189
			$queue->wconfig();
190
			if (write_config())
191
				mark_subsystem_dirty('shaper');
192
		} else
193
			$input_errors[] = gettext("Queue not found!");
194
		break;
195
	default:
196
		$output_form .= $dn_default_shaper_msg;
197
		$dontshow = true;
198
		break;
219
			break;
199 220
	}
200 221
} else if ($_POST) {
201 222
	unset($input_errors);
202 223

  
203 224
	if ($addnewpipe) {
204
		if (!empty($dummynet_pipe_list[$qname]))
225
		if (!empty($dummynet_pipe_list[$qname])) {
205 226
			$input_errors[] = gettext("You cannot name a child queue with the same name as a parent limiter");
206
		else {
227
		} else {
207 228
			$dnpipe =& new dnpipe_class();
208
			
229

  
209 230
			$dnpipe->ReadConfig($_POST);
210 231
			$dnpipe->validate_input($_POST, $input_errors);
211 232
			if (!$input_errors) {
......
213 234
				$dnpipe->SetNumber($number);
214 235
				unset($tmppath);
215 236
				$tmppath[] = $dnpipe->GetQname();
216
				$dnpipe->SetLink($tmppath);	
237
				$dnpipe->SetLink($tmppath);
217 238
				$dnpipe->wconfig();
218
				if (write_config())
239
				if (write_config()) {
219 240
					mark_subsystem_dirty('shaper');
241
				}
220 242
				$can_enable = true;
221 243
				$can_add = true;
222 244
			}
......
226 248
			$newjavascript = $dnpipe->build_javascript();
227 249
		}
228 250
	} else if ($parentqueue) { /* Add a new queue */
229
		if (!empty($dummynet_pipe_list[$qname]))
251
		if (!empty($dummynet_pipe_list[$qname])) {
230 252
			$input_errors[] = gettext("You cannot name a child queue with the same name as a parent limiter");
231
		else if ($dnpipe) {
253
		} else if ($dnpipe) {
232 254
			$tmppath =& $dnpipe->GetLink();
233 255
			array_push($tmppath, $qname);
234 256
			$tmp =& $dnpipe->add_queue($pipe, $_POST, $tmppath, $input_errors);
......
243 265
			}
244 266
			read_dummynet_config();
245 267
			$output_form .= $tmp->build_form();
246
		} else
268
		} else {
247 269
			$input_errors[] = gettext("Could not add new queue.");
270
		}
248 271
	} else if ($_POST['apply']) {
249
			write_config();
272
		write_config();
273

  
274
		$retval = 0;
275
		$retval = filter_configure();
276
		$savemsg = get_std_save_message($retval);
250 277

  
251
			$retval = 0;
252
			$retval = filter_configure();
278
		if (stristr($retval, "error") <> true) {
253 279
			$savemsg = get_std_save_message($retval);
254
			
255
			if (stristr($retval, "error") <> true)
256
					$savemsg = get_std_save_message($retval);
257
			else
258
					$savemsg = $retval;
280
		} else {
281
			$savemsg = $retval;
282
		}
259 283

  
260
 		/* XXX: TODO Make dummynet pretty graphs */ 
284
		/* XXX: TODO Make dummynet pretty graphs */
261 285
		//	enable_rrd_graphing();
262 286

  
263
			clear_subsystem_dirty('shaper');
264
			
265
			if ($queue) {
266
				$output_form .= $queue->build_form();
267
				$dontshow = false;
268
			}
269
			else {
270
				$output_form .= $dn_default_shaper_message;
271
				$dontshow = true;
272
			}
287
		clear_subsystem_dirty('shaper');
273 288

  
289
		if ($queue) {
290
			$output_form .= $queue->build_form();
291
			$dontshow = false;
292
		} else {
293
			$output_form .= $dn_default_shaper_message;
294
			$dontshow = true;
295
		}
274 296
	} else if ($queue) {
275
                $queue->validate_input($_POST, $input_errors);
276
                if (!$input_errors) {
297
		$queue->validate_input($_POST, $input_errors);
298
		if (!$input_errors) {
277 299
			$queue->update_dn_data($_POST);
278 300
			$queue->wconfig();
279
			if (write_config())
301
			if (write_config()) {
280 302
				mark_subsystem_dirty('shaper');
303
			}
281 304
			$dontshow = false;
282
                } 
305
		}
283 306
		read_dummynet_config();
284 307
		$output_form .= $queue->build_form();
285 308
	} else  {
......
292 315
}
293 316

  
294 317
if ($queue) {
295
                        if ($queue->GetEnabled())
296
                                $can_enable = true;
297
                        else
298
                                $can_enable = false;
299
                        if ($queue->CanHaveChildren()) { 
300
                       		$can_add = true;
301
                        } else
302
                                $can_add = false;
318
	if ($queue->GetEnabled()) {
319
		$can_enable = true;
320
	} else {
321
		$can_enable = false;
322
	}
323
	if ($queue->CanHaveChildren()) {
324
		$can_add = true;
325
	} else {
326
		$can_add = false;
327
	}
303 328
}
304 329

  
305 330
$tree = "<ul class=\"tree\" >";
306 331
if (is_array($dummynet_pipe_list)) {
307
        foreach ($dummynet_pipe_list as $tmpdn) {
308
                $tree .= $tmpdn->build_tree();
309
        }
332
	foreach ($dummynet_pipe_list as $tmpdn) {
333
		$tree .= $tmpdn->build_tree();
334
	}
310 335
}
311 336
$tree .= "</ul>";
312 337

  
......
319 344
$output_form .= "<input type=\"submit\" name=\"Submit\" value=\"" . gettext("Save") . "\" class=\"formbtn\" />";
320 345
if ($can_add || $addnewaltq) {
321 346
	$output_form .= "<a href=\"firewall_shaper_vinterface.php?pipe=";
322
	$output_form .= $pipe; 
347
	$output_form .= $pipe;
323 348
	if ($queue) {
324 349
		$output_form .= "&amp;queue=" . $queue->GetQname();
325 350
	}
......
334 359
}
335 360
$output_form .= "&amp;action=delete\">";
336 361
$output_form .= "<input type=\"button\" class=\"formbtn\" name=\"delete\"";
337
if ($queue)
362
if ($queue) {
338 363
	$output_form .= " value=\"" . gettext("Delete this queue") ."\" />";
339
else
364
} else {
340 365
	$output_form .= " value=\"" . gettext("Delete Limiter") ."\" />";
341
$output_form .= "</a>";  
366
}
367
$output_form .= "</a>";
342 368
$output_form .= "</td></tr>";
343 369
$output_form .= "</table>";
344
} 
345
else 
370
} else {
346 371
	$output_form .= "</table>";
372
}
347 373

  
348 374
$output = "<table summary=\"output form\">";
349 375
$output .= $output_form;
......
355 381
<script type="text/javascript">
356 382
//<![CDATA[
357 383
function show_source_port_range() {
358
        document.getElementById("sprtable").style.display = '';
384
	document.getElementById("sprtable").style.display = '';
359 385
	document.getElementById("sprtable1").style.display = '';
360 386
	document.getElementById("sprtable2").style.display = '';
361 387
	document.getElementById("sprtable5").style.display = '';
......
369 395
<body link="#0000CC" vlink="#0000CC" alink="#0000CC">
370 396

  
371 397
<?php
372
if ($queue)
398
if ($queue) {
373 399
	echo $queue->build_javascript();
374
else
400
} else {
375 401
	echo $newjavascript;
402
}
376 403

  
377
include("fbegin.inc"); 
404
include("fbegin.inc");
378 405
?>
379 406
<div id="inputerrors"></div>
380 407
<?php if ($input_errors) print_input_errors($input_errors); ?>
......
386 413
<?php print_info_box_np(gettext("The traffic shaper configuration has been changed.")."<br />".gettext("You must apply the changes in order for them to take effect."));?><br /></p>
387 414
<?php endif; ?>
388 415
<table width="100%" border="0" cellpadding="0" cellspacing="0" summary="traffic shaper limiter">
389
  <tr><td>
416
	<tr><td>
390 417
<?php
391 418
	$tab_array = array();
392 419
	$tab_array[0] = array(gettext("By Interface"), false, "firewall_shaper.php");
......
396 423
	$tab_array[4] = array(gettext("Wizards"), false, "firewall_shaper_wizards.php");
397 424
	display_top_tabs($tab_array);
398 425
?>
399
  </td></tr>
400
  <tr>
401
    <td>
402
	<div id="mainarea">
403
              <table class="tabcont" width="100%" border="0" cellpadding="0" cellspacing="0" summary="main area">
426
	</td></tr>
427
	<tr>
428
		<td>
429
			<div id="mainarea">
430
				<table class="tabcont" width="100%" border="0" cellpadding="0" cellspacing="0" summary="main area">
404 431
<?php if (count($dummynet_pipe_list) > 0): ?>
405
                        <tr class="tabcont"><td width="25%" align="left">
406
                        </td><td width="75%"> </td></tr>
432
					<tr class="tabcont">
433
						<td width="25%" align="left"></td>
434
						<td width="75%"> </td>
435
					</tr>
407 436
<?php endif; ?>
408
			<tr>
409
			<td width="25%" valign="top" align="left">
410
			<?php
411
				echo $tree; 
412
			?>
413
			<br /><br />
414
			<a href="firewall_shaper_vinterface.php?pipe=new&amp;action=add">
415
			<img src="./themes/<?= $g['theme']; ?>/images/icons/icon_plus.gif" title="<?=gettext("Create new limiter");?>" width="17" height="17" border="0" alt="add" />&nbsp;<?=gettext("Create new limiter");?>
416
			</a><br />
417
			</td>
418
			<td width="75%" valign="top" align="center">
419
			<div id="shaperarea" style="position:relative">
420
			<?php
421
				echo $output;
422
			?>	
437
					<tr>
438
						<td width="25%" valign="top" align="left">
439
							<?php
440
								echo $tree;
441
							?>
442
							<br /><br />
443
							<a href="firewall_shaper_vinterface.php?pipe=new&amp;action=add">
444
								<img src="./themes/<?= $g['theme']; ?>/images/icons/icon_plus.gif" title="<?=gettext("Create new limiter");?>" width="17" height="17" border="0" alt="add" />&nbsp;<?=gettext("Create new limiter");?>
445
							</a>
446
							<br />
447
						</td>
448
						<td width="75%" valign="top" align="center">
449
							<div id="shaperarea" style="position:relative">
450
								<?php
451
									echo $output;
452
								?>
453
							</div>
454
						</td>
455
					</tr>
456
				</table>
423 457
			</div>
424

  
425
		      </td></tr>
426
                    </table>
427
		</div>
428
	  </td>
458
		</td>
429 459
	</tr>
430 460
</table>
431 461
</form>
......
433 463
//<![CDATA[
434 464
<?php
435 465
	$totalrows = 0;
436
	if (is_array($config['dnshaper']) && is_array($config['dnshaper']['queue'])) 
466
	if (is_array($config['dnshaper']) && is_array($config['dnshaper']['queue'])) {
437 467
		$totalrows = count($config['dnshaper']['queue']);
468
	}
438 469
	echo "totalrows = {$totalrows}";
439 470
?>
440 471
//]]>

Also available in: Unified diff