From 0d48496768faa662538af290f1daae7d919ded7b Mon Sep 17 00:00:00 2001 From: Matthias Pfefferle Date: Fri, 21 Feb 2020 11:09:31 +0100 Subject: [PATCH] add blacklist settings --- includes/class-admin.php | 8 +++++++ includes/functions.php | 47 ++++++++++++++++++++++++++++++++++++++++ templates/settings.php | 25 +++++++++++++++++++++ 3 files changed, 80 insertions(+) diff --git a/includes/class-admin.php b/includes/class-admin.php index 866f873..0dca896 100644 --- a/includes/class-admin.php +++ b/includes/class-admin.php @@ -106,6 +106,14 @@ class Admin { 'default' => array( 'post', 'pages' ), ) ); + \register_setting( + 'activitypub', 'activitypub_blacklist', array( + 'type' => 'string', + 'description' => \esc_html__( 'Block fediverse instances', 'activitypub' ), + 'show_in_rest' => true, + 'default' => 'gab.com', + ) + ); } public static function add_settings_help_tab() { diff --git a/includes/functions.php b/includes/functions.php index 2cd3607..d1b7991 100644 --- a/includes/functions.php +++ b/includes/functions.php @@ -287,3 +287,50 @@ function url_to_authorid( $url ) { return 0; } + +/** + * Get the blacklist from the WordPress options table + * + * @return array the list of blacklisted hosts + * + * @uses apply_filters() Calls 'activitypub_blacklist' filter + */ +function get_blacklist() { + $blacklist = \get_option( 'activitypub_blacklist' ); + $blacklist_hosts = \explode( PHP_EOL, $blacklist ); + + // if no values have been set, revert to the defaults + if ( ! $blacklist || ! $blacklist_hosts || ! is_array( $blacklist_hosts ) ) { + $blacklist_hosts = array( + 'gab.com', + ); + } + + // clean out any blank values + foreach ( $blacklist_hosts as $key => $value ) { + if ( empty( $value ) ) { + unset( $blacklist_hosts[ $key ] ); + } else { + $blacklist_hosts[ $key ] = \trim( $blacklist_hosts[ $key ] ); + } + } + + return \apply_filters( 'activitypub_blacklist', $blacklist_hosts ); +} + +/** + * Check if an URL is blacklisted + * + * @param string $url an URL to check + * + * @return boolean + */ +function is_blacklisted( $url ) { + foreach ( \ActivityPub\get_blacklist() as $blacklisted_host ) { + if ( \strpos( $url, $blacklisted_host ) !== false ) { + return true; + } + } + + return false; +} diff --git a/templates/settings.php b/templates/settings.php index d3d69ea..ff1d0c7 100644 --- a/templates/settings.php +++ b/templates/settings.php @@ -90,6 +90,31 @@ +

+ +

+ + + + + + + + + + +
+ + + +

http:// and without www.. For example example.com.', 'activitypub' ); ?>

+
+ + +