Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public function filter_bytes( string $bytes ) {
if ( empty( $last_error ) ) {
$last_error = array( 'message' => 'Unknown error' );
}
throw new ByteStreamException( 'Failed to inflate data: ' . $last_error['message'] );
throw new ByteStreamException( esc_html( 'Failed to inflate data: ' . $last_error['message'] ) );
}

return $inflated_data;
Expand Down
22 changes: 18 additions & 4 deletions components/ByteStream/ReadStream/class-basebytereadstream.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,33 +24,45 @@ abstract class BaseByteReadStream implements ByteReadStream {
* fox jumps over the lazy dog.
* ^--^
* consumed but retained for seek()-ing backwards.
*
* @var int
*/
protected $max_lookbehind_bytes = 2048;

/**
* The remaining unconsumed bytes.
*
* @var string
*/
protected $buffer = '';

/**
* How many bytes have already been consumed in the current **buffer**.
*
* @var int
*/
protected $offset_in_current_buffer = 0;

/**
* How many bytes have already been forgotten in the current **stream**.
*
* @var int
*/
protected $bytes_already_forgotten = 0;

/**
* Whether the stream has been closed for reading.
*
* @var bool
*/
protected $is_read_closed = false;

/**
* How many bytes are expected in the stream. Optional.
*
* When it's null, the stream is unbounded and length() will also return null.
*
* @var int|null
*/
protected $expected_length = null;

Expand Down Expand Up @@ -172,10 +184,12 @@ public function seek( int $target_offset ): void {
if ( null !== $this->length() && $target_offset > $this->length() ) {
$length = $this->length();
throw new NotEnoughDataException(
sprintf(
'Cannot seek to past the stream length (seeked to %d, stream length is %d).',
$target_offset,
$length
esc_html(
sprintf(
'Cannot seek to past the stream length (seeked to %d, stream length is %d).',
$target_offset,
$length
)
)
);
}
Expand Down
2 changes: 2 additions & 0 deletions components/ByteStream/ReadStream/class-deflatereadstream.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ class DeflateReadStream extends BaseByteReadStream {
/**
* The offset of the underlying reader at the time of the first read
* from the DeflateReader.
*
* @var int|null
*/
protected $delegate_offset_0;
protected $deflate_context;
Expand Down
6 changes: 3 additions & 3 deletions components/ByteStream/ReadStream/class-filereadstream.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ class FileReadStream extends BaseByteReadStream {

public static function from_path( $file_path ) {
if ( ! file_exists( $file_path ) ) {
throw new ByteStreamException( sprintf( 'File %s does not exist', $file_path ) );
throw new ByteStreamException( esc_html( sprintf( 'File %s does not exist', $file_path ) ) );
}
if ( ! is_file( $file_path ) ) {
throw new ByteStreamException( sprintf( '%s is not a file', $file_path ) );
throw new ByteStreamException( esc_html( sprintf( '%s is not a file', $file_path ) ) );
}
$handle = fopen( $file_path, 'r' );
if ( ! $handle ) {
throw new ByteStreamException( sprintf( 'Failed to open file %s', $file_path ) );
throw new ByteStreamException( esc_html( sprintf( 'Failed to open file %s', $file_path ) ) );
}

return self::from_resource( $handle, filesize( $file_path ) );
Expand Down
10 changes: 8 additions & 2 deletions components/ByteStream/ReadStream/class-inflatereadstream.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,16 @@ class InflateReadStream extends BaseByteReadStream {
*
* If we just seeked to offset=0, we'd start inflating the plaintext,
* not the deflated body, and get an error.
*
* @var int|null
*/
protected $delegate_offset_0;

// Ensure properties are defined or inherited.
/**
* Ensure properties are defined or inherited.
*
* @var bool
*/
protected $is_closed = false;
protected $buffer = '';
protected $bytes_already_forgotten = 0;
Expand Down Expand Up @@ -59,7 +65,7 @@ protected function internal_pull( $n ): string {

$inflated = inflate_add( $this->inflate_context, $this->upstream->consume( $available ) );
if ( false === $inflated ) {
throw new ByteStreamException( 'Inflate error: ' . $this->get_error_string() );
throw new ByteStreamException( esc_html( 'Inflate error: ' . $this->get_error_string() ) );
}

return $inflated;
Expand Down
25 changes: 20 additions & 5 deletions components/ByteStream/ReadStream/class-transformedreadstream.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,29 +63,44 @@ private function flush_filters(): string {
return $flush;
}

/** @disregard P1038 */
/**
*
* @param string $offset The offset to get.
* @throws ByteStreamException If the filter is not found.
*/
#[ReturnTypeWillChange]
public function offsetGet( $offset ) {
if ( ! isset( $this->filters[ $offset ] ) ) {
throw new ByteStreamException( sprintf( 'Filter %s not found', $offset ) );
throw new ByteStreamException( esc_html( sprintf( 'Filter %s not found', $offset ) ) );
}

return $this->filters[ $offset ];
}

/** @disregard P1038 */
/**
*
* @param string $offset The offset to check.
* @return bool Whether the filter exists.
*/
#[ReturnTypeWillChange]
public function offsetExists( $offset ) {
return isset( $this->filters[ $offset ] );
}

/** @disregard P1038 */
/**
* @param string $offset The offset to set.
* @param ByteTransformer $value The filter to set.
* @throws ByteStreamException If the filters are immutable.
*/
#[ReturnTypeWillChange]
public function offsetSet( $offset, $value ) {
throw new ByteStreamException( 'Filters are immutable' );
}

/** @disregard P1038 */
/**
* @param string $offset The offset to unset.
* @throws ByteStreamException If the filters are immutable.
*/
#[ReturnTypeWillChange]
public function offsetUnset( $offset ) {
throw new ByteStreamException( 'Filters are immutable' );
Expand Down
9 changes: 5 additions & 4 deletions components/ByteStream/WriteStream/class-filewritestream.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@ public static function from_path( string $path, string $mode = 'append' ): self
$file_mode = 'ab'; // Append mode: appends to the file.
break;
default:
throw new ByteStreamException( "Invalid mode: $mode. Use 'truncate' or 'append'." );
throw new ByteStreamException( esc_html( "Invalid mode: $mode. Use 'truncate' or 'append'." ) );
}

$file_handle = fopen( $path, $file_mode );
if ( false === $file_handle ) {
throw new ByteStreamException( "Failed to open file at path: $path" );
throw new ByteStreamException( esc_html( "Failed to open file at path: $path" ) );
}

return new self( $file_handle );
Expand All @@ -40,7 +40,7 @@ public static function from_path( string $path, string $mode = 'append' ): self
/**
* Creates a new instance of FileWriter from an existing file handle.
*
* @param resource $fileHandle A valid file handle.
* @param resource $file_handle A valid file handle.
*
* @return FileWriteStream
* @throws ByteStreamException If the file handle is invalid.
Expand All @@ -52,7 +52,8 @@ public static function from_resource_handle( $file_handle ): self {
/**
* Private constructor to enforce the use of static factory methods.
*
* @param resource $fileHandle
* @param resource $file_handle A valid file handle.
* @throws ByteStreamException If the file handle is invalid.
*/
public function __construct( $file_handle ) {
if ( ! is_resource( $file_handle ) || 'stream' !== get_resource_type( $file_handle ) ) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,15 @@ public function close_writing(): void {
}
}

/** @disregard P1038 */
/**
*
* @param string $offset The offset to get.
* @throws ByteStreamException If the filter is not found.
*/
#[ReturnTypeWillChange]
public function offsetGet( $offset ) {
if ( ! isset( $this->filters[ $offset ] ) ) {
throw new ByteStreamException( sprintf( 'Filter %s not found', $offset ) );
throw new ByteStreamException( esc_html( sprintf( 'Filter %s not found', $offset ) ) );
}

return $this->filters[ $offset ];
Expand Down
2 changes: 1 addition & 1 deletion components/ByteStream/class-filereadwritestream.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public static function from_path( string $path, bool $truncate = false ): self {
$mode = $truncate ? 'w+b' : 'a+b';
$file_pointer = fopen( $path, $mode );
if ( ! $file_pointer ) {
throw new ByteStreamException( "Cannot open $path" );
throw new ByteStreamException( esc_html( "Cannot open $path" ) );
}

return new self( $file_pointer, filesize( $path ) );
Expand Down
10 changes: 5 additions & 5 deletions components/ByteStream/class-memorypipe.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ class MemoryPipe extends BaseByteReadStream implements ByteWriteStream {

protected $is_writing_closed;

public function __construct( ?string $string = null, $expected_length = null ) {
if ( null !== $string && strlen( $string ) > 0 && null !== $expected_length ) {
public function __construct( ?string $bytes = null, $expected_length = null ) {
if ( null !== $bytes && strlen( $bytes ) > 0 && null !== $expected_length ) {
throw new ByteStreamException( 'A MemoryPipe accepts either a non-empty string representing the entire data, or an expected length when the data is not available yet. It does not accept both arguments.' );
}
if ( null !== $string ) {
$this->buffer = $string;
$this->expected_length = strlen( $string );
if ( null !== $bytes ) {
$this->buffer = $bytes;
$this->expected_length = strlen( $bytes );
// If we have a full buffer, it's already in memory and we don't need.
// to clean up old data as we stream it.
// If we did clean up old data, we would lose the ability to seek() to.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,8 @@ public function next_entity() {
'post_meta',
array(
'post_id' => $this->post_id,
'meta_key' => $key,
'meta_value' => $value,
'meta_key' => $key, // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_key
'meta_value' => $value, // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_value
)
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ public function next_entity() {
return false;

default:
throw new DataLiberationException( 'Invalid state: ' . $this->state );
throw new DataLiberationException( esc_html( 'Invalid state: ' . $this->state ) );
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,8 @@ public function __construct( PDO $db, $options = array() ) {
$this->tables_to_process = $options['tables_to_process'] ?? null;
$this->create_table_query = $options['create_table_query'] ?? false;
$this->db_type = $db->getAttribute( PDO::ATTR_DRIVER_NAME );
if ( ! in_array( $this->db_type, array( 'sqlite', 'mysql' ) ) ) {
throw new DataLiberationException( 'Unsupported database type: ' . $this->db_type );
if ( ! in_array( $this->db_type, array( 'sqlite', 'mysql' ), true ) ) {
throw new DataLiberationException( esc_html( 'Unsupported database type: ' . $this->db_type ) );
}
if ( isset( $options['cursor'] ) ) {
$this->initialize_from_cursor( $options['cursor'] );
Expand Down Expand Up @@ -292,7 +292,7 @@ private function export_create_table_query() {
private function initialize_tables_to_process() {
$this->tables_to_process = array();
$result = $this->db->query( 'SHOW TABLES' );
while ( $row = $result->fetch( PDO::FETCH_NUM ) ) {
while ( $row = $result->fetch( PDO::FETCH_NUM ) ) { // phpcs:ignore Generic.CodeAnalysis.AssignmentInCondition.FoundInWhileCondition
$this->tables_to_process[] = $row[0];
}
sort( $this->tables_to_process );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
use function WordPress\Filesystem\wp_join_unix_paths;

/**
* https://www.w3.org/AudioVideo/ebook/
* EPub specification: https://www.w3.org/AudioVideo/ebook/
*
* An EPUB Publication is transported as a single file (a "portable document") that contains:
* * a Package Document (OPF file) which specifies all the Publication's constituent content documents and their required resources, defines a reading order and associates Publication-level metadata and navigation information.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,8 @@ class FilesystemEntityReader implements EntityReader {

/**
* The post type to emit.
*
* @var string
*/
private $post_type;

Expand Down Expand Up @@ -331,8 +333,8 @@ public function next_entity(): bool {
'post_meta',
array(
'post_id' => $post_tree_node['post_id'],
'meta_key' => $key,
'meta_value' => $value,
'meta_key' => $key, // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_key
'meta_value' => $value, // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_value
)
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ public function next_entity() {
'post_meta',
array(
'post_id' => $this->post_id,
'meta_key' => $key,
'meta_value' => $value,
'meta_key' => $key, // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_key
'meta_value' => $value, // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_value
)
);
}
Expand Down
10 changes: 5 additions & 5 deletions components/DataLiberation/EntityReader/class-wxrentityreader.php
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ public static function create( ByteReadStream $upstream = null, $cursor = null,
/**
* Constructor.
*
* @param WP_XML_Processor $xml The XML processor to use.
* @param XMLProcessor $xml The XML processor to use.
*
* @since WP_VERSION
*/
Expand Down Expand Up @@ -457,9 +457,9 @@ public function get_reentrancy_cursor() {
* even after evicting the actual bytes where $last_entity is stored.
*/
$xml_cursor = $this->xml->get_reentrancy_cursor();
$xml_cursor = json_decode( base64_decode( $xml_cursor ), true );
$xml_cursor = json_decode( base64_decode( $xml_cursor ), true ); // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_decode
$xml_cursor['upstream_bytes_forgotten'] = $this->entity_opener_byte_offset;
$xml_cursor = base64_encode( json_encode( $xml_cursor ) );
$xml_cursor = base64_encode( json_encode( $xml_cursor ) ); // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_encode

return json_encode(
array(
Expand Down Expand Up @@ -893,7 +893,7 @@ private function parse_site_option() {
* Connects a byte stream to automatically pull bytes from once
* the last input chunk have been processed.
*
* @param WP_Byte_Reader $stream The upstream stream.
* @param ByteReadStream $stream The upstream stream.
*/
public function connect_upstream( ByteReadStream $stream ) {
$this->upstream = $stream;
Expand Down Expand Up @@ -946,7 +946,7 @@ private function emit_entity() {
/**
* Sets the current entity tag and type.
*
* @param string $tag The entity tag name.
* @param string $tag_with_namespace The entity tag name.
*
* @since WP_VERSION
*/
Expand Down
1 change: 0 additions & 1 deletion components/HTML/class-wphtmlprocessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -3344,7 +3344,6 @@ private function step_in_table(): bool {
* This internal function performs the 'in table text' insertion mode
* logic for the generalized WP_HTML_Processor::step() function.
*
* @return bool Whether an element was found.
* @throws WP_HTML_Unsupported_Exception When encountering unsupported HTML input.
*
* @see https://html.spec.whatwg.org/#parsing-main-intabletext
Expand Down
3 changes: 1 addition & 2 deletions components/HTML/class-wptokenmap.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
<?php

/**
* Class for efficiently looking up and mapping string keys to string values, with limits.
*
Expand Down Expand Up @@ -407,8 +406,8 @@ public static function from_precomputed_table( $state ): ?WP_Token_Map {
if ( self::STORAGE_VERSION !== $state['storage_version'] ) {
_doing_it_wrong(
__METHOD__,
/* translators: 1: version string, 2: version string. */
sprintf(
/* translators: 1: version string, 2: version string. */
__( 'Loaded version \'%1$s\' incompatible with expected version \'%2$s\'.' ),
$state['storage_version'],
self::STORAGE_VERSION
Expand Down
Loading