Skip to content

Commit

Permalink
Merge pull request #43 from blast007/master
Browse files Browse the repository at this point in the history
Camo image proxying and Windows support
  • Loading branch information
kongr45gpen committed Feb 22, 2016
2 parents 3816aaa + 2294d30 commit 2d3b1fc
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 1 deletion.
3 changes: 3 additions & 0 deletions app/console.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
@ECHO OFF
SET BIN_TARGET=%~dp0/console
php "%BIN_TARGET%" %*
13 changes: 13 additions & 0 deletions src/Config/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,19 @@ public function getConfigTreeBuilder()
->integerNode('push_port')->defaultValue(8592)->end()
->end()
->end()
->arrayNode('camo')
->addDefaultsIfNotSet()
->canBeEnabled()
->info("Settings for the camo image proxy")
->children()
->scalarNode('base_url')->isRequired()->attribute('manual', true)->end()
->scalarNode('key')->isRequired()->attribute('manual', true)->end()
->arrayNode('whitelisted_domains')
->prototype('scalar')
->end()
->end()
->end()
->end()
->end()
->end()

Expand Down
31 changes: 30 additions & 1 deletion src/MarkdownEngine.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,21 @@
*/
class MarkdownEngine extends \Parsedown
{
/**
* Constructor for our extension of Parsedown
*/
function __construct()
{
$this->camo = Array();
$this->camo['enabled'] = \Service::getParameter('bzion.features.camo.enabled');

if ($this->camo['enabled']) {
$this->camo['key'] = \Service::getParameter('bzion.features.camo.key');
$this->camo['base_url'] = \Service::getParameter('bzion.features.camo.base_url');
$this->camo['whitelisted_domains'] = \Service::getParameter('bzion.features.camo.whitelisted_domains');
}
}

/**
* Whether or not to allow images to be rendered. If set to false, it'll be rendered as a hyperlink
* @var bool
Expand All @@ -30,7 +45,21 @@ class MarkdownEngine extends \Parsedown
protected function inlineImage($Excerpt)
{
if ($this->allowImages) {
return parent::inlineImage($Excerpt);
$Image = parent::inlineImage($Excerpt);

if ($this->camo['enabled']) {
$parts = parse_url($Image['element']['attributes']['src']);

if (!isset($parts['host']) || strlen($parts['host']) === 0) {
return null;
}

if (!in_array($parts['host'], $this->camo['whitelisted_domains'])) {
$Image['element']['attributes']['src'] = $this->camo['base_url'].hash_hmac('sha1', $Image['element']['attributes']['src'], $this->camo['key']).'/'.bin2hex($Image['element']['attributes']['src']);
}
}

return $Image;
}

return null;
Expand Down

0 comments on commit 2d3b1fc

Please sign in to comment.