-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStreamOnlyPlugin.php
949 lines (800 loc) · 36 KB
/
StreamOnlyPlugin.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
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
<?php
/**
* Stream Only Omeka Player Plugin
* Hooks, Filters, Options
*
* @copyright Copyright 2018 Code for Burlington
* @license http://www.gnu.org/licenses/gpl-3.0.txt GNU GPLv3
*
* Developer documentation available at
* https://github.com/codeforbtv/StreamOnly/wiki/Developer-Documentation:-Introduction
*/
require_once dirname(__FILE__) . '/helpers/StreamOnlyConstants.php';
require_once dirname(__FILE__) . '/helpers/StreamOnlyFunctions.php';
require_once dirname(__FILE__) . '/helpers/StreamOnlyItemType.php';
require_once dirname(__FILE__) . '/helpers/StreamOnlyAccess.php';
/**
* StreamOnly plugin.
*/
class StreamOnlyPlugin extends Omeka_Plugin_AbstractPlugin
{
protected $_hooks = array(
'install',
'config',
'config_form',
'uninstall',
'upgrade',
'before_save_item',
'after_save_item',
'before_delete_item',
'after_delete_item',
// 'before_save_file',
'after_save_file',
'before_delete_file',
// 'after_delete_file',
// 'before_save_element_text',
'after_save_element_text',
// 'before_delete_element_text',
'after_delete_element_text',
'admin_footer');
protected $_options = array();
protected $_filters = array();
protected static $_callbackOptions = array(
'width' => '200',
'height' => '20',
'autoplay' => false,
'controller' => true,
'loop' => false
);
protected $_soState = array(
'operation' => 'unknown',
'filelist' => array(),
'folder_elemtext_id' => NULL,
'license_elemtext_id' => NULL,
'timeout_elemtext_id' => NULL,
'folder_option' => NULL,
'license_option' => NULL,
'timeout_option' => NULL
);
// TODO This should be a class method, and be registered as a hook
// TODO Should include a warning that if there are large numbers of
// TODO Items of ItemType StreamOnly, this may take a while
public $uninstall_message = "All protected files will be moved to the default upload directory. Visitors to the site will be able to download them. All Items of Item Type StreamOnly will be set to undefined.";
/**
* Checks that the file indicated by the record should be protected
*
* @param $file - record of type File
* @return bool
*/
private function _isProtectedFile($file) {
return ($file->mime_type == "audio/mpeg");
}
/**
* Builds the path name to a directory where one might find a Protected File
* String ends in a DIRECTORY_SEPARATOR
*
* @param $which - 'Omeka' or 'StreamOnly'
* @param $folder - the SO folder, if $which = 'StreamOnly'
* @return string - the desired path
*/
private function _buildPath($which, $folder) {
switch ($which) {
case 'Omeka':
$path = FILES_DIR . DIRECTORY_SEPARATOR
. 'original' . DIRECTORY_SEPARATOR;
break;
case 'StreamOnly':
$path = DIRECTORY_SEPARATOR . $folder
. DIRECTORY_SEPARATOR;
break;
default:
// TODO Report internal error
die('Building a nonsense path');
}
return $path;
}
/**
* Check to see if the file is in the specified directory
*
* @param $file - record for a File
* @param $which - 'Omeka' or 'StreamOnly'
* @param $folder - the SO folder, if $which = 'StreamOnly'
* @return bool
*/
private function _isInDirectory($file, $which, $folder) {
return (file_exists($this->_buildPath($which, $folder) . $file->filename));
}
/**
* Moves the file from the source directory to the target directory
* It has already been found to exist in the source directory
* and not to exist in the target directory
*
* TODO Throw exception if failure
*
* @param $file - record from File Table
* @param $sourceDir - directory where the file is now
* @param $targetDir - directory where the file will be
*/
private function _moveFile($file, $sourceDir, $targetDir) {
$sourceFile = $sourceDir . $file->filename;
$targetFile = $targetDir . $file->filename;
if (!rename($sourceFile, $targetFile)) {
die('Error renaming file');
}
}
/**
* Only move those files that are protected files.
* Don't move a file if it's already in the target directory
* If a file is not in the source directory, report an error
*
* @param $item - record of the Item whose files are to be moved
* or NULL if the files to be moved are stored in $this->_soState['filelist']
* @param $source['which'] - 'Omeka' or 'StreamOnly', to be provided as input to _buildPath
* ['srcDir'] - folder in which to search for files to be moved
* @param $target['which'] - 'Omeka' or 'StreamOnly', to be provided as input to _buildPath
* ['destDir'] - folder in which to place the files to be moved
*/
private function _moveFiles($item, $source, $target) {
if ($item == NULL) {
$filelist = $this->_soState['filelist'];
} else {
$filelist = get_db()->getTable('File')->findBy(array('item_id' => $item->id));
}
foreach ($filelist as $file) {
if (!$this->_isProtectedFile($file)) continue;
if ($this->_isInDirectory($file, $target['which'], $target['dir'])) continue;
if (!$this->_isInDirectory($file, $source['which'], $source['dir']))
die("Need to move file, but can't find it"); // TODO throw exception
$sourceDir = $this->_buildPath($source['which'], $source['dir']);
$targetDir = $this->_buildPath($target['which'], $target['dir']);
$this->_moveFile($file, $sourceDir, $targetDir);
}
}
/****** PLUGIN *********************************************************/
/**
* StreamOnly plugin constructor.
*
* Stores useful info in protected variable $this->_soState
* Registers the callback function that outputs the HTML for audio/mpeg files.
*/
public function __construct()
{
parent::__construct();
// If the plugin is installed, store some useful info for optimization
$plugin = get_record('Plugin', array("name"=>SO_PLUGIN_NAME));
if ($plugin && ($plugin->active == 1)) {
$option = get_record('Option', array("name"=>OPTION_FOLDER));
if ($option) {
$this->_soState['folder_option'] = $option->value;
}
$element = get_record('Element', array("name"=>ELEMENT_FOLDER));
if ($element) {
$this->_soState['folder_elemtext_id'] = $element->id;
}
$option = get_record('Option', array("name"=>OPTION_LICENSES));
if ($option) {
$this->_soState['license_option'] = $option->value;
}
$element = get_record('Element', array("name"=>ELEMENT_LICENSE_COUNT));
if ($element) {
$this->_soState['license_elemtext_id'] = $element->id;
}
$option = get_record('Option', array("name"=>OPTION_TIMEOUT));
if ($option) {
$this->_soState['timeout_option'] = $option->value;
}
$element = get_record('Element', array("name"=>ELEMENT_TIMEOUT));
if ($element) {
$this->_soState['timeout_elemtext_id'] = $element->id;
}
}
// Register callback for display of protected files
add_file_display_callback(array('mimeTypes' => array('audio/mpeg')), 'soDisplayFile', self::$_callbackOptions);
}
/**
* Installs the StreamOnly Plugin
*
* Checks that the StreamOnly ItemType does not exist, then creates it
* Creates a table in the database to store info about SO protected audio/mpeg files
* (table can be accessed with $db->getTable('StreamOnly'))
* Initializes options with defaults for SO Items
* Creates the files/m3u/ directory, creates .htaccess and reserved.txt files in it
* Adds rule(s) to .htaccess file in Omeka root directory
*
* @param $pluginID (not used)
* @throws Omeka_Plugin_Installer_Exception
*/
public function hookInstall($pluginID) {
$db = $this->_db;
// Modify the .htaccess file so the script
// that downloads protected files can run
if (!so_update_access('add')) {
throw new Omeka_Plugin_Installer_Exception(__(
'Cannot modify the .htaccess file'));
}
// Check for existing Item Type, to avoid "duplicate name" death
$itemTypeList = $db->getTable('ItemType')->findBy(array('name'=>SO_ITEM_TYPE_NAME));
if (!empty($itemTypeList)) {
throw new Omeka_Plugin_Installer_Exception(__(
'The Item Type %s already exists', SO_ITEM_TYPE_NAME));
}
// Create the StreamOnly Item Type
$add_elements = so_elements();
insert_item_type(
array(
'name'=> SO_ITEM_TYPE_NAME,
'description' => SO_ITEM_TYPE_DESCRIPTION
),
$add_elements
);
// Create a table for the items of type StreamOnly
$sql = "
CREATE TABLE IF NOT EXISTS `$db->StreamOnlyModel` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`item_id` int(10) unsigned NOT NULL,
`so_directory` mediumtext COLLATE utf8_unicode_ci NOT NULL,
`so_licenses` int(10) unsigned NOT NULL,
`so_timeout` int(10) unsigned NOT NULL,
PRIMARY KEY (`id`),
KEY `item_id` (`item_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci";
$db->query($sql);
// Default number of simultaneous streams (determined by licensing)
set_option(OPTION_LICENSES, DEFAULT_LICENSES);
// Default path to the directory where StreamOnly audio files are stored
set_option(OPTION_FOLDER, DEFAULT_FOLDER);
// Number of seconds after which temporary files granting access to protected audio files can be deleted
set_option(OPTION_TIMEOUT, DEFAULT_TIMEOUT);
// Create the m3u directory under the files directory, and create the .htaccess file there
$m3uDir = FILES_DIR . DIRECTORY_SEPARATOR . SO_PLAYLIST;
if (!mkdir($m3uDir, 0700)) {
throw new Omeka_Plugin_Installer_Exception(__(
'Could not create the playlist directory'));
}
// TODO Report and recover from errors
// create the .htaccess file to protect the .m3u files
file_put_contents($m3uDir . DIRECTORY_SEPARATOR . HTACCESS, SO_DENY_ACCESS);
// Create an empty file for storing list of reserved files
file_put_contents($m3uDir . DIRECTORY_SEPARATOR . SO_RESERVED_FILES, "");
// create the file with the custom msg
file_put_contents($m3uDir . DIRECTORY_SEPARATOR . SO_CUSTOM_MSG_FILE, SO_CUSTOM_MSG_TEXT);
}
/**
* Uninstalls the plugin
*
* Removes the files/m3u/directory and all its files
* Removes options created by hookInstall() for storing defaults
* Removes the statements in the .htaccess file in the Omeka root directory
* that were added by hookInstall()
* Removes the table in the database with info about SO protected audio/mpeg files
* For each Item of Item Type StreamOnly
* Moves all files from the SO folder to the Omeka default uploads folder
* Resets the Item Type to NULL (undefined)
* Removes the Item Type StreamOnly
*
* Leaves in place the Elements unique to SO Item Type
* Leaves in place the Element Texts for Items that were of SO Item Type
*
* @param $args
* @throws Omeka_Plugin_Installer_Exception
*/
public function hookUninstall($args)
{
$db = get_db();
// undo the changes hookInstall() made to the .htaccess file
if (!so_update_access('remove')) {
throw new Omeka_Plugin_Installer_Exception(__(
'Cannot modify the .htaccess file'));
}
// Remove all files from the files/m3u/ directory, then delete the directory
$m3uDir = FILES_DIR . DIRECTORY_SEPARATOR . SO_PLAYLIST. DIRECTORY_SEPARATOR;
$dir = opendir($m3uDir);
if (!$dir) {
throw new Omeka_Plugin_Installer_Exception(__(
'Cannot find playlist directory'));
}
while ($file = readdir($dir)) { // TODO Deal with errors
unlink($m3uDir . $file); // TODO Deal with errors
}
rmdir($m3uDir); // TODO Deal with errors
// Remove the option values from the db table
delete_option(OPTION_LICENSES);
delete_option(OPTION_FOLDER);
delete_option(OPTION_TIMEOUT);
// Remove the references to the Item Type from all Items as needed,
// and move protected files from the SO folder to the upload folder
// Remove the StreamOnly Item Type, and associated Item Type Elements
// Get the StreamOnly ItemType record
$itemType = get_record('ItemType', array('name'=>SO_ITEM_TYPE_NAME));
if ($itemType == NULL) {
// Someone deleted the StreamOnly Item Type.
throw new Omeka_Plugin_Installer_Exception(__(
'Cannot complete uninstall; StreamOnly Item Type has been deleted'));
}
// TODO This could return a very large # of Items
// TODO Consider iterating using get_records()
// TODO Consider putting a warning in $uninstall_message()
// TODO https://omeka.readthedocs.io/en/latest/Reference/libraries/globals/get_records.html
// Get all the Items of ItemType StreamOnly
$itemList = $db->getTable('Item')->findBy(array('item_type_id'=>$itemType->id));
// For each Item, move the protected files from the StreamOnly folder to the default Omeka uploads folder
$target = array ('which'=>'Omeka', 'dir'=>"");
foreach ($itemList as $item) {
$soRecord = get_record('StreamOnlyModel', array('item_id' => $item->id));
$source = array('which' => 'StreamOnly', 'dir' => $soRecord['so_directory']);
$this->_moveFiles($item, $source, $target);
}
// Delete all the ItemTypesElements rows joined to this type
// findBy() does not seem to be implemented for this Table
// TODO Consider using get_records()
// TODO https://omeka.readthedocs.io/en/latest/Reference/libraries/globals/get_records.html
$itemTypesElementsList = $db->getTable('ItemTypesElements')->findBySql('item_type_id = ?', array( (int) $itemType->id));
foreach ($itemTypesElementsList as $itemTypeElement) {
$itemTypeElement->delete();
}
// For Items of this type set the Item Type to "undefined"
$db->update($db->Item, array('item_type_id' => null),
array('item_type_id = ?' => $itemType->id));
// Delete the StreamOnly Item Type
$itemType->delete();
// Delete the table used for the items of type StreamOnly
$sql = "DROP TABLE IF EXISTS `$db->StreamOnlyModel`";
$db->query($sql);
}
/**
* Called to display the Configuration Form
*
* @params - none
* @returns - none
*/
public function hookConfigForm()
{
include 'config_form.php';
}
/**
* Fires after the Configuration Form is filled out
*
* TODO How to report validation errors?
*
* @param - none
* @global - uses the $_POST server global variable
*/
public function hookConfig()
{
// should be an integer
$defaultLicenses = $_POST[OPTION_LICENSES];
// should be a valid directory with permissions 0700 or less restrictive
// length of path + filename should be less than 255 chars
$defaultFolder = $_POST[OPTION_FOLDER];
// should be an integer
$timeout = $_POST[OPTION_TIMEOUT];
// not sure how to validate - maybe anything goes?
$customMessage = $_POST[OPTION_CUSTOM_MSG];
set_option(OPTION_LICENSES, $defaultLicenses);
set_option(OPTION_FOLDER, $defaultFolder);
set_option(OPTION_TIMEOUT, $timeout);
file_put_contents(FILES_DIR . DIRECTORY_SEPARATOR .
SO_PLAYLIST . DIRECTORY_SEPARATOR .
SO_CUSTOM_MSG_FILE,
$customMessage);
}
/**
* Do any database conversions needed
* Reserved for future use
*/
public function hookUpgrade()
{
return;
}
/****** ITEMS **********************************************************/
/**
* Fires before the record is saved in the Item table
*
* TODO Some validation of the ItemTypeElements specific to
* TODO Items of ItemType StreamOnly would be helpful
*
* @param $args
* ['record'] record of type Item
* ['post'] array or FALSE [DO NOT USE]
* ['insert'] bool (true if record is being inserted)
*/
public function hookBeforeSaveItem($args) {
// check that this Item needs to be processed by the StreamOnly plugin
$item = $args['record'];
if (!isSOItem($item)) return;
$itemType = get_record('ItemType', array('name'=>SO_ITEM_TYPE_NAME));
$soRecord = ($args['insert'] ? false : get_record(SO_PLUGIN_MODEL, array("item_id"=>$item->id)));
// Why are we saving this Item's record?
if ($args['insert']) {
$this->_soState['operation'] = "insert";
} else if (($item->item_type_id == $itemType->id) && !$soRecord) {
$this->_soState['operation'] = "+StreamOnly";
} else if (($item->item_type_id != $itemType->id) && $soRecord) {
$this->_soState['operation'] = "-StreamOnly";
} else {
$this->_soState['operation'] = "update";
}
// Move files in filelist as appropriate for the scenario
switch ($this->_soState['operation']) {
// move protected files in filelist from Omeka to soRecord
case 'update':
case '-StreamOnly':
// move protected files in filelist from Omeka to soRecord
$source = array('which'=>'Omeka', 'dir' =>'');
$target = array('which'=>'StreamOnly',
'dir'=>$soRecord->so_directory);
$this->_moveFiles(NULL, $source, $target);
break;
// move protected files in filelist from Omeka to soOption
case '+StreamOnly':
// move protected files in filelist from Omeka to soOption
$source = array('which'=>'Omeka', 'dir' =>'');
$target = array('which'=>'StreamOnly',
'dir'=>$this->_soState['folder_option']);
$this->_moveFiles(NULL, $source, $target);
break;
// no action required
case 'insert': // $this->_soState['filelist'] is always empty
case 'delete': // $this->_soState['filelist'] is always empty
break;
}
}
/**
* Fires after the record is saved in the Item table
*
* @param $args
* ['record'] record of type Item
* ['post'] array or FALSE [DO NOT USE]
* ['insert'] bool (true if record is being inserted)
* @throws Omeka_Record_Exception
* @throws Omeka_Validate_Exception
*/
public function hookAfterSaveItem($args) {
// check that this Item needs to be processed by the StreamOnly plugin
$item = $args['record'];
if (!isSOItem($item)) return;
// Finalize the state of the StreamOnlyModel record for this Item
switch ($this->_soState['operation']) {
// Create SORecord based on ElemText/SOOption
case 'insert':
case '+StreamOnly':
$soRecord = new StreamOnlyModel(get_db());
$soRecord->item_id = $item->id;
// If the user specified a directory for this item, use it, otherwise use the default
$soDirectory = get_record('ElementText',
array("record_id"=>$item->id, "element_id"=>$this->_soState['folder_elemtext_id']));
if ($soDirectory) {
$soRecord->so_directory = $soDirectory->text;
} else {
$soRecord->so_directory = $this->_soState['folder_option'];
}
// If the user specified # licenses for this item, use it, otherwise use the default
$soLicenses = get_record('ElementText',
array("record_id"=>$item->id, "element_id"=>$this->_soState['license_elemtext_id']));
if ($soLicenses) {
$soRecord->so_licenses = $soLicenses->text;
} else {
$soRecord->so_licenses = $this->_soState['license_option'];
}
// If the user specified a timeout for this item, use it, otherwise use the default
$soTimeout = get_record('ElementText',
array("record_id"=>$item->id, "element_id"=>$this->_soState['timeout_elemtext_id']));
if ($soTimeout) {
$soRecord->so_timeout = $soTimeout->text;
} else {
$soRecord->so_timeout = $this->_soState['timeout_option'];
}
// TODO This might be where ElementText records get created so that next
// TODO time the user edits this record, values show up in the form fields
$soRecord->save();
break;
// move protected files from SORecord to Omeka and delete SORecord;
case '-StreamOnly':
$soRecord = get_record(SO_PLUGIN_MODEL, array("item_id"=>$item->id));
$source = array('which'=>'StreamOnly',
'dir'=>$soRecord->so_directory);
$target = array('which'=>'Omeka', 'dir' =>'');
$this->_moveFiles($item, $source, $target);
$soRecord->delete();
break;
// no action required
case 'update':
case 'delete': // This will never happen
break;
}
}
/**
* Fires before the record is deleted from the Item table
*
* @param $args['record'] record of type Item
*/
public function hookBeforeDeleteItem($args) {
// check that this Item needs to be processed by the StreamOnly plugin
$item = $args['record'];
if (!isSOItem($item)) return;
// We are deleting the Item's record
$this->_soState['operation'] = "delete";
}
/**
* Fires after the record is deleted from the Item table
*
* @param $args['record'] record of type Item
*/
public function hookAfterDeleteItem($args) {
// check that this Item needs to be processed by the StreamOnly plugin
$item = $args['record'];
if (!isSOItem($item)) return;
// Delete the record for this Item found in the StreamOnlyModel table
get_record(SO_PLUGIN_MODEL, array('item_id'=>$item->id))->delete();
}
/***** FILES ******************************************************/
/**
* Fires after the record is saved in the Files table
*
* @param $args
* ['record'] record of type File
* ['post'] array or FALSE [DO NOT USE]
* ['insert'] bool (true if record is being inserted)
*/
public function hookAfterSaveFile($args) {
$file = $args['record'];
// hookBeforeSaveItem() has not yet been called, so it is not known
// if the file should be moved, or where it should be moved to
// Add it to the list, to be processed later.
if (($this->_soState['operation'] == 'unknown') && $args['insert']) {
$this->_soState['filelist'][] = $file;
return;
}
// check that this Item needs to be processed by the StreamOnly plugin
$item = $file->getItem();
if (!isSOItem($item)) return;
// check that this file is one we need to protect against downloading
if (!$this->_isProtectedFile($file)) return;
// Leave the file in the appropriate place
switch ($this->_soState['operation']) {
// move protected file from Omeka to SOOption
case 'insert':
if (!$args['insert']) break; // no action the first time we're called
case '+StreamOnly':
$sourceDir = $this->_buildPath('Omeka', '');
$targetDir = $this->_buildPath('StreamOnly', $this->_soState['folder_option']);
$this->_moveFile($file, $sourceDir, $targetDir);
break;
//if $args['insert'] move protected file from Omeka to SORecord/SOOption
case 'update':
if ($args['insert']) {
$sourceDir = $this->_buildPath('Omeka', '');
$soRecord = get_record(SO_PLUGIN_MODEL, array('item_id' => $item->id));
if ($soRecord) {
$targetDir = $this->_buildPath('StreamOnly', $soRecord->so_directory);
} else {
$targetDir = $this->_buildPath('StreamOnly', $this->_soState['folder_option']);
}
$this->_moveFile($file, $sourceDir, $targetDir);
}
break;
// no action needed
case '-StreamOnly':
case 'delete': // never happens
break;
}
}
/**
* Fires before the record is deleted from the Files table
*
* @param $args['record'] record of type File
*/
public function hookBeforeDeleteFile($args)
{
$file = $args['record'];
// check that this Item needs to be processed by the StreamOnly plugin
$item = $file->getItem();
if (!isSOItem($item)) return;
// check that this file is one we need to protect against downloading
if (!$this->_isProtectedFile($file)) return;
// Leave the file in the right place
switch ($this->_soState['operation']) {
// move protected file from SORecord to Omeka
case 'unknown':
case 'update':
case 'delete':
case '-StreamOnly':
$soRecord = get_record(SO_PLUGIN_MODEL, array('item_id'=>$item->id));
$sourceDir = $this->_buildPath('StreamOnly', $soRecord->so_directory);
$targetDir = $this->_buildPath('Omeka', '');
$this->_moveFile($file, $sourceDir, $targetDir);
break;
// move protected file from SOOption to Omeka
case '+StreamOnly':
$sourceDir = $this->_buildPath('StreamOnly', $this->_soState['folder_option']);
$targetDir = $this->_buildPath('Omeka', '');
$this->_moveFile($file, $sourceDir, $targetDir);
break;
// no action required
case 'insert': // never happens
break;
}
}
/****** ELEMENT TEXTS ********************************************/
/**
* Fires after the record is saved to the ItemTypesElement table
*
* @param $args
* ['record'] record of type ElementText
* ['post'] array or FALSE [DO NOT USE]
* ['insert'] bool (true if record is being inserted)
*/
public function hookAfterSaveElementText($args) {
// Get the ElementTexts record
$elemText = $args['record'];
// Check that this ElementText is associated with an Item
if ($elemText->record_type != "Item") return;
// check that this Item needs to be processed by the StreamOnly plugin
$item = get_record_by_id('Item', $elemText->record_id);
if (!isSOItem($item)) return;
// Get the Element Type
$elemType = get_record_by_id("Element", $elemText->element_id)->name;
switch ($elemType) {
case ELEMENT_FOLDER:
switch ($this->_soState['operation']) {
// if (SOElemText != SOOption) move protected files from SOOption to SOElemText
case 'insert':
case '+StreamOnly':
// If needed, move protected files from SOOption to SOElemText
if ($elemText->text != $this->_soState['folder_option']) {
$source = array("which"=>'StreamOnly', "dir"=>$this->_soState['folder_option']);
$target = array("which"=>'StreamOnly', "dir"=>$elemText->text);
$this->_moveFiles($item, $source, $target);
}
break;
// If needed move files from SORecord to SOElemtext and update SORecord
case 'update':
$soRecord = get_record(SO_PLUGIN_MODEL, array("item_id"=>$elemText->record_id));
if ($elemText->text != $soRecord->so_directory) {
$source = array("which"=>'StreamOnly', "dir"=>$soRecord->so_directory);
$target = array("which"=>'StreamOnly', "dir"=>$elemText->text);
$this->_moveFiles($item, $source, $target);
$soRecord->so_directory = $elemText->text;
$soRecord->save();
}
break;
// no action needed
case '-StreamOnly':
case 'delete':
break;
}
break;
case ELEMENT_TIMEOUT:
switch ($this->_soState['operation']) {
case 'update':
$soRecord = get_record(SO_PLUGIN_MODEL, array("item_id"=>$elemText->record_id));
$soRecord->so_timeout = $elemText->text;
$soRecord->save();
break;
case 'insert': // SORecord doesn't exist yet, will be created by hookAfterSaveItem()
case '+StreamOnly': // SORecord doesn't exist yet, will be created by hookAfterSaveItem()
case '-StreamOnly': // no action required
case 'delete': // never happens
break;
}
break;
case ELEMENT_LICENSE_COUNT:
switch ($this->_soState['operation']) {
case 'update':
$soRecord = get_record(SO_PLUGIN_MODEL, array("item_id"=>$elemText->record_id));
$soRecord->so_licenses = $elemText->text;
$soRecord->save();
break;
case 'insert': // SORecord doesn't exist yet, will be created by hookAfterSaveItem()
case '+StreamOnly': // SORecord doesn't exist yet, will be created by hookAfterSaveItem()
case '-StreamOnly': // no action required
case 'delete': // never happens
break;
}
break;
// not a field that affects StreamOnly Items, no action required
default:
break;
}
}
/**
* Fires after the record is deleted from the ElementTexts table
*
* @param $args['record'] record from the ElementTexts table
*/
public function hookAfterDeleteElementText($args) {
// Get the ElementTexts record
$elemText = $args['record'];
// check that this Item needs to be processed by the StreamOnly plugin
$item = get_record_by_id('Item', $elemText->record_id);
if (!isSOItem($item)) return;
// Get the Element Type
$elemType = get_record_by_id("Element", $elemText->element_id)->name;
switch ($elemType) {
case ELEMENT_FOLDER:
switch ($this->_soState['operation']) {
// If needed, move protected files from SORecord to SOOption and update SORecord
case 'update':
$soRecord = get_record(SO_PLUGIN_MODEL, array("item_id"=>$elemText->record_id));
if ($soRecord->so_directory != $this->_soState['folder_option']) {
$source = array("which"=>'StreamOnly', "dir"=>$soRecord->so_directory);
$target = array("which"=>'StreamOnly', "dir"=>$this->_soState['folder_option']);
$this->_moveFiles($item, $source, $target);
$soRecord->so_directory = $this->_soState['folder_option'];
$soRecord->save();
}
break;
// no action needed
case 'insert': // never happens
case 'delete':
case '+StreamOnly':
case '-StreamOnly':
break;
}
break;
case ELEMENT_TIMEOUT:
switch ($this->_soState['operation']) {
case 'update':
// If needed update SORecord
$soRecord = get_record(SO_PLUGIN_MODEL, array("item_id"=>$elemText->record_id));
if ($soRecord->so_timeout != $this->_soState['timeout_option']) {
$soRecord->so_timeout = $this->_soState['timeout_option'];
$soRecord->save();
}
break;
// no action needed
case 'insert': // never happens
case 'delete': // no action needed
case '+StreamOnly': // no action needed
case '-StreamOnly': // no action needed
break;
}
break;
case ELEMENT_LICENSE_COUNT:
switch ($this->_soState['operation']) {
case 'update':
// If needed update SORecord
$soRecord = get_record(SO_PLUGIN_MODEL, array("item_id"=>$elemText->record_id));
if ($soRecord->so_licenses != $this->_soState['license_option']) {
$soRecord->so_licenses = $this->_soState['license_option'];
$soRecord->save();
}
break;
case 'insert': // never happens
case 'delete': // no action needed
case '+StreamOnly': // no action needed
case '-StreamOnly': // no action needed
break;
}
break;
default:
break;
}
}
/****** FOOTER ****************************************************/
/**
* This function fires when the footer is being output
* on an Admin page. It only needs to insert Javascript code
* for certain menus. The JS code will remove the ability of
* the Site Administrator to delete the StreamOnly Item Type,
* or to delete the three Item Type Elements created by the
* StreamOnly plugin. These will be deleted if/when the plugin
* is uninstalled.
*
* Typical URL for View Item Type xx menu
* http://domain/admin/item-types/show/xx
* Typical URL for Edit Item Type XX menu
* http://domain/admin/item-types/edit/xx
* where "xx" is the id of the record
* for the StreamOnly Item Type.
*
*/
public function hookAdminFooter() {
// TODO Script to update # seconds until audio file available?
// TODO You never know, it might be needed in Admin Footer
// Add JS for Show Item Type and Edit Item Type menus, if needed
if (soItemTypeNoDelete()) return;
// Add JS for Settings:Item Type Elements menu, if needed
if (soSettingsITE()) return;
}
}