Skip to content

Commit

Permalink
Remove manifest support as experimental and clean up discovery code.
Browse files Browse the repository at this point in the history
  • Loading branch information
dshanske committed Jun 29, 2024
1 parent 8e4e2b5 commit 9b209f7
Showing 1 changed file with 30 additions and 47 deletions.
77 changes: 30 additions & 47 deletions includes/class-indieauth-client-discovery.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

class IndieAuth_Client_Discovery {
protected $rels = array();
protected $manifest = array();
protected $html = array();
protected $mf2 = array();
protected $json = array();
Expand All @@ -25,7 +24,11 @@ public function __construct( $client_id ) {
);

// If this is an IP address on the donotfetch list then do not fetch.
if ( ( $ip && ! in_array( $ip, $donotfetch, true ) || 'localhost' === wp_parse_url( $client_id, PHP_URL_HOST ) ) ) {
if ( $ip && ! in_array( $ip, $donotfetch, true ) ) {
return;
}

if ( 'localhost' === wp_parse_url( $client_id, PHP_URL_HOST ) ) {
return;
}

Expand All @@ -38,10 +41,10 @@ public function __construct( $client_id ) {

public function export() {
return array(
'manifest' => $this->manifest,
'rels' => $this->rels,
'mf2' => $this->mf2,
'html' => $this->html,
'json' => $this->json,
'client_id' => $this->client_id,
'client_name' => $this->client_name,
'client_icon' => $this->client_icon,
Expand Down Expand Up @@ -77,36 +80,29 @@ private function parse( $url ) {

$content_type = wp_remote_retrieve_header( $response, 'content-type' );
if ( 'application/json' === $content_type ) {
$json = json_decode( wp_remote_retrieve_body( $response ), true );
if ( ! is_array( $json ) && empty( $json ) ) {
return;
$this->json = json_decode( wp_remote_retrieve_body( $response ), true );
if ( ! is_array( $this->json ) || empty( $this->json ) ) {
return new WP_Error( 'empty_json', __( 'Discovery Has Returned an Empty JSON Document', 'indieauth' ) );
}
if ( ! array_key_exists( 'client_id', $json ) ) {
return;
if ( ! array_key_exists( 'client_id', $this->json ) ) {
return new WP_Error( 'missing_client_id', __( 'No Client ID Found in JSON Client Metadata', 'indieauth' ) );
}
$this->client_id = $json['client_id'];
if ( array_key_exists( 'client_name', $json ) ) {
$this->client_name = $json['client_name'];
$this->client_id = $this->json['client_id'];
if ( array_key_exists( 'client_name', $this->json ) ) {
$this->client_name = $this->json['client_name'];
}
if ( array_key_exists( 'logo_uri', $json ) ) {
$this->client_icon = $json['logo_uri'];
if ( array_key_exists( 'logo_uri', $this->json ) ) {
$this->client_icon = $this->json['logo_uri'];
}
} elseif ( 'text/html' === $content_type ) {
$content = wp_remote_retrieve_body( $response );

if ( class_exists( 'Masterminds\\HTML5' ) ) {
$domdocument = new \Masterminds\HTML5( array( 'disable_html_ns' => true ) );
$domdocument = $domdocument->loadHTML( $content );
} else {
$domdocument = new DOMDocument();
libxml_use_internal_errors( true );
if ( function_exists( 'mb_convert_encoding' ) ) {
$content = mb_convert_encoding( $content, 'HTML-ENTITIES', mb_detect_encoding( $content ) );
}
$domdocument->loadHTML( $content );
libxml_use_internal_errors( false );
$content = wp_remote_retrieve_body( $response );
$domdocument = new DOMDocument();
libxml_use_internal_errors( true );
if ( function_exists( 'mb_convert_encoding' ) ) {
$content = mb_convert_encoding( $content, 'HTML-ENTITIES', mb_detect_encoding( $content ) );
}

$domdocument->loadHTML( $content );
libxml_use_internal_errors( false );
$this->get_mf2( $domdocument, $url );
if ( ! empty( $this->mf2 ) ) {
if ( array_key_exists( 'name', $this->mf2 ) ) {
Expand All @@ -119,10 +115,6 @@ private function parse( $url ) {
$this->client_icon = $this->mf2['logo'][0]['value'];
}
}
} elseif ( isset( $this->rels['manifest'] ) ) {
self::get_manifest( $this->rels['manifest'] );
$this->client_icon = $this->determine_icon( $this->manifest );
$this->client_name = $this->manifest['name'];
} else {
$this->client_icon = $this->determine_icon( $this->rels );
$this->get_html( $domdocument );
Expand All @@ -141,7 +133,7 @@ private function get_mf2( $input, $url ) {
}
$mf = Mf2\parse( $input, $url );
if ( array_key_exists( 'rels', $mf ) ) {
$this->rels = wp_array_slice_assoc( $mf['rels'], array( 'apple-touch-icon', 'icon', 'mask-icon', 'manifest' ) );
$this->rels = wp_array_slice_assoc( $mf['rels'], array( 'apple-touch-icon', 'icon', 'mask-icon' ) );
}
if ( array_key_exists( 'items', $mf ) ) {
foreach ( $mf['items'] as $item ) {
Expand All @@ -153,23 +145,14 @@ private function get_mf2( $input, $url ) {
}
}

private function get_manifest( $url ) {
if ( is_array( $url ) ) {
$url = $url[0];
}
$response = self::fetch( $url );
if ( is_wp_error( $response ) ) {
return $response;
}
$this->manifest = json_decode( wp_remote_retrieve_body( $response ), true );
}

private function get_html( $input ) {
if ( ! $input ) {
return;
}
$xpath = new DOMXPath( $input );
$this->html['title'] = $xpath->query( '//title' )->item( 0 )->textContent;
if ( ! empty( $xpath ) ) {
$title = $xpath->query( '//title' );
if ( ! empty( $title ) ) {
$this->html['title'] = $title->item( 0 )->textContent;
}
}
}

private function ifset( $array, $key, $default = false ) {
Expand Down

0 comments on commit 9b209f7

Please sign in to comment.