Skip to content

Commit

Permalink
Proxy Improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
n1rwana committed Aug 3, 2023
1 parent 6da44b7 commit 04dbede
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 14 deletions.
37 changes: 24 additions & 13 deletions Web/Models/Entities/Note.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,17 @@ public function preFilter($html, $config, $context)
'/<img[^>]*src\s*=\s*["\']([^"\']*)["\'][^>]*>/i',
function ($matches) {
$originalSrc = $matches[1];
$encodedSrc = '/image.php?url=' . base64_encode($originalSrc);
return str_replace($originalSrc, $encodedSrc, $matches[0]);
$src = $originalSrc;
if (!str_contains($src, "/image.php?url=")) {
$src = '/image.php?url=' . base64_encode($originalSrc);
} else {
if (!OPENVK_ROOT_CONF["openvk"]["preferences"]["imagesProxy"]["replaceInNotes"]) {
$src = preg_replace_callback('/(.*)\/image\.php\?url=(.*)/i', function ($matches) {
return base64_decode($matches[2]);
}, $src);
}
}
return str_replace($originalSrc, $src, $matches[0]);
},
$html
);
Expand All @@ -34,7 +43,7 @@ class Note extends Postable
{
protected $tableName = "notes";

protected function renderHTML(): string
protected function renderHTML(?string $content = NULL): string
{
$config = HTMLPurifier_Config::createDefault();
$config->set("Attr.AllowedClasses", []);
Expand Down Expand Up @@ -103,14 +112,16 @@ protected function renderHTML(): string
]);
$config->set('Filter.Custom', [new SecurityFilter()]);

$source = NULL;
if(is_null($this->getRecord())) {
if(isset($this->changes["source"]))
$source = $this->changes["source"];
else
throw new \LogicException("Can't render note without content set.");
} else {
$source = $this->getRecord()->source;
$source = $content;
if (!$source) {
if (is_null($this->getRecord())) {
if (isset($this->changes["source"]))
$source = $this->changes["source"];
else
throw new \LogicException("Can't render note without content set.");
} else {
$source = $this->getRecord()->source;
}
}

$purifier = new HTMLPurifier($config);
Expand Down Expand Up @@ -138,8 +149,8 @@ function getText(): string
$this->setCached_Content($cached);
$this->save();
}
return $cached;

return $this->renderHTML($cached);
}

function getSource(): string
Expand Down
9 changes: 8 additions & 1 deletion Web/Presenters/ImagesProxyPresenter.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,14 @@ private function placeholder(): void

public function renderIndex(): void
{
$url = base64_decode($this->requestParam("url"));
$this->assertUserLoggedIn();

$url = $this->requestParam("url");
if (OPENVK_ROOT_CONF["openvk"]["preferences"]["imagesProxy"]["settings"]["base64_decode_url"]) {
$url = base64_decode($url);
}

$url = OPENVK_ROOT_CONF["openvk"]["preferences"]["imagesProxy"]["settings"]["url_prefix"] . $url;
if (!$url || !filter_var($url, FILTER_VALIDATE_URL)) {
$this->placeholder();
}
Expand Down
5 changes: 5 additions & 0 deletions openvk-example.yml
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,11 @@ openvk:
fartscroll: false
testLabel: false
defaultMobileTheme: ""
imagesProxy:
replaceInNotes: true
settings:
url_prefix: ""
base64_decode_url: true

telemetry:
plausible:
Expand Down

0 comments on commit 04dbede

Please sign in to comment.