]> git.immae.eu Git - github/wallabag/wallabag.git/blame - inc/poche/Tools.class.php
I removed my previous commit. We have to create a new branch for that.
[github/wallabag/wallabag.git] / inc / poche / Tools.class.php
CommitLineData
eb1af592
NL
1<?php
2/**
c95b78a8 3 * wallabag, self hostable application allowing you to not miss any content anymore
eb1af592 4 *
c95b78a8
NL
5 * @category wallabag
6 * @author Nicolas Lœuillet <nicolas@loeuillet.org>
eb1af592
NL
7 * @copyright 2013
8 * @license http://www.wtfpl.net/ see COPYING file
9 */
182faf26 10
eb1af592
NL
11class Tools
12{
13 public static function initPhp()
14 {
15 define('START_TIME', microtime(true));
16
17 if (phpversion() < 5) {
18 die(_('Oops, it seems you don\'t have PHP 5.'));
19 }
20
eb1af592
NL
21 function stripslashesDeep($value) {
22 return is_array($value)
23 ? array_map('stripslashesDeep', $value)
24 : stripslashes($value);
25 }
26
27 if (get_magic_quotes_gpc()) {
28 $_POST = array_map('stripslashesDeep', $_POST);
29 $_GET = array_map('stripslashesDeep', $_GET);
30 $_COOKIE = array_map('stripslashesDeep', $_COOKIE);
31 }
32
33 ob_start();
34 register_shutdown_function('ob_end_flush');
35 }
36
37 public static function getPocheUrl()
38 {
39 $https = (!empty($_SERVER['HTTPS'])
40 && (strtolower($_SERVER['HTTPS']) == 'on'))
41 || (isset($_SERVER["SERVER_PORT"])
125f9ee8 42 && $_SERVER["SERVER_PORT"] == '443') // HTTPS detection.
182faf26 43 || (isset($_SERVER["SERVER_PORT"]) //Custom HTTPS port detection
445a1a1c
NL
44 && $_SERVER["SERVER_PORT"] == SSL_PORT)
45 || (isset($_SERVER['HTTP_X_FORWARDED_PROTO'])
46 && $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https');
125f9ee8 47
eb1af592
NL
48 $serverport = (!isset($_SERVER["SERVER_PORT"])
49 || $_SERVER["SERVER_PORT"] == '80'
50 || ($https && $_SERVER["SERVER_PORT"] == '443')
2916d24b 51 || ($https && $_SERVER["SERVER_PORT"]==SSL_PORT) //Custom HTTPS port detection
eb1af592
NL
52 ? '' : ':' . $_SERVER["SERVER_PORT"]);
53
54 $scriptname = str_replace('/index.php', '/', $_SERVER["SCRIPT_NAME"]);
55
45e9e0f5 56 if (!isset($_SERVER["HTTP_HOST"])) {
eb1af592
NL
57 return $scriptname;
58 }
59
69c57493 60 $host = (isset($_SERVER['HTTP_X_FORWARDED_HOST']) ? $_SERVER['HTTP_X_FORWARDED_HOST'] : (isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : $_SERVER['SERVER_NAME']));
79024eb0
NL
61
62 if (strpos($host, ':') !== false) {
63 $serverport = '';
64 }
752cd4a8 65
eb1af592 66 return 'http' . ($https ? 's' : '') . '://'
69c57493 67 . $host . $serverport . $scriptname;
eb1af592
NL
68 }
69
70 public static function redirect($url = '')
71 {
72 if ($url === '') {
73 $url = (empty($_SERVER['HTTP_REFERER'])?'?':$_SERVER['HTTP_REFERER']);
74 if (isset($_POST['returnurl'])) {
75 $url = $_POST['returnurl'];
76 }
77 }
78
79 # prevent loop
80 if (empty($url) || parse_url($url, PHP_URL_QUERY) === $_SERVER['QUERY_STRING']) {
81 $url = Tools::getPocheUrl();
82 }
83
84 if (substr($url, 0, 1) !== '?') {
85 $ref = Tools::getPocheUrl();
86 if (substr($url, 0, strlen($ref)) !== $ref) {
87 $url = $ref;
88 }
89 }
bc1ee852 90 self::logm('redirect to ' . $url);
eb1af592
NL
91 header('Location: '.$url);
92 exit();
93 }
94
95 public static function getTplFile($view)
96 {
74ec445a
NL
97 $views = array(
98 'install', 'import', 'export', 'config', 'tags',
032e0ca1 99 'edit-tags', 'view', 'login', 'error'
74ec445a
NL
100 );
101
102 if (in_array($view, $views)) {
103 return $view . '.twig';
eb1af592 104 }
74ec445a
NL
105
106 return 'home.twig';
eb1af592
NL
107 }
108
109 public static function getFile($url)
110 {
111 $timeout = 15;
112 $useragent = "Mozilla/5.0 (Windows NT 5.1; rv:18.0) Gecko/20100101 Firefox/18.0";
113
114 if (in_array ('curl', get_loaded_extensions())) {
115 # Fetch feed from URL
116 $curl = curl_init();
117 curl_setopt($curl, CURLOPT_URL, $url);
118 curl_setopt($curl, CURLOPT_TIMEOUT, $timeout);
f2d3ee98
NL
119 if (!ini_get('open_basedir') && !ini_get('safe_mode')) {
120 curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
121 }
eb1af592
NL
122 curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
123 curl_setopt($curl, CURLOPT_HEADER, false);
124
125 # for ssl, do not verified certificate
126 curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
127 curl_setopt($curl, CURLOPT_AUTOREFERER, TRUE );
128
129 # FeedBurner requires a proper USER-AGENT...
130 curl_setopt($curl, CURL_HTTP_VERSION_1_1, true);
131 curl_setopt($curl, CURLOPT_ENCODING, "gzip, deflate");
132 curl_setopt($curl, CURLOPT_USERAGENT, $useragent);
133
134 $data = curl_exec($curl);
135 $httpcode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
136 $httpcodeOK = isset($httpcode) and ($httpcode == 200 or $httpcode == 301);
137 curl_close($curl);
138 } else {
139 # create http context and add timeout and user-agent
140 $context = stream_context_create(
141 array(
142 'http' => array(
143 'timeout' => $timeout,
144 'header' => "User-Agent: " . $useragent,
145 'follow_location' => true
146 ),
147 'ssl' => array(
148 'verify_peer' => false,
149 'allow_self_signed' => true
150 )
151 )
152 );
153
154 # only download page lesser than 4MB
182faf26 155 $data = @file_get_contents($url, false, $context, -1, 4000000);
eb1af592
NL
156
157 if (isset($http_response_header) and isset($http_response_header[0])) {
158 $httpcodeOK = isset($http_response_header) and isset($http_response_header[0]) and ((strpos($http_response_header[0], '200 OK') !== FALSE) or (strpos($http_response_header[0], '301 Moved Permanently') !== FALSE));
159 }
160 }
161
162 # if response is not empty and response is OK
163 if (isset($data) and isset($httpcodeOK) and $httpcodeOK) {
164
165 # take charset of page and get it
166 preg_match('#<meta .*charset=.*>#Usi', $data, $meta);
167
168 # if meta tag is found
169 if (!empty($meta[0])) {
170 preg_match('#charset="?(.*)"#si', $meta[0], $encoding);
171 # if charset is found set it otherwise, set it to utf-8
172 $html_charset = (!empty($encoding[1])) ? strtolower($encoding[1]) : 'utf-8';
5f9bff0f 173 if (empty($encoding[1])) $encoding[1] = 'utf-8';
eb1af592
NL
174 } else {
175 $html_charset = 'utf-8';
176 $encoding[1] = '';
177 }
178
179 # replace charset of url to charset of page
180 $data = str_replace('charset=' . $encoding[1], 'charset=' . $html_charset, $data);
181
182 return $data;
183 }
184 else {
185 return FALSE;
186 }
187 }
188
189 public static function renderJson($data)
190 {
191 header('Cache-Control: no-cache, must-revalidate');
192 header('Expires: Sat, 26 Jul 1997 05:00:00 GMT');
193 header('Content-type: application/json; charset=UTF-8');
194 echo json_encode($data);
195 exit();
196 }
197
198 public static function logm($message)
199 {
53e3158d 200 if (DEBUG_POCHE && php_sapi_name() != 'cli') {
eb1af592 201 $t = strval(date('Y/m/d_H:i:s')) . ' - ' . $_SERVER["REMOTE_ADDR"] . ' - ' . strval($message) . "\n";
6a361945 202 file_put_contents(CACHE . '/log.txt', $t, FILE_APPEND);
bc1ee852 203 error_log('DEBUG POCHE : ' . $message);
eb1af592
NL
204 }
205 }
206
182faf26 207 public static function encodeString($string)
eb1af592
NL
208 {
209 return sha1($string . SALT);
210 }
63c35580 211
7f959169 212 public static function checkVar($var, $default = '')
63c35580 213 {
7f959169 214 return ((isset ($_REQUEST["$var"])) ? htmlentities($_REQUEST["$var"]) : $default);
63c35580 215 }
55821e04 216
6400371f
NL
217 public static function getDomain($url)
218 {
219 return parse_url($url, PHP_URL_HOST);
220 }
221
222 public static function getReadingTime($text) {
223 $word = str_word_count(strip_tags($text));
224 $minutes = floor($word / 200);
225 $seconds = floor($word % 200 / (200 / 60));
226 $time = array('minutes' => $minutes, 'seconds' => $seconds);
227
228 return $minutes;
229 }
230
1b2abab6
N
231 public static function getDocLanguage($userlanguage) {
232 $lang = explode('.', $userlanguage);
233 return str_replace('_', '-', $lang[0]);
234 }
d460914f
NL
235
236 public static function status($status_code)
237 {
238 if (strpos(php_sapi_name(), 'apache') !== false) {
239
240 header('HTTP/1.0 '.$status_code);
241 }
242 else {
243
244 header('Status: '.$status_code);
245 }
246 }
247
d460914f
NL
248 public static function download_db() {
249 header('Content-Disposition: attachment; filename="poche.sqlite.gz"');
250 self::status(200);
251
252 header('Content-Transfer-Encoding: binary');
253 header('Content-Type: application/octet-stream');
254 echo gzencode(file_get_contents(STORAGE_SQLITE));
255
256 exit;
257 }
53e3158d
NL
258
259 public static function getPageContent(Url $url)
260 {
261 // Saving and clearing context
262 $REAL = array();
263 foreach( $GLOBALS as $key => $value ) {
264 if( $key != 'GLOBALS' && $key != '_SESSION' && $key != 'HTTP_SESSION_VARS' ) {
265 $GLOBALS[$key] = array();
266 $REAL[$key] = $value;
267 }
268 }
269 // Saving and clearing session
f98373cc
MR
270 if ( isset($_SESSION) ) {
271 $REAL_SESSION = array();
272 foreach( $_SESSION as $key => $value ) {
273 $REAL_SESSION[$key] = $value;
274 unset($_SESSION[$key]);
275 }
53e3158d
NL
276 }
277
278 // Running code in different context
279 $scope = function() {
280 extract( func_get_arg(1) );
281 $_GET = $_REQUEST = array(
282 "url" => $url->getUrl(),
283 "max" => 5,
284 "links" => "preserve",
285 "exc" => "",
286 "format" => "json",
287 "submit" => "Create Feed"
288 );
289 ob_start();
290 require func_get_arg(0);
f98373cc
MR
291 $json = ob_get_contents();
292 ob_end_clean();
53e3158d
NL
293 return $json;
294 };
295 $json = $scope( "inc/3rdparty/makefulltextfeed.php", array("url" => $url) );
296
297 // Clearing and restoring context
298 foreach( $GLOBALS as $key => $value ) {
299 if( $key != "GLOBALS" && $key != "_SESSION" ) {
300 unset($GLOBALS[$key]);
301 }
302 }
303 foreach( $REAL as $key => $value ) {
304 $GLOBALS[$key] = $value;
305 }
306 // Clearing and restoring session
f98373cc
MR
307 if ( isset($REAL_SESSION) ) {
308 foreach( $_SESSION as $key => $value ) {
309 unset($_SESSION[$key]);
310 }
311 foreach( $REAL_SESSION as $key => $value ) {
312 $_SESSION[$key] = $value;
313 }
53e3158d 314 }
f98373cc 315
53e3158d
NL
316 return json_decode($json, true);
317 }
fb26cc93
MR
318
319 /**
320 * Returns whether we handle an AJAX (XMLHttpRequest) request.
321 * @return boolean whether we handle an AJAX (XMLHttpRequest) request.
322 */
323 public static function isAjaxRequest()
324 {
325 return isset($_SERVER['HTTP_X_REQUESTED_WITH']) && $_SERVER['HTTP_X_REQUESTED_WITH']==='XMLHttpRequest';
326 }
327
125f9ee8 328}