Skip to content

Commit 57ed323

Browse files
authored
Release version 4.0.2 (#88)
Merge pull request #88 from short-pixel-optimizer/updates
2 parents d7a144f + f40e690 commit 57ed323

File tree

6 files changed

+67
-27
lines changed

6 files changed

+67
-27
lines changed

classes/emr-plugin.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,12 @@ public function uiHelper()
6969

7070
public function useFeature($name)
7171
{
72+
// If for some obscure reason, it's called earlier or out of admin, still load the features.
73+
if (count($this->features) === 0)
74+
{
75+
$this->adminInit();
76+
}
77+
7278
switch($name)
7379
{
7480
case 'background':

classes/replacer.php

Lines changed: 45 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,17 @@ public function __construct($post_id)
4545
if (function_exists('wp_get_original_image_path')) // WP 5.3+
4646
{
4747
$source_file = wp_get_original_image_path($post_id);
48+
4849
if ($source_file === false) // if it's not an image, returns false, use the old way.
50+
{
4951
$source_file = trim(get_attached_file($post_id, apply_filters( 'emr_unfiltered_get_attached_file', true )));
52+
}
53+
else {
54+
$this->source_is_scaled = true;
55+
}
5056
}
5157
else
52-
$source_file = trim(get_attached_file($post_id, apply_filters( 'emr_unfiltered_get_attached_file', true )));
58+
$source_file = trim(get_attached_file($post_id, apply_filters( 'emr_unfiltered_get_attached_file', true )));
5359

5460
/* It happens that the SourceFile returns relative / incomplete when something messes up get_upload_dir with an error something.
5561
This case shoudl be detected here and create a non-relative path anyhow..
@@ -78,8 +84,7 @@ public function __construct($post_id)
7884
}
7985
else
8086
$this->source_url = wp_get_attachment_url($post_id);
81-
// $this->ThumbnailUpdater = new \ThumbnailUpdater($post_id);
82-
//$this->ThumbnailUpdater->setOldMetadata($this->source_metadata);
87+
8388
}
8489

8590
private function fs()
@@ -139,8 +144,15 @@ public function replaceWith($file, $fileName)
139144

140145
if (false === $result_moved)
141146
{
142-
$ex = sprintf( esc_html__('The uploaded file could not be moved to %1$s. This is most likely an issue with permissions, or upload failed.', "enable-media-replace"), $targetFile );
143-
throw new \RuntimeException($ex);
147+
if ($targetFileObj->exists())
148+
{
149+
Log::addDebug('Could remove file from tmp directory?');
150+
}
151+
else {
152+
$ex = sprintf( esc_html__('The uploaded file could not be moved to %1$s. This is most likely an issue with permissions, or upload failed.', "enable-media-replace"), $targetFile );
153+
throw new \RuntimeException($ex);
154+
}
155+
144156
}
145157

146158
// init targetFile.
@@ -513,10 +525,11 @@ protected function doSearchReplace($args = array())
513525
// get relurls of both source and target.
514526
$urls = $this->getRelativeURLS();
515527

516-
517528
if ($args['thumbnails_only'])
518529
{
519-
foreach($urls as $side => $data)
530+
// if (isset($urls['source']['file']) && $urls['source'])
531+
532+
/*foreach($urls as $side => $data)
520533
{
521534
if (isset($data['base']))
522535
{
@@ -526,7 +539,7 @@ protected function doSearchReplace($args = array())
526539
{
527540
unset($urls[$side]['file']);
528541
}
529-
}
542+
} */
530543
}
531544

532545
$search_urls = $urls['source'];
@@ -543,8 +556,18 @@ protected function doSearchReplace($args = array())
543556
}
544557
}
545558

