PluginProbe
ActivityPub / 9.3.0
ActivityPub v9.3.0
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-url.php

functions-url.php in ActivityPub 9.3.0, at includes/functions-url.php

188 lines 4.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * URL functions.
4 *
5 * Functions for URL manipulation.
6 *
7 * @package Activitypub
8 */
9
10 namespace Activitypub;
11
12 /**
13 * Get the REST URL relative to this plugin's namespace.
14 *
15 * @param string $path Optional. REST route path. Default ''.
16 *
17 * @return string REST URL relative to this plugin's namespace.
18 */
19 function get_rest_url_by_path( $path = '' ) {
20 // We'll handle the leading slash.
21 $path = \ltrim( $path, '/' );
22 $namespaced_path = \sprintf( '/%s/%s', ACTIVITYPUB_REST_NAMESPACE, $path );
23 return \get_rest_url( null, $namespaced_path );
24 }
25
26 /**
27 * Normalize a URL.
28 *
29 * @param string $url The URL.
30 *
31 * @return string The normalized URL.
32 */
33 function normalize_url( $url ) {
34 // Remove ActivityPub-specific query parameters.
35 $url = \remove_query_arg( array( 'activitypub', 'preview' ), $url );
36 $url = \untrailingslashit( $url );
37 $url = \preg_replace( '/^https?:\/\/(www\.)?/', '', $url );
38
39 return $url;
40 }
41
42 /**
43 * Normalize a host.
44 *
45 * @param string $host The host.
46 *
47 * @return string The normalized host.
48 */
49 function normalize_host( $host ) {
50 return \preg_replace( '/^www\./', '', $host );
51 }
52
53 /**
54 * Fold a host to its canonical spelling for comparison.
55 *
56 * Hosts are case-insensitive, IPv6 literals are bracketed in a URI but not in a stored host,
57 * and a trailing dot is the same fully qualified name. Two spellings that differ only in those
58 * ways address the same host, so anything comparing hosts has to fold them first.
59 *
60 * Unlike {@see normalize_host()} this is lossless: it does not strip `www.`, which is a
61 * different host rather than a different spelling of one.
62 *
63 * @since 9.3.0
64 *
65 * @param string $host The host.
66 *
67 * @return string The folded host.
68 */
69 function fold_host( $host ) {
70 return \rtrim( \trim( \strtolower( (string) $host ), '[]' ), '.' );
71 }
72
73 /**
74 * Check if a URL is from the same domain as the site.
75 *
76 * @param string $url The URL to check.
77 *
78 * @return boolean True if the URL is from the same domain, false otherwise.
79 */
80 function is_same_domain( $url ) {
81 $remote = \wp_parse_url( $url, PHP_URL_HOST );
82
83 if ( ! $remote ) {
84 return false;
85 }
86
87 $remote = normalize_host( $remote );
88 $self = normalize_host( home_host() );
89
90 return $remote === $self;
91 }
92
93 /**
94 * Retrieves the Host for the current site where the front end is accessible.
95 *
96 * @return string The host for the current site.
97 */
98 function home_host() {
99 return \wp_parse_url( \home_url(), PHP_URL_HOST );
100 }
101
102 /**
103 * Get the base URL for uploads.
104 *
105 * @return string The upload base URL.
106 */
107 function get_upload_baseurl() {
108 /**
109 * Early filter to allow plugins to set the upload base URL.
110 *
111 * @param string|false $maybe_upload_dir The upload base URL or false if not set.
112 */
113 $maybe_upload_dir = \apply_filters( 'pre_activitypub_get_upload_baseurl', false );
114 if ( false !== $maybe_upload_dir ) {
115 return $maybe_upload_dir;
116 }
117
118 $upload_dir = \wp_get_upload_dir();
119
120 /**
121 * Filters the upload base URL.
122 *
123 * @param string $upload_dir The upload base URL. Default \wp_get_upload_dir()['baseurl']
124 */
125 return \apply_filters( 'activitypub_get_upload_baseurl', $upload_dir['baseurl'] );
126 }
127
128 /**
129 * Get the authority (scheme + host) from a URL.
130 *
131 * @param string $url The URL to parse.
132 *
133 * @return string|false The authority, or false on failure.
134 */
135 function get_url_authority( $url ) {
136 $parsed = \wp_parse_url( $url );
137
138 if ( ! $parsed || empty( $parsed['scheme'] ) || empty( $parsed['host'] ) ) {
139 return false;
140 }
141
142 return $parsed['scheme'] . '://' . $parsed['host'];
143 }
144
145 /**
146 * Infer a shortname from the Actor ID or URL. Used only for fallbacks,
147 * we will try to use what's supplied.
148 *
149 * @param string $uri The URI.
150 *
151 * @return string Hopefully the name of the Follower.
152 */
153 function extract_name_from_uri( $uri ) {
154 $name = $uri;
155
156 if ( \filter_var( $name, FILTER_VALIDATE_URL ) ) {
157 $name = \rtrim( $name, '/' );
158 $path = \wp_parse_url( $name, PHP_URL_PATH );
159 if ( $path && '/' !== $path ) {
160 if ( \strpos( $name, '@' ) !== false ) {
161 // Expected: https://example.com/@user (default URL pattern).
162 $name = \preg_replace( '|^/@?|', '', $path );
163 } else {
164 // Expected: https://example.com/users/user (default ID pattern).
165 $parts = \explode( '/', $path );
166 $name = \array_pop( $parts );
167 }
168 } else {
169 $name = \wp_parse_url( $name, PHP_URL_HOST );
170 $name = \str_replace( 'www.', '', $name );
171 }
172 } elseif (
173 \is_email( $name ) ||
174 \strpos( $name, 'acct' ) === 0 ||
175 \strpos( $name, '@' ) === 0
176 ) {
177 // Expected: user@example.com or acct:user@example (WebFinger).
178 $name = \ltrim( $name, '@' );
179 if ( \str_starts_with( $name, 'acct:' ) ) {
180 $name = \substr( $name, 5 );
181 }
182 $parts = \explode( '@', $name );
183 $name = $parts[0];
184 }
185
186 return $name;
187 }
188