]> git.immae.eu Git - github/wallabag/wallabag.git/blame - inc/poche/Tools.class.php
Adding .htaccess files to prohibit access to critical directories, e.g., db/
[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 */
10
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
21 error_reporting(E_ALL);
22
23 function stripslashesDeep($value) {
24 return is_array($value)
25 ? array_map('stripslashesDeep', $value)
26 : stripslashes($value);
27 }
28
29 if (get_magic_quotes_gpc()) {
30 $_POST = array_map('stripslashesDeep', $_POST);
31 $_GET = array_map('stripslashesDeep', $_GET);
32 $_COOKIE = array_map('stripslashesDeep', $_COOKIE);
33 }
34
35 ob_start();
36 register_shutdown_function('ob_end_flush');
37 }
38
39 public static function getPocheUrl()
40 {
41 $https = (!empty($_SERVER['HTTPS'])
42 && (strtolower($_SERVER['HTTPS']) == 'on'))
43 || (isset($_SERVER["SERVER_PORT"])
125f9ee8 44 && $_SERVER["SERVER_PORT"] == '443') // HTTPS detection.
2916d24b 45 || (isset($_SERVER["SERVER_PORT"]) //Custom HTTPS port detection
445a1a1c
NL
46 && $_SERVER["SERVER_PORT"] == SSL_PORT)
47 || (isset($_SERVER['HTTP_X_FORWARDED_PROTO'])
48 && $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https');
125f9ee8 49
eb1af592
NL
50 $serverport = (!isset($_SERVER["SERVER_PORT"])
51 || $_SERVER["SERVER_PORT"] == '80'
52 || ($https && $_SERVER["SERVER_PORT"] == '443')
2916d24b 53 || ($https && $_SERVER["SERVER_PORT"]==SSL_PORT) //Custom HTTPS port detection
eb1af592
NL
54 ? '' : ':' . $_SERVER["SERVER_PORT"]);
55
56 $scriptname = str_replace('/index.php', '/', $_SERVER["SCRIPT_NAME"]);
57
45e9e0f5 58 if (!isset($_SERVER["HTTP_HOST"])) {
eb1af592
NL
59 return $scriptname;
60 }
61
62 return 'http' . ($https ? 's' : '') . '://'
45e9e0f5 63 . $_SERVER["HTTP_HOST"] . $serverport . $scriptname;
eb1af592
NL
64 }
65
66 public static function redirect($url = '')
67 {
68 if ($url === '') {
69 $url = (empty($_SERVER['HTTP_REFERER'])?'?':$_SERVER['HTTP_REFERER']);
70 if (isset($_POST['returnurl'])) {
71 $url = $_POST['returnurl'];
72 }
73 }
74
75 # prevent loop
76 if (empty($url) || parse_url($url, PHP_URL_QUERY) === $_SERVER['QUERY_STRING']) {
77 $url = Tools::getPocheUrl();
78 }
79
80 if (substr($url, 0, 1) !== '?') {
81 $ref = Tools::getPocheUrl();
82 if (substr($url, 0, strlen($ref)) !== $ref) {
83 $url = $ref;
84 }
85 }
bc1ee852 86 self::logm('redirect to ' . $url);
eb1af592
NL
87 header('Location: '.$url);
88 exit();
89 }
90
91 public static function getTplFile($view)
92 {
74ec445a
NL
93 $views = array(
94 'install', 'import', 'export', 'config', 'tags',
4886ed6d 95 'edit-tags', 'view', 'login', 'error', 'tag'
74ec445a
NL
96 );
97
98 if (in_array($view, $views)) {
99 return $view . '.twig';
eb1af592 100 }
74ec445a
NL
101
102 return 'home.twig';
eb1af592
NL
103 }
104
105 public static function getFile($url)
106 {
107 $timeout = 15;
108 $useragent = "Mozilla/5.0 (Windows NT 5.1; rv:18.0) Gecko/20100101 Firefox/18.0";
109
110 if (in_array ('curl', get_loaded_extensions())) {
111 # Fetch feed from URL
112 $curl = curl_init();
113 curl_setopt($curl, CURLOPT_URL, $url);
114 curl_setopt($curl, CURLOPT_TIMEOUT, $timeout);
f2d3ee98
NL
115 if (!ini_get('open_basedir') && !ini_get('safe_mode')) {
116 curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
117 }
eb1af592
NL
118 curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
119 curl_setopt($curl, CURLOPT_HEADER, false);
120
121 # for ssl, do not verified certificate
122 curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
123 curl_setopt($curl, CURLOPT_AUTOREFERER, TRUE );
124
125 # FeedBurner requires a proper USER-AGENT...
126 curl_setopt($curl, CURL_HTTP_VERSION_1_1, true);
127 curl_setopt($curl, CURLOPT_ENCODING, "gzip, deflate");
128 curl_setopt($curl, CURLOPT_USERAGENT, $useragent);
129
130 $data = curl_exec($curl);
131 $httpcode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
132 $httpcodeOK = isset($httpcode) and ($httpcode == 200 or $httpcode == 301);
133 curl_close($curl);
134 } else {
135 # create http context and add timeout and user-agent
136 $context = stream_context_create(
137 array(
138 'http' => array(
139 'timeout' => $timeout,
140 'header' => "User-Agent: " . $useragent,
141 'follow_location' => true
142 ),
143 'ssl' => array(
144 'verify_peer' => false,
145 'allow_self_signed' => true
146 )
147 )
148 );
149
150 # only download page lesser than 4MB
151 $data = @file_get_contents($url, false, $context, -1, 4000000);
152
153 if (isset($http_response_header) and isset($http_response_header[0])) {
154 $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));
155 }
156 }
157
158 # if response is not empty and response is OK
159 if (isset($data) and isset($httpcodeOK) and $httpcodeOK) {
160
161 # take charset of page and get it
162 preg_match('#<meta .*charset=.*>#Usi', $data, $meta);
163
164 # if meta tag is found
165 if (!empty($meta[0])) {
166 preg_match('#charset="?(.*)"#si', $meta[0], $encoding);
167 # if charset is found set it otherwise, set it to utf-8
168 $html_charset = (!empty($encoding[1])) ? strtolower($encoding[1]) : 'utf-8';
5f9bff0f 169 if (empty($encoding[1])) $encoding[1] = 'utf-8';
eb1af592
NL
170 } else {
171 $html_charset = 'utf-8';
172 $encoding[1] = '';
173 }
174
175 # replace charset of url to charset of page
176 $data = str_replace('charset=' . $encoding[1], 'charset=' . $html_charset, $data);
177
178 return $data;
179 }
180 else {
181 return FALSE;
182 }
183 }
184
185 public static function renderJson($data)
186 {
187 header('Cache-Control: no-cache, must-revalidate');
188 header('Expires: Sat, 26 Jul 1997 05:00:00 GMT');
189 header('Content-type: application/json; charset=UTF-8');
190 echo json_encode($data);
191 exit();
192 }
193
194 public static function logm($message)
195 {
196 if (DEBUG_POCHE) {
197 $t = strval(date('Y/m/d_H:i:s')) . ' - ' . $_SERVER["REMOTE_ADDR"] . ' - ' . strval($message) . "\n";
6a361945 198 file_put_contents(CACHE . '/log.txt', $t, FILE_APPEND);
bc1ee852 199 error_log('DEBUG POCHE : ' . $message);
eb1af592
NL
200 }
201 }
202
203 public static function encodeString($string)
204 {
205 return sha1($string . SALT);
206 }
63c35580 207
7f959169 208 public static function checkVar($var, $default = '')
63c35580 209 {
7f959169 210 return ((isset ($_REQUEST["$var"])) ? htmlentities($_REQUEST["$var"]) : $default);
63c35580 211 }
55821e04
NL
212
213 public static function getDomain($url)
214 {
d7c2f0cc 215 return parse_url($url, PHP_URL_HOST);
55821e04 216 }
d9178758
NL
217
218 public static function getReadingTime($text) {
219 $word = str_word_count(strip_tags($text));
220 $minutes = floor($word / 200);
221 $seconds = floor($word % 200 / (200 / 60));
222 $time = array('minutes' => $minutes, 'seconds' => $seconds);
223
224 return $minutes;
225 }
bb5a7d9e 226
1b2abab6
N
227 public static function getDocLanguage($userlanguage) {
228 $lang = explode('.', $userlanguage);
229 return str_replace('_', '-', $lang[0]);
230 }
d460914f
NL
231
232 public static function status($status_code)
233 {
234 if (strpos(php_sapi_name(), 'apache') !== false) {
235
236 header('HTTP/1.0 '.$status_code);
237 }
238 else {
239
240 header('Status: '.$status_code);
241 }
242 }
243
244
245 public static function download_db() {
246 header('Content-Disposition: attachment; filename="poche.sqlite.gz"');
247 self::status(200);
248
249 header('Content-Transfer-Encoding: binary');
250 header('Content-Type: application/octet-stream');
251 echo gzencode(file_get_contents(STORAGE_SQLITE));
252
253 exit;
254 }
125f9ee8 255}