]>
git.immae.eu Git - github/wallabag/wallabag.git/blob - inc/poche/Tools.class.php
9d8e1fd655175e81b243523829988cc0018b619e
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 || (isset($_SERVER["SERVER_PORT"]) //Custom HTTPS port detection
46 && $_SERVER["SERVER_PORT"] == SSL_PORT
);
48 $serverport = (!isset($_SERVER["SERVER_PORT"])
49 || $_SERVER["SERVER_PORT"] == '80'
50 || ($https && $_SERVER["SERVER_PORT"] == '443')
51 || ($https && $_SERVER["SERVER_PORT"]==SSL_PORT
) //Custom HTTPS port detection
52 ? '' : ':' . $_SERVER["SERVER_PORT"]);
54 $scriptname = str_replace('/index.php', '/', $_SERVER["SCRIPT_NAME"]);
56 if (!isset($_SERVER["HTTP_HOST"])) {
60 return 'http' . ($https ? 's' : '') . '://'
61 . $_SERVER["HTTP_HOST"] . $serverport . $scriptname;
64 public static function redirect($url = '')
67 $url = (empty($_SERVER['HTTP_REFERER'])?'?':$_SERVER['HTTP_REFERER']);
68 if (isset($_POST['returnurl'])) {
69 $url = $_POST['returnurl'];
74 if (empty($url) || parse_url($url, PHP_URL_QUERY
) === $_SERVER['QUERY_STRING']) {
75 $url = Tools
::getPocheUrl();
78 if (substr($url, 0, 1) !== '?') {
79 $ref = Tools
::getPocheUrl();
80 if (substr($url, 0, strlen($ref)) !== $ref) {
84 self
::logm('redirect to ' . $url);
85 header('Location: '.$url);
89 public static function getTplFile($view)
91 $default_tpl = 'home.twig';
95 $tpl_file = 'install.twig';
98 $tpl_file = 'import.twig';
101 $tpl_file = 'export.twig';
104 $tpl_file = 'config.twig';
107 $tpl_file = 'view.twig';
111 $tpl_file = 'login.twig';
115 $tpl_file = 'error.twig';
119 $tpl_file = $default_tpl;
126 public static function getFile($url)
129 $useragent = "Mozilla/5.0 (Windows NT 5.1; rv:18.0) Gecko/20100101 Firefox/18.0";
131 if (in_array ('curl', get_loaded_extensions())) {
132 # Fetch feed from URL
134 curl_setopt($curl, CURLOPT_URL
, $url);
135 curl_setopt($curl, CURLOPT_TIMEOUT
, $timeout);
136 if (!ini_get('open_basedir') && !ini_get('safe_mode')) {
137 curl_setopt($curl, CURLOPT_FOLLOWLOCATION
, true);
139 curl_setopt($curl, CURLOPT_RETURNTRANSFER
, true);
140 curl_setopt($curl, CURLOPT_HEADER
, false);
142 # for ssl, do not verified certificate
143 curl_setopt($curl, CURLOPT_SSL_VERIFYPEER
, FALSE);
144 curl_setopt($curl, CURLOPT_AUTOREFERER
, TRUE );
146 # FeedBurner requires a proper USER-AGENT...
147 curl_setopt($curl, CURL_HTTP_VERSION_1_1
, true);
148 curl_setopt($curl, CURLOPT_ENCODING
, "gzip, deflate");
149 curl_setopt($curl, CURLOPT_USERAGENT
, $useragent);
151 $data = curl_exec($curl);
152 $httpcode = curl_getinfo($curl, CURLINFO_HTTP_CODE
);
153 $httpcodeOK = isset($httpcode) and ($httpcode == 200 or $httpcode == 301);
156 # create http context and add timeout and user-agent
157 $context = stream_context_create(
160 'timeout' => $timeout,
161 'header' => "User-Agent: " . $useragent,
162 'follow_location' => true
165 'verify_peer' => false,
166 'allow_self_signed' => true
171 # only download page lesser than 4MB
172 $data = @file_get_contents($url, false, $context, -1, 4000000);
174 if (isset($http_response_header) and isset($http_response_header[0])) {
175 $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));
179 # if response is not empty and response is OK
180 if (isset($data) and isset($httpcodeOK) and $httpcodeOK) {
182 # take charset of page and get it
183 preg_match('#<meta .*charset=.*>#Usi', $data, $meta);
185 # if meta tag is found
186 if (!empty($meta[0])) {
187 preg_match('#charset="?(.*)"#si', $meta[0], $encoding);
188 # if charset is found set it otherwise, set it to utf-8
189 $html_charset = (!empty($encoding[1])) ? strtolower($encoding[1]) : 'utf-8';
190 if (empty($encoding[1])) $encoding[1] = 'utf-8';
192 $html_charset = 'utf-8';
196 # replace charset of url to charset of page
197 $data = str_replace('charset=' . $encoding[1], 'charset=' . $html_charset, $data);
206 public static function renderJson($data)
208 header('Cache-Control: no-cache, must-revalidate');
209 header('Expires: Sat, 26 Jul 1997 05:00:00 GMT');
210 header('Content-type: application/json; charset=UTF-8');
211 echo json_encode($data);
215 public static function logm($message)
218 $t = strval(date('Y/m/d_H:i:s')) . ' - ' . $_SERVER["REMOTE_ADDR"] . ' - ' . strval($message) . "\n";
219 file_put_contents(CACHE
. '/log.txt', $t, FILE_APPEND
);
220 error_log('DEBUG POCHE : ' . $message);
224 public static function encodeString($string)
226 return sha1($string . SALT
);
229 public static function checkVar($var, $default = '')
231 return ((isset ($_REQUEST["$var"])) ? htmlentities($_REQUEST["$var"]) : $default);
234 public static function getDomain($url)
236 return parse_url($url, PHP_URL_HOST
);
239 public static function getReadingTime($text) {
240 $word = str_word_count(strip_tags($text));
241 $minutes = floor($word / 200);
242 $seconds = floor($word %
200 / (200 / 60));
243 $time = array('minutes' => $minutes, 'seconds' => $seconds);
248 public static function getDocLanguage($userlanguage) {
249 $lang = explode('.', $userlanguage);
250 return str_replace('_', '-', $lang[0]);
253 public static function status($status_code)
255 if (strpos(php_sapi_name(), 'apache') !== false) {
257 header('HTTP/1.0 '.$status_code);
261 header('Status: '.$status_code);
266 public static function download_db() {
267 header('Content-Disposition: attachment; filename="poche.sqlite.gz"');
270 header('Content-Transfer-Encoding: binary');
271 header('Content-Type: application/octet-stream');
272 echo gzencode(file_get_contents(STORAGE_SQLITE
));