-
Notifications
You must be signed in to change notification settings - Fork 9
/
Eol.php
103 lines (93 loc) · 2.69 KB
/
Eol.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
<?php
/**
* Copyright 2009-2017 Horde LLC (http://www.horde.org/)
*
* See the enclosed file COPYING for license information (LGPL). If you
* did not receive this file, see http://www.horde.org/licenses/lgpl21.
*
* @category Horde
* @copyright 2009-2017 Horde LLC
* @license http://www.horde.org/licenses/lgpl21 LGPL 2.1
* @package Stream_Filter
*/
/**
* Stream filter class to convert EOL characters.
*
* Usage:
* stream_filter_register('horde_eol', 'Horde_Stream_Filter_Eol');
* stream_filter_[app|pre]pend($stream, 'horde_eol',
* [ STREAM_FILTER_[READ|WRITE|ALL] ],
* [ $params ]);
*
* $params is an array that can contain the following:
* - eol: (string) The EOL string to use.
* DEFAULT: <CR><LF> ("\r\n")
*
* @author Michael Slusarz <slusarz@horde.org>
* @category Horde
* @copyright 2009-2017 Horde LLC
* @license http://www.horde.org/licenses/lgpl21 LGPL 2.1
* @package Stream_Filter
*/
class Horde_Stream_Filter_Eol extends php_user_filter
{
/**
* Replacement data
*
* @var mixed
*/
protected $_replace;
/**
* Search array.
*
* @var mixed
*/
protected $_search;
/**
* First character of a multi-character EOL.
*
* @var string
*/
protected $_split = null;
/**
* @see stream_filter_register()
*/
#[ReturnTypeWillChange]
public function onCreate()
{
$eol = isset($this->params['eol'])
? $this->params['eol']
: "\r\n";
if (!strlen($eol)) {
$this->_search = array("\r", "\n");
$this->_replace = '';
} elseif (in_array($eol, array("\r", "\n"))) {
$this->_search = array("\r\n", ($eol == "\r") ? "\n" : "\r");
$this->_replace = $eol;
} else {
$this->_search = array("\r\n", "\r", "\n");
$this->_replace = array("\n", "\n", $eol);
if (strlen($eol) > 1) {
$this->_split = $eol[0];
}
}
return true;
}
/**
* @see stream_filter_register()
*/
#[ReturnTypeWillChange]
public function filter($in, $out, &$consumed, $closing)
{
while ($bucket = stream_bucket_make_writeable($in)) {
if (!is_null($this->_split) &&
($bucket->data[$bucket->datalen - 1] == $this->_split)) {
$bucket->data = substr($bucket->data, 0, -1);
}
$bucket->data = str_replace($this->_search, $this->_replace, $bucket->data);
$consumed += $bucket->datalen;
stream_bucket_append($out, $bucket);
}
return PSFS_PASS_ON;
}
}