| 1 |
<?php |
| 2 |
/** |
| 3 |
* Helper functions for WebFinger. |
| 4 |
* |
| 5 |
* @package Webfinger |
| 6 |
*/ |
| 7 |
|
| 8 |
if ( ! function_exists( 'url_to_authorid' ) ) : |
| 9 |
/** |
| 10 |
* Examine a url and try to determine the author ID it represents. |
| 11 |
* |
| 12 |
* Checks are supposedly from the hosted site blog. |
| 13 |
* |
| 14 |
* @param string $url Permalink to check. |
| 15 |
* |
| 16 |
* @return int User ID, or 0 on failure. |
| 17 |
*/ |
| 18 |
function url_to_authorid( $url ) { |
| 19 |
global $wp_rewrite; |
| 20 |
|
| 21 |
// Check if url has the same host. |
| 22 |
if ( \wp_parse_url( \site_url(), \PHP_URL_HOST ) !== \wp_parse_url( $url, \PHP_URL_HOST ) ) { |
| 23 |
return 0; |
| 24 |
} |
| 25 |
|
| 26 |
// First, check to see if there is a 'author=N' to match against. |
| 27 |
if ( \preg_match( '/[?&]author=(\d+)/i', $url, $values ) ) { |
| 28 |
$id = \absint( $values[1] ); |
| 29 |
if ( $id ) { |
| 30 |
return $id; |
| 31 |
} |
| 32 |
} |
| 33 |
|
| 34 |
// Check to see if we are using rewrite rules. |
| 35 |
$rewrite = $wp_rewrite->wp_rewrite_rules(); |
| 36 |
|
| 37 |
// Not using rewrite rules, and 'author=N' method failed, so we're out of options. |
| 38 |
if ( empty( $rewrite ) ) { |
| 39 |
return 0; |
| 40 |
} |
| 41 |
|
| 42 |
// Generate rewrite rule for the author url. |
| 43 |
$author_rewrite = $wp_rewrite->get_author_permastruct(); |
| 44 |
$author_regexp = \str_replace( '%author%', '', $author_rewrite ); |
| 45 |
|
| 46 |
// Match the rewrite rule with the passed url. |
| 47 |
if ( \preg_match( '/https?:\/\/(.+)' . \preg_quote( $author_regexp, '/' ) . '([^\/]+)/i', $url, $match ) ) { |
| 48 |
$user = \get_user_by( 'slug', $match[2] ); |
| 49 |
if ( $user ) { |
| 50 |
return $user->ID; |
| 51 |
} |
| 52 |
} |
| 53 |
|
| 54 |
return 0; |
| 55 |
} |
| 56 |
endif; |
| 57 |
|
| 58 |
if ( ! function_exists( 'get_user_by_various' ) ) : |
| 59 |
/** |
| 60 |
* Convenience method to get user data by ID, username, object or from current user. |
| 61 |
* |
| 62 |
* @param mixed $id_or_name_or_object The username, ID or object. If not provided, the current user will be used. |
| 63 |
* |
| 64 |
* @return \WP_User|false WP_User on success, false on failure. |
| 65 |
* |
| 66 |
* @author Will Norris |
| 67 |
* |
| 68 |
* @see get_user_by_various() # DiSo OpenID-Plugin |
| 69 |
*/ |
| 70 |
function get_user_by_various( $id_or_name_or_object = null ) { |
| 71 |
if ( null === $id_or_name_or_object ) { |
| 72 |
$user = \wp_get_current_user(); |
| 73 |
return $user->exists() ? $user : false; |
| 74 |
} |
| 75 |
|
| 76 |
if ( $id_or_name_or_object instanceof \WP_User ) { |
| 77 |
return $id_or_name_or_object; |
| 78 |
} |
| 79 |
|
| 80 |
if ( \is_numeric( $id_or_name_or_object ) ) { |
| 81 |
return \get_user_by( 'id', $id_or_name_or_object ); |
| 82 |
} |
| 83 |
|
| 84 |
return \get_user_by( 'login', $id_or_name_or_object ); |
| 85 |
} |
| 86 |
endif; |
| 87 |
|
| 88 |
/** |
| 89 |
* Build WebFinger endpoint. |
| 90 |
* |
| 91 |
* @return string The WebFinger URL. |
| 92 |
*/ |
| 93 |
function get_webfinger_endpoint() { |
| 94 |
global $wp_rewrite; |
| 95 |
|
| 96 |
if ( $wp_rewrite->using_permalinks() ) { |
| 97 |
return \home_url( '/.well-known/webfinger' ); |
| 98 |
} |
| 99 |
|
| 100 |
return \add_query_arg( 'well-known', 'webfinger', \home_url( '/' ) ); |
| 101 |
} |
| 102 |
|
| 103 |
/** |
| 104 |
* Returns all WebFinger "resources". |
| 105 |
* |
| 106 |
* @param mixed $id_or_name_or_object The username, ID or object. |
| 107 |
* @param bool $with_protocol Whether to include the protocol prefix. |
| 108 |
* |
| 109 |
* @return string The user-resource. |
| 110 |
*/ |
| 111 |
function get_webfinger_resource( $id_or_name_or_object, $with_protocol = true ) { |
| 112 |
return \Webfinger\User::get_resource( $id_or_name_or_object, $with_protocol ); |
| 113 |
} |
| 114 |
|
| 115 |
/** |
| 116 |
* Returns a WebFinger "username" (the part before the "@"). |
| 117 |
* |
| 118 |
* @param mixed $id_or_name_or_object The username, ID or object. |
| 119 |
* |
| 120 |
* @return string The username. |
| 121 |
*/ |
| 122 |
function get_webfinger_username( $id_or_name_or_object ) { |
| 123 |
return \Webfinger\User::get_username( $id_or_name_or_object ); |
| 124 |
} |
| 125 |
|
| 126 |
/** |
| 127 |
* Check if a passed URI has the same domain as the blog. |
| 128 |
* |
| 129 |
* @param string $uri The URI to check. |
| 130 |
* |
| 131 |
* @return bool True if the URI has the same host as the blog, false otherwise. |
| 132 |
*/ |
| 133 |
function is_same_host( $uri ) { |
| 134 |
if ( ! $uri || ! \is_string( $uri ) ) { |
| 135 |
return false; |
| 136 |
} |
| 137 |
|
| 138 |
$blog_host = \wp_parse_url( \home_url(), \PHP_URL_HOST ); |
| 139 |
$parts = \wp_parse_url( $uri ); |
| 140 |
$host = $parts['host'] ?? ''; |
| 141 |
|
| 142 |
/* |
| 143 |
* Schemes without an authority component -- `acct:` and `mailto:` -- carry |
| 144 |
* the host in the path, so `wp_parse_url()` reports no host for them. Fall |
| 145 |
* back to the part after the last "@" of the path, which also covers bare |
| 146 |
* e-mail addresses and bare `user@host` identifiers. Reading the path and |
| 147 |
* not the whole URI keeps a query or fragment (`mailto:me@example.com |
| 148 |
* ?subject=hi`) out of the host. |
| 149 |
*/ |
| 150 |
$path = $parts['path'] ?? ''; |
| 151 |
|
| 152 |
if ( ! $host && \str_contains( $path, '@' ) ) { |
| 153 |
$host = \substr( \strrchr( $path, '@' ), 1 ); |
| 154 |
} |
| 155 |
|
| 156 |
return (bool) $host && $host === $blog_host; |
| 157 |
} |
| 158 |
|