From 449da348c0336411ae66464b4f1af6b509e9173c Mon Sep 17 00:00:00 2001 From: Shivam Mathur Date: Mon, 17 Jan 2022 08:58:30 +0530 Subject: [PATCH] Fix enabling disabled extensions with dependencies Improve enabling extensions with no dependencies --- src/scripts/darwin.sh | 3 ++ src/scripts/extensions/add_extensions.ps1 | 18 +++++++--- src/scripts/extensions/add_extensions.sh | 13 ++++--- src/scripts/extensions/extension_map.php | 41 ++++++++++++++++++----- src/scripts/linux.sh | 3 ++ 5 files changed, 60 insertions(+), 18 deletions(-) diff --git a/src/scripts/darwin.sh b/src/scripts/darwin.sh index 3aea119b..735f4f45 100644 --- a/src/scripts/darwin.sh +++ b/src/scripts/darwin.sh @@ -23,11 +23,14 @@ disable_dependency_extensions() { disable_extension_helper() { local extension=$1 local disable_dependents=${2:-false} + get_extension_map if [ "$disable_dependents" = "true" ]; then disable_extension_dependents "$extension" fi sudo sed -Ei '' "/=(.*\/)?\"?$extension(.so)?$/d" "${ini_file:?}" sudo rm -rf "$scan_dir"/*"$extension"* + mkdir -p /tmp/extdisabled/"$version" + echo '' | sudo tee /tmp/extdisabled/"$version"/"$extension" >/dev/null 2>&1 } # Function to fetch a brew tap. diff --git a/src/scripts/extensions/add_extensions.ps1 b/src/scripts/extensions/add_extensions.ps1 index 60fffcdc..aba949ed 100644 --- a/src/scripts/extensions/add_extensions.ps1 +++ b/src/scripts/extensions/add_extensions.ps1 @@ -82,7 +82,7 @@ Function Add-Extension { # Function to get a map of extensions and their dependent shared extensions. Function Get-ExtensionMap { - php -d'error_reporting=0' $dist\..\src\scripts\ext\extension_map.php + php -d'error_reporting=0' $dist\..\src\scripts\extensions\extension_map.php $env:TEMP\map.orig } # Function to enable extension dependencies which are also extensions. @@ -94,9 +94,10 @@ Function Enable-ExtensionDependencies { [string] $extension ) - if (-not(Test-Path $env:TEMP\map.orig)) { - Get-ExtensionMap | Set-Content -Path $env:TEMP\map.orig + if (-not(Test-Path $env:TEMP\extdisabled\$extension)) { + return } + Get-ExtensionMap $entry = findstr /r "$extension`:.*" $env:TEMP\map.orig if($entry) { $entry.split(':')[1].trim().split(' ') | ForEach-Object { @@ -105,6 +106,7 @@ Function Enable-ExtensionDependencies { } } } + Remove-Item $env:TEMP\extdisabled\$extension -Force } # Function to disable dependent extensions. @@ -116,7 +118,7 @@ Function Disable-DependentExtensions() { [string] $extension ) - Get-ExtensionMap | Select-String -Pattern ".*:.*\s$extension(\s|$)" | ForEach-Object { + Select-String -Pattern ".*:.*\s$extension(\s|$)" $env:TEMP\map.orig | ForEach-Object { $dependent = $_.Matches[0].Value.split(':')[0]; Disable-ExtensionHelper -Extension $dependent -DisableDependents Add-Log $tick ":$extension" "Disabled $dependent as it depends on $extension" @@ -133,10 +135,13 @@ Function Disable-ExtensionHelper() { $extension, [switch] $DisableDependents ) + Get-ExtensionMap if($DisableDependents) { Disable-DependentExtensions $extension } Disable-PhpExtension -Extension $extension -Path $php_dir + New-Item $env:TEMP\extdisabled -Type Directory -Force > $null 2>&1 + New-Item $env:TEMP\extdisabled\$extension -Type File -Force > $null 2>&1 } # Function to disable an extension. @@ -174,7 +179,12 @@ Function Disable-Extension() { # Function to disable shared extensions. Function Disable-AllShared() { + Get-ExtensionMap (Get-Content $php_dir\php.ini) | Where-Object {$_ -notmatch '^(zend_)?extension\s*='} | Set-Content $php_dir\php.ini + New-Item $env:TEMP\extdisabled\$version -Type Directory -Force > $null 2>&1 + Get-Childitem $ext_dir\*.dll | ForEach-Object { + New-Item ("$env:TEMP\extdisabled\$version\" + ($_.Name.split('.')[0].split('_')[1])) -Type File -Force > $null 2>&1 + } Add-Log $tick "none" "Disabled all shared extensions" } diff --git a/src/scripts/extensions/add_extensions.sh b/src/scripts/extensions/add_extensions.sh index 41c9fb75..ed5d9045 100644 --- a/src/scripts/extensions/add_extensions.sh +++ b/src/scripts/extensions/add_extensions.sh @@ -49,25 +49,25 @@ enable_extension() { # Function to get a map of extensions and their dependent shared extensions. get_extension_map() { - php -d'error_reporting=0' "${dist:?}"/../src/scripts/extensions/extension_map.php + php -d'error_reporting=0' "${dist:?}"/../src/scripts/extensions/extension_map.php /tmp/map.orig } # Function to enable extension dependencies which are also extensions. enable_extension_dependencies() { local extension=$1 prefix=$2 - if ! [ -e /tmp/map.orig ]; then - get_extension_map | sudo tee /tmp/map.orig >/dev/null - fi + [ -e /tmp/extdisabled/"$version"/"$extension" ] || return; + get_extension_map for dependency in $(grep "$extension:" /tmp/map.orig | cut -d ':' -f 2 | tr '\n' ' '); do enable_extension "$dependency" "$prefix" done + rm /tmp/extdisabled/"$version"/"$extension" } # Function to disable dependent extensions. disable_extension_dependents() { local extension=$1 - for dependent in $(get_extension_map | grep -E ".*:.*\s$extension(\s|$)" | cut -d ':' -f 1 | tr '\n' ' '); do + for dependent in $(grep -E ".*:.*\s$extension(\s|$)" /tmp/map.orig | cut -d ':' -f 1 | tr '\n' ' '); do disable_extension_helper "$dependent" true add_log "${tick:?}" ":$extension" "Disabled $dependent as it depends on $extension" done @@ -93,8 +93,11 @@ disable_extension() { # Function to disable shared extensions. disable_all_shared() { + get_extension_map sudo sed -i.orig -E -e "/^(zend_)?extension\s*=/d" "${ini_file[@]}" "$pecl_file" 2>/dev/null || true sudo find "${ini_dir:-$scan_dir}"/.. -name "*.ini" -not -path "*php.ini" -not -path "*phar.ini" -not -path "*pecl.ini" -not -path "*mods-available*" -delete >/dev/null 2>&1 || true + mkdir -p /tmp/extdisabled/"$version" + sudo find "$ext_dir" -name '*.so' -print0 | xargs -0 -n 1 basename -s .so | xargs -n 1 -I{} touch /tmp/extdisabled/"$version"/{} add_log "${tick:?}" "none" "Disabled all shared extensions" } diff --git a/src/scripts/extensions/extension_map.php b/src/scripts/extensions/extension_map.php index fda0080d..54d84d6a 100644 --- a/src/scripts/extensions/extension_map.php +++ b/src/scripts/extensions/extension_map.php @@ -15,7 +15,7 @@ class ExtensionMap { /** @var string Prefix in PHP extension file. */ private $file_prefix; - /** @var string String to store the map */ + /** @var array Array to store the map */ private $map; /** @@ -25,7 +25,23 @@ class ExtensionMap { $this->extension_dir = ini_get('extension_dir'); $this->file_extension = (PHP_OS == 'WINNT' ? '.dll' : '.so'); $this->file_prefix = (PHP_OS == 'WINNT' ? 'php_' : ''); - $this->map = ''; + $this->map = array(); + } + + /** + * Function to read the extension map. + */ + public function parseMap($path) { + if(file_exists($path)) { + $handle = fopen($path, "r"); + if ($handle) { + while (($line = fgets($handle)) !== false) { + $line_parts = explode(':', $line); + $this->map[$line_parts[0]] = explode(' ', trim($line_parts[1])); + } + fclose($handle); + } + } } /** @@ -75,6 +91,9 @@ class ExtensionMap { * @throws ReflectionException */ public function addExtensionToMap($extension) { + if($this->map && array_key_exists($extension, $this->map) && !empty($this->map[$extension])) { + return; + } // PHP 5.3 does not allow using $this. $self = $this; @@ -84,15 +103,15 @@ class ExtensionMap { $dependencies = array_filter($dependencies, function ($dependency) use ($self) { return $self->checkSharedExtension($dependency); }); - $self->map .= $extension . ': ' . implode(' ', $dependencies) . PHP_EOL; + $self->map[$extension] = $dependencies; } /** - * Function to print the map of shared extensions and their dependent extensions. - * - * @return string + * Function to write the map of shared extensions and their dependent extensions. */ - public function __toString() { + public function write() { + $path = $_SERVER['argv'][1]; + $this->parseMap($path); $extensions = array_map('strtolower', $this->getSharedExtensions()); foreach ($extensions as $extension) { try { @@ -101,9 +120,13 @@ class ExtensionMap { } } - return $this->map; + $map_string = ''; + foreach($this->map as $extension => $dependencies) { + $map_string .= $extension . ': ' . implode(' ', $dependencies) . PHP_EOL; + } + file_put_contents($path, $map_string); } } $extension_map = new ExtensionMap(); -echo $extension_map; +$extension_map->write(); diff --git a/src/scripts/linux.sh b/src/scripts/linux.sh index 790ba506..bc2f5766 100644 --- a/src/scripts/linux.sh +++ b/src/scripts/linux.sh @@ -25,11 +25,14 @@ install_packages() { disable_extension_helper() { local extension=$1 local disable_dependents=${2:-false} + get_extension_map if [ "$disable_dependents" = "true" ]; then disable_extension_dependents "$extension" fi sudo sed -Ei "/=(.*\/)?\"?$extension(.so)?$/d" "${ini_file[@]}" "$pecl_file" sudo find "$ini_dir"/.. -name "*$extension.ini" -not -path "*phar.ini" -not -path "*pecl.ini" -not -path "*mods-available*" -delete >/dev/null 2>&1 || true + mkdir -p /tmp/extdisabled/"$version" + echo '' | sudo tee /tmp/extdisabled/"$version"/"$extension" >/dev/null 2>&1 } # Function to add PDO extension.