Browse Source

move rewrite_cached_urls to DiskCache::rewriteUrls()

Andrew Dolgov 1 year ago
parent
commit
133c2b482b
5 changed files with 77 additions and 65 deletions
  1. 2 2
      classes/api.php
  2. 73 13
      classes/diskcache.php
  3. 1 1
      classes/feeds.php
  4. 1 1
      classes/handler/public.php
  5. 0 48
      include/functions.php

+ 2 - 2
classes/api.php

@@ -379,7 +379,7 @@ class API extends Handler {
 					$article = $p->hook_render_article_api(array("article" => $article));
 				}
 
-				$article['content'] = rewrite_cached_urls($article['content']);
+				$article['content'] = DiskCache::rewriteUrls($article['content']);
 
 				array_push($articles, $article);
 
@@ -801,7 +801,7 @@ class API extends Handler {
 						$headline_row = $p->hook_render_article_api(array("headline" => $headline_row));
 					}
 
-					$headline_row['content'] = rewrite_cached_urls($headline_row['content']);
+					$headline_row['content'] = DiskCache::rewriteUrls($headline_row['content']);
 
 					array_push($headlines, $headline_row);
 				}

+ 73 - 13
classes/diskcache.php

@@ -1,63 +1,123 @@
 <?php
-class DiskCache {
+class DiskCache
+{
 	private $dir;
 
-	public function __construct($dir) {
+	public function __construct($dir)
+	{
 		$this->dir = basename($dir);
 	}
 
-	public function getDir() {
+	public function getDir()
+	{
 		return $this->dir;
 	}
 
-	public function isWritable() {
+	public function isWritable()
+	{
 		return is_dir($this->dir) && is_writable($this->dir);
 	}
 
-	public function exists($filename) {
+	public function exists($filename)
+	{
 		return file_exists($this->getFullPath($filename));
 	}
 
-	public function getSize($filename) {
+	public function getSize($filename)
+	{
 		if ($this->exists($filename))
 			return filesize($this->getFullPath($filename));
 		else
 			return -1;
 	}
 
-	public function getFullPath($filename) {
+	public function getFullPath($filename)
+	{
 		$filename = basename($filename);
 
 		return CACHE_DIR . "/" . $this->dir . "/" . $filename;
 	}
 
-	public function put($filename, $data) {
+	public function put($filename, $data)
+	{
 		return file_put_contents($this->getFullPath($filename), $data);
 	}
 
-	public function touch($filename) {
+	public function touch($filename)
+	{
 		return touch($this->getFullPath($filename));
 	}
 
-	public function get($filename) {
+	public function get($filename)
+	{
 		if ($this->exists($filename))
 			return file_get_contents($this->getFullPath($filename));
 		else
 			return null;
 	}
 
-	public function getMimeType($filename) {
+	public function getMimeType($filename)
+	{
 		if ($this->exists($filename))
 			return mime_content_type($this->getFullPath($filename));
 		else
 			return null;
 	}
 
-	public function send($filename) {
+	public function send($filename)
+	{
 		return send_local_file($this->getFullPath($filename));
 	}
 
-	static public function getUrl($filename) {
+	static public function getUrl($filename)
+	{
 		return get_self_url_prefix() . "/public.php?op=cached_url&file=" . $filename;
 	}
+
+	// check for locally cached (media) URLs and rewrite to local versions
+	// this is called separately after sanitize() and plugin render article hooks to allow
+	// plugins work on original source URLs used before caching
+	static public function rewriteUrls($str)
+	{
+		$res = trim($str);
+		if (!$res) return '';
+
+		$doc = new DOMDocument();
+		if ($doc->loadHTML('<?xml encoding="UTF-8">' . $res)) {
+			$xpath = new DOMXPath($doc);
+			$cache = new DiskCache("images");
+
+			$entries = $xpath->query('(//img[@src]|//picture/source[@src]|//video[@poster]|//video/source[@src]|//audio/source[@src])');
+
+			$need_saving = false;
+
+			foreach ($entries as $entry) {
+
+				if ($entry->hasAttribute('src') || $entry->hasAttribute('poster')) {
+
+					// should be already absolutized because this is called after sanitize()
+					$src = $entry->hasAttribute('poster') ? $entry->getAttribute('poster') : $entry->getAttribute('src');
+					$cached_filename = sha1($src);
+
+					if ($cache->getSize($cached_filename) > 0) {
+
+						$src = DiskCache::getUrl(sha1($src));
+
+						if ($entry->hasAttribute('poster'))
+							$entry->setAttribute('poster', $src);
+						else
+							$entry->setAttribute('src', $src);
+
+						$need_saving = true;
+					}
+				}
+			}
+
+			if ($need_saving) {
+				$doc->removeChild($doc->firstChild); //remove doctype
+				$res = $doc->saveHTML();
+			}
+		}
+		return $res;
+	}
 }

+ 1 - 1
classes/feeds.php

@@ -307,7 +307,7 @@ class Feeds extends Handler_Protected {
                     $line = $p->hook_render_article_cdm($line);
                 }
 
-                $line['content'] = rewrite_cached_urls($line['content']);
+                $line['content'] = DiskCache::rewriteUrls($line['content']);
 
                 if ($line['note'])
                     $line['note'] = Article::format_article_note($id, $line['note']);

+ 1 - 1
classes/handler/public.php

@@ -382,7 +382,7 @@ class Handler_Public extends Handler {
 				$line = $p->hook_render_article($line);
 			}
 
-			$line['content'] = rewrite_cached_urls($line['content']);
+			$line['content'] = DiskCache::rewriteUrls($line['content']);
 
 			$enclosures = Article::get_article_enclosures($line["id"]);
 

+ 0 - 48
include/functions.php

@@ -1233,51 +1233,6 @@
 		return false;
 	}
 
-	// check for locally cached (media) URLs and rewrite to local versions
-	// this is called separately after sanitize() and plugin render article hooks to allow
-	// plugins work on original source URLs used before caching
-
-	function rewrite_cached_urls($str) {
-		$res = trim($str); if (!$res) return '';
-
-		$doc = new DOMDocument();
-		$doc->loadHTML('<?xml encoding="UTF-8">' . $res);
-		$xpath = new DOMXPath($doc);
-
-		$entries = $xpath->query('(//img[@src]|//picture/source[@src]|//video[@poster]|//video/source[@src]|//audio/source[@src])');
-
-		$need_saving = false;
-
-		foreach ($entries as $entry) {
-
-			if ($entry->hasAttribute('src') || $entry->hasAttribute('poster')) {
-
-				// should be already absolutized because this is called after sanitize()
-				$src = $entry->hasAttribute('poster') ? $entry->getAttribute('poster') : $entry->getAttribute('src');
-				$cached_filename = CACHE_DIR . '/images/' . sha1($src);
-
-				if (file_exists($cached_filename)) {
-
-					$src = DiskCache::getUrl(sha1($src));
-
-					if ($entry->hasAttribute('poster'))
-						$entry->setAttribute('poster', $src);
-					else
-						$entry->setAttribute('src', $src);
-
-					$need_saving = true;
-				}
-			}
-		}
-
-		if ($need_saving) {
-			$doc->removeChild($doc->firstChild); //remove doctype
-			$res = $doc->saveHTML();
-		}
-
-		return $res;
-	}
-
 	function sanitize($str, $force_remove_images = false, $owner = false, $site_url = false, $highlight_words = false, $article_id = false) {
 		if (!$owner) $owner = $_SESSION["uid"];
 
@@ -1302,9 +1257,6 @@
 
 			if ($entry->hasAttribute('src')) {
 				$src = rewrite_relative_url($rewrite_base_url, $entry->getAttribute('src'));
-
-				// cache stuff has gone to rewrite_cached_urls()
-
 				$entry->setAttribute('src', $src);
 			}