Make WordPress Core

Ticket #23127: 23127.patch

File 23127.patch, 4.4 KB (added by gmariani405, 7 years ago)

Returns error message on fatal error, displays error message on fatal error if applicable

  • src/wp-includes/class-wp-image-editor-gd.php

     
    7878        }
    7979
    8080        /**
     81         * Catches fatal PHP errors that aren't normally relayed to the user
     82         */
     83        public function catch_fatal_errors() {
     84                header('Status: 500 Internal Server Error');
     85                $error = error_get_last();
     86                echo $error['message'];
     87        }
     88       
     89        /**
    8190         * Loads image from $this->file into new GD Resource.
    8291         *
    8392         * @since 3.5.0
     
    93102
    94103                // Set artificially high because GD uses uncompressed images in memory.
    95104                wp_raise_memory_limit( 'image' );
    96 
    97                 $this->image = @imagecreatefromstring( file_get_contents( $this->file ) );
    98 
     105               
     106                add_action( 'shutdown', array( $this,'catch_fatal_errors' ) );
     107               
     108                $size = getimagesize( $this->file );
     109               
     110                if ( ! $size )
     111                        return new WP_Error( 'invalid_image', __('Could not read image size.'), $this->file );
     112               
     113                // See if image dimensions will exhaust memory and avoid a fatal error
     114                // https://core.trac.wordpress.org/ticket/23127#comment:1
     115                $mem_usage  = memory_get_usage();
     116                $mem_limit = ini_get('memory_limit');
     117                switch ( substr($mem_limit, -1) ) {
     118                        case 'G': case 'g': $mem_limit = (int) $mem_limit * 1073741824;break;
     119                        case 'M': case 'm': $mem_limit = (int) $mem_limit * 1048576; break;
     120                        case 'K': case 'k': $mem_limit = (int) $mem_limit * 1024; break;
     121                        default: $mem_limit = (int) $mem_limit;
     122                }
     123                $est_mem_usage = $size[0] * $size[1] * $size['bits'] * $size['channels'] / 8 * 1.65;
     124                if ( ( $mem_limit - $mem_usage ) < $est_mem_usage )
     125                        return new WP_Error( 'image_memory_exceeded', __('Memory estimated to be exceeded. Please try another smaller file.'), $this->file );
     126               
     127                $this->image = imagecreatefromstring( file_get_contents( $this->file ) );
     128               
    99129                if ( ! is_resource( $this->image ) )
    100130                        return new WP_Error( 'invalid_image', __('File is not an image.'), $this->file );
    101131
    102                 $size = @getimagesize( $this->file );
    103                 if ( ! $size )
    104                         return new WP_Error( 'invalid_image', __('Could not read image size.'), $this->file );
    105 
    106132                if ( function_exists( 'imagealphablending' ) && function_exists( 'imagesavealpha' ) ) {
    107133                        imagealphablending( $this->image, false );
    108134                        imagesavealpha( $this->image, true );
     
    110136
    111137                $this->update_size( $size[0], $size[1] );
    112138                $this->mime_type = $size['mime'];
    113 
     139               
     140                remove_action( 'shutdown', array( $this,'catch_fatal_errors' ) );
     141               
    114142                return $this->set_quality();
    115143        }
    116144
  • src/wp-admin/includes/image.php

     
    131131                if ( $sizes ) {
    132132                        $editor = wp_get_image_editor( $file );
    133133
    134                         if ( ! is_wp_error( $editor ) )
     134                        if ( ! is_wp_error( $editor ) ) {
    135135                                $metadata['sizes'] = $editor->multi_resize( $sizes );
     136                        } else {
     137                                $metadata['sizes'] = array();
     138                        }
    136139                } else {
    137140                        $metadata['sizes'] = array();
    138141                }
  • src/wp-includes/js/plupload/plupload.js

     
    13801380                        } else {
    13811381                                file.loaded = offset; // reset all progress
    13821382
    1383                                 up.trigger('Error', {
    1384                                         code : plupload.HTTP_ERROR,
    1385                                         message : plupload.translate('HTTP Error.'),
    1386                                         file : file,
    1387                                         response : xhr.responseText,
    1388                                         status : xhr.status,
    1389                                         responseHeaders: xhr.getAllResponseHeaders()
    1390                                 });
     1383                                if ( xhr.responseText && xhr.responseText.indexOf('memory') > 0 && xhr.responseText.indexOf('exhausted') > 0 ) {
     1384                                        up.trigger('Error', {
     1385                                                code : plupload.IMAGE_MEMORY_ERROR,
     1386                                                message : plupload.translate('Memory exceeded. Please try another smaller file.'),
     1387                                                file : file,
     1388                                                response : xhr.responseText,
     1389                                                status : xhr.status,
     1390                                                responseHeaders: xhr.getAllResponseHeaders()
     1391                                        });
     1392                                } else {
     1393                                        up.trigger('Error', {
     1394                                                code : plupload.HTTP_ERROR,
     1395                                                message : plupload.translate('HTTP Error.'),
     1396                                                file : file,
     1397                                                response : xhr.responseText,
     1398                                                status : xhr.status,
     1399                                                responseHeaders: xhr.getAllResponseHeaders()
     1400                                        });
     1401                                }
    13911402                        }
    13921403                }
    13931404