Skip to content

Commit

Permalink
Don't trim $filter_name in sanitizeStreamFilter - Fixes filters which…
Browse files Browse the repository at this point in the history
… pass newline character as an argument.
  • Loading branch information
HeyRatFans authored and vagrant committed Oct 13, 2015
1 parent 1336fd4 commit bfdc19f
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 4 deletions.
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"

0 comments on commit bfdc19f

Please sign in to comment.