]>
git.immae.eu Git - github/wallabag/wallabag.git/blob - inc/poche/Tools.class.php
8b339ea57989f639df7c9bc17f1ee8ac40da92b3
3 * poche, a read it later open source system
6 * @author Nicolas LÅ“uillet <support@inthepoche.com>
8 * @license http://www.wtfpl.net/ see COPYING file
13 public static function initPhp()
15 define('START_TIME', microtime(true));
17 if (phpversion() < 5) {
18 die(_('Oops, it seems you don\'t have PHP 5.'));
21 error_reporting(E_ALL
);
23 function stripslashesDeep($value) {
24 return is_array($value)
25 ? array_map('stripslashesDeep', $value)
26 : stripslashes($value);
29 if (get_magic_quotes_gpc()) {
30 $_POST = array_map('stripslashesDeep', $_POST);
31 $_GET = array_map('stripslashesDeep', $_GET);
32 $_COOKIE = array_map('stripslashesDeep', $_COOKIE);
36 register_shutdown_function('ob_end_flush');
39 public static function getPocheUrl()
41 $https = (!empty($_SERVER['HTTPS'])
42 && (strtolower($_SERVER['HTTPS']) == 'on'))
43 || (isset($_SERVER["SERVER_PORT"])
44 && $_SERVER["SERVER_PORT"] == '443'); // HTTPS detection.
45 $serverport = (!isset($_SERVER["SERVER_PORT"])
46 || $_SERVER["SERVER_PORT"] == '80'
47 || ($https && $_SERVER["SERVER_PORT"] == '443')
48 ? '' : ':' . $_SERVER["SERVER_PORT"]);
50 $scriptname = str_replace('/index.php', '/', $_SERVER["SCRIPT_NAME"]);
52 if (!isset($_SERVER["SERVER_NAME"])) {
56 return 'http' . ($https ? 's' : '') . '://'
57 . $_SERVER["SERVER_NAME"] . $serverport . $scriptname;
60 public static function redirect($url = '')
63 $url = (empty($_SERVER['HTTP_REFERER'])?'?':$_SERVER['HTTP_REFERER']);
64 if (isset($_POST['returnurl'])) {
65 $url = $_POST['returnurl'];
70 if (empty($url) || parse_url($url, PHP_URL_QUERY
) === $_SERVER['QUERY_STRING']) {
71 $url = Tools
::getPocheUrl();
74 if (substr($url, 0, 1) !== '?') {
75 $ref = Tools
::getPocheUrl();
76 if (substr($url, 0, strlen($ref)) !== $ref) {
80 header('Location: '.$url);
84 public static function getTplFile($view)
86 $tpl_file = 'home.twig';
90 $tpl_file = 'install.twig';
93 $tpl_file = 'import.twig';
96 $tpl_file = 'export.twig';
99 $tpl_file = 'config.twig';
102 $tpl_file = 'view.twig';
110 public static function getFile($url)
113 $useragent = "Mozilla/5.0 (Windows NT 5.1; rv:18.0) Gecko/20100101 Firefox/18.0";
115 if (in_array ('curl', get_loaded_extensions())) {
116 # Fetch feed from URL
118 curl_setopt($curl, CURLOPT_URL
, $url);
119 curl_setopt($curl, CURLOPT_TIMEOUT
, $timeout);
120 curl_setopt($curl, CURLOPT_FOLLOWLOCATION
, true);
121 curl_setopt($curl, CURLOPT_RETURNTRANSFER
, true);
122 curl_setopt($curl, CURLOPT_HEADER
, false);
124 # for ssl, do not verified certificate
125 curl_setopt($curl, CURLOPT_SSL_VERIFYPEER
, FALSE);
126 curl_setopt($curl, CURLOPT_AUTOREFERER
, TRUE );
128 # FeedBurner requires a proper USER-AGENT...
129 curl_setopt($curl, CURL_HTTP_VERSION_1_1
, true);
130 curl_setopt($curl, CURLOPT_ENCODING
, "gzip, deflate");
131 curl_setopt($curl, CURLOPT_USERAGENT
, $useragent);
133 $data = curl_exec($curl);
134 $httpcode = curl_getinfo($curl, CURLINFO_HTTP_CODE
);
135 $httpcodeOK = isset($httpcode) and ($httpcode == 200 or $httpcode == 301);
138 # create http context and add timeout and user-agent
139 $context = stream_context_create(
142 'timeout' => $timeout,
143 'header' => "User-Agent: " . $useragent,
144 'follow_location' => true
147 'verify_peer' => false,
148 'allow_self_signed' => true
153 # only download page lesser than 4MB
154 $data = @file_get_contents($url, false, $context, -1, 4000000);
156 if (isset($http_response_header) and isset($http_response_header[0])) {
157 $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));
161 # if response is not empty and response is OK
162 if (isset($data) and isset($httpcodeOK) and $httpcodeOK) {
164 # take charset of page and get it
165 preg_match('#<meta .*charset=.*>#Usi', $data, $meta);
167 # if meta tag is found
168 if (!empty($meta[0])) {
169 preg_match('#charset="?(.*)"#si', $meta[0], $encoding);
170 # if charset is found set it otherwise, set it to utf-8
171 $html_charset = (!empty($encoding[1])) ? strtolower($encoding[1]) : 'utf-8';
173 $html_charset = 'utf-8';
177 # replace charset of url to charset of page
178 $data = str_replace('charset=' . $encoding[1], 'charset=' . $html_charset, $data);
187 public static function renderJson($data)
189 header('Cache-Control: no-cache, must-revalidate');
190 header('Expires: Sat, 26 Jul 1997 05:00:00 GMT');
191 header('Content-type: application/json; charset=UTF-8');
192 echo json_encode($data);
196 public static function logm($message)
199 $t = strval(date('Y/m/d_H:i:s')) . ' - ' . $_SERVER["REMOTE_ADDR"] . ' - ' . strval($message) . "\n";
200 file_put_contents(CACHE
. '/log.txt', $t, FILE_APPEND
);
204 public static function encodeString($string)
206 return sha1($string . SALT
);
209 public static function checkVar($var, $default = '')
211 return ((isset ($_REQUEST["$var"])) ? htmlentities($_REQUEST["$var"]) : $default);
214 public static function getDomain($url)
216 $pieces = parse_url($url);
217 $domain = isset($pieces['host']) ? $pieces['host'] : '';
218 if (preg_match('/(?P<domain>[a-z0-9][a-z0-9\-]{1,63}\.[a-z\.]{2,6})$/i', $domain, $regs)) {
219 return $regs['domain'];