Proxy Improvements

This commit is contained in:
n1rwana 2023-08-03 11:25:35 +03:00
parent 6da44b7c47
commit 04dbede4c6
3 changed files with 37 additions and 14 deletions

View file

@ -12,8 +12,17 @@ class SecurityFilter extends HTMLPurifier_Filter
'/<img[^>]*src\s*=\s*["\']([^"\']*)["\'][^>]*>/i', '/<img[^>]*src\s*=\s*["\']([^"\']*)["\'][^>]*>/i',
function ($matches) { function ($matches) {
$originalSrc = $matches[1]; $originalSrc = $matches[1];
$encodedSrc = '/image.php?url=' . base64_encode($originalSrc); $src = $originalSrc;
return str_replace($originalSrc, $encodedSrc, $matches[0]); 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 $html
); );
@ -34,7 +43,7 @@ class Note extends Postable
{ {
protected $tableName = "notes"; protected $tableName = "notes";
protected function renderHTML(): string protected function renderHTML(?string $content = NULL): string
{ {
$config = HTMLPurifier_Config::createDefault(); $config = HTMLPurifier_Config::createDefault();
$config->set("Attr.AllowedClasses", []); $config->set("Attr.AllowedClasses", []);
@ -103,14 +112,16 @@ class Note extends Postable
]); ]);
$config->set('Filter.Custom', [new SecurityFilter()]); $config->set('Filter.Custom', [new SecurityFilter()]);
$source = NULL; $source = $content;
if(is_null($this->getRecord())) { if (!$source) {
if(isset($this->changes["source"])) if (is_null($this->getRecord())) {
$source = $this->changes["source"]; if (isset($this->changes["source"]))
else $source = $this->changes["source"];
throw new \LogicException("Can't render note without content set."); else
} else { throw new \LogicException("Can't render note without content set.");
$source = $this->getRecord()->source; } else {
$source = $this->getRecord()->source;
}
} }
$purifier = new HTMLPurifier($config); $purifier = new HTMLPurifier($config);
@ -138,8 +149,8 @@ class Note extends Postable
$this->setCached_Content($cached); $this->setCached_Content($cached);
$this->save(); $this->save();
} }
return $cached; return $this->renderHTML($cached);
} }
function getSource(): string function getSource(): string

View file

@ -26,7 +26,14 @@ final class ImagesProxyPresenter extends OpenVKPresenter
public function renderIndex(): 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)) { if (!$url || !filter_var($url, FILTER_VALIDATE_URL)) {
$this->placeholder(); $this->placeholder();
} }

View file

@ -102,6 +102,11 @@ openvk:
fartscroll: false fartscroll: false
testLabel: false testLabel: false
defaultMobileTheme: "" defaultMobileTheme: ""
imagesProxy:
replaceInNotes: true
settings:
url_prefix: ""
base64_decode_url: true
telemetry: telemetry:
plausible: plausible: