]> git.wh0rd.org - tt-rss.git/blob - plugins/af_unburn/init.php
plugins: remove obsolete plugin_data/stored stuff
[tt-rss.git] / plugins / af_unburn / init.php
1 <?php
2 class Af_Unburn extends Plugin {
3 private $host;
4
5 function about() {
6 return array(1.0,
7 "Resolves feedburner and similar feed redirector URLs (requires CURL)",
8 "fox");
9 }
10
11 function init($host) {
12 $this->host = $host;
13
14 $host->add_hook($host::HOOK_ARTICLE_FILTER, $this);
15 }
16
17 function hook_article_filter($article) {
18 $owner_uid = $article["owner_uid"];
19
20 if (!function_exists("curl_init"))
21 return $article;
22
23 if ((strpos($article["link"], "feedproxy.google.com") !== FALSE ||
24 strpos($article["link"], "/~r/") !== FALSE ||
25 strpos($article["link"], "feedsportal.com") !== FALSE)) {
26
27 if (ini_get("safe_mode") || ini_get("open_basedir")) {
28 $ch = curl_init(geturl($article["link"]));
29 } else {
30 $ch = curl_init($article["link"]);
31 }
32
33 curl_setopt($ch, CURLOPT_TIMEOUT, 5);
34 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
35 curl_setopt($ch, CURLOPT_HEADER, true);
36 curl_setopt($ch, CURLOPT_FOLLOWLOCATION, !ini_get("safe_mode") && !ini_get("open_basedir"));
37 curl_setopt($ch, CURLOPT_USERAGENT, SELF_USER_AGENT);
38
39 if (defined('_CURL_HTTP_PROXY')) {
40 curl_setopt($ch, CURLOPT_PROXY, _CURL_HTTP_PROXY);
41 }
42
43 @curl_exec($ch);
44
45 $real_url = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL);
46
47 curl_close($ch);
48
49 if ($real_url) {
50 /* remove the rest of it */
51
52 $query = parse_url($real_url, PHP_URL_QUERY);
53
54 if ($query && strpos($query, "utm_source") !== FALSE) {
55 $args = array();
56 parse_str($query, $args);
57
58 foreach (array("utm_source", "utm_medium", "utm_campaign") as $param) {
59 if (isset($args[$param])) unset($args[$param]);
60 }
61
62 $new_query = http_build_query($args);
63
64 if ($new_query != $query) {
65 $real_url = str_replace("?$query", "?$new_query", $real_url);
66 }
67 }
68
69 $real_url = preg_replace("/\?$/", "", $real_url);
70
71 $article["plugin_data"] = "unburn,$owner_uid:" . $article["plugin_data"];
72 $article["link"] = $real_url;
73 }
74 }
75
76 return $article;
77 }
78
79 function geturl($url){
80
81 (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');
82
83 $curl = curl_init();
84 $header[0] = "Accept: text/xml,application/xml,application/xhtml+xml,";
85 $header[0] .= "text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5";
86 $header[] = "Cache-Control: max-age=0";
87 $header[] = "Connection: keep-alive";
88 $header[] = "Keep-Alive: 300";
89 $header[] = "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7";
90 $header[] = "Accept-Language: en-us,en;q=0.5";
91 $header[] = "Pragma: ";
92
93 curl_setopt($curl, CURLOPT_URL, $url);
94 curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 5.1; rv:5.0) Gecko/20100101 Firefox/5.0 Firefox/5.0');
95 curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
96 curl_setopt($curl, CURLOPT_HEADER, true);
97 curl_setopt($curl, CURLOPT_REFERER, $url);
98 curl_setopt($curl, CURLOPT_ENCODING, 'gzip,deflate');
99 curl_setopt($curl, CURLOPT_AUTOREFERER, true);
100 curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
101 //curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true); //CURLOPT_FOLLOWLOCATION Disabled...
102 curl_setopt($curl, CURLOPT_TIMEOUT, 60);
103
104 $html = curl_exec($curl);
105
106 $status = curl_getinfo($curl);
107 curl_close($curl);
108
109 if($status['http_code']!=200){
110 if($status['http_code'] == 301 || $status['http_code'] == 302) {
111 list($header) = explode("\r\n\r\n", $html, 2);
112 $matches = array();
113 preg_match("/(Location:|URI:)[^(\n)]*/", $header, $matches);
114 $url = trim(str_replace($matches[1],"",$matches[0]));
115 $url_parsed = parse_url($url);
116 return (isset($url_parsed))? geturl($url):'';
117 }
118 $oline='';
119 foreach($status as $key=>$eline){$oline.='['.$key.']'.$eline.' ';}
120 $line =$oline." \r\n ".$url."\r\n-----------------\r\n";
121 $handle = @fopen('./curl.error.log', 'a');
122 fwrite($handle, $line);
123 return FALSE;
124 }
125 return $url;
126 }
127
128 function api_version() {
129 return 2;
130 }
131
132 }
133 ?>