diff options
author | Andrew Dolgov <[email protected]> | 2018-09-07 09:38:11 +0300 |
---|---|---|
committer | Andrew Dolgov <[email protected]> | 2018-09-07 09:38:11 +0300 |
commit | cbd007bf380695858bd39c5b6f286efebfd0836a (patch) | |
tree | fc2499be0d4fcabf93d6e552ca9698ae68940964 |
initial
-rw-r--r-- | init.php | 62 |
1 files changed, 62 insertions, 0 deletions
diff --git a/init.php b/init.php new file mode 100644 index 0000000..90d01a8 --- /dev/null +++ b/init.php @@ -0,0 +1,62 @@ +<?php +class Zz_Text_Cached extends Plugin { + private $host; + + function about() { + return array(1.0, + "Rewrite destinations of text links if local cache exists", + "fox"); + } + + function init($host) { + $this->host = $host; + + $host->add_hook($host::HOOK_RENDER_ARTICLE, $this); + $host->add_hook($host::HOOK_RENDER_ARTICLE_CDM, $this); + } + + function hook_render_article($article) { + return $this->hook_render_article_cdm($article); + } + + function hook_render_article_cdm($article) { + + $doc = new DOMDocument(); + + $found = false; + + if (@$doc->loadHTML($article["content"])) { + + $xpath = new DOMXpath($doc); + $elems = $xpath->query('(//*[@href])'); + + foreach ($elems as $elem) { + $url = $elem->getAttribute("href"); + $local_filename = CACHE_DIR . "/images/" . sha1($url); + + if (file_exists($local_filename) && filesize($local_filename) > 0) { + $local_url = get_self_url_prefix() . '/public.php?op=cached_url&hash=' . sha1($url); + + $elem->setAttribute("href", $local_url); + $elem->nodeValue = ""; + $elem->appendChild($doc->createTextNode($local_url)); + + $found = true; + } + } + } + + if ($found) { + $article["content"] = $doc->saveXML(); + } + + return $article; + } + + + function api_version() { + return 2; + } + +} +?> |