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