ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
class.ilObjMediaObject.php
Go to the documentation of this file.
1<?php
2
28
29define("IL_MODE_ALIAS", 1);
30define("IL_MODE_OUTPUT", 2);
31define("IL_MODE_FULL", 3);
32
37{
38 public const DEFAULT_PREVIEW_SIZE = 80;
42 protected ilObjUser $user;
43 public bool $is_alias;
44 public string $origin_id;
45 public array $media_items;
46 public bool $contains_int_link;
48
49 public function __construct(
50 int $a_id = 0
51 ) {
52 global $DIC;
53
54 $this->user = $DIC->user();
55 $this->app_event_handler = $DIC["ilAppEventHandler"];
56 $this->lng = $DIC->language();
57 $this->is_alias = false;
58 $this->media_items = array();
59 $this->contains_int_link = false;
60 $this->type = "mob";
61 parent::__construct($a_id, false);
62 $this->image_converter = $DIC->fileConverters()->legacyImages();
63 $this->domain = $DIC->mediaObjects()->internal()->domain();
64 $this->manager = $this->domain->mediaObject();
65 $this->thumbs = $this->domain->thumbs();
66 }
67
68 public static function _exists(
69 int $id,
70 bool $reference = false,
71 ?string $type = null
72 ): bool {
73 if (is_int(strpos($id, "_"))) {
75 }
76
77 if (parent::_exists($id) && ilObject::_lookupType($id) === "mob") {
78 return true;
79 }
80 return false;
81 }
82
83 public function delete(): bool
84 {
85 $mob_logger = ilLoggerFactory::getLogger('mob');
86 $mob_logger->debug("ilObjMediaObject: Delete called for media object ID '" . $this->getId() . "'.");
87
88 if (!($this->getId() > 0)) {
89 return false;
90 }
91
92 $usages = $this->getUsages();
93
94 $mob_logger->debug("ilObjMediaObject: ... Found " . count($usages) . " usages.");
95
96 if (count($usages) == 0) {
97 // delete meta data of mob
98 $this->deleteMetaData();
99
100 // delete media items
102
103 // this is just to make sure, there should be no entries left at
104 // this point as they depend on the usage
105 self::handleQuotaUpdate($this);
106
107 // delete object
108 parent::delete();
109
110 $mob_logger->debug("ilObjMediaObject: ... deleted.");
111 } else {
112 foreach ($usages as $u) {
113 $mob_logger->debug("ilObjMediaObject: ... usage type:" . $u["type"] .
114 ", id:" . $u["id"] .
115 ", lang:" . ($u["lang"] ?? "") .
116 ", hist_nr:" . ($u["hist_nr"] ?? "") . ".");
117 }
118 $mob_logger->debug("ilObjMediaObject: ... not deleted.");
119 }
120 return true;
121 }
122
123 protected function beforeMDUpdateListener(string $a_element): bool
124 {
125 switch ($a_element) {
126 case 'General':
127 // Update Title and description
128 $paths = $this->domain->metadata()->learningObjectMetadata()->paths();
129 $reader = $this->domain->metadata()->learningObjectMetadata()->read(
130 0,
131 $this->getId(),
132 $this->getType(),
133 $paths->custom()->withNextStep('general')->get()
134 );
135
136 ilObject::_writeTitle($this->getId(), $reader->firstData($paths->title())->value());
137 ilObject::_writeDescription($this->getId(), $reader->firstData($paths->descriptions())->value());
138 break;
139 }
140 return false; // prevent parent from creating ilMD
141 }
142
143 protected function beforeCreateMetaData(): bool
144 {
145 $ilUser = $this->user;
146
147 $this->domain->metadata()->learningObjectMetadata()->derive()->fromBasicProperties(
148 $this->getTitle(),
149 $this->getLongDescription(),
150 $ilUser->getPref('language')
151 )->forObject(0, $this->getId(), $this->getType());
152
153 return false; // avoid parent to create md
154 }
155
156 protected function beforeUpdateMetaData(): bool
157 {
158 $paths = $this->domain->metadata()->learningObjectMetadata()->paths();
159
160 $manipulator = $this->domain->metadata()->learningObjectMetadata()
161 ->manipulate(0, $this->getId(), $this->getType())
162 ->prepareCreateOrUpdate($paths->title(), $this->getTitle());
163
164 if ($this->getDescription() !== '') {
165 $manipulator = $manipulator->prepareCreateOrUpdate($paths->firstDescription(), $this->getDescription());
166 } else {
167 $manipulator = $manipulator->prepareDelete($paths->firstDescription());
168 }
169
170 $manipulator->execute();
171
172 return false;
173 }
174
175 protected function beforeDeleteMetaData(): bool
176 {
177 // Delete meta data
178 $this->domain->metadata()->learningObjectMetadata()
179 ->deleteAll(0, $this->getId(), $this->getType());
180
181 return false;
182 }
183
184
185 public function addMediaItem(
186 ilMediaItem $a_item
187 ): void {
188 $this->media_items[] = $a_item;
189 }
190
191 public function &getMediaItems(): array
192 {
193 return $this->media_items;
194 }
195
199 public function getMediaItem(
200 string $a_purpose
201 ): ?ilMediaItem {
202 foreach ($this->media_items as $media_item) {
203 if ($media_item->getPurpose() == $a_purpose) {
204 return $media_item;
205 }
206 }
207 return null;
208 }
209
210 public function removeMediaItem(
211 string $a_purpose
212 ): void {
213 foreach ($this->media_items as $key => $media_item) {
214 if ($media_item->getPurpose() == $a_purpose) {
215 unset($this->media_items[$key]);
216 }
217 }
218 // update numbers and keys
219 $i = 1;
220 $media_items = array();
221 foreach ($this->media_items as $media_item) {
222 $media_items [$i] = $media_item;
223 $media_item->setMobId($this->getId());
224 $media_item->setNr($i);
225 $i++;
226 }
227 $this->media_items = $media_items;
228 }
229
230 public function removeAllMediaItems(): void
231 {
232 $this->media_items = array();
233 }
234
235 public function hasFullscreenItem(): bool
236 {
237 return $this->hasPurposeItem("Fullscreen");
238 }
239
243 public function hasPurposeItem(string $purpose): bool
244 {
245 if (is_object($this->getMediaItem($purpose))) {
246 return true;
247 } else {
248 return false;
249 }
250 }
251
256 public function read(): void
257 {
258 parent::read();
260 }
261
262 public function setAlias(bool $a_is_alias): void
263 {
264 $this->is_alias = $a_is_alias;
265 }
266
267 public function isAlias(): bool
268 {
269 return $this->is_alias;
270 }
271
275 public function setOriginID(string $a_id): void
276 {
277 $this->origin_id = $a_id;
278 }
279
280 public function getOriginID(): string
281 {
282 return $this->origin_id;
283 }
284
285 public function create(
286 bool $a_create_meta_data = false,
287 bool $a_save_media_items = true,
288 int $from_mob_id = 0
289 ): int {
290 $id = parent::create();
291
292 if (!$a_create_meta_data) {
293 $this->createMetaData();
294 }
295 $this->manager->create(
296 $id,
297 $this->getTitle(),
298 $from_mob_id
299 );
300
301 if ($a_save_media_items) {
302 $media_items = $this->getMediaItems();
303 for ($i = 0; $i < count($media_items); $i++) {
304 $item = $media_items[$i];
305 $item->setMobId($this->getId());
306 $item->setNr($i + 1);
307 $item->create();
308 }
309 }
310
311 self::handleQuotaUpdate($this);
312
313 $ilAppEventHandler = $this->app_event_handler;
314 $ilAppEventHandler->raise(
315 'components/ILIAS/MediaObjects',
316 'create',
317 array('object' => $this,
318 'obj_type' => 'mob',
319 'obj_id' => $this->getId())
320 );
321
322 return $id;
323 }
324
325 public function update(bool $a_upload = false): bool
326 {
327 parent::update();
328
329 if (!$a_upload) {
330 $this->updateMetaData();
331 }
332
333 // iterate all items
334 $media_items = $this->getMediaItems();
336
337 $j = 1;
338 foreach ($media_items as $key => $val) {
339 $item = $val;
340 if (is_object($item)) {
341 $item->setMobId($this->getId());
342 $item->setNr($j);
343 if ($item->getLocationType() == "Reference") {
344 $item->extractUrlParameters();
345 }
346 $item->create();
347 $j++;
348 }
349 }
350
351 self::handleQuotaUpdate($this);
352 $ilAppEventHandler = $this->app_event_handler;
353 $ilAppEventHandler->raise(
354 'components/ILIAS/MediaObjects',
355 'update',
356 array('object' => $this,
357 'obj_type' => 'mob',
358 'obj_id' => $this->getId())
359 );
360
361 return true;
362 }
363
367 protected static function handleQuotaUpdate(
368 ilObjMediaObject $a_mob
369 ): void {
370 }
371
375 public static function _getDirectory(
376 int $a_mob_id
377 ): string {
378 return ilFileUtils::getWebspaceDir() . "/" . self::_getRelativeDirectory($a_mob_id);
379 }
380
384 public static function _getRelativeDirectory(int $a_mob_id): string
385 {
386 return "mobs/mm_" . $a_mob_id;
387 }
388
392 public static function _getURL(
393 int $a_mob_id
394 ): string {
395 return ilUtil::getHtmlPath(ilFileUtils::getWebspaceDir() . "/mobs/mm_" . $a_mob_id);
396 }
397
401 public static function _lookupItemPath(
402 int $a_mob_id,
403 bool $a_url_encode = false,
404 bool $a_web = true,
405 string $a_purpose = ""
406 ): string {
407 if ($a_purpose == "") {
408 $a_purpose = "Standard";
409 }
410 $location = ilMediaItem::_lookupLocationForMobId($a_mob_id, $a_purpose);
411 if (preg_match("/https?\:/i", $location)) {
412 return $location;
413 }
414
415 if ($a_url_encode) {
416 $location = rawurlencode($location);
417 }
418
419 $path = ($a_web)
420 ? ILIAS_HTTP_PATH
421 : ".";
422
423 return $path . "/public/data/" . CLIENT_ID . "/mobs/mm_" . $a_mob_id . "/" . $location;
424 }
425
430 public function createDirectory(): void
431 {
434 if (!is_dir($path)) {
435 throw new ilMediaObjectsException("Failed to create directory $path.");
436 }
437 }
438
439 public function getFilesOfDirectory(
440 string $dir_path = ""
441 ): array {
442 return $this->manager->getFilesOfPath(
443 $this->getId(),
444 $dir_path
445 );
446 }
447
456 public function getXML(
457 int $a_mode = IL_MODE_FULL,
458 int $a_inst = 0,
459 bool $a_sign_locals = false,
460 bool $offline = false
461 ): string {
462 $ilUser = $this->user;
463 $xml = "";
464 switch ($a_mode) {
465 case IL_MODE_ALIAS:
466 $xml = "<MediaObject>";
467 $xml .= "<MediaAlias OriginId=\"il__mob_" . $this->getId() . "\"/>";
468 $media_items = $this->getMediaItems();
469 for ($i = 0; $i < count($media_items); $i++) {
470 $item = $media_items[$i];
471 $xml .= "<MediaAliasItem Purpose=\"" . $item->getPurpose() . "\">";
472
473 // Layout
474 $width = ($item->getWidth() != "")
475 ? "Width=\"" . $item->getWidth() . "\""
476 : "";
477 $height = ($item->getHeight() != "")
478 ? "Height=\"" . $item->getHeight() . "\""
479 : "";
480 $halign = ($item->getHAlign() != "")
481 ? "HorizontalAlign=\"" . $item->getHAlign() . "\""
482 : "";
483 $xml .= "<Layout $width $height $halign />";
484
485 // Caption
486 if ($item->getCaption() != "") {
487 $xml .= "<Caption Align=\"bottom\">" .
488 $this->escapeProperty($item->getCaption()) . "</Caption>";
489 }
490
491 // Text Representation
492 if ($item->getTextRepresentation() != "") {
493 $xml .= "<TextRepresentation>" .
494 $this->escapeProperty($item->getTextRepresentation()) . "</TextRepresentation>";
495 }
496
497 // Parameter
498 $parameters = $item->getParameters();
499 foreach ($parameters as $name => $value) {
500 $xml .= "<Parameter Name=\"$name\" Value=\"" . $this->escapeProperty($value) . "\"/>";
501 }
502 $xml .= $item->getMapAreasXML();
503 $xml .= "</MediaAliasItem>";
504 }
505 break;
506
507 // for output we need technical sections of meta data
508 case IL_MODE_OUTPUT:
509
510 // get first technical section
511 // $meta = $this->getMetaData();
512 $xml = "<MediaObject Id=\"il__mob_" . $this->getId() . "\">";
513
514 $media_items = $this->getMediaItems();
515 for ($i = 0; $i < count($media_items); $i++) {
516 $item = $media_items[$i];
517
518 $xml .= "<MediaItem Purpose=\"" . $item->getPurpose() . "\">";
519 if ($a_sign_locals && $item->getLocationType() == "LocalFile") {
520 // pre irss file
521 if (is_file($this->getDataDirectory() . "/" . $item->getLocation())) {
522 $location = ilWACSignedPath::signFile($this->getDataDirectory() . "/" . $item->getLocation());
523 $location = substr($location, strrpos($location, "/") + 1);
524 } else {
525 if ($offline) {
526 $location = $item->getLocation();
527 } else {
528 $location = $this->manager->getLocalSrc(
529 $this->getId(),
530 $item->getLocation()
531 );
532 }
533 }
534 } else {
535 $location = trim($item->getLocation());
536 // irss
537 if ($item->getLocationType() === "LocalFile" &&
538 !is_file($this->getDataDirectory() . "/" . $item->getLocation())) {
539 $location = $this->manager->getLocalSrc(
540 $this->getId(),
541 $item->getLocation()
542 );
543 }
544 if ($item->getLocationType() != "LocalFile") { //#25941
546 }
547 }
548
549 $xml .= "<Location Type=\"" . $item->getLocationType() . "\">" .
550 $this->handleAmps($location) . "</Location>";
551
552 // Format
553 $xml .= "<Format>" . $item->getFormat() . "</Format>";
554
555 // Layout
556 $width = ($item->getWidth() != "")
557 ? "Width=\"" . $item->getWidth() . "\""
558 : "";
559 $height = ($item->getHeight() != "")
560 ? "Height=\"" . $item->getHeight() . "\""
561 : "";
562 $halign = ($item->getHAlign() != "")
563 ? "HorizontalAlign=\"" . $item->getHAlign() . "\""
564 : "";
565 $xml .= "<Layout $width $height $halign />";
566
567 // Caption
568 if ($item->getCaption() != "") {
569 $xml .= "<Caption Align=\"bottom\">" .
570 $this->escapeProperty($item->getCaption()) . "</Caption>";
571 }
572
573 // Text Representation
574 if ($item->getTextRepresentation() != "") {
575 $xml .= "<TextRepresentation>" .
576 $this->escapeProperty($item->getTextRepresentation()) . "</TextRepresentation>";
577 }
578
579 // Title
580 if ($this->getTitle() != "") {
581 $xml .= "<Title>" .
582 $this->escapeProperty($this->getTitle()) . "</Title>";
583 }
584
585 // Parameter
586 $parameters = $item->getParameters();
587 if ($item->getFormat() === "video/vimeo") {
588 $parameters = ilExternalMediaAnalyzer::extractVimeoParameters($item->getLocation());
589 }
590 foreach ($parameters as $name => $value) {
591 $xml .= "<Parameter Name=\"$name\" Value=\"" . $this->escapeProperty($value) . "\"/>";
592 }
593 $xml .= $item->getMapAreasXML();
594
595 // Subtitles
596 if ($item->getPurpose() == "Standard") {
597 $this->manager->generateMissingVTT($this->getId());
598 $srts = $this->getSrtFiles(true);
599 foreach ($srts as $srt) {
600 $def = "";
601 $meta_lang = "";
602 if ($ilUser->getLanguage() != $meta_lang &&
603 $ilUser->getLanguage() == $srt["language"]) {
604 $def = ' Default="true" ';
605 }
606 $xml .= "<Subtitle File=\"" . $srt["src"] .
607 "\" Language=\"" . $srt["language"] . "\" " . $def . "/>";
608 }
609 }
610 if ($this->getVideoPreviewPic(true)) {
611 $xml .= "<PreviewPic File=\"" . $this->getVideoPreviewPic(true) .
612 "\" />";
613 }
614 if ($item->getLocationType() == "LocalFile") {
615 $lpos = strrpos($location, "/");
616 $base_url = substr($location, 0, $lpos);
617 $xml .= "<Url Base=\"" . $base_url .
618 "\" />";
619 }
620 $xml .= "</MediaItem>";
621 }
622 break;
623
624 // full xml for export
625 case IL_MODE_FULL:
626 $xml = "<MediaObject>";
627
628 // Title and Identifier
629 $xml .= "<Identifier Entry=\"il_" . IL_INST_ID . "_mob_" . $this->getId() . "\"/>";
630 if ($this->getTitle() != "") {
631 $xml .= "<Title>" .
632 $this->escapeProperty($this->getTitle()) . "</Title>";
633 }
634
635 $media_items = $this->getMediaItems();
636 for ($i = 0; $i < count($media_items); $i++) {
637 $item = $media_items[$i];
638
639 // highlight mode
640 $xml .= "<MediaItem Purpose=\"" . $item->getPurpose() . "\">";
641
642 // Location
643 $xml .= "<Location Type=\"" . $item->getLocationType() . "\">" .
644 $this->handleAmps($item->getLocation()) . "</Location>";
645
646 // Format
647 $xml .= "<Format>" . $item->getFormat() . "</Format>";
648
649 // Layout
650 $width = ($item->getWidth() != "")
651 ? "Width=\"" . $item->getWidth() . "\""
652 : "";
653 $height = ($item->getHeight() != "")
654 ? "Height=\"" . $item->getHeight() . "\""
655 : "";
656 $halign = ($item->getHAlign() != "")
657 ? "HorizontalAlign=\"" . $item->getHAlign() . "\""
658 : "";
659 $xml .= "<Layout $width $height $halign />";
660
661 // Caption
662 if ($item->getCaption() != "") {
663 $xml .= "<Caption Align=\"bottom\">" .
664 str_replace("&", "&amp;", $item->getCaption()) . "</Caption>";
665 }
666
667 // Text Representation
668 if ($item->getTextRepresentation() != "") {
669 $xml .= "<TextRepresentation>" .
670 str_replace("&", "&amp;", $item->getTextRepresentation()) . "</TextRepresentation>";
671 }
672
673 // Parameter
674 $parameters = $item->getParameters();
675 foreach ($parameters as $name => $value) {
676 $xml .= "<Parameter Name=\"$name\" Value=\"$value\"/>";
677 }
678 $xml .= $item->getMapAreasXML(true, $a_inst);
679 $xml .= "</MediaItem>";
680 }
681 break;
682 }
683 $xml .= "</MediaObject>";
684 return $xml;
685 }
686
690 protected function escapeProperty(
691 string $a_value
692 ): string {
693 return htmlspecialchars($a_value);
694 }
695
696
700 public function handleAmps(
701 string $a_str
702 ): string {
703 $a_str = str_replace("&amp;", "&", $a_str);
704 $a_str = str_replace("&", "&amp;", $a_str);
705 return $a_str;
706 }
707
708 public function exportXML(
709 ilXmlWriter $a_xml_writer,
710 int $a_inst = 0
711 ): void {
712 $a_xml_writer->appendXML($this->getXML(IL_MODE_FULL, $a_inst));
713 }
714
715
724 public function exportFiles(
725 string $a_target_dir
726 ): void {
727 $subdir = "il_" . IL_INST_ID . "_mob_" . $this->getId();
728 ilFileUtils::makeDir($a_target_dir . "/objects/" . $subdir);
729
730 $mobdir = ilFileUtils::getWebspaceDir() . "/mobs/mm_" . $this->getId();
731 ilFileUtils::rCopy($mobdir, $a_target_dir . "/objects/" . $subdir);
732 }
733
734 public function modifyExportIdentifier(
735 string $a_tag,
736 string $a_param,
737 string $a_value
738 ): string {
739 if ($a_tag == "Identifier" && $a_param == "Entry") {
740 $a_value = ilUtil::insertInstIntoID($a_value);
741 }
742
743 return $a_value;
744 }
745
746
748 // EDIT METHODS: these methods act on the media alias in the dom
750
755 public function setContainsIntLink(
756 bool $a_contains_link
757 ): void {
758 $this->contains_int_link = $a_contains_link;
759 }
760
765 public function containsIntLink(): bool
766 {
767 return $this->contains_int_link;
768 }
769
770 public static function _deleteAllUsages(
771 string $a_type,
772 int $a_id,
773 ?int $a_usage_hist_nr = 0,
774 string $a_lang = "-"
775 ): void {
776 global $DIC;
777
778 $ilDB = $DIC->database();
779
780 $and_hist = "";
781 if (!is_null($a_usage_hist_nr)) {
782 $and_hist = " AND usage_hist_nr = " . $ilDB->quote($a_usage_hist_nr, "integer");
783 }
784
785 $mob_ids = array();
786 $set = $ilDB->query("SELECT id FROM mob_usage" .
787 " WHERE usage_type = " . $ilDB->quote($a_type, "text") .
788 " AND usage_id = " . $ilDB->quote($a_id, "integer") .
789 " AND usage_lang = " . $ilDB->quote($a_lang, "text") .
790 $and_hist);
791 while ($row = $ilDB->fetchAssoc($set)) {
792 $mob_ids[] = $row["id"];
793 }
794
795 $q = "DELETE FROM mob_usage WHERE usage_type = " .
796 $ilDB->quote($a_type, "text") .
797 " AND usage_id= " . $ilDB->quote($a_id, "integer") .
798 " AND usage_lang = " . $ilDB->quote($a_lang, "text") .
799 $and_hist;
800 $ilDB->manipulate($q);
801
802 foreach ($mob_ids as $mob_id) {
803 self::handleQuotaUpdate(new self($mob_id));
804 }
805 }
806
810 public static function _getMobsOfObject(
811 string $a_type,
812 int $a_id,
813 int $a_usage_hist_nr = 0,
814 string $a_lang = "-"
815 ): array {
816 global $DIC;
817
818 $ilDB = $DIC->database();
819
820 $lstr = "";
821 if ($a_lang != "") {
822 $lstr = " AND usage_lang = " . $ilDB->quote($a_lang, "text");
823 }
824 $hist_str = "";
825 if ($a_usage_hist_nr > 0) {
826 $hist_str = " AND usage_hist_nr = " . $ilDB->quote($a_usage_hist_nr, "integer");
827 }
828
829 $q = "SELECT * FROM mob_usage WHERE " .
830 "usage_type = " . $ilDB->quote($a_type, "text") . " AND " .
831 "usage_id = " . $ilDB->quote($a_id, "integer") .
832 $lstr . $hist_str;
833 $mobs = array();
834 $mob_set = $ilDB->query($q);
835 while ($mob_rec = $ilDB->fetchAssoc($mob_set)) {
836 $mob_id = (int) $mob_rec['id'];
837 if (ilObject::_lookupType($mob_id) === "mob") {
838 $mobs[$mob_id] = $mob_id;
839 }
840 }
841
842 return $mobs;
843 }
844
848 public static function _saveUsage(
849 int $a_mob_id,
850 string $a_type,
851 int $a_id,
852 int $a_usage_hist_nr = 0,
853 string $a_lang = "-"
854 ): void {
855 global $DIC;
856
857 $ilDB = $DIC->database();
858
859 $ilDB->replace(
860 "mob_usage",
861 array(
862 "id" => array("integer", $a_mob_id),
863 "usage_type" => array("text", $a_type),
864 "usage_id" => array("integer", $a_id),
865 "usage_lang" => array("text", $a_lang),
866 "usage_hist_nr" => array("integer", $a_usage_hist_nr)
867 ),
868 array()
869 );
870
871 self::handleQuotaUpdate(new self($a_mob_id));
872 }
873
877 public static function _removeUsage(
878 int $a_mob_id,
879 string $a_type,
880 int $a_id,
881 int $a_usage_hist_nr = 0,
882 string $a_lang = "-"
883 ): void {
884 global $DIC;
885
886 $ilDB = $DIC->database();
887
888 $q = "DELETE FROM mob_usage WHERE " .
889 " id = " . $ilDB->quote($a_mob_id, "integer") . " AND " .
890 " usage_type = " . $ilDB->quote($a_type, "text") . " AND " .
891 " usage_id = " . $ilDB->quote($a_id, "integer") . " AND " .
892 " usage_lang = " . $ilDB->quote($a_lang, "text") . " AND " .
893 " usage_hist_nr = " . $ilDB->quote($a_usage_hist_nr, "integer");
894 $ilDB->manipulate($q);
895
896 self::handleQuotaUpdate(new self($a_mob_id));
897 }
898
902 public function getUsages(
903 bool $a_include_history = true
904 ): array {
905 return self::lookupUsages($this->getId(), $a_include_history);
906 }
907
913 public static function lookupUsages(
914 int $a_id,
915 bool $a_include_history = true
916 ): array {
917 global $DIC;
918
919 $ilDB = $DIC->database();
920
921 $hist_str = "";
922 if ($a_include_history) {
923 $hist_str = ", usage_hist_nr";
924 }
925
926 // get usages in pages
927 $q = "SELECT DISTINCT usage_type, usage_id, usage_lang" . $hist_str . " FROM mob_usage WHERE id = " .
928 $ilDB->quote($a_id, "integer");
929
930 if (!$a_include_history) {
931 $q .= " AND usage_hist_nr = " . $ilDB->quote(0, "integer");
932 }
933
934 $us_set = $ilDB->query($q);
935 $ret = array();
936 while ($us_rec = $ilDB->fetchAssoc($us_set)) {
937 $ut = "";
938 $ct = 0;
939 if (is_int(strpos($us_rec["usage_type"], ":"))) {
940 $us_arr = explode(":", $us_rec["usage_type"]);
941 $ut = $us_arr[1];
942 $ct = $us_arr[0];
943 }
944
945 // check whether page exists
946 $skip = false;
947 if ($ut == "pg") {
948 if (!ilPageObject::_exists($ct, $us_rec["usage_id"])) {
949 $skip = true;
950 }
951 }
952
953 if (!$skip) {
954 $ret[] = array(
955 "type" => $us_rec["usage_type"],
956 "id" => $us_rec["usage_id"],
957 "lang" => $us_rec["usage_lang"],
958 "hist_nr" => ($us_rec["usage_hist_nr"] ?? 0)
959 );
960 }
961 }
962
963 // get usages in media pools
964 $q = "SELECT DISTINCT mep_id FROM mep_tree JOIN mep_item ON (child = obj_id) WHERE mep_item.foreign_id = " .
965 $ilDB->quote($a_id, "integer") . " AND mep_item.type = " . $ilDB->quote("mob", "text");
966 $us_set = $ilDB->query($q);
967 while ($us_rec = $ilDB->fetchAssoc($us_set)) {
968 $ret[] = array("type" => "mep",
969 "id" => $us_rec["mep_id"]);
970 }
971
972 // get usages in news items (media casts)
973 $news_usages = ilNewsItem::_lookupMediaObjectUsages($a_id);
974 foreach ($news_usages as $nu) {
975 $ret[] = $nu;
976 }
977
978
979 // get usages in map areas
980 $q = "SELECT DISTINCT mob_id FROM media_item it, map_area area " .
981 " WHERE area.item_id = it.id " .
982 " AND area.link_type = " . $ilDB->quote("int", "text") . " " .
983 " AND area.target = " . $ilDB->quote("il__mob_" . $a_id, "text");
984 $us_set = $ilDB->query($q);
985 while ($us_rec = $ilDB->fetchAssoc($us_set)) {
986 $ret[] = array("type" => "map",
987 "id" => $us_rec["mob_id"]);
988 }
989
990 // get usages in personal clipboards
991 $users = ilObjUser::_getUsersForClipboadObject("mob", $a_id);
992 foreach ($users as $user) {
993 $ret[] = array("type" => "clip",
994 "id" => $user);
995 }
996
997 return $ret;
998 }
999
1004 public static function getParentObjectIdForUsage(
1005 array $a_usage,
1006 bool $a_include_all_access_obj_ids = false
1007 ): ?int {
1008 $cont_type = "";
1009 if (is_int(strpos($a_usage["type"], ":"))) {
1010 $us_arr = explode(":", $a_usage["type"]);
1011 $type = $us_arr[1];
1012 $cont_type = $us_arr[0];
1013 } else {
1014 $type = $a_usage["type"];
1015 }
1016
1017 $id = $a_usage["id"];
1018 $obj_id = null;
1019
1020 switch ($type) {
1021 // RTE / tiny mce
1022 case "html":
1023
1024 switch ($cont_type) {
1025 case "qpl":
1026 // Question Pool *Question* Text (Test)
1027 global $DIC;
1028 $qinfo = $DIC->testQuestion()->getGeneralQuestionProperties($id);
1029 if ($qinfo->getOriginalId() > 0) {
1030 $obj_id = ilObjTest::_lookupTestObjIdForQuestionId($id); // usage in test
1031 } else {
1032 $obj_id = (int) ($qinfo["obj_fi"] ?? 0); // usage in pool
1033 }
1034 break;
1035
1036 case "spl":
1037 // Question Pool *Question* Text (Survey)
1039 if ($quest) {
1040 $parent_id = $quest->getObjId();
1041
1042 // pool question copy - find survey, do not use pool itself
1043 if ($quest->getOriginalId() &&
1044 ilObject::_lookupType($parent_id) == "spl") {
1046 }
1047 // original question (in pool or survey)
1048 else {
1049 $obj_id = (int) $parent_id;
1050 }
1051
1052 unset($quest);
1053 }
1054 break;
1055
1056 case "exca":
1057 // Exercise assignment
1058 $returned_pk = $a_usage['id'];
1059 // #15995 - we are just checking against exercise object
1060 $obj_id = ilExSubmission::lookupExerciseIdForReturnedId($returned_pk);
1061 break;
1062
1063 case "frm":
1064 // Forum
1065 $post_pk = $a_usage['id'];
1066 $oPost = new ilForumPost($post_pk);
1067 $frm_pk = $oPost->getForumId();
1068 $obj_id = ilForum::_lookupObjIdForForumId($frm_pk);
1069 break;
1070
1071
1072 case "frm~d":
1073 $draft_id = $a_usage['id'];
1074 $oDraft = ilForumPostDraft::newInstanceByDraftId($draft_id);
1075
1076 $frm_pk = $oDraft->getForumId();
1077 $obj_id = ilForum::_lookupObjIdForForumId($frm_pk);
1078 break;
1079 case "frm~h":
1080 $history_id = $a_usage['id'];
1081 $oHistoryDraft = new ilForumDraftsHistory($history_id);
1082 $oDraft = ilForumPostDraft::newInstanceByDraftId($oHistoryDraft->getDraftId());
1083
1084 $frm_pk = $oDraft->getForumId();
1085 $obj_id = ilForum::_lookupObjIdForForumId($frm_pk);
1086 break;
1087 // temporary items (per user)
1088 case "frm~":
1089 case "exca~":
1090 $obj_id = (int) $a_usage['id'];
1091 break;
1092
1093 // "old" category pages
1094 case "cat":
1095 // InfoScreen Text
1096 case "tst":
1097 case "svy":
1098 // data collection
1099 case "dcl":
1100 $obj_id = (int) $id;
1101 break;
1102 }
1103 break;
1104
1105 // page editor
1106 case "pg":
1107
1108 switch ($cont_type) {
1109 // question feedback // parent obj id is q id
1110 case "qfbg":
1111 case "qpl":
1112
1113 if ($cont_type == "qfbg") {
1115 }
1116
1117 // Question Pool Question Pages
1118 global $DIC;
1119 $qinfo = $DIC->testQuestion()->getGeneralQuestionProperties($id);
1120 if ($qinfo->getOriginalId() > 0) {
1121 $obj_id = ilObjTest::_lookupTestObjIdForQuestionId($id); // usage in test
1122 } else {
1123 $obj_id = $qinfo["obj_fi"]; // usage in pool
1124 }
1125 if ($obj_id == 0) { // this is the case, if question is in learning module -> get lm id
1127 if ($pinfo && $pinfo["parent_type"] == "lm") {
1128 $obj_id = ilLMObject::_lookupContObjID($pinfo["page_id"]);
1129 }
1130 }
1131 break;
1132
1133 case "lm":
1134 // learning modules
1136 break;
1137
1138 case "term":
1139 $term_id = $id;
1140 $obj_id = ilGlossaryTerm::_lookGlossaryID($term_id);
1141 break;
1142
1143 case "wpg":
1144 // wiki page
1146 break;
1147
1148 case "sahs":
1149 // sahs page
1150 // can this implementation be used for other content types, too?
1151 $obj_id = ilPageObject::lookupParentId($id, 'sahs');
1152 break;
1153
1154 case "prtf":
1155 // portfolio
1157 break;
1158
1159 case "prtt":
1160 // portfolio template
1162 break;
1163
1164
1165 case "impr":
1166 // imprint page - always id 1
1167 // fallthrough
1168
1169 case "copa":
1170 case "cstr":
1171 $obj_id = $id;
1172 break;
1173
1174 default:
1175 $obj_id = ilPageObject::lookupParentId($id, $cont_type);
1176 break;
1177 }
1178 break;
1179
1180 // Media Pool
1181 case "mep":
1182 $obj_id = $id;
1183 break;
1184
1185 // News Context Object (e.g. MediaCast)
1186 case "news":
1188 break;
1189 }
1190
1191 return $obj_id;
1192 }
1193
1197 public static function _resizeImage(
1198 string $a_file,
1199 int $a_width,
1200 int $a_height,
1201 bool $a_constrain_prop = false
1202 ): string {
1203 global $DIC;
1204 $file_path = pathinfo($a_file);
1205 $location = substr($file_path["basename"], 0, strlen($file_path["basename"]) -
1206 strlen($file_path["extension"]) - 1) . "_" .
1207 $a_width . "_" .
1208 $a_height . "." . $file_path["extension"];
1209 $target_file = $file_path["dirname"] . "/" .
1210 $location;
1211
1212 $returned_target_file = $DIC->fileConverters()
1213 ->legacyImages()
1214 ->resizeToFixedSize(
1215 $a_file,
1216 $target_file,
1217 $a_width,
1218 $a_height,
1219 $a_constrain_prop
1220 );
1221
1222 if ($returned_target_file !== $target_file) {
1223 throw new RuntimeException('Could not resize image');
1224 }
1225
1226 return $location;
1227 }
1228
1232 public static function getMimeType(
1233 string $a_file,
1234 bool $a_external = false
1235 ): string {
1236 $mime = MimeType::lookupMimeType($a_file, MimeType::APPLICATION__OCTET_STREAM, $a_external);
1237 return $mime;
1238 }
1239
1240 public static function _determineWidthHeight(
1241 string $a_format,
1242 string $a_type,
1243 string $a_file,
1244 string $a_reference,
1245 bool $a_constrain_proportions,
1246 bool $a_use_original,
1247 ?int $a_user_width = null,
1248 ?int $a_user_height = null
1249 ): array {
1250 global $DIC;
1251
1252 $lng = $DIC->language();
1253 $size = [];
1254 $wr = 0;
1255 $hr = 0;
1256 $width = 0;
1257 $height = 0;
1258
1259 // determine width and height of known image types
1260 //$width = 640;
1261 //$height = 360;
1262 $info = "";
1263
1264 /*
1265 if ($a_format == "audio/mpeg") {
1266 $width = 300;
1267 $height = 20;
1268 }*/
1269
1270 if (ilUtil::deducibleSize($a_format)) {
1271 if ($a_type == "File") {
1272 $size = ilMediaImageUtil::getImageSize($a_file);
1273 } else {
1274 $size = ilMediaImageUtil::getImageSize($a_reference);
1275 }
1276 }
1277
1278 if (!isset($size[0])) {
1279 $size[0] = 0;
1280 }
1281 if (!isset($size[1])) {
1282 $size[1] = 0;
1283 }
1284
1285 if ($a_use_original) {
1286 if ($size[0] > 0 && $size[1] > 0) {
1287 //$width = $size[0];
1288 //$height = $size[1];
1289 $width = "";
1290 $height = "";
1291 } else {
1292 $info = $lng->txt("cont_could_not_determine_resource_size");
1293 }
1294 } else {
1295 $w = $a_user_width;
1296 $h = $a_user_height;
1297 $width = $w;
1298 $height = $h;
1299 //echo "<br>C-$width-$height-";
1300 if (ilUtil::deducibleSize($a_format) && $a_constrain_proportions) {
1301 if ($size[0] > 0 && $size[1] > 0) {
1302 if ($w > 0) {
1303 $wr = $size[0] / $w;
1304 }
1305 if ($h > 0) {
1306 $hr = $size[1] / $h;
1307 }
1308 //echo "<br>+".$wr."+".$size[0]."+".$w."+";
1309 //echo "<br>+".$hr."+".$size[1]."+".$h."+";
1310 $r = max($wr, $hr);
1311 if ($r > 0) {
1312 $width = (int) round($size[0] / $r);
1313 $height = (int) round($size[1] / $r);
1314 }
1315 }
1316 }
1317 //echo "<br>D-$width-$height-";
1318 }
1319 //echo "<br>E-$width-$height-";
1320
1321 if ($width == 0 && is_null($a_user_width)) {
1322 $width = "";
1323 }
1324 if ($height == 0 && is_null($a_user_height)) {
1325 $height = "";
1326 }
1327 return array("width" => $width, "height" => $height, "info" => $info);
1328 }
1329
1330 public function getDataDirectory(): string
1331 {
1332 return ilFileUtils::getWebspaceDir() . "/mobs/mm_" . $this->getId();
1333 }
1334
1340 public static function _saveTempFileAsMediaObject(
1341 string $name,
1342 string $tmp_name,
1343 bool $upload = true
1344 ): ilObjMediaObject {
1345 // create dummy object in db (we need an id)
1346 $media_object = new ilObjMediaObject();
1347 $media_object->setTitle($name);
1348 $media_object->setDescription("");
1349 $media_object->create();
1350
1351 // determine and create mob directory, move uploaded file to directory
1352 $media_object->createDirectory();
1353 $mob_dir = ilObjMediaObject::_getDirectory($media_object->getId());
1354
1355 $file = $mob_dir . "/" . $name;
1356 if ($upload) {
1357 $media_item = $media_object->addMediaItemFromLegacyUpload(
1358 "Standard",
1359 $tmp_name,
1360 $name,
1361 0,
1362 0,
1363 true,
1364 true
1365 );
1366 } else {
1367 $media_item = $media_object->addMediaItemFromLocalFile(
1368 "Standard",
1369 $tmp_name,
1370 $name
1371 );
1372 /*
1373 $media_item = new ilMediaItem();
1374 $media_object->addMediaItem($media_item);
1375 $media_item->setPurpose("Standard");
1376
1377 copy($tmp_name, $file);
1378 // get mime type
1379 $format = ilObjMediaObject::getMimeType($file);
1380 $media_item->setFormat($format);
1381 $location = $name;
1382 $media_item->setLocation($location);
1383 $media_item->setLocationType("LocalFile");*/
1384 }
1385
1386 // set real meta and object data
1387 $media_object->setTitle($name);
1388 $media_object->setDescription($media_item->getFormat());
1389 $media_item->setHAlign("Left");
1390
1391 /*
1392 self::renameExecutables($mob_dir);
1393 ilMediaSvgSanitizer::sanitizeDir($mob_dir); // see #20339
1394 */
1395
1396 $media_object->update();
1397
1398 return $media_object;
1399 }
1400
1402 string $purpose,
1403 string $tmp_name,
1404 string $name,
1405 int $resize_width = 0,
1406 int $resize_height = 0,
1407 bool $constrain_proportions = true,
1408 bool $deduce_size = false
1409 ): \ilMediaItem {
1410 $media_item = new ilMediaItem();
1411 $this->addMediaItem($media_item);
1412 $media_item->setPurpose($purpose);
1413 //$location = self::fixFilename($_FILES[$upload_name]['name']);
1414 $location = $name;
1415 $this->manager->addFileFromLegacyUpload($this->getId(), $tmp_name);
1416
1417 // get mime type
1418 $format = self::getMimeType($location, true);
1419
1420 // resize standard images
1421 if ($resize_width > 0 && $resize_height > 0 && is_int(strpos($format, "image"))) {
1422 /*
1423 $location = ilObjMediaObject::_resizeImage(
1424 $file,
1425 $resize_width,
1426 $resize_height,
1427 $constrain_proportions
1428 );*/
1429 }
1430
1431 if ($deduce_size) {
1432 /*
1433 if (ilUtil::deducibleSize($format)) {
1434 $size = ilMediaImageUtil::getImageSize($file);
1435 $media_item->setWidth($size[0]);
1436 $media_item->setHeight($size[1]);
1437 }*/
1438 }
1439
1440 // set real meta and object data
1441 $media_item->setFormat($format);
1442 $media_item->setLocation($location);
1443 $media_item->setLocationType("LocalFile");
1444 if ($purpose === "Standard") {
1445 $this->generatePreviewPic(320, 240);
1446 }
1447 return $media_item;
1448 }
1449
1450 public function addMediaItemFromUpload(
1451 string $purpose,
1452 UploadResult $result,
1453 string $upload_hash = "",
1454 ): \ilMediaItem {
1455 $media_item = new ilMediaItem();
1456 $this->addMediaItem($media_item);
1457 $media_item->setPurpose($purpose);
1458 $this->manager->addFileFromUpload($this->getId(), $result);
1459
1460 // get mime type
1461 $format = self::getMimeType($result->getName(), true);
1462
1463 // set real meta and object data
1464 $media_item->setFormat($format);
1465 $media_item->setLocation($result->getName());
1466 $media_item->setLocationType("LocalFile");
1467 if ($upload_hash !== "") {
1468 $media_item->setUploadHash($upload_hash);
1469 }
1470 if ($purpose === "Standard") {
1471 $this->generatePreviewPic(320, 240);
1472 }
1473 return $media_item;
1474 }
1475
1477 string $purpose,
1478 string $tmp_name,
1479 string $name
1480 ): \ilMediaItem {
1481 $media_item = new ilMediaItem();
1482 $this->addMediaItem($media_item);
1483 $media_item->setPurpose($purpose);
1484 $location = $name;
1485 $this->manager->addFileFromLocal($this->getId(), $tmp_name, $name);
1486
1487 // get mime type
1488 $format = self::getMimeType($location, true);
1489
1490 // set real meta and object data
1491 $media_item->setFormat($format);
1492 $media_item->setLocation($location);
1493 $media_item->setLocationType("LocalFile");
1494 if ($purpose === "Standard") {
1495 $this->generatePreviewPic(320, 240);
1496 }
1497 return $media_item;
1498 }
1499
1501 string $purpose,
1502 UploadResult $result,
1503 string $upload_hash = "",
1504 ): \ilMediaItem {
1505 $media_item = $this->getMediaItem($purpose);
1506 $this->manager->removeLocation($this->getId(), $media_item->getLocation());
1507 $this->manager->addFileFromUpload($this->getId(), $result);
1508
1509 // get mime type
1510 $format = self::getMimeType($result->getName(), true);
1511
1512 // set real meta and object data
1513 $media_item->setFormat($format);
1514 $media_item->setLocation($result->getName());
1515 $media_item->setLocationType("LocalFile");
1516 if ($upload_hash !== "") {
1517 $media_item->setUploadHash($upload_hash);
1518 }
1519 if ($purpose === "Standard") {
1520 $this->generatePreviewPic(320, 240);
1521 }
1522 return $media_item;
1523 }
1524
1528 public function uploadAdditionalFile(
1529 string $a_name,
1530 string $tmp_name,
1531 string $a_subdir = "",
1532 string $a_mode = "move_uploaded"
1533 ): void {
1534 $a_subdir = str_replace("..", "", $a_subdir);
1535 if ($a_mode == "rename") {
1536 $this->manager->addFileFromLocal(
1537 $this->getId(),
1538 $tmp_name,
1539 $a_subdir . "/" . $a_name
1540 );
1541 } else {
1542 $this->manager->addFileFromLegacyUpload(
1543 $this->getId(),
1544 $tmp_name,
1545 $a_subdir . "/" . $a_name
1546 );
1547 }
1548 }
1549
1551 UploadResult $result,
1552 string $subdir
1553 ): void {
1554 $this->manager->addFileFromUpload(
1555 $this->getId(),
1556 $result,
1557 $subdir
1558 );
1559 }
1560
1561 public function uploadSrtFile(
1562 string $a_tmp_name,
1563 string $a_language,
1564 string $a_mode = "move_uploaded"
1565 ): bool {
1566 if (is_file($a_tmp_name) && $a_language != "") {
1567 $this->uploadAdditionalFile("subtitle_" . $a_language . ".vtt", $a_tmp_name, "srt", $a_mode);
1568 return true;
1569 }
1570 return false;
1571 }
1572
1573 public function getSrtFiles(bool $vtt_only = false): array
1574 {
1575 return $this->manager->getSrtFiles($this->getId(), $vtt_only);
1576 }
1577
1581 public function makeThumbnail(
1582 string $source,
1583 string $thumbname,
1584 ): void {
1585 $format = self::getMimeType($source, true);
1586 $this->manager->generatePreview(
1587 $this->getId(),
1588 $source,
1589 true,
1590 $format,
1591 1,
1592 $thumbname
1593 );
1594 }
1595
1596 public function removeAdditionalFile(
1597 string $a_file
1598 ): void {
1599 $this->manager->removeLocation(
1600 $this->getId(),
1601 $a_file
1602 );
1603 }
1604
1605
1610 public function getLinkedMediaObjects(
1611 array $a_ignore = []
1612 ): array {
1613 $linked = array();
1614
1615 // get linked media objects (map areas)
1616 $med_items = $this->getMediaItems();
1617
1618 foreach ($med_items as $med_item) {
1619 $int_links = ilMapArea::_getIntLinks($med_item->getId());
1620 foreach ($int_links as $k => $int_link) {
1621 if ($int_link["Type"] == "MediaObject") {
1622 $l_id = ilInternalLink::_extractObjIdOfTarget($int_link["Target"]);
1623 if (ilObject::_exists($l_id)) {
1624 if (!in_array($l_id, $linked) &&
1625 !in_array($l_id, $a_ignore)) {
1626 $linked[] = $l_id;
1627 }
1628 }
1629 }
1630 }
1631 }
1632 //var_dump($linked);
1633 return $linked;
1634 }
1635
1636 public static function isTypeAllowed(
1637 string $a_type
1638 ): bool {
1639 global $DIC;
1640 return in_array($a_type, iterator_to_array(
1641 $DIC->mediaObjects()->internal()->domain()->mediaType()->getAllowedSuffixes()
1642 ), true);
1643 }
1644
1648 public function duplicate(): ilObjMediaObject
1649 {
1650 $new_obj = new ilObjMediaObject();
1651 $new_obj->setTitle($this->getTitle());
1652 $new_obj->setDescription($this->getDescription());
1653
1654 // media items
1655 foreach ($this->getMediaItems() as $key => $val) {
1656 $new_obj->addMediaItem($val);
1657 }
1658
1659 $new_obj->create(
1660 false,
1661 true,
1662 $this->getId() // "from" id
1663 );
1664
1665 // meta data
1666 $this->domain->metadata()->learningObjectMetadata()
1667 ->derive()
1668 ->fromObject(0, $this->getId(), "mob")
1669 ->forObject(0, $new_obj->getId(), "mob");
1670
1671 return $new_obj;
1672 }
1673
1674 public function uploadVideoPreviewPic(
1675 array $a_prevpic
1676 ): void {
1677 // remove old one
1678 if ($this->getVideoPreviewPic(true) != "") {
1679 $this->removeAdditionalFile($this->getVideoPreviewPic(true));
1680 }
1681
1682 $pi = pathinfo($a_prevpic["name"]);
1683 $ext = $pi["extension"];
1684 if (in_array($ext, array("jpg", "jpeg", "png"))) {
1685 $this->uploadAdditionalFile("mob_vpreview." . $ext, $a_prevpic["tmp_name"]);
1686 }
1687 }
1688
1689 public function generatePreviewPic(
1690 int $a_width,
1691 int $a_height,
1692 int $sec = 1
1693 ): void {
1695 $logger = $GLOBALS['DIC']->logger()->mob();
1696
1697 $item = $this->getMediaItem("Standard");
1698 if ($item->getFormat() === "image/svg+xml") {
1699 return;
1700 }
1701
1702 $logger->debug("Generate preview pic...");
1703 $logger->debug("..." . $item->getFormat());
1704
1705 $this->manager->generatePreview(
1706 $this->getId(),
1707 $item->getLocation(),
1708 $item->getLocationType() === "LocalFile",
1709 $item->getFormat(),
1710 $sec
1711 );
1712 }
1713
1714 public function getVideoPreviewPic(
1715 bool $a_filename_only = false
1716 ): string {
1717
1718 if (!$a_filename_only) {
1719 $src = $this->thumbs->getPreviewSrc($this->getId());
1720 if ($src !== "") {
1721 return $src;
1722 }
1723 }
1724
1725 $dir = ilObjMediaObject::_getDirectory($this->getId());
1726 $ppics = array("mob_vpreview.jpg",
1727 "mob_vpreview.jpeg",
1728 "mob_vpreview.png");
1729 $med = $this->getMediaItem("Standard");
1730 if ($med && $med->getFormat() === "image/svg+xml" && $med->getLocationType() === "LocalFile") {
1731 $ppics[] = $med->getLocation();
1732 }
1733 foreach ($ppics as $p) {
1734 if (is_file($dir . "/" . $p)) {
1735 if ($a_filename_only) {
1736 return $p;
1737 } else {
1738 return $dir . "/" . $p;
1739 }
1740 }
1741 }
1742 return "";
1743 }
1744
1748 public static function fixFilename(
1749 string $a_name
1750 ): string {
1751 $a_name = ilFileUtils::getASCIIFilename($a_name);
1752
1753 $rchars = array("`", "=", "$", "{", "}", "'", ";", " ", "(", ")");
1754 $a_name = str_replace($rchars, "_", $a_name);
1755 $a_name = str_replace("__", "_", $a_name);
1756 return $a_name;
1757 }
1758
1759
1763 public function getMultiSrtUploadDir(): string
1764 {
1765 return ilObjMediaObject::_getDirectory($this->getId()) . "/srt/tmp";
1766 }
1767
1768
1773 array $a_file
1774 ): void {
1775 $lng = $this->lng;
1776
1777 if (!is_file($a_file["tmp_name"])) {
1778 throw new ilMediaObjectsException($lng->txt("mob_file_could_not_be_uploaded"));
1779 }
1780
1781 $dir = $this->getMultiSrtUploadDir();
1782 ilFileUtils::delDir($dir, true);
1784 ilFileUtils::moveUploadedFile($a_file["tmp_name"], "multi_vtt.zip", $dir . "/" . "multi_vtt.zip");
1785 $this->domain->resources()->zip()->unzipFile($dir . "/multi_vtt.zip");
1786 }
1787
1791 public function clearMultiSrtDirectory(): void
1792 {
1793 ilFileUtils::delDir($this->getMultiSrtUploadDir());
1794 }
1795
1799 public function getMultiSrtFiles(): array
1800 {
1801 $items = array();
1802
1803 $lang_codes = $this->domain->metadata()->getLOMLanguageCodes();
1804
1805 $dir = $this->getMultiSrtUploadDir();
1806 $files = ilFileUtils::getDir($dir);
1807 foreach ($files as $k => $i) {
1808 // check directory
1809 if ($i["type"] == "file" && !in_array($k, array(".", ".."))) {
1810 if (pathinfo($k, PATHINFO_EXTENSION) == "vtt") {
1811 $lang = "";
1812 if (substr($k, strlen($k) - 7, 1) == "_") {
1813 $lang = substr($k, strlen($k) - 6, 2);
1814 if (!in_array($lang, $lang_codes)) {
1815 $lang = "";
1816 }
1817 }
1818 $items[] = array("filename" => $k, "lang" => $lang);
1819 }
1820 }
1821 }
1822 return $items;
1823 }
1824
1825 public static function renameExecutables(
1826 string $a_dir
1827 ): void {
1828 ilFileUtils::renameExecutables($a_dir);
1829 if (!self::isTypeAllowed("html")) {
1830 ilFileUtils::rRenameSuffix($a_dir, "html", "sec"); // see #20187
1831 }
1832 }
1833
1834 public function getExternalMetadata(): void
1835 {
1836 // see https://oembed.com/
1837 $st_item = $this->getMediaItem("Standard");
1838 if ($st_item->getLocationType() == "Reference") {
1839 if (ilExternalMediaAnalyzer::isVimeo($st_item->getLocation())) {
1840 $st_item->setFormat("video/vimeo");
1841 $par = ilExternalMediaAnalyzer::extractVimeoParameters($st_item->getLocation());
1843 $this->setTitle($meta["title"] ?? "");
1844 $description = str_replace("\n", "", $meta["description"] ?? "");
1845 $description = str_replace(["<br>", "<br />"], ["\n", "\n"], $description);
1846 $description = strip_tags($description);
1847 $this->setDescription($description);
1848 $st_item->setDuration((int) ($meta["duration"] ?? 0));
1849 $url = parse_url($meta["thumbnail_url"] ?? "");
1850 $file = basename($url["path"]);
1851 $ext = pathinfo($file, PATHINFO_EXTENSION);
1852 if ($ext == "") {
1853 $ext = "jpg";
1854 }
1855 $this->manager->addPreviewFromUrl(
1856 $this->getId(),
1857 $meta["thumbnail_url"],
1858 "/mob_vpreview." . $ext
1859 );
1860 }
1861 if (ilExternalMediaAnalyzer::isYoutube($st_item->getLocation())) {
1862 $st_item->setFormat("video/youtube");
1863 $par = ilExternalMediaAnalyzer::extractYoutubeParameters($st_item->getLocation());
1864 try {
1866 $this->setTitle($meta["title"] ?? "");
1867 $description = str_replace("\n", "", $meta["description"] ?? "");
1868 } catch (Exception $e) {
1869 $this->setTitle($st_item->getLocation());
1870 $description = "";
1871 }
1872 $description = str_replace(["<br>", "<br />"], ["\n", "\n"], $description);
1873 $description = strip_tags($description);
1874 $this->setDescription($description);
1875 $st_item->setDuration((int) ($meta["duration"] ?? 0));
1876 $thumbnail_url = $meta["thumbnail_url"] ?? "";
1877 $url = parse_url($thumbnail_url);
1878 if ($thumbnail_url !== "") {
1879 $mob_logger = ilLoggerFactory::getLogger('mob');
1880 $file = basename($url["path"]);
1881 $this->manager->addPreviewFromUrl(
1882 $this->getId(),
1883 $meta["thumbnail_url"],
1884 "/mob_vpreview." .
1885 pathinfo($file, PATHINFO_EXTENSION)
1886 );
1887 }
1888 }
1889 }
1890 }
1891
1892 public function getStandardSrc(): string
1893 {
1894 return $this->getLocationSrc("Standard");
1895 }
1896
1897 public function getFullscreenSrc(): string
1898 {
1899 return $this->getLocationSrc("Fullscreen");
1900 }
1901
1902 protected function getLocationSrc(string $purpose): string
1903 {
1904 return (string) $this->getMediaItem($purpose)?->getLocationSrc();
1905 }
1906}
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
$location
Definition: buildRTE.php:22
Mime type determination.
Definition: MimeType.php:30
static _lookupSurveyObjId(int $a_question_id)
static _instanciateQuestion(int $question_id)
Get question object.
return true
const IL_MODE_ALIAS
const IL_MODE_OUTPUT
const IL_MODE_FULL
static lookupExerciseIdForReturnedId(int $a_returned_id)
Get exercise from submission id (used in ilObjMediaObject)
static isVimeo(string $a_location)
Identify Vimeo links.
static extractVimeoParameters(string $a_location)
Extract Vimeo Parameter.
Class ilFileUtils.
static makeDirParents(string $a_dir)
Create a new directory and all parent directories.
static getWebspaceDir(string $mode="filesystem")
get webspace directory
static getDir(string $a_dir, bool $a_rec=false, ?string $a_sub_dir="")
get directory
static makeDir(string $a_dir)
creates a new directory and inherits all filesystem permissions of the parent directory You may pass ...
static delDir(string $a_dir, bool $a_clean_only=false)
removes a dir and all its content (subdirs and files) recursively
static rRenameSuffix(string $a_dir, string $a_old_suffix, string $a_new_suffix)
Renames all files with certain suffix and gives them a new suffix.
static moveUploadedFile(string $a_file, string $a_name, string $a_target, bool $a_raise_errors=true, string $a_mode="move_uploaded")
move uploaded file
static rCopy(string $a_sdir, string $a_tdir, bool $preserveTimeAttributes=false)
Copies content of a directory $a_sdir recursively to a directory $a_tdir.
Class ilForumDraftHistory.
static newInstanceByDraftId(int $draft_id)
static _lookupObjIdForForumId(int $a_for_id)
static _lookGlossaryID(int $term_id)
get glossary id form term id
static _lookupContObjID(int $a_id)
get learning module id for lm object
static getLogger(string $a_component_id)
Get component logger.
static _getIntLinks(int $a_item_id)
get all internal links of a media items map areas
static getImageSize(string $a_location)
Get image size from location.
Class ilMediaItem Media Item, component of a media object (file or reference)
static _getMediaItemsOfMOb(ilObjMediaObject $a_mob)
Read media items into(!) media object (static)
static deleteAllItemsOfMob(int $a_mob_id)
static _lookupLocationForMobId(int $a_mob_id, string $a_purpose)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
static _lookupMediaObjectUsages(int $a_mob_id)
Lookup media object usage(s)
static _lookupContextObjId(int $a_news_id)
Context Object ID.
replaceMediaItemFromUpload(string $purpose, UploadResult $result, string $upload_hash="",)
static getParentObjectIdForUsage(array $a_usage, bool $a_include_all_access_obj_ids=false)
Get's the repository object ID of a parent object, if possible see ilWebAccessChecker.
getSrtFiles(bool $vtt_only=false)
escapeProperty(string $a_value)
Escape property (e.g.
static _exists(int $id, bool $reference=false, ?string $type=null)
checks if an object exists in object_data
getMultiSrtFiles()
Get all srt files of srt multi upload.
static _getMobsOfObject(string $a_type, int $a_id, int $a_usage_hist_nr=0, string $a_lang="-")
MediaObjectManager $manager
exportFiles(string $a_target_dir)
export all media files of object to target directory note: target directory must be the export target...
makeThumbnail(string $source, string $thumbname,)
Make thumbnail.
modifyExportIdentifier(string $a_tag, string $a_param, string $a_value)
static _resizeImage(string $a_file, int $a_width, int $a_height, bool $a_constrain_prop=false)
Resize image and return new image file ("_width_height" string appended)
static isTypeAllowed(string $a_type)
getMultiSrtUploadDir()
Get directory for multi srt upload.
static _getURL(int $a_mob_id)
get directory for files of media object
getVideoPreviewPic(bool $a_filename_only=false)
getLocationSrc(string $purpose)
static handleQuotaUpdate(ilObjMediaObject $a_mob)
containsIntLink()
returns true, if mob was marked as containing an intern link (via setContainsIntLink) (this method sh...
static _determineWidthHeight(string $a_format, string $a_type, string $a_file, string $a_reference, bool $a_constrain_proportions, bool $a_use_original, ?int $a_user_width=null, ?int $a_user_height=null)
static _saveTempFileAsMediaObject(string $name, string $tmp_name, bool $upload=true)
getXML(int $a_mode=IL_MODE_FULL, int $a_inst=0, bool $a_sign_locals=false, bool $offline=false)
get MediaObject XLM Tag
getMediaItem(string $a_purpose)
get item for media purpose
static lookupUsages(int $a_id, bool $a_include_history=true)
Lookup usages of media object.
addAdditionalFileFromUpload(UploadResult $result, string $subdir)
static _removeUsage(int $a_mob_id, string $a_type, int $a_id, int $a_usage_hist_nr=0, string $a_lang="-")
Remove usage of mob in another container.
uploadAdditionalFile(string $a_name, string $tmp_name, string $a_subdir="", string $a_mode="move_uploaded")
Create new media object and update page in db and return new media object.
clearMultiSrtDirectory()
Clear multi srt directory.
duplicate()
Duplicate media object, return new media object.
static fixFilename(string $a_name)
Fix filename of uploaded file.
static getMimeType(string $a_file, bool $a_external=false)
get mime type for file
handleAmps(string $a_str)
Replace "&" (if not an "&") with "&".
static _lookupItemPath(int $a_mob_id, bool $a_url_encode=false, bool $a_web=true, string $a_purpose="")
Get path for item with specific purpose.
uploadSrtFile(string $a_tmp_name, string $a_language, string $a_mode="move_uploaded")
createDirectory()
Create file directory of media object.
beforeMDUpdateListener(string $a_element)
static renameExecutables(string $a_dir)
setAlias(bool $a_is_alias)
addMediaItem(ilMediaItem $a_item)
addMediaItemFromUpload(string $purpose, UploadResult $result, string $upload_hash="",)
exportXML(ilXmlWriter $a_xml_writer, int $a_inst=0)
hasPurposeItem(string $purpose)
returns whether object has media item with specific purpose
uploadVideoPreviewPic(array $a_prevpic)
addMediaItemFromLegacyUpload(string $purpose, string $tmp_name, string $name, int $resize_width=0, int $resize_height=0, bool $constrain_proportions=true, bool $deduce_size=false)
removeMediaItem(string $a_purpose)
static _getRelativeDirectory(int $a_mob_id)
Get relative (to webspace dir) directory.
create(bool $a_create_meta_data=false, bool $a_save_media_items=true, int $from_mob_id=0)
removeAdditionalFile(string $a_file)
uploadMultipleSubtitleFile(array $a_file)
Upload multi srt file.
static _getDirectory(int $a_mob_id)
Get absolute directory.
getFilesOfDirectory(string $dir_path="")
getLinkedMediaObjects(array $a_ignore=[])
Get all media objects linked in map areas of this media object.
getUsages(bool $a_include_history=true)
get all usages of current media object
addMediaItemFromLocalFile(string $purpose, string $tmp_name, string $name)
static _deleteAllUsages(string $a_type, int $a_id, ?int $a_usage_hist_nr=0, string $a_lang="-")
setContainsIntLink(bool $a_contains_link)
content parser set this flag to true, if the media object contains internal links (this method should...
static _saveUsage(int $a_mob_id, string $a_type, int $a_id, int $a_usage_hist_nr=0, string $a_lang="-")
Save usage of mob within another container (e.g.
InternalDomainService $domain
update(bool $a_upload=false)
static _lookupTestObjIdForQuestionId(int $q_id)
Get test Object ID for question ID.
User class.
static _getUsersForClipboadObject(string $a_type, int $a_id)
Class ilObject Basic functions for all objects.
static _lookupType(int $id, bool $reference=false)
static _writeDescription(int $obj_id, string $desc)
write description to db (static)
static _writeTitle(int $obj_id, string $title)
write title to db (static)
setTitle(string $title)
static _exists(int $id, bool $reference=false, ?string $type=null)
checks if an object exists in object_data
string $type
static _getPageForQuestionId(int $a_q_id, string $a_parent_type="")
static _exists(string $a_parent_type, int $a_id, string $a_lang="", bool $a_no_cache=false)
Checks whether page exists.
static lookupParentId(int $a_id, string $a_type)
static findPortfolioForPage(int $a_page_id)
Get portfolio id of page id.
Util class various functions, usage as namespace.
static deducibleSize(string $a_mime)
checks if mime type is provided by getimagesize()
static secureUrl(string $url)
static insertInstIntoID(string $a_value)
inserts installation id into ILIAS id
static signFile(string $path_to_file)
static lookupObjIdByPage(int $a_page_id)
returns the wiki/object id to a given page id
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
const CLIENT_ID
Definition: constants.php:41
const IL_INST_ID
Definition: constants.php:40
return['delivery_method'=> 'php',]
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
$info
Definition: entry_point.php:21
Interface Location.
Definition: Location.php:33
$path
Definition: ltiservices.php:30
__construct(Container $dic, ilPlugin $plugin)
@inheritDoc
global $lng
Definition: privfeed.php:31
if(!file_exists('../ilias.ini.php'))
global $DIC
Definition: shib_login.php:26
$q
Definition: shib_logout.php:23
$url
Definition: shib_logout.php:68
$GLOBALS["DIC"]
Definition: wac.php:54
$lang
Definition: xapiexit.php:25