| 1 |
<?php |
| 2 |
namespace Activitypub\Collection; |
| 3 |
|
| 4 |
use WP_Error; |
| 5 |
use WP_User_Query; |
| 6 |
use Activitypub\Model\User; |
| 7 |
use Activitypub\Model\Blog_User; |
| 8 |
use Activitypub\Model\Application_User; |
| 9 |
|
| 10 |
use function Activitypub\is_user_disabled; |
| 11 |
|
| 12 |
class Users { |
| 13 |
/** |
| 14 |
* The ID of the Blog User |
| 15 |
* |
| 16 |
* @var int |
| 17 |
*/ |
| 18 |
const BLOG_USER_ID = 0; |
| 19 |
|
| 20 |
/** |
| 21 |
* The ID of the Application User |
| 22 |
* |
| 23 |
* @var int |
| 24 |
*/ |
| 25 |
const APPLICATION_USER_ID = -1; |
| 26 |
|
| 27 |
/** |
| 28 |
* Get the User by ID |
| 29 |
* |
| 30 |
* @param int $user_id The User-ID. |
| 31 |
* |
| 32 |
* @return \Acitvitypub\Model\User The User. |
| 33 |
*/ |
| 34 |
public static function get_by_id( $user_id ) { |
| 35 |
if ( is_string( $user_id ) || is_numeric( $user_id ) ) { |
| 36 |
$user_id = (int) $user_id; |
| 37 |
} |
| 38 |
|
| 39 |
if ( is_user_disabled( $user_id ) ) { |
| 40 |
return new WP_Error( |
| 41 |
'activitypub_user_not_found', |
| 42 |
\__( 'User not found', 'activitypub' ), |
| 43 |
array( 'status' => 404 ) |
| 44 |
); |
| 45 |
} |
| 46 |
|
| 47 |
if ( self::BLOG_USER_ID === $user_id ) { |
| 48 |
return Blog_User::from_wp_user( $user_id ); |
| 49 |
} elseif ( self::APPLICATION_USER_ID === $user_id ) { |
| 50 |
return Application_User::from_wp_user( $user_id ); |
| 51 |
} elseif ( $user_id > 0 ) { |
| 52 |
return User::from_wp_user( $user_id ); |
| 53 |
} |
| 54 |
|
| 55 |
return new WP_Error( |
| 56 |
'activitypub_user_not_found', |
| 57 |
\__( 'User not found', 'activitypub' ), |
| 58 |
array( 'status' => 404 ) |
| 59 |
); |
| 60 |
} |
| 61 |
|
| 62 |
/** |
| 63 |
* Get the User by username. |
| 64 |
* |
| 65 |
* @param string $username The User-Name. |
| 66 |
* |
| 67 |
* @return \Acitvitypub\Model\User The User. |
| 68 |
*/ |
| 69 |
public static function get_by_username( $username ) { |
| 70 |
// check for blog user. |
| 71 |
if ( Blog_User::get_default_username() === $username ) { |
| 72 |
return self::get_by_id( self::BLOG_USER_ID ); |
| 73 |
} |
| 74 |
|
| 75 |
if ( get_option( 'activitypub_blog_user_identifier' ) === $username ) { |
| 76 |
return self::get_by_id( self::BLOG_USER_ID ); |
| 77 |
} |
| 78 |
|
| 79 |
// check for application user. |
| 80 |
if ( 'application' === $username ) { |
| 81 |
return self::get_by_id( self::APPLICATION_USER_ID ); |
| 82 |
} |
| 83 |
|
| 84 |
// check for 'activitypub_username' meta |
| 85 |
$user = new WP_User_Query( |
| 86 |
array( |
| 87 |
'number' => 1, |
| 88 |
'hide_empty' => true, |
| 89 |
'fields' => 'ID', |
| 90 |
// phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_query |
| 91 |
'meta_query' => array( |
| 92 |
'relation' => 'OR', |
| 93 |
array( |
| 94 |
'key' => 'activitypub_user_identifier', |
| 95 |
'value' => $username, |
| 96 |
'compare' => 'LIKE', |
| 97 |
), |
| 98 |
), |
| 99 |
) |
| 100 |
); |
| 101 |
|
| 102 |
if ( $user->results ) { |
| 103 |
return self::get_by_id( $user->results[0] ); |
| 104 |
} |
| 105 |
|
| 106 |
// check for login or nicename. |
| 107 |
$user = new WP_User_Query( |
| 108 |
array( |
| 109 |
'search' => $username, |
| 110 |
'search_columns' => array( 'user_login', 'user_nicename' ), |
| 111 |
'number' => 1, |
| 112 |
'hide_empty' => true, |
| 113 |
'fields' => 'ID', |
| 114 |
) |
| 115 |
); |
| 116 |
|
| 117 |
if ( $user->results ) { |
| 118 |
return self::get_by_id( $user->results[0] ); |
| 119 |
} |
| 120 |
|
| 121 |
return new WP_Error( |
| 122 |
'activitypub_user_not_found', |
| 123 |
\__( 'User not found', 'activitypub' ), |
| 124 |
array( 'status' => 404 ) |
| 125 |
); |
| 126 |
} |
| 127 |
|
| 128 |
/** |
| 129 |
* Get the User by resource. |
| 130 |
* |
| 131 |
* @param string $resource The User-Resource. |
| 132 |
* |
| 133 |
* @return \Acitvitypub\Model\User The User. |
| 134 |
*/ |
| 135 |
public static function get_by_resource( $resource ) { |
| 136 |
if ( \strpos( $resource, '@' ) === false ) { |
| 137 |
return new WP_Error( |
| 138 |
'activitypub_unsupported_resource', |
| 139 |
\__( 'Resource is invalid', 'activitypub' ), |
| 140 |
array( 'status' => 400 ) |
| 141 |
); |
| 142 |
} |
| 143 |
|
| 144 |
$resource = \str_replace( 'acct:', '', $resource ); |
| 145 |
|
| 146 |
$resource_identifier = \substr( $resource, 0, \strrpos( $resource, '@' ) ); |
| 147 |
$resource_host = self::normalize_host( \substr( \strrchr( $resource, '@' ), 1 ) ); |
| 148 |
$blog_host = self::normalize_host( \wp_parse_url( \home_url( '/' ), \PHP_URL_HOST ) ); |
| 149 |
|
| 150 |
if ( $blog_host !== $resource_host ) { |
| 151 |
return new WP_Error( |
| 152 |
'activitypub_wrong_host', |
| 153 |
\__( 'Resource host does not match blog host', 'activitypub' ), |
| 154 |
array( 'status' => 404 ) |
| 155 |
); |
| 156 |
} |
| 157 |
|
| 158 |
return self::get_by_username( $resource_identifier ); |
| 159 |
} |
| 160 |
|
| 161 |
/** |
| 162 |
* Get the User by resource. |
| 163 |
* |
| 164 |
* @param string $resource The User-Resource. |
| 165 |
* |
| 166 |
* @return \Acitvitypub\Model\User The User. |
| 167 |
*/ |
| 168 |
public static function get_by_various( $id ) { |
| 169 |
if ( is_numeric( $id ) ) { |
| 170 |
return self::get_by_id( $id ); |
| 171 |
} elseif ( filter_var( $id, FILTER_VALIDATE_URL ) ) { |
| 172 |
return self::get_by_resource( $id ); |
| 173 |
} else { |
| 174 |
return self::get_by_username( $id ); |
| 175 |
} |
| 176 |
} |
| 177 |
|
| 178 |
/** |
| 179 |
* Normalize the host. |
| 180 |
* |
| 181 |
* @param string $host The host. |
| 182 |
* |
| 183 |
* @return string The normalized host. |
| 184 |
*/ |
| 185 |
public static function normalize_host( $host ) { |
| 186 |
return \str_replace( 'www.', '', $host ); |
| 187 |
} |
| 188 |
|
| 189 |
/** |
| 190 |
* Get the User collection. |
| 191 |
* |
| 192 |
* @return array The User collection. |
| 193 |
*/ |
| 194 |
public static function get_collection() { |
| 195 |
$users = \get_users( |
| 196 |
array( |
| 197 |
'capability__in' => array( 'publish_posts' ), |
| 198 |
) |
| 199 |
); |
| 200 |
|
| 201 |
$return = array(); |
| 202 |
|
| 203 |
foreach ( $users as $user ) { |
| 204 |
$return[] = User::from_wp_user( $user->ID ); |
| 205 |
} |
| 206 |
|
| 207 |
return $return; |
| 208 |
} |
| 209 |
} |
| 210 |
|