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
14 * Initialize PHP environment
16 public static function initPhp()
18 define('START_TIME', microtime(true));
20 function stripslashesDeep($value) {
21 return is_array($value)
22 ? array_map('stripslashesDeep', $value)
23 : stripslashes($value);
26 if (get_magic_quotes_gpc()) {
27 $_POST = array_map('stripslashesDeep', $_POST);
28 $_GET = array_map('stripslashesDeep', $_GET);
29 $_COOKIE = array_map('stripslashesDeep', $_COOKIE);
33 register_shutdown_function('ob_end_flush');
37 * Get wallabag instance URL
41 public static function getPocheUrl()
45 $https = (!empty($_SERVER['HTTPS'])
46 && (strtolower($_SERVER['HTTPS']) == 'on'))
47 || (isset($_SERVER["SERVER_PORT"])
48 && $_SERVER["SERVER_PORT"] == '443') // HTTPS detection.
49 || (isset($_SERVER["SERVER_PORT"]) //Custom HTTPS port detection
50 && $_SERVER["SERVER_PORT"] == SSL_PORT
)
51 || (isset($_SERVER['HTTP_X_FORWARDED_PROTO'])
52 && $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https');
54 $serverport = (!isset($_SERVER["SERVER_PORT"])
55 || $_SERVER["SERVER_PORT"] == '80'
56 || $_SERVER["SERVER_PORT"] == HTTP_PORT
57 || ($https && $_SERVER["SERVER_PORT"] == '443')
58 || ($https && $_SERVER["SERVER_PORT"]==SSL_PORT
) //Custom HTTPS port detection
59 ? '' : ':' . $_SERVER["SERVER_PORT"]);
61 if (isset($_SERVER["HTTP_X_FORWARDED_PORT"])) {
62 $serverport = ':' . $_SERVER["HTTP_X_FORWARDED_PORT"];
65 $scriptname = str_replace('/index.php', '/', $_SERVER["SCRIPT_NAME"]);
67 if (!isset($_SERVER["HTTP_HOST"])) {
71 $host = (isset($_SERVER['HTTP_X_FORWARDED_HOST']) ? $_SERVER['HTTP_X_FORWARDED_HOST'] : (isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : $_SERVER['SERVER_NAME']));
73 if (strpos($host, ':') !== false) {
77 // check if BASE_URL is configured
81 $baseUrl = 'http' . ($https ? 's' : '') . '://' . $host . $serverport;
84 return $baseUrl . $scriptname;
93 public static function redirect($url = '')
96 $url = (empty($_SERVER['HTTP_REFERER'])?'?':$_SERVER['HTTP_REFERER']);
97 if (isset($_POST['returnurl'])) {
98 $url = $_POST['returnurl'];
103 if (empty($url) || parse_url($url, PHP_URL_QUERY
) === $_SERVER['QUERY_STRING']) {
104 $url = Tools
::getPocheUrl();
107 if (substr($url, 0, 1) !== '?') {
108 $ref = Tools
::getPocheUrl();
109 if (substr($url, 0, strlen($ref)) !== $ref) {
114 self
::logm('redirect to ' . $url);
115 header('Location: '.$url);
120 * Returns name of the template file to display
125 public static function getTplFile($view)
128 'install', 'import', 'export', 'config', 'tags',
129 'edit-tags', 'view', 'login', 'error', 'about', 'register'
132 return (in_array($view, $views) ? $view . '.twig' : 'home.twig');
136 * Download a file (typically, for downloading pictures on web server)
139 * @return bool|mixed|string
141 public static function getFile($url)
144 $useragent = "Mozilla/5.0 (Windows NT 5.1; rv:18.0) Gecko/20100101 Firefox/18.0";
146 if (in_array ('curl', get_loaded_extensions())) {
147 # Fetch feed from URL
149 curl_setopt($curl, CURLOPT_URL
, $url);
150 curl_setopt($curl, CURLOPT_TIMEOUT
, $timeout);
151 if (!ini_get('open_basedir') && !ini_get('safe_mode')) {
152 curl_setopt($curl, CURLOPT_FOLLOWLOCATION
, true);
154 curl_setopt($curl, CURLOPT_RETURNTRANSFER
, true);
155 curl_setopt($curl, CURLOPT_HEADER
, false);
157 # for ssl, do not verified certificate
158 curl_setopt($curl, CURLOPT_SSL_VERIFYPEER
, FALSE);
159 curl_setopt($curl, CURLOPT_AUTOREFERER
, TRUE );
161 # FeedBurner requires a proper USER-AGENT...
162 curl_setopt($curl, CURL_HTTP_VERSION_1_1
, true);
163 curl_setopt($curl, CURLOPT_ENCODING
, "gzip, deflate");
164 curl_setopt($curl, CURLOPT_USERAGENT
, $useragent);
166 $data = curl_exec($curl);
167 $httpcode = curl_getinfo($curl, CURLINFO_HTTP_CODE
);
168 $httpcodeOK = isset($httpcode) and ($httpcode == 200 or $httpcode == 301);
171 # create http context and add timeout and user-agent
172 $context = stream_context_create(
175 'timeout' => $timeout,
176 'header' => "User-Agent: " . $useragent,
177 'follow_location' => true
180 'verify_peer' => false,
181 'allow_self_signed' => true
186 # only download page lesser than 4MB
187 $data = @file_get_contents($url, false, $context, -1, 4000000);
189 if (isset($http_response_header) and isset($http_response_header[0])) {
190 $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));
194 # if response is not empty and response is OK
195 if (isset($data) and isset($httpcodeOK) and $httpcodeOK) {
197 # take charset of page and get it
198 preg_match('#<meta .*charset=.*>#Usi', $data, $meta);
200 # if meta tag is found
201 if (!empty($meta[0])) {
202 preg_match('#charset="?(.*)"#si', $meta[0], $encoding);
203 # if charset is found set it otherwise, set it to utf-8
204 $html_charset = (!empty($encoding[1])) ? strtolower($encoding[1]) : 'utf-8';
205 if (empty($encoding[1])) $encoding[1] = 'utf-8';
207 $html_charset = 'utf-8';
211 # replace charset of url to charset of page
212 $data = str_replace('charset=' . $encoding[1], 'charset=' . $html_charset, $data);
222 * Headers for JSON export
226 public static function renderJson($data)
228 header('Cache-Control: no-cache, must-revalidate');
229 header('Expires: Sat, 26 Jul 1997 05:00:00 GMT');
230 header('Content-type: application/json; charset=UTF-8');
231 echo json_encode($data);
236 * Create new line in log file
240 public static function logm($message)
242 if (DEBUG_POCHE
&& php_sapi_name() != 'cli') {
243 $t = strval(date('Y/m/d_H:i:s')) . ' - ' . $_SERVER["REMOTE_ADDR"] . ' - ' . strval($message) . "\n";
244 file_put_contents(CACHE
. '/log.txt', $t, FILE_APPEND
);
245 error_log('DEBUG POCHE : ' . $message);
250 * Encode a URL by using a salt
255 public static function encodeString($string)
257 return sha1($string . SALT
);
264 * @param string $default
267 public static function checkVar($var, $default = '')
269 return ((isset($_REQUEST["$var"])) ? htmlentities($_REQUEST["$var"]) : $default);
273 * Returns the domain name for a URL
278 public static function getDomain($url)
280 return parse_url($url, PHP_URL_HOST
);
284 * For a given text, we calculate reading time for an article
289 public static function getReadingTime($text)
291 return floor(str_word_count(strip_tags($text)) / 200);
295 * Returns the correct header for a status code
297 * @param $status_code
299 private static function _status($status_code)
301 if (strpos(php_sapi_name(), 'apache') !== false) {
303 header('HTTP/1.0 '.$status_code);
307 header('Status: '.$status_code);
312 * Get the content for a given URL (by a call to FullTextFeed)
317 public static function getPageContent(Url
$url)
319 // Saving and clearing context
321 foreach( $GLOBALS as $key => $value ) {
322 if( $key != 'GLOBALS' && $key != '_SESSION' && $key != 'HTTP_SESSION_VARS' ) {
323 $GLOBALS[$key] = array();
324 $REAL[$key] = $value;
327 // Saving and clearing session
328 if (isset($_SESSION)) {
329 $REAL_SESSION = array();
330 foreach( $_SESSION as $key => $value ) {
331 $REAL_SESSION[$key] = $value;
332 unset($_SESSION[$key]);
336 // Running code in different context
337 $scope = function() {
338 extract( func_get_arg(1) );
339 $_GET = $_REQUEST = array(
340 "url" => $url->getUrl(),
342 "links" => "preserve",
345 "submit" => "Create Feed"
348 require func_get_arg(0);
349 $json = ob_get_contents();
354 // Silence $scope function to avoid
355 // issues with FTRSS when error_reporting is to high
356 // FTRSS generates PHP warnings which break output
357 $json = @$scope("inc/3rdparty/makefulltextfeed.php", array("url" => $url));
359 // Clearing and restoring context
360 foreach ($GLOBALS as $key => $value) {
361 if($key != "GLOBALS" && $key != "_SESSION" ) {
362 unset($GLOBALS[$key]);
365 foreach ($REAL as $key => $value) {
366 $GLOBALS[$key] = $value;
369 // Clearing and restoring session
370 if (isset($REAL_SESSION)) {
371 foreach($_SESSION as $key => $value) {
372 unset($_SESSION[$key]);
375 foreach($REAL_SESSION as $key => $value) {
376 $_SESSION[$key] = $value;
380 return json_decode($json, true);
384 * Returns whether we handle an AJAX (XMLHttpRequest) request.
386 * @return boolean whether we handle an AJAX (XMLHttpRequest) request.
388 public static function isAjaxRequest()
390 return isset($_SERVER['HTTP_X_REQUESTED_WITH']) && $_SERVER['HTTP_X_REQUESTED_WITH']==='XMLHttpRequest';
396 public static function emptyCache()
398 $files = new RecursiveIteratorIterator(
399 new RecursiveDirectoryIterator(CACHE
, RecursiveDirectoryIterator
::SKIP_DOTS
),
400 RecursiveIteratorIterator
::CHILD_FIRST
403 foreach ($files as $fileInfo) {
404 $filename = $fileInfo->getFilename();
405 if (!$filename[0] == '.') {
406 $todo = ($fileInfo->isDir() ? 'rmdir' : 'unlink');
407 $todo($fileInfo->getRealPath());
411 Tools
::logm('empty cache');
415 public static function generateToken()
417 if (ini_get('open_basedir') === '') {
418 if (strtoupper(substr(PHP_OS
, 0, 3)) === 'WIN') {
419 // alternative to /dev/urandom for Windows
420 $token = substr(base64_encode(uniqid(mt_rand(), true)), 0, 20);
422 $token = substr(base64_encode(file_get_contents('/dev/urandom', false, null, 0, 20)), 0, 15);
426 $token = substr(base64_encode(uniqid(mt_rand(), true)), 0, 20);
429 return str_replace('+', '', $token);
434 * Returns the doctype for an HTML document (used for Mozilla Bookmarks)
435 * @param simple_html_dom $doc
436 * @return doctype $el
440 public static function get_doctype($doc)
442 $els = $doc->find('unknown');
444 foreach ($els as $e => $el)
445 if ($el->parent()->tag
== 'root')