-
Notifications
You must be signed in to change notification settings - Fork 85
Expand file tree
/
Copy pathclass-blocklist-subscriptions.php
More file actions
222 lines (186 loc) · 5.23 KB
/
class-blocklist-subscriptions.php
File metadata and controls
222 lines (186 loc) · 5.23 KB
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
<?php
/**
* Blocklist Subscriptions class file.
*
* @package Activitypub
*/
namespace Activitypub;
/**
* Blocklist Subscriptions class.
*
* Manages subscriptions to remote blocklists for automatic updates.
* Owns all remote blocklist logic: fetching, parsing, and importing.
*/
class Blocklist_Subscriptions {
/**
* Option key for storing subscriptions.
*/
const OPTION_KEY = 'activitypub_blocklist_subscriptions';
/**
* IFTAS DNI list URL.
*/
const IFTAS_DNI_URL = 'https://about.iftas.org/wp-content/uploads/2025/10/iftas-dni-latest.csv';
/**
* Get all subscriptions.
*
* @return array Array of URL => timestamp pairs.
*/
public static function get_all() {
return \get_option( self::OPTION_KEY, array() );
}
/**
* Add a subscription.
*
* Only adds the URL to the subscription list. Does not sync.
* Call sync() separately to fetch and import domains.
*
* @param string $url The blocklist URL to subscribe to.
* @return bool True on success, false on failure.
*/
public static function add( $url ) {
$url = \sanitize_url( $url );
if ( empty( $url ) || ! \filter_var( $url, FILTER_VALIDATE_URL ) ) {
return false;
}
$subscriptions = self::get_all();
// Not already subscribed.
if ( ! isset( $subscriptions[ $url ] ) ) {
// Add subscription with timestamp 0 (never synced).
$subscriptions[ $url ] = 0;
\update_option( self::OPTION_KEY, $subscriptions );
}
return true;
}
/**
* Remove a subscription.
*
* @param string $url The blocklist URL to unsubscribe from.
* @return bool True on success, false if not found.
*/
public static function remove( $url ) {
$subscriptions = self::get_all();
if ( ! isset( $subscriptions[ $url ] ) ) {
return false;
}
unset( $subscriptions[ $url ] );
\update_option( self::OPTION_KEY, $subscriptions );
return true;
}
/**
* Sync a single subscription.
*
* Fetches the blocklist URL, parses domains, and adds new ones to the blocklist.
* Updates the subscription timestamp on success.
*
* @param string $url The blocklist URL to sync.
* @return int|false Number of domains added, or false on failure.
*/
public static function sync( $url ) {
$response = \wp_safe_remote_get(
$url,
array(
'timeout' => 30,
'redirection' => 5,
)
);
if ( \is_wp_error( $response ) ) {
return false;
}
$response_code = \wp_remote_retrieve_response_code( $response );
if ( 200 !== $response_code ) {
return false;
}
$body = \wp_remote_retrieve_body( $response );
if ( empty( $body ) ) {
return false;
}
$domains = self::parse_csv_string( $body );
if ( empty( $domains ) ) {
return false;
}
// Get existing blocks and find new ones.
$existing = Moderation::get_site_blocks()[ Moderation::TYPE_DOMAIN ] ?? array();
$new_domains = \array_diff( $domains, $existing );
if ( ! empty( $new_domains ) ) {
Moderation::add_site_blocks( Moderation::TYPE_DOMAIN, $new_domains );
}
// Update timestamp if this is a subscription.
$subscriptions = self::get_all();
if ( isset( $subscriptions[ $url ] ) ) {
$subscriptions[ $url ] = \time();
\update_option( self::OPTION_KEY, $subscriptions );
}
return \count( $new_domains );
}
/**
* Sync all subscriptions.
*
* Called by cron job.
*/
public static function sync_all() {
\array_map( array( __CLASS__, 'sync' ), \array_keys( self::get_all() ) );
}
/**
* Parse CSV content from a string and extract domain names.
*
* Supports Mastodon CSV format (with #domain header) and simple
* one-domain-per-line format.
*
* @param string $content CSV content as a string.
* @return array Array of unique, valid domain names.
*/
public static function parse_csv_string( $content ) {
$domains = array();
if ( empty( $content ) ) {
return $domains;
}
// Split into lines.
$lines = \preg_split( '/\r\n|\r|\n/', $content );
if ( empty( $lines ) ) {
return $domains;
}
// Parse first line to detect format.
$first_line = \str_getcsv( $lines[0] );
$first_cell = \trim( $first_line[0] ?? '' );
$has_header = \str_starts_with( $first_cell, '#' ) || 'domain' === \strtolower( $first_cell );
// Find domain column index.
$domain_index = 0;
if ( $has_header ) {
foreach ( $first_line as $i => $col ) {
$col = \ltrim( \strtolower( \trim( $col ) ), '#' );
if ( 'domain' === $col ) {
$domain_index = $i;
break;
}
}
// Remove header from lines.
\array_shift( $lines );
}
// Process each line.
foreach ( $lines as $line ) {
$row = \str_getcsv( $line );
$domain = \trim( $row[ $domain_index ] ?? '' );
// Skip empty lines and comments.
if ( empty( $domain ) || \str_starts_with( $domain, '#' ) ) {
continue;
}
if ( self::is_valid_domain( $domain ) ) {
$domains[] = \strtolower( $domain );
}
}
return \array_unique( $domains );
}
/**
* Validate a domain name.
*
* @param string $domain The domain to validate.
* @return bool True if valid, false otherwise.
*/
public static function is_valid_domain( $domain ) {
// Must contain at least one dot (filter_var would accept "localhost").
if ( ! \str_contains( $domain, '.' ) ) {
return false;
}
return (bool) \filter_var( $domain, FILTER_VALIDATE_DOMAIN, FILTER_FLAG_HOSTNAME );
}
}