aboutsummaryrefslogtreecommitdiff
path: root/lib/models/Courseware/BlockTypes/Embed.php
blob: 69e0fe900ca3a1a7c09dd3a429b0af326e21f593 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
<?php

namespace Courseware\BlockTypes;

/**
 * This class represents the content of a Courseware embed block.
 *
 * @author  Ron Lucke <lucke@elan-ev.de>
 * @license GPL2 or any later version
 *
 * @since   Stud.IP 5.0
 */
class Embed extends BlockType
{
    public static function getType(): string
    {
        return 'embed';
    }

    public static function getTitle(): string
    {
        return _('Embed');
    }

    public static function getDescription(): string
    {
        return _('Bindet externe Inhalte wie Videos, Grafiken oder Musik ein.');
    }

    public function initialPayload(): array
    {
        return [
            'title' => '',
            'url' => '',
            'source' => '',
            'starttime' => '', // for youtube
            'endtime' => '', // for youtube
        ];
    }

    public static function getJsonSchema(): string
    {
        $schemaFile = __DIR__.'/Embed.json';
        return file_get_contents($schemaFile);
    }

    public function getPayload()
    {
        $payload = $this->decodePayloadString($this->block['payload']);

        $oembedRequest = $this->buildOembedRequest($payload['source'], $payload['url']);

        $payload['oembed-request'] = $oembedRequest;
        $payload['oembed-unauthorized'] = false;
        $payload['oembed-not-found'] = true;
        $payload['oembed'] = null;
        $payload['request'] = null;

        if ($oembedRequest) {
            $request = \FileManager::fetchURLMetadata($oembedRequest);
            if ($request['response_code'] === 200) {
                $payload['request'] = file_get_contents($oembedRequest, false, get_default_http_stream_context($oembedRequest));
                $payload['oembed'] = json_decode($payload['request']);
                $payload['oembed-not-found'] = false;
            }
            if ($request['response_code'] === 401) {
                $payload['oembed-unauthorized'] = true;
                $payload['oembed-not-found'] = false;
            }
        }

        return $payload;
    }

    private function buildOembedRequest($source, $url)
    {
        $endPoints = [
            'audiomack' => 'https://www.audiomack.com/oembed',
            'codepen' => 'https://codepen.io/api/oembed',
            'codesandbox' => 'https://codesandbox.io/oembed',
            'deviantart' => 'https://backend.deviantart.com/oembed',
            'ethfiddle' => 'https://ethfiddle.com/services/oembed/',
            'flickr' => 'https://www.flickr.com/services/oembed/',
            'giphy' => 'https://giphy.com/services/oembed',
            'kidoju' => 'https://www.kidoju.com/api/oembed',
            'learningapps' => 'https://learningapps.org/oembed.php',
            'sketchfab' => 'https://sketchfab.com/oembed',
            'slideshare' => 'https://www.slideshare.net/api/oembed/2',
            'soundcloud' => 'https://soundcloud.com/oembed',
            'speakerdeck' => 'https://speakerdeck.com/oembed.json',
            'sway' => 'https://sway.com/api/v1.0/oembed',
            'sway.office' => 'https://sway.office.com/api/v1.0/oembed',
            'spotify' => 'https://embed.spotify.com/oembed/',
            'vimeo' => 'https://vimeo.com/api/oembed.json',
            'youtube' => 'https://www.youtube.com/oembed',
        ];

        return  array_key_exists($source, $endPoints) ? $endPoints[$source].'?url='.rawurlencode($url).'&format=json' : null;
    }

    public static function getCategories(): array
    {
        return ['external'];
    }

    public static function getContentTypes(): array
    {
        return ['text', 'video', 'audio', 'image', 'rich'];
    }

    public static function getFileTypes(): array
    {
        return [];
    }

    public static function getTags(): array
    {
        return [
            _('Text'), _('Video'), _('Audio'), _('Bild'), _('Webseite'), 'YouTube',
            'Vimeo', 'Spotify', 'swan.office', 'speakerdeck', 'soundcloud',
            'slideshare', 'sketchfab', 'learningapps', 'kidoju', 'giphy',
            'flickr', 'ethfiddle', 'deviantart', 'codesandbox', 'codepen',
            'audiomack', _('extern'), _('einbinden'), _('einbetten'), _('einfügen'),
            _('Multimedia'), _('Grafik'), _('Podcast'), _('Medien'), _('bereitstellen'),
            'gif', 'meme', _('Cloud'), _('Interaktion')
        ];
    }
}