]> git.immae.eu Git - github/wallabag/wallabag.git/blame - src/Wallabag/Wallabag/Tools.php
new folders
[github/wallabag/wallabag.git] / src / Wallabag / Wallabag / Tools.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 7 * @copyright 2013
3602405e 8 * @license http://opensource.org/licenses/MIT see COPYING file
eb1af592 9 */
182faf26 10
6ad93dff
NL
11namespace Wallabag\Wallabag;
12
3602405e 13final class Tools
eb1af592 14{
3602405e
NL
15 /**
16 * Initialize PHP environment
17 */
eb1af592
NL
18 public static function initPhp()
19 {
20 define('START_TIME', microtime(true));
21
eb1af592
NL
22 function stripslashesDeep($value) {
23 return is_array($value)
24 ? array_map('stripslashesDeep', $value)
25 : stripslashes($value);
26 }
27
28 if (get_magic_quotes_gpc()) {
29 $_POST = array_map('stripslashesDeep', $_POST);
30 $_GET = array_map('stripslashesDeep', $_GET);
31 $_COOKIE = array_map('stripslashesDeep', $_COOKIE);
32 }
33
34 ob_start();
35 register_shutdown_function('ob_end_flush');
36 }
37
3602405e
NL
38 /**
39 * Get wallabag instance URL
40 *
41 * @return string
42 */
eb1af592
NL
43 public static function getPocheUrl()
44 {
45 $https = (!empty($_SERVER['HTTPS'])
46 && (strtolower($_SERVER['HTTPS']) == 'on'))
47 || (isset($_SERVER["SERVER_PORT"])
125f9ee8 48 && $_SERVER["SERVER_PORT"] == '443') // HTTPS detection.
182faf26 49 || (isset($_SERVER["SERVER_PORT"]) //Custom HTTPS port detection
445a1a1c
NL
50 && $_SERVER["SERVER_PORT"] == SSL_PORT)
51 || (isset($_SERVER['HTTP_X_FORWARDED_PROTO'])
52 && $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https');
125f9ee8 53
eb1af592
NL
54 $serverport = (!isset($_SERVER["SERVER_PORT"])
55 || $_SERVER["SERVER_PORT"] == '80'
2d4cfc58 56 || $_SERVER["SERVER_PORT"] == HTTP_PORT
eb1af592 57 || ($https && $_SERVER["SERVER_PORT"] == '443')
2916d24b 58 || ($https && $_SERVER["SERVER_PORT"]==SSL_PORT) //Custom HTTPS port detection
eb1af592 59 ? '' : ':' . $_SERVER["SERVER_PORT"]);
5af2555f
AK
60
61 if (isset($_SERVER["HTTP_X_FORWARDED_PORT"])) {
62 $serverport = ':' . $_SERVER["HTTP_X_FORWARDED_PORT"];
63 }
eb1af592
NL
64
65 $scriptname = str_replace('/index.php', '/', $_SERVER["SCRIPT_NAME"]);
66
45e9e0f5 67 if (!isset($_SERVER["HTTP_HOST"])) {
eb1af592
NL
68 return $scriptname;
69 }
70
69c57493 71 $host = (isset($_SERVER['HTTP_X_FORWARDED_HOST']) ? $_SERVER['HTTP_X_FORWARDED_HOST'] : (isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : $_SERVER['SERVER_NAME']));
79024eb0
NL
72
73 if (strpos($host, ':') !== false) {
74 $serverport = '';
75 }
752cd4a8 76
eb1af592 77 return 'http' . ($https ? 's' : '') . '://'
69c57493 78 . $host . $serverport . $scriptname;
eb1af592
NL
79 }
80
3602405e
NL
81 /**
82 * Redirects to a URL
83 *
84 * @param string $url
85 */
eb1af592
NL
86 public static function redirect($url = '')
87 {
88 if ($url === '') {
89 $url = (empty($_SERVER['HTTP_REFERER'])?'?':$_SERVER['HTTP_REFERER']);
90 if (isset($_POST['returnurl'])) {
91 $url = $_POST['returnurl'];
92 }
93 }
94
95 # prevent loop
96 if (empty($url) || parse_url($url, PHP_URL_QUERY) === $_SERVER['QUERY_STRING']) {
97 $url = Tools::getPocheUrl();
98 }
99
100 if (substr($url, 0, 1) !== '?') {
101 $ref = Tools::getPocheUrl();
102 if (substr($url, 0, strlen($ref)) !== $ref) {
103 $url = $ref;
104 }
105 }
3602405e 106
bc1ee852 107 self::logm('redirect to ' . $url);
eb1af592
NL
108 header('Location: '.$url);
109 exit();
110 }
111
3602405e
NL
112 /**
113 * Returns name of the template file to display
114 *
115 * @param $view
116 * @return string
117 */
eb1af592
NL
118 public static function getTplFile($view)
119 {
74ec445a
NL
120 $views = array(
121 'install', 'import', 'export', 'config', 'tags',
3c133bff 122 'edit-tags', 'view', 'login', 'error', 'about'
74ec445a
NL
123 );
124
3602405e 125 return (in_array($view, $views) ? $view . '.twig' : 'home.twig');
eb1af592
NL
126 }
127
3602405e
NL
128 /**
129 * Download a file (typically, for downloading pictures on web server)
130 *
131 * @param $url
132 * @return bool|mixed|string
133 */
eb1af592
NL
134 public static function getFile($url)
135 {
136 $timeout = 15;
137 $useragent = "Mozilla/5.0 (Windows NT 5.1; rv:18.0) Gecko/20100101 Firefox/18.0";
138
139 if (in_array ('curl', get_loaded_extensions())) {
140 # Fetch feed from URL
141 $curl = curl_init();
142 curl_setopt($curl, CURLOPT_URL, $url);
143 curl_setopt($curl, CURLOPT_TIMEOUT, $timeout);
f2d3ee98
NL
144 if (!ini_get('open_basedir') && !ini_get('safe_mode')) {
145 curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
146 }
eb1af592
NL
147 curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
148 curl_setopt($curl, CURLOPT_HEADER, false);
149
150 # for ssl, do not verified certificate
151 curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
152 curl_setopt($curl, CURLOPT_AUTOREFERER, TRUE );
153
154 # FeedBurner requires a proper USER-AGENT...
155 curl_setopt($curl, CURL_HTTP_VERSION_1_1, true);
156 curl_setopt($curl, CURLOPT_ENCODING, "gzip, deflate");
157 curl_setopt($curl, CURLOPT_USERAGENT, $useragent);
158
159 $data = curl_exec($curl);
160 $httpcode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
161 $httpcodeOK = isset($httpcode) and ($httpcode == 200 or $httpcode == 301);
162 curl_close($curl);
163 } else {
164 # create http context and add timeout and user-agent
165 $context = stream_context_create(
166 array(
167 'http' => array(
168 'timeout' => $timeout,
169 'header' => "User-Agent: " . $useragent,
170 'follow_location' => true
171 ),
172 'ssl' => array(
173 'verify_peer' => false,
174 'allow_self_signed' => true
175 )
176 )
177 );
178
179 # only download page lesser than 4MB
182faf26 180 $data = @file_get_contents($url, false, $context, -1, 4000000);
eb1af592
NL
181
182 if (isset($http_response_header) and isset($http_response_header[0])) {
183 $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));
184 }
185 }
186
187 # if response is not empty and response is OK
188 if (isset($data) and isset($httpcodeOK) and $httpcodeOK) {
189
190 # take charset of page and get it
191 preg_match('#<meta .*charset=.*>#Usi', $data, $meta);
192
193 # if meta tag is found
194 if (!empty($meta[0])) {
195 preg_match('#charset="?(.*)"#si', $meta[0], $encoding);
196 # if charset is found set it otherwise, set it to utf-8
197 $html_charset = (!empty($encoding[1])) ? strtolower($encoding[1]) : 'utf-8';
5f9bff0f 198 if (empty($encoding[1])) $encoding[1] = 'utf-8';
eb1af592
NL
199 } else {
200 $html_charset = 'utf-8';
201 $encoding[1] = '';
202 }
203
204 # replace charset of url to charset of page
205 $data = str_replace('charset=' . $encoding[1], 'charset=' . $html_charset, $data);
206
207 return $data;
208 }
209 else {
210 return FALSE;
211 }
212 }
213
3602405e
NL
214 /**
215 * Headers for JSON export
216 *
217 * @param $data
218 */
eb1af592
NL
219 public static function renderJson($data)
220 {
221 header('Cache-Control: no-cache, must-revalidate');
222 header('Expires: Sat, 26 Jul 1997 05:00:00 GMT');
223 header('Content-type: application/json; charset=UTF-8');
224 echo json_encode($data);
225 exit();
226 }
227
3602405e
NL
228 /**
229 * Create new line in log file
230 *
231 * @param $message
232 */
eb1af592
NL
233 public static function logm($message)
234 {
53e3158d 235 if (DEBUG_POCHE && php_sapi_name() != 'cli') {
eb1af592 236 $t = strval(date('Y/m/d_H:i:s')) . ' - ' . $_SERVER["REMOTE_ADDR"] . ' - ' . strval($message) . "\n";
6a361945 237 file_put_contents(CACHE . '/log.txt', $t, FILE_APPEND);
bc1ee852 238 error_log('DEBUG POCHE : ' . $message);
eb1af592
NL
239 }
240 }
241
3602405e
NL
242 /**
243 * Encode a URL by using a salt
244 *
245 * @param $string
246 * @return string
247 */
182faf26 248 public static function encodeString($string)
eb1af592
NL
249 {
250 return sha1($string . SALT);
251 }
63c35580 252
3602405e
NL
253 /**
254 * Cleans a variable
255 *
256 * @param $var
257 * @param string $default
258 * @return string
259 */
7f959169 260 public static function checkVar($var, $default = '')
63c35580 261 {
3602405e 262 return ((isset($_REQUEST["$var"])) ? htmlentities($_REQUEST["$var"]) : $default);
63c35580 263 }
55821e04 264
3602405e
NL
265 /**
266 * Returns the domain name for a URL
267 *
268 * @param $url
269 * @return string
270 */
6400371f
NL
271 public static function getDomain($url)
272 {
273 return parse_url($url, PHP_URL_HOST);
274 }
275
3602405e
NL
276 /**
277 * For a given text, we calculate reading time for an article
278 *
279 * @param $text
280 * @return float
281 */
282 public static function getReadingTime($text)
283 {
284 return floor(str_word_count(strip_tags($text)) / 200);
1b2abab6 285 }
d460914f 286
3602405e
NL
287 /**
288 * Returns the correct header for a status code
289 *
290 * @param $status_code
291 */
292 private static function _status($status_code)
d460914f
NL
293 {
294 if (strpos(php_sapi_name(), 'apache') !== false) {
295
296 header('HTTP/1.0 '.$status_code);
297 }
298 else {
299
300 header('Status: '.$status_code);
301 }
302 }
303
3602405e
NL
304 /**
305 * Get the content for a given URL (by a call to FullTextFeed)
306 *
307 * @param Url $url
308 * @return mixed
309 */
53e3158d
NL
310 public static function getPageContent(Url $url)
311 {
312 // Saving and clearing context
313 $REAL = array();
314 foreach( $GLOBALS as $key => $value ) {
315 if( $key != 'GLOBALS' && $key != '_SESSION' && $key != 'HTTP_SESSION_VARS' ) {
3602405e
NL
316 $GLOBALS[$key] = array();
317 $REAL[$key] = $value;
53e3158d
NL
318 }
319 }
320 // Saving and clearing session
3602405e 321 if (isset($_SESSION)) {
f98373cc
MR
322 $REAL_SESSION = array();
323 foreach( $_SESSION as $key => $value ) {
324 $REAL_SESSION[$key] = $value;
325 unset($_SESSION[$key]);
326 }
53e3158d
NL
327 }
328
329 // Running code in different context
330 $scope = function() {
331 extract( func_get_arg(1) );
332 $_GET = $_REQUEST = array(
3602405e
NL
333 "url" => $url->getUrl(),
334 "max" => 5,
335 "links" => "preserve",
336 "exc" => "",
337 "format" => "json",
338 "submit" => "Create Feed"
53e3158d
NL
339 );
340 ob_start();
341 require func_get_arg(0);
f98373cc
MR
342 $json = ob_get_contents();
343 ob_end_clean();
53e3158d
NL
344 return $json;
345 };
3602405e 346
81315897
JBF
347 // Silence $scope function to avoid
348 // issues with FTRSS when error_reporting is to high
349 // FTRSS generates PHP warnings which break output
c78c1a3f 350 $json = @$scope("vendor/wallabag/Fivefilters_Libraries/makefulltextfeed.php", array("url" => $url));
53e3158d
NL
351
352 // Clearing and restoring context
3602405e
NL
353 foreach ($GLOBALS as $key => $value) {
354 if($key != "GLOBALS" && $key != "_SESSION" ) {
53e3158d
NL
355 unset($GLOBALS[$key]);
356 }
357 }
3602405e 358 foreach ($REAL as $key => $value) {
53e3158d
NL
359 $GLOBALS[$key] = $value;
360 }
3602405e 361
53e3158d 362 // Clearing and restoring session
3602405e
NL
363 if (isset($REAL_SESSION)) {
364 foreach($_SESSION as $key => $value) {
f98373cc
MR
365 unset($_SESSION[$key]);
366 }
3602405e
NL
367
368 foreach($REAL_SESSION as $key => $value) {
f98373cc
MR
369 $_SESSION[$key] = $value;
370 }
53e3158d 371 }
f98373cc 372
53e3158d
NL
373 return json_decode($json, true);
374 }
fb26cc93
MR
375
376 /**
377 * Returns whether we handle an AJAX (XMLHttpRequest) request.
3602405e 378 *
fb26cc93
MR
379 * @return boolean whether we handle an AJAX (XMLHttpRequest) request.
380 */
381 public static function isAjaxRequest()
382 {
3602405e 383 return isset($_SERVER['HTTP_X_REQUESTED_WITH']) && $_SERVER['HTTP_X_REQUESTED_WITH']==='XMLHttpRequest';
fb26cc93
MR
384 }
385
2f26729c
NL
386 /*
387 * Empty cache folder
388 */
389 public static function emptyCache()
390 {
391 $files = new RecursiveIteratorIterator(
392 new RecursiveDirectoryIterator(CACHE, RecursiveDirectoryIterator::SKIP_DOTS),
393 RecursiveIteratorIterator::CHILD_FIRST
394 );
395
396 foreach ($files as $fileInfo) {
397 $todo = ($fileInfo->isDir() ? 'rmdir' : 'unlink');
398 $todo($fileInfo->getRealPath());
399 }
400
401 Tools::logm('empty cache');
402 Tools::redirect();
403 }
404
405 public static function generateToken()
406 {
407 if (ini_get('open_basedir') === '') {
408 if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
409 // alternative to /dev/urandom for Windows
410 $token = substr(base64_encode(uniqid(mt_rand(), true)), 0, 20);
411 } else {
412 $token = substr(base64_encode(file_get_contents('/dev/urandom', false, null, 0, 20)), 0, 15);
413 }
414 }
415 else {
416 $token = substr(base64_encode(uniqid(mt_rand(), true)), 0, 20);
417 }
418
419 return str_replace('+', '', $token);
420 }
421
125f9ee8 422}