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