2014-12-24 21:58:24 +01:00
|
|
|
<?php
|
|
|
|
|
2016-08-25 03:17:58 +02:00
|
|
|
namespace Miniflux\Handler\Proxy;
|
2014-12-24 21:58:24 +01:00
|
|
|
|
2016-08-25 03:17:58 +02:00
|
|
|
use Miniflux\Helper;
|
2016-04-18 01:34:54 +02:00
|
|
|
use PicoFeed\Client\ClientException;
|
2015-02-01 22:54:57 +01:00
|
|
|
use PicoFeed\Config\Config as PicoFeedConfig;
|
|
|
|
use PicoFeed\Filter\Filter;
|
2014-12-24 21:58:24 +01:00
|
|
|
use PicoFeed\Client\Client;
|
2015-02-07 17:09:43 +01:00
|
|
|
use PicoFeed\Logging\Logger;
|
2014-12-24 21:58:24 +01:00
|
|
|
|
2015-05-17 19:33:13 +02:00
|
|
|
function rewrite_link($link)
|
2015-02-07 17:09:43 +01:00
|
|
|
{
|
2015-04-11 02:57:44 +02:00
|
|
|
if (Helper\is_secure_connection() && strpos($link, 'http:') === 0) {
|
2015-02-07 17:19:25 +01:00
|
|
|
$link = '?action=proxy&url='.rawurlencode($link);
|
2014-12-24 21:58:24 +01:00
|
|
|
}
|
2015-02-01 22:54:57 +01:00
|
|
|
|
|
|
|
return $link;
|
|
|
|
}
|
|
|
|
|
2015-05-17 19:33:13 +02:00
|
|
|
function rewrite_html($html, $website, $proxy_images, $cloak_referrer)
|
2015-02-01 22:54:57 +01:00
|
|
|
{
|
|
|
|
if ($html === '' // no content, no proxy
|
|
|
|
|| (! $cloak_referrer && ! $proxy_images) // neither cloaking nor proxing enabled
|
2015-04-11 02:57:44 +02:00
|
|
|
|| (! $cloak_referrer && $proxy_images && ! Helper\is_secure_connection())) { // only proxy enabled, but not connected via HTTPS
|
2015-02-01 22:54:57 +01:00
|
|
|
|
|
|
|
return $html;
|
2014-12-24 21:58:24 +01:00
|
|
|
}
|
2015-01-09 14:43:45 +01:00
|
|
|
|
2015-02-01 22:54:57 +01:00
|
|
|
$config = new PicoFeedConfig();
|
|
|
|
$config->setFilterImageProxyUrl('?action=proxy&url=%s');
|
2015-01-09 14:43:45 +01:00
|
|
|
|
2015-02-01 22:54:57 +01:00
|
|
|
if (! $cloak_referrer && $proxy_images) {
|
|
|
|
// image proxy mode only: https links do not need to be proxied, since
|
|
|
|
// they do not trigger mixed content warnings.
|
|
|
|
$config->setFilterImageProxyProtocol('http');
|
2016-04-18 01:44:45 +02:00
|
|
|
} elseif (! $proxy_images && $cloak_referrer && Helper\is_secure_connection()) {
|
2015-02-01 22:54:57 +01:00
|
|
|
// cloaking mode only: if a request from a HTTPS connection to a HTTP
|
|
|
|
// connection is made, the referrer will be omitted by the browser.
|
|
|
|
// Only the referrer for HTTPS to HTTPs requests needs to be cloaked.
|
|
|
|
$config->setFilterImageProxyProtocol('https');
|
|
|
|
}
|
|
|
|
|
|
|
|
$filter = Filter::html($html, $website);
|
|
|
|
$filter->setConfig($config);
|
|
|
|
|
|
|
|
return $filter->execute();
|
|
|
|
}
|
|
|
|
|
|
|
|
function download($url)
|
|
|
|
{
|
2015-05-17 19:26:51 +02:00
|
|
|
try {
|
2016-12-26 15:44:53 +01:00
|
|
|
if (DEBUG_MODE) {
|
2015-05-17 19:26:51 +02:00
|
|
|
Logger::enable();
|
|
|
|
}
|
2015-02-07 17:09:43 +01:00
|
|
|
|
2015-05-17 19:26:51 +02:00
|
|
|
$client = Client::getInstance();
|
2016-12-26 15:44:53 +01:00
|
|
|
$client->setUserAgent(HTTP_USER_AGENT);
|
2015-05-17 19:26:51 +02:00
|
|
|
$client->enablePassthroughMode();
|
|
|
|
$client->execute($url);
|
2016-12-26 15:44:53 +01:00
|
|
|
} catch (ClientException $e) {
|
|
|
|
}
|
2014-12-24 21:58:24 +01:00
|
|
|
}
|