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
7 changes: 4 additions & 3 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
<?xml version="1.0"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" bootstrap="test/Horde/Stream/Filter/bootstrap.php" colors="true" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd">
<coverage>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" bootstrap="vendor/autoload.php" colors="true" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/11.0/phpunit.xsd">
<source>
<include>
<directory suffix=".php">lib</directory>
<directory suffix=".php">src</directory>
</include>
</coverage>
</source>
<testsuites>
<testsuite name="horde/Stream/Filter">
<directory>test</directory>
Expand Down
40 changes: 40 additions & 0 deletions src/Bin2hex.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

declare(strict_types=1);

/**
* Copyright 2011-2026 Horde LLC (http://www.horde.org/)
*
* See the enclosed file LICENSE for license information (LGPL). If you
* did not receive this file, see http://www.horde.org/licenses/lgpl21.
*
* @author Michael Slusarz <slusarz@horde.org>
*/

namespace Horde\Stream\Filter;

use php_user_filter;

class Bin2hex extends php_user_filter
{
public const FILTER_NAME = 'horde.stream.filter.bin2hex';

public static function register(): void
{
if (in_array(self::FILTER_NAME, stream_get_filters())) {
return;
}
stream_filter_register(self::FILTER_NAME, static::class);
}

public function filter($in, $out, &$consumed, $closing): int
{
while ($bucket = stream_bucket_make_writeable($in)) {
$bucket->data = bin2hex($bucket->data);
$consumed += $bucket->datalen;
stream_bucket_append($out, $bucket);
}

return PSFS_PASS_ON;
}
}
114 changes: 114 additions & 0 deletions src/Crc32.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
<?php

declare(strict_types=1);

/**
* Copyright 2011-2026 Horde LLC (http://www.horde.org/)
*
* See the enclosed file LICENSE for license information (LGPL). If you
* did not receive this file, see http://www.horde.org/licenses/lgpl21.
*
* @author Michael Slusarz <slusarz@horde.org>
*/

namespace Horde\Stream\Filter;

use php_user_filter;

class Crc32 extends php_user_filter
{
public const FILTER_NAME = 'horde.stream.filter.crc32';

public static function register(): void
{
if (in_array(self::FILTER_NAME, stream_get_filters())) {
return;
}
stream_filter_register(self::FILTER_NAME, static::class);
}

public function onCreate(): bool
{
$this->params->crc32 = 0;

return true;
}

public function filter($in, $out, &$consumed, $closing): int
{
while ($bucket = stream_bucket_make_writeable($in)) {
$consumed += $bucket->datalen;
$this->params->crc32 = $this->crc32Combine(
$this->params->crc32,
crc32($bucket->data),
$bucket->datalen,
);
stream_bucket_append($out, $bucket);
}

return PSFS_PASS_ON;
}

private function crc32Combine(int $crc1, int $crc2, int $len2): int
{
$odd = [0xedb88320];
$row = 1;

for ($n = 1; $n < 32; ++$n) {
$odd[$n] = $row;
$row <<= 1;
}

$this->gf2MatrixSquare($even, $odd);
$this->gf2MatrixSquare($odd, $even);

do {
$this->gf2MatrixSquare($even, $odd);

if ($len2 & 1) {
$crc1 = $this->gf2MatrixTimes($even, $crc1);
}

$len2 >>= 1;

if ($len2 === 0) {
break;
}

$this->gf2MatrixSquare($odd, $even);
if ($len2 & 1) {
$crc1 = $this->gf2MatrixTimes($odd, $crc1);
}

$len2 >>= 1;
} while ($len2 !== 0);

$crc1 ^= $crc2;

return $crc1;
}

private function gf2MatrixSquare(?array &$square, array &$mat): void
{
$square = [];
for ($n = 0; $n < 32; ++$n) {
$square[$n] = $this->gf2MatrixTimes($mat, $mat[$n]);
}
}

private function gf2MatrixTimes(array $mat, int $vec): int
{
$i = $sum = 0;

while ($vec) {
if ($vec & 1) {
$sum ^= $mat[$i];
}

$vec = ($vec >> 1) & 0x7FFFFFFF;
++$i;
}

return $sum;
}
}
78 changes: 78 additions & 0 deletions src/Eol.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?php

declare(strict_types=1);

