PluginProbe
ActivityPub / 0.4.3
ActivityPub v0.4.3
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.4.3, at includes/functions.php

220 lines 5.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Returns the ActivityPub default JSON-context
4 *
5 * @return array the activitypub context
6 */
7 function get_activitypub_context() {
8 $context = array(
9 'https://www.w3.org/ns/activitystreams',
10 'https://w3id.org/security/v1',
11 array(
12 'manuallyApprovesFollowers' => 'as:manuallyApprovesFollowers',
13 'sensitive' => 'as:sensitive',
14 'movedTo' => array(
15 '@id' => 'as:movedTo',
16 '@type' => '@id',
17 ),
18 'Hashtag' => 'as:Hashtag',
19 'ostatus' => 'http://ostatus.org#',
20 'atomUri' => 'ostatus:atomUri',
21 'inReplyToAtomUri' => 'ostatus:inReplyToAtomUri',
22 'conversation' => 'ostatus:conversation',
23 'toot' => 'http://joinmastodon.org/ns#',
24 'Emoji' => 'toot:Emoji',
25 'focalPoint' => array(
26 '@container' => '@list',
27 '@id' => 'toot:focalPoint',
28 ),
29 'featured' => array(
30 '@id' => 'toot:featured',
31 '@type' => '@id',
32 ),
33 'schema' => 'http://schema.org#',
34 'PropertyValue' => 'schema:PropertyValue',
35 'value' => 'schema:value',
36 ),
37 );
38
39 return apply_filters( 'activitypub_json_context', $context );
40 }
41
42 function activitypub_safe_remote_post( $url, $body, $user_id ) {
43 $date = gmdate( 'D, d M Y H:i:s T' );
44 $signature = Activitypub_Signature::generate_signature( $user_id, $url, $date );
45
46 $wp_version = get_bloginfo( 'version' );
47 $user_agent = apply_filters( 'http_headers_useragent', 'WordPress/' . $wp_version . '; ' . get_bloginfo( 'url' ) );
48 $args = array(
49 'timeout' => 100,
50 'limit_response_size' => 1048576,
51 'redirection' => 3,
52 'user-agent' => "$user_agent; ActivityPub",
53 'headers' => array(
54 'Accept' => 'application/activity+json',
55 'Content-Type' => 'application/activity+json',
56 'Signature' => $signature,
57 'Date' => $date,
58 ),
59 'body' => $body,
60 );
61
62 return wp_safe_remote_post( $url, $args );
63 }
64
65 /**
66 * Returns a users WebFinger "resource"
67 *
68 * @param int $user_id
69 *
70 * @return string The user-resource
71 */
72 function activitypub_get_webfinger_resource( $user_id ) {
73 // use WebFinger plugin if installed
74 if ( function_exists( 'get_webfinger_resource' ) ) {
75 return get_webfinger_resource( $user_id, false );
76 }
77
78 $user = get_user_by( 'id', $user_id );
79
80 return $user->user_login . '@' . wp_parse_url( home_url(), PHP_URL_HOST );
81 }
82
83 /**
84 * [get_metadata_by_actor description]
85 *
86 * @param [type] $actor [description]
87 * @return [type] [description]
88 */
89 function activitypub_get_remote_metadata_by_actor( $actor ) {
90 $metadata = get_transient( 'activitypub_' . $actor );
91
92 if ( $metadata ) {
93 return $metadata;
94 }
95
96 if ( ! wp_http_validate_url( $actor ) ) {
97 return new WP_Error( 'activitypub_no_valid_actor_url', __( 'The "actor" is no valid URL', 'activitypub' ), $actor );
98 }
99
100 $wp_version = get_bloginfo( 'version' );
101
102 $user_agent = apply_filters( 'http_headers_useragent', 'WordPress/' . $wp_version . '; ' . get_bloginfo( 'url' ) );
103 $args = array(
104 'timeout' => 100,
105 'limit_response_size' => 1048576,
106 'redirection' => 3,
107 'user-agent' => "$user_agent; ActivityPub",
108 'headers' => array( 'accept' => 'application/activity+json' ),
109 );
110
111 $response = wp_safe_remote_get( $actor, $args );
112
113 if ( is_wp_error( $response ) ) {
114 return $response;
115 }
116
117 $metadata = wp_remote_retrieve_body( $response );
118 $metadata = json_decode( $metadata, true );
119
120 if ( ! $metadata ) {
121 return new WP_Error( 'activitypub_invalid_json', __( 'No valid JSON data', 'activitypub' ), $actor );
122 }
123
124 set_transient( 'activitypub_' . $actor, $metadata, WEEK_IN_SECONDS );
125
126 return $metadata;
127 }
128
129 /**
130 * [get_inbox_by_actor description]
131 * @param [type] $actor [description]
132 * @return [type] [description]
133 */
134 function activitypub_get_inbox_by_actor( $actor ) {
135 $metadata = activitypub_get_remote_metadata_by_actor( $actor );
136
137 if ( is_wp_error( $metadata ) ) {
138 return $metadata;
139 }
140
141 if ( isset( $metadata['endpoints'] ) && isset( $metadata['endpoints']['sharedInbox'] ) ) {
142 return $metadata['endpoints']['sharedInbox'];
143 }
144
145 if ( array_key_exists( 'inbox', $metadata ) ) {
146 return $metadata['inbox'];
147 }
148
149 return new WP_Error( 'activitypub_no_inbox', __( 'No "Inbox" found', 'activitypub' ), $metadata );
150 }
151
152 /**
153 * [get_inbox_by_actor description]
154 * @param [type] $actor [description]
155 * @return [type] [description]
156 */
157 function activitypub_get_publickey_by_actor( $actor, $key_id ) {
158 $metadata = activitypub_get_remote_metadata_by_actor( $actor );
159
160 if ( is_wp_error( $metadata ) ) {
161 return $metadata;
162 }
163
164 if (
165 isset( $metadata['publicKey'] ) &&
166 isset( $metadata['publicKey']['id'] ) &&
167 isset( $metadata['publicKey']['owner'] ) &&
168 isset( $metadata['publicKey']['publicKeyPem'] ) &&
169 $key_id === $metadata['publicKey']['id'] &&
170 $actor === $metadata['publicKey']['owner']
171 ) {
172 return $metadata['publicKey']['publicKeyPem'];
173 }
174
175 return new WP_Error( 'activitypub_no_public_key', __( 'No "Public-Key" found', 'activitypub' ), $metadata );
176 }
177
178 function activitypub_get_follower_inboxes( $user_id, $followers ) {
179 $inboxes = array();
180 foreach ( $followers as $follower ) {
181 $inboxes[] = activitypub_get_inbox_by_actor( $follower );
182 }
183
184 return array_unique( $inboxes );
185 }
186
187 function activitypub_get_identifier_settings( $user_id ) {
188 ?>
189 <table class="form-table">
190 <tbody>
191 <tr>
192 <th scope="row">
193 <label><?php esc_html_e( 'Profile identifier', 'activitypub' ); ?></label>
194 </th>
195 <td>
196 <p><code><?php echo activitypub_get_webfinger_resource( $user_id ); ?></code> or <code><?php echo get_author_posts_url( $user_id ); ?></code></p>
197 <p class="description"><?php printf( __( 'Try to follow "@%s" in the mastodon/friendi.ca search field.', 'activitypub' ), activitypub_get_webfinger_resource( $user_id ) ); ?></p>
198 </td>
199 </tr>
200 </tbody>
201 </table>
202 <?php
203 }
204
205 function activitypub_get_followers( $user_id ) {
206 $followers = Db_Activitypub_Followers::get_followers( $user_id );
207
208 if ( ! $followers ) {
209 return array();
210 }
211
212 return $followers;
213 }
214
215 function activitypub_count_followers( $user_id ) {
216 $followers = activitypub_get_followers( $user_id );
217
218 return count( $followers );
219 }
220