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