PluginProbe
ActivityPub / 1.0.3
ActivityPub v1.0.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 / class-http.php

class-http.php in ActivityPub 1.0.3, at includes/class-http.php

112 lines 3.1 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 use WP_Error;
5 use Activitypub\Collection\Users;
6
7 /**
8 * ActivityPub HTTP Class
9 *
10 * @author Matthias Pfefferle
11 */
12 class Http {
13 /**
14 * Send a POST Request with the needed HTTP Headers
15 *
16 * @param string $url The URL endpoint
17 * @param string $body The Post Body
18 * @param int $user_id The WordPress User-ID
19 *
20 * @return array|WP_Error The POST Response or an WP_ERROR
21 */
22 public static function post( $url, $body, $user_id ) {
23 do_action( 'activitypub_pre_http_post', $url, $body, $user_id );
24
25 $date = \gmdate( 'D, d M Y H:i:s T' );
26 $digest = Signature::generate_digest( $body );
27 $signature = Signature::generate_signature( $user_id, 'post', $url, $date, $digest );
28
29 $wp_version = \get_bloginfo( 'version' );
30
31 /**
32 * Filter the HTTP headers user agent.
33 *
34 * @param string $user_agent The user agent string.
35 */
36 $user_agent = \apply_filters( 'http_headers_useragent', 'WordPress/' . $wp_version . '; ' . \get_bloginfo( 'url' ) );
37 $args = array(
38 'timeout' => 100,
39 'limit_response_size' => 1048576,
40 'redirection' => 3,
41 'user-agent' => "$user_agent; ActivityPub",
42 'headers' => array(
43 'Accept' => 'application/activity+json',
44 'Content-Type' => 'application/activity+json',
45 'Digest' => $digest,
46 'Signature' => $signature,
47 'Date' => $date,
48 ),
49 'body' => $body,
50 );
51
52 $response = \wp_safe_remote_post( $url, $args );
53 $code = \wp_remote_retrieve_response_code( $response );
54
55 if ( $code >= 400 ) {
56 $response = new WP_Error( $code, __( 'Failed HTTP Request', 'activitypub' ), array( 'status' => $code ) );
57 }
58
59 \do_action( 'activitypub_safe_remote_post_response', $response, $url, $body, $user_id );
60
61 return $response;
62 }
63
64 /**
65 * Send a GET Request with the needed HTTP Headers
66 *
67 * @param string $url The URL endpoint
68 * @param int $user_id The WordPress User-ID
69 *
70 * @return array|WP_Error The GET Response or an WP_ERROR
71 */
72 public static function get( $url ) {
73 do_action( 'activitypub_pre_http_get', $url );
74
75 $date = \gmdate( 'D, d M Y H:i:s T' );
76 $signature = Signature::generate_signature( Users::APPLICATION_USER_ID, 'get', $url, $date );
77
78 $wp_version = \get_bloginfo( 'version' );
79
80 /**
81 * Filter the HTTP headers user agent.
82 *
83 * @param string $user_agent The user agent string.
84 */
85 $user_agent = \apply_filters( 'http_headers_useragent', 'WordPress/' . $wp_version . '; ' . \get_bloginfo( 'url' ) );
86
87 $args = array(
88 'timeout' => apply_filters( 'activitypub_remote_get_timeout', 100 ),
89 'limit_response_size' => 1048576,
90 'redirection' => 3,
91 'user-agent' => "$user_agent; ActivityPub",
92 'headers' => array(
93 'Accept' => 'application/activity+json',
94 'Content-Type' => 'application/activity+json',
95 'Signature' => $signature,
96 'Date' => $date,
97 ),
98 );
99
100 $response = \wp_safe_remote_get( $url, $args );
101 $code = \wp_remote_retrieve_response_code( $response );
102
103 if ( $code >= 400 ) {
104 $response = new WP_Error( $code, __( 'Failed HTTP Request', 'activitypub' ), array( 'status' => $code ) );
105 }
106
107 \do_action( 'activitypub_safe_remote_get_response', $response, $url );
108
109 return $response;
110 }
111 }
112