]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - classes/File_redirection.php
Merge branch '0.8.x' of git@gitorious.org:laconica/dev into 0.8.x
[quix0rs-gnu-social.git] / classes / File_redirection.php
1 <?php
2 /*
3  * Laconica - a distributed open-source microblogging tool
4  * Copyright (C) 2008, 2009, Control Yourself, Inc.
5  *
6  * This program is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU Affero General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.     See the
14  * GNU Affero General Public License for more details.
15  *
16  * You should have received a copy of the GNU Affero General Public License
17  * along with this program.     If not, see <http://www.gnu.org/licenses/>.
18  */
19
20 if (!defined('LACONICA')) { exit(1); }
21
22 require_once INSTALLDIR.'/classes/Memcached_DataObject.php';
23 require_once INSTALLDIR.'/classes/File.php';
24 require_once INSTALLDIR.'/classes/File_oembed.php';
25
26 define('USER_AGENT', 'Laconica user agent / file probe');
27
28
29 /**
30  * Table Definition for file_redirection
31  */
32
33 class File_redirection extends Memcached_DataObject 
34 {
35     ###START_AUTOCODE
36     /* the code below is auto generated do not remove the above tag */
37
38     public $__table = 'file_redirection';                // table name
39     public $id;                              // int(11)  not_null primary_key group_by
40     public $url;                             // varchar(255)  unique_key
41     public $file_id;                         // int(11)  group_by
42     public $redirections;                    // int(11)  group_by
43     public $httpcode;                        // int(11)  group_by
44
45     /* Static get */
46     function staticGet($k,$v=NULL) { return DB_DataObject::staticGet('File_redirection',$k,$v); }
47
48     /* the code above is auto generated do not remove the tag below */
49     ###END_AUTOCODE
50
51
52
53     function _commonCurl($url, $redirs) {
54         $curlh = curl_init();
55         curl_setopt($curlh, CURLOPT_URL, $url);
56         curl_setopt($curlh, CURLOPT_AUTOREFERER, true); // # setup referer header when folowing redirects
57         curl_setopt($curlh, CURLOPT_CONNECTTIMEOUT, 10); // # seconds to wait
58         curl_setopt($curlh, CURLOPT_MAXREDIRS, $redirs); // # max number of http redirections to follow
59         curl_setopt($curlh, CURLOPT_USERAGENT, USER_AGENT);
60         curl_setopt($curlh, CURLOPT_FOLLOWLOCATION, true); // Follow redirects
61         curl_setopt($curlh, CURLOPT_RETURNTRANSFER, true);
62         curl_setopt($curlh, CURLOPT_FILETIME, true);
63         curl_setopt($curlh, CURLOPT_HEADER, true); // Include header in output
64         return $curlh;
65     }
66
67     function _redirectWhere_imp($short_url, $redirs = 10, $protected = false) {
68         if ($redirs < 0) return false;
69
70         // let's see if we know this...
71         $a = File::staticGet('url', $short_url);
72         if (empty($a->id)) {
73             $b = File_redirection::staticGet('url', $short_url);
74             if (empty($b->id)) {
75                 // we'll have to figure it out
76             } else {
77                 // this is a redirect to $b->file_id
78                 $a = File::staticGet($b->file_id);
79                 $url = $a->url;
80             }
81         } else {
82             // this is a direct link to $a->url
83             $url = $a->url;
84         }
85         if (isset($url)) {
86             return $url;
87         }
88
89
90
91         $curlh = File_redirection::_commonCurl($short_url, $redirs);
92         // Don't include body in output
93         curl_setopt($curlh, CURLOPT_NOBODY, true);
94         curl_exec($curlh);
95         $info = curl_getinfo($curlh);
96         curl_close($curlh);
97
98         if (405 == $info['http_code']) {
99             $curlh = File_redirection::_commonCurl($short_url, $redirs);
100             curl_exec($curlh);
101             $info = curl_getinfo($curlh);
102             curl_close($curlh);
103         }
104
105         if (!empty($info['redirect_count']) && File::isProtected($info['url'])) {
106             return File_redirection::_redirectWhere_imp($short_url, $info['redirect_count'] - 1, true);
107         }
108
109         $ret = array('code' => $info['http_code']
110                 , 'redirects' => $info['redirect_count']
111                 , 'url' => $info['url']);
112
113         if (!empty($info['content_type'])) $ret['type'] = $info['content_type'];
114         if ($protected) $ret['protected'] = true;
115         if (!empty($info['download_content_length'])) $ret['size'] = $info['download_content_length'];
116         if (isset($info['filetime']) && ($info['filetime'] > 0)) $ret['time'] = $info['filetime'];
117         return $ret;
118     }
119
120     function where($in_url) {
121         $ret = File_redirection::_redirectWhere_imp($in_url);
122         return $ret;
123     }
124
125     function makeShort($long_url) {
126         $long_url = File_redirection::_canonUrl($long_url);
127         // do we already know this long_url and have a short redirection for it?
128         $file       = new File;
129         $file_redir = new File_redirection;
130         $file->url  = $long_url;
131         $file->joinAdd($file_redir);
132         $file->selectAdd('length(file_redirection.url) as len');
133         $file->limit(1);
134         $file->orderBy('len');
135         $file->find(true);
136         if (!empty($file->url) && (strlen($file->url) < strlen($long_url))) {
137             return $file->url;
138         }
139
140         // if yet unknown, we must find a short url according to user settings
141         $short_url = File_redirection::_userMakeShort($long_url, common_current_user());
142         return $short_url;
143     }
144
145     function _userMakeShort($long_url, $user) {
146         if (empty($user)) {
147             // common current user does not find a user when called from the XMPP daemon
148             // therefore we'll set one here fix, so that XMPP given URLs may be shortened
149             $user->urlshorteningservice = 'ur1.ca';
150         }
151         $curlh = curl_init();
152         curl_setopt($curlh, CURLOPT_CONNECTTIMEOUT, 20); // # seconds to wait
153         curl_setopt($curlh, CURLOPT_USERAGENT, 'Laconica');
154         curl_setopt($curlh, CURLOPT_RETURNTRANSFER, true);
155
156         switch($user->urlshorteningservice) {
157             case 'ur1.ca':
158                 require_once INSTALLDIR.'/lib/Shorturl_api.php';
159                 $short_url_service = new LilUrl;
160                 $short_url = $short_url_service->shorten($long_url);
161                 break;
162
163             case '2tu.us':
164                 $short_url_service = new TightUrl;
165                 require_once INSTALLDIR.'/lib/Shorturl_api.php';
166                 $short_url = $short_url_service->shorten($long_url);
167                 break;
168
169             case 'ptiturl.com':
170                 require_once INSTALLDIR.'/lib/Shorturl_api.php';
171                 $short_url_service = new PtitUrl;
172                 $short_url = $short_url_service->shorten($long_url);
173                 break;
174
175             case 'bit.ly':
176                 curl_setopt($curlh, CURLOPT_URL, 'http://bit.ly/api?method=shorten&long_url='.urlencode($long_url));
177                 $short_url = current(json_decode(curl_exec($curlh))->results)->hashUrl;
178                 break;
179
180             case 'is.gd':
181                 curl_setopt($curlh, CURLOPT_URL, 'http://is.gd/api.php?longurl='.urlencode($long_url));
182                 $short_url = curl_exec($curlh);
183                 break;
184             case 'snipr.com':
185                 curl_setopt($curlh, CURLOPT_URL, 'http://snipr.com/site/snip?r=simple&link='.urlencode($long_url));
186                 $short_url = curl_exec($curlh);
187                 break;
188             case 'metamark.net':
189                 curl_setopt($curlh, CURLOPT_URL, 'http://metamark.net/api/rest/simple?long_url='.urlencode($long_url));
190                 $short_url = curl_exec($curlh);
191                 break;
192             case 'tinyurl.com':
193                 curl_setopt($curlh, CURLOPT_URL, 'http://tinyurl.com/api-create.php?url='.urlencode($long_url));
194                 $short_url = curl_exec($curlh);
195                 break;
196             default:
197                 $short_url = false;
198         }
199
200         curl_close($curlh);
201
202         if ($short_url) {
203             $short_url = (string)$short_url;
204             // store it
205             $file = File::staticGet('url', $long_url);
206             if (empty($file)) {
207                 $redir_data = File_redirection::where($long_url);
208                 $file = File::saveNew($redir_data, $long_url);
209                 $file_id = $file->id;
210                 if (!empty($redir_data['oembed']['json'])) {
211                     File_oembed::saveNew($redir_data['oembed']['json'], $file_id);
212                 }
213             } else {
214                 $file_id = $file->id;
215             }
216             $file_redir = File_redirection::staticGet('url', $short_url);
217             if (empty($file_redir)) {
218                 $file_redir = new File_redirection;
219                 $file_redir->url = $short_url;
220                 $file_redir->file_id = $file_id;
221                 $file_redir->insert();
222             }
223             return $short_url;
224         }
225         return $long_url;
226     }
227
228     function _canonUrl($in_url, $default_scheme = 'http://') {
229         if (empty($in_url)) return false;
230         $out_url = $in_url;
231         $p = parse_url($out_url);
232         if (empty($p['host']) || empty($p['scheme'])) {
233             list($scheme) = explode(':', $in_url, 2);
234             switch ($scheme) {
235             case 'fax':
236             case 'tel':
237                 $out_url = str_replace('.-()', '', $out_url);
238                 break;
239
240             case 'mailto':
241             case 'aim':
242             case 'jabber':
243             case 'xmpp':
244                 // don't touch anything
245                 break;
246
247             default:
248                 $out_url = $default_scheme . ltrim($out_url, '/');
249                 $p = parse_url($out_url);
250                 if (empty($p['scheme'])) return false;
251                 break;
252             }
253         }
254
255         if (('ftp' == $p['scheme']) || ('http' == $p['scheme']) || ('https' == $p['scheme'])) {
256             if (empty($p['host'])) return false;
257             if (empty($p['path'])) {
258                 $out_url .= '/';
259             }
260         }
261
262         return $out_url;
263     }
264
265     function saveNew($data, $file_id, $url) {
266         $file_redir = new File_redirection;
267         $file_redir->url = $url;
268         $file_redir->file_id = $file_id;
269         $file_redir->redirections = intval($data['redirects']);
270         $file_redir->httpcode = intval($data['code']);
271         $file_redir->insert();
272     }
273 }
274