Просмотр исходного кода

:octocat: QRFpdf: proper value check/clamp

smiley 3 лет назад
Родитель
Сommit
22ad482e57
1 измененных файлов с 22 добавлено и 3 удалено
  1. 22 3
      src/Output/QRFpdf.php

+ 22 - 3
src/Output/QRFpdf.php

@@ -15,7 +15,7 @@ use chillerlan\QRCode\Data\QRMatrix;
 use chillerlan\Settings\SettingsContainerInterface;
 use chillerlan\Settings\SettingsContainerInterface;
 use FPDF;
 use FPDF;
 
 
-use function array_values, class_exists, count, is_array;
+use function array_values, class_exists, count, is_array, is_numeric, max, min;
 
 
 /**
 /**
  * QRFpdf output module (requires fpdf)
  * QRFpdf output module (requires fpdf)
@@ -48,14 +48,33 @@ class QRFpdf extends QROutputAbstract{
 	 * @inheritDoc
 	 * @inheritDoc
 	 */
 	 */
 	protected function moduleValueIsValid($value):bool{
 	protected function moduleValueIsValid($value):bool{
-		return is_array($value) && count($value) >= 3;
+
+		if(!is_array($value) || count($value) < 3){
+			return false;
+		}
+
+		// check the first 3 values of the array
+		for($i = 0; $i < 3; $i++){
+			if(!is_numeric($value[$i])){
+				return false;
+			}
+		}
+
+		return true;
 	}
 	}
 
 
 	/**
 	/**
 	 * @inheritDoc
 	 * @inheritDoc
 	 */
 	 */
 	protected function getModuleValue($value):array{
 	protected function getModuleValue($value):array{
-		return array_values($value);
+		$v = [];
+
+		for($i = 0; $i < 3; $i++){
+			// clamp value
+			$v[] = (int)max(0, min(255, $value[$i]));
+		}
+
+		return $v;
 	}
 	}
 
 
 	/**
 	/**