-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontainer.inc
690 lines (542 loc) · 18.8 KB
/
container.inc
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
abstract class StorageContainer {
public $container_id;
public $service_id;
public $name;
public $access_control;
public $external;
public $status;
public $settings;
public function __construct(array $fields) {
foreach ($fields as $key => $value) {
$this->$key = $value;
}
$this->settings += $this->serviceSettingsDefault();
}
public function service() {
return storage_service_load($this->service_id);
}
public function count() {
return db_select('storage_instance')
->condition('container_id', $this->container_id)
->isNotNull('file_id')
->countQuery()
->execute()
->fetchField();
}
public function size($format = FALSE) {
$query = db_select('storage_instance');
$query->join('storage_file', NULL, 'storage_instance.file_id = storage_file.file_id');
$query->addExpression('SUM(storage_file.size)', 'size');
$size = (int) $query->condition('container_id', $this->container_id)
->execute()
->fetchField();
return $format ? storage_format_byte_count($size) : $size;
}
public function status() {
switch ($this->status) {
case STORAGE_CONTAINER_STATUS_ACTIVE:
return t('Active');
break;
case STORAGE_CONTAINER_STATUS_SUSPENDED:
return t('Suspended');
break;
case STORAGE_CONTAINER_STATUS_DESTROY:
return t('Being destroyed');
break;
}
}
private function htaccessCreate() {
$lines = array(
'SetHandler Drupal_Security_Do_Not_Remove_See_SA_2006_006',
'Options None',
'Options +FollowSymLinks',
);
if ($this->access_control) {
$lines[] = 'Deny from all';
}
else {
$lines[] = '<IfModule mod_headers.c>';
$lines[] = ' Header set Content-Disposition "attachment"';
$lines[] = '</IfModule>';
}
$temp_uri = new StorageTempURI();
file_put_contents($temp_uri, implode("\n", $lines) . "\n");
$storage = new Storage(array('filename' => '.htaccess'));
try {
$this->serviceInstanceCreate($storage, $temp_uri);
}
catch (StorageException $e) {};
}
/**
* Creates a storage container.
*
* @throws StorageException
* When it wasn't possible to create the container.
*/
public function create($already_exists = FALSE) {
if (!$already_exists) {
try {
$this->serviceCreate();
}
catch (StorageException $e) {
$msg = 'Failed to create storage container @container.';
watchdog('storage', $msg, array('@container' => $this->name()), WATCHDOG_ERROR);
throw new StorageException(t($msg, array('@container' => $this->name())));
}
}
drupal_write_record('storage_container', $this);
if ($this->servicePostCreate()) {
$this->update();
}
if ($this->service()->htaccess) {
$this->htaccessCreate();
}
$msg = 'Storage container !container has been created.';
drupal_set_message(t($msg, array('!container' => $this->link())));
watchdog('storage', $msg, array('!container' => check_plain($this->name())), WATCHDOG_NOTICE,
l(t('view'), $this->path()));
}
public function name() {
return $this->service()->name . ($this->name ? (' - ' . $this->name) : '');
}
public function path() {
return 'admin/structure/storage/container/' . $this->container_id;
}
public function link() {
return l($this->name(), $this->path());
}
public function update() {
drupal_write_record('storage_container', $this, 'container_id');
}
public function flushServingContainer() {
$selector_subquery = db_select('storage_class_container');
$selector_subquery->join('storage_selector', NULL, 'storage_class_container.class_id = storage_selector.class_id');
$selector_subquery->fields('storage_selector', array('selector_id'))
->condition('storage_class_container.container_id', $this->container_id);
db_update('storage')
->expression('serving_container', 'NULL')
->condition('selector_id', $selector_subquery, 'IN')
->execute();
cache_clear_all();
}
public function instanceCreate(Storage $storage, $uri) {
if ($this->status == STORAGE_CONTAINER_STATUS_SUSPENDED || $this->external) {
throw new StorageException();
}
$reference = $this->serviceInstanceCreate($storage, $uri);
$instance = array(
'container_id' => $this->container_id,
'file_id' => $storage->file_id,
'reference' => $reference,
);
drupal_write_record('storage_instance', $instance);
$storage->flushServingContainer();
$message = 'File instance created: ' . $storage->filename . '<br />';
$message .= $this->logInfo();
$message .= $storage->logInfo();
watchdog('storage', $message, NULL);
return $reference;
}
public function instanceCopy(Storage $storage, StorageContainerInterface $source_container) {
if ($this->status == STORAGE_CONTAINER_STATUS_SUSPENDED || $this->external) {
throw new StorageException();
}
$reference = $this->serviceInstanceCopy($storage, $source_container);
$instance = array(
'container_id' => $this->container_id,
'file_id' => $storage->file_id,
'reference' => $reference,
);
drupal_write_record('storage_instance', $instance);
$storage->flushServingContainer();
$message = 'File instance copied: ' . $storage->filename . '<br />';
$message .= $this->logInfo();
$message .= $storage->logInfo();
watchdog('storage', $message, NULL);
}
public function tryInstanceCopy(Storage $storage) {
// Find instances of the storage's file in other containers of the same type
// that are in the storage's class.
$query = db_select('storage_instance', NULL, array('fetch' => PDO::FETCH_ASSOC));
$query->join('storage_container', NULL, 'storage_instance.container_id = storage_container.container_id');
$query->join('storage_class_container', NULL, 'storage_instance.container_id = storage_class_container.container_id');
$query->join('storage_selector', NULL, 'storage_class_container.class_id = storage_selector.class_id');
$result = $query->fields('storage_instance', array('container_id', 'reference'))
->condition('storage_instance.file_id', $storage->file_id)
->condition('storage_instance.container_id', $this->container_id, '<>')
->condition('storage_container.service_id', $this->service_id)
->condition('storage_selector.selector_id', $storage->selector_id)
->orderBy('storage_selector.migrating')
->orderBy('storage_class_container.weight', 'DESC')
->orderBy('storage_class_container.container_id')
->execute();
foreach ($result as $row) {
$source_container = storage_container_load($row['container_id']);
$source_container->reference = $row['reference'];
try {
$this->instanceCopy($storage, $source_container);
}
catch (StorageException $e) {
continue;
}
return TRUE;
}
return FALSE;
}
/**
* Ensure that an instance exists in the container.
*
* @param $storage
* The storage that there must be an instance of.
*
* @throws StorageException
* When it isn't possible to ensure that the instance exists.
*/
public function ensureInstanceExists(Storage $storage) {
// Do we have a file_id for the storage yet?
if ($storage->file_id) {
if ($this->instanceExists($storage)) {
return;
}
$check = FALSE;
}
else {
// We don't have a file_id yet, so we might check for an instance once we do.
$check = TRUE;
}
// Is it possible to short circuit?
if ($this->service()->copy && !$check) {
if ($this->tryInstanceCopy($storage)) {
return;
}
}
$uri = $storage->getUri();
$exists = FALSE;
// If it's a new file, there is no point in checking for an instance.
if ($check && !$storage->new_file) {
$exists = $this->instanceExists($storage);
}
if (!$exists) {
$this->instanceCreate($storage, $uri);
}
}
public function instanceExists(Storage $storage) {
return db_select('storage_instance')
->fields('storage_instance', array('reference'))
->condition('file_id', $storage->file_id)
->condition('container_id', $this->container_id)
->execute()
->fetchField();
}
/**
* Destroy an instance if it isn't required by any other storages.
*
* @param $storage
* The storage to have its instance destroyed.
* @param $reference
* The reference of the instance to be destroyed.
*
* @throws StorageException
* When the instance should be destroyed, but it isn't possible.
*/
public function instanceDestroyIfUnrequired(Storage $storage, $reference) {
// Find other storages that use the same file.
$storage_ids = db_select('storage')
->fields('storage', array('storage_id'))
->condition('storage.file_id', $storage->file_id)
->condition('storage.storage_id', $storage->storage_id, '<>')
->execute()
->fetchCol();
if (count($storage_ids)) {
// Are any of the other storages supposed to have an instance in this
// container?
$query = db_select('storage');
$query->join('storage_selector', NULL, 'storage.selector_id = storage_selector.selector_id');
$query->join('storage_class_container', NULL, 'storage_selector.class_id = storage_class_container.class_id');
$count = $query->condition('storage.storage_id', $storage_ids, 'IN')
->condition('storage_class_container.container_id', $this->container_id)
->countQuery()
->execute()
->fetchField();
if ($count > 0) {
return;
}
// Do any of the other storages have this container as their initial
// container?
$count = db_select('storage')
->condition('storage_id', $storage_ids, 'IN')
->condition('initial_container_id', $this->container_id)
->countQuery()
->execute()
->fetchField();
if ($count > 0) {
return;
}
}
$this->instanceDestroy($storage, $reference);
}
/**
* Destroy an instance in the container
*
* @param $storage
* The storage to have its instance destroyed.
* @param $reference
* The reference of the instance to be destroyed.
*
* @throws StorageException
* When it isn't possible to destroy the instance.
*/
public function instanceDestroy(Storage $storage, $reference) {
if ($this->status == STORAGE_CONTAINER_STATUS_SUSPENDED) {
throw new StorageException();
}
// Silently fail to destroy an instance in an external container.
if (!$this->external) {
$this->serviceInstanceDestroy($reference);
}
db_delete('storage_instance')
->condition('file_id', $storage->file_id)
->condition('container_id', $this->container_id)
->execute();
$message = 'File instance destroyed: ' . $storage->filename . '<br />';
$message .= $this->logInfo();
$message .= $storage->logInfo();
watchdog('storage', $message, NULL);
}
public function instanceLink($reference) {
if ($this->service()->serve) {
return l($reference, $this->serviceInstanceServe($reference, FALSE));
}
else {
return $reference;
}
}
public function classes() {
$classes = array();
$result = db_select('storage_class_container', NULL, array('fetch' => PDO::FETCH_ASSOC))
->fields('storage_class_container', array('class_id'))
->condition('container_id', $this->container_id)
->orderBy('class_id')
->execute();
foreach ($result as $class_info) {
$classes[] = storage_class_load($class_info['class_id']);
}
return $classes;
}
/**
* Delete container.
*/
public function delete() {
db_delete('storage_container')
->condition('container_id', $this->container_id)
->execute();
db_delete('storage_class_container')
->condition('container_id', $this->container_id)
->execute();
db_delete('storage_instance')
->condition('container_id', $this->container_id)
->execute();
$this->flushServingContainer();
}
/**
* Destroy container.
*/
public function destroy() {
try {
$this->serviceDestroy();
$this->delete();
}
catch (StorageException $e) {
db_update('storage_container')
->fields(array('status' => STORAGE_CONTAINER_STATUS_DESTROY))
->condition('container_id', $this->container_id)
->execute();
}
$message = 'Storage container <i>' . check_plain($this->name()) . '</i> has been destroyed.';
drupal_set_message($message);
watchdog('storage', $message, NULL);
}
/**
* Determines if a reference exists in a container.
*
* The reference may either have a record in the database, or exist in the
* container itself.
*
* @param $reference
* The reference to look for.
* @return
* Whether the reference exists in the container.
*/
public function referenceExists($reference) {
$exists = (bool) db_select('storage_instance')
->condition('container_id', $this->container_id)
->condition('reference', $reference)
->countQuery()
->execute()
->fetchField();
if ($exists) {
return TRUE;
}
return $this->serviceInstanceExists($reference);
}
/**
* Generates a unique filename for a container, based on a desired filename.
*
* @param $filename
* Desired filename.
* @return
* Unique filename.
*/
public function uniqueFilename($filename) {
if ($this->referenceExists($filename)) {
$pos = strrpos($filename, '.');
if ($pos !== FALSE) {
$name = substr($filename, 0, $pos);
$ext = substr($filename, $pos);
}
else {
$name = $filename;
$ext = '';
}
$count = 0;
do {
$filename = $name . '_' . $count++ . $ext;
} while ($this->referenceExists($filename));
}
return $filename;
}
public function logInfo() {
$info = 'container: ' . check_plain($this->name()) . ', <br />';
return $info;
}
/**
* Default implementations.
*/
public function serviceManifest(array &$sandbox, &$finished) {
if (!isset($sandbox['references'])) {
$sandbox['references'] = $this->serviceListReferences();
}
$count = 100;
$manifest = array();
while ($count-- && ($reference = array_shift($sandbox['references']))) {
try {
$info = $this->serviceInstanceCheck($reference);
}
catch (StorageException $e) {
continue;
}
$manifest[$reference] = $info;
}
$finished = (count($sandbox['references']) == 0);
return $manifest;
}
public function serviceInstanceGetContents($reference) {
// $uri can be a StorageTempURI - keep an instance of it here otherwise
// file_get_contents() will destroy it too soon.
$uri = $this->serviceInstanceGetUri($reference);
$contents = file_get_contents($uri);
if ($contents === FALSE) {
throw new StorageException();
}
return $contents;
}
public function serviceInstanceGetUri($reference) {
$temp_uri = new StorageTempURI();
$success = file_put_contents($temp_uri, $this->serviceInstanceGetContents($reference));
if (!$success) {
throw new StorageException();
}
return $temp_uri;
}
/**
* Get information about an instance.
*
* @param $reference
* The reference of the instance the be examined.
*
* @throws StorageException
* When it isn't possible to get information about the instance.
*/
public function serviceInstanceCheck($reference) {
$uri = $this->serviceInstanceGetUri($reference);
$stat = @stat($uri);
if ($stat === FALSE) {
throw new StorageException();
}
$info = array(
'uri' => $uri,
'size' => $stat['size'],
'whirlpool' => hash_file('whirlpool', $uri, TRUE),
);
return $info;
}
public function serviceInstanceServe($reference, $https) {
return $this->settings['base_url'] . '/' . $reference;
}
public function serviceInstanceOutput($reference) {
// $uri can be a StorageTempURI - keep an instance of it here otherwise
// readfile() will destroy it too soon.
$uri = $this->serviceInstanceGetUri($reference);
readfile($uri);
}
/**
* Optional implementations.
*/
public function serviceSettingsDefault() {
return array();
}
public function servicePostCreate() {}
public function serviceUpdate() {}
public function serviceMaintenance() {}
public function serviceListReferences() {
throw new StorageException();
}
public function serviceInstanceCopy(Storage $storage, StorageContainerInterface $source_container) {}
}
interface StorageContainerInterface {
public function __construct(array $fields);
public function service();
public function count();
public function size($format = FALSE);
public function status();
public function create($already_exists = FALSE);
public function name();
public function path();
public function link();
public function update();
public function flushServingContainer();
public function instanceCreate(Storage $storage, $uri);
public function instanceCopy(Storage $storage, StorageContainerInterface $source_container);
public function tryInstanceCopy(Storage $storage);
public function ensureInstanceExists(Storage $storage);
public function instanceExists(Storage $storage);
public function instanceDestroyIfUnrequired(Storage $storage, $reference);
public function instanceDestroy(Storage $storage, $reference);
public function instanceLink($reference);
public function classes();
public function delete();
public function destroy();
public function referenceExists($reference);
public function uniqueFilename($filename);
public function logInfo();
public function serviceSettingsForm($already_exists);
public function serviceSettingsValidate($already_exists);
public function serviceCreate();
public function servicePostCreate();
public function serviceInfo();
public function serviceUpdate();
public function serviceDestroy();
public function serviceMaintenance();
public function serviceListReferences();
public function serviceManifest(array &$sandbox, &$finished);
public function serviceInstanceCreate(Storage $storage, $uri);
public function serviceInstanceCopy(Storage $storage, StorageContainerInterface $source_container);
public function serviceInstanceExists($reference);
public function serviceInstanceDestroy($reference);
public function serviceInstanceGetContents($reference);
public function serviceInstanceGetUri($reference);
public function serviceInstanceCheck($reference);
public function serviceInstanceServe($reference, $https);
public function serviceInstanceOutput($reference);
}