]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/Oembed/OembedPlugin.php
Moved oEmbed stuff out to a plugin (Oembed).
[quix0rs-gnu-social.git] / plugins / Oembed / OembedPlugin.php
1 <?php
2
3 class OembedPlugin extends Plugin
4 {
5     public function onCheckSchema()
6     {
7         $schema = Schema::get();
8         $schema->ensureTable('file_oembed', File_oembed::schemaDef());
9         return true;
10     }
11
12     public function onRouterInitialized(URLMapper $m)
13     {
14         $m->connect('main/oembed', array('action' => 'oembed'));
15     }
16
17     public function onEndShowHeadElements(Action $action)
18     {
19         switch ($action->getActionName()) {
20         case 'attachment':
21             $action->element('link',array('rel'=>'alternate',
22                 'type'=>'application/json+oembed',
23                 'href'=>common_local_url(
24                     'oembed',
25                     array(),
26                     array('format'=>'json', 'url'=>
27                         common_local_url('attachment',
28                             array('attachment' => $action->attachment->id)))),
29                 'title'=>'oEmbed'),null);
30             $action->element('link',array('rel'=>'alternate',
31                 'type'=>'text/xml+oembed',
32                 'href'=>common_local_url(
33                     'oembed',
34                     array(),
35                     array('format'=>'xml','url'=>
36                         common_local_url('attachment',
37                             array('attachment' => $action->attachment->id)))),
38                 'title'=>'oEmbed'),null);
39             break;
40         case 'shownotice':
41             $this->element('link',array('rel'=>'alternate',
42                 'type'=>'application/json+oembed',
43                 'href'=>common_local_url(
44                     'oembed',
45                     array(),
46                     array('format'=>'json','url'=>$this->notice->getUrl())),
47                 'title'=>'oEmbed'),null);
48             $this->element('link',array('rel'=>'alternate',
49                 'type'=>'text/xml+oembed',
50                 'href'=>common_local_url(
51                     'oembed',
52                     array(),
53                     array('format'=>'xml','url'=>$this->notice->getUrl())),
54                 'title'=>'oEmbed'),null);
55             break;
56         }
57
58         return true;
59     }
60
61     /**
62      * Save embedding information for a File, if applicable.
63      *
64      * Normally this event is called through File::saveNew()
65      *
66      * @param File   $file       The newly inserted File object.
67      * @param array  $redir_data lookup data eg from File_redirection::where()
68      * @param string $given_url
69      *
70      * @return boolean success
71      */
72     public function onEndFileSaveNew(File $file, array $redir_data, $given_url)
73     {
74         $fo = File_oembed::getKV('file_id', $file->id);
75         if ($fo instanceof File_oembed) {
76             common_log(LOG_WARNING, "Strangely, a File_oembed object exists for new file $file_id", __FILE__);
77              return true;
78         }
79
80         if (isset($redir_data['oembed']['json'])
81                 && !empty($redir_data['oembed']['json'])) {
82             File_oembed::saveNew($redir_data['oembed']['json'], $file->id);
83         } elseif (isset($redir_data['type'])
84                 && (('text/html' === substr($redir_data['type'], 0, 9)
85                 || 'application/xhtml+xml' === substr($redir_data['type'], 0, 21)))) {
86
87             try {
88                 $oembed_data = File_oembed::_getOembed($given_url);
89                 if ($oembed_data === false) {
90                     throw new Exception('Did not get oEmbed data from URL');
91                 }
92             } catch (Exception $e) {
93                 return true;
94             }
95
96             File_oembed::saveNew($oembed_data, $file->id);
97         }
98         return true;
99     }
100
101     public function onEndShowAttachmentLink(HTMLOutputter $out, File $file)
102     {
103         $oembed = File_oembed::getKV('file_id', $file->id);
104         if (empty($oembed->author_name) && empty($oembed->provider)) {
105             return true;
106         }
107         $out->elementStart('div', array('id'=>'oembed_info', 'class'=>'entry-content'));
108         if (!empty($oembed->author_name)) {
109             $out->elementStart('div', 'fn vcard author');
110             if (empty($oembed->author_url)) {
111                 $out->text($oembed->author_name);
112             } else {
113                 $out->element('a', array('href' => $oembed->author_url,
114                                          'class' => 'url'),
115                                 $oembed->author_name);
116             }
117         }
118         if (!empty($oembed->provider)) {
119             $out->elementStart('div', 'fn vcard');
120             if (empty($oembed->provider_url)) {
121                 $out->text($oembed->provider);
122             } else {
123                 $out->element('a', array('href' => $oembed->provider_url,
124                                          'class' => 'url'),
125                                 $oembed->provider);
126             }
127         }
128         $out->elementEnd('div');
129     }
130
131     public function onFileEnclosureMetadata(File $file, &$enclosure)
132     {
133         // Never treat generic HTML links as an enclosure type!
134         // But if we have oEmbed info, we'll consider it golden.
135         $oembed = File_oembed::getKV('file_id', $file->id);
136         if (!$oembed instanceof File_oembed || !in_array($oembed->type, array('photo', 'video'))) {
137             return true;
138         }
139
140         foreach (array('mimetype', 'url', 'title', 'modified') as $key) {
141             if (!empty($oembed->{$key})) {
142                 $enclosure->{$key} = $oembed->{$key};
143             }
144         }
145         return true;
146     }
147     
148     public function onStartShowAttachmentRepresentation(HTMLOutputter $out, File $file)
149     {
150         $oembed = File_oembed::getKV('file_id', $file->id);
151         if (empty($oembed->type)) {
152             return true;
153         }
154         switch ($oembed->type) {
155         case 'rich':
156         case 'video':
157         case 'link':
158             if (!empty($oembed->html)) {
159                 require_once INSTALLDIR.'/extlib/htmLawed/htmLawed.php';
160                 $config = array(
161                     'safe'=>1,
162                     'elements'=>'*+object+embed');
163                 $out->raw(htmLawed($oembed->html,$config));
164             }
165             break;
166
167         case 'photo':
168             $out->element('img', array('src' => $oembed->url, 'width' => $oembed->width, 'height' => $oembed->height, 'alt' => 'alt'));
169             break;
170
171         default:
172             Event::handle('ShowUnsupportedAttachmentRepresentation', array($out, $file));
173         }
174     }
175
176     public function onPluginVersion(array &$versions)
177     {
178         $versions[] = array('name' => 'Oembed',
179                             'version' => GNUSOCIAL_VERSION,
180                             'author' => 'Mikael Nordfeldth',
181                             'homepage' => 'http://gnu.io/',
182                             'description' =>
183                             // TRANS: Plugin description.
184                             _m('Plugin for using and representing Oembed data.'));
185         return true;
186     }
187 }