hash( 'sha256', $token ), 'user' => get_current_user_id(), 'expires' => time() + self::TTL, 'alt' => sanitize_text_field( (string) $alt_text ), 'status' => 'pending', ), self::TTL ); return array( 'upload_id' => $id, 'upload_url' => home_url( '/' . self::PREFIX . '/' . $token ), 'expires_at' => gmdate( 'c', time() + self::TTL ), 'max_bytes' => self::max_bytes(), ); } /** Status of one of the current user's links. */ public static function status( $upload_id ) { $data = get_transient( self::key( preg_replace( '/[^a-f0-9]/', '', (string) $upload_id ) ) ); if ( ! is_array( $data ) || (int) $data['user'] !== get_current_user_id() ) { return new WP_Error( 'agent_publisher_upload_unknown', 'No such upload link (it may have expired). Call request-upload for a new one.', array( 'status' => 404 ) ); } if ( 'done' === $data['status'] ) { return array( 'status' => 'done', 'image' => Agent_Publisher_Abilities::image_info( (int) $data['attachment'] ) + array( 'warnings' => Agent_Publisher_Policy::image_warnings( (int) $data['attachment'] ) ) ); } return array( 'status' => time() > (int) $data['expires'] ? 'expired' : 'pending' ); } public static function max_bytes() { return (int) apply_filters( 'agent_publisher_max_link_upload', wp_max_upload_size() ); } /* ------------------------------------------------------------------ */ /* The upload page */ /* ------------------------------------------------------------------ */ public static function route( WP $wp ) { $path = trim( (string) $wp->request, '/' ); if ( 0 !== strpos( $path, self::PREFIX . '/' ) ) { return; } $token = substr( $path, strlen( self::PREFIX ) + 1 ); $data = preg_match( '/^[a-f0-9]{64}$/', $token ) ? get_transient( self::key( self::upload_id( $token ) ) ) : false; $valid = is_array( $data ) && hash_equals( (string) $data['hash'], hash( 'sha256', $token ) ); $user = $valid ? get_userdata( (int) $data['user'] ) : false; $post = 'POST' === ( isset( $_SERVER['REQUEST_METHOD'] ) ? strtoupper( sanitize_key( wp_unslash( $_SERVER['REQUEST_METHOD'] ) ) ) : 'GET' ); if ( ! $valid || ! $user ) { self::fail( $post, __( 'This upload link is not valid. Ask Claude for a new one.', 'acadium-agent-publisher' ), 404 ); } if ( 'done' === $data['status'] ) { self::fail( $post, __( 'This upload link has already been used. Ask Claude for a new one to add another image.', 'acadium-agent-publisher' ), 410 ); } if ( time() > (int) $data['expires'] ) { self::fail( $post, __( 'This upload link has expired. Ask Claude for a new one.', 'acadium-agent-publisher' ), 410 ); } if ( ! user_can( $user, 'upload_files' ) || ! Agent_Publisher_Policy::can_connect( $user ) ) { self::fail( $post, __( 'Your WordPress account can\'t upload files on this site.', 'acadium-agent-publisher' ), 403 ); } if ( $post ) { self::handle_upload( $token, $data, $user ); } self::page( $data, $user ); } /** * Authenticated by the single-use secret in the URL (a capability URL), * so there is no login or nonce; the file is checked like any upload. */ private static function handle_upload( $token, array $data, WP_User $user ) { // phpcs:disable WordPress.Security.NonceVerification.Missing -- capability URL: the secret token in the path authenticates this request (see class comment). if ( empty( $_FILES['file']['tmp_name'] ) || ! empty( $_FILES['file']['error'] ) ) { $too_big = ! empty( $_FILES['file']['error'] ) && in_array( (int) $_FILES['file']['error'], array( UPLOAD_ERR_INI_SIZE, UPLOAD_ERR_FORM_SIZE ), true ); self::json( array( 'error' => $too_big ? self::too_big_message() : __( 'No file was received. Try again.', 'acadium-agent-publisher' ) ), $too_big ? 413 : 400 ); } if ( ( isset( $_FILES['file']['size'] ) ? (int) $_FILES['file']['size'] : 0 ) > self::max_bytes() ) { self::json( array( 'error' => self::too_big_message() ), 413 ); } require_once ABSPATH . 'wp-admin/includes/file.php'; require_once ABSPATH . 'wp-admin/includes/media.php'; require_once ABSPATH . 'wp-admin/includes/image.php'; // The upload belongs to the person who asked Claude for the link. wp_set_current_user( $user->ID ); $id = media_handle_upload( 'file', 0, array(), array( 'test_form' => false, 'mimes' => Agent_Publisher_Abilities::upload_mimes(), ) ); if ( is_wp_error( $id ) ) { self::json( array( 'error' => $id->get_error_message() ), 400 ); } $alt = isset( $_POST['alt'] ) ? sanitize_text_field( wp_unslash( $_POST['alt'] ) ) : $data['alt']; update_post_meta( $id, '_wp_attachment_image_alt', $alt ); update_post_meta( $id, '_agent_publisher_source', 'upload_link' ); // phpcs:enable WordPress.Security.NonceVerification.Missing $data['status'] = 'done'; $data['attachment'] = (int) $id; set_transient( self::key( self::upload_id( $token ) ), $data, self::RESULT_TTL ); Agent_Publisher_Policy::log( 'upload', 0, wp_get_attachment_url( $id ) ); $meta = wp_get_attachment_metadata( $id ); self::json( array( 'id' => (int) $id, 'width' => (int) ( $meta['width'] ?? 0 ), 'height' => (int) ( $meta['height'] ?? 0 ), ) ); } private static function too_big_message() { /* translators: %s: maximum file size, e.g. "80 MB". */ return sprintf( __( 'This file is larger than this site allows (%s).', 'acadium-agent-publisher' ), size_format( self::max_bytes() ) ); } private static function headers( $status ) { status_header( $status ); nocache_headers(); header( 'X-Frame-Options: DENY' ); header( "Content-Security-Policy: frame-ancestors 'none'" ); header( 'Referrer-Policy: no-referrer' ); header( 'X-Robots-Tag: noindex, nofollow' ); } private static function json( array $body, $status = 200 ) { self::headers( $status ); header( 'Content-Type: application/json; charset=utf-8' ); echo wp_json_encode( $body ); exit; } private static function fail( $post, $message, $status ) { if ( $post ) { self::json( array( 'error' => $message ), $status ); } self::open( $status, __( 'Upload not possible', 'acadium-agent-publisher' ) ); echo '
' . esc_html( $message ) . '