Ticket #32075: 32075-improved-patch-v5.patch
File 32075-improved-patch-v5.patch, 12.3 KB (added by , 9 years ago) |
---|
-
src/wp-admin/admin.php
From 627a0fe4f94e3c5554a9d10f47a14c45b4de2c74 Mon Sep 17 00:00:00 2001 From: jrfnl <github_nospam@adviesenzo.nl> Date: Sat, 13 Feb 2016 15:46:04 +0100 Subject: [PATCH] Prevent WP setting the memory limit to a value lower than it currently is. Also fixes a bug in how the memory limits were tested in the first place. --- src/wp-admin/admin.php | 16 +----- src/wp-admin/includes/file.php | 3 +- src/wp-admin/includes/image-edit.php | 3 +- src/wp-includes/class-wp-image-editor-gd.php | 10 +--- src/wp-includes/class-wp-image-editor-imagick.php | 3 +- src/wp-includes/default-constants.php | 31 +++++----- src/wp-includes/deprecated.php | 3 +- src/wp-includes/functions.php | 70 +++++++++++++++++++++++ src/wp-includes/load.php | 28 +++++++++ tests/phpunit/tests/functions.php | 16 ++++++ 10 files changed, 136 insertions(+), 47 deletions(-) diff --git a/src/wp-admin/admin.php b/src/wp-admin/admin.php index d67160d..dd30e20 100644
a b else 138 138 require(ABSPATH . 'wp-admin/menu.php'); 139 139 140 140 if ( current_user_can( 'manage_options' ) ) { 141 /** 142 * Filter the maximum memory limit available for administration screens. 143 * 144 * This only applies to administrators, who may require more memory for tasks like updates. 145 * Memory limits when processing images (uploaded or edited by users of any role) are 146 * handled separately. 147 * 148 * The WP_MAX_MEMORY_LIMIT constant specifically defines the maximum memory limit available 149 * when in the administration back-end. The default is 256M, or 256 megabytes of memory. 150 * 151 * @since 3.0.0 152 * 153 * @param string 'WP_MAX_MEMORY_LIMIT' The maximum WordPress memory limit. Default 256M. 154 */ 155 @ini_set( 'memory_limit', apply_filters( 'admin_memory_limit', WP_MAX_MEMORY_LIMIT ) ); 141 wp_raise_memory_limit( 'admin' ); 156 142 } 157 143 158 144 /** -
src/wp-admin/includes/file.php
diff --git a/src/wp-admin/includes/file.php b/src/wp-admin/includes/file.php index cb85c05..68a38cf 100644
a b function unzip_file($file, $to) { 558 558 return new WP_Error('fs_unavailable', __('Could not access filesystem.')); 559 559 560 560 // Unzip can use a lot of memory, but not this much hopefully 561 /** This filter is documented in wp-admin/admin.php */ 562 @ini_set( 'memory_limit', apply_filters( 'admin_memory_limit', WP_MAX_MEMORY_LIMIT ) ); 561 wp_raise_memory_limit( 'admin' ); 563 562 564 563 $needed_dirs = array(); 565 564 $to = trailingslashit($to); -
src/wp-admin/includes/image-edit.php
diff --git a/src/wp-admin/includes/image-edit.php b/src/wp-admin/includes/image-edit.php index 8947f53..a954d24 100644
a b function image_edit_apply_changes( $image, $changes ) { 586 586 function stream_preview_image( $post_id ) { 587 587 $post = get_post( $post_id ); 588 588 589 /** This filter is documented in wp-admin/admin.php */ 590 @ini_set( 'memory_limit', apply_filters( 'admin_memory_limit', WP_MAX_MEMORY_LIMIT ) ); 589 wp_raise_memory_limit( 'admin' ); 591 590 592 591 $img = wp_get_image_editor( _load_image_to_edit_path( $post_id ) ); 593 592 -
src/wp-includes/class-wp-image-editor-gd.php
diff --git a/src/wp-includes/class-wp-image-editor-gd.php b/src/wp-includes/class-wp-image-editor-gd.php index 2093c6b..9cb756d 100644
a b class WP_Image_Editor_GD extends WP_Image_Editor { 96 96 if ( ! is_file( $this->file ) && ! preg_match( '|^https?://|', $this->file ) ) 97 97 return new WP_Error( 'error_loading_image', __('File doesn’t exist?'), $this->file ); 98 98 99 /**100 * Filter the memory limit allocated for image manipulation.101 *102 * @since 3.5.0103 *104 * @param int|string $limit Maximum memory limit to allocate for images. Default WP_MAX_MEMORY_LIMIT.105 * Accepts an integer (bytes), or a shorthand string notation, such as '256M'.106 */107 99 // Set artificially high because GD uses uncompressed images in memory 108 @ini_set( 'memory_limit', apply_filters( 'image_memory_limit', WP_MAX_MEMORY_LIMIT ));100 wp_raise_memory_limit( 'image' ); 109 101 110 102 $this->image = @imagecreatefromstring( file_get_contents( $this->file ) ); 111 103 -
src/wp-includes/class-wp-image-editor-imagick.php
diff --git a/src/wp-includes/class-wp-image-editor-imagick.php b/src/wp-includes/class-wp-image-editor-imagick.php index a14fa40..1888316 100644
a b class WP_Image_Editor_Imagick extends WP_Image_Editor { 129 129 if ( ! is_file( $this->file ) && ! preg_match( '|^https?://|', $this->file ) ) 130 130 return new WP_Error( 'error_loading_image', __('File doesn’t exist?'), $this->file ); 131 131 132 /** This filter is documented in wp-includes/class-wp-image-editor-imagick.php */133 132 // Even though Imagick uses less PHP memory than GD, set higher limit for users that have low PHP.ini limits 134 @ini_set( 'memory_limit', apply_filters( 'image_memory_limit', WP_MAX_MEMORY_LIMIT ));133 wp_raise_memory_limit( 'image' ); 135 134 136 135 try { 137 136 $this->image = new Imagick( $this->file ); -
src/wp-includes/default-constants.php
diff --git a/src/wp-includes/default-constants.php b/src/wp-includes/default-constants.php index c9092bd..9b45fb7 100644
a b 17 17 function wp_initial_constants() { 18 18 global $blog_id; 19 19 20 // set memory limits20 // Define memory limits. 21 21 if ( !defined('WP_MEMORY_LIMIT') ) { 22 22 if ( is_multisite() ) { 23 23 define('WP_MEMORY_LIMIT', '64M'); … … function wp_initial_constants() { 26 26 } 27 27 } 28 28 29 $current_limit = @ini_get( 'memory_limit' ); 30 $current_limit_int = wp_php_ini_bytes_to_int( $current_limit ); 31 29 32 if ( ! defined( 'WP_MAX_MEMORY_LIMIT' ) ) { 30 define( 'WP_MAX_MEMORY_LIMIT', '256M' ); 33 if ( -1 === $current_limit_int || $current_limit_int > 268435456 ) { 34 define( 'WP_MAX_MEMORY_LIMIT', $current_limit ); 35 } else { 36 define( 'WP_MAX_MEMORY_LIMIT', '256M' ); 37 } 38 } 39 40 // Set memory limits. 41 $wp_limit_int = wp_php_ini_bytes_to_int( WP_MEMORY_LIMIT ); 42 if ( -1 !== $current_limit_int && ( -1 === $wp_limit_int || $wp_limit_int > $current_limit_int ) ) { 43 @ini_set( 'memory_limit', WP_MEMORY_LIMIT ); 31 44 } 32 45 33 46 if ( ! isset($blog_id) ) 34 47 $blog_id = 1; 35 48 36 // set memory limits.37 if ( function_exists( 'memory_get_usage' ) ) {38 $current_limit = @ini_get( 'memory_limit' );39 $current_limit_int = intval( $current_limit );40 if ( false !== strpos( $current_limit, 'G' ) )41 $current_limit_int *= 1024;42 $wp_limit_int = intval( WP_MEMORY_LIMIT );43 if ( false !== strpos( WP_MEMORY_LIMIT, 'G' ) )44 $wp_limit_int *= 1024;45 46 if ( -1 != $current_limit && ( -1 == WP_MEMORY_LIMIT || $current_limit_int < $wp_limit_int ) )47 @ini_set( 'memory_limit', WP_MEMORY_LIMIT );48 }49 50 49 if ( !defined('WP_CONTENT_DIR') ) 51 50 define( 'WP_CONTENT_DIR', ABSPATH . 'wp-content' ); // no trailing slash, full paths only - WP_CONTENT_URL is defined further down 52 51 -
src/wp-includes/deprecated.php
diff --git a/src/wp-includes/deprecated.php b/src/wp-includes/deprecated.php index 202ad84..e2d8692 100644
a b function wp_load_image( $file ) { 3162 3162 return __('The GD image library is not installed.'); 3163 3163 3164 3164 // Set artificially high because GD uses uncompressed images in memory 3165 @ini_set( 'memory_limit', apply_filters( 'image_memory_limit', WP_MAX_MEMORY_LIMIT ) ); 3165 wp_raise_memory_limit( 'image' ); 3166 3166 3167 $image = imagecreatefromstring( file_get_contents( $file ) ); 3167 3168 3168 3169 if ( !is_resource( $image ) ) -
src/wp-includes/functions.php
diff --git a/src/wp-includes/functions.php b/src/wp-includes/functions.php index 3e9b792..68b908e 100644
a b function mysql_to_rfc3339( $date_string ) { 5206 5206 // Strip timezone information 5207 5207 return preg_replace( '/(?:Z|[+-]\d{2}(?::\d{2})?)$/', '', $formatted ); 5208 5208 } 5209 5210 /** 5211 * WP raise memory limit. 5212 * 5213 * Raises the PHP memory limit for memory intensive processes. 5214 * Only allows filter to raise and not lower the exciting limit. 5215 * 5216 * @since 4.5.0 5217 * 5218 * @param string $context Context in which the function is called. 5219 * Either 'admin' or 'image'. Defaults to 'admin'. 5220 */ 5221 function wp_raise_memory_limit( $context = 'admin' ) { 5222 $current_limit = @ini_get( 'memory_limit' ); 5223 $current_limit_int = wp_php_ini_bytes_to_int( $current_limit ); 5224 5225 if ( -1 === $current_limit_int ) { 5226 return; 5227 } 5228 5229 $wp_max_limit = WP_MAX_MEMORY_LIMIT; 5230 $wp_max_limit_int = wp_php_ini_bytes_to_int( $wp_max_limit ); 5231 $filtered_limit = $wp_max_limit; 5232 5233 switch ( $context ) { 5234 case 'admin': 5235 /** 5236 * Filter the maximum memory limit available for administration screens. 5237 * 5238 * This only applies to administrators, who may require more memory for tasks like updates. 5239 * Memory limits when processing images (uploaded or edited by users of any role) are 5240 * handled separately. 5241 * 5242 * The WP_MAX_MEMORY_LIMIT constant specifically defines the maximum memory limit available 5243 * when in the administration back-end. The default is 256M (256 megabytes 5244 * of memory) or the original `memory_limit` php.ini value if this is higher. 5245 * 5246 * @since 3.0.0 5247 * 5248 * @param int|string $filtered_limit The maximum WordPress memory limit. 5249 * Accepts an integer (bytes), or a shorthand string 5250 * notation, such as '256M'. 5251 */ 5252 $filtered_limit = apply_filters( 'admin_memory_limit', $filtered_limit ); 5253 break; 5254 5255 case 'image': 5256 /** 5257 * Filter the memory limit allocated for image manipulation. 5258 * 5259 * @since 3.5.0 5260 * 5261 * @param int|string $filtered_limit Maximum memory limit to allocate for images. 5262 * Default 256M or the original php.ini memory_limit, 5263 * whichever is higher. 5264 * Accepts an integer (bytes), or a shorthand string 5265 * notation, such as '256M'. 5266 */ 5267 $filtered_limit = apply_filters( 'image_memory_limit', $filtered_limit ); 5268 break; 5269 } 5270 5271 $filtered_limit_int = wp_php_ini_bytes_to_int( $filtered_limit ); 5272 5273 if ( -1 === $filtered_limit_int || ( $filtered_limit_int > $wp_max_limit_int && $filtered_limit_int > $current_limit_int ) ) { 5274 @ini_set( 'memory_limit', $filtered_limit ); 5275 } elseif ( -1 === $wp_max_limit_int || $wp_max_limit_int > $current_limit_int ) { 5276 @ini_set( 'memory_limit', $wp_max_limit ); 5277 } 5278 } -
src/wp-includes/load.php
diff --git a/src/wp-includes/load.php b/src/wp-includes/load.php index ec59459..851dbd8 100644
a b function wp_installing( $is_installing = null ) { 893 893 894 894 return (bool) $installing; 895 895 } 896 897 /** 898 * Convert a PHP ini shorthand byte value to an integer byte value. 899 * 900 * @since 4.5.0 901 * 902 * @see http://php.net/manual/en/function.ini-get.php 903 * @see http://php.net/manual/en/faq.using.php#faq.using.shorthandbytes 904 * 905 * @param string $value An PHP ini byte value, either shorthand or ordinary. 906 * @return int Value in bytes. 907 */ 908 function wp_php_ini_bytes_to_int( $value ) { 909 $value = trim( $value ); 910 $last = strtolower( $value[ strlen( $value ) - 1 ] ); 911 912 switch( $last ) { 913 // Note: the `break` statement is left out on purpose! 914 case 'g': 915 $value *= 1024; 916 case 'm': 917 $value *= 1024; 918 case 'k': 919 $value *= 1024; 920 } 921 922 return (int) $value; 923 } -
tests/phpunit/tests/functions.php
diff --git a/tests/phpunit/tests/functions.php b/tests/phpunit/tests/functions.php index 631b303..f0643fb 100644
a b class Tests_Functions extends WP_UnitTestCase { 717 717 the_date( 'Y', 'before ', ' after', false ); 718 718 $this->assertEquals( '', ob_get_clean() ); 719 719 } 720 721 /** 722 * Test raising the memory limit. 723 * 724 * {@internal Unfortunately as the default for 'WP_MAX_MEMORY_LIMIT' in the 725 * test suite is -1, we can not test the memory limit negotiations.}} 726 * 727 * @ticket 32075 728 */ 729 function test_wp_raise_memory_limit() { 730 $original = ini_get( 'memory_limit' ); 731 732 ini_set( 'memory_limit', '40M' ); 733 wp_raise_memory_limit(); 734 $this->assertEquals( '-1', ini_get( 'memory_limit' ) ); 735 } 720 736 }