|  | 
|  | 1 | +<?php | 
|  | 2 | +/** | 
|  | 3 | + * WebFinger REST-Class file. | 
|  | 4 | + * | 
|  | 5 | + * @package Activitypub | 
|  | 6 | + */ | 
|  | 7 | + | 
|  | 8 | +namespace Activitypub\Rest; | 
|  | 9 | + | 
|  | 10 | +/** | 
|  | 11 | + * ActivityPub WebFinger REST-Class. | 
|  | 12 | + * | 
|  | 13 | + * @author Matthias Pfefferle | 
|  | 14 | + * | 
|  | 15 | + * @see https://webfinger.net/ | 
|  | 16 | + */ | 
|  | 17 | +class Webfinger_Controller extends \WP_REST_Controller { | 
|  | 18 | +	/** | 
|  | 19 | +	 * The namespace of this controller's route. | 
|  | 20 | +	 * | 
|  | 21 | +	 * @var string | 
|  | 22 | +	 */ | 
|  | 23 | +	protected $namespace = ACTIVITYPUB_REST_NAMESPACE; | 
|  | 24 | + | 
|  | 25 | +	/** | 
|  | 26 | +	 * The base of this controller's route. | 
|  | 27 | +	 * | 
|  | 28 | +	 * @var string | 
|  | 29 | +	 */ | 
|  | 30 | +	protected $rest_base = 'webfinger'; | 
|  | 31 | + | 
|  | 32 | +	/** | 
|  | 33 | +	 * Register routes. | 
|  | 34 | +	 */ | 
|  | 35 | +	public function register_routes() { | 
|  | 36 | +		\register_rest_route( | 
|  | 37 | +			$this->namespace, | 
|  | 38 | +			'/' . $this->rest_base, | 
|  | 39 | +			array( | 
|  | 40 | +				array( | 
|  | 41 | +					'methods'             => \WP_REST_Server::READABLE, | 
|  | 42 | +					'callback'            => array( $this, 'get_item' ), | 
|  | 43 | +					'permission_callback' => '__return_true', | 
|  | 44 | +					'args'                => array( | 
|  | 45 | +						'resource' => array( | 
|  | 46 | +							'description' => 'The WebFinger resource.', | 
|  | 47 | +							'type'        => 'string', | 
|  | 48 | +							'required'    => true, | 
|  | 49 | +							'pattern'     => '^(acct:)|^(https?://)(.+)$', | 
|  | 50 | +						), | 
|  | 51 | +					), | 
|  | 52 | +				), | 
|  | 53 | +				'schema' => array( $this, 'get_item_schema' ), | 
|  | 54 | +			) | 
|  | 55 | +		); | 
|  | 56 | +	} | 
|  | 57 | + | 
|  | 58 | +	/** | 
|  | 59 | +	 * Retrieves the WebFinger profile. | 
|  | 60 | +	 * | 
|  | 61 | +	 * @param \WP_REST_Request $request The request object. | 
|  | 62 | +	 * | 
|  | 63 | +	 * @return \WP_REST_Response Response object. | 
|  | 64 | +	 */ | 
|  | 65 | +	public function get_item( $request ) { | 
|  | 66 | +		/** | 
|  | 67 | +		 * Action triggered prior to the ActivityPub profile being created and sent to the client. | 
|  | 68 | +		 */ | 
|  | 69 | +		\do_action( 'activitypub_rest_webfinger_pre' ); | 
|  | 70 | + | 
|  | 71 | +		$resource = $request->get_param( 'resource' ); | 
|  | 72 | +		$response = $this->get_profile( $resource ); | 
|  | 73 | +		$code     = 200; | 
|  | 74 | + | 
|  | 75 | +		if ( \is_wp_error( $response ) ) { | 
|  | 76 | +			$code       = 400; | 
|  | 77 | +			$error_data = $response->get_error_data(); | 
|  | 78 | + | 
|  | 79 | +			if ( isset( $error_data['status'] ) ) { | 
|  | 80 | +				$code = $error_data['status']; | 
|  | 81 | +			} | 
|  | 82 | +		} | 
|  | 83 | + | 
|  | 84 | +		return new \WP_REST_Response( | 
|  | 85 | +			$response, | 
|  | 86 | +			$code, | 
|  | 87 | +			array( | 
|  | 88 | +				'Access-Control-Allow-Origin' => '*', | 
|  | 89 | +				'Content-Type'                => 'application/jrd+json; charset=' . \get_option( 'blog_charset' ), | 
|  | 90 | +			) | 
|  | 91 | +		); | 
|  | 92 | +	} | 
|  | 93 | + | 
|  | 94 | +	/** | 
|  | 95 | +	 * Get the WebFinger profile. | 
|  | 96 | +	 * | 
|  | 97 | +	 * @param string $webfinger The WebFinger resource. | 
|  | 98 | +	 * | 
|  | 99 | +	 * @return array|\WP_Error The WebFinger profile or WP_Error if not found. | 
|  | 100 | +	 */ | 
|  | 101 | +	public function get_profile( $webfinger ) { | 
|  | 102 | +		/** | 
|  | 103 | +		 * Filter the WebFinger data. | 
|  | 104 | +		 * | 
|  | 105 | +		 * @param array  $data      The WebFinger data. | 
|  | 106 | +		 * @param string $webfinger The WebFinger resource. | 
|  | 107 | +		 */ | 
|  | 108 | +		return \apply_filters( 'webfinger_data', array(), $webfinger ); | 
|  | 109 | +	} | 
|  | 110 | + | 
|  | 111 | +	/** | 
|  | 112 | +	 * Retrieves the schema for the WebFinger endpoint. | 
|  | 113 | +	 * | 
|  | 114 | +	 * @return array Schema data. | 
|  | 115 | +	 */ | 
|  | 116 | +	public function get_item_schema() { | 
|  | 117 | +		if ( $this->schema ) { | 
|  | 118 | +			return $this->add_additional_fields_schema( $this->schema ); | 
|  | 119 | +		} | 
|  | 120 | + | 
|  | 121 | +		$this->schema = array( | 
|  | 122 | +			'$schema'    => 'http://json-schema.org/draft-04/schema#', | 
|  | 123 | +			'title'      => 'webfinger', | 
|  | 124 | +			'type'       => 'object', | 
|  | 125 | +			'required'   => array( 'subject', 'links' ), | 
|  | 126 | +			'properties' => array( | 
|  | 127 | +				'subject' => array( | 
|  | 128 | +					'description' => 'The subject of this WebFinger record.', | 
|  | 129 | +					'type'        => 'string', | 
|  | 130 | +					'format'      => 'uri', | 
|  | 131 | +				), | 
|  | 132 | +				'aliases' => array( | 
|  | 133 | +					'description' => 'Alternative identifiers for the subject.', | 
|  | 134 | +					'type'        => 'array', | 
|  | 135 | +					'items'       => array( | 
|  | 136 | +						'type'   => 'string', | 
|  | 137 | +						'format' => 'uri', | 
|  | 138 | +					), | 
|  | 139 | +				), | 
|  | 140 | +				'links'   => array( | 
|  | 141 | +					'description' => 'Links associated with the subject.', | 
|  | 142 | +					'type'        => 'array', | 
|  | 143 | +					'items'       => array( | 
|  | 144 | +						'type'       => 'object', | 
|  | 145 | +						'properties' => array( | 
|  | 146 | +							'rel'      => array( | 
|  | 147 | +								'description' => 'The relation type of the link.', | 
|  | 148 | +								'type'        => 'string', | 
|  | 149 | +								'required'    => true, | 
|  | 150 | +							), | 
|  | 151 | +							'type'     => array( | 
|  | 152 | +								'description' => 'The content type of the link.', | 
|  | 153 | +								'type'        => 'string', | 
|  | 154 | +							), | 
|  | 155 | +							'href'     => array( | 
|  | 156 | +								'description' => 'The target URL of the link.', | 
|  | 157 | +								'type'        => 'string', | 
|  | 158 | +								'format'      => 'uri', | 
|  | 159 | +							), | 
|  | 160 | +							'template' => array( | 
|  | 161 | +								'description' => 'A URI template for the link.', | 
|  | 162 | +								'type'        => 'string', | 
|  | 163 | +								'format'      => 'uri', | 
|  | 164 | +							), | 
|  | 165 | +						), | 
|  | 166 | +					), | 
|  | 167 | +				), | 
|  | 168 | +			), | 
|  | 169 | +		); | 
|  | 170 | + | 
|  | 171 | +		return $this->add_additional_fields_schema( $this->schema ); | 
|  | 172 | +	} | 
|  | 173 | +} | 
0 commit comments