]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/Oembed/actions/oembed.php
Merge branch 'master' of git.gnu.io:gnu/gnu-social into mmn_fixes
[quix0rs-gnu-social.git] / plugins / Oembed / actions / oembed.php
1 <?php
2 /**
3  * StatusNet, the distributed open-source microblogging tool
4  *
5  * oEmbed data action for /main/oembed(.xml|.json) requests
6  *
7  * PHP version 5
8  *
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.
13  *
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.
18  *
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/>.
21  */
22
23 if (!defined('GNUSOCIAL')) { exit(1); }
24
25 /**
26  * Oembed provider implementation
27  *
28  * This class handles all /main/oembed(.xml|.json)/ requests.
29  *
30  * @category  oEmbed
31  * @package   GNUsocial
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/
38  */
39
40 class OembedAction extends Action
41 {
42     protected function handle()
43     {
44         parent::handle();
45
46         $url = $this->trimmed('url');
47         $tls = parse_url($url, PHP_URL_SCHEME) == 'https';
48         $root_url = common_root_url($tls);
49
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));
53         }
54
55         $path = substr($url,strlen($root_url));
56
57         $r = Router::get();
58
59         // $r->map will throw ClientException 404 if it fails to find a mapping
60         $proxy_args = $r->map($path);
61
62         $oembed=array();
63         $oembed['version']='1.0';
64         $oembed['provider_name']=common_config('site', 'name');
65         $oembed['provider_url']=common_root_url();
66
67         switch ($proxy_args['action']) {
68         case 'shownotice':
69             $oembed['type']='link';
70             try {
71                 $notice = Notice::getByID($proxy_args['notice']);
72             } catch (NoResultException $e) {
73                 throw new ClientException($e->getMessage(), 404);
74             }
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'),
79                 $authorname,
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();
85
86                         // maybe add thumbnail
87                         $attachments = $notice->attachments();
88                         if (!empty($attachments)) {
89                                 foreach ($attachments as $attachment) {
90                                         if(is_object($attachment)) {
91                                                 try {
92                                                         $thumb = $attachment->getThumbnail();
93                                                 } catch (ServerException $e) {
94                                                         //
95                                                 }
96                                                 try {
97                                                         $thumb_url = File_thumbnail::url($thumb->filename);
98                                                         $oembed['thumbnail_url'] = $thumb_url;
99                                                         break; // only first one
100                                                 } catch (ClientException $e) {
101                                                         //
102                                                 }
103                                         }
104                                 }
105                         }   
106                               
107             break;
108
109         case 'attachment':
110             $id = $proxy_args['attachment'];
111             $attachment = File::getKV($id);
112             if(empty($attachment)){
113                 // TRANS: Client error displayed in oEmbed action when attachment not found.
114                 // TRANS: %d is an attachment ID.
115                 $this->clientError(sprintf(_('Attachment %s not found.'),$id), 404);
116             }
117             if (empty($attachment->filename) && $file_oembed = File_oembed::getKV('file_id', $attachment->id)) {
118                 // Proxy the existing oembed information
119                 $oembed['type']=$file_oembed->type;
120                 $oembed['provider']=$file_oembed->provider;
121                 $oembed['provider_url']=$file_oembed->provider_url;
122                 $oembed['width']=$file_oembed->width;
123                 $oembed['height']=$file_oembed->height;
124                 $oembed['html']=$file_oembed->html;
125                 $oembed['title']=$file_oembed->title;
126                 $oembed['author_name']=$file_oembed->author_name;
127                 $oembed['author_url']=$file_oembed->author_url;
128                 $oembed['url']=$file_oembed->getUrl();
129             } elseif (substr($attachment->mimetype,0,strlen('image/'))==='image/') {
130                 $oembed['type']='photo';
131                 if ($attachment->filename) {
132                     $filepath = File::path($attachment->filename);
133                     $gis = @getimagesize($filepath);
134                     if ($gis) {
135                         $oembed['width'] = $gis[0];
136                         $oembed['height'] = $gis[1];
137                     } else {
138                         // TODO Either throw an error or find a fallback?
139                     }
140                 }
141                 $oembed['url']=$attachment->getUrl();
142                 try {
143                     $thumb = $attachment->getThumbnail();
144                     $oembed['thumbnail_url'] = $thumb->getUrl();
145                     $oembed['thumbnail_width'] = $thumb->width;
146                     $oembed['thumbnail_height'] = $thumb->height;
147                     unset($thumb);
148                 } catch (UnsupportedMediaException $e) {
149                     // No thumbnail data available
150                 }
151             } else {
152                 $oembed['type']='link';
153                 $oembed['url']=common_local_url('attachment',
154                     array('attachment' => $attachment->id));
155             }
156             if ($attachment->title) {
157                 $oembed['title']=$attachment->title;
158             }
159             break;
160         default:
161             // TRANS: Server error displayed in oEmbed request when a path is not supported.
162             // TRANS: %s is a path.
163             $this->serverError(sprintf(_('"%s" not supported for oembed requests.'),$path), 501);
164         }
165
166         switch ($this->trimmed('format')) {
167         case 'xml':
168             $this->init_document('xml');
169             $this->elementStart('oembed');
170             foreach(array(
171                         'version', 'type', 'provider_name',
172                         'provider_url', 'title', 'author_name',
173                         'author_url', 'url', 'html', 'width',
174                         'height', 'cache_age', 'thumbnail_url',
175                         'thumbnail_width', 'thumbnail_height',
176                     ) as $key) {
177                 if (isset($oembed[$key]) && $oembed[$key]!='') {
178                     $this->element($key, null, $oembed[$key]);
179                 }
180             }
181             $this->elementEnd('oembed');
182             $this->end_document('xml');
183             break;
184
185         case 'json':
186         case null:
187             $this->init_document('json');
188             $this->raw(json_encode($oembed));
189             $this->end_document('json');
190             break;
191         default:
192             // TRANS: Error message displaying attachments. %s is a raw MIME type (eg 'image/png')
193             $this->serverError(sprintf(_('Content type %s not supported.'), $apidata['content-type']), 501);
194         }
195     }
196
197     public function init_document($type)
198     {
199         switch ($type) {
200         case 'xml':
201             header('Content-Type: application/xml; charset=utf-8');
202             $this->startXML();
203             break;
204         case 'json':
205             header('Content-Type: application/json; charset=utf-8');
206
207             // Check for JSONP callback
208             $callback = $this->arg('callback');
209             if ($callback) {
210                 print $callback . '(';
211             }
212             break;
213         default:
214             // TRANS: Server error displayed in oEmbed action when request specifies an unsupported data format.
215             $this->serverError(_('Not a supported data format.'), 501);
216             break;
217         }
218     }
219
220     public function end_document($type)
221     {
222         switch ($type) {
223         case 'xml':
224             $this->endXML();
225             break;
226         case 'json':
227             // Check for JSONP callback
228             $callback = $this->arg('callback');
229             if ($callback) {
230                 print ')';
231             }
232             break;
233         default:
234             // TRANS: Server error displayed in oEmbed action when request specifies an unsupported data format.
235             $this->serverError(_('Not a supported data format.'), 501);
236             break;
237         }
238         return;
239     }
240
241     /**
242      * Is this action read-only?
243      *
244      * @param array $args other arguments
245      *
246      * @return boolean is read only action?
247      */
248     function isReadOnly($args)
249     {
250         return true;
251     }
252 }