2016-09-25 23:22:33 +02:00
|
|
|
<?php
|
2017-02-14 17:28:07 +01:00
|
|
|
function getContents($url,
|
|
|
|
$use_include_path = false,
|
|
|
|
$context = null,
|
|
|
|
$offset = 0,
|
|
|
|
$maxlen = null){
|
2016-09-25 23:22:33 +02:00
|
|
|
$contextOptions = array(
|
|
|
|
'http' => array(
|
2016-12-12 11:29:34 +01:00
|
|
|
'user_agent' => ini_get('user_agent'),
|
|
|
|
'accept_encoding' => 'gzip'
|
2016-09-25 23:22:33 +02:00
|
|
|
)
|
|
|
|
);
|
|
|
|
|
2017-07-29 19:28:00 +02:00
|
|
|
if(defined('PROXY_URL') && !defined('NOPROXY')) {
|
2016-09-25 23:22:33 +02:00
|
|
|
$contextOptions['http']['proxy'] = PROXY_URL;
|
|
|
|
$contextOptions['http']['request_fulluri'] = true;
|
|
|
|
|
2017-07-29 19:28:00 +02:00
|
|
|
if(is_null($context)) {
|
2016-09-25 23:22:33 +02:00
|
|
|
$context = stream_context_create($contextOptions);
|
|
|
|
} else {
|
|
|
|
$prevContext = $context;
|
2017-07-29 19:28:00 +02:00
|
|
|
if(!stream_context_set_option($context, $contextOptions)) {
|
2016-09-25 23:22:33 +02:00
|
|
|
$context = $prevContext;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-29 19:28:00 +02:00
|
|
|
if(is_null($maxlen)) {
|
2016-12-15 20:24:31 +01:00
|
|
|
$content = file_get_contents($url, $use_include_path, $context, $offset);
|
2016-09-25 23:22:33 +02:00
|
|
|
} else {
|
2016-12-15 20:24:31 +01:00
|
|
|
$content = file_get_contents($url, $use_include_path, $context, $offset, $maxlen);
|
2016-09-25 23:22:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if($content === false)
|
|
|
|
debugMessage('Cant\'t download ' . $url);
|
|
|
|
|
|
|
|
// handle compressed data
|
2017-07-29 19:28:00 +02:00
|
|
|
foreach($http_response_header as $header) {
|
|
|
|
if(stristr($header, 'content-encoding')) {
|
|
|
|
switch(true) {
|
2016-09-25 23:22:33 +02:00
|
|
|
case stristr($header, 'gzip'):
|
|
|
|
$content = gzinflate(substr($content, 10, -8));
|
|
|
|
break;
|
|
|
|
case stristr($header, 'compress'):
|
|
|
|
//TODO
|
|
|
|
case stristr($header, 'deflate'):
|
|
|
|
//TODO
|
|
|
|
case stristr($header, 'brotli'):
|
|
|
|
//TODO
|
|
|
|
returnServerError($header . '=> Not implemented yet');
|
|
|
|
break;
|
|
|
|
case stristr($header, 'identity'):
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
returnServerError($header . '=> Unknown compression');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $content;
|
|
|
|
}
|
|
|
|
|
2017-02-14 17:28:07 +01:00
|
|
|
function getSimpleHTMLDOM($url,
|
|
|
|
$use_include_path = false,
|
|
|
|
$context = null,
|
|
|
|
$offset = 0,
|
|
|
|
$maxLen = null,
|
|
|
|
$lowercase = true,
|
|
|
|
$forceTagsClosed = true,
|
|
|
|
$target_charset = DEFAULT_TARGET_CHARSET,
|
|
|
|
$stripRN = true,
|
|
|
|
$defaultBRText = DEFAULT_BR_TEXT,
|
|
|
|
$defaultSpanText = DEFAULT_SPAN_TEXT){
|
2016-09-25 23:22:33 +02:00
|
|
|
$content = getContents($url, $use_include_path, $context, $offset, $maxLen);
|
2017-02-14 17:28:07 +01:00
|
|
|
return str_get_html($content,
|
|
|
|
$lowercase,
|
|
|
|
$forceTagsClosed,
|
|
|
|
$target_charset,
|
|
|
|
$stripRN,
|
|
|
|
$defaultBRText,
|
|
|
|
$defaultSpanText);
|
2016-09-25 23:22:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Maintain locally cached versions of pages to avoid multiple downloads.
|
|
|
|
* @param url url to cache
|
|
|
|
* @param duration duration of the cache file in seconds (default: 24h/86400s)
|
|
|
|
* @return content of the file as string
|
|
|
|
*/
|
2017-02-14 17:28:07 +01:00
|
|
|
function getSimpleHTMLDOMCached($url,
|
|
|
|
$duration = 86400,
|
|
|
|
$use_include_path = false,
|
|
|
|
$context = null,
|
|
|
|
$offset = 0,
|
|
|
|
$maxLen = null,
|
|
|
|
$lowercase = true,
|
|
|
|
$forceTagsClosed = true,
|
|
|
|
$target_charset = DEFAULT_TARGET_CHARSET,
|
|
|
|
$stripRN = true,
|
|
|
|
$defaultBRText = DEFAULT_BR_TEXT,
|
|
|
|
$defaultSpanText = DEFAULT_SPAN_TEXT){
|
2016-09-25 23:22:33 +02:00
|
|
|
debugMessage('Caching url ' . $url . ', duration ' . $duration);
|
|
|
|
|
2016-10-08 16:30:01 +02:00
|
|
|
// Initialize cache
|
|
|
|
$cache = Cache::create('FileCache');
|
|
|
|
$cache->setPath(CACHE_DIR . '/pages');
|
|
|
|
$cache->purgeCache(86400); // 24 hours (forced)
|
2016-09-25 23:22:33 +02:00
|
|
|
|
2016-10-08 16:30:01 +02:00
|
|
|
$params = [$url];
|
|
|
|
$cache->setParameters($params);
|
2016-09-25 23:22:33 +02:00
|
|
|
|
2016-10-08 16:30:01 +02:00
|
|
|
// Determine if cached file is within duration
|
|
|
|
$time = $cache->getTime();
|
|
|
|
if($time !== false
|
|
|
|
&& (time() - $duration < $time)
|
2017-07-29 19:28:00 +02:00
|
|
|
&& (!defined('DEBUG') || DEBUG !== true)) { // Contents within duration
|
2016-10-08 16:30:01 +02:00
|
|
|
$content = $cache->loadData();
|
|
|
|
} else { // Content not within duration
|
2016-09-25 23:22:33 +02:00
|
|
|
$content = getContents($url, $use_include_path, $context, $offset, $maxLen);
|
2017-07-29 19:28:00 +02:00
|
|
|
if($content !== false) {
|
2016-10-08 16:30:01 +02:00
|
|
|
$cache->saveData($content);
|
2016-09-25 23:22:33 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-14 17:28:07 +01:00
|
|
|
return str_get_html($content,
|
|
|
|
$lowercase,
|
|
|
|
$forceTagsClosed,
|
|
|
|
$target_charset,
|
|
|
|
$stripRN,
|
|
|
|
$defaultBRText,
|
|
|
|
$defaultSpanText);
|
2016-09-25 23:22:33 +02:00
|
|
|
}
|