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