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