]>
git.immae.eu Git - github/wallabag/wallabag.git/blob - inc/poche/Tools.class.php
3 * wallabag, self hostable application allowing you to not miss any content anymore
6 * @author Nicolas LÅ“uillet <nicolas@loeuillet.org>
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 function stripslashesDeep($value) {
22 return is_array($value)
23 ? array_map('stripslashesDeep', $value)
24 : stripslashes($value);
27 if (get_magic_quotes_gpc()) {
28 $_POST = array_map('stripslashesDeep', $_POST);
29 $_GET = array_map('stripslashesDeep', $_GET);
30 $_COOKIE = array_map('stripslashesDeep', $_COOKIE);
34 register_shutdown_function('ob_end_flush');
37 public static function getPocheUrl()
39 $https = (!empty($_SERVER['HTTPS'])
40 && (strtolower($_SERVER['HTTPS']) == 'on'))
41 || (isset($_SERVER["SERVER_PORT"])
42 && $_SERVER["SERVER_PORT"] == '443') // HTTPS detection.
43 || (isset($_SERVER["SERVER_PORT"]) //Custom HTTPS port detection
44 && $_SERVER["SERVER_PORT"] == SSL_PORT
)
45 || (isset($_SERVER['HTTP_X_FORWARDED_PROTO'])
46 && $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https');
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 $host = (isset($_SERVER['HTTP_X_FORWARDED_HOST']) ? $_SERVER['HTTP_X_FORWARDED_HOST'] : (isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : $_SERVER['SERVER_NAME']));
62 if (strpos($host, ':') !== false) {
66 return 'http' . ($https ? 's' : '') . '://'
67 . $host . $serverport . $scriptname;
70 public static function redirect($url = '')
73 $url = (empty($_SERVER['HTTP_REFERER'])?'?':$_SERVER['HTTP_REFERER']);
74 if (isset($_POST['returnurl'])) {
75 $url = $_POST['returnurl'];
80 if (empty($url) || parse_url($url, PHP_URL_QUERY
) === $_SERVER['QUERY_STRING']) {
81 $url = Tools
::getPocheUrl();
84 if (substr($url, 0, 1) !== '?') {
85 $ref = Tools
::getPocheUrl();
86 if (substr($url, 0, strlen($ref)) !== $ref) {
90 self
::logm('redirect to ' . $url);
91 header('Location: '.$url);
95 public static function getTplFile($view)
98 'install', 'import', 'export', 'config', 'tags',
99 'edit-tags', 'view', 'login', 'error'
102 if (in_array($view, $views)) {
103 return $view . '.twig';
109 public static function getFile($url)
112 $useragent = "Mozilla/5.0 (Windows NT 5.1; rv:18.0) Gecko/20100101 Firefox/18.0";
114 if (in_array ('curl', get_loaded_extensions())) {
115 # Fetch feed from URL
117 curl_setopt($curl, CURLOPT_URL
, $url);
118 curl_setopt($curl, CURLOPT_TIMEOUT
, $timeout);
119 if (!ini_get('open_basedir') && !ini_get('safe_mode')) {
120 curl_setopt($curl, CURLOPT_FOLLOWLOCATION
, true);
122 curl_setopt($curl, CURLOPT_RETURNTRANSFER
, true);
123 curl_setopt($curl, CURLOPT_HEADER
, false);
125 # for ssl, do not verified certificate
126 curl_setopt($curl, CURLOPT_SSL_VERIFYPEER
, FALSE);
127 curl_setopt($curl, CURLOPT_AUTOREFERER
, TRUE );
129 # FeedBurner requires a proper USER-AGENT...
130 curl_setopt($curl, CURL_HTTP_VERSION_1_1
, true);
131 curl_setopt($curl, CURLOPT_ENCODING
, "gzip, deflate");
132 curl_setopt($curl, CURLOPT_USERAGENT
, $useragent);
134 $data = curl_exec($curl);
135 $httpcode = curl_getinfo($curl, CURLINFO_HTTP_CODE
);
136 $httpcodeOK = isset($httpcode) and ($httpcode == 200 or $httpcode == 301);
139 # create http context and add timeout and user-agent
140 $context = stream_context_create(
143 'timeout' => $timeout,
144 'header' => "User-Agent: " . $useragent,
145 'follow_location' => true
148 'verify_peer' => false,
149 'allow_self_signed' => true
154 # only download page lesser than 4MB
155 $data = @file_get_contents($url, false, $context, -1, 4000000);
157 if (isset($http_response_header) and isset($http_response_header[0])) {
158 $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));
162 # if response is not empty and response is OK
163 if (isset($data) and isset($httpcodeOK) and $httpcodeOK) {
165 # take charset of page and get it
166 preg_match('#<meta .*charset=.*>#Usi', $data, $meta);
168 # if meta tag is found
169 if (!empty($meta[0])) {
170 preg_match('#charset="?(.*)"#si', $meta[0], $encoding);
171 # if charset is found set it otherwise, set it to utf-8
172 $html_charset = (!empty($encoding[1])) ? strtolower($encoding[1]) : 'utf-8';
173 if (empty($encoding[1])) $encoding[1] = 'utf-8';
175 $html_charset = 'utf-8';
179 # replace charset of url to charset of page
180 $data = str_replace('charset=' . $encoding[1], 'charset=' . $html_charset, $data);
189 public static function renderJson($data)
191 header('Cache-Control: no-cache, must-revalidate');
192 header('Expires: Sat, 26 Jul 1997 05:00:00 GMT');
193 header('Content-type: application/json; charset=UTF-8');
194 echo json_encode($data);
198 public static function logm($message)
200 if (DEBUG_POCHE
&& php_sapi_name() != 'cli') {
201 $t = strval(date('Y/m/d_H:i:s')) . ' - ' . $_SERVER["REMOTE_ADDR"] . ' - ' . strval($message) . "\n";
202 file_put_contents(CACHE
. '/log.txt', $t, FILE_APPEND
);
203 error_log('DEBUG POCHE : ' . $message);
207 public static function encodeString($string)
209 return sha1($string . SALT
);
212 public static function checkVar($var, $default = '')
214 return ((isset ($_REQUEST["$var"])) ? htmlentities($_REQUEST["$var"]) : $default);
217 public static function getDomain($url)
219 return parse_url($url, PHP_URL_HOST
);
222 public static function getReadingTime($text) {
223 $word = str_word_count(strip_tags($text));
224 $minutes = floor($word / 200);
225 $seconds = floor($word %
200 / (200 / 60));
226 $time = array('minutes' => $minutes, 'seconds' => $seconds);
231 public static function getDocLanguage($userlanguage) {
232 $lang = explode('.', $userlanguage);
233 return str_replace('_', '-', $lang[0]);
236 public static function status($status_code)
238 if (strpos(php_sapi_name(), 'apache') !== false) {
240 header('HTTP/1.0 '.$status_code);
244 header('Status: '.$status_code);
248 public static function download_db() {
249 header('Content-Disposition: attachment; filename="poche.sqlite.gz"');
252 header('Content-Transfer-Encoding: binary');
253 header('Content-Type: application/octet-stream');
254 echo gzencode(file_get_contents(STORAGE_SQLITE
));
259 public static function getPageContent(Url
$url)
261 // Saving and clearing context
263 foreach( $GLOBALS as $key => $value ) {
264 if( $key != 'GLOBALS' && $key != '_SESSION' && $key != 'HTTP_SESSION_VARS' ) {
265 $GLOBALS[$key] = array();
266 $REAL[$key] = $value;
269 // Saving and clearing session
270 if ( isset($_SESSION) ) {
271 $REAL_SESSION = array();
272 foreach( $_SESSION as $key => $value ) {
273 $REAL_SESSION[$key] = $value;
274 unset($_SESSION[$key]);
278 // Running code in different context
279 $scope = function() {
280 extract( func_get_arg(1) );
281 $_GET = $_REQUEST = array(
282 "url" => $url->getUrl(),
284 "links" => "preserve",
287 "submit" => "Create Feed"
290 require func_get_arg(0);
291 $json = ob_get_contents();
295 $json = $scope( "inc/3rdparty/makefulltextfeed.php", array("url" => $url) );
297 // Clearing and restoring context
298 foreach( $GLOBALS as $key => $value ) {
299 if( $key != "GLOBALS" && $key != "_SESSION" ) {
300 unset($GLOBALS[$key]);
303 foreach( $REAL as $key => $value ) {
304 $GLOBALS[$key] = $value;
306 // Clearing and restoring session
307 if ( isset($REAL_SESSION) ) {
308 foreach( $_SESSION as $key => $value ) {
309 unset($_SESSION[$key]);
311 foreach( $REAL_SESSION as $key => $value ) {
312 $_SESSION[$key] = $value;
316 return json_decode($json, true);
320 * Returns whether we handle an AJAX (XMLHttpRequest) request.
321 * @return boolean whether we handle an AJAX (XMLHttpRequest) request.
323 public static function isAjaxRequest()
325 return isset($_SERVER['HTTP_X_REQUESTED_WITH']) && $_SERVER['HTTP_X_REQUESTED_WITH']==='XMLHttpRequest';