forked from justinhunt/moodle-filter_poodll
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpoodllfilelib.php
2398 lines (1860 loc) · 75.5 KB
/
poodllfilelib.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
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* internal library of functions and constants for Poodll modules
* accessed directly by poodll flash wdgets on web pages.
* @package mod-poodllpairwork
* @category mod
* @author Justin Hunt
*
*/
/**
* Includes and requires
*/
//ob_start();
global $CFG;
define('POODLL_VIDEO_PLACEHOLDER_HASH','c2a342a0a664f2f1c4ea5387554a67caf3dd158e');
define('POODLL_AUDIO_PLACEHOLDER_HASH','e118549e4fc88836f418b6da6028f1fec571cd43');
//we need to do this, because when called from a widet, cfg is not set
//but the relative path fails from a quiz but it has already been set in that case
//, so we check before we call it, to cover both bases
if(!isset($CFG)){
require_once("../../config.php");
}
//require_once('../filter/poodll/poodllinit.php');
require_once($CFG->dirroot . "/filter/poodll/poodllinit.php");
//commented just while getting other mods working
//added for moodle 2
require_once($CFG->libdir . '/filelib.php');
$datatype = optional_param('datatype', "", PARAM_TEXT); // Type of action/data we are requesting
$contextid = optional_param('contextid', 0, PARAM_INT); // the id of the course
$courseid = optional_param('courseid', 0, PARAM_INT); // the id of the course
$moduleid = optional_param('moduleid', 0, PARAM_INT); // the id of the module
//added justin 20120803 careful here, I think $component is a php keyword or something
//it screwed the whole world
$comp = optional_param('component', "", PARAM_TEXT); // the component
$farea = optional_param('filearea', "", PARAM_TEXT); // the filearea
$itemid = optional_param('itemid', 0, PARAM_INT); // the id of the module
$hash = optional_param('hash', "", PARAM_TEXT); // file or dir hash
$requestid = optional_param('requestid', "", PARAM_TEXT); // file or dir hash
$paramone = optional_param('paramone', "", PARAM_TEXT); // nature of value depends on datatype, maybe path
$paramtwo = optional_param('paramtwo', "", PARAM_TEXT); // nature of value depends on datatype, maybe protocol
$paramthree = optional_param('paramthree', "", PARAM_TEXT); // nature of value depends on datatype, maybe filearea
//from the general recorder (mp3)
$p1 = optional_param('p1', "", PARAM_TEXT);
$p2 = optional_param('p2', "", PARAM_TEXT);
$p3 = optional_param('p3', "", PARAM_TEXT);
$p4 = optional_param('p4', "", PARAM_TEXT);
$p5 = optional_param('p5', "", PARAM_TEXT);
$filedata = optional_param('filedata', "", PARAM_TEXT);
$fileext = optional_param('fileext', "", PARAM_TEXT);
//map general recorder upload data to what we expect otherwise
if($p1!=''){
$contextid = $p2;
$comp = $p3;
$farea = $p4;
$itemid=$p5;
$paramone = $filedata;
$paramtwo = $fileext;
$paramthree = 'audio';
}
switch($datatype){
case "uploadfile":
header("Content-type: text/xml");
echo "<?xml version=\"1.0\"?>\n";
//uploadfile filedata(base64), fileextension (needs to be cleaned), blah blah
//paramone is the file data, paramtwo is the file extension, paramthree is the mediatype (audio,video, image)
//requestid is the actionid
$returnxml = uploadfile($paramone,$paramtwo, $paramthree, $requestid,$contextid, $comp, $farea,$itemid);
break;
case "poodllpluginfile":
//poodllpluginfile($contextid,$component,$filearea,$itemid,$filepath,$filename);
//lets hard code this for now, very very mild security
poodllpluginfile($contextid,"mod_assignment","submission",$itemid,"/",$paramone);
return;
case "getlast20files":
header("Content-type: text/html");
$returnxml="";
echo "hi";
getLast20Files();
break;
case "getrepodata":
header("Content-type: text/xml");
echo "<?xml version=\"1.0\"?>\n";
$returnxml=fetch_repos();
break;
case "repodirlist":
header("Content-type: text/xml");
echo "<?xml version=\"1.0\"?>\n";
$returnxml=fetch_repodirlist($paramone);
break;
case "instancedirlist":
header("Content-type: text/xml");
echo "<?xml version=\"1.0\"?>\n";
//paramone=path, paramtwo=filearea
$returnxml=fetch_instancedirlist($moduleid, $courseid, $itemid, $paramone, $paramtwo);
break;
case "legacydirlist":
header("Content-type: text/xml");
echo "<?xml version=\"1.0\"?>\n";
//paramone=path, paramtwo=filearea
$returnxml=fetch_legacydirlist($courseid);
break;
case "instancedeleteall":
header("Content-type: text/xml");
echo "<?xml version=\"1.0\"?>\n";
$returnxml=instance_deleteall($moduleid, $courseid, $itemid, $paramone, $requestid);
break;
case "instancecopyfile":
header("Content-type: text/xml");
echo "<?xml version=\"1.0\"?>\n";
//$moduleid, $courseid, $itemid, $filearea, $filepath,$newpath, $requestid)
$returnxml=instance_copyfilein($moduleid, $courseid,$itemid, $paramone, $paramtwo, $paramthree, $requestid);
break;
case "instanceduplicatefile":
header("Content-type: text/xml");
echo "<?xml version=\"1.0\"?>\n";
//module, course, itemid, filearea, filepath, (origfile)hash, reqid
$returnxml=instance_duplicatefile($moduleid, $courseid, $itemid, $paramone, $paramtwo, $hash, $requestid);
break;
case "instancedeletefile":
header("Content-type: text/xml");
echo "<?xml version=\"1.0\"?>\n";
$returnxml=instance_deletefile($hash, $requestid);
break;
case "instancefetchfileinfo":
header("Content-type: text/xml");
echo "<?xml version=\"1.0\"?>\n";
$returnxml=instance_fetchfileinfo($hash, $requestid);
break;
case "instancecreatedir":
header("Content-type: text/xml");
echo "<?xml version=\"1.0\"?>\n";
$returnxml=instance_createdir($moduleid, $courseid, $itemid, $paramone, $paramtwo, $requestid);
break;
case "instancecopydir":
header("Content-type: text/xml");
echo "<?xml version=\"1.0\"?>\n";
$returnxml=instance_copydirin($moduleid, $courseid, $itemid, $paramone, $paramtwo, $paramthree, $requestid);
break;
case "instancerenamefile":
header("Content-type: text/xml");
echo "<?xml version=\"1.0\"?>\n";
//module, course, originalhash, newfilename, copyas, reqid
$returnxml=instance_renamefile($moduleid, $courseid, $hash, $paramone, false, $requestid);
break;
case "instancecopyasfile":
header("Content-type: text/xml");
echo "<?xml version=\"1.0\"?>\n";
//module, course, originalhash, newfilename, copyas, reqid
$returnxml=instance_renamefile($moduleid, $courseid, $hash, $paramone, true, $requestid);
break;
case "getmoddata":
header("Content-type: text/xml");
echo "<?xml version=\"1.0\"?>\n";
$returnxml=getmoddata($courseid, $requestid);
break;
case "fetchrealurl":
header("Content-type: text/xml");
echo "<?xml version=\"1.0\"?>\n";
$returnxml=fetchrealurl($moduleid,$courseid, $itemid, $paramone, $paramtwo, $requestid);
break;
case "instancedownload":
//paramone=mimetype paramtwo=path paramthree=hash
instance_download($paramone,$paramtwo,$hash,$requestid);
case "instanceremotedownload":
//($contextid,$filename,$component, $filearea,$itemid, $requestid)
//e.g (15, '123456789.flv','user','draft','746337947',777777)
$returnxml=instance_remotedownload($contextid, $paramone,$paramtwo,$paramthree,$itemid,$requestid);
//move the output to here so that there is no trace of stray characters entering output before file downloaded
header("Content-type: text/xml");
echo "<?xml version=\"1.0\"?>";
//$returnxml="<hello />";
//instance_remotedownload('930884190835059.flv','user','draft',746337947);
break;
default:
return;
/*
header("Content-type: text/xml");
echo "<?xml version=\"1.0\"?>\n";
$returnxml="";
break;
*/
}
echo $returnxml;
return;
//For uploading a file diorect from an HTML5 or SWF widget
function uploadfile($filedata, $fileextension, $mediatype, $actionid,$contextid, $comp, $farea,$itemid){
global $CFG,$USER;
//setup our return object
$return=fetchReturnArray(true);
//make sure nobodyapassed in a bogey file extension
switch($fileextension){
case "mp3":
case "flv":
case "jpg":
case "png":
case "xml":
case "mov":
case "wav":
case "mp4":
case "3gpp":
case "3gp":
case "3g2":
case "aac":
case "wma":
case "wmv":
case "smf":
case "amr":
case "ogg":
break;
case "":
default:
//if we are set to FFMPEG convert,lets not muddle with the file extension
if($CFG->filter_poodll_ffmpeg && $mediatype=='audio' && $CFG->filter_poodll_audiotranscode){
//do nothing
}elseif($CFG->filter_poodll_ffmpeg && $mediatype=='video' && $CFG->filter_poodll_videotranscode){
//do nothing
}else{
if($mediatype=='video'){
$fileextension="mp4";
}elseif($mediatype=='image'){
$fileextension="jpg";
}else{
$fileextension="mp3";
}
}
}
//init our fs object
$fs = get_file_storage();
//assume a root level filepath
$filepath="/";
//make our filerecord
$record = new stdClass();
$record->filearea = $farea;
$record->component = $comp;
$record->filepath = $filepath;
$record->itemid = $itemid;
$record->license = $CFG->sitedefaultlicense;
$record->author = 'Moodle User';
$record->contextid = $contextid;
$record->userid = $USER->id;
$record->source = '';
//make filename and set it
//we are trying to remove useless junk in the draft area here
//when we know its stable, we will do the same for non images too
if($mediatype=='image'){
$filenamebase = "upfile_" . $actionid ;
}else{
$filenamebase = "upfile_" . rand(100,32767) . rand(100,32767) ;
}
$fileextension = "." . $fileextension;
$filename = $filenamebase . $fileextension;
$record->filename = $filename;
//in most cases we will be storing files in a draft area and lettign Moodle do the rest
//previously we only allowed one file in draft, but we removed that limit
/*
if($farea=='draft'){
$fs->delete_area_files($contextid,$comp,$farea,$itemid);
}
*/
//if file already exists, raise an error
if($fs->file_exists($contextid,$comp,$farea,$itemid,$filepath,$filename)){
if($mediatype=='image'){
//delete any existing draft files.
$file = $fs->get_file($contextid,$comp,$farea,$itemid,$filepath,$filename);
$file->delete();
//check there is no metadata prefixed to the base 64. From OL widgets, none, from JS yes
$metapos = strPos($filedata,",");
if($metapos >10 && $metapos <30){
$filedata = substr($filedata,$metapos+1);
}
//decode the data and store it
$xfiledata = base64_decode($filedata);
//create the file
$stored_file = $fs->create_file_from_string($record, $xfiledata);
}else{
$stored_file = false;
$return['success']=false;
array_push($return['messages'],"Already exists, file with filename:" . $filename );
}
}else{
//check there is no metadata prefixed to the base 64. From OL widgets, none, from JS yes
//if so it will look like this: data:image/png;base64,iVBORw0K
//we remove it, there must be a better way of course ...
//$metapos = strPos($filedata,";base64,");
$metapos = strPos($filedata,",");
if($metapos >10 && $metapos <30){
//$trunced = substr($filedata,0,$metapos+8);
$filedata = substr($filedata,$metapos+1);
}
//decode the data and store it in memory
$xfiledata = base64_decode($filedata);
//Determine if we need to convert and what format the conversions should take
if($CFG->filter_poodll_ffmpeg && $CFG->filter_poodll_audiotranscode && $fileextension!=".mp3" && $mediatype=="audio"){
$convext = ".mp3";
}else if($CFG->filter_poodll_ffmpeg && $CFG->filter_poodll_videotranscode && $fileextension!=".mp4" && $mediatype=="video"){
$convext = ".mp4";
}else{
$convext=false;
}
//if we need to convert with ffmpeg, get on with it
if($convext){
//determine the temp directory
if (isset($CFG->tempdir)){
$tempdir = $CFG->tempdir . "/";
}else{
//moodle 2.1 users have no $CFG->tempdir
$tempdir = $CFG->dataroot . "/temp/";
}
//actually make the file on disk so FFMPEG can get it
$ret = file_put_contents($tempdir . $filename, $xfiledata);
//if successfully saved to disk, convert
if($ret){
$do_bg_encoding = ($CFG->filter_poodll_bgtranscode_audio && $convext==".mp3") ||
($CFG->filter_poodll_bgtranscode_video && $convext==".mp4");
if($do_bg_encoding && $CFG->version>=2014051200){
$stored_file = convert_with_ffmpeg_bg($record,$tempdir,$filename,$filenamebase, $convext );
}else{
$stored_file = convert_with_ffmpeg($record,$tempdir,$filename,$filenamebase, $convext );
}
if($stored_file){
$filename=$stored_file->get_filename();
//if failed, default to using the original uploaded data
//and delete the temp file we made
}else{
$stored_file = $fs->create_file_from_string($record, $xfiledata);
if(is_readable(realpath($tempdir . $filename))){
unlink(realpath($tempdir . $filename));
}
}
//if couldn't create on disk fall back to the original data
}else{
$stored_file = $fs->create_file_from_string($record, $xfiledata);
}
//if we are not converting, then just create our moodle file entry with original file data
}else{
$stored_file = $fs->create_file_from_string($record, $xfiledata);
}
}
//if successful return filename
if($stored_file){
array_push($return['messages'],$filename );
//if unsuccessful, return error
}else{
$return['success']=false;
array_push($return['messages'],"unable to save file with filename:" . $filename );
}
//we process the result for return to browser
$xml_output=prepareXMLReturn($return, $actionid);
//we return to widget/client the result of our file operation
return $xml_output;
}
/*
* Extract an image from the video for use as splash
* image stored in same location with same name (diff ext)
* as original video file
*
*/
function get_splash_ffmpeg($videofile, $newfilename){
global $CFG, $USER;
//determine the temp directory
if (isset($CFG->tempdir)){
$tempdir = $CFG->tempdir . "/";
}else{
//moodle 2.1 users have no $CFG->tempdir
$tempdir = $CFG->dataroot . "/temp/";
}
//init our fs object
$fs = get_file_storage();
//it would be best if we could use $videofile->get_content_filehandle somehow ..
//but this works for now.
$tempvideofilepath = $tempdir . $videofile->get_filename();
$tempsplashfilepath = $tempdir . $newfilename;
$ok = $videofile->copy_content_to($tempvideofilepath);
//call on ffmpeg to create the snapshot
//$ffmpegopts = "-vframes 1 -an ";
//this takes the frame after 1 s
$ffmpegopts = "-ss 00:00:01 -vframes 1 -an ";
//if there is a version in poodll filter dir, use that
//else use ffmpeg version on path
if(file_exists($CFG->dirroot . '/filter/poodll/ffmpeg')){
$ffmpegpath = $CFG->dirroot . '/filter/poodll/ffmpeg';
}else{
$ffmpegpath = 'ffmpeg';
}
//branch logic if windows
$iswindows =(strtoupper(substr(PHP_OS, 0, 3)) === 'WIN');
$command = $ffmpegpath . " -i " . $tempvideofilepath . " " . $ffmpegopts . " " . $tempsplashfilepath;
if($iswindows){
$output = system($command, $fv);
}else{
shell_exec($command . " >/dev/null 2>/dev/null ");
}
//add the play button
//this can be done from ffmpeg, but probably not on all installs, so we do in php
if(is_readable(realpath($tempsplashfilepath))){
//provided this is not a place holder. We don't really want to confuse even more
if($videofile->get_contenthash()!=POODLL_VIDEO_PLACEHOLDER_HASH){
$bg = imagecreatefrompng($tempsplashfilepath);
$btn = imagecreatefrompng($CFG->dirroot . '/filter/poodll/pix/playbutton.png');
imagealphablending($bg, 1);
imagealphablending($btn, 1);
//bail if we failed here
if(!($bg && $btn)){return false;}
//put the button on the bg picture
imagecopy($bg, $btn, (imagesx($bg)-imagesx($btn)) / 2, (imagesy($bg)-imagesy($btn)) / 2, 0 , 0,imagesx($btn) , imagesy($btn));
$btnok = imagepng($bg, $tempsplashfilepath, 7);
}//end of if place holder
}else{
return false;
}
//initialize return value
$stored_file = false;
//Check if we could create the image
if(is_readable(realpath($tempsplashfilepath))){
//make our filerecord
$record = new stdClass();
$record->filearea = $videofile->get_filearea();
$record->component = $videofile->get_component();
$record->filepath = $videofile->get_filepath();
$record->itemid = $videofile->get_itemid();
$record->license = $CFG->sitedefaultlicense;
$record->author = 'Moodle User';
$record->contextid = $videofile->get_contextid();
$record->userid = $USER->id;
$record->source = '';
//set the image filename and call on Moodle to make a stored file from the image
$record->filename = $newfilename;
//delete the existing file if we had one
$hash = $fs->get_pathname_hash($record->contextid,
$record->component,
$record->filearea,
$record->itemid,
$record->filepath,
$record->filename);
$stored_file = $fs->get_file_by_hash($hash);
if($stored_file){
$record->filename = 'temp_' . $record->filename;
$temp_file = $fs->create_file_from_pathname($record, $tempsplashfilepath );
$stored_file->replace_file_with($temp_file);
$temp_file->delete();
}else{
//create the new file
$stored_file = $fs->create_file_from_pathname($record, $tempsplashfilepath );
}
//need to kill the two temp files here
if(is_readable(realpath($tempsplashfilepath ))){
unlink(realpath($tempsplashfilepath ));
}
if(is_readable(realpath($tempvideofilepath))){
unlink(realpath($tempvideofilepath));
}
//delete the temp file we made, regardless
}else{
if(is_readable(realpath($tempvideofile))){
unlink(realpath($tempvideofile));
}
}
//return the stored file
return $stored_file;
}
/*
* Convert a video file to a different format using ffmpeg
*
*/
function convert_with_ffmpeg_bg($filerecord, $tempdir, $tempfilename, $convfilenamebase, $convext){
global $CFG;
//init our fs object
$fs = get_file_storage();
$convfilename = $convfilenamebase . $convext;
$placeholderfilename= "convertingmessage" . $convext;
$filerecord->filename = $convfilename;
$stored_file = $fs->create_file_from_pathname($filerecord, $CFG->dirroot . '/filter/poodll/' . $placeholderfilename);
//we need this id later, to find the old draft file and remove it, in ad hoc task
$filerecord->id = $stored_file->get_id();
// set up task and add custom data
$conv_task = new \filter_poodll\task\adhoc_convert_media();
$qdata = array(
'filerecord' => $filerecord,
'filename' => $convfilename,
'tempdir' => $tempdir,
'tempfilename' => $tempfilename,
'convfilenamebase' => $convfilenamebase,
'convext' => $convext
);
$conv_task->set_custom_data($qdata);
// queue it
\core\task\manager::queue_adhoc_task($conv_task);
//error_log('queeued:' . $convfilename);
//error_log(print_r($qdata,true));
return $stored_file;
}
/*
* Convert a video file to a different format using ffmpeg
*
*/
function convert_with_ffmpeg($filerecord, $tempdir, $tempfilename, $convfilenamebase, $convext, $throwawayname = false){
global $CFG;
//init our fs object
$fs = get_file_storage();
//if use ffmpeg, then attempt to convert mp3 or mp4
$convfilename = $convfilenamebase . $convext;
//work out the options we pass to ffmpeg. diff versions supp. dioff switches
//has to be this way really.
switch ($convext){
case '.mp4':
//$ffmpegopts = "-c:v libx264 -profile:v baseline";
$ffmpegopts = $CFG->filter_poodll_ffmpeg_mp4opts;
break;
case '.mp3':
$ffmpegopts = $CFG->filter_poodll_ffmpeg_mp3opts;
break;
default:
$ffmpegopts = "";
}
//if there is a version in poodll filter dir, use that
//else use ffmpeg version on path
if(file_exists($CFG->dirroot . '/filter/poodll/ffmpeg')){
$ffmpegpath = $CFG->dirroot . '/filter/poodll/ffmpeg';
}else{
$ffmpegpath = 'ffmpeg';
}
//branch logic depending on if windows or nopt
$iswindows =(strtoupper(substr(PHP_OS, 0, 3)) === 'WIN');
$command = $ffmpegpath . " -i " . $tempdir . $tempfilename . " " . $ffmpegopts . " " . $tempdir . $convfilename;
if($iswindows){
$output = system($command, $fv);
}else{
shell_exec($command . " >/dev/null 2>/dev/null ");
}
/* About FFMPEG conv
it would be better to do the conversion in the background not here.
in that case you would place an ampersand at the end .. like this ...
" >/dev/null 2>/dev/null &");
But you have to get the information back to Moodle, and copy the file over, so the plumbing gets tough.
Right now there is no "converting message" displayed to user, but we need to do this.
*/
//Check if conversion worked
if(is_readable(realpath($tempdir . $convfilename))){
if($throwawayname){
$filerecord->filename = $throwawayname;
}else{
$filerecord->filename = $convfilename;
}
//error_log('we converted successfully');
$stored_file = $fs->create_file_from_pathname($filerecord, $tempdir . $convfilename);
//error_log('we stashed successfully');
//need to kill the two temp files here
if(is_readable(realpath($tempdir . $convfilename))){
unlink(realpath($tempdir . $convfilename));
}
if(is_readable(realpath($tempdir . $tempfilename))){
unlink(realpath($tempdir . $tempfilename));
}
$filename = $convfilename;
//if failed, set return value to FALSE
//and delete the temp file we made
}else{
$stored_file = false;
if(is_readable(realpath($tempdir . $tempfilename))){
unlink(realpath($tempdir . $tempfilename));
}
}
return $stored_file;
}//end of convert with FFMPEG
//Fetch a sub directory list for file explorer
//calls itself recursively, dangerous
function fetch_repodircontents($dir, $recursive=false){
$xml_output="";
$files = scandir($dir);
if (!empty($files)) {
foreach ($files as $afile) {
if ($afile == "." || $afile == "..") {
continue;
}
//here we encode the filename
//because the xml breaks otherwise when there arequotes etc.
$escapedafile = htmlspecialchars( $afile,ENT_QUOTES);
if(is_dir($dir."/".$afile)){
if(!$recursive){
$xml_output .= "\t<directory name='" . $escapedafile . "' />\n";
}else{
//recursive
$xml_output .= "\t<directory name='" . $escapedafile . "' >\n";
$xml_output .= fetch_repodircontents($dir."/".$afile,true);
$xml_output .= "\t</directory>";
}
}else{
$xml_output .= "\t<file name='" . $escapedafile ."' isleaf='true' "
. " filesize='" . filesize($dir . "/" . $afile)
. "' created='" . date('d M Y H:i:s', filectime($dir . "/" . $afile))
. "' modified='" . date('d M Y H:i:s', filemtime($dir . "/" . $afile))
. "' type='" . htmlspecialchars(mime_content_type ($dir . "/" . $afile),ENT_QUOTES)
. "'/>\n";
}
}
}
return $xml_output;
}
//Fetch a directory list from the repo
function fetch_repodirlist($startpath=''){
global $CFG;
global $basedir;
global $usecheckboxes;
global $id;
global $USER, $CFG;
//Handle directories
$fullpath = $CFG->{'dataroot'} . "/repository/" . $startpath;
//open xml to return
$xml_output = "<directorylist>";
/* New way which works with php5, but not is_dir : Justin */
$files = scandir($fullpath);
if (!empty($files)) {
$xml_output .= fetch_repodircontents($fullpath,true);
}
//close xml to return
$xml_output .= "</directorylist>";
//Return the data
return $xml_output;
}
//Fetch a directory list from the repo
function fetch_repos(){
global $CFG, $DB;
global $basedir;
global $usecheckboxes;
global $id;
global $USER, $CFG;
//Handle directories
$fullpath = $CFG->{'dataroot'} . DIRECTORY_SEPARATOR . "repository";
//open xml to return
$xml_output = "<repositories>";
/// USe this to extract only unique file paths that can be tacked on / add ri.contextid to hone down the permissions level
$sql = "SELECT DISTINCT(ric.value) as filepath FROM {repository} r , {repository_instances} ri , {repository_instance_config} ric WHERE r.type='filesystem' AND ric.name='fs_path' AND r.id=ri.typeid AND ri.id=ric.instanceid" ;
//or try this if above dont work
//$sql = "SELECT DISTINCT(ric.value) as filepath FROM {repository} r JOIN {repository_instances} ri ON r.id=ri.typeid JOIN {repository_instance_config} ric ON ri.id=ric.instanceid WHERE r.type='filesystem' AND ric.name='fs_path'";
//possibly could be shortened to
//$sql = "SELECT UNIQUE(ric.value) as filepath FROM {repository_instance_config} ric WHERE ric.name='fs_path'";
$records=$DB->get_records_sql($sql);
if (!empty($records)) {
foreach ($records as $record){
$adir= htmlspecialchars( $record->filepath,ENT_QUOTES);
if(is_dir($fullpath . DIRECTORY_SEPARATOR . $adir)){
if($adir != "." && $adir != ".."){
$xml_output .="<repo name='" . $adir . "' path='" . $fullpath . DIRECTORY_SEPARATOR . $adir . "'/>";
}
}
}
}
/* This way just got all the sub directories of repository
$files = scandir($fullpath);
if (!empty($files)) {
foreach ($files as $afile){
$afile = htmlspecialchars( $afile,ENT_QUOTES);
if(is_dir($fullpath . DIRECTORY_SEPARATOR . $afile)){
if($afile != "." && $afile != ".."){
$xml_output .="<repo name='" . $afile . "' path='" . $fullpath . DIRECTORY_SEPARATOR . $afile . "'/>";
}
}
}
}
*/
//close xml to return
$xml_output .= "</repositories>";
//Return the data
return $xml_output;
}
//This will fetch the contents of a module instance directory, can be recursively called
function fetch_instancedir_contents($thedir, &$thecontext, $recursive=false){
$browser = get_file_browser();
$xml_output="";
//first process subdirectories (if recursive)
if(!empty($thedir['subdirs']) && $recursive){
usort($thedir['subdirs'], "compareDirnames");
foreach ($thedir['subdirs'] as $subdir) {
//this is only necessary of you deleted the dirfile without deleting the subfiles
//ie only a dev, not real world situation
if(!array_key_exists('dirfile',$subdir)){return;}
$f = $subdir['dirfile'];
//$filename =$f->get_filename();
$filename=poodllBasename($f->get_filepath());
//$filename=basename($f->get_filepath(),"/");
/*
if ($filename == "." || $filename == "..") {
continue;
}
*/
//fetch our info object
$fileinfo = $browser->get_file_info($thecontext, $f->get_component(),$f->get_filearea(), $f->get_itemid(), $f->get_filepath(), $f->get_filename());
//If we could get an info object, process. But if we couldn't, although we have info via $f, we don't have permissions
//so we don't reveal it
if($fileinfo){
$urltofile = $fileinfo->get_url();
/* if(true){
if(!$fileinfo){
$urltofile="denied";
}else{
$urltofile = $fileinfo->get_url();
}
*/
//filehash for any delete/edit manipulations we wish to do
$hash= $f->get_pathnamehash();
//output xml for dir (escape for odd quotes that kill xml parser)
$xml_output .= "\t<directory name='" . htmlspecialchars($filename,ENT_QUOTES) ."' url='" . htmlspecialchars($urltofile,ENT_QUOTES) . "' hash='" . $hash . "'>\n";
$xml_output .= fetch_instancedir_contents($subdir,$thecontext,true);
$xml_output .= "\t</directory>";
//}else{
// $xml_output .= "<directory url='booboo' name='" . $thecontext->id .'@'. $f->get_component() .'@'. $f->get_filearea() .'@'. $f->get_itemid() .'@'. $f->get_filepath() .'@'. $f->get_filename() . "' hash='buubuu' />";
}
}
}
//then process files
$files = $thedir['files'];
if (!empty($files)) {
usort($files, "compareFilenames");
foreach ($files as $f) {
$filename =$f->get_filename();
if ($filename == "." || $filename == "..") {
continue;
}
//fetch our info object
$fileinfo = $browser->get_file_info($thecontext, $f->get_component(),$f->get_filearea(), $f->get_itemid(), $f->get_filepath(), $f->get_filename());
//get the url to the file
if($fileinfo){
$urltofile = $fileinfo->get_url();
/* if(true){
if(!$fileinfo){
$urltofile="denied";
}else{
$urltofile = $fileinfo->get_url();
}
*/
//filehash for any delete/edit manipulations we wish to do
$hash= $f->get_pathnamehash();
//create the output xml for this file/dir, we escape special characters so as not to break XML parsing
$xml_output .= "\t<file name='" . htmlspecialchars($filename,ENT_QUOTES) ."' isleaf='true' url='" .
htmlspecialchars($urltofile,ENT_QUOTES) . "' filesize='" . $f->get_filesize()
. "' created='" . date('d M Y H:i:s', $f->get_timecreated())
. "' modified='" . date('d M Y H:i:s', $f->get_timemodified())
. "' type='" . $f->get_mimetype()
. "' hash='" . $hash . "'/>\n";
}//end of if($fileinfo)
}//end of for each
}//end of if empty
return $xml_output;
}
//This is just so we can get a list of stuff in the old course legacy files area
//we can try if it works but it might not.
function fetch_legacydirlist($courseid){
$thiscontext = context_course::instance($courseid); //get_context_instance(CONTEXT_COURSE, $courseid);
$contextid = $thiscontext->id;
$fs = get_file_storage();
//set up xml to return
$xml_output = "<directorylist>\n";
$fullpath = "/$contextid/course/legacy/0/";
$file = $fs->get_file_by_hash(sha1($fullpath));
if(!$file){
$fullpath = "/$contextid/course/legacy/0";
$file = $fs->get_file_by_hash(sha1($fullpath));
}
if ($file) {
//set up xml to return
//$xml_output .= "we have a file";
$topdir = $fs->get_area_tree($file->get_contextid(), $file->get_component(), $file->get_filearea(),$file->get_itemid());
$xml_output .= fetch_instancedir_contents($topdir,$thiscontext,true);
}else{
$xml_output .= "no files " . $courseid . " " . $contextid . " " . $fullpath;
}
//close xml to return
$xml_output .= "</directorylist>";
//Return the data
return $xml_output;
}
//This will fetch the directory list of all the files
//available in a module instance (ie added from repository)
function fetch_instancedirlist($moduleid, $courseid, $itemid, $path, $filearea){
global $CFG, $DB;
//FIlter could submit submission/draft/content/intro as options here
if($filearea == "") {$filearea ="content";}
//fetch info and ids about the module calling this data
$course = $DB->get_record('course', array('id'=>$courseid));
$modinfo = get_fast_modinfo($course);
$cm = $modinfo->get_cm($moduleid);
//get filehandling objects
$browser = get_file_browser();
$fs = get_file_storage();
//set up xml to return
$xml_output = "<directorylist>\n";
//get a handle on the module context
$thiscontext = context_module::instance($moduleid); //get_context_instance(CONTEXT_MODULE,$moduleid);
$contextid = $thiscontext->id;
//fetch a list of files in this area, and sort them alphabetically
$topdir = $fs->get_area_tree($contextid, "mod_" . $cm->modname, $filearea,$itemid);
$xml_output .= $cm->modname . " " . $itemid;
//when dev/testing set the recursive flag to false if you prefer not to wait for infinite loops.
$xml_output .= fetch_instancedir_contents($topdir,$thiscontext,true);