3 * StatusNet - the distributed open-source microblogging tool
4 * Copyright (C) 2008, 2009, StatusNet, 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('STATUSNET') && !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', 'StatusNet user agent / file probe');
29 * Table Definition for file_redirection
32 class File_redirection extends Memcached_DataObject
35 /* the code below is auto generated do not remove the above tag */
37 public $__table = 'file_redirection'; // table name
38 public $url; // varchar(255) primary_key not_null
39 public $file_id; // int(4)
40 public $redirections; // int(4)
41 public $httpcode; // int(4)
42 public $modified; // timestamp() not_null default_CURRENT_TIMESTAMP
45 function staticGet($k,$v=NULL) { return Memcached_DataObject::staticGet('File_redirection',$k,$v); }
47 /* the code above is auto generated do not remove the tag below */
50 function _commonCurl($url, $redirs) {
52 curl_setopt($curlh, CURLOPT_URL, $url);
53 curl_setopt($curlh, CURLOPT_AUTOREFERER, true); // # setup referer header when folowing redirects
54 curl_setopt($curlh, CURLOPT_CONNECTTIMEOUT, 10); // # seconds to wait
55 curl_setopt($curlh, CURLOPT_MAXREDIRS, $redirs); // # max number of http redirections to follow
56 curl_setopt($curlh, CURLOPT_USERAGENT, USER_AGENT);
57 curl_setopt($curlh, CURLOPT_FOLLOWLOCATION, true); // Follow redirects
58 curl_setopt($curlh, CURLOPT_RETURNTRANSFER, true);
59 curl_setopt($curlh, CURLOPT_FILETIME, true);
60 curl_setopt($curlh, CURLOPT_HEADER, true); // Include header in output
64 function _redirectWhere_imp($short_url, $redirs = 10, $protected = false) {
65 if ($redirs < 0) return false;
67 // let's see if we know this...
68 $a = File::staticGet('url', $short_url);
71 // this is a direct link to $a->url
74 $b = File_redirection::staticGet('url', $short_url);
76 // this is a redirect to $b->file_id
77 $a = File::staticGet('id', $b->file_id);
82 if(strpos($short_url,'://') === false){
85 $curlh = File_redirection::_commonCurl($short_url, $redirs);
86 // Don't include body in output
87 curl_setopt($curlh, CURLOPT_NOBODY, true);
89 $info = curl_getinfo($curlh);
92 if (405 == $info['http_code']) {
93 $curlh = File_redirection::_commonCurl($short_url, $redirs);
95 $info = curl_getinfo($curlh);
99 if (!empty($info['redirect_count']) && File::isProtected($info['url'])) {
100 return File_redirection::_redirectWhere_imp($short_url, $info['redirect_count'] - 1, true);
103 $ret = array('code' => $info['http_code']
104 , 'redirects' => $info['redirect_count']
105 , 'url' => $info['url']);
107 if (!empty($info['content_type'])) $ret['type'] = $info['content_type'];
108 if ($protected) $ret['protected'] = true;
109 if (!empty($info['download_content_length'])) $ret['size'] = $info['download_content_length'];
110 if (isset($info['filetime']) && ($info['filetime'] > 0)) $ret['time'] = $info['filetime'];
114 function where($in_url) {
115 $ret = File_redirection::_redirectWhere_imp($in_url);
119 function makeShort($long_url) {
121 $canon = File_redirection::_canonUrl($long_url);
123 $short_url = File_redirection::_userMakeShort($canon);
125 // Did we get one? Is it shorter?
126 if (!empty($short_url) && mb_strlen($short_url) < mb_strlen($long_url)) {
133 function _userMakeShort($long_url) {
134 $short_url = common_shorten_url($long_url);
135 if (!empty($short_url) && $short_url != $long_url) {
136 $short_url = (string)$short_url;
138 $file = File::staticGet('url', $long_url);
140 $redir_data = File_redirection::where($long_url);
141 $file = File::saveNew($redir_data, $long_url);
142 $file_id = $file->id;
143 if (!empty($redir_data['oembed']['json'])) {
144 File_oembed::saveNew($redir_data['oembed']['json'], $file_id);
147 $file_id = $file->id;
149 $file_redir = File_redirection::staticGet('url', $short_url);
150 if (empty($file_redir)) {
151 $file_redir = new File_redirection;
152 $file_redir->url = $short_url;
153 $file_redir->file_id = $file_id;
154 $file_redir->insert();
161 function _canonUrl($in_url, $default_scheme = 'http://') {
162 if (empty($in_url)) return false;
164 $p = parse_url($out_url);
165 if (empty($p['host']) || empty($p['scheme'])) {
166 list($scheme) = explode(':', $in_url, 2);
170 $out_url = str_replace('.-()', '', $out_url);
177 // don't touch anything
181 $out_url = $default_scheme . ltrim($out_url, '/');
182 $p = parse_url($out_url);
183 if (empty($p['scheme'])) return false;
188 if (('ftp' == $p['scheme']) || ('ftps' == $p['scheme']) || ('http' == $p['scheme']) || ('https' == $p['scheme'])) {
189 if (empty($p['host'])) return false;
190 if (empty($p['path'])) {
198 function saveNew($data, $file_id, $url) {
199 $file_redir = new File_redirection;
200 $file_redir->url = $url;
201 $file_redir->file_id = $file_id;
202 $file_redir->redirections = intval($data['redirects']);
203 $file_redir->httpcode = intval($data['code']);
204 $file_redir->insert();