3 * Laconica - a distributed open-source microblogging tool
4 * Copyright (C) 2008, Controlez-Vous, Inc.
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.
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.
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/>.
20 if (!defined('LACONICA')) { exit(1); }
22 require_once INSTALLDIR.'/classes/Memcached_DataObject.php';
23 require_once INSTALLDIR.'/classes/File.php';
24 require_once INSTALLDIR.'/classes/File_oembed.php';
26 define('USER_AGENT', 'Laconica user agent / file probe');
30 * Table Definition for file_redirection
33 class File_redirection extends Memcached_DataObject
36 /* the code below is auto generated do not remove the above tag */
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
46 function staticGet($k,$v=NULL) { return DB_DataObject::staticGet('File_redirection',$k,$v); }
48 /* the code above is auto generated do not remove the tag below */
53 function _commonCurl($url, $redirs) {
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
67 function _redirectWhere_imp($short_url, $redirs = 10, $protected = false) {
68 if ($redirs < 0) return false;
70 // let's see if we know this...
71 $a = File::staticGet('url', $short_url);
73 $b = File_redirection::staticGet('url', $short_url);
75 // we'll have to figure it out
77 // this is a redirect to $b->file_id
78 $a = File::staticGet($b->file_id);
82 // this is a direct link to $a->url
91 $curlh = File_redirection::_commonCurl($short_url, $redirs);
92 // Don't include body in output
93 curl_setopt($curlh, CURLOPT_NOBODY, true);
95 $info = curl_getinfo($curlh);
98 if (405 == $info['http_code']) {
99 $curlh = File_redirection::_commonCurl($short_url, $redirs);
101 $info = curl_getinfo($curlh);
105 if (!empty($info['redirect_count']) && File::isProtected($info['url'])) {
106 return File_redirection::_redirectWhere_imp($short_url, $info['redirect_count'] - 1, true);
109 $ret = array('code' => $info['http_code']
110 , 'redirects' => $info['redirect_count']
111 , 'url' => $info['url']);
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'];
120 function where($in_url) {
121 $ret = File_redirection::_redirectWhere_imp($in_url);
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?
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');
134 $file->orderBy('len');
136 if (!empty($file->url) && (strlen($file->url) < strlen($long_url))) {
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());
145 function _userMakeShort($long_url, $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';
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);
156 switch($user->urlshorteningservice) {
158 require_once INSTALLDIR.'/lib/Shorturl_api.php';
159 $short_url_service = new LilUrl;
160 $short_url = $short_url_service->shorten($long_url);
164 $short_url_service = new TightUrl;
165 require_once INSTALLDIR.'/lib/Shorturl_api.php';
166 $short_url = $short_url_service->shorten($long_url);
170 require_once INSTALLDIR.'/lib/Shorturl_api.php';
171 $short_url_service = new PtitUrl;
172 $short_url = $short_url_service->shorten($long_url);
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;
181 curl_setopt($curlh, CURLOPT_URL, 'http://is.gd/api.php?longurl='.urlencode($long_url));
182 $short_url = curl_exec($curlh);
185 curl_setopt($curlh, CURLOPT_URL, 'http://snipr.com/site/snip?r=simple&link='.urlencode($long_url));
186 $short_url = curl_exec($curlh);
189 curl_setopt($curlh, CURLOPT_URL, 'http://metamark.net/api/rest/simple?long_url='.urlencode($long_url));
190 $short_url = curl_exec($curlh);
193 curl_setopt($curlh, CURLOPT_URL, 'http://tinyurl.com/api-create.php?url='.urlencode($long_url));
194 $short_url = curl_exec($curlh);
203 $short_url = (string)$short_url;
205 $file = File::staticGet('url', $long_url);
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);
214 $file_id = $file->id;
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();
228 function _canonUrl($in_url, $default_scheme = 'http://') {
229 if (empty($in_url)) return false;
231 $p = parse_url($out_url);
232 if (empty($p['host']) || empty($p['scheme'])) {
233 list($scheme) = explode(':', $in_url, 2);
237 $out_url = str_replace('.-()', '', $out_url);
244 // don't touch anything
248 $out_url = $default_scheme . ltrim($out_url, '/');
249 $p = parse_url($out_url);
250 if (empty($p['scheme'])) return false;
255 if (('ftp' == $p['scheme']) || ('http' == $p['scheme']) || ('https' == $p['scheme'])) {
256 if (empty($p['host'])) return false;
257 if (empty($p['path'])) {
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();