summaryrefslogtreecommitdiff
path: root/plugins/af_gocomics/af_gocomics.php
blob: 1dd188672e1af1a2d0828f45fe1e75a3de1cefd7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
<?php
class Af_GoComics extends Plugin {

	private $link;
	private $host;

	function about() {
		return array(1.0,
			"Strip unnecessary stuff from gocomics feeds",
			"fox");
	}

	function init($host) {
		$this->link = $host->get_link();
		$this->host = $host;

		$host->add_hook($host::HOOK_ARTICLE_FILTER, $this);
	}

	function hook_article_filter($article) {
		$owner_uid = $article["owner_uid"];

		if (strpos($article["guid"], "gocomics.com") !== FALSE && strpos($article["guid"], "gocomics,$owner_uid:") === FALSE) {
			$doc = new DOMDocument();
			@$doc->loadHTML(fetch_file_contents($article["link"]));

			$basenode = false;

			if ($doc) {
				$xpath = new DOMXPath($doc);
				$entries = $xpath->query('(//img[@src])'); // we might also check for img[@class='strip'] I guess...

				$matches = array();

				foreach ($entries as $entry) {

					if (preg_match("/(http:\/\/assets.amuniversal.com\/.*)/i", $entry->getAttribute("src"), $matches)) {

						$entry->setAttribute("src", $matches[0]);
						$basenode = $entry;
						break;
					}
				}

				if ($basenode) {
					$article["content"] = $doc->saveXML($basenode, LIBXML_NOEMPTYTAG);

					// we need to update guid with owner_uid because our local article is different from the one
					// other users with this plugin disabled might get
					$article["guid"] = "gocomics,$owner_uid:" . $article["guid"];
				}
			}
		}

		return $article;
	}
}
?>