php-src/ext/curl/sync-constants.php
Ayesh Karunaratne edb9f65f16
ext/curl: Bump minimum Curl version to >= 7.61.0 (#13259)
Bumps the minimum required libcurl version to 7.61.0.

Please also see #4917, which bumped minimum libcurl version to the current >= 7.29.0.
This bumps the minimum requirement to Curl 7.61.0 (released 2018 Sept).

Ubuntu, Debian, RHEL, and RHEL derivatives have major and LTS version bumps this year. Following are the
libcurl-dev/libcurl-devel versions available in the oldest supported (LTS or otherwise) in major OSs.

 - Debian buster: [7.64](https://packages.debian.org/buster/libcurl4-openssl-dev)
 - Ubuntu 20.04: [7.68](https://packages.ubuntu.com/focal/libcurl-dev)
 - CentOS/RHEL 7: 7.29
 - RHEL 8/Rocky 8/EL 8: 7.61
 - Fedora 38: 7.87

RHEL/CentOS 7 reaches EOL mid 2024, so for PHP 8.4 scheduled towards the end of this year, we can safely
bump the minimum libcurl version.

7.61.0 was selected as the new minimum because RHEL and derivatives have libcurl-devel version 7.61. RHEL 8 is
a current and supported RHEL version.
2024-01-31 13:48:17 +00:00

326 lines
8.1 KiB
PHP
Executable file

#!/usr/bin/env php
<?php
/**
* This script checks the constants defined in the curl PHP extension, against those documented on the cURL website:
* https://curl.se/libcurl/c/symbols-in-versions.html
*
* See the discussion at: https://github.com/php/php-src/pull/2961
*/
const CURL_DOC_FILE = 'https://curl.se/libcurl/c/symbols-in-versions.html';
const SOURCE_FILE = __DIR__ . '/curl_arginfo.h';
const MIN_SUPPORTED_CURL_VERSION = '7.61.0';
const IGNORED_CURL_CONSTANTS = [
'CURLOPT_PROGRESSDATA',
'CURLOPT_XFERINFODATA',
];
const IGNORED_PHP_CONSTANTS = [
'CURLOPT_BINARYTRANSFER',
'CURLOPT_RETURNTRANSFER',
'CURLOPT_SAFE_UPLOAD',
];
const CONSTANTS_REGEX_PATTERN = '~^CURL(?:E|INFO|OPT|_VERSION|_HTTP)_[A-Z0-9_]+$~';
/**
* A simple helper to create ASCII tables.
* It assumes that the same number of columns is always given to add().
*/
class AsciiTable
{
/**
* @var array
*/
private $values = [];
/**
* @var array
*/
private $length = [];
/**
* @var int
*/
private $padding = 4;
/**
* @param string[] $values
*
* @return void
*/
public function add(string ...$values) : void
{
$this->values[] = $values;
foreach ($values as $key => $value) {
$length = strlen($value);
if (isset($this->length[$key])) {
$this->length[$key] = max($this->length[$key], $length);
} else {
$this->length[$key] = $length;
}
}
}
/**
* @return string
*/
public function __toString() : string
{
$result = '';
foreach ($this->values as $values) {
foreach ($values as $key => $value) {
if ($key !== 0) {
$result .= str_repeat(' ', $this->padding);
}
$result .= str_pad($value, $this->length[$key]);
}
$result .= "\n";
}
return $result;
}
}
$curlConstants = getCurlConstants();
$sourceConstants = getSourceConstants();
$notInPHP = []; // In cURL doc, but missing from PHP
$notInCurl = []; // In the PHP source, but not in the cURL doc
$outdated = []; // In the PHP source, but removed before the minimum supported cURL version
foreach ($curlConstants as $name => [$introduced, $deprecated, $removed]) {
$inPHP = in_array($name, $sourceConstants);
if ($removed !== null) {
if (version_compare($removed, MIN_SUPPORTED_CURL_VERSION) <= 0) {
// constant removed before the minimum supported version
continue;
}
}
if (! $inPHP) {
$notInPHP[$name] = [$introduced, $removed];
}
}
foreach ($sourceConstants as $name) {
if (! isset($curlConstants[$name])) {
$notInCurl[] = $name;
continue;
}
$removed = $curlConstants[$name][2];
if ($removed === null) {
continue;
}
if (version_compare($removed, MIN_SUPPORTED_CURL_VERSION) <= 0) {
// constant removed before the minimum supported version
$outdated[$name] = $removed;
}
}
$allGood = true;
if ($notInPHP) {
uasort($notInPHP, function($a, $b) {
return version_compare($a[0], $b[0]);
});
$table = new AsciiTable();
$table->add('Constant', 'Introduced', '', 'Removed', '');
foreach ($notInPHP as $name => [$introduced, $removed]) {
if ($removed === null) {
$removed = '';
$removedHex = '';
} else {
$removedHex = getHexVersion($removed);
}
$table->add($name, $introduced, getHexVersion($introduced), $removed, $removedHex);
}
echo "Constants missing from the PHP source:\n\n";
echo $table, "\n";
$allGood = false;
}
if ($notInCurl) {
$table = new AsciiTable();
foreach ($notInCurl as $name) {
$table->add($name);
}
echo "Constants defined in the PHP source, but absent from the cURL documentation:\n\n";
echo $table, "\n";
$allGood = false;
}
if ($outdated) {
uasort($outdated, function($a, $b) {
return version_compare($a, $b);
});
$table = new AsciiTable();
$table->add('Constant', 'Removed');
foreach ($outdated as $name => $version) {
$table->add($name, $version);
}
echo "Constants defined in the PHP source, but removed before the minimum supported cURL version:\n\n";
echo $table, "\n";
$allGood = false;
}
if ($allGood) {
echo "All good! Source code and cURL documentation are in sync.\n";
}
/**
* Loads and parses the cURL constants from the online documentation.
*
* The result is an associative array where the key is the constant name, and the value is a numeric array with:
* - the introduced version
* - the deprecated version (nullable)
* - the removed version (nullable)
*
* @return array
*/
function getCurlConstants() : array
{
$html = file_get_contents(CURL_DOC_FILE);
// Extract the constant list from the HTML file (located in the only <pre> tag in the page)
preg_match('~<table>(.*?)</table>~s', $html, $matches);
$constantList = $matches[1];
/**
* Parse the cURL constant lines. Possible formats:
*
* Name Introduced Deprecated Removed
* CURLOPT_CRLFILE 7.19.0
* CURLOPT_DNS_USE_GLOBAL_CACHE 7.9.3 7.11.1
* CURLOPT_FTPASCII 7.1 7.11.1 7.15.5
* CURLOPT_HTTPREQUEST 7.1 - 7.15.5
*/
$regexp = '@<tr><td>(?:<a href=".*?">)?(?<const>[A-Za-z0-9_]+)(?:</a>)?</td><td>(?:<a href=".*?">)?(?<added>[\d\.]+)(?:</a>)?</td><td>(?:<a href=".*?">)?(?<deprecated>[\d\.]+)?(?:</a>)?</td><td>(<a href=".*?">)?(?<removed>[\d\.]+)?(</a>)?</td></tr>@m';
preg_match_all($regexp, $constantList, $matches, PREG_SET_ORDER);
$constants = [];
foreach ($matches as $match) {
$name = $match['const'];
$introduced = $match['added'];
$deprecated = $match['deprecated'] ?? null;
$removed = $match['removed'] ?? null;
if (in_array($name, IGNORED_CURL_CONSTANTS, true) || !preg_match(CONSTANTS_REGEX_PATTERN, $name)) {
// not a wanted constant
continue;
}
if ($deprecated === '-') { // deprecated version can be a hyphen
$deprecated = null;
}
$constants[$name] = [$introduced, $deprecated, $removed];
}
return $constants;
}
/**
* Parses the defined cURL constants from the PHP extension source code.
*
* The result is a numeric array whose values are the constant names.
*
* @return array
*/
function getSourceConstants() : array
{
$source = file_get_contents(SOURCE_FILE);
preg_match_all('/REGISTER_LONG_CONSTANT\(\"\w+\", (\w+), .+\)/', $source, $matches);
$constants = [];
foreach ($matches[1] as $name) {
if ($name === '__c') { // macro
continue;
}
if (in_array($name, IGNORED_PHP_CONSTANTS, true) || !preg_match(CONSTANTS_REGEX_PATTERN, $name)) {
// not a wanted constant
continue;
}
$constants[] = $name;
}
return $constants;
}
/**
* Converts a version number to its hex representation as used in the extension source code.
*
* Example: 7.25.1 => 0x071901
*
* @param string $version
*
* @return string
*
* @throws \RuntimeException
*/
function getHexVersion(string $version) : string
{
$parts = explode('.', $version);
if (count($parts) === 2) {
$parts[] = '0';
}
if (count($parts) !== 3) {
throw new \RuntimeException('Invalid version number: ' . $version);
}
$hex = '0x';
foreach ($parts as $value) {
if (! ctype_digit($value) || strlen($value) > 3) {
throw new \RuntimeException('Invalid version number: ' . $version);
}
$value = (int) $value;
if ($value > 255) {
throw new \RuntimeException('Invalid version number: ' . $version);
}
$value = dechex($value);
if (strlen($value) === 1) {
$value = '0' . $value;
}
$hex .= $value;
}
return $hex;
}