| @@ -1,13 +1,7 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | namespace Activitypub; |
| 3 | 3 | |
| 4 | -use WP_Error; | |
| 5 | -use Activitypub\Http; | |
| 6 | -use Activitypub\Activity\Activity; | |
| 7 | -use Activitypub\Collection\Followers; | |
| 8 | -use Activitypub\Collection\Users; | |
| 9 | - | |
| 10 | 4 | /** |
| 11 | 5 | * Returns the ActivityPub default JSON-context |
| 12 | 6 | * |
| 13 | 7 | * @return array the activitypub context |
| @@ -12,538 +6,232 @@ | ||
| 12 | 6 | * |
| 13 | 7 | * @return array the activitypub context |
| 14 | 8 | */ |
| 15 | 9 | function get_context() { |
| 16 | - $context = Activity::CONTEXT; | |
| 10 | + $context = array( | |
| 11 | + 'https://www.w3.org/ns/activitystreams', | |
| 12 | + 'https://w3id.org/security/v1', | |
| 13 | + array( | |
| 14 | + 'manuallyApprovesFollowers' => 'as:manuallyApprovesFollowers', | |
| 15 | + 'sensitive' => 'as:sensitive', | |
| 16 | + 'movedTo' => array( | |
| 17 | + '@id' => 'as:movedTo', | |
| 18 | + '@type' => '@id', | |
| 19 | + ), | |
| 20 | + 'Hashtag' => 'as:Hashtag', | |
| 21 | + 'ostatus' => 'http://ostatus.org#', | |
| 22 | + 'atomUri' => 'ostatus:atomUri', | |
| 23 | + 'inReplyToAtomUri' => 'ostatus:inReplyToAtomUri', | |
| 24 | + 'conversation' => 'ostatus:conversation', | |
| 25 | + 'toot' => 'http://joinmastodon.org/ns#', | |
| 26 | + 'Emoji' => 'toot:Emoji', | |
| 27 | + 'focalPoint' => array( | |
| 28 | + '@container' => '@list', | |
| 29 | + '@id' => 'toot:focalPoint', | |
| 30 | + ), | |
| 31 | + 'featured' => array( | |
| 32 | + '@id' => 'toot:featured', | |
| 33 | + '@type' => '@id', | |
| 34 | + ), | |
| 35 | + 'schema' => 'http://schema.org#', | |
| 36 | + 'PropertyValue' => 'schema:PropertyValue', | |
| 37 | + 'value' => 'schema:value', | |
| 38 | + ), | |
| 39 | + ); | |
| 17 | 40 | |
| 18 | - return \apply_filters( 'activitypub_json_context', $context ); | |
| 41 | + return apply_filters( 'activitypub_json_context', $context ); | |
| 19 | 42 | } |
| 20 | 43 | |
| 21 | 44 | function safe_remote_post( $url, $body, $user_id ) { |
| 22 | - return Http::post( $url, $body, $user_id ); | |
| 23 | -} | |
| 45 | + $date = gmdate( 'D, d M Y H:i:s T' ); | |
| 46 | + $signature = \Activitypub\Signature::generate_signature( $user_id, $url, $date ); | |
| 24 | 47 | |
| 25 | -function safe_remote_get( $url ) { | |
| 26 | - return Http::get( $url ); | |
| 48 | + $wp_version = get_bloginfo( 'version' ); | |
| 49 | + $user_agent = apply_filters( 'http_headers_useragent', 'WordPress/' . $wp_version . '; ' . get_bloginfo( 'url' ) ); | |
| 50 | + $args = array( | |
| 51 | + 'timeout' => 100, | |
| 52 | + 'limit_response_size' => 1048576, | |
| 53 | + 'redirection' => 3, | |
| 54 | + 'user-agent' => "$user_agent; ActivityPub", | |
| 55 | + 'headers' => array( | |
| 56 | + 'Accept' => 'application/activity+json', | |
| 57 | + 'Content-Type' => 'application/activity+json', | |
| 58 | + 'Signature' => $signature, | |
| 59 | + 'Date' => $date, | |
| 60 | + ), | |
| 61 | + 'body' => $body, | |
| 62 | + ); | |
| 63 | + | |
| 64 | + $response = wp_safe_remote_post( $url, $args ); | |
| 65 | + | |
| 66 | + do_action( 'activitypub_safe_remote_post_response', $response, $url, $body, $user_id ); | |
| 67 | + | |
| 68 | + return $response; | |
| 27 | 69 | } |
| 28 | 70 | |
| 29 | 71 | /** |
| 30 | 72 | * Returns a users WebFinger "resource" |
| 31 | 73 | * |
| 32 | - * @param int $user_id The User-ID. | |
| 74 | + * @param int $user_id | |
| 33 | 75 | * |
| 34 | - * @return string The User-Resource. | |
| 76 | + * @return string The user-resource | |
| 35 | 77 | */ |
| 36 | 78 | function get_webfinger_resource( $user_id ) { |
| 37 | - return Webfinger::get_user_resource( $user_id ); | |
| 79 | + // use WebFinger plugin if installed | |
| 80 | + if ( function_exists( '\get_webfinger_resource' ) ) { | |
| 81 | + return \get_webfinger_resource( $user_id, false ); | |
| 82 | + } | |
| 83 | + | |
| 84 | + $user = get_user_by( 'id', $user_id ); | |
| 85 | + | |
| 86 | + return $user->user_login . '@' . wp_parse_url( home_url(), PHP_URL_HOST ); | |
| 38 | 87 | } |
| 39 | 88 | |
| 40 | 89 | /** |
| 41 | - * Requests the Meta-Data from the Actors profile | |
| 90 | + * [get_metadata_by_actor description] | |
| 42 | 91 | * |
| 43 | - * @param string $actor The Actor URL. | |
| 44 | - * @param bool $cached If the result should be cached. | |
| 92 | + * @param sting $actor | |
| 45 | 93 | * |
| 46 | - * @return array|WP_Error The Actor profile as array or WP_Error on failure. | |
| 94 | + * @return array | |
| 47 | 95 | */ |
| 48 | -function get_remote_metadata_by_actor( $actor, $cached = true ) { | |
| 49 | - $pre = apply_filters( 'pre_get_remote_metadata_by_actor', false, $actor ); | |
| 50 | - if ( $pre ) { | |
| 51 | - return $pre; | |
| 52 | - } | |
| 53 | - if ( preg_match( '/^@?' . ACTIVITYPUB_USERNAME_REGEXP . '$/i', $actor ) ) { | |
| 54 | - $actor = Webfinger::resolve( $actor ); | |
| 55 | - } | |
| 96 | +function get_remote_metadata_by_actor( $actor ) { | |
| 97 | + $metadata = get_transient( 'activitypub_' . $actor ); | |
| 56 | 98 | |
| 57 | - if ( ! $actor ) { | |
| 58 | - return new WP_Error( 'activitypub_no_valid_actor_identifier', \__( 'The "actor" identifier is not valid', 'activitypub' ), array( 'status' => 404, 'actor' => $actor ) ); | |
| 99 | + if ( $metadata ) { | |
| 100 | + return $metadata; | |
| 59 | 101 | } |
| 60 | 102 | |
| 61 | - if ( is_wp_error( $actor ) ) { | |
| 62 | - return $actor; | |
| 103 | + if ( ! wp_http_validate_url( $actor ) ) { | |
| 104 | + return new \WP_Error( 'activitypub_no_valid_actor_url', __( 'The "actor" is no valid URL', 'activitypub' ), $actor ); | |
| 63 | 105 | } |
| 64 | 106 | |
| 65 | - $transient_key = 'activitypub_' . $actor; | |
| 107 | + $wp_version = get_bloginfo( 'version' ); | |
| 66 | 108 | |
| 67 | - // only check the cache if needed. | |
| 68 | - if ( $cached ) { | |
| 69 | - $metadata = \get_transient( $transient_key ); | |
| 109 | + $user_agent = apply_filters( 'http_headers_useragent', 'WordPress/' . $wp_version . '; ' . get_bloginfo( 'url' ) ); | |
| 110 | + $args = array( | |
| 111 | + 'timeout' => 100, | |
| 112 | + 'limit_response_size' => 1048576, | |
| 113 | + 'redirection' => 3, | |
| 114 | + 'user-agent' => "$user_agent; ActivityPub", | |
| 115 | + 'headers' => array( 'accept' => 'application/activity+json' ), | |
| 116 | + ); | |
| 70 | 117 | |
| 71 | - if ( $metadata ) { | |
| 72 | - return $metadata; | |
| 73 | - } | |
| 74 | - } | |
| 118 | + $response = wp_safe_remote_get( $actor, $args ); | |
| 75 | 119 | |
| 76 | - if ( ! \wp_http_validate_url( $actor ) ) { | |
| 77 | - $metadata = new WP_Error( 'activitypub_no_valid_actor_url', \__( 'The "actor" is no valid URL', 'activitypub' ), array( 'status' => 400, 'actor' => $actor ) ); | |
| 78 | - return $metadata; | |
| 79 | - } | |
| 80 | - | |
| 81 | - $response = Http::get( $actor ); | |
| 82 | - | |
| 83 | - if ( \is_wp_error( $response ) ) { | |
| 120 | + if ( is_wp_error( $response ) ) { | |
| 84 | 121 | return $response; |
| 85 | 122 | } |
| 86 | 123 | |
| 87 | - $metadata = \wp_remote_retrieve_body( $response ); | |
| 88 | - $metadata = \json_decode( $metadata, true ); | |
| 124 | + $metadata = wp_remote_retrieve_body( $response ); | |
| 125 | + $metadata = json_decode( $metadata, true ); | |
| 89 | 126 | |
| 90 | 127 | if ( ! $metadata ) { |
| 91 | - $metadata = new WP_Error( 'activitypub_invalid_json', \__( 'No valid JSON data', 'activitypub' ), array( 'status' => 400, 'actor' => $actor ) ); | |
| 92 | - return $metadata; | |
| 128 | + return new \WP_Error( 'activitypub_invalid_json', __( 'No valid JSON data', 'activitypub' ), $actor ); | |
| 93 | 129 | } |
| 94 | 130 | |
| 95 | - \set_transient( $transient_key, $metadata, WEEK_IN_SECONDS ); | |
| 131 | + set_transient( 'activitypub_' . $actor, $metadata, WEEK_IN_SECONDS ); | |
| 96 | 132 | |
| 97 | 133 | return $metadata; |
| 98 | 134 | } |
| 99 | 135 | |
| 100 | 136 | /** |
| 101 | - * Returns the followers of a given user. | |
| 102 | - * | |
| 103 | - * @param int $user_id The User-ID. | |
| 104 | - * | |
| 105 | - * @return array The followers. | |
| 137 | + * [get_inbox_by_actor description] | |
| 138 | + * @param [type] $actor [description] | |
| 139 | + * @return [type] [description] | |
| 106 | 140 | */ |
| 107 | -function get_followers( $user_id ) { | |
| 108 | - return Followers::get_followers( $user_id ); | |
| 109 | -} | |
| 141 | +function get_inbox_by_actor( $actor ) { | |
| 142 | + $metadata = \Activitypub\get_remote_metadata_by_actor( $actor ); | |
| 110 | 143 | |
| 111 | -/** | |
| 112 | - * Count the number of followers for a given user. | |
| 113 | - * | |
| 114 | - * @param int $user_id The User-ID. | |
| 115 | - * | |
| 116 | - * @return int The number of followers. | |
| 117 | - */ | |
| 118 | -function count_followers( $user_id ) { | |
| 119 | - return Followers::count_followers( $user_id ); | |
| 120 | -} | |
| 121 | - | |
| 122 | -/** | |
| 123 | - * Examine a url and try to determine the author ID it represents. | |
| 124 | - * | |
| 125 | - * Checks are supposedly from the hosted site blog. | |
| 126 | - * | |
| 127 | - * @param string $url Permalink to check. | |
| 128 | - * | |
| 129 | - * @return int User ID, or 0 on failure. | |
| 130 | - */ | |
| 131 | -function url_to_authorid( $url ) { | |
| 132 | - global $wp_rewrite; | |
| 133 | - | |
| 134 | - // check if url hase the same host | |
| 135 | - if ( \wp_parse_url( \site_url(), \PHP_URL_HOST ) !== \wp_parse_url( $url, \PHP_URL_HOST ) ) { | |
| 136 | - return 0; | |
| 144 | + if ( is_wp_error( $metadata ) ) { | |
| 145 | + return $metadata; | |
| 137 | 146 | } |
| 138 | 147 | |
| 139 | - // first, check to see if there is a 'author=N' to match against | |
| 140 | - if ( \preg_match( '/[?&]author=(\d+)/i', $url, $values ) ) { | |
| 141 | - $id = \absint( $values[1] ); | |
| 142 | - if ( $id ) { | |
| 143 | - return $id; | |
| 144 | - } | |
| 148 | + if ( isset( $metadata['endpoints'] ) && isset( $metadata['endpoints']['sharedInbox'] ) ) { | |
| 149 | + return $metadata['endpoints']['sharedInbox']; | |
| 145 | 150 | } |
| 146 | 151 | |
| 147 | - // check to see if we are using rewrite rules | |
| 148 | - $rewrite = $wp_rewrite->wp_rewrite_rules(); | |
| 149 | - | |
| 150 | - // not using rewrite rules, and 'author=N' method failed, so we're out of options | |
| 151 | - if ( empty( $rewrite ) ) { | |
| 152 | - return 0; | |
| 152 | + if ( array_key_exists( 'inbox', $metadata ) ) { | |
| 153 | + return $metadata['inbox']; | |
| 153 | 154 | } |
| 154 | 155 | |
| 155 | - // generate rewrite rule for the author url | |
| 156 | - $author_rewrite = $wp_rewrite->get_author_permastruct(); | |
| 157 | - $author_regexp = \str_replace( '%author%', '', $author_rewrite ); | |
| 158 | - | |
| 159 | - // match the rewrite rule with the passed url | |
| 160 | - if ( \preg_match( '/https?:\/\/(.+)' . \preg_quote( $author_regexp, '/' ) . '([^\/]+)/i', $url, $match ) ) { | |
| 161 | - $user = \get_user_by( 'slug', $match[2] ); | |
| 162 | - if ( $user ) { | |
| 163 | - return $user->ID; | |
| 164 | - } | |
| 165 | - } | |
| 166 | - | |
| 167 | - return 0; | |
| 156 | + return new \WP_Error( 'activitypub_no_inbox', __( 'No "Inbox" found', 'activitypub' ), $metadata ); | |
| 168 | 157 | } |
| 169 | 158 | |
| 170 | 159 | /** |
| 171 | - * Check for Tombstone Objects | |
| 172 | - * | |
| 173 | - * @see https://www.w3.org/TR/activitypub/#delete-activity-outbox | |
| 174 | - * | |
| 175 | - * @param WP_Error $wp_error A WP_Error-Response of an HTTP-Request | |
| 176 | - * | |
| 177 | - * @return boolean true if HTTP-Code is 410 or 404 | |
| 160 | + * [get_inbox_by_actor description] | |
| 161 | + * @param [type] $actor [description] | |
| 162 | + * @return [type] [description] | |
| 178 | 163 | */ |
| 179 | -function is_tombstone( $wp_error ) { | |
| 180 | - if ( ! is_wp_error( $wp_error ) ) { | |
| 181 | - return false; | |
| 182 | - } | |
| 164 | +function get_publickey_by_actor( $actor, $key_id ) { | |
| 165 | + $metadata = \Activitypub\get_remote_metadata_by_actor( $actor ); | |
| 183 | 166 | |
| 184 | - if ( in_array( (int) $wp_error->get_error_code(), array( 404, 410 ), true ) ) { | |
| 185 | - return true; | |
| 167 | + if ( is_wp_error( $metadata ) ) { | |
| 168 | + return $metadata; | |
| 186 | 169 | } |
| 187 | 170 | |
| 188 | - return false; | |
| 189 | -} | |
| 190 | - | |
| 191 | -/** | |
| 192 | - * Get the REST URL relative to this plugin's namespace. | |
| 193 | - * | |
| 194 | - * @param string $path Optional. REST route path. Otherwise this plugin's namespaced root. | |
| 195 | - * | |
| 196 | - * @return string REST URL relative to this plugin's namespace. | |
| 197 | - */ | |
| 198 | -function get_rest_url_by_path( $path = '' ) { | |
| 199 | - // we'll handle the leading slash. | |
| 200 | - $path = ltrim( $path, '/' ); | |
| 201 | - $namespaced_path = sprintf( '/%s/%s', ACTIVITYPUB_REST_NAMESPACE, $path ); | |
| 202 | - return \get_rest_url( null, $namespaced_path ); | |
| 203 | -} | |
| 204 | - | |
| 205 | -/** | |
| 206 | - * Convert a string from camelCase to snake_case. | |
| 207 | - * | |
| 208 | - * @param string $string The string to convert. | |
| 209 | - * | |
| 210 | - * @return string The converted string. | |
| 211 | - */ | |
| 212 | -// phpcs:ignore Universal.NamingConventions.NoReservedKeywordParameterNames.stringFound | |
| 213 | -function camel_to_snake_case( $string ) { | |
| 214 | - return strtolower( preg_replace( '/(?<!^)[A-Z]/', '_$0', $string ) ); | |
| 215 | -} | |
| 216 | - | |
| 217 | -/** | |
| 218 | - * Convert a string from snake_case to camelCase. | |
| 219 | - * | |
| 220 | - * @param string $string The string to convert. | |
| 221 | - * | |
| 222 | - * @return string The converted string. | |
| 223 | - */ | |
| 224 | -// phpcs:ignore Universal.NamingConventions.NoReservedKeywordParameterNames.stringFound | |
| 225 | -function snake_to_camel_case( $string ) { | |
| 226 | - return lcfirst( str_replace( '_', '', ucwords( $string, '_' ) ) ); | |
| 227 | -} | |
| 228 | - | |
| 229 | -/** | |
| 230 | - * Escapes a Tag, to be used as a hashtag. | |
| 231 | - * | |
| 232 | - * @param string $string The string to escape. | |
| 233 | - * | |
| 234 | - * @return string The escaped hastag. | |
| 235 | - */ | |
| 236 | -function esc_hashtag( $string ) { | |
| 237 | - | |
| 238 | - $hashtag = \wp_specialchars_decode( $string, ENT_QUOTES ); | |
| 239 | - // Remove all characters that are not letters, numbers, or underscores. | |
| 240 | - $hashtag = \preg_replace( '/emoji-regex(*SKIP)(?!)|[^\p{L}\p{Nd}_]+/u', '_', $hashtag ); | |
| 241 | - | |
| 242 | - // Capitalize every letter that is preceded by an underscore. | |
| 243 | - $hashtag = preg_replace_callback( | |
| 244 | - '/_(.)/', | |
| 245 | - function ( $matches ) { | |
| 246 | - return '' . strtoupper( $matches[1] ); | |
| 247 | - }, | |
| 248 | - $hashtag | |
| 249 | - ); | |
| 250 | - | |
| 251 | - // Add a hashtag to the beginning of the string. | |
| 252 | - $hashtag = ltrim( $hashtag, '#' ); | |
| 253 | - $hashtag = '#' . $hashtag; | |
| 254 | - | |
| 255 | - /** | |
| 256 | - * Allow defining your own custom hashtag generation rules. | |
| 257 | - * | |
| 258 | - * @param string $hashtag The hashtag to be returned. | |
| 259 | - * @param string $string The original string. | |
| 260 | - */ | |
| 261 | - $hashtag = apply_filters( 'activitypub_esc_hashtag', $hashtag, $string ); | |
| 262 | - | |
| 263 | - return esc_html( $hashtag ); | |
| 264 | -} | |
| 265 | - | |
| 266 | -/** | |
| 267 | - * Check if a request is for an ActivityPub request. | |
| 268 | - * | |
| 269 | - * @return bool False by default. | |
| 270 | - */ | |
| 271 | -function is_activitypub_request() { | |
| 272 | - global $wp_query; | |
| 273 | - | |
| 274 | - /* | |
| 275 | - * ActivityPub requests are currently only made for | |
| 276 | - * author archives, singular posts, and the homepage. | |
| 277 | - */ | |
| 278 | - if ( ! \is_author() && ! \is_singular() && ! \is_home() && ! defined( '\REST_REQUEST' ) ) { | |
| 279 | - return false; | |
| 280 | - } | |
| 281 | - | |
| 282 | - // One can trigger an ActivityPub request by adding ?activitypub to the URL. | |
| 283 | - // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.VariableRedeclaration | |
| 284 | - global $wp_query; | |
| 285 | - if ( isset( $wp_query->query_vars['activitypub'] ) ) { | |
| 286 | - return true; | |
| 287 | - } | |
| 288 | - | |
| 289 | - /* | |
| 290 | - * The other (more common) option to make an ActivityPub request | |
| 291 | - * is to send an Accept header. | |
| 292 | - */ | |
| 293 | - if ( isset( $_SERVER['HTTP_ACCEPT'] ) ) { | |
| 294 | - $accept = sanitize_text_field( wp_unslash( $_SERVER['HTTP_ACCEPT'] ) ); | |
| 295 | - | |
| 296 | - /* | |
| 297 | - * $accept can be a single value, or a comma separated list of values. | |
| 298 | - * We want to support both scenarios, | |
| 299 | - * and return true when the header includes at least one of the following: | |
| 300 | - * - application/activity+json | |
| 301 | - * - application/ld+json | |
| 302 | - * - application/json | |
| 303 | - */ | |
| 304 | - if ( preg_match( '/(application\/(ld\+json|activity\+json|json))/i', $accept ) ) { | |
| 305 | - return true; | |
| 306 | - } | |
| 307 | - } | |
| 308 | - | |
| 309 | - return false; | |
| 310 | -} | |
| 311 | - | |
| 312 | -/** | |
| 313 | - * This function checks if a user is disabled for ActivityPub. | |
| 314 | - * | |
| 315 | - * @param int $user_id The User-ID. | |
| 316 | - * | |
| 317 | - * @return boolean True if the user is disabled, false otherwise. | |
| 318 | - */ | |
| 319 | -function is_user_disabled( $user_id ) { | |
| 320 | - $return = false; | |
| 321 | - | |
| 322 | - switch ( $user_id ) { | |
| 323 | - // if the user is the application user, it's always enabled. | |
| 324 | - case \Activitypub\Collection\Users::APPLICATION_USER_ID: | |
| 325 | - $return = false; | |
| 326 | - break; | |
| 327 | - // if the user is the blog user, it's only enabled in single-user mode. | |
| 328 | - case \Activitypub\Collection\Users::BLOG_USER_ID: | |
| 329 | - if ( is_user_type_disabled( 'blog' ) ) { | |
| 330 | - $return = true; | |
| 331 | - break; | |
| 332 | - } | |
| 333 | - | |
| 334 | - $return = false; | |
| 335 | - break; | |
| 336 | - // if the user is any other user, it's enabled if it can publish posts. | |
| 337 | - default: | |
| 338 | - if ( ! \get_user_by( 'id', $user_id ) ) { | |
| 339 | - $return = true; | |
| 340 | - break; | |
| 341 | - } | |
| 342 | - | |
| 343 | - if ( is_user_type_disabled( 'user' ) ) { | |
| 344 | - $return = true; | |
| 345 | - break; | |
| 346 | - } | |
| 347 | - | |
| 348 | - if ( ! \user_can( $user_id, 'publish_posts' ) ) { | |
| 349 | - $return = true; | |
| 350 | - break; | |
| 351 | - } | |
| 352 | - | |
| 353 | - $return = false; | |
| 354 | - break; | |
| 355 | - } | |
| 356 | - | |
| 357 | - return apply_filters( 'activitypub_is_user_disabled', $return, $user_id ); | |
| 358 | -} | |
| 359 | - | |
| 360 | -/** | |
| 361 | - * Checks if a User-Type is disabled for ActivityPub. | |
| 362 | - * | |
| 363 | - * This function is used to check if the 'blog' or 'user' | |
| 364 | - * type is disabled for ActivityPub. | |
| 365 | - * | |
| 366 | - * @param enum $type Can be 'blog' or 'user'. | |
| 367 | - * | |
| 368 | - * @return boolean True if the user type is disabled, false otherwise. | |
| 369 | - */ | |
| 370 | -function is_user_type_disabled( $type ) { | |
| 371 | - switch ( $type ) { | |
| 372 | - case 'blog': | |
| 373 | - if ( \defined( 'ACTIVITYPUB_SINGLE_USER_MODE' ) ) { | |
| 374 | - if ( ACTIVITYPUB_SINGLE_USER_MODE ) { | |
| 375 | - $return = false; | |
| 376 | - break; | |
| 377 | - } | |
| 378 | - } | |
| 379 | - | |
| 380 | - if ( \defined( 'ACTIVITYPUB_DISABLE_BLOG_USER' ) ) { | |
| 381 | - $return = ACTIVITYPUB_DISABLE_BLOG_USER; | |
| 382 | - break; | |
| 383 | - } | |
| 384 | - | |
| 385 | - if ( '1' !== \get_option( 'activitypub_enable_blog_user', '0' ) ) { | |
| 386 | - $return = true; | |
| 387 | - break; | |
| 388 | - } | |
| 389 | - | |
| 390 | - $return = false; | |
| 391 | - break; | |
| 392 | - case 'user': | |
| 393 | - if ( \defined( 'ACTIVITYPUB_SINGLE_USER_MODE' ) ) { | |
| 394 | - if ( ACTIVITYPUB_SINGLE_USER_MODE ) { | |
| 395 | - $return = true; | |
| 396 | - break; | |
| 397 | - } | |
| 398 | - } | |
| 399 | - | |
| 400 | - if ( \defined( 'ACTIVITYPUB_DISABLE_USER' ) ) { | |
| 401 | - $return = ACTIVITYPUB_DISABLE_USER; | |
| 402 | - break; | |
| 403 | - } | |
| 404 | - | |
| 405 | - if ( '1' !== \get_option( 'activitypub_enable_users', '1' ) ) { | |
| 406 | - $return = true; | |
| 407 | - break; | |
| 408 | - } | |
| 409 | - | |
| 410 | - $return = false; | |
| 411 | - break; | |
| 412 | - default: | |
| 413 | - $return = new WP_Error( 'activitypub_wrong_user_type', __( 'Wrong user type', 'activitypub' ), array( 'status' => 400 ) ); | |
| 414 | - break; | |
| 415 | - } | |
| 416 | - | |
| 417 | - return apply_filters( 'activitypub_is_user_type_disabled', $return, $type ); | |
| 418 | -} | |
| 419 | - | |
| 420 | -/** | |
| 421 | - * Check if the blog is in single-user mode. | |
| 422 | - * | |
| 423 | - * @return boolean True if the blog is in single-user mode, false otherwise. | |
| 424 | - */ | |
| 425 | -function is_single_user() { | |
| 426 | 171 | if ( |
| 427 | - false === is_user_type_disabled( 'blog' ) && | |
| 428 | - true === is_user_type_disabled( 'user' ) | |
| 172 | + isset( $metadata['publicKey'] ) && | |
| 173 | + isset( $metadata['publicKey']['id'] ) && | |
| 174 | + isset( $metadata['publicKey']['owner'] ) && | |
| 175 | + isset( $metadata['publicKey']['publicKeyPem'] ) && | |
| 176 | + $key_id === $metadata['publicKey']['id'] && | |
| 177 | + $actor === $metadata['publicKey']['owner'] | |
| 429 | 178 | ) { |
| 430 | - return true; | |
| 179 | + return $metadata['publicKey']['publicKeyPem']; | |
| 431 | 180 | } |
| 432 | 181 | |
| 433 | - return false; | |
| 182 | + return new \WP_Error( 'activitypub_no_public_key', __( 'No "Public-Key" found', 'activitypub' ), $metadata ); | |
| 434 | 183 | } |
| 435 | 184 | |
| 436 | -/** | |
| 437 | - * Check if a site supports the block editor. | |
| 438 | - * | |
| 439 | - * @return boolean True if the site supports the block editor, false otherwise. | |
| 440 | - */ | |
| 441 | -function site_supports_blocks() { | |
| 442 | - if ( \version_compare( \get_bloginfo( 'version' ), '5.9', '<' ) ) { | |
| 443 | - return false; | |
| 444 | - } | |
| 185 | +function get_follower_inboxes( $user_id ) { | |
| 186 | + $followers = \Activitypub\Db\Followers::get_followers( $user_id ); | |
| 187 | + $inboxes = array(); | |
| 445 | 188 | |
| 446 | - if ( ! \function_exists( 'register_block_type_from_metadata' ) ) { | |
| 447 | - return false; | |
| 189 | + foreach ( $followers as $follower ) { | |
| 190 | + $inbox = \Activitypub\get_inbox_by_actor( $follower ); | |
| 191 | + if ( ! $inbox ) { | |
| 192 | + continue; | |
| 193 | + } | |
| 194 | + // init array if empty | |
| 195 | + if ( ! isset( $inboxes[ $inbox ] ) ) { | |
| 196 | + $inboxes[ $inbox ] = array(); | |
| 197 | + } | |
| 198 | + $inboxes[ $inbox ][] = $follower; | |
| 448 | 199 | } |
| 449 | 200 | |
| 450 | - /** | |
| 451 | - * Allow plugins to disable block editor support, | |
| 452 | - * thus disabling blocks registered by the ActivityPub plugin. | |
| 453 | - * | |
| 454 | - * @param boolean $supports_blocks True if the site supports the block editor, false otherwise. | |
| 455 | - */ | |
| 456 | - return apply_filters( 'activitypub_site_supports_blocks', true ); | |
| 201 | + return $inboxes; | |
| 457 | 202 | } |
| 458 | 203 | |
| 459 | -/** | |
| 460 | - * Check if data is valid JSON. | |
| 461 | - * | |
| 462 | - * @param string $data The data to check. | |
| 463 | - * | |
| 464 | - * @return boolean True if the data is JSON, false otherwise. | |
| 465 | - */ | |
| 466 | -function is_json( $data ) { | |
| 467 | - return \is_array( \json_decode( $data, true ) ) ? true : false; | |
| 204 | +function get_identifier_settings( $user_id ) { | |
| 205 | + ?> | |
| 206 | +<table class="form-table"> | |
| 207 | + <tbody> | |
| 208 | + <tr> | |
| 209 | + <th scope="row"> | |
| 210 | + <label><?php esc_html_e( 'Profile identifier', 'activitypub' ); ?></label> | |
| 211 | + </th> | |
| 212 | + <td> | |
| 213 | + <p><code><?php echo esc_html( \Activitypub\get_webfinger_resource( $user_id ) ); ?></code> or <code><?php echo esc_url( get_author_posts_url( $user_id ) ); ?></code></p> | |
| 214 | + <?php // translators: the webfinger resource ?> | |
| 215 | + <p class="description"><?php printf( esc_html__( 'Try to follow "@%s" in the Mastodon/Friendica search field.', 'activitypub' ), esc_html( \Activitypub\get_webfinger_resource( $user_id ) ) ); ?></p> | |
| 216 | + </td> | |
| 217 | + </tr> | |
| 218 | + </tbody> | |
| 219 | +</table> | |
| 220 | + <?php | |
| 468 | 221 | } |
| 469 | 222 | |
| 470 | -/** | |
| 471 | - * Check if a blog is public based on the `blog_public` option | |
| 472 | - * | |
| 473 | - * @return bollean True if public, false if not | |
| 474 | - */ | |
| 475 | -function is_blog_public() { | |
| 476 | - return (bool) apply_filters( 'activitypub_is_blog_public', \get_option( 'blog_public', 1 ) ); | |
| 477 | -} | |
| 223 | +function get_followers( $user_id ) { | |
| 224 | + $followers = \Activitypub\Db\Followers::get_followers( $user_id ); | |
| 478 | 225 | |
| 479 | -/** | |
| 480 | - * Get active users based on a given duration | |
| 481 | - * | |
| 482 | - * @param int $duration The duration to check in month(s) | |
| 483 | - * | |
| 484 | - * @return int The number of active users | |
| 485 | - */ | |
| 486 | -function get_active_users( $duration = 1 ) { | |
| 487 | - | |
| 488 | - $duration = intval( $duration ); | |
| 489 | - $transient_key = sprintf( 'monthly_active_users_%d', $duration ); | |
| 490 | - $count = get_transient( $transient_key ); | |
| 491 | - | |
| 492 | - if ( false === $count ) { | |
| 493 | - global $wpdb; | |
| 494 | - $query = "SELECT COUNT( DISTINCT post_author ) FROM {$wpdb->posts} WHERE post_type = 'post' AND post_status = 'publish' AND post_date <= DATE_SUB( NOW(), INTERVAL %d MONTH )"; | |
| 495 | - $query = $wpdb->prepare( $query, $duration ); | |
| 496 | - $count = $wpdb->get_var( $query ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery | |
| 497 | - | |
| 498 | - set_transient( $transient_key, $count, DAY_IN_SECONDS ); | |
| 226 | + if ( ! $followers ) { | |
| 227 | + return array(); | |
| 499 | 228 | } |
| 500 | 229 | |
| 501 | - // if 0 authors where active | |
| 502 | - if ( 0 === $count ) { | |
| 503 | - return 0; | |
| 504 | - } | |
| 505 | - | |
| 506 | - // if single user mode | |
| 507 | - if ( is_single_user() ) { | |
| 508 | - return 1; | |
| 509 | - } | |
| 510 | - | |
| 511 | - // if blog user is disabled | |
| 512 | - if ( is_user_disabled( Users::BLOG_USER_ID ) ) { | |
| 513 | - return $count; | |
| 514 | - } | |
| 515 | - | |
| 516 | - // also count blog user | |
| 517 | - return $count + 1; | |
| 230 | + return $followers; | |
| 518 | 231 | } |
| 519 | 232 | |
| 520 | -/** | |
| 521 | - * Get the total number of users | |
| 522 | - * | |
| 523 | - * @return int The total number of users | |
| 524 | - */ | |
| 525 | -function get_total_users() { | |
| 526 | - // if single user mode | |
| 527 | - if ( is_single_user() ) { | |
| 528 | - return 1; | |
| 529 | - } | |
| 233 | +function count_followers( $user_id ) { | |
| 234 | + $followers = \Activitypub\get_followers( $user_id ); | |
| 530 | 235 | |
| 531 | - $users = \get_users( | |
| 532 | - array( | |
| 533 | - 'capability__in' => array( 'publish_posts' ), | |
| 534 | - ) | |
| 535 | - ); | |
| 536 | - | |
| 537 | - if ( is_array( $users ) ) { | |
| 538 | - $users = count( $users ); | |
| 539 | - } else { | |
| 540 | - $users = 1; | |
| 541 | - } | |
| 542 | - | |
| 543 | - // if blog user is disabled | |
| 544 | - if ( is_user_disabled( Users::BLOG_USER_ID ) ) { | |
| 545 | - return $users; | |
| 546 | - } | |
| 547 | - | |
| 548 | - return $users + 1; | |
| 236 | + return count( $followers ); | |
| 549 | 237 | } |