WP_REST_Server::READABLE, 'callback' => array( $this, 'handle' ), 'permission_callback' => array( 'MYBOT_REST_Controller', 'admin_permission' ), 'args' => array( 'message_id' => array( 'required' => true, 'type' => 'integer', 'sanitize_callback' => 'absint', ), 'index' => array( 'required' => false, 'type' => 'integer', 'default' => 0, 'sanitize_callback' => 'absint', ), ), ) ); add_filter( 'rest_pre_serve_request', array( $this, 'serve' ), 10, 3 ); } /** * Resolves a request to a stored picture, without reading it yet. * * @param WP_REST_Request $request Request. * @return WP_REST_Response|WP_Error */ public function handle( $request ) { $message_id = absint( $request->get_param( 'message_id' ) ); $index = absint( $request->get_param( 'index' ) ); $record = MYBOT_Attachments::record( mybot()->logger->attachments_of( $message_id ), $index ); if ( empty( $record ) ) { return new WP_Error( 'mybot_attachment_missing', __( 'That message has no picture at this position.', 'aihouse' ), array( 'status' => 404 ) ); } $path = MYBOT_Attachments::path( (string) $record['file'] ); if ( '' === $path || ! file_exists( $path ) ) { /* * The row says there was a picture and the disk says otherwise. Say so plainly rather than * answering an empty image: a broken thumbnail is a mystery, a 404 is a fact. */ return new WP_Error( 'mybot_attachment_gone', __( 'That picture is no longer on the server.', 'aihouse' ), array( 'status' => 404 ) ); } $response = new WP_REST_Response( array( 'mybot_attachment' => array( 'file' => (string) $record['file'], 'mime' => (string) $record['mime'], ), ) ); /* * `nosniff` matters more here than usual: the content type came from a record in the database, * and a browser that decides for itself what a file is must never be given the chance. */ $response->header( 'Content-Type', (string) $record['mime'] ); $response->header( 'Content-Length', (string) filesize( $path ) ); $response->header( 'X-Content-Type-Options', 'nosniff' ); $response->header( 'Content-Disposition', 'inline' ); $response->header( 'Cache-Control', 'private, no-store, max-age=0' ); $response->header( 'X-Robots-Tag', 'noindex, nofollow' ); return $response; } /** * Writes the picture into the response. * * @param bool $served Whether the request has already been served. * @param WP_HTTP_Response $result The response the callback returned. * @param WP_REST_Request $request Request. * @return bool True when the bytes have been written here. */ public function serve( $served, $result, $request ) { if ( $served || ! $result instanceof WP_REST_Response ) { return $served; } if ( '/' . MYBOT_REST_NAMESPACE . self::ROUTE !== $request->get_route() ) { return $served; } $data = $result->get_data(); if ( ! is_array( $data ) || empty( $data['mybot_attachment']['file'] ) ) { return $served; } /* * Validated a second time, on purpose. This is the last step before bytes leave the server, and * `path()` is the only thing that decides what a file name may be — the same rule the store path * used when the file was written. */ $path = MYBOT_Attachments::path( (string) $data['mybot_attachment']['file'] ); if ( '' === $path || ! file_exists( $path ) ) { return $served; } // A HEAD request gets the headers above and no body; reading the file for one would be waste. if ( 'HEAD' === $request->get_method() ) { return true; } // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_readfile readfile( $path ); return true; } }