-
Notifications
You must be signed in to change notification settings - Fork 2
/
class-wp-filesystem-s3.php
690 lines (605 loc) · 19.3 KB
/
class-wp-filesystem-s3.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
<?php
/**
* WordPress S3 Filesystem.
Copyright (C) 2011 Caseproof, LLC
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>
*/
/**
* WordPress Filesystem Class for direct PHP file and folder manipulation.
*
* @uses WP_Filesystem_Base Extends class
*/
class WP_Filesystem_S3 extends WP_Filesystem_Base
{
var $errors = null;
var $s3Obj = null;
/**
* constructor
*
* @param mixed $arg ingored argument
*/
function __construct($arg)
{
$this->method = 'S3';
$this->errors = new WP_Error();
}
/**
* connect filesystem.
*
* @return bool Returns true on success or false on failure (always true for WP_Filesystem_Direct).
*/
function connect()
{
// Create the s3Obj & connect to S3
if($w3tcconfig = $this->_get_w3_config())
{
$engine = $w3tcconfig->get_string('cdn.engine');
if($engine == 's3' or $engine == 'cf')
{
$this->s3Obj = new S3( $w3tcconfig->get_string( 'cdn.'.$engine.'.key' ),
$w3tcconfig->get_string( 'cdn.'.$engine.'.secret' ) );
return ($this->s3Obj !== false);
}
}
return false;
}
/**
* Reads entire file into a string
*
* @param string $file Name of the file to read.
* @return string|bool The function returns the read data or false on failure.
*/
function get_contents($file)
{
// Pull down file from Amazon S3 and return it
if($w3tcconfig = $this->_get_w3_config())
{
$engine = $w3tcconfig->get_string('cdn.engine');
if($engine == 's3' or $engine == 'cf')
{
$bucket = $w3tcconfig->get_string("cdn.{$engine}.bucket");
$uri = $this->_get_s3_uri($file);
if( is_object($this->s3Obj) )
return $this->s3Obj->getObject( $bucket, $uri );
}
}
return false;
}
/**
* Reads entire file into an array
*
* @param string $file Path to the file.
* @return array|bool the file contents in an array or false on failure.
*/
function get_contents_array($file)
{
// Pull down file from Amazon S3 and run file on it
if($w3tcconfig = $this->_get_w3_config())
{
$engine = $w3tcconfig->get_string('cdn.engine');
if($engine == 's3' or $engine == 'cf')
{
$bucket = $w3tcconfig->get_string("cdn.{$engine}.bucket");
$uri = $this->_get_s3_uri($file);
if(is_object($this->s3Obj))
{
$tmp_file = $this->cwd() . '/' . uniqid( 'S3_file_' );
if($this->s3Obj->getObject( $bucket, $uri, $tmp_file ))
{
if( file_exists($tmp_file) )
{
$file_array = @file($tmp_file);
@unlink($tmp_file);
return $file_array;
}
}
}
}
}
return false;
}
/**
* Write a string to a file
*
* @param string $file Remote path to the file where to write the data.
* @param string $contents The data to write.
* @param int $mode (optional) The file permissions as octal number, usually 0644.
* @return bool False upon failure.
*/
function put_contents($file, $contents, $mode = false )
{
// push this thing up to s3
if($w3tcconfig = $this->_get_w3_config())
{
$engine = $w3tcconfig->get_string('cdn.engine');
if($engine == 's3' or $engine == 'cf')
{
$bucket = $w3tcconfig->get_string('cdn.'.$engine.'.bucket');
$uri = $this->_get_s3_uri( $file );
if( is_object($this->s3Obj) )
{
$result = $this->s3Obj->putObject( $contents, $bucket, $uri, $this->_get_s3_perms( $mode ), array(), $this->_get_mime_type($file) );
// We are deleting any reference of the file -- remember this is CDN ONLY
if(file_exists( $file ))
@unlink( $file );
}
}
}
return $result;
}
/**
* Gets the current working directory
*
* @return string|bool the current working directory on success, or false on failure.
*/
function cwd()
{
// Get the bucket .. or /tmp dir?
return '/tmp'; // We just work out of the tmp dir
}
/**
* Change directory
*
* @param string $dir The new current directory.
* @return bool Returns true on success or false on failure.
*/
function chdir($dir)
{
// chdir with buckets?
return true; // Don't worry about this now
}
/**
* Changes file group
*
* @param string $file Path to the file.
* @param mixed $group A group name or number.
* @param bool $recursive (optional) If set True changes file group recursivly. Defaults to False.
* @return bool Returns true on success or false on failure.
*/
function chgrp($file, $group, $recursive = false)
{
// Change group permissions
return true; // for now we don't care what group it's in
}
/**
* Changes filesystem permissions
*
* @param string $file Path to the file.
* @param int $mode (optional) The permissions as octal number, usually 0644 for files, 0755 for dirs.
* @param bool $recursive (optional) If set True changes file group recursivly. Defaults to False.
* @return bool Returns true on success or false on failure.
*/
function chmod($file, $mode = false, $recursive = false)
{
// Change S3 permissions
return true; // TODO: Make this actually work
}
/**
* Changes file owner
*
* @param string $file Path to the file.
* @param mixed $owner A user name or number.
* @param bool $recursive (optional) If set True changes file owner recursivly. Defaults to False.
* @return bool Returns true on success or false on failure.
*/
function chown($file, $owner, $recursive = false)
{
// Change S3 owner
return true; // for now we don't care who the owner is
}
/**
* Gets file owner
*
* @param string $file Path to the file.
* @return string Username of the user.
*/
function owner($file)
{
// Get Owner?
return "amazon"; // we don't care for now
}
/**
* Gets file permissions
*
* FIXME does not handle errors in fileperms()
*
* @param string $file Path to the file.
* @return string Mode of the file (last 4 digits).
*/
function getchmod($file)
{
return $this->_get_file_perms(S3::ACL_PUBLIC_READ);
}
function group($file)
{
return "amazon"; // we don't care for now
}
function copy($source, $destination, $overwrite = false)
{
// Copy the S3 file from one place to another
// push this thing up to s3
if($w3tcconfig = $this->_get_w3_config())
{
$engine = $w3tcconfig->get_string('cdn.engine');
if($engine == 's3' or $engine == 'cf')
{
$bucket = $w3tcconfig->get_string('cdn.'.$engine.'.bucket');
$src_uri = $this->_get_s3_uri($source);
$dest_uri = $this->_get_s3_uri($destination);
if( is_object($this->s3Obj) )
$this->s3Obj->copyObject( $bucket, $src_uri, $bucket, $dest_uri, $this->_get_s3_perms($this->getchmod($src_uri)), array(), array( "Content-Type" => $this->_get_mime_type($dest_uri) ) );
}
}
}
function move($source, $destination, $overwrite = false)
{
// Move the S3 file from one place to another
$this->copy($source, $destination, $overwrite);
$this->delete($source);
}
function delete($file, $recursive = false)
{
// Delete an S3 file
if($w3tcconfig = $this->_get_w3_config())
{
$engine = $w3tcconfig->get_string('cdn.engine');
if($engine == 's3' or $engine == 'cf')
{
// Check to make sure the file exists before trying to delete it
if(!$this->exists($file))
{
$uploadpath = wp_upload_dir();
$file = path_join($uploadpath['basedir'], $file );
if(!$this->exists($file))
return false;
}
$bucket = $w3tcconfig->get_string('cdn.'.$engine.'.bucket');
$uri = $this->_get_s3_uri($file);
return $this->s3Obj->deleteObject( $bucket, $uri );
}
}
return false;
}
function exists($file)
{
if($w3tcconfig = $this->_get_w3_config())
{
$engine = $w3tcconfig->get_string('cdn.engine');
if($engine == 's3' or $engine == 'cf')
{
$bucket = $w3tcconfig->get_string('cdn.'.$engine.'.bucket');
$uri = $this->_get_s3_uri( $file );
$s3File = @$this->s3Obj->getObject( $bucket, $uri );
return ($s3File !== false);
}
}
// File doesn't exist in S3
return false;
}
function is_file($file)
{
return $this->exists($file);
}
function is_dir($path)
{
// S3 doesn't really recognize directories but will auto
// create them -- so if the path isn't a file then we'll
// say it could be a directory -- if it doesn't exist then
// when a file is uploaded to it -- it will.
return !$this->is_file($path);
}
function is_readable($file)
{
$chmod = $this->getchmod($file);
return $this->_get_s3_perms( $chmod );
}
function is_writable($file)
{
// We'll assume that any file is writable by
// the owner of the current bucket setup in W3TC
return $this->is_file($file);
}
function atime($file)
{
if($this->exists($file))
{
$info = $this->_get_info($file);
if($info and is_array($info))
return $info['time'];
else
return time();
}
return false;
}
function mtime($file)
{
if($this->exists($file))
{
$info = $this->_get_info($file);
if($info and is_array($info))
return $info['time'];
else
return time();
}
return false;
}
function size($file)
{
if($this->exists($file))
{
$info = $this->_get_info($file);
if($info and is_array($info))
return $info['size'];
else
return 0;
}
return false;
}
/** I don't think we can update the timestamp without reuploading */
function touch($file, $time = 0, $atime = 0)
{
return true;
}
/** This only returns true because by the default in the S3 class the directories
* are automatically created for a file given a path. For instance, if
* I do a put_contents on a file path of 'cool-folder/wordup/g/dontmess.txt'
* and assuming that none of the directories in this path exists then
* S3 will create a 'cool-folder', 'wordup' and 'g' folder automatically.
* Therefore -- to stay backwards compatible with a standard filesystem
* we just return true here to indicate that the directory exists (even
* if it does not) because as soon as a file is placed in the non-existent
* folder then it will be created.
*/
function mkdir($path, $chmod = false, $chown = false, $chgrp = false)
{
return true;
}
function rmdir($path, $recursive = false)
{
return true;
}
function dirlist($path, $include_hidden = true, $recursive = false)
{
if($w3tcconfig = $this->_get_w3_config())
{
$engine = $w3tcconfig->get_string('cdn.engine');
if($engine == 's3' or $engine == 'cf')
$bucket = $w3tcconfig->get_string("cdn.{$engine}.bucket");
}
return $bucket; // TODO: List the dir at some point -- for now just return the bucket name
}
/** Gets the W3TC Config file if W3TC is active & installed
* @version Blair Williams (Caseproof, LLC)
* @return boolean W3_Config Object or false
*/
function _get_w3_config()
{
$w3tcconfig = & w3_instance('W3_Config');
return $w3tcconfig;
}
/** Pulls a file from S3 and saves it in the working directory. */
function _get_file($file)
{
if($w3tcconfig = $this->_get_w3_config())
{
$engine = $w3tcconfig->get_string('cdn.engine');
if($engine == 's3' or $engine == 'cf')
{
$bucket = $w3tcconfig->get_string("cdn.{$engine}.bucket");
$uri = $this->_get_s3_uri( $file );
if(is_object($this->s3Obj))
{
$tmp_file = $this->cwd() . '/' . basename($file);
$s3File = $this->s3Obj->getObject( $bucket, $uri, $tmp_file );
return $s3File;
}
}
}
return false;
}
function _get_mime_type($file)
{
preg_replace('#\.([^\.]*?)$#', $file, $matches);
$ext = $matches[1];
if( isset($ext) and $ext and !empty($ext) )
{
$mime_array = array(
"ez" => "application/andrew-inset",
"hqx" => "application/mac-binhex40",
"cpt" => "application/mac-compactpro",
"doc" => "application/msword",
"bin" => "application/octet-stream",
"dms" => "application/octet-stream",
"lha" => "application/octet-stream",
"lzh" => "application/octet-stream",
"exe" => "application/octet-stream",
"class" => "application/octet-stream",
"so" => "application/octet-stream",
"dll" => "application/octet-stream",
"oda" => "application/oda",
"pdf" => "application/pdf",
"ai" => "application/postscript",
"eps" => "application/postscript",
"ps" => "application/postscript",
"smi" => "application/smil",
"smil" => "application/smil",
"wbxml" => "application/vnd.wap.wbxml",
"wmlc" => "application/vnd.wap.wmlc",
"wmlsc" => "application/vnd.wap.wmlscriptc",
"bcpio" => "application/x-bcpio",
"vcd" => "application/x-cdlink",
"pgn" => "application/x-chess-pgn",
"cpio" => "application/x-cpio",
"csh" => "application/x-csh",
"dcr" => "application/x-director",
"dir" => "application/x-director",
"dxr" => "application/x-director",
"dvi" => "application/x-dvi",
"spl" => "application/x-futuresplash",
"gtar" => "application/x-gtar",
"hdf" => "application/x-hdf",
"js" => "application/x-javascript",
"skp" => "application/x-koan",
"skd" => "application/x-koan",
"skt" => "application/x-koan",
"skm" => "application/x-koan",
"latex" => "application/x-latex",
"nc" => "application/x-netcdf",
"cdf" => "application/x-netcdf",
"sh" => "application/x-sh",
"shar" => "application/x-shar",
"swf" => "application/x-shockwave-flash",
"sit" => "application/x-stuffit",
"sv4cpio" => "application/x-sv4cpio",
"sv4crc" => "application/x-sv4crc",
"tar" => "application/x-tar",
"tcl" => "application/x-tcl",
"tex" => "application/x-tex",
"texinfo" => "application/x-texinfo",
"texi" => "application/x-texinfo",
"t" => "application/x-troff",
"tr" => "application/x-troff",
"roff" => "application/x-troff",
"man" => "application/x-troff-man",
"me" => "application/x-troff-me",
"ms" => "application/x-troff-ms",
"ustar" => "application/x-ustar",
"src" => "application/x-wais-source",
"xhtml" => "application/xhtml+xml",
"xht" => "application/xhtml+xml",
"zip" => "application/zip",
"au" => "audio/basic",
"snd" => "audio/basic",
"mid" => "audio/midi",
"midi" => "audio/midi",
"kar" => "audio/midi",
"mpga" => "audio/mpeg",
"mp2" => "audio/mpeg",
"mp3" => "audio/mpeg",
"aif" => "audio/x-aiff",
"aiff" => "audio/x-aiff",
"aifc" => "audio/x-aiff",
"m3u" => "audio/x-mpegurl",
"ram" => "audio/x-pn-realaudio",
"rm" => "audio/x-pn-realaudio",
"rpm" => "audio/x-pn-realaudio-plugin",
"ra" => "audio/x-realaudio",
"wav" => "audio/x-wav",
"pdb" => "chemical/x-pdb",
"xyz" => "chemical/x-xyz",
"bmp" => "image/bmp",
"gif" => "image/gif",
"ief" => "image/ief",
"jpeg" => "image/jpeg",
"jpg" => "image/jpeg",
"jpe" => "image/jpeg",
"png" => "image/png",
"tiff" => "image/tiff",
"tif" => "image/tif",
"djvu" => "image/vnd.djvu",
"djv" => "image/vnd.djvu",
"wbmp" => "image/vnd.wap.wbmp",
"ras" => "image/x-cmu-raster",
"pnm" => "image/x-portable-anymap",
"pbm" => "image/x-portable-bitmap",
"pgm" => "image/x-portable-graymap",
"ppm" => "image/x-portable-pixmap",
"rgb" => "image/x-rgb",
"xbm" => "image/x-xbitmap",
"xpm" => "image/x-xpixmap",
"xwd" => "image/x-windowdump",
"igs" => "model/iges",
"iges" => "model/iges",
"msh" => "model/mesh",
"mesh" => "model/mesh",
"silo" => "model/mesh",
"wrl" => "model/vrml",
"vrml" => "model/vrml",
"css" => "text/css",
"html" => "text/html",
"htm" => "text/html",
"asc" => "text/plain",
"txt" => "text/plain",
"rtx" => "text/richtext",
"rtf" => "text/rtf",
"sgml" => "text/sgml",
"sgm" => "text/sgml",
"tsv" => "text/tab-seperated-values",
"wml" => "text/vnd.wap.wml",
"wmls" => "text/vnd.wap.wmlscript",
"etx" => "text/x-setext",
"xml" => "text/xml",
"xsl" => "text/xml",
"mpeg" => "video/mpeg",
"mpg" => "video/mpeg",
"mpe" => "video/mpeg",
"qt" => "video/quicktime",
"mov" => "video/quicktime",
"mxu" => "video/vnd.mpegurl",
"avi" => "video/x-msvideo",
"movie" => "video/x-sgi-movie",
"ice" => "x-conference-xcooltalk"
);
if(isset($mime_array[$ext]) and $mime_array[$ext] and !empty($mime_array[$ext]))
return $mime_array[$ext];
}
return false;
}
/** Takes in a filesystem URI and converts it to an S3 URI. */
function _get_s3_uri($file)
{
preg_match("#(wp-content.*$)#", $file, $matches);
if( isset($matches[1]) and $matches[1] and !empty($matches[1]) )
return $matches[1];
else
return $file;
}
/** Translates a unix filesystem mode to the amazon mode */
function _get_s3_perms($mode = false)
{
if($mode and !($mode % 100))
$amazon_perms = S3::ACL_PRIVATE;
else if($mode and !($mode % 10))
$amazon_perms = S3::ACL_PUBLIC_READ;
else if($mode and ($mode % 10))
$amazon_perms = S3::ACL_PUBLIC_READ_WRITE;
else // Default to public readable
$amazon_perms = S3::ACL_PUBLIC_READ;
return $amazon_perms;
}
/** Translates an amazon mode into a unix filesystem mode */
function _get_file_perms($amazon_perms)
{
if($amazon_perms == S3::ACL_PRIVATE)
return 600;
else if($amazon_perms == S3::ACL_PUBLIC_READ)
return 644;
else // S3::ACL_PUBLIC_READ_WRITE
return 666;
}
/** Get Info from Amazon about file */
function _get_info($remote_file)
{
if($w3tcconfig = $this->_get_w3_config())
{
$engine = $w3tcconfig->get_string('cdn.engine');
if($engine == 's3' or $engine == 'cf')
{
$bucket = $w3tcconfig->get_string("cdn.{$engine}.bucket");
if( is_object($this->s3Obj) )
return $this->s3Obj->getObjectInfo($bucket, $remote_file);
}
}
return false;
}
}
?>