Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add wxr-parser-large-file. #140

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Polish up and add some docs
obenland committed Dec 2, 2022
commit 93c7cb9dde432a299ee1af68b5123e873eede580
61 changes: 43 additions & 18 deletions src/helpers/class-xml-character-filter.php
Original file line number Diff line number Diff line change
@@ -1,47 +1,72 @@
<?php

/*
* String which will be prefixed to stream URLs to add this filter
/**
* XML_Character_Filter file.
*
* @package WordPress
* @subpackage Importer
*/

// String which will be prefixed to stream URLs to add this filter.
define( 'XML_CHARACTER_FILTER_PREFIX', 'php://filter/read=xml_character_filter/resource=' );

/**
* Class XML_Character_Filter
* Class XML_Character_Filter.
*
* Filter for PHP stream
*
* Remove control characters except newline, tab and return
* XML Character Stream Filter to sanitize XML input. Removes control characters except newline, tab and return.
*
* Usage: php://filter/read=xml_character_filter/resource=zip://archive.zip#import.xml;
*/
class XML_Character_Filter extends php_user_filter {
/**
* List of control characters to remove.
*
* @var array
*/
private $chars = array();

private $chars = [];

/**
* This method is called whenever data is read from or written to the attached stream (such as with fread() or fwrite()).
*
* @param resource $in A resource pointing to a bucket brigade which contains one or more bucket objects containing data to be filtered.
* @param resource $out A resource pointing to a second bucket brigade into which the modified buckets should be placed.
* @param int $consumed Reference to the length of the data that the filter reads in and alters.
* @param bool $closing Whether the stream is in the process of closing.
* @return int PSFS_PASS_ON|PSFS_FEED_ME|PSFS_ERR_FATAL.
*/
public function filter( $in, $out, &$consumed, $closing ) {
while ( $bucket = stream_bucket_make_writeable( $in ) ) {
$consumed += $bucket->datalen;
while ( $bucket = stream_bucket_make_writeable( $in ) ) { //phpcs:ignore WordPress.CodeAnalysis.AssignmentInCondition
$consumed += $bucket->datalen;
$bucket->data = $this->replace_chars( $bucket->data );
stream_bucket_append( $out, $bucket );
}

return PSFS_PASS_ON;
}

private function replace_chars( $string ) {
return str_replace( $this->chars, ' ', $string );
}

/**
* This method is called during instantiation of the filter class object.
*
* @return bool
*/
public function onCreate() {
for ( $ascii_num = 0; $ascii_num < 32; $ascii_num ++ ) {
if ( $ascii_num !== 9 && $ascii_num !== 10 && $ascii_num !== 13 ) {
for ( $ascii_num = 0; $ascii_num < 32; $ascii_num++ ) {
if ( 9 !== $ascii_num && 10 !== $ascii_num && 13 !== $ascii_num ) {
$this->chars[] = chr( $ascii_num );
}
}
$this->chars[] = chr( 127 );

return true;
}
}

/**
* Replace control characters.
*
* @param string $string Data to replace.
* @return string
*/
private function replace_chars( $string ) {
return str_replace( $this->chars, ' ', $string );
}
}
stream_filter_register( 'xml_character_filter', 'XML_Character_Filter' );
Loading