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