3 if (!defined('GNUSOCIAL')) { exit(1); }
5 class OembedPlugin extends Plugin
7 public function onCheckSchema()
9 $schema = Schema::get();
10 $schema->ensureTable('file_oembed', File_oembed::schemaDef());
14 public function onRouterInitialized(URLMapper $m)
16 $m->connect('main/oembed', array('action' => 'oembed'));
19 public function onEndShowHeadElements(Action $action)
21 switch ($action->getActionName()) {
23 $action->element('link',array('rel'=>'alternate',
24 'type'=>'application/json+oembed',
25 'href'=>common_local_url(
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(
37 array('format'=>'xml','url'=>
38 common_local_url('attachment',
39 array('attachment' => $action->attachment->id)))),
40 'title'=>'oEmbed'),null);
44 $action->element('link',array('rel'=>'alternate',
45 'type'=>'application/json+oembed',
46 'href'=>common_local_url(
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(
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.
69 * Save embedding information for a File, if applicable.
71 * Normally this event is called through File::saveNew()
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
77 * @return boolean success
79 public function onEndFileSaveNew(File $file, array $redir_data, $given_url)
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__);
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)))) {
95 $oembed_data = File_oembed::_getOembed($given_url);
96 if ($oembed_data === false) {
97 throw new Exception('Did not get oEmbed data from URL');
99 } catch (Exception $e) {
103 File_oembed::saveNew($oembed_data, $file->id);
108 public function onEndShowAttachmentLink(HTMLOutputter $out, File $file)
110 $oembed = File_oembed::getKV('file_id', $file->id);
111 if (empty($oembed->author_name) && empty($oembed->provider)) {
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);
120 $out->element('a', array('href' => $oembed->author_url,
122 $oembed->author_name);
125 if (!empty($oembed->provider)) {
126 $out->elementStart('div', 'fn vcard');
127 if (empty($oembed->provider_url)) {
128 $out->text($oembed->provider);
130 $out->element('a', array('href' => $oembed->provider_url,
135 $out->elementEnd('div');
138 public function onFileEnclosureMetadata(File $file, &$enclosure)
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'))) {
147 foreach (array('mimetype', 'url', 'title', 'modified') as $key) {
148 if (!empty($oembed->{$key})) {
149 $enclosure->{$key} = $oembed->{$key};
155 public function onStartShowAttachmentRepresentation(HTMLOutputter $out, File $file)
157 $oembed = File_oembed::getKV('file_id', $file->id);
158 if (empty($oembed->type)) {
161 switch ($oembed->type) {
165 if (!empty($oembed->html)
166 && (StatusNet::isAjax() || common_config('attachments', 'show_html'))) {
167 require_once INSTALLDIR.'/extlib/htmLawed/htmLawed.php';
170 'elements'=>'*+object+embed');
171 $out->raw(htmLawed($oembed->html,$config));
176 $out->element('img', array('src' => $oembed->url, 'width' => $oembed->width, 'height' => $oembed->height, 'alt' => 'alt'));
180 Event::handle('ShowUnsupportedAttachmentRepresentation', array($out, $file));
184 public function onPluginVersion(array &$versions)
186 $versions[] = array('name' => 'Oembed',
187 'version' => GNUSOCIAL_VERSION,
188 'author' => 'Mikael Nordfeldth',
189 'homepage' => 'http://gnu.io/',
191 // TRANS: Plugin description.
192 _m('Plugin for using and representing Oembed data.'));