3 class OembedPlugin extends Plugin
5 public function onCheckSchema()
7 $schema = Schema::get();
8 $schema->ensureTable('file_oembed', File_oembed::schemaDef());
12 public function onRouterInitialized(URLMapper $m)
14 $m->connect('main/oembed', array('action' => 'oembed'));
17 public function onEndShowHeadElements(Action $action)
19 switch ($action->getActionName()) {
21 $action->element('link',array('rel'=>'alternate',
22 'type'=>'application/json+oembed',
23 'href'=>common_local_url(
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(
35 array('format'=>'xml','url'=>
36 common_local_url('attachment',
37 array('attachment' => $action->attachment->id)))),
38 'title'=>'oEmbed'),null);
41 $this->element('link',array('rel'=>'alternate',
42 'type'=>'application/json+oembed',
43 'href'=>common_local_url(
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(
53 array('format'=>'xml','url'=>$this->notice->getUrl())),
54 'title'=>'oEmbed'),null);
62 * Save embedding information for a File, if applicable.
64 * Normally this event is called through File::saveNew()
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
70 * @return boolean success
72 public function onEndFileSaveNew(File $file, array $redir_data, $given_url)
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__);
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)))) {
88 $oembed_data = File_oembed::_getOembed($given_url);
89 if ($oembed_data === false) {
90 throw new Exception('Did not get oEmbed data from URL');
92 } catch (Exception $e) {
96 File_oembed::saveNew($oembed_data, $file->id);
101 public function onEndShowAttachmentLink(HTMLOutputter $out, File $file)
103 $oembed = File_oembed::getKV('file_id', $file->id);
104 if (empty($oembed->author_name) && empty($oembed->provider)) {
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);
113 $out->element('a', array('href' => $oembed->author_url,
115 $oembed->author_name);
118 if (!empty($oembed->provider)) {
119 $out->elementStart('div', 'fn vcard');
120 if (empty($oembed->provider_url)) {
121 $out->text($oembed->provider);
123 $out->element('a', array('href' => $oembed->provider_url,
128 $out->elementEnd('div');
131 public function onFileEnclosureMetadata(File $file, &$enclosure)
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'))) {
140 foreach (array('mimetype', 'url', 'title', 'modified') as $key) {
141 if (!empty($oembed->{$key})) {
142 $enclosure->{$key} = $oembed->{$key};
148 public function onStartShowAttachmentRepresentation(HTMLOutputter $out, File $file)
150 $oembed = File_oembed::getKV('file_id', $file->id);
151 if (empty($oembed->type)) {
154 switch ($oembed->type) {
158 if (!empty($oembed->html)) {
159 require_once INSTALLDIR.'/extlib/htmLawed/htmLawed.php';
162 'elements'=>'*+object+embed');
163 $out->raw(htmLawed($oembed->html,$config));
168 $out->element('img', array('src' => $oembed->url, 'width' => $oembed->width, 'height' => $oembed->height, 'alt' => 'alt'));
172 Event::handle('ShowUnsupportedAttachmentRepresentation', array($out, $file));
176 public function onPluginVersion(array &$versions)
178 $versions[] = array('name' => 'Oembed',
179 'version' => GNUSOCIAL_VERSION,
180 'author' => 'Mikael Nordfeldth',
181 'homepage' => 'http://gnu.io/',
183 // TRANS: Plugin description.
184 _m('Plugin for using and representing Oembed data.'));