2023-05-24 16:32:00 +02:00
|
|
|
<?php
|
2023-07-03 11:20:44 +02:00
|
|
|
namespace Activitypub\Collection;
|
2023-05-24 16:32:00 +02:00
|
|
|
|
|
|
|
use WP_Error;
|
|
|
|
use WP_User_Query;
|
|
|
|
use Activitypub\Model\User;
|
|
|
|
use Activitypub\Model\Blog_User;
|
2023-06-01 11:45:07 +02:00
|
|
|
use Activitypub\Model\Application_User;
|
2023-05-24 16:32:00 +02:00
|
|
|
|
2023-12-12 17:10:52 +01:00
|
|
|
use function Activitypub\url_to_authorid;
|
2023-07-11 09:09:37 +02:00
|
|
|
use function Activitypub\is_user_disabled;
|
|
|
|
|
2023-07-03 11:20:44 +02:00
|
|
|
class Users {
|
2023-05-24 16:32:00 +02:00
|
|
|
/**
|
|
|
|
* The ID of the Blog User
|
|
|
|
*
|
|
|
|
* @var int
|
|
|
|
*/
|
|
|
|
const BLOG_USER_ID = 0;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The ID of the Application User
|
|
|
|
*
|
|
|
|
* @var int
|
|
|
|
*/
|
|
|
|
const APPLICATION_USER_ID = -1;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the User by ID
|
|
|
|
*
|
|
|
|
* @param int $user_id The User-ID.
|
|
|
|
*
|
|
|
|
* @return \Acitvitypub\Model\User The User.
|
|
|
|
*/
|
|
|
|
public static function get_by_id( $user_id ) {
|
2023-06-21 17:10:52 +02:00
|
|
|
if ( is_string( $user_id ) || is_numeric( $user_id ) ) {
|
|
|
|
$user_id = (int) $user_id;
|
|
|
|
}
|
2023-05-24 16:32:00 +02:00
|
|
|
|
2023-07-11 09:09:37 +02:00
|
|
|
if ( is_user_disabled( $user_id ) ) {
|
|
|
|
return new WP_Error(
|
|
|
|
'activitypub_user_not_found',
|
|
|
|
\__( 'User not found', 'activitypub' ),
|
|
|
|
array( 'status' => 404 )
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-06-28 14:22:27 +02:00
|
|
|
if ( self::BLOG_USER_ID === $user_id ) {
|
|
|
|
return Blog_User::from_wp_user( $user_id );
|
|
|
|
} elseif ( self::APPLICATION_USER_ID === $user_id ) {
|
|
|
|
return Application_User::from_wp_user( $user_id );
|
|
|
|
} elseif ( $user_id > 0 ) {
|
|
|
|
return User::from_wp_user( $user_id );
|
2023-05-24 16:32:00 +02:00
|
|
|
}
|
2023-06-21 17:10:52 +02:00
|
|
|
|
|
|
|
return new WP_Error(
|
|
|
|
'activitypub_user_not_found',
|
|
|
|
\__( 'User not found', 'activitypub' ),
|
|
|
|
array( 'status' => 404 )
|
|
|
|
);
|
2023-05-24 16:32:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the User by username.
|
|
|
|
*
|
|
|
|
* @param string $username The User-Name.
|
|
|
|
*
|
|
|
|
* @return \Acitvitypub\Model\User The User.
|
|
|
|
*/
|
|
|
|
public static function get_by_username( $username ) {
|
|
|
|
// check for blog user.
|
2023-05-30 11:22:20 +02:00
|
|
|
if ( Blog_User::get_default_username() === $username ) {
|
2023-05-24 16:32:00 +02:00
|
|
|
return self::get_by_id( self::BLOG_USER_ID );
|
|
|
|
}
|
|
|
|
|
2023-06-28 16:42:33 +02:00
|
|
|
if ( get_option( 'activitypub_blog_user_identifier' ) === $username ) {
|
|
|
|
return self::get_by_id( self::BLOG_USER_ID );
|
|
|
|
}
|
|
|
|
|
2023-05-26 16:08:08 +02:00
|
|
|
// check for application user.
|
2023-06-01 11:45:07 +02:00
|
|
|
if ( 'application' === $username ) {
|
2023-05-26 16:08:08 +02:00
|
|
|
return self::get_by_id( self::APPLICATION_USER_ID );
|
|
|
|
}
|
|
|
|
|
2023-05-24 16:32:00 +02:00
|
|
|
// check for 'activitypub_username' meta
|
|
|
|
$user = new WP_User_Query(
|
|
|
|
array(
|
|
|
|
'number' => 1,
|
|
|
|
'hide_empty' => true,
|
|
|
|
'fields' => 'ID',
|
2023-07-31 20:15:11 +02:00
|
|
|
// phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_query
|
2023-05-24 16:32:00 +02:00
|
|
|
'meta_query' => array(
|
|
|
|
'relation' => 'OR',
|
|
|
|
array(
|
2023-05-26 16:08:08 +02:00
|
|
|
'key' => 'activitypub_user_identifier',
|
2023-05-24 16:32:00 +02:00
|
|
|
'value' => $username,
|
|
|
|
'compare' => 'LIKE',
|
|
|
|
),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
if ( $user->results ) {
|
|
|
|
return self::get_by_id( $user->results[0] );
|
|
|
|
}
|
|
|
|
|
2023-12-12 17:10:52 +01:00
|
|
|
$username = str_replace( array( '*', '%' ), '', $username );
|
|
|
|
|
2023-05-24 16:32:00 +02:00
|
|
|
// check for login or nicename.
|
|
|
|
$user = new WP_User_Query(
|
|
|
|
array(
|
|
|
|
'search' => $username,
|
|
|
|
'search_columns' => array( 'user_login', 'user_nicename' ),
|
|
|
|
'number' => 1,
|
|
|
|
'hide_empty' => true,
|
|
|
|
'fields' => 'ID',
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
if ( $user->results ) {
|
|
|
|
return self::get_by_id( $user->results[0] );
|
|
|
|
}
|
|
|
|
|
|
|
|
return new WP_Error(
|
|
|
|
'activitypub_user_not_found',
|
|
|
|
\__( 'User not found', 'activitypub' ),
|
|
|
|
array( 'status' => 404 )
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the User by resource.
|
|
|
|
*
|
|
|
|
* @param string $resource The User-Resource.
|
|
|
|
*
|
|
|
|
* @return \Acitvitypub\Model\User The User.
|
|
|
|
*/
|
|
|
|
public static function get_by_resource( $resource ) {
|
2023-12-12 17:10:52 +01:00
|
|
|
$scheme = 'acct';
|
|
|
|
$match = array();
|
|
|
|
// try to extract the scheme and the host
|
|
|
|
if ( preg_match( '/^([a-zA-Z^:]+):(.*)$/i', $resource, $match ) ) {
|
|
|
|
// extract the scheme
|
|
|
|
$scheme = esc_attr( $match[1] );
|
2023-05-24 16:32:00 +02:00
|
|
|
}
|
|
|
|
|
2023-12-12 17:10:52 +01:00
|
|
|
switch ( $scheme ) {
|
|
|
|
// check for http(s) URIs
|
|
|
|
case 'http':
|
|
|
|
case 'https':
|
|
|
|
$url_parts = wp_parse_url( $resource );
|
2023-05-24 16:32:00 +02:00
|
|
|
|
2023-12-12 17:10:52 +01:00
|
|
|
// check for http(s)://blog.example.com/@username
|
|
|
|
if (
|
|
|
|
isset( $url_parts['path'] ) &&
|
|
|
|
str_starts_with( $url_parts['path'], '/@' )
|
|
|
|
) {
|
|
|
|
$identifier = str_replace( '/@', '', $url_parts['path'] );
|
|
|
|
$identifier = untrailingslashit( $identifier );
|
2023-05-24 16:32:00 +02:00
|
|
|
|
2023-12-12 17:10:52 +01:00
|
|
|
return self::get_by_username( $identifier );
|
|
|
|
}
|
|
|
|
|
|
|
|
// check for http(s)://blog.example.com/author/username
|
|
|
|
$user_id = url_to_authorid( $resource );
|
|
|
|
|
|
|
|
if ( $user_id ) {
|
|
|
|
return self::get_by_id( $user_id );
|
|
|
|
}
|
|
|
|
|
|
|
|
// check for http(s)://blog.example.com/
|
|
|
|
if (
|
|
|
|
self::normalize_url( site_url() ) === self::normalize_url( $resource ) ||
|
|
|
|
self::normalize_url( home_url() ) === self::normalize_url( $resource )
|
|
|
|
) {
|
|
|
|
return self::get_by_id( self::BLOG_USER_ID );
|
|
|
|
}
|
|
|
|
|
|
|
|
return new WP_Error(
|
|
|
|
'activitypub_no_user_found',
|
|
|
|
\__( 'User not found', 'activitypub' ),
|
|
|
|
array( 'status' => 404 )
|
|
|
|
);
|
|
|
|
// check for acct URIs
|
|
|
|
case 'acct':
|
|
|
|
$resource = \str_replace( 'acct:', '', $resource );
|
|
|
|
$identifier = \substr( $resource, 0, \strrpos( $resource, '@' ) );
|
|
|
|
$host = self::normalize_host( \substr( \strrchr( $resource, '@' ), 1 ) );
|
|
|
|
$blog_host = self::normalize_host( \wp_parse_url( \home_url( '/' ), \PHP_URL_HOST ) );
|
2023-05-24 16:32:00 +02:00
|
|
|
|
2023-12-12 17:10:52 +01:00
|
|
|
if ( $blog_host !== $host ) {
|
|
|
|
return new WP_Error(
|
|
|
|
'activitypub_wrong_host',
|
|
|
|
\__( 'Resource host does not match blog host', 'activitypub' ),
|
|
|
|
array( 'status' => 404 )
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
// prepare wildcards https://github.com/mastodon/mastodon/issues/22213
|
|
|
|
if ( in_array( $identifier, array( '_', '*', '' ), true ) ) {
|
|
|
|
return self::get_by_id( self::BLOG_USER_ID );
|
|
|
|
}
|
|
|
|
|
|
|
|
return self::get_by_username( $identifier );
|
|
|
|
default:
|
|
|
|
return new WP_Error(
|
|
|
|
'activitypub_wrong_scheme',
|
|
|
|
\__( 'Wrong scheme', 'activitypub' ),
|
|
|
|
array( 'status' => 404 )
|
|
|
|
);
|
|
|
|
}
|
2023-05-24 16:32:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the User by resource.
|
|
|
|
*
|
|
|
|
* @param string $resource The User-Resource.
|
|
|
|
*
|
|
|
|
* @return \Acitvitypub\Model\User The User.
|
|
|
|
*/
|
|
|
|
public static function get_by_various( $id ) {
|
|
|
|
if ( is_numeric( $id ) ) {
|
|
|
|
return self::get_by_id( $id );
|
2023-12-12 17:10:52 +01:00
|
|
|
} elseif (
|
|
|
|
// is URL
|
|
|
|
filter_var( $id, FILTER_VALIDATE_URL ) ||
|
|
|
|
// is acct
|
|
|
|
str_starts_with( $id, 'acct:' )
|
|
|
|
) {
|
2023-05-24 16:32:00 +02:00
|
|
|
return self::get_by_resource( $id );
|
|
|
|
} else {
|
|
|
|
return self::get_by_username( $id );
|
|
|
|
}
|
|
|
|
}
|
2023-06-21 15:46:34 +02:00
|
|
|
|
|
|
|
/**
|
2023-12-12 17:10:52 +01:00
|
|
|
* Normalize a host.
|
2023-06-21 15:46:34 +02:00
|
|
|
*
|
|
|
|
* @param string $host The host.
|
|
|
|
*
|
|
|
|
* @return string The normalized host.
|
|
|
|
*/
|
|
|
|
public static function normalize_host( $host ) {
|
|
|
|
return \str_replace( 'www.', '', $host );
|
|
|
|
}
|
2023-08-09 13:07:30 +02:00
|
|
|
|
2023-12-12 17:10:52 +01:00
|
|
|
/**
|
|
|
|
* Normalize a URL.
|
|
|
|
*
|
|
|
|
* @param string $url The URL.
|
|
|
|
*
|
|
|
|
* @return string The normalized URL.
|
|
|
|
*/
|
|
|
|
public static function normalize_url( $url ) {
|
|
|
|
$url = \untrailingslashit( $url );
|
|
|
|
$url = \str_replace( 'https://', '', $url );
|
|
|
|
$url = \str_replace( 'http://', '', $url );
|
|
|
|
$url = \str_replace( 'www.', '', $url );
|
|
|
|
|
|
|
|
return $url;
|
|
|
|
}
|
|
|
|
|
2023-08-09 13:07:30 +02:00
|
|
|
/**
|
|
|
|
* Get the User collection.
|
|
|
|
*
|
|
|
|
* @return array The User collection.
|
|
|
|
*/
|
|
|
|
public static function get_collection() {
|
|
|
|
$users = \get_users(
|
|
|
|
array(
|
|
|
|
'capability__in' => array( 'publish_posts' ),
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
$return = array();
|
|
|
|
|
|
|
|
foreach ( $users as $user ) {
|
|
|
|
$return[] = User::from_wp_user( $user->ID );
|
|
|
|
}
|
|
|
|
|
|
|
|
return $return;
|
|
|
|
}
|
2023-05-24 16:32:00 +02:00
|
|
|
}
|