PHP 8.1 fixes and PHP 5.x CI fix (#463)

* Fix call signatures on example 12 for PHP 8.1 strpos null errors

* Fix #401 - PHP 8.1 casts to int - getGDgamma - CoonsPatchMesh

Fixes: tecnickcom/TCPDF#401

* PHP 8.1 - cast in _RC4 before sending to str_repeat

* Enable back extensions on PHP 5.{3,4,5}

* Improve the test script to allow testing other PHP versions locally

* Fix typo on "parametrs" -> "parameters"

* Reduce possible side effects with the OS

* Fix #461 - fix barcodes handling using false, PHP 8.1 deprecation

Fixes: tecnickcom/TCPDF#461

* Fix #401 - PHP 8.1 casts to int - CoonsPatchMesh

Fixes: tecnickcom/TCPDF#401

Co-authored-by: Nicola Asuni <nicolaasuni@users.noreply.github.com>
This commit is contained in:
William Desportes 2021-12-31 09:34:37 +01:00 committed by GitHub
parent a5b2fdbbf1
commit 960e7d563d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 43 additions and 40 deletions

View File

@ -91,7 +91,7 @@ $pdf->Rect(145, 10, 40, 20, 'D', array('all' => $style3));
// Curve
$pdf->Text(5, 34, 'Curve examples');
$pdf->Curve(5, 40, 30, 55, 70, 45, 60, 75, null, $style6);
$pdf->Curve(5, 40, 30, 55, 70, 45, 60, 75, '', $style6);
$pdf->Curve(80, 40, 70, 75, 150, 45, 100, 75, 'F', $style6);
$pdf->Curve(140, 40, 150, 55, 180, 45, 200, 75, 'DF', $style6, array(200, 220, 200));
@ -99,18 +99,18 @@ $pdf->Curve(140, 40, 150, 55, 180, 45, 200, 75, 'DF', $style6, array(200, 220, 2
$pdf->Text(5, 79, 'Circle and ellipse examples');
$pdf->setLineStyle($style5);
$pdf->Circle(25,105,20);
$pdf->Circle(25,105,10, 90, 180, null, $style6);
$pdf->Circle(25,105,10, 90, 180, '', $style6);
$pdf->Circle(25,105,10, 270, 360, 'F');
$pdf->Circle(25,105,10, 270, 360, 'C', $style6);
$pdf->setLineStyle($style5);
$pdf->Ellipse(100,103,40,20);
$pdf->Ellipse(100,105,20,10, 0, 90, 180, null, $style6);
$pdf->Ellipse(100,105,20,10, 0, 90, 180, '', $style6);
$pdf->Ellipse(100,105,20,10, 0, 270, 360, 'DF', $style6);
$pdf->setLineStyle($style5);
$pdf->Ellipse(175,103,30,15,45);
$pdf->Ellipse(175,105,15,7.50, 45, 90, 180, null, $style6);
$pdf->Ellipse(175,105,15,7.50, 45, 90, 180, '', $style6);
$pdf->Ellipse(175,105,15,7.50, 45, 270, 360, 'F', $style6, array(220, 200, 200));
// Polygon
@ -130,9 +130,9 @@ $pdf->Text(5, 169, 'Regular polygon examples');
$pdf->setLineStyle($style5);
$pdf->RegularPolygon(20, 190, 15, 6, 0, 1, 'F');
$pdf->RegularPolygon(55, 190, 15, 6);
$pdf->RegularPolygon(55, 190, 10, 6, 45, 0, 'DF', array($style6, 0, $style7, 0, $style7, $style7));
$pdf->RegularPolygon(90, 190, 15, 3, 0, 1, 'DF', array('all' => $style5), array(200, 220, 200), 'F', array(255, 200, 200));
$pdf->RegularPolygon(125, 190, 15, 4, 30, 1, null, array('all' => $style5), null, null, $style6);
$pdf->RegularPolygon(55, 190, 10, 6, 45, false, 'DF', array($style6, 0, $style7, 0, $style7, $style7));
$pdf->RegularPolygon(90, 190, 15, 3, 0, true, 'DF', array('all' => $style5), array(200, 220, 200), 'F', array(255, 200, 200));
$pdf->RegularPolygon(125, 190, 15, 4, 30, true, '', array('all' => $style5), array(), '', $style6);
$pdf->RegularPolygon(160, 190, 15, 10);
// Star polygon
@ -140,9 +140,9 @@ $pdf->Text(5, 209, 'Star polygon examples');
$pdf->setLineStyle($style5);
$pdf->StarPolygon(20, 230, 15, 20, 3, 0, 1, 'F');
$pdf->StarPolygon(55, 230, 15, 12, 5);
$pdf->StarPolygon(55, 230, 7, 12, 5, 45, 0, 'DF', array('all' => $style7), array(220, 220, 200), 'F', array(255, 200, 200));
$pdf->StarPolygon(90, 230, 15, 20, 6, 0, 1, 'DF', array('all' => $style5), array(220, 220, 200), 'F', array(255, 200, 200));
$pdf->StarPolygon(125, 230, 15, 5, 2, 30, 1, null, array('all' => $style5), null, null, $style6);
$pdf->StarPolygon(55, 230, 7, 12, 5, 45, false, 'DF', array('all' => $style7), array(220, 220, 200), 'F', array(255, 200, 200));
$pdf->StarPolygon(90, 230, 15, 20, 6, 0, true, 'DF', array('all' => $style5), array(220, 220, 200), 'F', array(255, 200, 200));
$pdf->StarPolygon(125, 230, 15, 5, 2, 30, true, '', array('all' => $style5), array(), '', $style6);
$pdf->StarPolygon(160, 230, 15, 10, 3);
$pdf->StarPolygon(160, 230, 7, 50, 26);
@ -151,7 +151,7 @@ $pdf->Text(5, 249, 'Rounded rectangle examples');
$pdf->setLineStyle(array('width' => 0.5, 'cap' => 'butt', 'join' => 'miter', 'dash' => 0, 'color' => array(0, 0, 0)));
$pdf->RoundedRect(5, 255, 40, 30, 3.50, '1111', 'DF');
$pdf->RoundedRect(50, 255, 40, 30, 6.50, '1000');
$pdf->RoundedRect(95, 255, 40, 30, 10.0, '1111', null, $style6);
$pdf->RoundedRect(95, 255, 40, 30, 10.0, '1111', '', $style6);
$pdf->RoundedRect(140, 255, 40, 30, 8.0, '0101', 'DF', $style6, array(200, 200, 200));
// Arrows

View File

@ -160,7 +160,7 @@ $pdf->Text(20, 205, 'QRCODE H');
o5 = File Size (numeric);
o6 = Checksum (numeric).
Parameters t, s and f are required for a Macro Control Block, all other parametrs are optional.
Parameters t, s and f are required for a Macro Control Block, all other parameters are optional.
To use a comma character ',' on text options, replace it with the character 255: "\xff".
*/

View File

@ -28,7 +28,8 @@ require_once('config/tcpdf_config_alt.php');
// Include the main TCPDF library (search the library on the following directories).
$tcpdf_include_dirs = array(
realpath('../tcpdf.php'),
realpath(dirname(__FILE__) . '/../tcpdf.php'),// True source file
realpath('../tcpdf.php'),// Relative from $PWD
'/usr/share/php/tcpdf/tcpdf.php',
'/usr/share/tcpdf/tcpdf.php',
'/usr/share/php-tcpdf/tcpdf.php',

View File

@ -506,7 +506,7 @@ class TCPDF_STATIC {
return $out;
}
if ($last_enc_key != $key) {
$k = str_repeat($key, ((256 / strlen($key)) + 1));
$k = str_repeat($key, (int) ((256 / strlen($key)) + 1));
$rc4 = range(0, 255);
$j = 0;
for ($i = 0; $i < 256; ++$i) {

View File

@ -7409,9 +7409,9 @@ class TCPDF {
if (!isset($this->gdgammacache['#'.$c])) {
$colors = imagecolorsforindex($img, $c);
// GD alpha is only 7 bit (0 -> 127)
$this->gdgammacache['#'.$c] = (((127 - $colors['alpha']) / 127) * 255);
$this->gdgammacache['#'.$c] = (int) (((127 - $colors['alpha']) / 127) * 255);
// correct gamma
$this->gdgammacache['#'.$c] = (pow(($this->gdgammacache['#'.$c] / 255), 2.2) * 255);
$this->gdgammacache['#'.$c] = (int) (pow(($this->gdgammacache['#'.$c] / 255), 2.2) * 255);
// store the latest values on cache to improve performances
if (count($this->gdgammacache) > 8) {
// remove one element from the cache array
@ -14529,8 +14529,8 @@ class TCPDF {
if ($patch_array[$i]['points'][$j] > $bpcd) {
$patch_array[$i]['points'][$j] = $bpcd;
}
$this->gradients[$n]['stream'] .= chr(floor($patch_array[$i]['points'][$j] / 256));
$this->gradients[$n]['stream'] .= chr(floor($patch_array[$i]['points'][$j] % 256));
$this->gradients[$n]['stream'] .= chr((int) floor($patch_array[$i]['points'][$j] / 256));
$this->gradients[$n]['stream'] .= chr((int) floor(intval($patch_array[$i]['points'][$j]) % 256));
}
$count_cols = count($patch_array[$i]['colors']);
for ($j=0; $j < $count_cols; ++$j) {
@ -15324,7 +15324,7 @@ class TCPDF {
// create new barcode object
$barcodeobj = new TCPDFBarcode($code, $type);
$arrcode = $barcodeobj->getBarcodeArray();
if (($arrcode === false) OR empty($arrcode) OR ($arrcode['maxw'] <= 0)) {
if (empty($arrcode) OR ($arrcode['maxw'] <= 0)) {
$this->Error('Error in 1D barcode string');
}
if ($arrcode['maxh'] <= 0) {
@ -15642,7 +15642,7 @@ class TCPDF {
// create new barcode object
$barcodeobj = new TCPDF2DBarcode($code, $type);
$arrcode = $barcodeobj->getBarcodeArray();
if (($arrcode === false) OR empty($arrcode) OR !isset($arrcode['num_rows']) OR ($arrcode['num_rows'] == 0) OR !isset($arrcode['num_cols']) OR ($arrcode['num_cols'] == 0)) {
if (empty($arrcode) OR !isset($arrcode['num_rows']) OR ($arrcode['num_rows'] == 0) OR !isset($arrcode['num_cols']) OR ($arrcode['num_cols'] == 0)) {
$this->Error('Error in 2D barcode string');
}
// set default values

View File

@ -53,7 +53,7 @@ class TCPDFBarcode {
* Array representation of barcode.
* @protected
*/
protected $barcode_array;
protected $barcode_array = array();
/**
* This is the class constructor.
@ -373,7 +373,7 @@ class TCPDFBarcode {
break;
}
default: {
$this->barcode_array = false;
$this->barcode_array = array();
$arrcode = false;
break;
}

View File

@ -53,7 +53,7 @@ class TCPDF2DBarcode {
* Array representation of barcode.
* @protected
*/
protected $barcode_array = false;
protected $barcode_array = array();
/**
* This is the class constructor.
@ -63,7 +63,7 @@ class TCPDF2DBarcode {
* <li>$arrcode['num_cols'] required number of columns</li>
* <li>$arrcode['bcode'][$r][$c] value of the cell is $r row and $c column (0 = transparent, 1 = black)</li></ul>
* @param string $code code to print
* @param string $type type of barcode: <ul><li>DATAMATRIX : Datamatrix (ISO/IEC 16022)</li><li>PDF417 : PDF417 (ISO/IEC 15438:2006)</li><li>PDF417,a,e,t,s,f,o0,o1,o2,o3,o4,o5,o6 : PDF417 with parameters: a = aspect ratio (width/height); e = error correction level (0-8); t = total number of macro segments; s = macro segment index (0-99998); f = file ID; o0 = File Name (text); o1 = Segment Count (numeric); o2 = Time Stamp (numeric); o3 = Sender (text); o4 = Addressee (text); o5 = File Size (numeric); o6 = Checksum (numeric). NOTES: Parameters t, s and f are required for a Macro Control Block, all other parametrs are optional. To use a comma character ',' on text options, replace it with the character 255: "\xff".</li><li>QRCODE : QRcode Low error correction</li><li>QRCODE,L : QRcode Low error correction</li><li>QRCODE,M : QRcode Medium error correction</li><li>QRCODE,Q : QRcode Better error correction</li><li>QRCODE,H : QR-CODE Best error correction</li><li>RAW: raw mode - comma-separad list of array rows</li><li>RAW2: raw mode - array rows are surrounded by square parenthesis.</li><li>TEST : Test matrix</li></ul>
* @param string $type type of barcode: <ul><li>DATAMATRIX : Datamatrix (ISO/IEC 16022)</li><li>PDF417 : PDF417 (ISO/IEC 15438:2006)</li><li>PDF417,a,e,t,s,f,o0,o1,o2,o3,o4,o5,o6 : PDF417 with parameters: a = aspect ratio (width/height); e = error correction level (0-8); t = total number of macro segments; s = macro segment index (0-99998); f = file ID; o0 = File Name (text); o1 = Segment Count (numeric); o2 = Time Stamp (numeric); o3 = Sender (text); o4 = Addressee (text); o5 = File Size (numeric); o6 = Checksum (numeric). NOTES: Parameters t, s and f are required for a Macro Control Block, all other parameters are optional. To use a comma character ',' on text options, replace it with the character 255: "\xff".</li><li>QRCODE : QRcode Low error correction</li><li>QRCODE,L : QRcode Low error correction</li><li>QRCODE,M : QRcode Medium error correction</li><li>QRCODE,Q : QRcode Better error correction</li><li>QRCODE,H : QR-CODE Best error correction</li><li>RAW: raw mode - comma-separad list of array rows</li><li>RAW2: raw mode - array rows are surrounded by square parenthesis.</li><li>TEST : Test matrix</li></ul>
*/
public function __construct($code, $type) {
$this->setBarcode($code, $type);
@ -246,7 +246,7 @@ class TCPDF2DBarcode {
/**
* Set the barcode.
* @param string $code code to print
* @param string $type type of barcode: <ul><li>DATAMATRIX : Datamatrix (ISO/IEC 16022)</li><li>PDF417 : PDF417 (ISO/IEC 15438:2006)</li><li>PDF417,a,e,t,s,f,o0,o1,o2,o3,o4,o5,o6 : PDF417 with parameters: a = aspect ratio (width/height); e = error correction level (0-8); t = total number of macro segments; s = macro segment index (0-99998); f = file ID; o0 = File Name (text); o1 = Segment Count (numeric); o2 = Time Stamp (numeric); o3 = Sender (text); o4 = Addressee (text); o5 = File Size (numeric); o6 = Checksum (numeric). NOTES: Parameters t, s and f are required for a Macro Control Block, all other parametrs are optional. To use a comma character ',' on text options, replace it with the character 255: "\xff".</li><li>QRCODE : QRcode Low error correction</li><li>QRCODE,L : QRcode Low error correction</li><li>QRCODE,M : QRcode Medium error correction</li><li>QRCODE,Q : QRcode Better error correction</li><li>QRCODE,H : QR-CODE Best error correction</li><li>RAW: raw mode - comma-separad list of array rows</li><li>RAW2: raw mode - array rows are surrounded by square parenthesis.</li><li>TEST : Test matrix</li></ul>
* @param string $type type of barcode: <ul><li>DATAMATRIX : Datamatrix (ISO/IEC 16022)</li><li>PDF417 : PDF417 (ISO/IEC 15438:2006)</li><li>PDF417,a,e,t,s,f,o0,o1,o2,o3,o4,o5,o6 : PDF417 with parameters: a = aspect ratio (width/height); e = error correction level (0-8); t = total number of macro segments; s = macro segment index (0-99998); f = file ID; o0 = File Name (text); o1 = Segment Count (numeric); o2 = Time Stamp (numeric); o3 = Sender (text); o4 = Addressee (text); o5 = File Size (numeric); o6 = Checksum (numeric). NOTES: Parameters t, s and f are required for a Macro Control Block, all other parameters are optional. To use a comma character ',' on text options, replace it with the character 255: "\xff".</li><li>QRCODE : QRcode Low error correction</li><li>QRCODE,L : QRcode Low error correction</li><li>QRCODE,M : QRcode Medium error correction</li><li>QRCODE,Q : QRcode Better error correction</li><li>QRCODE,H : QR-CODE Best error correction</li><li>RAW: raw mode - comma-separad list of array rows</li><li>RAW2: raw mode - array rows are surrounded by square parenthesis.</li><li>TEST : Test matrix</li></ul>
* @return void
*/
public function setBarcode($code, $type) {
@ -338,7 +338,7 @@ class TCPDF2DBarcode {
break;
}
default: {
$this->barcode_array = false;
$this->barcode_array = array();
}
}
}

View File

@ -22,10 +22,12 @@ EXAMPLE_BARCODE_FILES="$(find examples/barcodes -type f -name 'example*.php' \
TEMP_FOLDER="$(mktemp -d /tmp/TCPDF-tests.XXXXXXXXX)"
OUTPUT_FILE="${TEMP_FOLDER}/output.pdf"
OUTPUT_FILE_ERROR="${TEMP_FOLDER}/errors.txt"
ROOT_DIR="$(php -r 'echo realpath(__DIR__);')"
# Allows you to use PHP_BINARY="php8.1" ./tests/launch.sh
PHP_BINARY="${PHP_BINARY:-php}"
ROOT_DIR="$(${PHP_BINARY} -r 'echo realpath(__DIR__);')"
TESTS_DIR="${ROOT_DIR}/tests/"
PHP_EXT_DIR="$(php -r 'echo ini_get("extension_dir");')"
PHP_EXT_DIR="$(${PHP_BINARY} -r 'echo ini_get("extension_dir");')"
echo "php extension dir: ${PHP_EXT_DIR}"
@ -36,32 +38,28 @@ COVERAGE_EXTENSION="-d extension=pcov.so"
IMAGICK_OR_GD="-dextension=gd.so"
JSON_EXT="-dextension=json.so"
XML_EXT="-dextension=xml.so"
if [ "$(php -r 'echo PHP_MAJOR_VERSION;')" = "5" ];then
if [ "$(${PHP_BINARY} -r 'echo PHP_MAJOR_VERSION;')" = "5" ];then
X_DEBUG_EXT="$(find ${PHP_EXT_DIR} -type f -name 'xdebug.so' || '')"
echo "Xdebug found at: ${X_DEBUG_EXT}"
# pcov does not exist for PHP 5
COVERAGE_EXTENSION="-d zend_extension=${X_DEBUG_EXT} -d xdebug.mode=coverage"
# 5.5, 5.4, 5.3
if [ "$(php -r 'echo (PHP_MINOR_VERSION < 6) ? "true" : "false";')" = "true" ];then
# seems like there is no bcmath extension to be found
BCMATH_EXT=""
if [ "$(${PHP_BINARY} -r 'echo (PHP_MINOR_VERSION < 6) ? "true" : "false";')" = "true" ];then
IMAGICK_OR_GD="-dextension=imagick.so"
# Seems not to exist in 5.5, 5.4, 5.3
JSON_EXT=""
XML_EXT=""
fi
fi
# PHP >= 8.x.x
if [ "$(php -r 'echo (PHP_MAJOR_VERSION >= 8) ? "true" : "false";')" = "true" ];then
if [ "$(${PHP_BINARY} -r 'echo (PHP_MAJOR_VERSION >= 8) ? "true" : "false";')" = "true" ];then
# The json ext is bundled into PHP 8.0
JSON_EXT=""
fi
echo "Root folder: ${ROOT_DIR}"
echo "Temporary folder: ${TEMP_FOLDER}"
echo "PHP version: $(${PHP_BINARY} -v)"
FAILED_FLAG=0
@ -69,13 +67,15 @@ cd "${ROOT_DIR}/examples"
for file in $EXAMPLE_FILES; do
echo "File: $file"
php -l "${ROOT_DIR}/$file" > /dev/null
${PHP_BINARY} -l "${ROOT_DIR}/$file" > /dev/null
if [ $? -eq 0 ]; then
echo "File-lint-passed: $file"
fi
set +e
# Some examples load a bit more into memory (this is why the limit is set to 1G)
php -n \
# Avoid side effects on classes installed on the system, set include_path to a folder wihout php classes (include_path)
${PHP_BINARY} -n \
-d include_path="${TEMP_FOLDER}" \
-d date.timezone=UTC \
${IMAGICK_OR_GD} ${COVERAGE_EXTENSION} \
${BCMATH_EXT} \
@ -136,12 +136,14 @@ done
for file in $EXAMPLE_BARCODE_FILES; do
echo "File: $file"
php -l "${ROOT_DIR}/$file" > /dev/null
${PHP_BINARY} -l "${ROOT_DIR}/$file" > /dev/null
if [ $? -eq 0 ]; then
echo "File-lint-passed: $file"
fi
set +e
php -n \
# Avoid side effects on classes installed on the system, set include_path to a folder wihout php classes (include_path)
${PHP_BINARY} -n \
-d include_path="${TEMP_FOLDER}" \
-d date.timezone=UTC \
${BCMATH_EXT} ${COVERAGE_EXTENSION} \
-d display_errors=on \