Project

General

Profile

« Previous | Next » 

Revision b68e0c0c

Added by Marcos M over 1 year ago

Consolidate shaper input validation

Remove the duplicate function and integrate it with the rest of the
validate_input() methods.
do_input_validation() in guiconfig.inc cannot be included in shaper.inc;
this change avoids moving it somewhere that may not be ideal.

View differences:

src/etc/inc/shaper.inc
564 564
	}
565 565
}
566 566

  
567
/*
568
 * This is duplicated here since we cannot include guiconfig.inc.
569
 * Including it makes all stuff break.
570
 */
571
function shaper_do_input_validation($postdata, $reqdfields, $reqdfieldsn, $input_errors) {
572

  
573
	/* check for bad control characters */
574
	foreach ($postdata as $pn => $pd) {
575
		if (is_string($pd) && preg_match("/[\\x00-\\x08\\x0b\\x0c\\x0e-\\x1f]/", $pd)) {
576
			$input_errors[] = sprintf(gettext("The field '%s' contains invalid characters."), $pn);
577
		}
578
	}
579

  
580
	for ($i = 0; $i < count($reqdfields); $i++) {
581
		if ($postdata[$reqdfields[$i]] == "") {
582
			$input_errors[] = sprintf(gettext("The field '%s' is required."), $reqdfieldsn[$i]);
583
		}
584
	}
585
}
586

  
587 567
function cleanup_queue_from_rules($queue) {
588 568
	$rulelist = config_get_path('filter/rule', []);
589 569
	foreach ($rulelist as & $rule) {
......
785 765
	}
786 766

  
787 767
	function validate_input($data, &$input_errors) {
768
		// check for invalid data; taken from do_input_validation()
769
		foreach ($data as $key => $value) {
770
			if (is_string($value) && preg_match("/[\\x00-\\x08\\x0b\\x0c\\x0e-\\x1f]/", $value)) {
771
				$input_errors[] = sprintf(gettext("The field '%s' contains invalid characters."), $key);
772
			}
773
		}
788 774

  
789
		$reqdfields[] = "bandwidth";
790
		$reqdfieldsn[] = gettext("Bandwidth");
791
		$reqdfields[] = "bandwidthtype";
792
		$reqdfieldsn[] = gettext("Bandwidthtype");
793

  
794
		shaper_do_input_validation($data, $reqdfields, $reqdfieldsn, $input_errors);
795

  
796
		if ($data['bandwidth'] == null) {
775
		if (!isset($data['bandwidth']) || ($data['bandwidth'] == '')) {
797 776
			$input_errors[] = gettext("Bandwidth must be set.  This is usually the interface speed.");
798 777
		} else {
799 778
			if ((!is_numeric($data['bandwidth']))) {
......
803 782
				$input_errors[] = gettext("Bandwidth cannot be negative.");
804 783
			}
805 784
		}
806
		if ($data['bandwidthtype'] == "%") {
785
		if (!isset($data['bandwidthtype']) || ($data['bandwidthtype'] == '')) {
786
			$input_errors[] = gettext("The field 'Bandwidthtype' is required.");
787
		} elseif ($data['bandwidthtype'] == "%") {
807 788
			if ($data['bandwidth'] > 100 || $data['bandwidth'] < 0) {
808 789
				$input_errors[] = gettext("Bandwidth in percentage should be between 1 and 100.");
809 790
			}
......
1582 1563
	function validate_input($data, &$input_errors) {
1583 1564
		global $altq_list_queues;
1584 1565

  
1585
		$reqdfields[] = "name";
1586
		$reqdfieldsn[] = gettext("Name");
1587
		shaper_do_input_validation($data, $reqdfields, $reqdfieldsn, $input_errors);
1566
		// check for invalid data and required fields; taken from do_input_validation()
1567
		$reqdfields = [
1568
			"name" => gettext("Name"),
1569
		];
1570
		foreach ($data as $key => $value) {
1571
			if (is_string($value) && preg_match("/[\\x00-\\x08\\x0b\\x0c\\x0e-\\x1f]/", $value)) {
1572
				$input_errors[] = sprintf(gettext("The field '%s' contains invalid characters."), $key);
1573
			}
1574
		}
1575
		foreach ($reqdfields as $key => $descr) {
1576
			if (!isset($data[$key]) || ($data[$key] == '')) {
1577
				$input_errors[] = sprintf(gettext("The field '%s' is required."), $descr);
1578
			}
1579
		}
1580

  
1588 1581
		$parent = $altq_list_queues[$this->GetInterface()];
1589 1582

  
1590 1583
		if ($data['bandwidth'] && !is_numeric($data['bandwidth'])) {
......
2306 2299
	function validate_input($data, &$input_errors) {
2307 2300
		parent::validate_input($data, $input_errors);
2308 2301

  
2309
		$reqdfields[] = "bandwidth";
2310
		$reqdfieldsn[] = gettext("Bandwidth");
2311
		$reqdfields[] = "bandwidthtype";
2312
		$reqdfieldsn[] = gettext("Bandwidthtype");
2313

  
2314
		shaper_do_input_validation($data, $reqdfields, $reqdfieldsn, $input_errors);
2315

  
2316
		if (isset($data['linkshare3']) && !empty($data['linkshare3'])) {
2317
			if ($data['bandwidth'] && !is_numeric($data['bandwidth'])) {
2318
				$input_errors[] = gettext("Bandwidth must be an integer.");
2302
		// check for invalid data; taken from do_input_validation()
2303
		foreach ($data as $key => $value) {
2304
			if (is_string($value) && preg_match("/[\\x00-\\x08\\x0b\\x0c\\x0e-\\x1f]/", $value)) {
2305
				$input_errors[] = sprintf(gettext("The field '%s' contains invalid characters."), $key);
2319 2306
			}
2307
		}
2320 2308

  
2321
			if ($data['bandwidth'] < 0) {
2322
				$input_errors[] = gettext("Bandwidth cannot be negative.");
2309
		if (isset($data['linkshare3']) && !empty($data['linkshare3'])) {
2310
			if (!isset($data['bandwidth']) || ($data['bandwidth'] == '')) {
2311
				$input_errors[] = gettext("The field 'Bandwidth' is required.");
2312
			} else {
2313
				if (!is_numeric($data['bandwidth'])) {
2314
					$input_errors[] = gettext("Bandwidth must be an integer.");
2315
				}
2316
				if ((int)$data['bandwidth'] < 0) {
2317
					$input_errors[] = gettext("Bandwidth cannot be negative.");
2318
				}
2323 2319
			}
2324 2320

  
2325
			if ($data['bandwidthtype'] == "%") {
2321
			if (!isset($data['bandwidthtype']) || ($data['bandwidthtype'] == '')) {
2322
				$input_errors[] = gettext("The field 'Bandwidthtype' is required.");
2323
			} elseif ($data['bandwidthtype'] == "%") {
2326 2324
				if (($data['bandwidth'] > 100) || ($data['bandwidth'] < 0)) {
2327 2325
					$input_errors[] = gettext("Bandwidth in percentage should be between 1 and 100.");
2328 2326
				}
......
3053 3051
	function validate_input($data, &$input_errors) {
3054 3052
		parent::validate_input($data, $input_errors);
3055 3053

  
3056
		$reqdfields[] = "bandwidth";
3057
		$reqdfieldsn[] = gettext("Bandwidth");
3058
		$reqdfields[] = "bandwidthtype";
3059
		$reqdfieldsn[] = gettext("Bandwidthtype");
3060

  
3061
		shaper_do_input_validation($data, $reqdfields, $reqdfieldsn, $input_errors);
3054
		// check for invalid data and required fields; taken from do_input_validation()
3055
		$reqdfields = [
3056
			"bandwidth" => gettext("Bandwidth"),
3057
			"bandwidthtype" => gettext("Bandwidthtype"),
3058
		];
3059
		foreach ($data as $key => $value) {
3060
			if (is_string($value) && preg_match("/[\\x00-\\x08\\x0b\\x0c\\x0e-\\x1f]/", $value)) {
3061
				$input_errors[] = sprintf(gettext("The field '%s' contains invalid characters."), $key);
3062
			}
3063
		}
3064
		foreach ($reqdfields as $key => $descr) {
3065
			if (!isset($data[$key]) || ($data[$key] == '')) {
3066
				$input_errors[] = sprintf(gettext("The field '%s' is required."), $descr);
3067
			}
3068
		}
3062 3069

  
3063 3070
		if ($data['priority'] > 7) {
3064 3071
			$input_errors[] = gettext("Priority must be an integer between 0 and 7.");
......
3345 3352
		if ($data['priority'] > 7) {
3346 3353
				$input_errors[] = gettext("Priority must be an integer between 0 and 7.");
3347 3354
		}
3348
		$reqdfields[] = "bandwidth";
3349
		$reqdfieldsn[] = gettext("Bandwidth");
3350
		$reqdfields[] = "bandwidthtype";
3351
		$reqdfieldsn[] = gettext("Bandwidthtype");
3352 3355

  
3353
		shaper_do_input_validation($data, $reqdfields, $reqdfieldsn, $input_errors);
3356
		// check for invalid data and required fields; taken from do_input_validation()
3357
		$reqdfields = [
3358
			"bandwidth" => gettext("Bandwidth"),
3359
			"bandwidthtype" => gettext("Bandwidthtype"),
3360
		];
3361
		foreach ($data as $key => $value) {
3362
			if (is_string($value) && preg_match("/[\\x00-\\x08\\x0b\\x0c\\x0e-\\x1f]/", $value)) {
3363
				$input_errors[] = sprintf(gettext("The field '%s' contains invalid characters."), $key);
3364
			}
3365
		}
3366
		foreach ($reqdfields as $key => $descr) {
3367
			if (!isset($data[$key]) || ($data[$key] == '')) {
3368
				$input_errors[] = sprintf(gettext("The field '%s' is required."), $descr);
3369
			}
3370
		}
3354 3371
	}
3355 3372

  
3356 3373
	function ReadConfig(&$q) {
......
3690 3707
	}
3691 3708

  
3692 3709
	function validate_input($data, &$input_errors) {
3693
		$reqdfields[] = "bandwidth";
3694
		$reqdfieldsn[] = gettext("Bandwidth");
3695
		/*$reqdfields[] = "burst";
3696
		$reqdfieldsn[] = gettext("Burst"); */
3697
		$reqdfields[] = "bandwidthtype";
3698
		$reqdfieldsn[] = gettext("Bandwidthtype");
3699
		$reqdfields[] = "newname";
3700
		$reqdfieldsn[] = gettext("Name");
3701

  
3702
		shaper_do_input_validation($data, $reqdfields, $reqdfieldsn, $input_errors);
3710
		// check for invalid data and required fields; taken from do_input_validation()
3711
		$reqdfields = [
3712
			"bandwidth" => gettext("Bandwidth"),
3713
			"bandwidthtype" => gettext("Bandwidthtype"),
3714
			"newname" => gettext("Name"),
3715
		];
3716
		foreach ($data as $key => $value) {
3717
			if (is_string($value) && preg_match("/[\\x00-\\x08\\x0b\\x0c\\x0e-\\x1f]/", $value)) {
3718
				$input_errors[] = sprintf(gettext("The field '%s' contains invalid characters."), $key);
3719
			}
3720
		}
3721
		foreach ($reqdfields as $key => $descr) {
3722
			if (!isset($data[$key]) || ($data[$key] == '')) {
3723
				$input_errors[] = sprintf(gettext("The field '%s' is required."), $descr);
3724
			}
3725
		}
3703 3726

  
3704 3727
		if ($data['plr'] && (!is_numeric($data['plr']) ||
3705 3728
		    ($data['plr'] < 0) || ($data['plr'] > 1))) {

Also available in: Unified diff