Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve CURLFile serialization in logs #43

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions classes/class-imagify-tools.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,9 @@ public function load_plugin() {
}

// Load files.
if ( file_exists( IMAGIFY_TOOLS_PATH . 'vendor/autoload.php' ) ) {
require_once IMAGIFY_TOOLS_PATH . 'vendor/autoload.php';
}
include_once IMAGIFY_TOOLS_FUNCTIONS_PATH . 'compat.php';
include_once IMAGIFY_TOOLS_FUNCTIONS_PATH . 'common.php';

Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
"name": "wpmedia/imagify-debug-tool-plugin",
"description": "Debug plugin for Imagify plugin.",
"require": {
"php": ">=5.2.0"
"php": ">=5.3.0",
"gisostallenberg/curlfile-serializer": "*"
},
"require-dev": {
"php": ">=7.0.0",
Expand Down
99 changes: 75 additions & 24 deletions functions/common.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
<?php
defined( 'ABSPATH' ) || die( 'Cheatin’ uh?' );

use GisoStallenberg\CURLFileSerializer\CURLFileSerializer;

defined( 'ABSPATH' ) || exit;

/**
* Check if Imagify Tools is activated on the network.
Expand Down Expand Up @@ -95,11 +98,46 @@ function imagify_tools_compress_data( $data ) {
$bsf = '64_' . $bsf;
$bsf = 'base' . $bsf;

// phpcs:disable PEAR.Functions.FunctionCallSignature.Indent, PEAR.Functions.FunctionCallSignature.SpaceBeforeOpenBracket, PEAR.Functions.FunctionCallSignature.ContentAfterOpenBracket, PEAR.Functions.FunctionCallSignature.CloseBracketLine, WordPress.PHP.DiscouragedPHPFunctions.serialize_serialize
// phpcs:disable PEAR.Functions.FunctionCallSignature.Indent, PEAR.Functions.FunctionCallSignature.SpaceBeforeOpenBracket, PEAR.Functions.FunctionCallSignature.ContentAfterOpenBracket, PEAR.Functions.FunctionCallSignature.CloseBracketLine
return $bsf
( $gz
( serialize( $data ) ) );
// phpcs:enable PEAR.Functions.FunctionCallSignature.Indent, PEAR.Functions.FunctionCallSignature.SpaceBeforeOpenBracket, PEAR.Functions.FunctionCallSignature.ContentAfterOpenBracket, PEAR.Functions.FunctionCallSignature.CloseBracketLine, WordPress.PHP.DiscouragedPHPFunctions.serialize_serialize
( imagify_tools_serialize_data( $data ) ) );
// phpcs:enable PEAR.Functions.FunctionCallSignature.Indent, PEAR.Functions.FunctionCallSignature.SpaceBeforeOpenBracket, PEAR.Functions.FunctionCallSignature.ContentAfterOpenBracket, PEAR.Functions.FunctionCallSignature.CloseBracketLine
}


/**
* Serialize some data.
*
* @since 1.1.2
*
* @param mixed $data The data to serialize.
* @return string
*/
function imagify_tools_serialize_data( $data ) {
return serialize( imagify_tools_serialize_curlfile( $data ) ); // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.serialize_serialize
}


/**
* Serialize a CURLFile instance.
* If the provided data is an array or an object, the data is searched.
*
* @since 1.1.2
*
* @param mixed $data The data to serialize.
* @return mixed
*/
function imagify_tools_serialize_curlfile( $data ) {
if ( $data instanceof CURLFile ) {
return CURLFileSerializer::create( $data );
}
if ( is_array( $data ) || is_object( $data ) ) {
foreach ( $data as &$value ) {
$value = imagify_tools_serialize_curlfile( $value );
}
}
return $data;
}


Expand All @@ -114,8 +152,6 @@ function imagify_tools_compress_data( $data ) {
* @return mixed The decompressed data.
*/
function imagify_tools_decompress_data( $data ) {
static $object_names, $prefix, $prefix_len;

if ( ! $data || ! is_string( $data ) ) {
return $data;
}
Expand All @@ -142,27 +178,42 @@ function imagify_tools_decompress_data( $data ) {
return $data;
}

if ( ! isset( $object_names ) ) {
// Some serialized objects must not be unserialized, it would trigger a fatal error.
$object_names = array(
'CURLFile',
);

if ( $object_names ) {
$prefix = 'IMGT_Not_Unserialized_';
$prefix_len = strlen( $prefix );
$object_names = array_combine( $object_names, $object_names );
$object_names = array_map( 'strlen', $object_names );
}
}
return imagify_tools_unserialize_data( $data_tmp );
}

if ( $object_names ) {
foreach ( $object_names as $object_name => $object_name_len ) {
$data_tmp = preg_replace( '@O:' . $object_name_len . ':"' . $object_name . '":(\d+):{@', 'O:' . ( $prefix_len + $object_name_len ) . ':"' . $prefix . $object_name . '":$1:{', $data_tmp );

/**
* Unerialize some data.
*
* @since 1.1.2
*
* @param string $data The data to unserialize.
* @return mixed
*/
function imagify_tools_unserialize_data( $data ) {
return imagify_tools_unserialize_curlfile( maybe_unserialize( $data ) );
}


/**
* Unerialize a CURLFile instance.
* If the provided data is an array or an object, the data is searched.
*
* @since 1.1.2
*
* @param mixed $data The data to unserialize.
* @return mixed
*/
function imagify_tools_unserialize_curlfile( $data ) {
if ( $data instanceof CURLFileSerializer ) {
return $data->getCURLFile();
}
if ( is_array( $data ) || is_object( $data ) ) {
foreach ( $data as &$value ) {
$value = imagify_tools_unserialize_curlfile( $value );
}
}

return maybe_unserialize( $data_tmp );
return $data;
}

/**
Expand Down
5 changes: 3 additions & 2 deletions imagify-tools.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@
* Plugin URI: https://wordpress.org/plugins/imagify/
* Description: A WordPress plugin helping debug in Imagify.
* Version: 1.1.1
* Requires PHP: 5.3
* Author: WP Media
* Author URI: https://wp-media.me/
* Licence: GPLv2
*
* Text Domain: imagify-tools
* Domain Path: languages
*
* Copyright 2019 WP Media
* Copyright 2020 WP Media
*
* @package WP-Media\Imagify\WordPress-Tools-Plugin
*/
Expand All @@ -22,7 +23,7 @@
* Plugin init.
*/
function imagify_tools_init() {
if ( ! function_exists( 'filter_var' ) || ! function_exists( 'filter_input' ) ) {
if ( ! function_exists( 'filter_var' ) || ! function_exists( 'filter_input' ) || version_compare( PHP_VERSION, '5.3' ) < 0 ) {
return;
}

Expand Down
2 changes: 1 addition & 1 deletion phpcs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<exclude-pattern>tests.php</exclude-pattern>

<!-- Check for cross-version support for PHP 5.2 and higher + WP 3.7 and higher. -->
<config name="testVersion" value="5.2-"/>
<config name="testVersion" value="5.3-"/>
<config name="minimum_supported_wp_version" value="3.7"/>

<!-- Run against the PHPCompatibility ruleset dedicated to WP. -->
Expand Down