PluginProbe
ActivityPub / 0.7.2
ActivityPub v0.7.2
9.3.1 9.3.0 9.2.2 9.2.1 9.2.0 9.1.0 9.0.2 9.0.1 9.0.0 8.3.0 8.2.1 8.2.0 8.1.1 1.0.5 1.0.6 1.0.7 1.0.8 1.0.9 1.1.0 1.2.0 1.3.0 2.0.0 2.0.1 2.1.0 2.1.1 All 160 releases
activitypub / includes / functions.php

functions.php in ActivityPub 0.7.2, at includes/functions.php

235 lines 6.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace Activitypub;
3
4 /**
5 * Returns the ActivityPub default JSON-context
6 *
7 * @return array the activitypub context
8 */
9 function get_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 );
40
41 return apply_filters( 'activitypub_json_context', $context );
42 }
43
44 function safe_remote_post( $url, $body, $user_id ) {
45 $date = gmdate( 'D, d M Y H:i:s T' );
46 $signature = \Activitypub\Signature::generate_signature( $user_id, $url, $date );
47
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;
69 }
70
71 /**
72 * Returns a users WebFinger "resource"
73 *
74 * @param int $user_id
75 *
76 * @return string The user-resource
77 */
78 function get_webfinger_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 );
87 }
88
89 /**
90 * [get_metadata_by_actor description]
91 *
92 * @param sting $actor
93 *
94 * @return array
95 */
96 function get_remote_metadata_by_actor( $actor ) {
97 $metadata = get_transient( 'activitypub_' . $actor );
98
99 if ( $metadata ) {
100 return $metadata;
101 }
102
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 );
105 }
106
107 $wp_version = get_bloginfo( 'version' );
108
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 );
117
118 $response = wp_safe_remote_get( $actor, $args );
119
120 if ( is_wp_error( $response ) ) {
121 return $response;
122 }
123
124 $metadata = wp_remote_retrieve_body( $response );
125 $metadata = json_decode( $metadata, true );
126
127 if ( ! $metadata ) {
128 return new \WP_Error( 'activitypub_invalid_json', __( 'No valid JSON data', 'activitypub' ), $actor );
129 }
130
131 set_transient( 'activitypub_' . $actor, $metadata, WEEK_IN_SECONDS );
132
133 return $metadata;
134 }
135
136 /**
137 * [get_inbox_by_actor description]
138 * @param [type] $actor [description]
139 * @return [type] [description]
140 */
141 function get_inbox_by_actor( $actor ) {
142 $metadata = \Activitypub\get_remote_metadata_by_actor( $actor );
143
144 if ( is_wp_error( $metadata ) ) {
145 return $metadata;
146 }
147
148 if ( isset( $metadata['endpoints'] ) && isset( $metadata['endpoints']['sharedInbox'] ) ) {
149 return $metadata['endpoints']['sharedInbox'];
150 }
151
152 if ( array_key_exists( 'inbox', $metadata ) ) {
153 return $metadata['inbox'];
154 }
155
156 return new \WP_Error( 'activitypub_no_inbox', __( 'No "Inbox" found', 'activitypub' ), $metadata );
157 }
158
159 /**
160 * [get_inbox_by_actor description]
161 * @param [type] $actor [description]
162 * @return [type] [description]
163 */
164 function get_publickey_by_actor( $actor, $key_id ) {
165 $metadata = \Activitypub\get_remote_metadata_by_actor( $actor );
166
167 if ( is_wp_error( $metadata ) ) {
168 return $metadata;
169 }
170
171 if (
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']
178 ) {
179 return $metadata['publicKey']['publicKeyPem'];
180 }
181
182 return new \WP_Error( 'activitypub_no_public_key', __( 'No "Public-Key" found', 'activitypub' ), $metadata );
183 }
184
185 function get_follower_inboxes( $user_id ) {
186 $followers = \Activitypub\Db\Followers::get_followers( $user_id );
187 $inboxes = array();
188
189 foreach ( $followers as $follower ) {
190 $inbox = \Activitypub\get_inbox_by_actor( $follower );
191 // init array if empty
192 if ( ! empty( $inboxes[ $inbox ] ) ) {
193 $inboxes[ $inbox ] = array();
194 }
195 $inboxes[ $inbox ][] = $follower;
196 }
197
198 return $inboxes;
199 }
200
201 function get_identifier_settings( $user_id ) {
202 ?>
203 <table class="form-table">
204 <tbody>
205 <tr>
206 <th scope="row">
207 <label><?php esc_html_e( 'Profile identifier', 'activitypub' ); ?></label>
208 </th>
209 <td>
210 <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>
211 <?php // translators: the webfinger resource ?>
212 <p class="description"><?php printf( esc_html__( 'Try to follow "@%s" in the mastodon/friendi.ca search field.', 'activitypub' ), esc_html( \Activitypub\get_webfinger_resource( $user_id ) ) ); ?></p>
213 </td>
214 </tr>
215 </tbody>
216 </table>
217 <?php
218 }
219
220 function get_followers( $user_id ) {
221 $followers = \Activitypub\Db\Followers::get_followers( $user_id );
222
223 if ( ! $followers ) {
224 return array();
225 }
226
227 return $followers;
228 }
229
230 function count_followers( $user_id ) {
231 $followers = \Activitypub\get_followers( $user_id );
232
233 return count( $followers );
234 }
235