Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optionaly proxify images src with atmos/camo #781

Merged
merged 4 commits into from
Sep 1, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
128 changes: 68 additions & 60 deletions _docs/website/index.html

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions common.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

$f3->set('DEBUG',0);
$f3->set('version','2.16-SNAPSHOT');
$f3->set('AUTOLOAD',__dir__.'/;libs/f3/;libs/;libs/WideImage/;daos/;libs/twitteroauth/;libs/FeedWriter/;libs/fulltextrss/content-extractor/;libs/fulltextrss/readability/');
$f3->set('AUTOLOAD',__dir__.'/;libs/f3/;libs/;libs/WideImage/;daos/;libs/twitteroauth/;libs/FeedWriter/;libs/fulltextrss/content-extractor/;libs/fulltextrss/readability/;libs/WillWashburn/');
$f3->set('cache',__dir__.'/data/cache');
$f3->set('BASEDIR',__dir__);
$f3->set('LOCALES',__dir__.'/public/lang/');
Expand Down Expand Up @@ -38,7 +38,7 @@ function($f3) {
foreach($trace as $entry) {
$tracestr = $tracestr . $entry['file'] . ':' . $entry['line'] . "\n";
}

\F3::get('logger')->log($f3->get('ERROR.text') . $tracestr, \ERROR);
if (\F3::get('DEBUG')!=0) {
echo $f3->get('lang_error') . ": ";
Expand Down
2 changes: 2 additions & 0 deletions defaults.ini
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,5 @@ unread_order=
load_images_on_mobile=0
auto_hide_read_on_mobile=0
env_prefix=selfoss_
camo_domain=
camo_key=
52 changes: 39 additions & 13 deletions helpers/ViewHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,18 @@ class ViewHelper {
public function highlight($content, $searchWords) {
if(strlen(trim($searchWords))==0)
return $content;

if(!is_array($searchWords))
$searchWords = \helpers\Search::splitTerms($searchWords);

foreach($searchWords as $word)
$content = preg_replace('/(?!<[^<>])('.$word.')(?![^<>]*>)/i','<span class=found>$0</span>',$content);

return $content;
}
/**


/**
* removes img src attribute and saves the value in ref for
* loading it later
*
Expand All @@ -43,13 +43,13 @@ public function highlight($content, $searchWords) {
public function lazyimg($content) {
return preg_replace("/<img([^<]+)src=(['\"])([^\"']*)(['\"])([^<]*)>/i","<img$1ref='$3'$5>",$content);
}
/**


/**
* format given date as "x days ago"
*
* @return string with replaced formateddate
* @param
* @param
*/
public function dateago($datestr) {
$date = new \DateTime($datestr);
Expand All @@ -58,15 +58,41 @@ public function dateago($datestr) {
$ageInMinutes = $ageInSeconds / 60;
$ageInHours = $ageInMinutes / 60;
$ageInDays = $ageInHours / 24;

if($ageInMinutes<1)
return \F3::get('lang_seconds',round($ageInSeconds, 0));
if($ageInHours<1)
return \F3::get('lang_minutes',round($ageInMinutes, 0));
if($ageInDays<1)
return \F3::get('lang_hours',round($ageInHours, 0));

//return $datestr;
return \F3::get('lang_timestamp', $date->getTimestamp());
}
}

/**
* Proxify imgs through atmos/camo when not https
*
* @param string $content item content
* @return string item content
*/
public function camoflauge($content)
{
if (empty($content)) {
return $content;
}

$camo = new \WillWashburn\Phpamo\Phpamo(\F3::get('camo_key'), \F3::get('camo_domain'));
$dom = new \DOMDocument();
$dom->loadHTML($content);

foreach ($dom->getElementsByTagName('img') as $item) {
if ($item->hasAttribute('src')) {
$src = $item->getAttribute('src');
$item->setAttribute('src', $camo->camoHttpOnly($src));
}
}

return $dom->saveHTML();
}
}
17 changes: 17 additions & 0 deletions libs/WillWashburn/Phpamo/Encoder/EncoderInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php namespace WillWashburn\Phpamo\Encoder;

/**
* EncoderInterface
*
* @package WillWashburn\Phpamo\Encoder
*/
interface EncoderInterface {

/**
* @param $url
*
* @return mixed
*/
public function encode($url);

}
18 changes: 18 additions & 0 deletions libs/WillWashburn/Phpamo/Encoder/HexEncoder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php namespace WillWashburn\Phpamo\Encoder;

/**
* Class HexEncoder
* @package WillWashburn\Phpamo\Encoder
*/
class HexEncoder implements EncoderInterface
{
/**
* @param $url
*
* @return mixed
*/
public function encode($url)
{
return bin2hex($url);
}
}
20 changes: 20 additions & 0 deletions libs/WillWashburn/Phpamo/Encoder/QueryStringEncoder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php namespace WillWashburn\Phpamo\Encoder;

/**
* Encodes for the QueryString implementation of camo
*
* @package WillWashburn\Phpamo\Encoder
*/
class QueryStringEncoder implements EncoderInterface
{

/**
* @param $url
*
* @return mixed
*/
public function encode($url)
{
return rawurlencode($url);
}
}
19 changes: 19 additions & 0 deletions libs/WillWashburn/Phpamo/Formatter/FormatterInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php namespace WillWashburn\Phpamo\Formatter;

/**
* Interface FormatterInterface
*
* @package WillWashburn\Phpamo\Formatter
*/
interface FormatterInterface
{

/**
* @param $domain
* @param $digest
* @param $url
*
* @return mixed
*/
public function formatCamoUrl($domain, $digest, $url);
}
39 changes: 39 additions & 0 deletions libs/WillWashburn/Phpamo/Formatter/HexFormatter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php namespace WillWashburn\Phpamo\Formatter;

use WillWashburn\Phpamo\Encoder\HexEncoder;

/**
* Class HexFormatter
*
* @package WillWashburn\Phpamo\Formatter
*/
class HexFormatter implements FormatterInterface
{
/**
* @var HexEncoder
*/
private $encoder;

/**
* HexFormatter constructor.
*
* @param HexEncoder $encoder
*/
public function __construct(HexEncoder $encoder)
{
$this->encoder = $encoder;
}


/**
* @param $domain
* @param $digest
* @param $url
*
* @return mixed
*/
public function formatCamoUrl($domain, $digest, $url)
{
return 'https://' . $domain . '/' . $digest . '/' . $this->encoder->encode($url).'/';
}
}
38 changes: 38 additions & 0 deletions libs/WillWashburn/Phpamo/Formatter/QueryStringFormatter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php namespace WillWashburn\Phpamo\Formatter;

use WillWashburn\Phpamo\Encoder\QueryStringEncoder;

/**
* Class QueryStringFormatter
*
* @package WillWashburn\Phpamo\Formatter
*/
class QueryStringFormatter implements FormatterInterface
{
/**
* @var QueryStringEncoder
*/
private $encoder;

/**
* QueryStringFormatter constructor.
*
* @param QueryStringEncoder $encoder
*/
public function __construct(QueryStringEncoder $encoder)
{
$this->encoder = $encoder;
}

/**
* @param $domain
* @param $digest
* @param $url
*
* @return mixed
*/
public function formatCamoUrl($domain, $digest, $url)
{
return 'https://' . $domain . '/' . $digest . '?url=' . $this->encoder->encode($url);
}
}
107 changes: 107 additions & 0 deletions libs/WillWashburn/Phpamo/Phpamo.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
<?php namespace WillWashburn\Phpamo;

use Exception;
use WillWashburn\Phpamo\Encoder\HexEncoder;
use WillWashburn\Phpamo\Formatter\FormatterInterface;
use WillWashburn\Phpamo\Formatter\HexFormatter;

/**
* Fah-ham-o
* An exercise on going ham-o with camo.
*
* For more information about setting up Camo, please see
* https://github.com/atmos/camo
*
* @package WillWashburn\Phpamo
*/
class Phpamo
{
private $domain;
private $key;
private $formatter;

/**
* You're only required to pass in the key and domain for the basic setup
*
* @param $key
* @param $domain
* @param FormatterInterface $formatter
*/
public function __construct(
$key,
$domain,
FormatterInterface $formatter = null
)
{
$this->key = $key;
$this->domain = $domain;

$this->runSanityChecks();

if ( is_null($formatter) ) {
$formatter = new HexFormatter(new HexEncoder());
}

$this->formatter = $formatter;
}

/**
* Camoflauge all urls
*
* @param $url
*
* @return string
*/
public function camo($url)
{
return $this->formatter->formatCamoUrl(
$this->domain,
$this->getDigest($url),
$url
);
}

/**
* Camoflauge only the urls that are not currently https
*
* @param $url
*
* @return mixed
*/
public function camoHttpOnly($url)
{

$parts = parse_url($url);

if ( isset($parts['scheme']) && $parts['scheme'] == 'https' ) {
return $url;
}

return $this->camo($url);
}

/**
* @param $url string
*
* @return string
*/
protected function getDigest($url)
{
return hash_hmac('sha1', $url, $this->key);
}

/**
* @throws \Exception
*/
private function runSanityChecks()
{
if ( empty($this->key) ) {
throw new Exception('You need to supply a key to Phpamo');
}

if ( empty($this->domain) ) {
throw new Exception('You need to add a domain to Phpamo');
}
}

}
Loading