]> git.wh0rd.org - tt-rss.git/blame - plugins/af_unburn/init.php
Merge remote-tracking branch 'upstream/master'
[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 ||
e02555c1 28 strpos($article["link"], "feedsportal.com") !== FALSE)) {
9b725068 29
e02555c1 30 if (strpos($article["plugin_data"], "unburn,$owner_uid:") === FALSE) {
9b725068 31
e2b0054b 32 $ch = curl_init(geturl($article["link"]));
e02555c1
AD
33 curl_setopt($ch, CURLOPT_TIMEOUT, 5);
34 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
35 curl_setopt($ch, CURLOPT_HEADER, true);
e2b0054b 36 //curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
e02555c1 37 curl_setopt($ch, CURLOPT_USERAGENT, SELF_USER_AGENT);
9b725068 38
e02555c1 39 $contents = @curl_exec($ch);
9b725068 40
e02555c1 41 $real_url = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL);
8c685908 42
e02555c1 43 curl_close($ch);
8c685908 44
e02555c1
AD
45 if ($real_url) {
46 /* remove the rest of it */
8c685908 47
e02555c1 48 $query = parse_url($real_url, PHP_URL_QUERY);
8c685908 49
e02555c1
AD
50 if ($query && strpos($query, "utm_source") !== FALSE) {
51 $args = array();
52 parse_str($query, $args);
53
54 foreach (array("utm_source", "utm_medium", "utm_campaign") as $param) {
55 if (isset($args[$param])) unset($args[$param]);
56 }
8c685908 57
e02555c1 58 $new_query = http_build_query($args);
8c685908 59
e02555c1
AD
60 if ($new_query != $query) {
61 $real_url = str_replace("?$query", "?$new_query", $real_url);
62 }
8c685908 63 }
8c685908 64
e02555c1 65 $real_url = preg_replace("/\?$/", "", $real_url);
1e6463fd 66
e02555c1
AD
67 $article["plugin_data"] = "unburn,$owner_uid:" . $article["plugin_data"];
68 $article["link"] = $real_url;
69 }
70 } else if (isset($article["stored"]["link"])) {
71 $article["link"] = $article["stored"]["link"];
9b725068
AD
72 }
73 }
74
75 return $article;
76 }
e2b0054b
AD
77
78 function geturl($url){
79
80 (function_exists('curl_init')) ? '' : die('cURL Must be installed for geturl function to work. Ask your host to enable it or uncomment extension=php_curl.dll in php.ini');
81
82 $curl = curl_init();
83 $header[0] = "Accept: text/xml,application/xml,application/xhtml+xml,";
84 $header[0] .= "text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5";
85 $header[] = "Cache-Control: max-age=0";
86 $header[] = "Connection: keep-alive";
87 $header[] = "Keep-Alive: 300";
88 $header[] = "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7";
89 $header[] = "Accept-Language: en-us,en;q=0.5";
90 $header[] = "Pragma: ";
91
92 curl_setopt($curl, CURLOPT_URL, $url);
93 curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 5.1; rv:5.0) Gecko/20100101 Firefox/5.0 Firefox/5.0');
94 curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
95 curl_setopt($curl, CURLOPT_HEADER, true);
96 curl_setopt($curl, CURLOPT_REFERER, $url);
97 curl_setopt($curl, CURLOPT_ENCODING, 'gzip,deflate');
98 curl_setopt($curl, CURLOPT_AUTOREFERER, true);
99 curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
100 //curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true); //CURLOPT_FOLLOWLOCATION Disabled...
101 curl_setopt($curl, CURLOPT_TIMEOUT, 60);
102
103 $html = curl_exec($curl);
104
105 $status = curl_getinfo($curl);
106 curl_close($curl);
107
108 if($status['http_code']!=200){
109 if($status['http_code'] == 301 || $status['http_code'] == 302) {
110 list($header) = explode("\r\n\r\n", $html, 2);
111 $matches = array();
112 preg_match("/(Location:|URI:)[^(\n)]*/", $header, $matches);
113 $url = trim(str_replace($matches[1],"",$matches[0]));
114 $url_parsed = parse_url($url);
115 return (isset($url_parsed))? geturl($url, $referer):'';
116 }
117 $oline='';
118 foreach($status as $key=>$eline){$oline.='['.$key.']'.$eline.' ';}
119 $line =$oline." \r\n ".$url."\r\n-----------------\r\n";
120 $handle = @fopen('./curl.error.log', 'a');
121 fwrite($handle, $line);
122 return FALSE;
123 }
124 return $url;
125 }
9b725068
AD
126}
127?>