]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - classes/File_redirection.php
Merge branch '0.8.x' into group-rss-empty
[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  * Table Definition for file_redirection
30  */
31
32 class File_redirection extends Memcached_DataObject
33 {
34     ###START_AUTOCODE
35     /* the code below is auto generated do not remove the above tag */
36
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
43
44     /* Static get */
45     function staticGet($k,$v=NULL) { return Memcached_DataObject::staticGet('File_redirection',$k,$v); }
46
47     /* the code above is auto generated do not remove the tag below */
48     ###END_AUTOCODE
49
50     function _commonCurl($url, $redirs) {
51         $curlh = curl_init();
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
61         return $curlh;
62     }
63
64     function _redirectWhere_imp($short_url, $redirs = 10, $protected = false) {
65         if ($redirs < 0) return false;
66
67         // let's see if we know this...
68         $a = File::staticGet('url', $short_url);
69         if (empty($a->id)) {
70             $b = File_redirection::staticGet('url', $short_url);
71             if (empty($b->id)) {
72                 // we'll have to figure it out
73             } else {
74                 // this is a redirect to $b->file_id
75                 $a = File::staticGet($b->file_id);
76                 $url = $a->url;
77             }
78         } else {
79             // this is a direct link to $a->url
80             $url = $a->url;
81         }
82         if (isset($url)) {
83             return $url;
84         }
85
86         $curlh = File_redirection::_commonCurl($short_url, $redirs);
87         // Don't include body in output
88         curl_setopt($curlh, CURLOPT_NOBODY, true);
89         curl_exec($curlh);
90         $info = curl_getinfo($curlh);
91         curl_close($curlh);
92
93         if (405 == $info['http_code']) {
94             $curlh = File_redirection::_commonCurl($short_url, $redirs);
95             curl_exec($curlh);
96             $info = curl_getinfo($curlh);
97             curl_close($curlh);
98         }
99
100         if (!empty($info['redirect_count']) && File::isProtected($info['url'])) {
101             return File_redirection::_redirectWhere_imp($short_url, $info['redirect_count'] - 1, true);
102         }
103
104         $ret = array('code' => $info['http_code']
105                 , 'redirects' => $info['redirect_count']
106                 , 'url' => $info['url']);
107
108         if (!empty($info['content_type'])) $ret['type'] = $info['content_type'];
109         if ($protected) $ret['protected'] = true;
110         if (!empty($info['download_content_length'])) $ret['size'] = $info['download_content_length'];
111         if (isset($info['filetime']) && ($info['filetime'] > 0)) $ret['time'] = $info['filetime'];
112         return $ret;
113     }
114
115     function where($in_url) {
116         $ret = File_redirection::_redirectWhere_imp($in_url);
117         return $ret;
118     }
119
120     function makeShort($long_url) {
121         $long_url = File_redirection::_canonUrl($long_url);
122         // do we already know this long_url and have a short redirection for it?
123         $file       = new File;
124         $file_redir = new File_redirection;
125         $file->url  = $long_url;
126         $file->joinAdd($file_redir);
127         $file->selectAdd('length(file_redirection.url) as len');
128         $file->limit(1);
129         $file->orderBy('len');
130         $file->find(true);
131         if (!empty($file->url) && (strlen($file->url) < strlen($long_url))) {
132             return $file->url;
133         }
134
135         // if yet unknown, we must find a short url according to user settings
136         $short_url = File_redirection::_userMakeShort($long_url, common_current_user());
137         return $short_url;
138     }
139
140     function _userMakeShort($long_url, $user) {
141         $short_url = common_shorten_url($long_url);
142         if ($short_url) {
143             $short_url = (string)$short_url;
144             // store it
145             $file = File::staticGet('url', $long_url);
146             if (empty($file)) {
147                 $redir_data = File_redirection::where($long_url);
148                 $file = File::saveNew($redir_data, $long_url);
149                 $file_id = $file->id;
150                 if (!empty($redir_data['oembed']['json'])) {
151                     File_oembed::saveNew($redir_data['oembed']['json'], $file_id);
152                 }
153             } else {
154                 $file_id = $file->id;
155             }
156             $file_redir = File_redirection::staticGet('url', $short_url);
157             if (empty($file_redir)) {
158                 $file_redir = new File_redirection;
159                 $file_redir->url = $short_url;
160                 $file_redir->file_id = $file_id;
161                 $file_redir->insert();
162             }
163             return $short_url;
164         }
165         return $long_url;
166     }
167
168     function _canonUrl($in_url, $default_scheme = 'http://') {
169         if (empty($in_url)) return false;
170         $out_url = $in_url;
171         $p = parse_url($out_url);
172         if (empty($p['host']) || empty($p['scheme'])) {
173             list($scheme) = explode(':', $in_url, 2);
174             switch ($scheme) {
175             case 'fax':
176             case 'tel':
177                 $out_url = str_replace('.-()', '', $out_url);
178                 break;
179
180             case 'mailto':
181             case 'aim':
182             case 'jabber':
183             case 'xmpp':
184                 // don't touch anything
185                 break;
186
187             default:
188                 $out_url = $default_scheme . ltrim($out_url, '/');
189                 $p = parse_url($out_url);
190                 if (empty($p['scheme'])) return false;
191                 break;
192             }
193         }
194
195         if (('ftp' == $p['scheme']) || ('http' == $p['scheme']) || ('https' == $p['scheme'])) {
196             if (empty($p['host'])) return false;
197             if (empty($p['path'])) {
198                 $out_url .= '/';
199             }
200         }
201
202         return $out_url;
203     }
204
205     function saveNew($data, $file_id, $url) {
206         $file_redir = new File_redirection;
207         $file_redir->url = $url;
208         $file_redir->file_id = $file_id;
209         $file_redir->redirections = intval($data['redirects']);
210         $file_redir->httpcode = intval($data['code']);
211         $file_redir->insert();
212     }
213 }
214