PluginProbe
ActivityPub / 8.3.0
ActivityPub v8.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 8.3.0, at includes/functions-url.php

168 lines 4.0 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 * Check if a URL is from the same domain as the site.
55 *
56 * @param string $url The URL to check.
57 *
58 * @return boolean True if the URL is from the same domain, false otherwise.
59 */
60 function is_same_domain( $url ) {
61 $remote = \wp_parse_url( $url, PHP_URL_HOST );
62
63 if ( ! $remote ) {
64 return false;
65 }
66
67 $remote = normalize_host( $remote );
68 $self = normalize_host( home_host() );
69
70 return $remote === $self;
71 }
72
73 /**
74 * Retrieves the Host for the current site where the front end is accessible.
75 *
76 * @return string The host for the current site.
77 */
78 function home_host() {
79 return \wp_parse_url( \home_url(), PHP_URL_HOST );
80 }
81
82 /**
83 * Get the base URL for uploads.
84 *
85 * @return string The upload base URL.
86 */
87 function get_upload_baseurl() {
88 /**
89 * Early filter to allow plugins to set the upload base URL.
90 *
91 * @param string|false $maybe_upload_dir The upload base URL or false if not set.
92 */
93 $maybe_upload_dir = apply_filters( 'pre_activitypub_get_upload_baseurl', false );
94 if ( false !== $maybe_upload_dir ) {
95 return $maybe_upload_dir;
96 }
97
98 $upload_dir = \wp_get_upload_dir();
99
100 /**
101 * Filters the upload base URL.
102 *
103 * @param string $upload_dir The upload base URL. Default \wp_get_upload_dir()['baseurl']
104 */
105 return apply_filters( 'activitypub_get_upload_baseurl', $upload_dir['baseurl'] );
106 }
107
108 /**
109 * Get the authority (scheme + host) from a URL.
110 *
111 * @param string $url The URL to parse.
112 *
113 * @return string|false The authority, or false on failure.
114 */
115 function get_url_authority( $url ) {
116 $parsed = wp_parse_url( $url );
117
118 if ( ! $parsed || empty( $parsed['scheme'] ) || empty( $parsed['host'] ) ) {
119 return false;
120 }
121
122 return $parsed['scheme'] . '://' . $parsed['host'];
123 }
124
125 /**
126 * Infer a shortname from the Actor ID or URL. Used only for fallbacks,
127 * we will try to use what's supplied.
128 *
129 * @param string $uri The URI.
130 *
131 * @return string Hopefully the name of the Follower.
132 */
133 function extract_name_from_uri( $uri ) {
134 $name = $uri;
135
136 if ( \filter_var( $name, FILTER_VALIDATE_URL ) ) {
137 $name = \rtrim( $name, '/' );
138 $path = \wp_parse_url( $name, PHP_URL_PATH );
139 if ( $path && '/' !== $path ) {
140 if ( \strpos( $name, '@' ) !== false ) {
141 // Expected: https://example.com/@user (default URL pattern).
142 $name = \preg_replace( '|^/@?|', '', $path );
143 } else {
144 // Expected: https://example.com/users/user (default ID pattern).
145 $parts = \explode( '/', $path );
146 $name = \array_pop( $parts );
147 }
148 } else {
149 $name = \wp_parse_url( $name, PHP_URL_HOST );
150 $name = \str_replace( 'www.', '', $name );
151 }
152 } elseif (
153 \is_email( $name ) ||
154 \strpos( $name, 'acct' ) === 0 ||
155 \strpos( $name, '@' ) === 0
156 ) {
157 // Expected: user@example.com or acct:user@example (WebFinger).
158 $name = \ltrim( $name, '@' );
159 if ( str_starts_with( $name, 'acct:' ) ) {
160 $name = \substr( $name, 5 );
161 }
162 $parts = \explode( '@', $name );
163 $name = $parts[0];
164 }
165
166 return $name;
167 }
168