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