3 * wallabag, self hostable application allowing you to not miss any content anymore
6 * @author Nicolas LÅ“uillet <nicolas@loeuillet.org>
8 * @license http://opensource.org/licenses/MIT see COPYING file
11 namespace Wallabag\Wallabag
;
13 use \RecursiveIteratorIterator
;
14 use \RecursiveDirectoryIterator
;
19 * Initialize PHP environment
21 public static function initPhp()
23 define('START_TIME', microtime(true));
25 function stripslashesDeep($value) {
26 return is_array($value)
27 ? array_map('stripslashesDeep', $value)
28 : stripslashes($value);
31 if (get_magic_quotes_gpc()) {
32 $_POST = array_map('stripslashesDeep', $_POST);
33 $_GET = array_map('stripslashesDeep', $_GET);
34 $_COOKIE = array_map('stripslashesDeep', $_COOKIE);
38 register_shutdown_function('ob_end_flush');
42 * Get wallabag instance URL
46 public static function getPocheUrl()
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');
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"]);
64 if (isset($_SERVER["HTTP_X_FORWARDED_PORT"])) {
65 $serverport = ':' . $_SERVER["HTTP_X_FORWARDED_PORT"];
68 $scriptname = str_replace('/index.php', '/', $_SERVER["SCRIPT_NAME"]);
70 if (!isset($_SERVER["HTTP_HOST"])) {
74 $host = (isset($_SERVER['HTTP_X_FORWARDED_HOST']) ? $_SERVER['HTTP_X_FORWARDED_HOST'] : (isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : $_SERVER['SERVER_NAME']));
76 if (strpos($host, ':') !== false) {
80 return 'http' . ($https ? 's' : '') . '://'
81 . $host . $serverport . $scriptname;
89 public static function redirect($url = '')
92 $url = (empty($_SERVER['HTTP_REFERER'])?'?':$_SERVER['HTTP_REFERER']);
93 if (isset($_POST['returnurl'])) {
94 $url = $_POST['returnurl'];
99 if (empty($url) || parse_url($url, PHP_URL_QUERY
) === $_SERVER['QUERY_STRING']) {
100 $url = Tools
::getPocheUrl();
103 if (substr($url, 0, 1) !== '?') {
104 $ref = Tools
::getPocheUrl();
105 if (substr($url, 0, strlen($ref)) !== $ref) {
110 self
::logm('redirect to ' . $url);
111 header('Location: '.$url);
116 * Returns name of the template file to display
121 public static function getTplFile($view)
124 'install', 'import', 'export', 'config', 'tags',
125 'edit-tags', 'view', 'login', 'error', 'about'
128 return (in_array($view, $views) ? $view . '.twig' : 'home.twig');
132 * Download a file (typically, for downloading pictures on web server)
135 * @return bool|mixed|string
137 public static function getFile($url)
140 $useragent = "Mozilla/5.0 (Windows NT 5.1; rv:18.0) Gecko/20100101 Firefox/18.0";
142 if (in_array ('curl', get_loaded_extensions())) {
143 # Fetch feed from URL
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);
150 curl_setopt($curl, CURLOPT_RETURNTRANSFER
, true);
151 curl_setopt($curl, CURLOPT_HEADER
, false);
153 # for ssl, do not verified certificate
154 curl_setopt($curl, CURLOPT_SSL_VERIFYPEER
, FALSE);
155 curl_setopt($curl, CURLOPT_AUTOREFERER
, TRUE );
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);
162 $data = curl_exec($curl);
163 $httpcode = curl_getinfo($curl, CURLINFO_HTTP_CODE
);
164 $httpcodeOK = isset($httpcode) and ($httpcode == 200 or $httpcode == 301);
167 # create http context and add timeout and user-agent
168 $context = stream_context_create(
171 'timeout' => $timeout,
172 'header' => "User-Agent: " . $useragent,
173 'follow_location' => true
176 'verify_peer' => false,
177 'allow_self_signed' => true
182 # only download page lesser than 4MB
183 $data = @file_get_contents($url, false, $context, -1, 4000000);
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));
190 # if response is not empty and response is OK
191 if (isset($data) and isset($httpcodeOK) and $httpcodeOK) {
193 # take charset of page and get it
194 preg_match('#<meta .*charset=.*>#Usi', $data, $meta);
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';
203 $html_charset = 'utf-8';
207 # replace charset of url to charset of page
208 $data = str_replace('charset=' . $encoding[1], 'charset=' . $html_charset, $data);
218 * Headers for JSON export
222 public static function renderJson($data)
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);
232 * Create new line in log file
236 public static function logm($message)
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);
246 * Encode a URL by using a salt
251 public static function encodeString($string)
253 return sha1($string . SALT
);
260 * @param string $default
263 public static function checkVar($var, $default = '')
265 return ((isset($_REQUEST["$var"])) ? htmlentities($_REQUEST["$var"]) : $default);
269 * Returns the domain name for a URL
274 public static function getDomain($url)
276 return parse_url($url, PHP_URL_HOST
);
280 * For a given text, we calculate reading time for an article
285 public static function getReadingTime($text)
287 return floor(str_word_count(strip_tags($text)) / 200);
291 * Returns the correct header for a status code
293 * @param $status_code
295 private static function _status($status_code)
297 if (strpos(php_sapi_name(), 'apache') !== false) {
299 header('HTTP/1.0 '.$status_code);
303 header('Status: '.$status_code);
308 * Get the content for a given URL (by a call to FullTextFeed)
313 public static function getPageContent(Url
$url)
315 // Saving and clearing context
317 foreach( $GLOBALS as $key => $value ) {
318 if( $key != 'GLOBALS' && $key != '_SESSION' && $key != 'HTTP_SESSION_VARS' ) {
319 $GLOBALS[$key] = array();
320 $REAL[$key] = $value;
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]);
332 // Running code in different context
333 $scope = function() {
334 extract( func_get_arg(1) );
335 $_GET = $_REQUEST = array(
336 "url" => $url->getUrl(),
338 "links" => "preserve",
341 "submit" => "Create Feed"
344 require func_get_arg(0);
345 $json = ob_get_contents();
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));
355 // Clearing and restoring context
356 foreach ($GLOBALS as $key => $value) {
357 if($key != "GLOBALS" && $key != "_SESSION" ) {
358 unset($GLOBALS[$key]);
361 foreach ($REAL as $key => $value) {
362 $GLOBALS[$key] = $value;
365 // Clearing and restoring session
366 if (isset($REAL_SESSION)) {
367 foreach($_SESSION as $key => $value) {
368 unset($_SESSION[$key]);
371 foreach($REAL_SESSION as $key => $value) {
372 $_SESSION[$key] = $value;
376 return json_decode($json, true);
380 * Returns whether we handle an AJAX (XMLHttpRequest) request.
382 * @return boolean whether we handle an AJAX (XMLHttpRequest) request.
384 public static function isAjaxRequest()
386 return isset($_SERVER['HTTP_X_REQUESTED_WITH']) && $_SERVER['HTTP_X_REQUESTED_WITH']==='XMLHttpRequest';
392 public static function emptyCache()
394 $files = new RecursiveIteratorIterator(
395 new RecursiveDirectoryIterator(CACHE
, RecursiveDirectoryIterator
::SKIP_DOTS
),
396 RecursiveIteratorIterator
::CHILD_FIRST
399 foreach ($files as $fileInfo) {
400 $todo = ($fileInfo->isDir() ? 'rmdir' : 'unlink');
401 $todo($fileInfo->getRealPath());
404 Tools
::logm('empty cache');
408 public static function generateToken()
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);
415 $token = substr(base64_encode(file_get_contents('/dev/urandom', false, null, 0, 20)), 0, 15);
419 $token = substr(base64_encode(uniqid(mt_rand(), true)), 0, 20);
422 return str_replace('+', '', $token);