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

Don't trim $filter_name argument in sanitizeStreamFilter. #123

Merged
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
47 changes: 47 additions & 0 deletions examples/lib/FilterReplace.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

namespace lib;

use php_user_filter;

class FilterReplace extends php_user_filter
{
const FILTER_NAME = 'convert.replace.';

private $search;

private $replace;

public function onCreate()
{
if( strpos( $this->filtername, self::FILTER_NAME ) !== 0 ){
return false;
}

$params = substr( $this->filtername, strlen( self::FILTER_NAME ) );

if( !preg_match( '/([^:]+):([^$]+)$/', $params, $matches ) ){
return false;
}

$this->search = $matches[1];
$this->replace = $matches[2];

return true;
}

public function filter( $in, $out, &$consumed, $closing )
{
while( $res = stream_bucket_make_writeable( $in ) ){

$res->data = str_replace( $this->search, $this->replace, $res->data );

$consumed += $res->datalen;

/** @noinspection PhpParamsInspection */
stream_bucket_append( $out, $res );
}

return PSFS_PASS_ON;
}
}
4 changes: 1 addition & 3 deletions src/Modifier/StreamFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -203,9 +203,7 @@ public function prependStreamFilter($filter_name)
protected function sanitizeStreamFilter($filter_name)
{
$this->assertStreamable();
$filter_name = (string) $filter_name;

return trim($filter_name);
return (string) $filter_name;
}

/**
Expand Down
11 changes: 10 additions & 1 deletion test/StreamFilterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use League\Csv\Reader;
use League\Csv\Writer;
use lib\FilterReplace;
use PHPUnit_Framework_TestCase;
use SplFileObject;
use SplTempFileObject;
Expand Down Expand Up @@ -113,11 +114,19 @@ public function testGetFilterPath()
$this->assertFalse($csv->getIterator()->getRealPath());
}


public function testGetFilterPathWithAllStream()
{
$filter = 'php://filter/string.toupper/resource='.__DIR__.'/foo.csv';
$csv = Reader::createFromPath($filter);
$this->assertFalse($csv->getIterator()->getRealPath());
}

public function testSetStreamFilterWriterNewLine()
{
stream_filter_register(FilterReplace::FILTER_NAME.'*', '\lib\FilterReplace');
$csv = Writer::createFromPath(__DIR__.'/newline.csv');
$csv->appendStreamFilter(FilterReplace::FILTER_NAME."\r\n:\n");
$this->assertTrue($csv->hasStreamFilter(FilterReplace::FILTER_NAME."\r\n:\n"));
$csv->insertOne([1, 'two', 3, "new\r\nline"]);
}
}
2 changes: 2 additions & 0 deletions test/newline.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
1,two,3,"new
line"