ILIAS  release_8 Revision v8.24
class.ilMediaItem.php
Go to the documentation of this file.
1<?php
2
25{
26 protected string $tried_thumb = "";
27 protected string $text_representation = "";
28 protected ilDBInterface $db;
29 protected ilLanguage $lng;
30
31 public int $id = 0;
32 public string $purpose = "";
33 public string $location = "";
34 public string $location_type = "";
35 public string $format = "";
36 public string $width = "";
37 public string $height = "";
38 public string $caption = "";
39 public string $halign = "";
40 public array $parameters = [];
41 public int $mob_id = 0;
42 public int $nr = 0;
43 public array $mapareas = [];
44 public int $map_cnt = 0;
48 public $map_image = null; // image map work copy image
49 public int $color1; // map area line color 1
50 public int $color2; // map area line color 2
51 protected int $duration = 0;
52 protected string $upload_hash = '';
53
54 public function __construct(
55 int $a_id = 0
56 ) {
57 global $DIC;
58
59 $this->db = $DIC->database();
60 $this->lng = $DIC->language();
61 $this->parameters = array();
62 $this->mapareas = array();
63 $this->map_cnt = 0;
64
65 if ($a_id != 0) {
66 $this->setId($a_id);
67 $this->read();
68 }
69 }
70
74 public function setId(int $a_id): void
75 {
76 $this->id = $a_id;
77 }
78
79 public function getId(): int
80 {
81 return $this->id;
82 }
83
87 public function setMobId(int $a_mob_id): void
88 {
89 $this->mob_id = $a_mob_id;
90 }
91
92 public function getMobId(): int
93 {
94 return $this->mob_id;
95 }
96
100 public function setNr(int $a_nr): void
101 {
102 $this->nr = $a_nr;
103 }
104
105 public function getNr(): int
106 {
107 return $this->nr;
108 }
109
117 private static function getGDSupportedImageType(string $a_desired_type): string
118 {
119 $a_desired_type = strtolower($a_desired_type);
120 // get supported Image Types
121 $im_types = ImageTypes();
122
123 switch ($a_desired_type) {
124 case "jpg":
125 case "jpeg":
126 if ($im_types & IMG_JPG) {
127 return "jpg";
128 }
129 if ($im_types & IMG_GIF) {
130 return "gif";
131 }
132 if ($im_types & IMG_PNG) {
133 return "png";
134 }
135 break;
136
137 case "gif":
138 if ($im_types & IMG_GIF) {
139 return "gif";
140 }
141 if ($im_types & IMG_JPG) {
142 return "jpg";
143 }
144 if ($im_types & IMG_PNG) {
145 return "png";
146 }
147 break;
148
149 case "svg":
150 case "png":
151 if ($im_types & IMG_PNG) {
152 return "png";
153 }
154 if ($im_types & IMG_JPG) {
155 return "jpg";
156 }
157 if ($im_types & IMG_GIF) {
158 return "gif";
159 }
160 break;
161 }
162
163 return "";
164 }
165
166 public function setDuration(int $a_val): void
167 {
168 $this->duration = $a_val;
169 }
170
171 public function getDuration(): int
172 {
173 return $this->duration;
174 }
175
176 public function setTextRepresentation(string $a_val): void
177 {
178 $this->text_representation = $a_val;
179 }
180
181 public function getTextRepresentation(): string
182 {
184 }
185
186 public function setUploadHash(string $a_val): void
187 {
188 $this->upload_hash = $a_val;
189 }
190
191 public function getUploadHash(): string
192 {
193 return $this->upload_hash;
194 }
195
196 public function create(): void
197 {
199
200 $item_id = $ilDB->nextId("media_item");
201 $query = "INSERT INTO media_item (id,mob_id, purpose, location, " .
202 "location_type, format, width, " .
203 "height, halign, caption, nr, text_representation, upload_hash, duration) VALUES " .
204 "(" .
205 $ilDB->quote($item_id, "integer") . "," .
206 $ilDB->quote($this->getMobId(), "integer") . "," .
207 $ilDB->quote($this->getPurpose(), "text") . "," .
208 $ilDB->quote($this->getLocation(), "text") . "," .
209 $ilDB->quote($this->getLocationType(), "text") . "," .
210 $ilDB->quote($this->getFormat(), "text") . "," .
211 $ilDB->quote($this->getWidth(), "text") . "," .
212 $ilDB->quote($this->getHeight(), "text") . "," .
213 $ilDB->quote($this->getHAlign(), "text") . "," .
214 $ilDB->quote($this->getCaption(), "text") . "," .
215 $ilDB->quote($this->getNr(), "integer") . "," .
216 $ilDB->quote($this->getTextRepresentation(), "text") . "," .
217 $ilDB->quote($this->getUploadHash(), "text") . "," .
218 $ilDB->quote($this->getDuration(), "integer") .
219 ")";
220 $ilDB->manipulate($query);
221
222 $this->setId($item_id);
223
224 // create mob parameters
225 $params = $this->getParameters();
226 foreach ($params as $name => $value) {
227 $query = "INSERT INTO mob_parameter (med_item_id, name, value) VALUES " .
228 "(" . $ilDB->quote($item_id, "integer") . "," .
229 $ilDB->quote($name, "text") . "," .
230 $ilDB->quote($value, "text") . ")";
231 $ilDB->manipulate($query);
232 }
233
234 // create map areas
235 for ($i = 0; $i < count($this->mapareas); $i++) {
236 if (is_object($this->mapareas[$i])) {
237 $this->mapareas[$i]->setItemId($this->getId());
238 $this->mapareas[$i]->setNr($i + 1);
239 $this->mapareas[$i]->create();
240 }
241 }
242 }
243
244 public function update(): void
245 {
247
248 $query = "UPDATE media_item SET " .
249 " mob_id = " . $ilDB->quote($this->getMobId(), "integer") . "," .
250 " purpose = " . $ilDB->quote($this->getPurpose(), "text") . "," .
251 " location = " . $ilDB->quote($this->getLocation(), "text") . "," .
252 " location_type = " . $ilDB->quote($this->getLocationType(), "text") . "," .
253 " format = " . $ilDB->quote($this->getFormat(), "text") . "," .
254 " width = " . $ilDB->quote($this->getWidth(), "text") . "," .
255 " height = " . $ilDB->quote($this->getHeight(), "text") . "," .
256 " halign = " . $ilDB->quote($this->getHAlign(), "text") . "," .
257 " caption = " . $ilDB->quote($this->getCaption(), "text") . "," .
258 " nr = " . $ilDB->quote($this->getNr(), "integer") . "," .
259 " text_representation = " . $ilDB->quote($this->getTextRepresentation(), "text") . "," .
260 " upload_hash = " . $ilDB->quote($this->getUploadHash(), "text") . "," .
261 " duration = " . $ilDB->quote($this->getDuration(), "integer") .
262 " WHERE id = " . $ilDB->quote($this->getId(), "integer");
263 $ilDB->manipulate($query);
264
265 // delete mob parameters
266 $query = "DELETE FROM mob_parameter WHERE med_item_id = " .
267 $ilDB->quote($this->getId(), "integer");
268 $ilDB->manipulate($query);
269
270 // create mob parameters
271 $params = $this->getParameters();
272 foreach ($params as $name => $value) {
273 $query = "INSERT INTO mob_parameter (med_item_id, name, value) VALUES " .
274 "(" . $ilDB->quote($this->getId(), "integer") . "," .
275 $ilDB->quote($name, "text") . "," .
276 $ilDB->quote($value, "text") . ")";
277 $ilDB->manipulate($query);
278 }
279 }
280
281 public function writeParameter(
282 string $a_name,
283 string $a_value
284 ): void {
285 $ilDB = $this->db;
286
287 $query = "INSERT INTO mob_parameter (med_item_id, name, value) VALUES " .
288 "(" . $ilDB->quote($this->getId(), "integer") . "," .
289 $ilDB->quote($a_name, "text") . "," .
290 $ilDB->quote($a_value, "text") . ")";
291 $ilDB->manipulate($query);
292 }
293
297 public function read(): void
298 {
299 $ilDB = $this->db;
300
301 $item_id = $this->getId();
302 $mob_id = $this->getMobId();
303 $nr = $this->getNr();
304 $query = "";
305 if ($item_id > 0) {
306 $query = "SELECT * FROM media_item WHERE id = " .
307 $ilDB->quote($this->getId(), "integer");
308 } elseif ($mob_id > 0 && $nr > 0) {
309 $query = "SELECT * FROM media_item WHERE mob_id = " .
310 $ilDB->quote($this->getMobId(), "integer") . " " .
311 "AND nr=" . $ilDB->quote($this->getNr(), "integer");
312 }
313 if ($query != "") {
314 $item_set = $ilDB->query($query);
315 $item_rec = $ilDB->fetchAssoc($item_set);
316
317 $this->setLocation((string) $item_rec["location"]);
318 $this->setLocationType((string) $item_rec["location_type"]);
319 $this->setFormat((string) $item_rec["format"]);
320 $this->setWidth((string) $item_rec["width"]);
321 $this->setHeight((string) $item_rec["height"]);
322 $this->setHAlign((string) $item_rec["halign"]);
323 $this->setCaption((string) $item_rec["caption"]);
324 $this->setPurpose((string) $item_rec["purpose"]);
325 $this->setNr((int) $item_rec["nr"]);
326 $this->setMobId((int) $item_rec["mob_id"]);
327 $this->setId((int) $item_rec["id"]);
328 $this->setThumbTried((string) $item_rec["tried_thumb"]);
329 $this->setTextRepresentation((string) $item_rec["text_representation"]);
330 $this->setUploadHash((string) $item_rec["upload_hash"]);
331 $this->setDuration((int) $item_rec["duration"]);
332
333 // get item parameter
334 $query = "SELECT * FROM mob_parameter WHERE med_item_id = " .
335 $ilDB->quote($this->getId(), "integer");
336 $par_set = $ilDB->query($query);
337 while ($par_rec = $ilDB->fetchAssoc($par_set)) {
338 $this->setParameter($par_rec["name"], $par_rec["value"]);
339 }
340
341 // get item map areas
342 $max = ilMapArea::_getMaxNr($this->getId());
343 for ($i = 1; $i <= $max; $i++) {
344 $area = new ilMapArea($this->getId(), $i);
345 $this->addMapArea($area);
346 }
347 }
348 }
349
353 public function writeThumbTried(string $a_tried): void
354 {
355 $ilDB = $this->db;
356
357 $q = "UPDATE media_item SET tried_thumb = " .
358 $ilDB->quote($a_tried, "text") .
359 " WHERE id = " . $ilDB->quote($this->getId(), "integer");
360
361 $ilDB->manipulate($q);
362 }
363
364 public static function _lookupLocationForMobId(
365 int $a_mob_id,
366 string $a_purpose
367 ): string {
368 global $DIC;
369
370 $ilDB = $DIC->database();
371
372 // read media_object record
373 $query = "SELECT * FROM media_item WHERE mob_id = " .
374 $ilDB->quote($a_mob_id, "integer") . " " .
375 "AND purpose = " . $ilDB->quote($a_purpose, "text");
376 $set = $ilDB->query($query);
377 if ($rec = $ilDB->fetchAssoc($set)) {
378 return $rec["location"];
379 }
380
381 return "";
382 }
383
384 public static function _lookupMobId(
385 int $a_med_id
386 ): int {
387 global $DIC;
388
389 $ilDB = $DIC->database();
390
391 // read media_object record
392 $query = "SELECT * FROM media_item WHERE id = " .
393 $ilDB->quote($a_med_id, "integer");
394 $set = $ilDB->query($query);
395 if ($rec = $ilDB->fetchAssoc($set)) {
396 return (int) $rec["mob_id"];
397 }
398
399 return 0;
400 }
401
408 public static function _getMediaItemsOfMObId(
409 int $a_mobId,
410 string $a_purpose
411 ): ?array {
412 global $DIC;
413
414 $ilDB = $DIC->database();
415
416 // read media_object record
417 $query = "SELECT * FROM media_item WHERE mob_id = " .
418 $ilDB->quote($a_mobId, "integer") . " " .
419 "AND purpose=" . $ilDB->quote($a_purpose, "text") . " ORDER BY nr";
420 $item_set = $ilDB->query($query);
421
422 while ($item_rec = $ilDB->fetchAssoc($item_set)) {
423 return $item_rec;
424 }
425 return null;
426 }
427
431 public static function _getMediaItemsOfMOb(
432 ilObjMediaObject $a_mob
433 ): void {
434 global $DIC;
435
436 $ilDB = $DIC->database();
437
438 // read media_object record
439 $query = "SELECT * FROM media_item WHERE mob_id = " .
440 $ilDB->quote($a_mob->getId(), "integer") . " " .
441 "ORDER BY nr";
442 $item_set = $ilDB->query($query);
443 while ($item_rec = $ilDB->fetchAssoc($item_set)) {
444 $media_item = new ilMediaItem();
445 $media_item->setNr((int) $item_rec["nr"]);
446 $media_item->setId((int) $item_rec["id"]);
447 $media_item->setLocation((string) $item_rec["location"]);
448 $media_item->setLocationType((string) $item_rec["location_type"]);
449 $media_item->setFormat((string) $item_rec["format"]);
450 $media_item->setWidth((string) $item_rec["width"]);
451 $media_item->setHeight((string) $item_rec["height"]);
452 $media_item->setHAlign((string) $item_rec["halign"]);
453 $media_item->setCaption((string) $item_rec["caption"]);
454 $media_item->setPurpose((string) $item_rec["purpose"]);
455 $media_item->setMobId((int) $item_rec["mob_id"]);
456 $media_item->setThumbTried((string) $item_rec["tried_thumb"]);
457 $media_item->setTextRepresentation((string) $item_rec["text_representation"]);
458 $media_item->setUploadHash((string) $item_rec["upload_hash"]);
459 $media_item->setDuration((int) $item_rec["duration"]);
460
461 // get item parameter
462 $query = "SELECT * FROM mob_parameter WHERE med_item_id = " .
463 $ilDB->quote($item_rec["id"], "integer");
464 $par_set = $ilDB->query($query);
465 while ($par_rec = $ilDB->fetchAssoc($par_set)) {
466 $media_item->setParameter($par_rec["name"], $par_rec["value"]);
467 }
468
469 // get item map areas
470 $max = ilMapArea::_getMaxNr($media_item->getId());
471 for ($i = 1; $i <= $max; $i++) {
472 $area = new ilMapArea($media_item->getId(), $i);
473 $media_item->addMapArea($area);
474 }
475
476 // add media item to media object
477 $a_mob->addMediaItem($media_item);
478 }
479 }
480
481 public static function deleteAllItemsOfMob(int $a_mob_id): void
482 {
483 global $DIC;
484
485 $ilDB = $DIC->database();
486
487 // iterate all media items ob mob
488 $query = "SELECT * FROM media_item WHERE mob_id = " .
489 $ilDB->quote($a_mob_id, "integer");
490 $item_set = $ilDB->query($query);
491 while ($item_rec = $ilDB->fetchAssoc($item_set)) {
492 // delete all parameters of media item
493 $query = "DELETE FROM mob_parameter WHERE med_item_id = " .
494 $ilDB->quote($item_rec["id"], "integer");
495 $ilDB->manipulate($query);
496
497 // delete all map areas of media item
498 $query = "DELETE FROM map_area WHERE item_id = " .
499 $ilDB->quote($item_rec["id"], "integer");
500 $ilDB->manipulate($query);
501 }
502
503 // delete media items
504 $query = "DELETE FROM media_item WHERE mob_id = " .
505 $ilDB->quote($a_mob_id, "integer");
506 $ilDB->manipulate($query);
507 }
508
509 public function setPurpose(string $a_purpose): void
510 {
511 $this->purpose = $a_purpose;
512 }
513
514 public function getPurpose(): string
515 {
516 return $this->purpose;
517 }
518
519 public function setLocation(string $a_location): void
520 {
521 $this->location = $a_location;
522 }
523
524 public function getLocation(): string
525 {
526 return $this->location;
527 }
528
529 public function setLocationType(string $a_type): void
530 {
531 $this->location_type = $a_type;
532 }
533
534 public function getLocationType(): string
535 {
536 return $this->location_type;
537 }
538
539 public function setFormat(string $a_format): void
540 {
541 $this->format = $a_format;
542 }
543
544 public function getFormat(): string
545 {
546 return $this->format;
547 }
548
549 public function setThumbTried(string $a_tried): void
550 {
551 $this->tried_thumb = $a_tried;
552 }
553
554 public function getThumbTried(): string
555 {
556 return $this->tried_thumb;
557 }
558
559 public function addMapArea(ilMapArea $a_map_area): void
560 {
561 $this->mapareas[$this->map_cnt] = $a_map_area;
562 $this->map_cnt++;
563 }
564
565 public function deleteMapArea(int $nr): void
566 {
567 for ($i = 1; $i <= $this->map_cnt; $i++) {
568 if ($i > $nr) {
569 $this->mapareas[$i - 2] = $this->mapareas[$i - 1];
570 $this->mapareas[$i - 2]->setNr($i - 1);
571 }
572 }
573 if ($nr <= $this->map_cnt) {
574 unset($this->mapareas[$this->map_cnt - 1]);
575 $this->map_cnt--;
576 }
577 }
578
579 public function getMapArea(int $nr): ?ilMapArea
580 {
581 return $this->mapareas[$nr - 1] ?? null;
582 }
583
584 public function getMapAreas(): array
585 {
586 return $this->mapareas;
587 }
588
589 public function getWidth(): string
590 {
591 return $this->width;
592 }
593
594 public function setWidth(string $a_width): void
595 {
596 $this->width = $a_width;
597 }
598
599 public function getHeight(): string
600 {
601 return $this->height;
602 }
603
604 public function setHeight(string $a_height): void
605 {
606 $this->height = $a_height;
607 }
608
609 public function getOriginalSize(): ?array
610 {
611 $mob_dir = ilObjMediaObject::_getDirectory($this->getMobId());
612
613 if (ilUtil::deducibleSize($this->getFormat())) {
614 if ($this->getLocationType() == "LocalFile") {
615 $loc = $mob_dir . "/" . $this->getLocation();
616 } else {
617 $loc = $this->getLocation();
618 }
619
620 $size = ilMediaImageUtil::getImageSize($loc);
621 if ($size[0] > 0 && $size[1] > 0) {
622 return array("width" => $size[0], "height" => $size[1]);
623 }
624 }
625
626 return null;
627 }
628
629 public function setCaption(string $a_caption): void
630 {
631 $this->caption = $a_caption;
632 }
633
634 public function getCaption(): string
635 {
636 return $this->caption;
637 }
638
642 public function setHAlign(string $a_halign): void
643 {
644 $this->halign = $a_halign;
645 }
646
647 public function getHAlign(): string
648 {
649 return $this->halign;
650 }
651
652 public function setParameter(
653 string $a_name,
654 string $a_value
655 ): void {
656 if (self::checkParameter($a_name, $a_value)) {
657 $this->parameters[$a_name] = $a_value;
658 }
659 }
660
661 public function resetParameters(): void
662 {
663 $this->parameters = [];
664 }
665
669 public function setParameters(string $a_par): void
670 {
671 $this->resetParameters();
672 $par_arr = ilUtil::extractParameterString($a_par);
673 if (is_array($par_arr)) {
674 foreach ($par_arr as $par => $val) {
675 $this->setParameter($par, $val);
676 }
677 }
678 }
679
683 public static function checkParameter(
684 string $a_par,
685 string $a_val
686 ): bool {
687 // do not allow event attributes
688 if (substr(strtolower(trim($a_par)), 0, 2) == "on") {
689 return false;
690 }
691 // no javascript in value
692 if (is_int(strpos(strtolower($a_val), "javascript"))) {
693 return false;
694 }
695 // do not allow to change the src attribute
696 if (strtolower(trim($a_par)) == "src") {
697 return false;
698 }
699
700 return true;
701 }
702
703 public function getParameters(): array
704 {
705 return $this->parameters;
706 }
707
708 public function getParameterString(): string
709 {
710 if (is_array($this->parameters)) {
711 $target_arr = [];
712 foreach ($this->parameters as $par => $val) {
713 $target_arr[] = "$par=\"$val\"";
714 }
715 return implode(", ", $target_arr);
716 }
717 return "";
718 }
719
720 public function getParameter(string $a_name): string
721 {
722 return (string) ($this->parameters[$a_name] ?? "");
723 }
724
728 public function getWorkDirectory(): string
729 {
730 return ilFileUtils::getDataDir() . "/map_workfiles/item_" . $this->getId();
731 }
732
736 public function createWorkDirectory(): void
737 {
738 if (!is_dir(ilFileUtils::getDataDir() . "/map_workfiles")) {
740 }
741 $work_dir = $this->getWorkDirectory();
742 if (!is_dir($work_dir)) {
744 }
745 }
746
750 public function getSuffix(): string
751 {
752 $loc_arr = explode(".", $this->getLocation());
753
754 return $loc_arr[count($loc_arr) - 1];
755 }
756
760 public function getMapWorkCopyType(): string
761 {
762 return self::getGDSupportedImageType($this->getSuffix());
763 }
764
769 public function getMapWorkCopyName(
770 bool $a_reference_copy = false
771 ): string {
772 $file_arr = explode("/", $this->getLocation());
773 $o_file = $file_arr[count($file_arr) - 1];
774 $file_arr = explode(".", $o_file);
775 unset($file_arr[count($file_arr) - 1]);
776 $file = implode(".", $file_arr);
777
778 if (!$a_reference_copy) {
779 return $this->getWorkDirectory() . "/" . $file . "." . $this->getMapWorkCopyType();
780 } else {
781 return $this->getWorkDirectory() . "/l_copy_" . $o_file;
782 }
783 }
784
788 public function getDirectory(): string
789 {
790 return ilObjMediaObject::_getDirectory($this->getMobId());
791 }
792
796 public function getThumbnailDirectory(
797 string $a_mode = "filesystem"
798 ): string {
799 return ilObjMediaObject::_getThumbnailDirectory($this->getMobId(), $a_mode);
800 }
801
805 public function getThumbnailTarget(
806 string $a_size = ""
807 ): string {
808 $jpeg_file = $this->getThumbnailDirectory() . "/" .
809 $this->getPurpose() . ".jpeg";
810 $format = "png";
811 if (is_file($jpeg_file)) {
812 $format = "jpeg";
813 }
814 if (is_int(strpos($this->getFormat(), "image"))) {
815 $thumb_file = $this->getThumbnailDirectory() . "/" .
816 $this->getPurpose() . "." . $format;
817 $thumb_file_small = $this->getThumbnailDirectory() . "/" .
818 $this->getPurpose() . "_small." . $format;
819 // generate thumbnail (if not tried before)
820 if ($this->getThumbTried() == "n" && $this->getLocationType() == "LocalFile" && $this->getFormat() !== "image/svg+xml") {
821 if (is_file($thumb_file)) {
822 unlink($thumb_file);
823 }
824 if (is_file($thumb_file_small)) {
825 unlink($thumb_file_small);
826 }
827 $this->writeThumbTried("y");
829 $med_file = $this->getDirectory() . "/" . $this->getLocation();
830
831 if (is_file($med_file)) {
832 $mob = new ilObjMediaObject($this->getMobId());
833 $mob->makeThumbnail($this->getLocation(), $this->getPurpose() . "." . $format, $format, "80");
834 $mob->makeThumbnail($this->getLocation(), $this->getPurpose() . "_small." . $format, $format, "40");
835 }
836 }
837 if ($this->getFormat() === "image/svg+xml") {
838 return ilObjMediaObject::_getURL($this->getMobId()) . "/" . $this->getLocation();
839 }
840 if ($a_size == "small") {
841 if (is_file($thumb_file_small)) {
842 $random = new \ilRandom();
843 return $this->getThumbnailDirectory("output") . "/" .
844 $this->getPurpose() . "_small." . $format . "?dummy=" . $random->int(1, 999999);
845 }
846 } else {
847 if (is_file($thumb_file)) {
848 $random = new \ilRandom();
849 return $this->getThumbnailDirectory("output") . "/" .
850 $this->getPurpose() . "." . $format . "?dummy=" . $random->int(1, 999999);
851 }
852 }
853 }
854
855 return "";
856 }
857
863 public function copyOriginal(): void
864 {
866 $this->createWorkDirectory();
867
868 $geom = ($this->getWidth() != "" && $this->getHeight() != "")
869 ? $this->getWidth() . "x" . $this->getHeight()
870 : "";
871
872 if ($this->getLocationType() !== "Reference") {
874 $this->getDirectory() . "/" . $this->getLocation(),
875 $this->getMapWorkCopyName(),
876 $this->getMapWorkCopyType(),
877 $geom
878 );
879 } else {
880 // first copy the external file, if necessary
881 if (!is_file($this->getMapWorkCopyName(true)) || (filesize($this->getMapWorkCopyName(true)) == 0)) {
882 $handle = fopen($this->getLocation(), "r");
883 $lcopy = fopen($this->getMapWorkCopyName(true), "w");
884 if ($handle && $lcopy) {
885 while (!feof($handle)) {
886 $content = fread($handle, 4096);
887 fwrite($lcopy, $content);
888 }
889 }
890 fclose($lcopy);
891 fclose($handle);
892 }
893
894 // now, create working copy
896 $this->getMapWorkCopyName(true),
897 $this->getMapWorkCopyName(),
898 $this->getMapWorkCopyType(),
899 $geom
900 );
901 }
902 if (!is_file($this->getMapWorkCopyName())) {
903 throw new ilMapEditingException($lng->txt("cont_map_file_not_generated"));
904 }
905 }
906
912 public function makeMapWorkCopy(
913 int $a_area_nr = 0,
914 bool $a_exclude = false
915 ): void {
916 $lng = $this->lng;
917
918 $this->copyOriginal();
919 $this->buildMapWorkImage();
920
921 // determine ratios
922 $size = getimagesize($this->getMapWorkCopyName());
923 $x_ratio = 1;
924 if ($size[0] > 0 && $this->getWidth() > 0) {
925 $x_ratio = $this->getWidth() / $size[0];
926 }
927 $y_ratio = 1;
928 if ($size[1] > 0 && $this->getHeight() > 0) {
929 $y_ratio = $this->getHeight() / $size[1];
930 }
931
932 // draw map areas
933 for ($i = 0; $i < count($this->mapareas); $i++) {
934 if (((($i + 1) == $a_area_nr) && !$a_exclude) ||
935 ((($i + 1) != $a_area_nr) && $a_exclude) ||
936 ($a_area_nr == 0)
937 ) {
938 $area = $this->mapareas[$i];
939 $area->draw(
940 $this->getMapWorkImage(),
941 $this->color1,
942 $this->color2,
943 true,
944 $x_ratio,
945 $y_ratio
946 );
947 }
948 }
949
950 $this->saveMapWorkImage();
951 }
952
958 public function addAreaToMapWorkCopy(
959 string $a_shape,
960 string $a_coords
961 ): void {
962 $this->buildMapWorkImage();
963
964 // determine ratios
965 $size = getimagesize($this->getMapWorkCopyName());
966 $x_ratio = 1;
967 if ($size[0] > 0 && $this->getWidth() > 0) {
968 $x_ratio = $this->getWidth() / $size[0];
969 }
970 $y_ratio = 1;
971 if ($size[1] > 0 && $this->getHeight() > 0) {
972 $y_ratio = $this->getHeight() / $size[1];
973 }
974
975 // add new area to work image
976 $area = new ilMapArea();
977 $area->setShape($a_shape);
978 $area->setCoords($a_coords);
979 $area->draw(
980 $this->getMapWorkImage(),
981 $this->color1,
982 $this->color2,
983 false,
984 $x_ratio,
985 $y_ratio
986 );
987
988 $this->saveMapWorkImage();
989 }
990
994 public function outputMapWorkCopy(): void
995 {
996 if ($this->getMapWorkCopyType() != "") {
997 header("Pragma: no-cache");
998 header("Expires: 0");
999 header("Content-type: image/" . strtolower($this->getMapWorkCopyType()));
1000 readfile($this->getMapWorkCopyName());
1001 }
1002 exit;
1003 }
1004
1008 public function buildMapWorkImage(): void
1009 {
1010 $im_type = strtolower($this->getMapWorkCopyType());
1011
1012 switch ($im_type) {
1013 case "gif":
1014 $this->map_image = imagecreatefromgif($this->getMapWorkCopyName());
1015 break;
1016
1017 case "jpg":
1018 case "jpeg":
1019 $this->map_image = imagecreatefromjpeg($this->getMapWorkCopyName());
1020 break;
1021
1022 case "png":
1023 $this->map_image = imagecreatefrompng($this->getMapWorkCopyName());
1024 break;
1025 }
1026
1027 // try to allocate black and white as color. if this is not possible, get the closest colors
1028 if (imagecolorstotal($this->map_image) > 250) {
1029 $this->color1 = imagecolorclosest($this->map_image, 0, 0, 0);
1030 $this->color2 = imagecolorclosest($this->map_image, 255, 255, 255);
1031 } else {
1032 $this->color1 = imagecolorallocate($this->map_image, 0, 0, 0);
1033 $this->color2 = imagecolorallocate($this->map_image, 255, 255, 255);
1034 }
1035 }
1036
1040 public function saveMapWorkImage(): void
1041 {
1042 $im_type = strtolower($this->getMapWorkCopyType());
1043
1044 // save image work-copy and free memory
1045 switch ($im_type) {
1046 case "gif":
1047 imagegif($this->map_image, $this->getMapWorkCopyName());
1048 break;
1049
1050 case "jpg":
1051 case "jpeg":
1052 imagejpeg($this->map_image, $this->getMapWorkCopyName());
1053 break;
1054
1055 case "png":
1056 imagepng($this->map_image, $this->getMapWorkCopyName());
1057 break;
1058 }
1059
1060 imagedestroy($this->map_image);
1061 }
1062
1066 public function getMapWorkImage()
1067 {
1068 return $this->map_image;
1069 }
1070
1074 public function getMapAreasXML(
1075 bool $a_insert_inst = false,
1076 int $a_inst = 0
1077 ): string {
1078 $xml = "";
1079
1080 // build xml of map areas
1081 for ($i = 0; $i < count($this->mapareas); $i++) {
1082 $area = $this->mapareas[$i];
1083
1084 // highlight mode
1085 $hm = "";
1086 if ($area->getHighlightMode() != "") {
1087 $hm = ' HighlightMode="' . $area->getHighlightMode() . '" ';
1088 $hcl = ($area->getHighlightClass() != "")
1089 ? $area->getHighlightClass()
1090 : "Accented";
1091 $hm .= 'HighlightClass="' . $hcl . '" ';
1092 }
1093
1094 $xml .= "<MapArea Shape=\"" . $area->getShape() . "\" Coords=\"" . $area->getCoords() . "\" " . $hm . ">";
1095 if ($area->getLinkType() == IL_INT_LINK) {
1096 $target_frame = $area->getTargetFrame();
1097
1098 if ($area->getType() == "GlossaryItem" && $target_frame == "") {
1099 $target_frame = "Glossary";
1100 }
1101
1102 $tf_str = ($target_frame == "")
1103 ? ""
1104 : "TargetFrame=\"" . $target_frame . "\"";
1105
1106 $xml .= "<IntLink Target=\"" . $area->getTarget($a_insert_inst, $a_inst) . "\" Type=\"" .
1107 $area->getType() . "\" $tf_str>";
1108 // see bug 17893 and http://stackoverflow.com/questions/4026502/xml-error-at-ampersand
1109 $xml .= htmlspecialchars($area->getTitle(), ENT_QUOTES);
1110 $xml .= "</IntLink>";
1111 } else {
1112 $xml .= "<ExtLink Href=\"" . str_replace("&", "&amp;", $area->getHref()) . "\" Title=\"" .
1113 str_replace("&", "&amp;", $area->getExtTitle()) . "\">";
1114 $xml .= str_replace("&", "&amp;", $area->getTitle());
1115 $xml .= "</ExtLink>";
1116 }
1117 $xml .= "</MapArea>";
1118 }
1119 return $xml;
1120 }
1121
1126 public static function _resolveMapAreaLinks(
1127 int $a_mob_id
1128 ): void {
1129 global $DIC;
1130
1131 $ilDB = $DIC->database();
1132
1133 //echo "mediaItems::resolve<br>";
1134 // read media_object record
1135 $query = "SELECT * FROM media_item WHERE mob_id = " .
1136 $ilDB->quote($a_mob_id, "integer") . " " .
1137 "ORDER BY nr";
1138 $item_set = $ilDB->query($query);
1139 while ($item_rec = $ilDB->fetchAssoc($item_set)) {
1140 ilMapArea::_resolveIntLinks($item_rec["id"]);
1141 }
1142 }
1143
1148 public static function _getMapAreasIntLinks(
1149 int $a_mob_id
1150 ): array {
1151 global $DIC;
1152
1153 $ilDB = $DIC->database();
1154
1155 // read media_items records
1156 $query = "SELECT * FROM media_item WHERE mob_id = " .
1157 $ilDB->quote($a_mob_id, "integer") . " ORDER BY nr";
1158
1159 $item_set = $ilDB->query($query);
1160 $links = array();
1161 while ($item_rec = $ilDB->fetchAssoc($item_set)) {
1162 $map_links = ilMapArea::_getIntLinks($item_rec["id"]);
1163 foreach ($map_links as $key => $map_link) {
1164 $links[$key] = $map_link;
1165 }
1166 }
1167 return $links;
1168 }
1169
1173 public function extractUrlParameters(): void
1174 {
1176 $this->getLocation(),
1177 $this->getParameters()
1178 );
1179 foreach ($par as $k => $v) {
1180 $this->setParameter($k, $v);
1181 }
1182 }
1183
1184 public function determineDuration(): void
1185 {
1186 $ana = new ilMediaAnalyzer();
1187
1188 if (ilExternalMediaAnalyzer::isVimeo($this->getLocation())) {
1189 $par = ilExternalMediaAnalyzer::extractVimeoParameters($this->getLocation());
1191 if ($meta["duration"] > 0) {
1192 $this->setDuration((int) $meta["duration"]);
1193 }
1194 } else {
1195 $file = ($this->getLocationType() == "Reference")
1196 ? $this->getLocation()
1197 : ilObjMediaObject::_getDirectory($this->getMobId()) . "/" . $this->getLocation();
1198
1199 $remote = false;
1200
1201 try {
1202 if (substr($file, 0, 4) == "http") {
1203 if ($fp_remote = fopen($file, 'rb')) {
1204 $tmpdir = ilFileUtils::ilTempnam();
1205 ilFileUtils::makeDir($tmpdir);
1206 $localtempfilename = tempnam($tmpdir, 'getID3');
1207 if ($fp_local = fopen($localtempfilename, 'wb')) {
1208 while ($buffer = fread($fp_remote, 8192)) {
1209 fwrite($fp_local, $buffer);
1210 }
1211 fclose($fp_local);
1212 $file = $localtempfilename;
1213 }
1214 fclose($fp_remote);
1215 }
1216 }
1217
1218 $ana->setFile($file);
1219 $ana->analyzeFile();
1220 $this->setDuration((int) $ana->getPlaytimeSeconds());
1221
1222 if ($remote) {
1223 unlink($localtempfilename);
1224 }
1225 } catch (Exception $e) {
1226 }
1227 }
1228 }
1229
1235 public static function getMediaItemsForUploadHash(
1236 string $a_hash
1237 ): array {
1238 global $DIC;
1239
1240 $db = $DIC->database();
1241
1242 $set = $db->queryF(
1243 "SELECT * FROM media_item " .
1244 " WHERE upload_hash = %s ",
1245 array("text"),
1246 array($a_hash)
1247 );
1248 $media_items = array();
1249 while ($rec = $db->fetchAssoc($set)) {
1250 $media_items[] = $rec;
1251 }
1252 return $media_items;
1253 }
1254}
$location
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Definition: buildRTE.php:22
const IL_INT_LINK
static isVimeo(string $a_location)
Identify Vimeo links.
static extractVimeoParameters(string $a_location)
Extract Vimeo Parameter.
static extractUrlParameters(string $a_location, array $a_parameter)
Extract URL information to parameter array.
static makeDir(string $a_dir)
creates a new directory and inherits all filesystem permissions of the parent directory You may pass ...
static ilTempnam(?string $a_temp_path=null)
Returns a unique and non existing Path for e temporary file or directory.
static getDataDir()
get data directory (outside webspace)
static createDirectory(string $a_dir, int $a_mod=0755)
create directory
language handling
Class ilMapArea.
static _resolveIntLinks(int $a_item_id)
resolve internal links of an item id
static _getMaxNr(int $a_item_id)
get maximum nr of media item (static)
static _getIntLinks(int $a_item_id)
get all internal links of a media items map areas
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
static getImageSize(string $a_location)
Get image size from location.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
setLocationType(string $a_type)
setNr(int $a_nr)
set number of media item within media object
addAreaToMapWorkCopy(string $a_shape, string $a_coords)
draw a new area in work image
setUploadHash(string $a_val)
static _getMediaItemsOfMOb(ilObjMediaObject $a_mob)
Read media items into(!) media object (static)
setWidth(string $a_width)
getWorkDirectory()
get work directory for image map editing
static _getMapAreasIntLinks(int $a_mob_id)
get all internal links of map areas of a mob
outputMapWorkCopy()
output raw map work copy file
addMapArea(ilMapArea $a_map_area)
string $text_representation
setDuration(int $a_val)
ilLanguage $lng
extractUrlParameters()
Extract parameters of special external references to parameter array.
saveMapWorkImage()
save image map work image as file
createWorkDirectory()
create work directory for image map editing
getSuffix()
get location suffix
makeMapWorkCopy(int $a_area_nr=0, bool $a_exclude=false)
make map work copy of image
static deleteAllItemsOfMob(int $a_mob_id)
setThumbTried(string $a_tried)
setLocation(string $a_location)
static checkParameter(string $a_par, string $a_val)
Check parameter (filter javascript related and other unsafe parameters/values)
getMapAreasXML(bool $a_insert_inst=false, int $a_inst=0)
get xml code of media items' areas
static _resolveMapAreaLinks(int $a_mob_id)
resolve internal links of all media items of a media object
getDirectory()
get media file directory
setCaption(string $a_caption)
getMapWorkCopyType()
get image type of image map work copy
getMapWorkCopyName(bool $a_reference_copy=false)
Get name of image map work copy file.
buildMapWorkImage()
build image map work image
read()
read media item data (item id or (mob_id and nr) must be set)
setHeight(string $a_height)
setParameter(string $a_name, string $a_value)
static getGDSupportedImageType(string $a_desired_type)
returns the best supported image type by this PHP build
setParameters(string $a_par)
set all parameters via parameter string (format: par1="value1", par2="value2", ......
deleteMapArea(int $nr)
static getMediaItemsForUploadHash(string $a_hash)
Get media items for upload hash.
__construct(int $a_id=0)
ilDBInterface $db
getParameter(string $a_name)
setId(int $a_id)
set media item id
setTextRepresentation(string $a_val)
static _getMediaItemsOfMObId(int $a_mobId, string $a_purpose)
read media item with specific purpose and mobId
writeParameter(string $a_name, string $a_value)
setFormat(string $a_format)
copyOriginal()
Copy the original file for map editing to the working directory.
static _lookupLocationForMobId(int $a_mob_id, string $a_purpose)
setHAlign(string $a_halign)
set horizontal align
getThumbnailDirectory(string $a_mode="filesystem")
get media file directory
setPurpose(string $a_purpose)
static _lookupMobId(int $a_med_id)
getThumbnailTarget(string $a_size="")
get thumbnail target
writeThumbTried(string $a_tried)
write thumbnail creation try data ("y"/"n")
setMobId(int $a_mob_id)
set id of parent media object
static _createThumbnailDirectory(int $a_obj_id)
Create thumbnail directory.
static _getURL(int $a_mob_id)
get directory for files of media object
addMediaItem(ilMediaItem $a_item)
static _getDirectory(int $a_mob_id)
Get absolute directory.
static convertImage(string $a_from, string $a_to, string $a_target_format="", string $a_geometry="", string $a_background_color="")
convert image
static deducibleSize(string $a_mime)
checks if mime type is provided by getimagesize()
static extractParameterString(string $a_parstr)
if(!file_exists(getcwd() . '/ilias.ini.php'))
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Definition: confirmReg.php:20
return['3gp', '7z', 'ai', 'aif', 'aifc', 'aiff', 'au', 'arw', 'avi', 'backup', 'bak', 'bas', 'bpmn', 'bpmn2', 'bmp', 'bib', 'bibtex', 'bz', 'bz2', 'c', 'c++', 'cc', 'cct', 'cdf', 'cer', 'class', 'cls', 'conf', 'cpp', 'crt', 'crs', 'crw', 'cr2', 'css', 'cst', 'csv', 'cur', 'db', 'dcr', 'des', 'dng', 'doc', 'docx', 'dot', 'dotx', 'dtd', 'dvi', 'el', 'eps', 'epub', 'f', 'f77', 'f90', 'flv', 'for', 'g3', 'gif', 'gl', 'gan', 'ggb', 'gsd', 'gsm', 'gtar', 'gz', 'gzip', 'h', 'hpp', 'htm', 'html', 'htmls', 'ibooks', 'ico', 'ics', 'ini', 'ipynb', 'java', 'jbf', 'jpeg', 'jpg', 'js', 'jsf', 'jso', 'json', 'latex', 'lang', 'less', 'log', 'lsp', 'ltx', 'm1v', 'm2a', 'm2v', 'm3u', 'm4a', 'm4v', 'markdown', 'm', 'mat', 'md', 'mdl', 'mdown', 'mid', 'min', 'midi', 'mobi', 'mod', 'mov', 'movie', 'mp2', 'mp3', 'mp4', 'mpa', 'mpeg', 'mpg', 'mph', 'mpga', 'mpp', 'mpt', 'mpv', 'mpx', 'mv', 'mw', 'mv4', 'nb', 'nbp', 'nef', 'nif', 'niff', 'obj', 'obm', 'odt', 'ods', 'odp', 'odg', 'odf', 'oga', 'ogg', 'ogv', 'old', 'p', 'pas', 'pbm', 'pcl', 'pct', 'pcx', 'pdf', 'pgm', 'pic', 'pict', 'png', 'por', 'pov', 'project', 'properties', 'ppa', 'ppm', 'pps', 'ppsx', 'ppt', 'pptx', 'ppz', 'ps', 'psd', 'pwz', 'qt', 'qtc', 'qti', 'qtif', 'r', 'ra', 'ram', 'rar', 'rast', 'rda', 'rev', 'rexx', 'ris', 'rf', 'rgb', 'rm', 'rmd', 'rmi', 'rmm', 'rmp', 'rt', 'rtf', 'rtx', 'rv', 's', 's3m', 'sav', 'sbs', 'sec', 'sdml', 'sgm', 'sgml', 'smi', 'smil', 'srt', 'sps', 'spv', 'stl', 'svg', 'swa', 'swf', 'swz', 'tar', 'tex', 'texi', 'texinfo', 'text', 'tgz', 'tif', 'tiff', 'ttf', 'txt', 'tmp', 'uvproj', 'vdf', 'vimeo', 'viv', 'vivo', 'vrml', 'vsdx', 'wav', 'webm', 'wmv', 'wmx', 'wmz', 'woff', 'wwd', 'xhtml', 'xif', 'xls', 'xlsx', 'xmind', 'xml', 'xsl', 'xsd', 'zip']
global $DIC
Definition: feed.php:28
$target_arr
Definition: goto.php:50
Interface ilDBInterface.
fetchAssoc(ilDBStatement $statement)
queryF(string $query, array $types, array $values)
exit
Definition: login.php:28
if(! $DIC->user() ->getId()||!ilLTIConsumerAccess::hasCustomProviderCreationAccess()) $params
Definition: ltiregstart.php:33
if($format !==null) $name
Definition: metadata.php:247
$format
Definition: metadata.php:235
$i
Definition: metadata.php:41
$xml
Definition: metadata.php:351
string $key
Consumer key/client ID value.
Definition: System.php:193
$query
$lng