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