1: <?php
2: /**
3: * Stream filter class to convert EOL characters.
4: *
5: * Usage:
6: * stream_filter_register('horde_eol', 'Horde_Stream_Filter_Eol');
7: * stream_filter_[app|pre]pend($stream, 'horde_eol',
8: * [ STREAM_FILTER_[READ|WRITE|ALL] ],
9: * [ $params ]);
10: *
11: * $params is an arrat that can contain the following:
12: * - eol: (string) The EOL string to use.
13: * DEFAULT: <CR><LF> ("\r\n")
14: *
15: * Copyright 2009-2012 Horde LLC (http://www.horde.org/)
16: *
17: * See the enclosed file COPYING for license information (LGPL). If you
18: * did not receive this file, see http://www.horde.org/licenses/lgpl21.
19: *
20: * @author Michael Slusarz <slusarz@horde.org>
21: * @category Horde
22: * @license http://www.horde.org/licenses/lgpl21 LGPL 2.1
23: * @package Stream_Filter
24: */
25: class Horde_Stream_Filter_Eol extends php_user_filter
26: {
27: /**
28: * Search array.
29: *
30: * @param mixed
31: */
32: protected $_search;
33:
34: /**
35: * Replacement data
36: *
37: * @param mixed
38: */
39: protected $_replace;
40:
41: /**
42: * @see stream_filter_register()
43: */
44: public function onCreate()
45: {
46: $eol = isset($this->params['eol']) ? $this->params['eol'] : "\r\n";
47:
48: if (!strlen($eol)) {
49: $this->_search = array("\r", "\n");
50: $this->_replace = '';
51: } elseif (in_array($eol, array("\r", "\n"))) {
52: $this->_search = array("\r\n", ($eol == "\r") ? "\n" : "\r");
53: $this->_replace = $eol;
54: } else {
55: $this->_search = array("\r\n", "\r", "\n");
56: $this->_replace = array("\n", "\n", $eol);
57: }
58:
59: return true;
60: }
61:
62: /**
63: * @see stream_filter_register()
64: */
65: public function filter($in, $out, &$consumed, $closing)
66: {
67: while ($bucket = stream_bucket_make_writeable($in)) {
68: $bucket->data = str_replace($this->_search, $this->_replace, $bucket->data);
69: $consumed += $bucket->datalen;
70: stream_bucket_append($out, $bucket);
71: }
72:
73: return PSFS_PASS_ON;
74: }
75:
76: }
77: