3 * StatusNet, the distributed open-source microblogging tool
5 * oEmbed data action for /main/oembed(.xml|.json) requests
9 * LICENCE: This program is free software: you can redistribute it and/or modify
10 * it under the terms of the GNU Affero General Public License as published by
11 * the Free Software Foundation, either version 3 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU Affero General Public License for more details.
19 * You should have received a copy of the GNU Affero General Public License
20 * along with this program. If not, see <http://www.gnu.org/licenses/>.
23 if (!defined('GNUSOCIAL')) { exit(1); }
26 * Oembed provider implementation
28 * This class handles all /main/oembed(.xml|.json)/ requests.
32 * @author Craig Andrews <candrews@integralblue.com>
33 * @author Mikael Nordfeldth <mmn@hethane.se>
34 * @copyright 2008 StatusNet, Inc.
35 * @copyright 2014 Free Software Foundation, Inc.
36 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
37 * @link http://status.net/
40 class OembedAction extends Action
42 protected function handle()
46 $url = $this->trimmed('url');
47 if (substr(strtolower($url),0,strlen(common_root_url())) !== strtolower(common_root_url())) {
48 // TRANS: Error message displaying attachments. %s is the site's base URL.
49 $this->clientError(sprintf(_('oEmbed data will only be provided for %s URLs.'), common_root_url()), 400);
52 $path = substr($url,strlen(common_root_url()));
56 $proxy_args = $r->map($path);
58 // TRANS: Client error displayed in oEmbed action when path not found.
59 // TRANS: %s is a path.
60 $this->clientError(sprintf(_('"%s" not found.'),$path), 404);
64 $oembed['version']='1.0';
65 $oembed['provider_name']=common_config('site', 'name');
66 $oembed['provider_url']=common_root_url();
68 switch ($proxy_args['action']) {
70 $oembed['type']='link';
71 $id = $proxy_args['notice'];
72 $notice = Notice::getKV($id);
74 // TRANS: Client error displayed in oEmbed action when notice not found.
75 // TRANS: %s is a notice.
76 $this->clientError(sprintf(_("Notice %s not found."),$id), 404);
78 $profile = $notice->getProfile();
79 if (empty($profile)) {
80 // TRANS: Server error displayed in oEmbed action when notice has not profile.
81 $this->serverError(_('Notice has no profile.'), 500);
83 $authorname = $profile->getFancyName();
84 // TRANS: oEmbed title. %1$s is the author name, %2$s is the creation date.
85 $oembed['title'] = sprintf(_('%1$s\'s status on %2$s'),
87 common_exact_date($notice->created));
88 $oembed['author_name']=$authorname;
89 $oembed['author_url']=$profile->profileurl;
90 $oembed['url']=$notice->getUrl();
91 $oembed['html']=$notice->getRendered();
95 $id = $proxy_args['attachment'];
96 $attachment = File::getKV($id);
97 if(empty($attachment)){
98 // TRANS: Client error displayed in oEmbed action when attachment not found.
99 // TRANS: %d is an attachment ID.
100 $this->clientError(sprintf(_('Attachment %s not found.'),$id), 404);
102 if (empty($attachment->filename) && $file_oembed = File_oembed::getKV('file_id', $attachment->id)) {
103 // Proxy the existing oembed information
104 $oembed['type']=$file_oembed->type;
105 $oembed['provider']=$file_oembed->provider;
106 $oembed['provider_url']=$file_oembed->provider_url;
107 $oembed['width']=$file_oembed->width;
108 $oembed['height']=$file_oembed->height;
109 $oembed['html']=$file_oembed->html;
110 $oembed['title']=$file_oembed->title;
111 $oembed['author_name']=$file_oembed->author_name;
112 $oembed['author_url']=$file_oembed->author_url;
113 $oembed['url']=$file_oembed->getUrl();
114 } elseif (substr($attachment->mimetype,0,strlen('image/'))==='image/') {
115 $oembed['type']='photo';
116 if ($attachment->filename) {
117 $filepath = File::path($attachment->filename);
118 $gis = @getimagesize($filepath);
120 $oembed['width'] = $gis[0];
121 $oembed['height'] = $gis[1];
123 // TODO Either throw an error or find a fallback?
126 $oembed['url']=$attachment->getUrl();
128 $thumb = $attachment->getThumbnail();
129 $oembed['thumbnail_url'] = $thumb->getUrl();
130 $oembed['thumbnail_width'] = $thumb->width;
131 $oembed['thumbnail_height'] = $thumb->height;
133 } catch (UnsupportedMediaException $e) {
134 // No thumbnail data available
137 $oembed['type']='link';
138 $oembed['url']=common_local_url('attachment',
139 array('attachment' => $attachment->id));
141 if ($attachment->title) {
142 $oembed['title']=$attachment->title;
146 // TRANS: Server error displayed in oEmbed request when a path is not supported.
147 // TRANS: %s is a path.
148 $this->serverError(sprintf(_('"%s" not supported for oembed requests.'),$path), 501);
151 switch ($this->trimmed('format')) {
153 $this->init_document('xml');
154 $this->elementStart('oembed');
156 'version', 'type', 'provider_name',
157 'provider_url', 'title', 'author_name',
158 'author_url', 'url', 'html', 'width',
159 'height', 'cache_age', 'thumbnail_url',
160 'thumbnail_width', 'thumbnail_height',
162 if (isset($oembed[$key]) && $oembed[$key]!='') {
163 $this->element($key, null, $oembed[$key]);
166 $this->elementEnd('oembed');
167 $this->end_document('xml');
172 $this->init_document('json');
173 $this->raw(json_encode($oembed));
174 $this->end_document('json');
177 // TRANS: Error message displaying attachments. %s is a raw MIME type (eg 'image/png')
178 $this->serverError(sprintf(_('Content type %s not supported.'), $apidata['content-type']), 501);
182 public function init_document($type)
186 header('Content-Type: application/xml; charset=utf-8');
190 header('Content-Type: application/json; charset=utf-8');
192 // Check for JSONP callback
193 $callback = $this->arg('callback');
195 print $callback . '(';
199 // TRANS: Server error displayed in oEmbed action when request specifies an unsupported data format.
200 $this->serverError(_('Not a supported data format.'), 501);
205 public function end_document($type)
212 // Check for JSONP callback
213 $callback = $this->arg('callback');
219 // TRANS: Server error displayed in oEmbed action when request specifies an unsupported data format.
220 $this->serverError(_('Not a supported data format.'), 501);
227 * Is this action read-only?
229 * @param array $args other arguments
231 * @return boolean is read only action?
233 function isReadOnly($args)