*/ public static function all() { $data = Secrets::get_option( self::OPTION, array() ); return isset( $data['connections'] ) && is_array( $data['connections'] ) ? $data['connections'] : array(); } /** * One connection or null. * * @param string $id Connection ID. * @return array|null */ public static function get( $id ) { $all = self::all(); return isset( $all[ $id ] ) ? $all[ $id ] : null; } /** * Parses a "srk1." blob. Returns the decoded parts or a WP_Error. * * @param string $blob Blob. * @return array|\WP_Error */ public static function parse_blob( $blob ) { $blob = trim( (string) $blob ); if ( 0 !== strpos( $blob, 'srk1.' ) ) { return new \WP_Error( 'bad_blob', __( 'That is not a Site Replica connection key. It should start with "srk1."', 'accessnow-site-replica' ) ); } $json = Secrets::b64url_decode( substr( $blob, 5 ) ); $data = false === $json ? null : json_decode( $json, true ); if ( ! is_array( $data ) || empty( $data['u'] ) || empty( $data['k'] ) || empty( $data['s'] ) ) { return new \WP_Error( 'bad_blob', __( 'The connection key is incomplete. Copy the whole key again.', 'accessnow-site-replica' ) ); } $secret = Secrets::b64url_decode( $data['s'] ); if ( false === $secret || 32 !== strlen( $secret ) ) { return new \WP_Error( 'bad_blob', __( 'The connection key is damaged. Copy it again.', 'accessnow-site-replica' ) ); } $url = esc_url_raw( (string) $data['u'], array( 'http', 'https' ) ); if ( '' === $url ) { return new \WP_Error( 'bad_blob', __( 'The connection key has an invalid site address.', 'accessnow-site-replica' ) ); } return array( 'url' => untrailingslashit( $url ), 'kid' => (string) $data['k'], 'secret' => (string) $data['s'], 'expires' => (int) ( $data['e'] ?? 0 ), 'name' => sanitize_text_field( (string) ( $data['n'] ?? '' ) ), 'endpoints' => self::clean_endpoints( array( 'ajax' => $data['a'] ?? null, 'rest' => $data['r'] ?? null, ) ), ); } /** * The source's own API addresses (from its key or its hello answer), validated. * * @param mixed $raw {ajax, rest} as received. * @return array Only the ones that are usable http(s) URLs. */ public static function clean_endpoints( $raw ) { $out = array(); if ( ! is_array( $raw ) ) { return $out; } foreach ( array( 'ajax', 'rest' ) as $name ) { if ( isset( $raw[ $name ] ) && is_string( $raw[ $name ] ) ) { $url = esc_url_raw( $raw[ $name ], array( 'http', 'https' ) ); if ( '' !== $url ) { $out[ $name ] = $url; } } } return $out; } /** * Saves a connection (new or updated) and returns its ID. * * @param array $conn Connection data. */ public static function save( array $conn ) { $all = self::all(); if ( empty( $conn['id'] ) ) { // One connection per site: a new key for a site already connected replaces the old // key and keeps the connection's history (last pull, working URL shape). $id = self::find_by_url( (string) $conn['url'] ); if ( null !== $id ) { $conn['id'] = $id; } } if ( empty( $conn['id'] ) ) { $conn['id'] = 'c' . strtolower( wp_generate_password( 8, false, false ) ); $conn['created'] = time(); } $all[ $conn['id'] ] = array_merge( isset( $all[ $conn['id'] ] ) ? $all[ $conn['id'] ] : array(), $conn ); Secrets::update_option( self::OPTION, array( 'connections' => $all ) ); return $conn['id']; } /** * ID of the connection to a site, or null. * * @param string $url Site URL. * @return string|null */ public static function find_by_url( $url ) { foreach ( self::all() as $id => $existing ) { if ( untrailingslashit( (string) $existing['url'] ) === untrailingslashit( $url ) ) { return (string) $id; } } return null; } /** * Updates some fields of a connection. * * @param string $id ID. * @param array $fields Fields. */ public static function update( $id, array $fields ) { $all = self::all(); if ( isset( $all[ $id ] ) ) { $all[ $id ] = array_merge( $all[ $id ], $fields ); Secrets::update_option( self::OPTION, array( 'connections' => $all ) ); } } /** * Removes a connection. * * @param string $id ID. */ public static function delete( $id ) { $all = self::all(); if ( isset( $all[ $id ] ) ) { Logger::activity( 'pull', 'Connection removed: ' . $all[ $id ]['url'] ); unset( $all[ $id ] ); Secrets::update_option( self::OPTION, array( 'connections' => $all ) ); } } }