forked from pollen8/fabrik
-
Notifications
You must be signed in to change notification settings - Fork 381
/
amazons3storage.php
583 lines (482 loc) · 11.4 KB
/
amazons3storage.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
<?php
/**
* Amazon s3 Storage adaptor for Fabrik file upload element
*
* @package Joomla
* @subpackage Fabrik
* @copyright Copyright (C) 2005-2013 fabrikar.com - All rights reserved.
* @license GNU/GPL http://www.gnu.org/copyleft/gpl.html
*/
// No direct access
defined('_JEXEC') or die('Restricted access');
require_once JPATH_ROOT . '/plugins/fabrik_element/fileupload/adaptor.php';
/**
* Amazon s3 Storage adaptor for Fabrik file upload element
*
* @package Joomla
* @subpackage Fabrik
* @since 3.0
*/
class Amazons3storage extends FabrikStorageAdaptor
{
/**
* Are we using SSL to store/retrieve files
*
* @var bool
*/
protected $ssl = false;
/**
* S3 domain
* Must have trailing slash
*
* @var string
*/
protected $domain = 's3.amazonaws.com/';
/**
* Constructor
*
* @param JRegistry &$params options
*/
public function __construct(&$params)
{
require_once COM_FABRIK_FRONTEND . '/libs/amazons3/S3.php';
$this->ssl = $params->get('fileupload_ssl', false);
$this->s3 = new S3($params->get('fileupload_aws_accesskey'), $params->get('fileupload_aws_secretkey'), $this->ssl);
parent::__construct($params);
}
/**
* Get the bucket name
*
* @return string
*/
protected function getBucketName()
{
$params = $this->getParams();
$w = new FabrikWorker;
return $w->parseMessageForPlaceHolder($params->get('fileupload_aws_bucketname', 'robclayburnsfabrik'));
}
/**
* Get upload location
*
* @return bool
*/
protected function getLocation()
{
$loc = $this->params->get('fileupload_aws_location');
return $loc == '' ? false : $loc;
}
/**
* Does a file exist
*
* @param string $filepath path to test for
*
* @return bool
*/
public function exists($filepath)
{
if (!$this->bucketExists())
{
return false;
}
$bucket = $this->getBucketName();
$filepath = str_replace("%20", " ", $filepath);
$filepath = $this->urlToPath($filepath);
$response = $this->s3->getObject($bucket, $filepath);
return $response === false ? false : true;
}
/**
* Convert a full url into a full server path
*
* @param string $url URL
*
* @see /plugins/fabrik_element/fileupload/storageAdaptor#urlToPath($url)
*
* @return string path
*/
public function urlToPath($url)
{
$prefix = $this->ssl ? 'https://' : 'http://';
$bucket = $this->getBucketName();
$url = $this->removePrependedURL($url);
if (strstr($url, $prefix))
{
// We've got the full url to the image - remove the bucket name etc to get file name
$url = str_replace($prefix, '', $url);
$url = str_replace($bucket . '.' . $this->domain, '', $url);
}
return $url;
}
/**
* Remove the full server path from the filepath
*
* @param string $filepath file path
*
* @return string
*/
private function removePrependedURL($filepath)
{
if (substr($filepath, 0, JString::strlen(COM_FABRIK_BASE)) == COM_FABRIK_BASE)
{
$filepath = Fabrikstring::ltrimword($filepath, COM_FABRIK_BASE);
}
return $filepath;
}
/**
* Does the bucket exist
*
* @return bool
*/
private function bucketExists()
{
$bucket = $this->getBucketName();
$buckets = $this->s3->listBuckets();
if (!is_array($buckets))
{
return false;
}
if (!in_array($bucket, $buckets))
{
return false;
}
return true;
}
/**
* Upload the file
*
* @param string $tmpFile tmp file location
* @param string $filepath final upload location
*
* @return bool
*/
public function upload($tmpFile, $filepath)
{
$filepath = str_replace("\\", '/', $filepath);
$bucket = $this->getBucketName();
$acl = $this->getAcl();
if (!$this->bucketExists())
{
$this->s3->putBucket($bucket, $acl, $this->getLocation());
}
// $$$ rob avoid urls like http://bucket.s3.amazonaws.com//home/users/path/to/file/Chrysanthemum.jpg
$filepath = JString::ltrim($filepath, '/');
// Move the file
if ($this->s3->putObjectFile($tmpFile, $bucket, $filepath, $acl))
{
$this->uploadedFilePath = $this->getS3BaseURL() . str_replace(" ", "%20", $filepath);
return true;
}
else
{
return false;
}
}
/**
* When creating file paths, do we need to append them with JPATH_SITE
*
* @since 3.0.6.2
*
* @return bool
*/
public function appendServerPath()
{
$params = $this->getParams();
return (bool) $params->get('fileupload_s3_serverpath', 1);
}
/**
* Build the base url for the files
*
* @return string
*/
private function getS3BaseURL()
{
$prefix = $this->ssl ? 'https://' : 'http://';
$bucket = $this->getBucketName();
return $prefix . $bucket . '.' . $this->domain;
}
/**
* Write a file
*
* @param string $file file name
* @param string $buffer the buffer to write
*
* @return bool
*/
public function write($file, $buffer)
{
$file = $this->urlToPath($file);
$file = str_replace("%20", " ", $file);
$file = str_replace("\\", '/', $file);
$bucket = $this->getBucketName();
if ($this->s3->putObject($buffer, $bucket, $file, S3::ACL_PUBLIC_READ))
{
return true;
}
else
{
return false;
}
}
/**
* Read a file
*
* @param string $filepath file path
*
* @return mixed Returns file contents or boolean False if failed
*/
public function read($filepath)
{
$file = $this->urlToPath($filepath);
$file = str_replace("%20", " ", $file);
$file = str_replace("\\", '/', $file);
$bucket = $this->getBucketName();
$s3object = $this->s3->getObject($bucket, $file);
if ($s3object === false)
{
return false;
}
return $s3object->body;
}
/**
* Get the S3 Acl setting
*
* @return string
*/
protected function getAcl()
{
$params = $this->getParams();
$acl = $params->get('fileupload_amazon_acl', 2);
switch ($acl)
{
case 1:
$acl = S3::ACL_PRIVATE;
break;
case 2:
default:
$acl = S3::ACL_PUBLIC_READ;
break;
case 3:
$acl = S3::ACL_PUBLIC_READ_WRITE;
break;
}
return $acl;
}
/**
* Does a folder exist - not applicable for S3 storage
*
* @param string $folder folder to test
*
* @return true
*/
public function folderExists($folder)
{
return true;
}
/**
* Create a folder - not applicable for S3 storage
*
* @param string $path folder path
* @param bitmask $mode Permissions
* @return bool
*/
public function createFolder($path, $mode = 0755)
{
return true;
}
/**
* Clean a path
*
* @param string $path path to clear
*
* @return string cleaned path
*/
public function clean($path)
{
$prefix = $this->ssl ? 'https://' : 'http://';
if (strstr($path, $prefix))
{
// If we are cleaning up a full url then check that fabrik hasnt unwittingly prepended the JPATH_SITE to the start of the url
$path = $this->removePrependedURL($path);
$part = Fabrikstring::ltrimword($path, $prefix);
$path = $prefix . JPath::clean($part);
}
else
{
$path = JPath::clean($path);
}
$path = str_replace("\\", '/', $path);
return $path;
}
/**
* Clean a fle name
*
* @param string $filename File name to clean
* @param int $repeatCounter Repeat group counter
*
* @return string cleaned name
*/
public function cleanName($filename, $repeatCounter)
{
$this->randomizeName($filename);
return $filename;
}
/**
* Delete a file
*
* @param string $filepath File to delete
*
* @return void
*/
public function delete($filepath)
{
$filepath = $this->urlToPath($filepath);
$this->s3->deleteObject($this->getBucketName(), $filepath);
}
/**
* Set a file's permissions
*
* @param string $filepath file to set permissions for
*
* @return string
*/
public function setPermissions($filepath)
{
// Not applicable
}
/**
* Get the file's URL
*
* @param string $file file path
*
* @return string URL
*/
public function getFileUrl($file)
{
return $file;
}
/**
* Get the thumbnail file for the file given
*
* @param string $file file path
*
* @return string thumbnail
*/
public function _getThumb($file)
{
$params = $this->getParams();
$w = new FabrikWorker;
$ulDir = COM_FABRIK_BASE . $params->get('ul_directory');
$ulDir = $this->clean($ulDir);
$ulDir = $w->parseMessageForPlaceHolder($ulDir);
$thumbdir = $this->clean(COM_FABRIK_BASE . $params->get('thumb_dir'));
$thumbdir = $w->parseMessageForPlaceHolder($thumbdir);
$file = $w->parseMessageForPlaceHolder($file);
$file = str_replace($ulDir, $thumbdir, $file);
$f = basename($file);
$dir = dirname($file);
// Jaanus added: create also thumb suffix as for filesystemstrage
$ext = JFile::getExt($f);
$fclean = JFile::stripExt($f);
$file = $dir . '/' . $params->get('thumb_prefix') . $fclean . $params->get('thumb_suffix') . '.' . $ext;
return $file;
}
/**
* Get the cropped file for the file given
*
* @param string $file main image file path
*
* @return string cropped image
*/
public function _getCropped($file)
{
$params = $this->getParams();
$w = new FabrikWorker;
$ulDir = COM_FABRIK_BASE . $params->get('ul_directory');
$ulDir = $this->clean($ulDir);
$ulDir = $w->parseMessageForPlaceHolder($ulDir);
$thumbdir = $this->clean(COM_FABRIK_BASE . $params->get('fileupload_crop_dir'));
$thumbdir = $w->parseMessageForPlaceHolder($thumbdir);
$file = $w->parseMessageForPlaceHolder($file);
$file = str_replace($ulDir, $thumbdir, $file);
$f = basename($file);
$dir = dirname($file);
$file = $dir . '/' . $f;
return $file;
}
/**
* Convert a full server path into a full url
*
* @param string $path server path
*
* @return string url
*/
public function pathToURL($path)
{
return $path;
}
/**
* Make a nested folder structure - not applicable for S3 storaga
*
* @param string $folderPath path to folder - eg /images/stories
* @param int $mode folder permissions
*
* @return void
*/
public function makeRecursiveFolders($folderPath, $mode = 0755)
{
return;
}
/**
* Get file info
*
* @param string $filepath path
*
* @return array
*/
public function getFileInfo($filepath)
{
$bucket = $this->getBucketName();
$s3Info = $this->s3->getObjectInfo($bucket, $filepath);
if ($s3Info === false)
{
return false;
}
$thisFileInfo = array('filesize' => $s3Info['size'], 'mime_type' => $s3Info['type'], 'filename' => basename($filepath));
return $thisFileInfo;
}
/**
* Get the complete folder path, including the server root
*
* @param string $filepath the file path
*
* @return string
*/
public function getFullPath($filepath)
{
$filepath = $this->urlToPath($filepath);
$filepath = str_replace("%20", " ", $filepath);
$filepath = str_replace("\\", '/', $filepath);
return $filepath;
}
/**
* Allows storage model to modify pathname just before it is rendered. For instance,
* if using Amazon S3 with 'Authenticated URL' option.
*
* @param string $filepath path to file
*
* @return string
*/
public function preRenderPath($filepath)
{
$params = $this->getParams();
if ($lifetime = (int) $params->get('fileupload_amazon_auth_url', 0))
{
$file = $this->urlToPath($filepath);
$file = str_replace("%20", " ", $file);
$file = str_replace("\\", '/', $file);
$bucket = trim($this->getBucketName());
$hostbucket = !$this->ssl;
$filepath = $this->s3->getAuthenticatedURL($bucket, $file, $lifetime, $hostbucket, $this->ssl);
}
return $filepath;
}
}