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 $tls = parse_url($url, PHP_URL_SCHEME) == 'https';
48 $root_url = common_root_url($tls);
50 if (substr(strtolower($url),0,mb_strlen($root_url)) !== strtolower($root_url)) {
51 // TRANS: Error message displaying attachments. %s is the site's base URL.
52 throw new ClientException(sprintf(_('oEmbed data will only be provided for %s URLs.'), $root_url));
55 $path = substr($url,strlen($root_url));
59 // $r->map will throw ClientException 404 if it fails to find a mapping
60 $proxy_args = $r->map($path);
63 $oembed['version']='1.0';
64 $oembed['provider_name']=common_config('site', 'name');
65 $oembed['provider_url']=common_root_url();
67 switch ($proxy_args['action']) {
69 $oembed['type']='link';
71 $notice = Notice::getByID($proxy_args['notice']);
72 } catch (NoResultException $e) {
73 throw new ClientException($e->getMessage(), 404);
75 $profile = $notice->getProfile();
76 $authorname = $profile->getFancyName();
77 // TRANS: oEmbed title. %1$s is the author name, %2$s is the creation date.
78 $oembed['title'] = sprintf(_('%1$s\'s status on %2$s'),
80 common_exact_date($notice->created));
81 $oembed['author_name']=$authorname;
82 $oembed['author_url']=$profile->profileurl;
83 $oembed['url']=$notice->getUrl();
84 $oembed['html']=$notice->getRendered();
86 // maybe add thumbnail
87 foreach ($notice->attachments() as $attachment) {
88 if (!$attachment instanceof File) {
89 common_debug('ATTACHMENTS array entry from notice id=='._ve($notice->getID()).' is something else than a File dataobject: '._ve($attachment));
93 $thumb = $attachment->getThumbnail();
94 $thumb_url = File_thumbnail::url($thumb->filename);
95 $oembed['thumbnail_url'] = $thumb_url;
96 break; // only first one
97 } catch (UseFileAsThumbnailException $e) {
98 $oembed['thumbnail_url'] = $attachment->getUrl();
99 break; // we're happy with that
100 } catch (ServerException $e) {
102 } catch (ClientException $e) {
109 $id = $proxy_args['attachment'];
110 $attachment = File::getKV($id);
111 if(empty($attachment)){
112 // TRANS: Client error displayed in oEmbed action when attachment not found.
113 // TRANS: %d is an attachment ID.
114 $this->clientError(sprintf(_('Attachment %s not found.'),$id), 404);
116 if (empty($attachment->filename) && $file_oembed = File_oembed::getKV('file_id', $attachment->id)) {
117 // Proxy the existing oembed information
118 $oembed['type']=$file_oembed->type;
119 $oembed['provider']=$file_oembed->provider;
120 $oembed['provider_url']=$file_oembed->provider_url;
121 $oembed['width']=$file_oembed->width;
122 $oembed['height']=$file_oembed->height;
123 $oembed['html']=$file_oembed->html;
124 $oembed['title']=$file_oembed->title;
125 $oembed['author_name']=$file_oembed->author_name;
126 $oembed['author_url']=$file_oembed->author_url;
127 $oembed['url']=$file_oembed->getUrl();
128 } elseif (substr($attachment->mimetype,0,strlen('image/'))==='image/') {
129 $oembed['type']='photo';
130 if ($attachment->filename) {
131 $filepath = File::path($attachment->filename);
132 $gis = @getimagesize($filepath);
134 $oembed['width'] = $gis[0];
135 $oembed['height'] = $gis[1];
137 // TODO Either throw an error or find a fallback?
140 $oembed['url']=$attachment->getUrl();
142 $thumb = $attachment->getThumbnail();
143 $oembed['thumbnail_url'] = $thumb->getUrl();
144 $oembed['thumbnail_width'] = $thumb->width;
145 $oembed['thumbnail_height'] = $thumb->height;
147 } catch (UnsupportedMediaException $e) {
148 // No thumbnail data available
151 $oembed['type']='link';
152 $oembed['url']=common_local_url('attachment',
153 array('attachment' => $attachment->id));
155 if ($attachment->title) {
156 $oembed['title']=$attachment->title;
160 // TRANS: Server error displayed in oEmbed request when a path is not supported.
161 // TRANS: %s is a path.
162 $this->serverError(sprintf(_('"%s" not supported for oembed requests.'),$path), 501);
165 switch ($this->trimmed('format')) {
167 $this->init_document('xml');
168 $this->elementStart('oembed');
170 'version', 'type', 'provider_name',
171 'provider_url', 'title', 'author_name',
172 'author_url', 'url', 'html', 'width',
173 'height', 'cache_age', 'thumbnail_url',
174 'thumbnail_width', 'thumbnail_height',
176 if (isset($oembed[$key]) && $oembed[$key]!='') {
177 $this->element($key, null, $oembed[$key]);
180 $this->elementEnd('oembed');
181 $this->end_document('xml');
186 $this->init_document('json');
187 $this->raw(json_encode($oembed));
188 $this->end_document('json');
191 // TRANS: Error message displaying attachments. %s is a raw MIME type (eg 'image/png')
192 $this->serverError(sprintf(_('Content type %s not supported.'), $apidata['content-type']), 501);
196 public function init_document($type)
200 header('Content-Type: application/xml; charset=utf-8');
204 header('Content-Type: application/json; charset=utf-8');
206 // Check for JSONP callback
207 $callback = $this->arg('callback');
209 print $callback . '(';
213 // TRANS: Server error displayed in oEmbed action when request specifies an unsupported data format.
214 $this->serverError(_('Not a supported data format.'), 501);
219 public function end_document($type)
226 // Check for JSONP callback
227 $callback = $this->arg('callback');
233 // TRANS: Server error displayed in oEmbed action when request specifies an unsupported data format.
234 $this->serverError(_('Not a supported data format.'), 501);
241 * Is this action read-only?
243 * @param array $args other arguments
245 * @return boolean is read only action?
247 function isReadOnly($args)