forked from keichan34/wpro
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wpro.php
717 lines (590 loc) · 24.7 KB
/
wpro.php
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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
<?php
/**
Plugin Name: WP Read-Only
Plugin URI: http://wordpress.org/extend/plugins/wpro/
Description: Plugin for running your Wordpress site without Write Access to the web directory. Amazon S3 is used for uploads/binary storage. This plugin was made with cluster/load balancing server setups in mind - where you do not want your WordPress to write anything to the local web directory.
Version: 1.0
Author: alfreddatakillen
Author URI: http://nurd.nu/
License: GPLv2
*/
// define('WPRO_DEBUG', true);
// PHP < 5.2.1 compatibility
if ( !function_exists('sys_get_temp_dir')) {
function sys_get_temp_dir() {
if( $temp = getenv('TMP') ) return $temp;
if( $temp = getenv('TEMP') ) return $temp;
if( $temp = getenv('TMPDIR') ) return $temp;
$temp = tempnam(__FILE__, '');
if (file_exists($temp)) {
unlink($temp);
return dirname($temp);
}
return null;
}
}
// open_basedir / safe_mode disallows CURLOPT_FOLLOWLOCATION
function curl_exec_follow($ch, &$maxredirect = null) {
$mr = $maxredirect === null ? 5 : intval($maxredirect);
if (ini_get('open_basedir') == '' && ini_get('safe_mode' == 'Off')) {
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, $mr > 0);
curl_setopt($ch, CURLOPT_MAXREDIRS, $mr);
} else {
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false);
if ($mr > 0) {
$newurl = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL);
$rch = curl_copy_handle($ch);
curl_setopt($rch, CURLOPT_HEADER, true);
curl_setopt($rch, CURLOPT_NOBODY, true);
curl_setopt($rch, CURLOPT_FORBID_REUSE, false);
curl_setopt($rch, CURLOPT_RETURNTRANSFER, true);
do {
curl_setopt($rch, CURLOPT_URL, $newurl);
$header = curl_exec($rch);
if (curl_errno($rch)) {
$code = 0;
} else {
$code = curl_getinfo($rch, CURLINFO_HTTP_CODE);
if ($code == 301 || $code == 302) {
preg_match('/Location:(.*?)\n/', $header, $matches);
$newurl = trim(array_pop($matches));
} else {
$code = 0;
}
}
} while ($code && --$mr);
curl_close($rch);
if (!$mr) {
if ($maxredirect === null) {
trigger_error('Too many redirects. When following redirects, libcurl hit the maximum amount.', E_USER_WARNING);
} else {
$maxredirect = 0;
}
return false;
}
curl_setopt($ch, CURLOPT_URL, $newurl);
}
}
return curl_exec($ch);
}
function wpro_get_option($option, $default = false) {
if (!defined('WPRO_ON') || !WPRO_ON) {
return get_site_option($option, $default);
}
$constantName = strtoupper(str_replace('-', '_', $option));
if (defined($constantName)) {
return constant($constantName);
} else {
return $default;
}
}
new WordpressReadOnly;
/* * * * * * * * * * * * * * * * * * * * * * *
GENERIC FUNCTION FOR NORMALIZING URLS:
* * * * * * * * * * * * * * * * * * * * * * */
class WordpressReadOnlyGeneric {
public $temporaryLocalData = array();
function debug($msg) {
if (defined('WPRO_DEBUG') && WPRO_DEBUG) {
$fh = fopen('/tmp/wpro-debug', 'a');
fwrite($fh, trim($msg) . "\n");
fclose($fh);
}
}
function removeTemporaryLocalData($file) {
$this->debug('WordpressReadOnlyGeneric::removeTemporaryLocalData("' . $file . '");');
$this->temporaryLocalData[] = $file;
}
function url_normalizer($url) {
if (strpos($url, '%') !== false) return $url;
$url = explode('/', $url);
foreach ($url as $key => $val) $url[$key] = urlencode($val);
return str_replace('%3A', ':', join('/', $url));
}
}
/* * * * * * * * * * * * * * * * * * * * * * *
BACKENDS:
* * * * * * * * * * * * * * * * * * * * * * */
class WordpressReadOnlyBackend extends WordpressReadOnlyGeneric {
function upload($localpath, $url, $mimetype) {
return false;
}
function file_exists($path) {
$this->debug('WordpressReadOnlyBackend::file_exists("' . $path . '");');
$path = $this->url_normalizer($path);
$this->debug('-> testing url: ' . $path);
// If at this point, the testing url is not a full http url,
// then there is something wrong in the wp_upload_dir functionality,
// because of write permission errors to the system tmp or any thing else.
$ch = curl_init();
curl_setopt($ch, CURLOPT_NOBODY, 1);
curl_setopt($ch, CURLOPT_URL, $path);
$result = trim(curl_exec_follow($ch));
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$this->debug('-> http return code: ' . $httpCode);
if ($httpCode != 200) return false;
return true;
}
}
class WordpressReadOnlyFTP extends WordpressReadOnlyBackend {
function __construct() {
}
function upload($file, $fullurl, $mime) {
return false;
}
}
class WordpressReadOnlyS3 extends WordpressReadOnlyBackend {
public $key;
public $secret;
public $bucket;
public $endpoint;
function __construct() {
$this->key = wpro_get_option('wpro-aws-key');
$this->secret = wpro_get_option('wpro-aws-secret');
$this->bucket = wpro_get_option('wpro-aws-bucket');
$this->endpoint = wpro_get_option('wpro-aws-endpoint');
}
function upload($file, $fullurl, $mime) {
$this->debug('WordpressReadOnlyS3::upload("' . $file . '", "' . $fullurl . '", "' . $mime . '");');
$fullurl = $this->url_normalizer($fullurl);
if (!preg_match('/^http:\/\/([^\/]+)\/(.*)$/', $fullurl, $regs)) return false;
$url = $regs[2];
if (!file_exists($file)) return false;
$this->removeTemporaryLocalData($file);
$fin = fopen($file, 'r');
if (!$fin) return false;
$fout = fsockopen($this->endpoint, 80, $errno, $errstr, 30);
if (!$fout) return false;
$datetime = gmdate('r');
$string2sign = "PUT\n\n" . $mime . "\n" . $datetime . "\nx-amz-acl:public-read\n/" . $this->bucket . "/" . $url;
$this->debug('STRING TO SIGN:');
$this->debug($string2sign);
$debug = '';
for ($i = 0; $i < strlen($string2sign); $i++) $debug .= dechex(ord(substr($string2sign, $i, 1))) . ' ';
$this->debug($debug);
// Todo: Make this work with php cURL instead of fsockopen/etc..
$query = "PUT /" . $this->bucket . "/" . $url . " HTTP/1.1\n";
$query .= "Host: " . $this->endpoint . "\n";
$query .= "x-amz-acl: public-read\n";
$query .= "Connection: keep-alive\n";
$query .= "Content-Type: " . $mime . "\n";
$query .= "Content-Length: " . filesize($file) . "\n";
$query .= "Date: " . $datetime . "\n";
$query .= "Authorization: AWS " . $this->key . ":" . $this->amazon_hmac($string2sign) . "\n\n";
$this->debug('SEND:');
$this->debug($query);
fwrite($fout, $query);
while (feof($fin) === false) fwrite($fout, fread($fin, 8192));
fclose($fin);
// Get the amazon response:
$this->debug('RECEIVE:');
$response = '';
while (!feof($fout)) {
$data = fgets($fout, 256);
$this->debug($data);
$response .= $data;
if (strpos($response, "\r\n\r\n") !== false) { // Header fully returned.
$this->debug('ALL RESPONSE HEADERS RECEIVED.');
if (strpos($response, 'Content-Length: 0') !== false) break; // Return if Content-Length: 0 (and header is fully returned)
if (substr($response, -7) == "\r\n0\r\n\r\n") break; // Keep-alive responses does not return EOF, they end with this string.
}
}
fclose($fout);
if (strpos($response, '<Error>') !== false) return false;
return true;
}
function amazon_hmac($string) {
return base64_encode(extension_loaded('hash') ?
hash_hmac('sha1', $string, $this->secret, true) : pack('H*', sha1(
(str_pad($this->secret, 64, chr(0x00)) ^ (str_repeat(chr(0x5c), 64))) .
pack('H*', sha1((str_pad($this->secret, 64, chr(0x00)) ^
(str_repeat(chr(0x36), 64))) . $string)))));
}
}
/* * * * * * * * * * * * * * * * * * * * * * *
THE MAIN PLUGIN CLASS:
* * * * * * * * * * * * * * * * * * * * * * */
class WordpressReadOnly extends WordpressReadOnlyGeneric {
public $backend = null;
public $tempdir = '/tmp';
function __construct() {
if (!defined('WPRO_ON') || !WPRO_ON) {
add_action('admin_init', array($this, 'admin_init')); // Register the settings.
// if ($this->is_trusted()) { // To early to find out, however in admin_init hook, it seems(?) to be too late to add network_admin_menu (which is weird, but i don't get it working there.)
if (is_multisite()) {
add_action('network_admin_menu', array($this, 'network_admin_menu'));
} else {
add_action('admin_menu', array($this, 'admin_menu')); // Will add the settings menu.
}
add_action('admin_post_wpro_settings_POST', array($this, 'admin_post')); // Gets called from plugin admin page POST request.
// }
}
add_filter('wp_handle_upload', array($this, 'handle_upload')); // The very filter that takes care of uploads.
add_filter('upload_dir', array($this, 'upload_dir')); // Sets the paths and urls for uploads.
add_filter('wp_generate_attachment_metadata', array($this, 'generate_attachment_metadata')); // We use this filter to store resized versions of the images.
add_filter('load_image_to_edit_path', array($this, 'load_image_to_edit_path')); // This filter downloads the image to our local temporary directory, prior to editing the image.
add_filter('wp_save_image_file', array($this, 'save_image_file'), 10, 5); // Store image file.
add_filter('wp_upload_bits', array($this, 'upload_bits')); // On XMLRPC uploads, files arrives as strings, which we are handling in this filter.
add_filter('wp_handle_upload_prefilter', array($this, 'handle_upload_prefilter')); // This is where we check for filename dupes (and change them to avoid overwrites).
add_filter('shutdown', array($this, 'shutdown'));
switch (wpro_get_option('wpro-service')) {
case 'ftp':
$this->backend = new WordpressReadOnlyFTP();
break;
default:
$this->backend = new WordpressReadOnlyS3();
}
$this->tempdir = sys_get_temp_dir();
if (substr($this->tempdir, -1) != '/') $this->tempdir = $this->tempdir . '/';
}
function is_trusted() {
if (is_multisite()) {
if (is_super_admin()) {
return true;
}
} else {
if (current_user_can('manage_options')) {
return true;
}
}
return false;
}
/* * * * * * * * * * * * * * * * * * * * * * *
REGISTER THE SETTINGS:
* * * * * * * * * * * * * * * * * * * * * * */
function admin_init() {
add_site_option('wpro-service', '');
add_site_option('wpro-folder', '');
add_site_option('wpro-aws-key', '');
add_site_option('wpro-aws-secret', '');
add_site_option('wpro-aws-bucket', '');
add_site_option('wpro-aws-virthost', '');
add_site_option('wpro-aws-endpoint', '');
add_site_option('wpro-ftp-server', '');
add_site_option('wpro-ftp-user', '');
add_site_option('wpro-ftp-password', '');
add_site_option('wpro-ftp-pasvmode', '');
add_site_option('wpro-ftp-webroot', '');
}
/* * * * * * * * * * * * * * * * * * * * * * *
ADMIN MENU:
* * * * * * * * * * * * * * * * * * * * * * */
function admin_menu() {
add_options_page('WPRO Plugin Settings', 'WPRO Settings', 'manage_options', 'wpro', array($this, 'admin_form'));
}
function network_admin_menu() {
add_submenu_page('settings.php', 'WPRO Plugin Settings', 'WPRO Settings', 'manage_options', 'wpro', array($this, 'admin_form'));
}
function admin_post() {
// We are handling the POST settings stuff ourselves, instead of using the Settings API.
// This is because the Settings API has no way of storing network wide options in multisite installs.
if (!$this->is_trusted()) return false;
if ($_POST['action'] != 'wpro_settings_POST') return false;
foreach (array('wpro-service', 'wpro-folder', 'wpro-aws-key', 'wpro-aws-secret', 'wpro-aws-bucket', 'wpro-aws-virthost', 'wpro-aws-endpoint', 'wpro-ftp-server', 'wpro-ftp-user', 'wpro-ftp-password', 'wpro-ftp-pasvmode') as $allowedPostData) {
$data = false;
if (isset($_POST[$allowedPostData])) $data = stripslashes($_POST[$allowedPostData]);
update_site_option($allowedPostData, $data);
}
header('Location: ' . admin_url('network/settings.php?page=wpro&updated=true'));
exit();
}
function admin_form() {
if (!$this->is_trusted()) {
wp_die ( __ ('You do not have sufficient permissions to access this page.'));
}
$wproService = wpro_get_option('wpro-service');
?>
<script language="JavaScript">
(function($) {
$(document).ready(function() {
$('#wpro-service-s3').change(function() {
$('.wpro-service-div:visible').slideUp(function() {
$('#wpro-service-s3-div').slideDown();
});
});
$('#wpro-service-ftp').change(function() {
$('.wpro-service-div:visible').slideUp(function() {
$('#wpro-service-ftp-div').slideDown();
});
});
});
})(jQuery);
</script>
<div class="wrap">
<div id="icon-plugins" class="icon32"><br /></div>
<h2>WP Read-Only (WPRO)</h2>
<?php if ($_GET['updated']) { ?>
<div id="message" class="updated">
<p>Options saved.</p>
</div>
<?php } ?>
<form name="wpro-settings-form" action="<?php echo admin_url('admin-post.php');?>" method="post">
<input type="hidden" name="action" value="wpro_settings_POST" />
<h3><?php echo __('Common Settings'); ?></h3>
<table class="form-table">
<tr>
<th><label>Storage Service</label></th>
<td>
<input name="wpro-service" id="wpro-service-s3" type="radio" value="s3" <?php if ($wproService != 'ftp') echo ('checked="checked"'); ?>/> <label for="wpro-service-s3">Amazon S3</label><br />
<input name="wpro-service" id="wpro-service-ftp" type="radio" value="ftp" <?php if ($wproService == 'ftp') echo ('checked="checked"'); ?>/> <label for="wpro-service-ftp">FTP Server</label><br />
</td>
</tr>
<tr>
<th><label for="wpro-folder">Prepend all paths with folder</th>
<td><input name="wpro-folder" id="wpro-folder" type="text" value="<?php echo(wpro_get_option('wpro-folder')); ?>" class="regular-text code" /></td>
</tr>
</table>
<div class="wpro-service-div" id="wpro-service-s3-div" <?php if ($wproService == 'ftp') echo ('style="display:none"'); ?> >
<h3><?php echo __('Amazon S3 Settings'); ?></h3>
<table class="form-table">
<tr>
<th><label for="wpro-aws-key">AWS Key</label></th>
<td><input name="wpro-aws-key" id="wpro-aws-key" type="text" value="<?php echo wpro_get_option('wpro-aws-key'); ?>" class="regular-text code" /></td>
</tr>
<tr>
<th><label for="wpro-aws-secret">AWS Secret</label></th>
<td><input name="wpro-aws-secret" id="wpro-aws-secret" type="text" value="<?php echo wpro_get_option('wpro-aws-secret'); ?>" class="regular-text code" /></td>
</tr>
<tr>
<th><label for="wpro-aws-bucket">S3 Bucket</label></th>
<td>
<input name="wpro-aws-bucket" id="wpro-aws-bucket" type="text" value="<?php echo wpro_get_option('wpro-aws-bucket'); ?>" class="regular-text code" /><br />
<input name="wpro-aws-virthost" id="wpro-aws-virthost" type="checkbox" value="1" <?php if (wpro_get_option('wpro-aws-virthost')) echo('checked="checked"'); ?> /> Virtual hosting is enabled for this bucket.
</td>
</tr>
<tr>
<th><label for="wpro-aws-endpoint">Bucket AWS Region</label></th>
<td>
<select name="wpro-aws-endpoint" id="wpro-aws-endpoint">
<?php
$aws_regions = array(
's3.amazonaws.com' => 'US East Region (Standard)',
's3-us-west-2.amazonaws.com' => 'US West (Oregon) Region',
's3-us-west-1.amazonaws.com' => 'US West (Northern California) Region',
's3-eu-west-1.amazonaws.com' => 'EU (Ireland) Region',
's3-ap-southeast-1.amazonaws.com' => 'Asia Pacific (Singapore) Region',
's3-ap-northeast-1.amazonaws.com' => 'Asia Pacific (Tokyo) Region',
's3-sa-east-1.amazonaws.com' => 'South America (Sao Paulo) Region'
);
// Endpoints comes from http://docs.amazonwebservices.com/general/latest/gr/rande.html
foreach ($aws_regions as $endpoint => $endpoint_name) {
echo ('<option value="' . $endpoint . '"');
if ($endpoint == wpro_get_option('wpro-aws-endpoint')) {
echo(' selected="selected"');
}
echo ('>' . $endpoint_name . '</option>');
}
?>
</select>
</td>
</tr>
</table>
</div>
<div class="wpro-service-div" id="wpro-service-ftp-div" <?php if ($wproService != 'ftp') echo ('style="display:none"'); ?> >
<h3><?php echo __('FTP Settings'); ?></h3>
<table class="form-table">
<tr>
<th><label for="wpro-ftp-server">FTP Server</label></th>
<td><input name="wpro-ftp-server" id="wpro-ftp-server" type="text" value="<?php echo wpro_get_option('wpro-ftp-server'); ?>" class="regular-text code" /></td>
</tr>
<tr>
<th><label for="wpro-ftp-user">FTP Username</label></th>
<td><input name="wpro-ftp-user" id="wpro-ftp-user" type="text" value="<?php echo wpro_get_option('wpro-ftp-user'); ?>" class="regular-text code" /></td>
</tr>
</table>
</div>
<p class="submit">
<input type="submit" name="submit" class="button-primary" value="<?php echo __('Save Changes'); ?>" />
</p>
</form>
</div>
<?php
}
/* * * * * * * * * * * * * * * * * * * * * * *
TAKING CARE OF UPLOADS:
* * * * * * * * * * * * * * * * * * * * * * */
function handle_upload($data) {
$this->debug('WordpressReadOnly::handle_upload($data);');
$this->debug('-> $data = ');
$this->debug(print_r($data, true));
$data['url'] = $this->url_normalizer($data['url']);
if (!file_exists($data['file'])) return false;
$response = $this->backend->upload($data['file'], $data['url'], $data['type']);
if (!$response) return false;
return $data;
}
public $upload_basedir = ''; // Variable for caching in the upload_dir()-method
function upload_dir($data) {
// $this->debug('WordpressReadOnly::upload_dir($data);');
// $this->debug('-> $data = ');
// $this->debug(print_r($data, true));
if ($this->upload_basedir == '') {
$this->upload_basedir = $this->tempdir . 'wpro' . time() . rand(0, 999999);
while (is_dir($this->upload_basedir)) $this->upload_basedir = $this->tempdir . 'wpro' . time() . rand(0, 999999);
}
$data['basedir'] = $this->upload_basedir;
switch (wpro_get_option('wpro-service')) {
case 'ftp':
$data['baseurl'] = 'http://' . trim(str_replace('//', '/', trim(wpro_get_option('wpro-ftp-webroot'), '/') . '/' . trim(wpro_get_option('wpro-folder'))), '/');
break;
default:
if (wpro_get_option('wpro-aws-virthost')) {
$data['baseurl'] = 'http://' . trim(str_replace('//', '/', wpro_get_option('wpro-aws-bucket') . '/' . trim(wpro_get_option('wpro-folder'))), '/');
} else {
$data['baseurl'] = 'http://' . trim(str_replace('//', '/', wpro_get_option('wpro-aws-bucket') . '.s3.amazonaws.com/' . trim(wpro_get_option('wpro-folder'))), '/');
}
}
$data['path'] = $this->upload_basedir . $data['subdir'];
$data['url'] = $data['baseurl'] . $data['subdir'];
// $this->debug('-> RETURNS = ');
// $this->debug(print_r($data, true));
return $data;
}
function generate_attachment_metadata($data) {
if (!is_array($data) || !isset($data['sizes']) || !is_array($data['sizes'])) return $data;
$upload_dir = wp_upload_dir();
foreach ($data['sizes'] as $size => $sizedata) {
$file = $upload_dir['basedir'] . '/' . $sizedata['file'];
$url = $upload_dir['baseurl'].'/' . $sizedata['file'];
$mime = 'application/octet-stream';
switch(substr($file, -4)) {
case '.gif':
$mime = 'image/gif';
break;
case '.jpg':
$mime = 'image/jpeg';
break;
case '.png':
$mime = 'image/png';
break;
}
$this->backend->upload($file, $url, $mime);
}
return $data;
}
function load_image_to_edit_path($filepath) {
$this->debug('WordpressReadOnly::load_image_to_edit_path("' . $filepath . '");');
if (substr($filepath, 0, 7) == 'http://') {
$ending = '';
if (preg_match('/\.([^\.\/]+)$/', $filepath, $regs)) $ending = '.' . $regs[1];
$tmpfile = $this->tempdir . 'wpro' . time() . rand(0, 999999) . $ending;
while (file_exists($tmpfile)) $tmpfile = $this->tempdir . 'wpro' . time() . rand(0, 999999) . $ending;
$filepath = $this->url_normalizer($filepath);
$this->debug('-> Loading file from: ' . $filepath);
$this->debug('-> Storing file at: ' . $tmpfile);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $filepath);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_AUTOREFERER, true);
$fh = fopen($tmpfile, 'w');
fwrite($fh, curl_exec_follow($ch));
fclose($fh);
$this->removeTemporaryLocalData($tmpfile);
return $tmpfile;
}
return $filepath;
}
function save_image_file($dummy, $filename, $image, $mime_type, $post_id) {
$this->debug('WordpressReadOnly::save_image_file("' . $dummy . '", "' . $filename . '", "' . $image . '", "' . $mime_type . '", "' . $post_id . '");');
if (substr($filename, 0, strlen($this->tempdir)) != $this->tempdir) return false;
$filename = substr($filename, strlen($this->tempdir));
if (!preg_match('/^wpro[0-9]+(\/.+)$/', $filename, $regs)) return false;
$filename = $regs[1];
$tmpfile = $this->tempdir . 'wpro' . time() . rand(0, 999999);
while (file_exists($tmpfile)) $tmpfile = $this->tempdir . 'wpro' . time() . rand(0, 999999);
$this->debug('-> Storing image as temporary file: ' . $tmpfile);
switch ($mime_type) {
case 'image/jpeg':
imagejpeg($image, $tmpfile, apply_filters('jpeg_quality', 90, 'edit_image'));
break;
case 'image/png':
imagepng($image, $tmpfile);
break;
case 'image/gif':
imagegif($image, $tmpfile);
break;
default:
return false;
}
$upload = wp_upload_dir();
$url = $upload['baseurl'];
if (substr($url, -1) != '/') $url .= '/';
while (substr($filename, 0, 1) == '/') $filename = substr($filename, 1);
$url .= $filename;
return $this->backend->upload($tmpfile, $this->url_normalizer($url), $mime_type);
}
function upload_bits($data) {
$this->debug('WordpressReadOnly::upload_bits($data);');
$this->debug('-> $data = ');
$this->debug(print_r($data, true));
$ending = '';
if (preg_match('/\.([^\.\/]+)$/', $data['name'], $regs)) $ending = '.' . $regs[1];
$tmpfile = $this->tempdir . 'wpro' . time() . rand(0, 999999) . $ending;
while (file_exists($tmpfile)) $tmpfile = $this->tempdir . 'wpro' . time() . rand(0, 999999) . $ending;
$fh = fopen($tmpfile, 'wb');
fwrite($fh, $data['bits']);
fclose($fh);
$upload = wp_upload_dir();
return array(
'file' => $tmpfile,
'url' => $this->url_normalizer($upload['url'] . '/' . $data['name']),
'error' => false
);
}
// Handle duplicate filenames:
// Wordpress never calls the wp_handle_upload_overrides filter properly, so we do not have any good way of setting a callback for wp_unique_filename_callback, which would be the most beautiful way of doing this. So, instead we are usting the wp_handle_upload_prefilter to check for duplicates and rename the files...
function handle_upload_prefilter($file) {
$this->debug('WordpressReadOnly::handle_upload_prefilter($file);');
$this->debug('-> $file = ');
$this->debug(print_r($file, true));
$upload = wp_upload_dir();
$name = $file['name'];
$path = trim($upload['url'], '/') . '/' . $name;
$counter = 0;
while ($this->backend->file_exists($path)) {
if (preg_match('/\.([^\.\/]+)$/', $file['name'], $regs)) {
$ending = '.' . $regs[1];
$preending = substr($file['name'], 0, 0 - strlen($ending));
$name = $preending . '_' . $counter . $ending;
} else {
$name = $file['name'] . '_' . $counter;
}
$path = trim($upload['url'], '/') . '/' . $name;
$counter++;
}
$file['name'] = $name;
return $file;
}
function shutdown() {
$this->debug('WordpressReadOnly::shutdown()');
$this->temporaryLocalData = array_merge($this->temporaryLocalData, $this->backend->temporaryLocalData);
$this->debug('-> $this->temporaryLocalData = ');
$this->debug(print_r($this->temporaryLocalData, true));
$tempdir = sys_get_temp_dir();
if (substr($tempdir, -1) == '/') $tempdir = substr($tempdir, 0, -1);
foreach ($this->temporaryLocalData as $file) {
if (substr($file, 0, strlen($tempdir) + 1) == $tempdir . '/') {
while ($file != $tempdir) {
if (substr($file, -1) == '/') {
$file = substr($file, 0, -1);
} else {
if (is_file($file)) {
if (@unlink($file) == false) break;
$this->debug('-> Removed file: ' . $file);
} else if (is_dir($file)) {
if (@rmdir($file) == false) break;
$this->debug('-> Removed directory: ' . $file);
} else {
break;
}
if (preg_match('/^(.+)\/[^\/]+$/', $file, $regs)) {
$file = $regs[1];
} else {
break;
}
}
}
}
}
}
}