/**
* Copyright 2009-2026 Horde LLC (http://www.horde.org/)
*
* See the enclosed file LICENSE for license information (LGPL). If you
* did not receive this file, see http://www.horde.org/licenses/lgpl21.
*
* @author Michael Slusarz <slusarz@horde.org>
* @author Angelo Milazzo <bsod85@gmail.com>
*/

namespace Horde\Stream\Filter;

use php_user_filter;

class Eol extends php_user_filter
{
public const FILTER_NAME = 'horde.stream.filter.eol';

/** @var string[] */
private array $search = [];

private string|array $replace = '';

private string $prependNext = '';

public static function register(): void
{
if (in_array(self::FILTER_NAME, stream_get_filters())) {
return;
}
stream_filter_register(self::FILTER_NAME, static::class);
}

public function onCreate(): bool
{
$eol = $this->params['eol'] ?? "\r\n";

if (!strlen($eol)) {
$this->search = ["\r", "\n"];
$this->replace = '';
} elseif (in_array($eol, ["\r", "\n"])) {
$this->search = ["\r\n", ($eol === "\r") ? "\n" : "\r"];
$this->replace = $eol;
} else {
$this->search = ["\r\n", "\r", "\n"];
$this->replace = ["\n", "\n", $eol];
}

return true;
}

public function filter($in, $out, &$consumed, $closing): int
{
while ($bucket = stream_bucket_make_writeable($in)) {
$bucket->data = $this->prependNext . $bucket->data;
$this->prependNext = '';

if (
!$closing
&& in_array("\r\n", $this->search)
&& ($bucket->data[$bucket->datalen - 1] === "\r")
) {
$bucket->data = substr($bucket->data, 0, -1);
$this->prependNext = "\r";
}

$bucket->data = str_replace($this->search, $this->replace, $bucket->data);
$consumed += $bucket->datalen;
stream_bucket_append($out, $bucket);
}

return PSFS_PASS_ON;
}
}
40 changes: 40 additions & 0 deletions src/Htmlspecialchars.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

declare(strict_types=1);

/**
* Copyright 2012-2026 Horde LLC (http://www.horde.org/)
*
* See the enclosed file LICENSE for license information (LGPL). If you
* did not receive this file, see http://www.horde.org/licenses/lgpl21.
*
* @author Jan Schneider <jan@horde.org>
*/

namespace Horde\Stream\Filter;

use php_user_filter;

class Htmlspecialchars extends php_user_filter
{
public const FILTER_NAME = 'horde.stream.filter.htmlspecialchars';

public static function register(): void
{
if (in_array(self::FILTER_NAME, stream_get_filters())) {
return;
}
stream_filter_register(self::FILTER_NAME, static::class);
}

public function filter($in, $out, &$consumed, $closing): int
{
while ($bucket = stream_bucket_make_writeable($in)) {
$bucket->data = htmlspecialchars($bucket->data);
$consumed += $bucket->datalen;
stream_bucket_append($out, $bucket);
}

return PSFS_PASS_ON;
}
}
51 changes: 51 additions & 0 deletions src/NullFilter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

declare(strict_types=1);

/**
* Copyright 2012-2026 Horde LLC (http://www.horde.org/)
*
* See the enclosed file LICENSE for license information (LGPL). If you
* did not receive this file, see http://www.horde.org/licenses/lgpl21.
*
* @author Michael J Rubinsky <mrubinsk@horde.org>
*/

namespace Horde\Stream\Filter;

use php_user_filter;

class NullFilter extends php_user_filter
{
public const FILTER_NAME = 'horde.stream.filter.null';

private string $search = "\0";

private string $replace = '';

public static function register(): void
{
if (in_array(self::FILTER_NAME, stream_get_filters())) {
return;
}
stream_filter_register(self::FILTER_NAME, static::class);
}

public function onCreate(): bool
{
$this->replace = $this->params->replace ?? '';

return true;
}

public function filter($in, $out, &$consumed, $closing): int
{
while ($bucket = stream_bucket_make_writeable($in)) {
$bucket->data = str_replace($this->search, $this->replace, $bucket->data);
$consumed += $bucket->datalen;
stream_bucket_append($out, $bucket);
}

return PSFS_PASS_ON;
}
}
6 changes: 0 additions & 6 deletions test/Horde/Stream/Filter/AllTests.php

This file was deleted.

Loading