-
Notifications
You must be signed in to change notification settings - Fork 1
/
init.php
176 lines (156 loc) · 5.35 KB
/
init.php
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
<?php
class Tumblr_GDPR_UA extends Plugin
{
private $host;
public function about()
{
return array(
1.1,
"Fixes Tumblr feeds for GDPR compliance by masquerading as GoogleBot
(changing user agent). Requires curl.",
"hkockerbeck");
}
public function flags()
{
return array("needs_curl" => true);
}
public function api_version()
{
return 2;
}
public function init($host)
{
// store the provided reference to host
$this->host = $host;
// hook on some hooks ;)
if (function_exists("curl_init")) {
$host->add_hook($host::HOOK_SUBSCRIBE_FEED, $this);
$host->add_hook($host::HOOK_FEED_BASIC_INFO, $this);
$host->add_hook($host::HOOK_FETCH_FEED, $this);
$host->add_hook($host::HOOK_PREFS_TAB, $this);
}
}
// when subscribing to a new feed
public function hook_subscribe_feed(
$feed_data,
$fetch_url,
$auth_login,
$auth_pass
) {
// if the feed is hosted by Tumblr
if ($this->is_tumblr_domain($fetch_url)) {
// re-fetch the feed data with changed user agent
$feed_data = $this->fetch_contents($fetch_url, $auth_login, $auth_pass);
}
return $feed_data;
}
// get basic info about a feed (title and site url, mostly)
public function hook_feed_basic_info(
$basic_info,
$fetch_url,
$owner_uid,
$feed,
$auth_login,
$auth_pass
) {
// if the feed is hosted by Tumblr
if ($this->is_tumblr_domain($fetch_url)) {
// re-fetch the feed data with changed user agent
$contents = $this->fetch_contents($fetch_url, $auth_login, $auth_pass);
// extract info we need from the feed data
$parser = new FeedParser($contents);
$parser->init();
if (!$parser->error()) {
$basic_info = array(
'title' => mb_substr($parser->get_title(), 0, 199),
'site_url' => mb_substr(rewrite_relative_url($fetch_url, $parser->get_link()), 0, 245)
);
}
}
return $basic_info;
}
// fetch feed to look for new articles
public function hook_fetch_feed(
$feed_data,
$fetch_url,
$owner_uid,
$feed,
$last_article_timestamp,
$auth_login,
$auth_pass
) {
// if the feed is hosted by Tumblr
if ($this->is_tumblr_domain($fetch_url)) {
// re-fetch the feed data with changed user agent
$feed_data = $this->fetch_contents($fetch_url, $auth_login, $auth_pass);
}
return $feed_data;
}
// segment in TT-RSS' prefs to add additional domains
public function hook_prefs_tab($args)
{
if ($args != "prefPrefs") {
return;
}
// replacements in the template
$replacements = array(
'{title}' => 'Tumblr GDPR UA',
'{domainlist}' => implode(PHP_EOL, $this->host->get($this, 'tumblr_domains', array())). PHP_EOL,
'{user_agent}' => $this->host->get($this, 'user_agent'),
);
// set up a _very_ basic template engine
// so we don't have print out everything
$template = file_get_contents(__DIR__."/pref_template.html");
$template = str_replace(array_keys($replacements), array_values($replacements), $template);
print $template;
}
// save data from prefs segment
public function save()
{
$tumblr_domains = explode("\r\n", $_POST['tumblr_domains']);
$tumblr_domains = array_unique(array_filter($tumblr_domains));
$this->host->set($this, 'tumblr_domains', $tumblr_domains);
$user_agent = $_POST['user_agent'];
$this->host->set($this, 'user_agent', $user_agent);
}
// fetch feed data with changed user agent
private function fetch_contents(
$fetch_url,
$auth_login = false,
$auth_pass = false
) {
$options = array(
'url' => $fetch_url,
'login' => $auth_login,
'pass' => $auth_pass,
'useragent' => $this->user_agent());
return fetch_file_contents($options);
}
// helper function: does string $haystack end with string $needle?
private function ends_with($haystack, $needle)
{
return mb_substr($haystack, -mb_strlen($needle)) === $needle;
}
// is the domain in question on tumblr.com or one of the additional domains?
private function is_tumblr_domain($fetch_url)
{
// extract domain from whole url
$url = parse_url($fetch_url, PHP_URL_HOST);
// look through list of "known tumblr" urls
$domains = $this->host->get($this, 'tumblr_domains', array());
array_push($domains, 'tumblr.com');
$found = array_filter($domains, function ($t) use ($url) {
// does the domain in question end with a tumblr url?
return $this->ends_with($url, $t);
});
return !empty($found);
}
// if the user provided a custom user agent in the settings, use that
// otherwise, fall back to Googlebot
private function user_agent()
{
$fallback_ua = 'Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)';
$ua = $this->host->get($this, 'user_agent');
return ($ua == '') ? $fallback_ua : $ua;
}
}