]> git.wh0rd.org - tt-rss.git/blame - plugins/af_unburn/init.php
af_unburn: set useragent
[tt-rss.git] / plugins / af_unburn / init.php
CommitLineData
9b725068
AD
1<?php
2class Af_Unburn extends Plugin {
3
4 private $link;
5 private $host;
6
7 function about() {
8 return array(1.0,
6368785e 9 "Resolves feedburner and similar feed redirector URLs (requires CURL)",
9b725068
AD
10 "fox");
11 }
12
13 function init($host) {
14 $this->link = $host->get_link();
15 $this->host = $host;
16
17 $host->add_hook($host::HOOK_ARTICLE_FILTER, $this);
18 }
19
20 function hook_article_filter($article) {
21 $owner_uid = $article["owner_uid"];
22
23 if (!function_exists("curl_init"))
24 return $article;
25
eb161344 26 if ((strpos($article["link"], "feedproxy.google.com") !== FALSE ||
a90952ec 27 strpos($article["link"], "/~r/") !== FALSE ||
eb161344 28 strpos($article["link"], "feedsportal.com") !== FALSE) &&
b30abdad 29 strpos($article["plugin_data"], "unburn,$owner_uid:") === FALSE) {
9b725068
AD
30
31 $ch = curl_init($article["link"]);
8055097c 32 curl_setopt($ch, CURLOPT_TIMEOUT, 5);
9b725068
AD
33 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
34 curl_setopt($ch, CURLOPT_HEADER, true);
35 curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
389859b5 36 curl_setopt($ch, CURLOPT_USERAGENT, SELF_USER_AGENT);
9b725068
AD
37
38 $contents = @curl_exec($ch);
39
40 $real_url = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL);
41
8c685908
AD
42 curl_close($ch);
43
9b725068 44 if ($real_url) {
8c685908
AD
45 /* remove the rest of it */
46
47 $query = parse_url($real_url, PHP_URL_QUERY);
48
49 if ($query && strpos($query, "utm_source") !== FALSE) {
50 $args = array();
51 parse_str($query, $args);
52
53 foreach (array("utm_source", "utm_medium", "utm_campaign") as $param) {
54 if (isset($args[$param])) unset($args[$param]);
55 }
56
57 $new_query = http_build_query($args);
58
59 if ($new_query != $query) {
60 $real_url = str_replace("?$query", "?$new_query", $real_url);
61 }
62 }
63
1e6463fd
AD
64 $real_url = preg_replace("/\?$/", "", $real_url);
65
b30abdad 66 $article["plugin_data"] = "unburn,$owner_uid:" . $article["plugin_data"];
9b725068
AD
67 $article["link"] = $real_url;
68 }
69 }
70
71 return $article;
72 }
73}
74?>