This repository was archived by the owner on Mar 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupload-larger-plugins.php
More file actions
334 lines (274 loc) · 12.3 KB
/
upload-larger-plugins.php
File metadata and controls
334 lines (274 loc) · 12.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
<?php
/*
Plugin Name: Upload Larger Plugins
Version: 1.2
Plugin URI: http://wordpress.org/plugins/upload-larger-plugins
Description: Allow plugins larger than the PHP-defined limit to be uploaded.
Author: David Anderson
Donate: http://david.dw-perspective.org.uk/donate
Author URI: http://david.dw-perspective.org.uk
License: MIT
*/
if (!defined('ABSPATH')) die('No direct access');
// Globals
define('UPLOADLARGERPLUGINS_VERSION', '1.2');
define('UPLOADLARGERPLUGINS_SLUG', "upload-larger-plugins");
define('UPLOADLARGERPLUGINS_DIR', dirname(realpath(__FILE__)));
define('UPLOADLARGERPLUGINS_URL', plugins_url('', __FILE__));
$simba_upload_larger_plugins = new Simba_Upload_Larger_Plugins();
class Simba_Upload_Larger_Plugins {
private $upload_dir;
public function __construct() {
#add_filter('plugin_action_links', array($this, 'action_links'), 10, 2 );
add_action('install_plugins_upload', array($this, 'install_plugins_upload'), 9, 1);
add_action('install_plugins_pre_upload', array($this, 'install_plugins_pre_upload'));
add_action('admin_enqueue_scripts', array($this, 'admin_enqueue_scripts'));
add_action('plugins_loaded', array($this, 'load_translations'));
add_action('admin_head', array($this, 'admin_head'));
add_action('wp_ajax_ulp_plupload_action', array($this, 'ulp_plupload_action'));
add_action('admin_init', array($this, 'admin_init'));
// This filter only exists on WP 3.7+
add_filter('upgrader_pre_download', array($this, 'upgrader_pre_download'), 10, 3);
}
public function admin_init() {
if (empty($_GET['plugincksha1']) || empty($_GET['overridebd']) || empty($_GET['package'])) return;
if (!current_user_can('install_plugins')) return;
global $wp_version;
require(ABSPATH.WPINC.'/version.php');
// The rest of the code's purpose is to work-around the lack of the upgrader_pre_download filter before WP 3.7
// The below would work on >= 3.7 too; but there, we use a more elegant/direct method.
if (version_compare($wp_version, '3.7', '>=')) return;
$upgrader = new stdClass;
$upgrader->strings = array('download_failed' => __('Error when trying to find uploaded file', 'uploadlargerplugins'));
$try_file = $this->upgrader_pre_download(false, $_GET['package'], $upgrader);
// The File_Upload_Upgrader object eventually gets constructed with this (where $urlholder = 'package', and $uploads = wp_upload_dir())
//File_Upload_Upgrader::filename = $_GET[$urlholder];
//File_Upload_Upgrader::package = $uploads['basedir'] . '/' . $this->filename;
if ( ! ( ( $uploads = wp_upload_dir() ) && false === $uploads['error'] ) ) return;
if (is_string($try_file) && file_exists($try_file)) {
$upload_dir = untrailingslashit(get_temp_dir());
// if (!is_writable($upload_dir)) return;
$this->upload_basedir = $upload_dir;
add_filter('upload_dir', array($this, 'upload_dir'));
add_action('upgrader_process_complete', array($this, 'upgrader_process_complete'));
}
}
// Only hooked on WP < 3.7
public function upgrader_process_complete() {
remove_filter('upload_dir', array($this, 'upload_dir'));
}
public function upgrader_pre_download($result, $package, $upgrader) {
if (empty($_GET['plugincksha1']) || empty($_GET['overridebd'])) return $result;
$upload_dir = untrailingslashit(get_temp_dir());
# Sanity checks
if ($upload_dir != $_GET['overridebd']) return new WP_Error('download_failed', $upgrader->strings['download_failed']);
$try_file = $upload_dir.'/'.basename($package);
if (!file_exists($try_file) || sha1_file($try_file) != $_GET['plugincksha1']) return new WP_Error('download_failed', $upgrader->strings['download_failed']);
return $try_file;
}
public function admin_enqueue_scripts() {
if (!current_user_can('install_plugins')) return;
global $pagenow;
if ($pagenow != 'plugin-install.php' || !isset($_REQUEST['tab']) || 'upload' != $_REQUEST['tab']) return;
wp_enqueue_script('ulp-admin-ui', UPLOADLARGERPLUGINS_URL.'/admin.js', array('jquery', 'plupload-all'), '1');
wp_localize_script('ulp-admin-ui', 'ulplion', array(
'notarchive' => __('This file does not appear to be a zip file.', 'uploadlargerplugins'),
'notarchive2' => '<p>'.__('This file does not appear to be a zip file.', 'uploadlargerplugins').'</p>',
'uploaderror' => __('Upload error:','uploadlargerplugins'),
'makesure' => __('(make sure that you were trying to upload a zip file','uploadlargerplugins'),
'uploaderr' => __('Upload error', 'uploadlargerplugins'),
'jsonnotunderstood' => __('Error: the server sent us a response (JSON) which we did not understand.', 'uploadlargerplugins'),
'error' => __('Error:','uploadlargerplugins')
));
}
public function load_translations() {
// Tell WordPress where to find the translations
load_plugin_textdomain('uploadlargerplugins', false, basename(dirname(__FILE__)).'/languages/');
}
public function upload_dir($uploads) {
if (!empty($this->upload_dir)) $uploads['path'] = $this->upload_dir;
if (!empty($this->upload_basedir)) $uploads['basedir'] = $this->upload_basedir;
return $uploads;
}
public function ulp_plupload_action() {
// check ajax nonce
@set_time_limit(900);
if (!current_user_can('install_plugins')) return;
check_ajax_referer('uploadlargerplugins-uploader');
$upload_dir = untrailingslashit(get_temp_dir());
if (!is_writable($upload_dir)) exit;
$this->upload_dir = $upload_dir;
add_filter('upload_dir', array($this, 'upload_dir'));
// handle file upload
$farray = array('test_form' => true, 'action' => 'ulp_plupload_action');
$farray['test_type'] = false;
$farray['ext'] = 'zip';
$farray['type'] = 'application/zip';
// if (isset($_POST['chunks'])) {
//
// } else {
// # Over-write - that's OK.
// $farray['unique_filename_callback'] = array($this, 'unique_filename_callback');
// }
$status = wp_handle_upload(
$_FILES['async-upload'],
$farray
);
remove_filter('upload_dir', array($this, 'upload_dir'));
if (isset($status['error'])) {
echo json_encode(array('e' => $status['error']));
exit;
}
# Should be a no-op
$name = basename($_POST['name']);
// If this was the chunk, then we should instead be concatenating onto the final file
if (isset($_POST['chunks']) && isset($_POST['chunk']) && preg_match('/^[0-9]+$/',$_POST['chunk'])) {
# A random element is added, because otherwise it is theoretically possible for another user to upload into a shared temporary directory in between the upload and install, and over-write
$final_file = $name;
rename($status['file'], $upload_dir.'/'.$final_file.'.'.$_POST['chunk'].'.zip.tmp');
$status['file'] = $upload_dir.'/'.$final_file.'.'.$_POST['chunk'].'.zip.tmp';
// Final chunk? If so, then stich it all back together
if ($_POST['chunk'] == $_POST['chunks']-1) {
if ($wh = fopen($upload_dir.'/'.$final_file, 'wb')) {
for ($i=0 ; $i<$_POST['chunks']; $i++) {
$rf = $upload_dir.'/'.$final_file.'.'.$i.'.zip.tmp';
if ($rh = fopen($rf, 'rb')) {
while ($line = fread($rh, 32768)) fwrite($wh, $line);
fclose($rh);
@unlink($rf);
}
}
fclose($wh);
$status['file'] = $upload_dir.'/'.$final_file;
}
}
}
$response = array();
if (!isset($_POST['chunks']) || (isset($_POST['chunk']) && $_POST['chunk'] == $_POST['chunks']-1)) {
$file = basename($status['file']);
if (!preg_match('/\.zip$/i', $file, $matches)) {
@unlink($status['file']);
echo json_encode(array('e' => sprintf(__('Error: %s', 'uploadlargerplugins'), __('This file does not appear to be a zip file.', 'uploadlargerplugins'))));
exit;
}
}
// send the redirect URL
$response['m'] = admin_url('update.php?action=upload-plugin&overridebd='.urlencode(dirname($status['file'])).'&plugincksha1='.sha1_file($status['file']).'&_wpnonce='.wp_create_nonce( 'plugin-upload' ).'&package='.urlencode(basename($status['file'])));
echo json_encode($response);
exit;
}
public function admin_head() {
if (!current_user_can('install_plugins')) return;
global $pagenow;
if ($pagenow != 'plugin-install.php' || !isset($_REQUEST['tab']) || 'upload' != $_REQUEST['tab']) return;
$chunk_size = min(wp_max_upload_size()-1024, 1024*1024*2-1024);
# The multiple_queues argument is ignored in plupload 2.x (WP3.9+) - http://make.wordpress.org/core/2014/04/11/plupload-2-x-in-wordpress-3-9/
# max_file_size is also in filters as of plupload 2.x, but in its default position is still supported for backwards-compatibility. Likewise, our use of filters.extensions below is supported by a backwards-compatibility option (the current way is filters.mime-types.extensions
$plupload_init = array(
'runtimes' => 'html5,flash,silverlight,html4',
'browse_button' => 'plupload-browse-button',
'container' => 'plupload-upload-ui',
'drop_element' => 'drag-drop-area',
'file_data_name' => 'async-upload',
'multiple_queues' => false,
'max_file_count' => 1,
'max_file_size' => '100Gb',
'chunk_size' => $chunk_size.'b',
'url' => admin_url('admin-ajax.php'),
'filters' => array(array('title' => __('Allowed Files'), 'extensions' => 'zip')),
'multipart' => true,
'multi_selection' => false,
'urlstream_upload' => true,
// additional post data to send to our ajax hook
'multipart_params' => array(
'_ajax_nonce' => wp_create_nonce('uploadlargerplugins-uploader'),
'action' => 'ulp_plupload_action'
)
);
// 'flash_swf_url' => includes_url('js/plupload/plupload.flash.swf'),
// 'silverlight_xap_url' => includes_url('js/plupload/plupload.silverlight.xap'),
# WP 3.9 updated to plupload 2.0 - https://core.trac.wordpress.org/ticket/25663
if (is_file(ABSPATH.'wp-includes/js/plupload/Moxie.swf')) {
$plupload_init['flash_swf_url'] = includes_url('js/plupload/Moxie.swf');
} else {
$plupload_init['flash_swf_url'] = includes_url('js/plupload/plupload.flash.swf');
}
if (is_file(ABSPATH.'wp-includes/js/plupload/Moxie.xap')) {
$plupload_init['silverlight_xap_url'] = includes_url('js/plupload/Moxie.xap');
} else {
$plupload_init['silverlight_xap_url'] = includes_url('js/plupload/plupload.silverlight.swf');
}
?><script type="text/javascript">
var ulp_plupload_config=<?php echo json_encode($plupload_init); ?>;
</script>
<style type="text/css">
.drag-drop #drag-drop-area {
border: 4px dashed #ddd;
height: 200px;
}
#filelist {
width: 100%;
}
#filelist .file {
padding: 5px;
background: #ececec;
border: solid 1px #ccc;
margin: 4px 0;
}
#filelist .fileprogress {
width: 0%;
background: #f6a828;
height: 5px;
}
</style>
<?php
}
public function install_plugins_pre_upload() {
# Unhook the default uploader
remove_action('install_plugins_upload', 'install_plugins_upload');
}
public function install_plugins_upload( $page = 1 ) {
?>
<!-- Upload form from Upload Larger Plugins -->
<h4><?php _e('Install a plugin in .zip format'); ?></h4>
<p class="install-help"><?php _e('If you have a plugin in a .zip format, you may install it by uploading it here.'); ?></p>
<?php
global $wp_version;
if (version_compare($wp_version, '3.3', '<')) {
echo '<em>'.sprintf(__('This feature requires %s version %s or later', 'uploadlargerplugins'), 'WordPress', '3.3').'</em>';
} else {
?>
<div id="plupload-upload-ui" class="drag-drop" style="width: 70%;">
<div id="drag-drop-area">
<div class="drag-drop-inside">
<p class="drag-drop-info"><?php _e('Drop plugin zip here', 'uploadlargerplugins'); ?></p>
<p><?php _ex('or', 'Uploader: Drop plugin zip here - or - Select File'); ?></p>
<p class="drag-drop-buttons"><input id="plupload-browse-button" type="button" value="<?php echo esc_attr(__('Select File', 'uploadlargerplugins')); ?>" class="button" /></p>
</div>
</div>
<div id="filelist">
</div>
</div>
<?php
}
?>
<?php
/*
<div style="display:none;">
<form method="post" enctype="multipart/form-data" class="wp-upload-form" action="<?php echo self_admin_url('update.php?action=upload-plugin'); ?>">
<?php wp_nonce_field( 'plugin-upload'); ?>
<input type="file" id="pluginzip" name="pluginzip" />
<?php submit_button( __( 'Install Now' ), 'button', 'install-plugin-submit', false ); ?>
</form>
</div>
*/
}
public function action_links($links, $file) {
if ($file == UPLOADLARGERPLUGINS_SLUG."/".basename(__FILE__)) {
array_unshift( $links,
'<a href="options-general.php?page=upload_larger_plugins">'.__('Settings', 'uploadlargerplugins').'</a>'
);
}
return $links;
}
}