546-
Log::addDebug('Source', $this->source_metadata);
547-
Log::addDebug('Target', $this->target_metadata);
559+
// Original can be unbalanced
560+
if (isset($search_urls['original']))
561+
{
562+
if (! isset($replace_urls['original']))
563+
{
564+
$replace_urls['original'] = $replace_urls['file'];
565+
}
566+
}
567+
568+
569+
Log::addDebug('Source', $search_urls);
570+
Log::addDebug('Target', $replace_urls);
548571
/* If on the other hand, some sizes are available in source, but not in target, try to replace them with something closeby. */
549572
foreach($search_urls as $size => $url)
550573
{
@@ -563,6 +586,11 @@ protected function doSearchReplace($args = array())
563586
Log::addDebug('Unset size ' . $size . ' - no closest found in source');
564587
}
565588
}
589+
elseif ($url === $replace_urls[$size]) { // identical
590+
unset($replace_urls[$size]);
591+
unset($search_urls[$size]);
592+
Log::addDebug('Unset size ' . $size . ' - search and replace identical');
593+
}
566594
}
567595

568596
/* If source and target are the same, remove them from replace. This happens when replacing a file with same name, and +/- same dimensions generated.
@@ -707,7 +735,6 @@ private function handleMetaData($url, $search_urls, $replace_urls)
707735
}
708736

709737
$sql = $wpdb->prepare($sql, $prepare);
710-
Log::addTemp('Handle MEta SQL ' . $sql);
711738

712739
if ($wpdb->last_error)
713740
Log::addWarn('Error' . $wpdb->last_error, $wpdb->last_query);
@@ -828,6 +855,10 @@ private function getFilesFromMetadata($meta)
828855
$fileArray = array();
829856
if (isset($meta['file']))
830857
$fileArray['file'] = $meta['file'];
858+
if (isset($meta['original_image']))
859+
{
860+
$fileArray['original'] = $meta['original_image'];
861+
}
831862

832863
if (isset($meta['sizes']))
833864
{
@@ -839,6 +870,8 @@ private function getFilesFromMetadata($meta)
839870
}
840871
}
841872
}
873+
874+
// scaled
842875
return $fileArray;
843876
}
844877

@@ -860,9 +893,6 @@ private function getRelativeURLS()
860893
'target' => array('url' => $this->target_url, 'metadata' => $this->getFilesFromMetadata($this->target_metadata) ),
861894
);
862895

863-
// Log::addDebug('Source Metadata', $this->source_metadata);
864-
// Log::addDebug('Target Metadata', $this->target_metadata);
865-
866896
$result = array();
867897

868898
foreach($dataArray as $index => $item)
@@ -880,7 +910,7 @@ private function getRelativeURLS()
880910
}
881911

882912
}
883-
// Log::addDebug('Relative URLS', $result);
913+
Log::addDebug('Relative URLS', $result);
884914
return $result;
885915
}
886916

classes/uihelper.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,6 @@ public function getSuccesRedirect($post_id)
8282
}
8383

8484
$url = apply_filters('emr_returnurl', $url);
85-
Log::addDebug('Success URL- ' . $url);
8685

8786
return $url;
8887

@@ -100,7 +99,6 @@ public function getFailedRedirect($attach_id)
10099
);
101100

102101
$url = apply_filters('emr_returnurl_failed', $url);
103-
Log::addDebug('Failed URL- ' . $url);
104102
return $url;
105103
}
106104

enable-media-replace.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* Plugin Name: Enable Media Replace
44
* Plugin URI: https://wordpress.org/plugins/enable-media-replace/
55
* Description: Enable replacing media files by uploading a new file in the "Edit Media" section of the WordPress Media Library.
6-
* Version: 4.0.1
6+
* Version: 4.0.2
77
* Author: ShortPixel
88
* Author URI: https://shortpixel.com
99
* GitHub Plugin URI: https://github.com/short-pixel-optimizer/enable-media-replace
@@ -27,7 +27,7 @@
2727

2828
namespace EnableMediaReplace;
2929

30-
define( 'EMR_VERSION', '4.0.1' );
30+
define( 'EMR_VERSION', '4.0.2' );
3131

3232
if ( ! defined( 'ABSPATH' ) ) {
3333
exit; // Exit if accessed directly.

readme.txt

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ Tags: replace, attachment, media, files, replace image, remove background, repla
55
Requires at least: 4.9.7
66
Tested up to: 6.1
77
Requires PHP: 5.6
8-
Stable tag: 4.0.1
8+
Stable tag: 4.0.2
99

1010
Easily replace any attached image/file by simply uploading a new file in the Media Library edit view - a real time saver!
1111

@@ -61,6 +61,14 @@ If you want more control over the format in which the time is shown, you can use
6161

6262
== Changelog ==
6363

64+
= 4.0.2 =
65+
66+
Release date: January 13, 2023
67+
* Fix: patched a security vulnerability that could allow loading an unauthorized file during the replace operation;
68+
* Fix: finish the replace operation even if the tmp file cannot be removed due to file permissions issues;
69+
* Fix: when replacing a scaled file with a non-scaled file, some links were broken;
70+
* Fix: under certain conditions a PHP warning about an undefined array key was displayed.
71+
6472
= 4.0.1 =
6573

6674
Release date: November 23, 2022

views/upload.php

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,6 @@
7474
$custom_minute = str_pad($_POST['custom_minute'], 2, 0, STR_PAD_LEFT);
7575

7676
// create a mysql time representation from what we have.
77-
Log::addDebug($_POST);
7877
Log::addDebug('Custom Date - ' . $custom_date . ' ' . $custom_hour . ':' . $custom_minute);
7978
$custom_date = \DateTime::createFromFormat('Y-m-d G:i', $custom_date . ' ' . $custom_hour . ':' . $custom_minute);
8079
if ($custom_date === false) {
@@ -115,8 +114,7 @@
115114
$replacer->setTimeMode($timestamp_replace, $datetime);
116115

117116
/** Check if file is uploaded properly **/
118-
// @todo Post remove Bg should be removed.
119-
if (is_uploaded_file($_FILES["userfile"]["tmp_name"]) || isset($_POST["remove_bg"])) {
117+
if (is_uploaded_file($_FILES["userfile"]["tmp_name"])) {
120118
Log::addDebug($_FILES['userfile']);
121119

122120
// New method for validating that the uploaded file is allowed, using WP:s internal wp_check_filetype_and_ext() function.
@@ -131,15 +129,15 @@
131129
}
132130

133131

134-
if ($filedata["ext"] == false && ! current_user_can('unfiltered_upload') && ! isset($_POST["remove_bg"])) {
132+
if ($filedata["ext"] == false && ! current_user_can('unfiltered_upload')) {
133+
Log::addWarn('Uploaded File type does not meet security guidelines, aborting');
135134
Notices::addError(esc_html__("File type does not meet security guidelines. Try another.", 'enable-media-replace'));
136135
wp_safe_redirect($redirect_error);
137136
exit();
138137
}
139138

140139
// Here we have the uploaded file
141140
$new_filename = $_FILES["userfile"]["name"];
142-
//$new_filesize = $_FILES["userfile"]["size"]; // Seems not to be in use.
143141
$new_filetype = $filedata["type"] ? $filedata["type"] : $_FILES['userfile']['type'];
144142

145143
// Gather all functions that both options do.
@@ -153,7 +151,7 @@
153151
}
154152
*/
155153
try {
156-
$result = $replacer->replaceWith($_FILES["userfile"]["tmp_name"], $new_filename , isset($_POST["remove_bg"]));
154+
$result = $replacer->replaceWith($_FILES["userfile"]["tmp_name"], $new_filename);
157155
} catch (\RunTimeException $e) {
158156
var_dump($e->getMessage());
159157
die;
@@ -168,7 +166,7 @@
168166

169167
// Execute hook actions - thanks rubious for the suggestion!
170168
} else {
171-
//TODO Better error handling when no file is selected.
169+
//@TODO Better error handling when no file is selected.
172170
//For now just go back to media management
173171
//$returnurl = admin_url("upload.php");
174172
Log::addInfo('Failed. Redirecting - '. $redirect_error);

0 commit comments

Comments
 (0)