From 55c96a6b816a2d52a0e51b1b4f88c04c47175972 Mon Sep 17 00:00:00 2001 From: Alex Pott <alex.a.pott@googlemail.com> Date: Fri, 1 Mar 2019 11:01:32 +0000 Subject: [PATCH] Issue #3000057 by kim.pepper, andypost, claudiu.cristea, voleger, dww, Berdir, larowlan: Deprecate drupal_set_time_limit() and file_upload_max_size() and move to Environment component --- core/includes/common.inc | 21 +++---- core/includes/file.inc | 21 ++----- .../Drupal/Component/Utility/Environment.php | 60 +++++++++++++++++++ core/lib/Drupal/Core/Cron.php | 13 ++-- core/lib/Drupal/Core/Form/FormBuilder.php | 3 +- .../Core/Test/FunctionalTestSetupTrait.php | 3 +- core/modules/editor/editor.admin.inc | 3 +- .../editor/src/Form/EditorImageDialog.php | 4 +- .../src/Plugin/Field/FieldType/FileItem.php | 5 +- .../rest/resource/FileUploadResource.php | 3 +- core/modules/locale/src/Form/ImportForm.php | 3 +- core/modules/node/node.module | 3 +- core/modules/simpletest/src/TestBase.php | 5 +- .../Core/Common/LegacyFunctionsTest.php | 9 ++- .../Core/File/FileSystemDeprecationTest.php | 7 +++ 15 files changed, 119 insertions(+), 44 deletions(-) diff --git a/core/includes/common.inc b/core/includes/common.inc index 2f8176509695..9e208d8fe289 100644 --- a/core/includes/common.inc +++ b/core/includes/common.inc @@ -11,16 +11,17 @@ use Drupal\Component\Gettext\PoItem; use Drupal\Component\Serialization\Json; use Drupal\Component\Utility\Bytes; +use Drupal\Component\Utility\Environment; use Drupal\Component\Utility\Html; use Drupal\Component\Utility\SortArray; use Drupal\Component\Utility\UrlHelper; use Drupal\Core\Cache\Cache; -use Drupal\Core\Render\HtmlResponseAttachmentsProcessor; +use Drupal\Core\Render\BubbleableMetadata; +use Drupal\Core\Render\Element; use Drupal\Core\Render\Element\Link; +use Drupal\Core\Render\HtmlResponseAttachmentsProcessor; use Drupal\Core\Render\Markup; use Drupal\Core\StringTranslation\TranslatableMarkup; -use Drupal\Core\Render\BubbleableMetadata; -use Drupal\Core\Render\Element; /** * @defgroup php_wrappers PHP wrapper functions @@ -407,15 +408,15 @@ function drupal_http_header_attributes(array $attributes = []) { * indicates unlimited execution time. * * @ingroup php_wrappers + * + * @deprecated in Drupal 8.7.0 and will be removed before Drupal 9.0.0. Use + * \Drupal\Core\Environment::setTimeLimit() instead. + * + * @see https://www.drupal.org/node/3000058 */ function drupal_set_time_limit($time_limit) { - if (function_exists('set_time_limit')) { - $current = ini_get('max_execution_time'); - // Do not set time limit if it is currently unlimited. - if ($current != 0) { - @set_time_limit($time_limit); - } - } + @trigger_error('drupal_set_time_limit() is deprecated in Drupal 8.7.0 and will be removed before Drupal 9.0.0. Use \Drupal\Core\Environment::setTimeLimit() instead. See https://www.drupal.org/node/3000058.', E_USER_DEPRECATED); + Environment::setTimeLimit($time_limit); } /** diff --git a/core/includes/file.inc b/core/includes/file.inc index 6f4552fc4775..104a31ff5405 100644 --- a/core/includes/file.inc +++ b/core/includes/file.inc @@ -7,7 +7,7 @@ use Drupal\Component\FileSystem\FileSystem as ComponentFileSystem; use Drupal\Component\PhpStorage\FileStorage; -use Drupal\Component\Utility\Bytes; +use Drupal\Component\Utility\Environment; use Drupal\Component\Utility\UrlHelper; use Drupal\Core\File\Exception\FileException; use Drupal\Core\File\Exception\FileWriteException; @@ -1048,22 +1048,13 @@ function file_scan_directory($dir, $mask, $options = [], $depth = 0) { * @return * A file size limit in bytes based on the PHP upload_max_filesize and * post_max_size + * + * @deprecated in Drupal 8.7.0 and will be removed before Drupal 9.0.0. + * Use \Drupal\Component\Utility\Environment::getUploadMaxSize() instead. */ function file_upload_max_size() { - static $max_size = -1; - - if ($max_size < 0) { - // Start with post_max_size. - $max_size = Bytes::toInt(ini_get('post_max_size')); - - // If upload_max_size is less, then reduce. Except if upload_max_size is - // zero, which indicates no limit. - $upload_max = Bytes::toInt(ini_get('upload_max_filesize')); - if ($upload_max > 0 && $upload_max < $max_size) { - $max_size = $upload_max; - } - } - return $max_size; + @trigger_error('file_upload_max_size() is deprecated in Drupal 8.7.0 and will be removed before Drupal 9.0.0. Use \Drupal\Component\Utility\Environment::getUploadMaxSize() instead. See https://www.drupal.org/node/3000058.', E_USER_DEPRECATED); + return Environment::getUploadMaxSize(); } /** diff --git a/core/lib/Drupal/Component/Utility/Environment.php b/core/lib/Drupal/Component/Utility/Environment.php index bfee60cb773d..d3fc4623a025 100644 --- a/core/lib/Drupal/Component/Utility/Environment.php +++ b/core/lib/Drupal/Component/Utility/Environment.php @@ -37,4 +37,64 @@ public static function checkMemoryLimit($required, $memory_limit = NULL) { return ((!$memory_limit) || ($memory_limit == -1) || (Bytes::toInt($memory_limit) >= Bytes::toInt($required))); } + /** + * Attempts to set the PHP maximum execution time. + * + * This function is a wrapper around the PHP function set_time_limit(). When + * called, set_time_limit() restarts the timeout counter from zero. In other + * words, if the timeout is the default 30 seconds, and 25 seconds into script + * execution a call such as set_time_limit(20) is made, the script will run + * for a total of 45 seconds before timing out. + * + * If the current time limit is not unlimited it is possible to decrease the + * total time limit if the sum of the new time limit and the current time + * spent running the script is inferior to the original time limit. It is + * inherent to the way set_time_limit() works, it should rather be called with + * an appropriate value every time you need to allocate a certain amount of + * time to execute a task than only once at the beginning of the script. + * + * Before calling set_time_limit(), we check if this function is available + * because it could be disabled by the server administrator. + * + * @param int $time_limit + * An integer time limit in seconds, or 0 for unlimited execution time. + * + * @return bool + * Whether set_time_limit() was successful or not. + */ + public static function setTimeLimit($time_limit) { + if (function_exists('set_time_limit')) { + $current = ini_get('max_execution_time'); + // Do not set time limit if it is currently unlimited. + if ($current != 0) { + return set_time_limit($time_limit); + } + } + return FALSE; + } + + /** + * Determines the maximum file upload size by querying the PHP settings. + * + * @return int + * A file size limit in bytes based on the PHP upload_max_filesize and + * post_max_size settings. + */ + public static function getUploadMaxSize() { + static $max_size = -1; + + if ($max_size < 0) { + // Start with post_max_size. + $max_size = Bytes::toInt(ini_get('post_max_size')); + + // If upload_max_size is less, then reduce. Except if upload_max_size is + // zero, which indicates no limit. + $upload_max = Bytes::toInt(ini_get('upload_max_filesize')); + if ($upload_max > 0 && $upload_max < $max_size) { + $max_size = $upload_max; + } + } + return $max_size; + } + } diff --git a/core/lib/Drupal/Core/Cron.php b/core/lib/Drupal/Core/Cron.php index fb097ced9d5c..a18bf5b5a200 100644 --- a/core/lib/Drupal/Core/Cron.php +++ b/core/lib/Drupal/Core/Cron.php @@ -3,16 +3,17 @@ namespace Drupal\Core; use Drupal\Component\Datetime\TimeInterface; +use Drupal\Component\Utility\Environment; use Drupal\Component\Utility\Timer; use Drupal\Core\Extension\ModuleHandlerInterface; -use Drupal\Core\Queue\QueueWorkerManagerInterface; -use Drupal\Core\Queue\RequeueException; -use Drupal\Core\State\StateInterface; use Drupal\Core\Lock\LockBackendInterface; use Drupal\Core\Queue\QueueFactory; -use Drupal\Core\Session\AnonymousUserSession; -use Drupal\Core\Session\AccountSwitcherInterface; +use Drupal\Core\Queue\QueueWorkerManagerInterface; +use Drupal\Core\Queue\RequeueException; use Drupal\Core\Queue\SuspendQueueException; +use Drupal\Core\Session\AccountSwitcherInterface; +use Drupal\Core\Session\AnonymousUserSession; +use Drupal\Core\State\StateInterface; use Psr\Log\LoggerInterface; use Psr\Log\NullLogger; @@ -120,7 +121,7 @@ public function run() { $this->accountSwitcher->switchTo(new AnonymousUserSession()); // Try to allocate enough time to run all the hook_cron implementations. - drupal_set_time_limit(240); + Environment::setTimeLimit(240); $return = FALSE; diff --git a/core/lib/Drupal/Core/Form/FormBuilder.php b/core/lib/Drupal/Core/Form/FormBuilder.php index d64c1df5b8b5..df716e3f8877 100644 --- a/core/lib/Drupal/Core/Form/FormBuilder.php +++ b/core/lib/Drupal/Core/Form/FormBuilder.php @@ -3,6 +3,7 @@ namespace Drupal\Core\Form; use Drupal\Component\Utility\Crypt; +use Drupal\Component\Utility\Environment; use Drupal\Component\Utility\Html; use Drupal\Component\Utility\NestedArray; use Drupal\Component\Utility\UrlHelper; @@ -1390,7 +1391,7 @@ protected function buttonWasClicked($element, FormStateInterface &$form_state) { * based on the PHP upload_max_filesize and post_max_size. */ protected function getFileUploadMaxSize() { - return file_upload_max_size(); + return Environment::getUploadMaxSize(); } /** diff --git a/core/lib/Drupal/Core/Test/FunctionalTestSetupTrait.php b/core/lib/Drupal/Core/Test/FunctionalTestSetupTrait.php index 0ec3388fec18..6b4ce17693e6 100644 --- a/core/lib/Drupal/Core/Test/FunctionalTestSetupTrait.php +++ b/core/lib/Drupal/Core/Test/FunctionalTestSetupTrait.php @@ -4,6 +4,7 @@ use Drupal\Component\FileCache\FileCacheFactory; use Drupal\Component\Render\FormattableMarkup; +use Drupal\Component\Utility\Environment; use Drupal\Core\Config\Development\ConfigSchemaChecker; use Drupal\Core\Database\Database; use Drupal\Core\DrupalKernel; @@ -620,7 +621,7 @@ protected function prepareEnvironment() { 'hash_salt' => $this->databasePrefix, ]); - drupal_set_time_limit($this->timeLimit); + Environment::setTimeLimit($this->timeLimit); // Save and clean the shutdown callbacks array because it is static cached // and will be changed by the test run. Otherwise it will contain callbacks diff --git a/core/modules/editor/editor.admin.inc b/core/modules/editor/editor.admin.inc index f91db4b9619d..969810ed6abb 100644 --- a/core/modules/editor/editor.admin.inc +++ b/core/modules/editor/editor.admin.inc @@ -5,6 +5,7 @@ * Administration functions for editor.module. */ +use Drupal\Component\Utility\Environment; use Drupal\Core\StreamWrapper\StreamWrapperInterface; use Drupal\editor\Entity\Editor; @@ -77,7 +78,7 @@ function editor_image_upload_settings_form(Editor $editor) { '#states' => $show_if_image_uploads_enabled, ]; - $default_max_size = format_size(file_upload_max_size()); + $default_max_size = format_size(Environment::getUploadMaxSize()); $form['max_size'] = [ '#type' => 'textfield', '#default_value' => $image_upload['max_size'], diff --git a/core/modules/editor/src/Form/EditorImageDialog.php b/core/modules/editor/src/Form/EditorImageDialog.php index 4e799302fa46..b31055e39f09 100644 --- a/core/modules/editor/src/Form/EditorImageDialog.php +++ b/core/modules/editor/src/Form/EditorImageDialog.php @@ -3,6 +3,7 @@ namespace Drupal\editor\Form; use Drupal\Component\Utility\Bytes; +use Drupal\Component\Utility\Environment; use Drupal\Core\Form\FormBase; use Drupal\Core\Form\FormStateInterface; use Drupal\editor\Entity\Editor; @@ -91,8 +92,7 @@ public function buildForm(array $form, FormStateInterface $form_state, Editor $e else { $max_dimensions = 0; } - $max_filesize = min(Bytes::toInt($image_upload['max_size']), file_upload_max_size()); - + $max_filesize = min(Bytes::toInt($image_upload['max_size']), Environment::getUploadMaxSize()); $existing_file = isset($image_element['data-entity-uuid']) ? \Drupal::service('entity.repository')->loadEntityByUuid('file', $image_element['data-entity-uuid']) : NULL; $fid = $existing_file ? $existing_file->id() : NULL; diff --git a/core/modules/file/src/Plugin/Field/FieldType/FileItem.php b/core/modules/file/src/Plugin/Field/FieldType/FileItem.php index 202a96682b15..fe4b4a93261e 100644 --- a/core/modules/file/src/Plugin/Field/FieldType/FileItem.php +++ b/core/modules/file/src/Plugin/Field/FieldType/FileItem.php @@ -4,6 +4,7 @@ use Drupal\Component\Utility\Bytes; use Drupal\Component\Render\PlainTextOutput; +use Drupal\Component\Utility\Environment; use Drupal\Component\Utility\Random; use Drupal\Core\Field\FieldDefinitionInterface; use Drupal\Core\Field\FieldStorageDefinitionInterface; @@ -178,7 +179,7 @@ public function fieldSettingsForm(array $form, FormStateInterface $form_state) { '#type' => 'textfield', '#title' => t('Maximum upload size'), '#default_value' => $settings['max_filesize'], - '#description' => t('Enter a value like "512" (bytes), "80 KB" (kilobytes) or "50 MB" (megabytes) in order to restrict the allowed file size. If left empty the file sizes will be limited only by PHP\'s maximum post and file upload sizes (current limit <strong>%limit</strong>).', ['%limit' => format_size(file_upload_max_size())]), + '#description' => t('Enter a value like "512" (bytes), "80 KB" (kilobytes) or "50 MB" (megabytes) in order to restrict the allowed file size. If left empty the file sizes will be limited only by PHP\'s maximum post and file upload sizes (current limit <strong>%limit</strong>).', ['%limit' => format_size(Environment::getUploadMaxSize())]), '#size' => 10, '#element_validate' => [[get_class($this), 'validateMaxFilesize']], '#weight' => 5, @@ -301,7 +302,7 @@ public function getUploadValidators() { $settings = $this->getSettings(); // Cap the upload size according to the PHP limit. - $max_filesize = Bytes::toInt(file_upload_max_size()); + $max_filesize = Bytes::toInt(Environment::getUploadMaxSize()); if (!empty($settings['max_filesize'])) { $max_filesize = min($max_filesize, Bytes::toInt($settings['max_filesize'])); } diff --git a/core/modules/file/src/Plugin/rest/resource/FileUploadResource.php b/core/modules/file/src/Plugin/rest/resource/FileUploadResource.php index 3b1f5f321618..0cb21a44bf0b 100644 --- a/core/modules/file/src/Plugin/rest/resource/FileUploadResource.php +++ b/core/modules/file/src/Plugin/rest/resource/FileUploadResource.php @@ -4,6 +4,7 @@ use Drupal\Component\Utility\Bytes; use Drupal\Component\Utility\Crypt; +use Drupal\Component\Utility\Environment; use Drupal\Core\Config\Config; use Drupal\Core\Entity\EntityTypeManagerInterface; use Drupal\Core\Field\FieldDefinitionInterface; @@ -527,7 +528,7 @@ protected function getUploadValidators(FieldDefinitionInterface $field_definitio $settings = $field_definition->getSettings(); // Cap the upload size according to the PHP limit. - $max_filesize = Bytes::toInt(file_upload_max_size()); + $max_filesize = Bytes::toInt(Environment::getUploadMaxSize()); if (!empty($settings['max_filesize'])) { $max_filesize = min($max_filesize, Bytes::toInt($settings['max_filesize'])); } diff --git a/core/modules/locale/src/Form/ImportForm.php b/core/modules/locale/src/Form/ImportForm.php index 122093ae69d2..1d20e5ef5ea4 100644 --- a/core/modules/locale/src/Form/ImportForm.php +++ b/core/modules/locale/src/Form/ImportForm.php @@ -2,6 +2,7 @@ namespace Drupal\locale\Form; +use Drupal\Component\Utility\Environment; use Drupal\Core\Form\FormBase; use Drupal\Core\Extension\ModuleHandlerInterface; use Drupal\Core\Form\FormStateInterface; @@ -99,7 +100,7 @@ public function buildForm(array $form, FormStateInterface $form_state) { $validators = [ 'file_validate_extensions' => ['po'], - 'file_validate_size' => [file_upload_max_size()], + 'file_validate_size' => [Environment::getUploadMaxSize()], ]; $form['file'] = [ '#type' => 'file', diff --git a/core/modules/node/node.module b/core/modules/node/node.module index 6fcaf2a6f29c..58f79f7ec48b 100644 --- a/core/modules/node/node.module +++ b/core/modules/node/node.module @@ -8,6 +8,7 @@ * API pattern. */ +use Drupal\Component\Utility\Environment; use Drupal\Component\Utility\Xss; use Drupal\Core\Access\AccessResult; use Drupal\Core\Cache\Cache; @@ -1232,7 +1233,7 @@ function node_access_rebuild($batch_mode = FALSE) { } else { // Try to allocate enough time to rebuild node grants - drupal_set_time_limit(240); + Environment::setTimeLimit(240); // Rebuild newest nodes first so that recent content becomes available // quickly. diff --git a/core/modules/simpletest/src/TestBase.php b/core/modules/simpletest/src/TestBase.php index b47d2d7e850e..9af30b70e211 100644 --- a/core/modules/simpletest/src/TestBase.php +++ b/core/modules/simpletest/src/TestBase.php @@ -3,9 +3,10 @@ namespace Drupal\simpletest; use Drupal\Component\Assertion\Handle; +use Drupal\Component\Render\FormattableMarkup; use Drupal\Component\Render\MarkupInterface; use Drupal\Component\Utility\Crypt; -use Drupal\Component\Render\FormattableMarkup; +use Drupal\Component\Utility\Environment; use Drupal\Core\Database\Database; use Drupal\Core\File\FileSystemInterface; use Drupal\Core\Site\Settings; @@ -1178,7 +1179,7 @@ private function prepareEnvironment() { 'container_yamls' => [], ]); - drupal_set_time_limit($this->timeLimit); + Environment::setTimeLimit($this->timeLimit); } /** diff --git a/core/tests/Drupal/KernelTests/Core/Common/LegacyFunctionsTest.php b/core/tests/Drupal/KernelTests/Core/Common/LegacyFunctionsTest.php index 8e1d1acbb206..b9bc2f709c7e 100644 --- a/core/tests/Drupal/KernelTests/Core/Common/LegacyFunctionsTest.php +++ b/core/tests/Drupal/KernelTests/Core/Common/LegacyFunctionsTest.php @@ -5,7 +5,7 @@ use Drupal\KernelTests\KernelTestBase; /** - * Tests legacy functions in common.inc + * Tests legacy functions in common.inc. * * @group Common * @group legacy @@ -23,4 +23,11 @@ public function testFormatDate() { $this->assertEquals('1970-01-01', $date); } + /** + * @expectedDeprecation drupal_set_time_limit() is deprecated in Drupal 8.7.0 and will be removed before Drupal 9.0.0. Use \Drupal\Core\Environment::setTimeLimit() instead. See https://www.drupal.org/node/3000058. + */ + public function testDrupalSetTimeLimit() { + drupal_set_time_limit(1000); + } + } diff --git a/core/tests/Drupal/KernelTests/Core/File/FileSystemDeprecationTest.php b/core/tests/Drupal/KernelTests/Core/File/FileSystemDeprecationTest.php index 1e434c424676..5f2fc570cdab 100644 --- a/core/tests/Drupal/KernelTests/Core/File/FileSystemDeprecationTest.php +++ b/core/tests/Drupal/KernelTests/Core/File/FileSystemDeprecationTest.php @@ -90,4 +90,11 @@ public function testDeprecatedFileCreate() { $this->assertNotNull(file_create_filename('', '')); } + /** + * @expectedDeprecation file_upload_max_size() is deprecated in Drupal 8.7.0 and will be removed before Drupal 9.0.0. Use \Drupal\Component\Utility\Environment::getUploadMaxSize() instead. See https://www.drupal.org/node/3000058. + */ + public function testDeprecatedFileUploadMaxSize() { + $this->assertNotNull(file_upload_max_size()); + } + } -- GitLab