| @@ -1,95 +1,128 @@ | ||
| 1 | 1 | <?php |
| 2 | -/** | |
| 3 | - * Mention class file. | |
| 4 | - * | |
| 5 | - * @package Activitypub | |
| 6 | - */ | |
| 2 | +namespace Activitypub; | |
| 7 | 3 | |
| 8 | -namespace Activitypub; | |
| 4 | +use WP_Error; | |
| 5 | +use Activitypub\Webfinger; | |
| 9 | 6 | |
| 10 | -use Activitypub\Collection\Remote_Actors; | |
| 7 | +use function Activitypub\object_to_uri; | |
| 11 | 8 | |
| 12 | 9 | /** |
| 13 | - * ActivityPub Mention Class. | |
| 10 | + * ActivityPub Mention Class | |
| 14 | 11 | * |
| 15 | 12 | * @author Alex Kirk |
| 16 | 13 | */ |
| 17 | 14 | class Mention { |
| 18 | 15 | /** |
| 19 | - * Initialize the class, registering WordPress hooks. | |
| 16 | + * Initialize the class, registering WordPress hooks | |
| 20 | 17 | */ |
| 21 | 18 | public static function init() { |
| 22 | - \add_filter( 'the_content', array( self::class, 'the_content' ), 99 ); | |
| 23 | - \add_filter( 'comment_text', array( self::class, 'the_content' ) ); | |
| 24 | - \add_filter( 'activitypub_extra_field_content', array( self::class, 'the_content' ) ); | |
| 19 | + \add_filter( 'the_content', array( self::class, 'the_content' ), 99, 1 ); | |
| 20 | + \add_filter( 'comment_text', array( self::class, 'the_content' ), 10, 1 ); | |
| 25 | 21 | \add_filter( 'activitypub_extract_mentions', array( self::class, 'extract_mentions' ), 99, 2 ); |
| 26 | - \add_filter( 'activitypub_activity_object_array', array( self::class, 'filter_activity_object' ), 99 ); | |
| 27 | 22 | } |
| 28 | 23 | |
| 29 | 24 | /** |
| 30 | - * Filter only the activity object and replace summery it with URLs | |
| 31 | - * add tag to user. | |
| 25 | + * Filter to replace the mentions in the content with links | |
| 32 | 26 | * |
| 33 | - * @param array $object_array Array of activity. | |
| 27 | + * @param string $the_content the post-content | |
| 34 | 28 | * |
| 35 | - * @return array The activity object array. | |
| 29 | + * @return string the filtered post-content | |
| 36 | 30 | */ |
| 37 | - public static function filter_activity_object( $object_array ) { | |
| 38 | - if ( ! empty( $object_array['summary'] ) ) { | |
| 39 | - $object_array['summary'] = self::the_content( $object_array['summary'] ); | |
| 31 | + public static function the_content( $the_content ) { | |
| 32 | + // small protection against execution timeouts: limit to 1 MB | |
| 33 | + if ( mb_strlen( $the_content ) > MB_IN_BYTES ) { | |
| 34 | + return $the_content; | |
| 40 | 35 | } |
| 36 | + $tag_stack = array(); | |
| 37 | + $protected_tags = array( | |
| 38 | + 'pre', | |
| 39 | + 'code', | |
| 40 | + 'textarea', | |
| 41 | + 'style', | |
| 42 | + 'a', | |
| 43 | + ); | |
| 44 | + $content_with_links = ''; | |
| 45 | + $in_protected_tag = false; | |
| 46 | + foreach ( wp_html_split( $the_content ) as $chunk ) { | |
| 47 | + if ( preg_match( '#^<!--[\s\S]*-->$#i', $chunk, $m ) ) { | |
| 48 | + $content_with_links .= $chunk; | |
| 49 | + continue; | |
| 50 | + } | |
| 41 | 51 | |
| 42 | - if ( ! empty( $object_array['content'] ) ) { | |
| 43 | - $object_array['content'] = self::the_content( $object_array['content'] ); | |
| 52 | + if ( preg_match( '#^<(/)?([a-z-]+)\b[^>]*>$#i', $chunk, $m ) ) { | |
| 53 | + $tag = strtolower( $m[2] ); | |
| 54 | + if ( '/' === $m[1] ) { | |
| 55 | + // Closing tag. | |
| 56 | + $i = array_search( $tag, $tag_stack ); | |
| 57 | + // We can only remove the tag from the stack if it is in the stack. | |
| 58 | + if ( false !== $i ) { | |
| 59 | + $tag_stack = array_slice( $tag_stack, 0, $i ); | |
| 60 | + } | |
| 61 | + } else { | |
| 62 | + // Opening tag, add it to the stack. | |
| 63 | + $tag_stack[] = $tag; | |
| 64 | + } | |
| 65 | + | |
| 66 | + // If we're in a protected tag, the tag_stack contains at least one protected tag string. | |
| 67 | + // The protected tag state can only change when we encounter a start or end tag. | |
| 68 | + $in_protected_tag = array_intersect( $tag_stack, $protected_tags ); | |
| 69 | + | |
| 70 | + // Never inspect tags. | |
| 71 | + $content_with_links .= $chunk; | |
| 72 | + continue; | |
| 73 | + } | |
| 74 | + | |
| 75 | + if ( $in_protected_tag ) { | |
| 76 | + // Don't inspect a chunk inside an inspected tag. | |
| 77 | + $content_with_links .= $chunk; | |
| 78 | + continue; | |
| 79 | + } | |
| 80 | + | |
| 81 | + // Only reachable when there is no protected tag in the stack. | |
| 82 | + $content_with_links .= \preg_replace_callback( '/@' . ACTIVITYPUB_USERNAME_REGEXP . '/', array( self::class, 'replace_with_links' ), $chunk ); | |
| 44 | 83 | } |
| 45 | 84 | |
| 46 | - return $object_array; | |
| 85 | + return $content_with_links; | |
| 47 | 86 | } |
| 48 | 87 | |
| 49 | 88 | /** |
| 50 | - * Filter to replace the mentions in the content with links. | |
| 89 | + * A callback for preg_replace to build the user links | |
| 51 | 90 | * |
| 52 | - * @param string $the_content The post content. | |
| 91 | + * @param array $result the preg_match results | |
| 53 | 92 | * |
| 54 | - * @return string The filtered post-content. | |
| 93 | + * @return string the final string | |
| 55 | 94 | */ |
| 56 | - public static function the_content( $the_content ) { | |
| 57 | - return enrich_content_data( $the_content, '/@' . ACTIVITYPUB_USERNAME_REGEXP . '/', array( self::class, 'replace_with_links' ) ); | |
| 58 | - } | |
| 59 | - | |
| 60 | - /** | |
| 61 | - * A callback for preg_replace to build the user links. | |
| 62 | - * | |
| 63 | - * @param array $result The preg_match results. | |
| 64 | - * | |
| 65 | - * @return string The final string. | |
| 66 | - */ | |
| 67 | 95 | public static function replace_with_links( $result ) { |
| 68 | - $post = Remote_Actors::fetch_by_acct( $result[0] ); | |
| 96 | + $metadata = get_remote_metadata_by_actor( $result[0] ); | |
| 69 | 97 | |
| 70 | - if ( \is_wp_error( $post ) ) { | |
| 71 | - return $result[0]; | |
| 72 | - } | |
| 98 | + if ( | |
| 99 | + ! empty( $metadata ) && | |
| 100 | + ! is_wp_error( $metadata ) && | |
| 101 | + ( ! empty( $metadata['id'] ) || ! empty( $metadata['url'] ) ) | |
| 102 | + ) { | |
| 103 | + $username = ltrim( $result[0], '@' ); | |
| 104 | + if ( ! empty( $metadata['name'] ) ) { | |
| 105 | + $username = $metadata['name']; | |
| 106 | + } | |
| 107 | + if ( ! empty( $metadata['preferredUsername'] ) ) { | |
| 108 | + $username = $metadata['preferredUsername']; | |
| 109 | + } | |
| 73 | 110 | |
| 74 | - $actor = Remote_Actors::get_actor( $post ); | |
| 111 | + $url = isset( $metadata['url'] ) ? object_to_uri( $metadata['url'] ) : object_to_uri( $metadata['id'] ); | |
| 75 | 112 | |
| 76 | - if ( \is_wp_error( $actor ) ) { | |
| 77 | - return $result[0]; | |
| 113 | + return \sprintf( '<a rel="mention" class="u-url mention" href="%s">@<span>%s</span></a>', esc_url( $url ), esc_html( $username ) ); | |
| 78 | 114 | } |
| 79 | 115 | |
| 80 | - $username = $actor->get_preferred_username() ?: $actor->get_name() ?: Sanitize::webfinger( $result[0] ); | |
| 81 | - $url = object_to_uri( $actor->get_url() ?: $actor->get_id() ); | |
| 82 | - | |
| 83 | - return \sprintf( '<a rel="mention" class="u-url mention" href="%1$s">@%2$s</a>', \esc_url( $url ), \esc_html( $username ) ); | |
| 116 | + return $result[0]; | |
| 84 | 117 | } |
| 85 | 118 | |
| 86 | 119 | /** |
| 87 | - * Get the Inboxes for the mentioned Actors. | |
| 120 | + * Get the Inboxes for the mentioned Actors | |
| 88 | 121 | * |
| 89 | - * @param array $mentioned The list of Actors that were mentioned. | |
| 122 | + * @param array $mentioned The list of Actors that were mentioned | |
| 90 | 123 | * |
| 91 | - * @return array The list of Inboxes. | |
| 124 | + * @return array The list of Inboxes | |
| 92 | 125 | */ |
| 93 | 126 | public static function get_inboxes( $mentioned ) { |
| 94 | 127 | $inboxes = array(); |
| 95 | 128 | |
| @@ -95,9 +128,9 @@ | ||
| 95 | 128 | |
| 96 | 129 | foreach ( $mentioned as $actor ) { |
| 97 | 130 | $inbox = self::get_inbox_by_mentioned_actor( $actor ); |
| 98 | 131 | |
| 99 | - if ( ! \is_wp_error( $inbox ) && $inbox ) { | |
| 132 | + if ( ! is_wp_error( $inbox ) && $inbox ) { | |
| 100 | 133 | $inboxes[] = $inbox; |
| 101 | 134 | } |
| 102 | 135 | } |
| 103 | 136 | |
| @@ -104,13 +137,13 @@ | ||
| 104 | 137 | return $inboxes; |
| 105 | 138 | } |
| 106 | 139 | |
| 107 | 140 | /** |
| 108 | - * Get the inbox from the Remote-Profile of a mentioned Actor. | |
| 141 | + * Get the inbox from the Remote-Profile of a mentioned Actor | |
| 109 | 142 | * |
| 110 | - * @param string $actor The Actor URL. | |
| 143 | + * @param string $actor The Actor-URL | |
| 111 | 144 | * |
| 112 | - * @return string|\WP_Error The Inbox-URL or WP_Error if not found. | |
| 145 | + * @return string The Inbox-URL | |
| 113 | 146 | */ |
| 114 | 147 | public static function get_inbox_by_mentioned_actor( $actor ) { |
| 115 | 148 | $metadata = get_remote_metadata_by_actor( $actor ); |
| 116 | 149 | |
| @@ -117,9 +150,9 @@ | ||
| 117 | 150 | if ( \is_wp_error( $metadata ) ) { |
| 118 | 151 | return $metadata; |
| 119 | 152 | } |
| 120 | 153 | |
| 121 | - if ( isset( $metadata['endpoints']['sharedInbox'] ) ) { | |
| 154 | + if ( isset( $metadata['endpoints'] ) && isset( $metadata['endpoints']['sharedInbox'] ) ) { | |
| 122 | 155 | return $metadata['endpoints']['sharedInbox']; |
| 123 | 156 | } |
| 124 | 157 | |
| 125 | 158 | if ( \array_key_exists( 'inbox', $metadata ) ) { |
| @@ -125,26 +158,26 @@ | ||
| 125 | 158 | if ( \array_key_exists( 'inbox', $metadata ) ) { |
| 126 | 159 | return $metadata['inbox']; |
| 127 | 160 | } |
| 128 | 161 | |
| 129 | - return new \WP_Error( 'activitypub_no_inbox', \__( 'No "Inbox" found', 'activitypub' ), $metadata ); | |
| 162 | + return new WP_Error( 'activitypub_no_inbox', \__( 'No "Inbox" found', 'activitypub' ), $metadata ); | |
| 130 | 163 | } |
| 131 | 164 | |
| 132 | 165 | /** |
| 133 | 166 | * Extract the mentions from the post_content. |
| 134 | 167 | * |
| 135 | - * @param array $mentions The already found mentions. | |
| 168 | + * @param array $mentions The already found mentions. | |
| 136 | 169 | * @param string $post_content The post content. |
| 137 | 170 | * |
| 138 | - * @return array The discovered mentions. | |
| 171 | + * @return mixed The discovered mentions. | |
| 139 | 172 | */ |
| 140 | 173 | public static function extract_mentions( $mentions, $post_content ) { |
| 141 | 174 | \preg_match_all( '/@' . ACTIVITYPUB_USERNAME_REGEXP . '/i', $post_content, $matches ); |
| 142 | 175 | foreach ( $matches[0] as $match ) { |
| 143 | 176 | $link = Webfinger::resolve( $match ); |
| 144 | - if ( ! \is_wp_error( $link ) ) { | |
| 177 | + if ( ! is_wp_error( $link ) ) { | |
| 145 | 178 | $mentions[ $match ] = $link; |
| 146 | 179 | } |
| 147 | 180 | } |
| 148 | - return \array_unique( $mentions ); | |
| 181 | + return $mentions; | |
| 149 | 182 | } |
| 150 | 183 | } |