summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndrew Dolgov <[email protected]>2022-11-26 14:47:09 +0300
committerAndrew Dolgov <[email protected]>2022-11-26 14:47:09 +0300
commit0834b7ad208648b3a1c5adc09922a1f93b3e1f99 (patch)
tree1d4551ee9686e1454fbcdc3b730a2de7ac43739e
parente4f00908fc1696ec8129f3cb7d43f2065ca8bd7c (diff)
pass http referrer when downloading media
-rw-r--r--init.php31
1 files changed, 23 insertions, 8 deletions
diff --git a/init.php b/init.php
index d695b44..07abca4 100644
--- a/init.php
+++ b/init.php
@@ -5,6 +5,9 @@ class Api_Resize_Media extends Plugin {
const DEFAULT_QUALITY = 80;
const IGNORE_SCHEMES = [ "magnet", "data" ];
+ /** @var array<int, string> */
+ private $article_link_cache = [];
+
/** @var PluginHost $host */
private $host;
@@ -181,6 +184,7 @@ class Api_Resize_Media extends Plugin {
public function api_resize() : void {
$url = UrlHelper::validate($_REQUEST["url"]);
+ $referrer = UrlHelper::validate($_REQUEST["referrer"] ?? "");
$width = (int) $_REQUEST["width"];
$force_stamp = sql_bool_to_bool($_REQUEST["force_stamp"]);
@@ -223,7 +227,8 @@ class Api_Resize_Media extends Plugin {
}
} else {
- $data = UrlHelper::fetch(["url" => $url, "max_size" => Config::get(Config::MAX_CACHE_FILE_SIZE)]);
+ $data = UrlHelper::fetch(["url" => $url, "http_referrer" => $referrer,
+ "max_size" => Config::get(Config::MAX_CACHE_FILE_SIZE)]);
if ($data) {
if ($this->cache->put($local_filename, $data)) {
@@ -280,7 +285,7 @@ class Api_Resize_Media extends Plugin {
}
}
- private function rewrite_url_if_needed(string $url, int $width, bool $force_stamp = false) : string {
+ private function rewrite_url_if_needed(string $url, int $width, string $referrer = "", bool $force_stamp = false) : string {
$scheme = parse_url($url, PHP_URL_SCHEME) ?? "";
if ($width > 0 && !in_array($scheme, self::IGNORE_SCHEMES)) {
@@ -300,7 +305,7 @@ class Api_Resize_Media extends Plugin {
if ($this->cache->exists($flag)) {
return $this->host->get_public_method_url($this, "api_resize",
- ["url" => $url, "width" => $width, "force_stamp" => $force_stamp]);
+ ["url" => $url, "referrer" => $referrer, "width" => $width, "force_stamp" => $force_stamp]);
}
}
}
@@ -328,7 +333,7 @@ class Api_Resize_Media extends Plugin {
foreach ($imgs as $img) {
$orig_src = $img->getAttribute("src");
- $new_src = $this->rewrite_url_if_needed($orig_src, $width);
+ $new_src = $this->rewrite_url_if_needed($orig_src, $width, $article['link']);
if ($new_src != $orig_src) {
$need_saving = true;
@@ -353,7 +358,7 @@ class Api_Resize_Media extends Plugin {
$vids = $xpath->query("//video[@poster]");
foreach ($vids as $vid) {
- $new_poster = $this->rewrite_url_if_needed($vid->getAttribute("poster"), $width, true);
+ $new_poster = $this->rewrite_url_if_needed($vid->getAttribute("poster"), $width, $article['link'], true);
if ($new_poster != $vid->getAttribute("poster")) {
$vid->setAttribute("poster", $new_poster);
@@ -365,7 +370,7 @@ class Api_Resize_Media extends Plugin {
$psrcs = $xpath->query("//picture/source[@src]");
foreach ($psrcs as $src) {
- $new_src = $this->rewrite_url_if_needed($src->getAttribute("src"), $width);
+ $new_src = $this->rewrite_url_if_needed($src->getAttribute("src"), $width, $article['link']);
if ($new_src != $src->getAttribute("src")) {
$src->setAttribute("src", $new_src);
@@ -380,7 +385,7 @@ class Api_Resize_Media extends Plugin {
for ($i = 0; $i < count($tmp); $i++) {
if (preg_match("/image/", $tmp[$i]["content_type"])) {
- $tmp[$i]["content_url"] = $this->rewrite_url_if_needed($tmp[$i]["content_url"], $width);
+ $tmp[$i]["content_url"] = $this->rewrite_url_if_needed($tmp[$i]["content_url"], $width, $article['link']);
}
}
}
@@ -402,7 +407,17 @@ class Api_Resize_Media extends Plugin {
function hook_enclosure_entry($enc, $id, $rv) {
$force_width = (int) $this->host->profile_get($this, "force_width", 0);
- $enc["content_url"] = $this->rewrite_url_if_needed($enc["content_url"], $force_width);
+ if (!isset($this->article_link_cache[$id])) {
+ $article = ORM::for_table('ttrss_entries')
+ ->join('ttrss_user_entries', ['ref_id', '=', 'id'], 'ue')
+ ->where('ue.owner_uid', $_SESSION['uid'])
+ ->find_one((int)$id);
+
+ $this->article_link_cache[$id] = $article->link;
+ }
+
+ $enc["content_url"] = $this->rewrite_url_if_needed($enc["content_url"], $force_width,
+ ($this->article_link_cache[$id] ?? ""));
return $enc;
}