1 | <?php |
---|
2 | /*********************************************** |
---|
3 | * File : paddingfilter.php |
---|
4 | * Project : Z-Push |
---|
5 | * Descr : Our own filter for stream padding with zero strings. |
---|
6 | * |
---|
7 | * Created : 18.07.2012 |
---|
8 | * |
---|
9 | * Copyright 2007 - 2012 Zarafa Deutschland GmbH |
---|
10 | * |
---|
11 | * This program is free software: you can redistribute it and/or modify |
---|
12 | * it under the terms of the GNU Affero General Public License, version 3, |
---|
13 | * as published by the Free Software Foundation with the following additional |
---|
14 | * term according to sec. 7: |
---|
15 | * |
---|
16 | * According to sec. 7 of the GNU Affero General Public License, version 3, |
---|
17 | * the terms of the AGPL are supplemented with the following terms: |
---|
18 | * |
---|
19 | * "Zarafa" is a registered trademark of Zarafa B.V. |
---|
20 | * "Z-Push" is a registered trademark of Zarafa Deutschland GmbH |
---|
21 | * The licensing of the Program under the AGPL does not imply a trademark license. |
---|
22 | * Therefore any rights, title and interest in our trademarks remain entirely with us. |
---|
23 | * |
---|
24 | * However, if you propagate an unmodified version of the Program you are |
---|
25 | * allowed to use the term "Z-Push" to indicate that you distribute the Program. |
---|
26 | * Furthermore you may use our trademarks where it is necessary to indicate |
---|
27 | * the intended purpose of a product or service provided you use it in accordance |
---|
28 | * with honest practices in industrial or commercial matters. |
---|
29 | * If you want to propagate modified versions of the Program under the name "Z-Push", |
---|
30 | * you may only do so if you have a written permission by Zarafa Deutschland GmbH |
---|
31 | * (to acquire a permission please contact Zarafa at trademark@zarafa.com). |
---|
32 | * |
---|
33 | * This program is distributed in the hope that it will be useful, |
---|
34 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
35 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
36 | * GNU Affero General Public License for more details. |
---|
37 | * |
---|
38 | * You should have received a copy of the GNU Affero General Public License |
---|
39 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
---|
40 | * |
---|
41 | * Consult LICENSE file for details |
---|
42 | ************************************************/ |
---|
43 | |
---|
44 | /* Define our filter class |
---|
45 | * |
---|
46 | * Usage: stream_filter_append($stream, 'padding.X'); |
---|
47 | * where X is a number a stream will be padded to be |
---|
48 | * multiple of (e.g. padding.3 will pad the stream |
---|
49 | * to be multiple of 3 which is useful in base64 |
---|
50 | * encoding). |
---|
51 | * |
---|
52 | * */ |
---|
53 | class padding_filter extends php_user_filter { |
---|
54 | private $padding = 4; // default padding |
---|
55 | |
---|
56 | /** |
---|
57 | * This method is called whenever data is read from or written to the attached stream |
---|
58 | * |
---|
59 | * @see php_user_filter::filter() |
---|
60 | * |
---|
61 | * @param resource $in |
---|
62 | * @param resource $out |
---|
63 | * @param int $consumed |
---|
64 | * @param boolean $closing |
---|
65 | * |
---|
66 | * @access public |
---|
67 | * @return int |
---|
68 | * |
---|
69 | */ |
---|
70 | function filter($in, $out, &$consumed, $closing) { |
---|
71 | while ($bucket = stream_bucket_make_writeable($in)) { |
---|
72 | if ($this->padding != 0 && $bucket->datalen < 8192) { |
---|
73 | $bucket->data .= str_pad($bucket->data, $this->padding, 0x0); |
---|
74 | } |
---|
75 | $consumed += ($this->padding != 0 && $bucket->datalen < 8192) ? ($bucket->datalen + $this->padding) : $bucket->datalen; |
---|
76 | stream_bucket_append($out, $bucket); |
---|
77 | } |
---|
78 | return PSFS_PASS_ON; |
---|
79 | } |
---|
80 | |
---|
81 | /** |
---|
82 | * Called when creating the filter |
---|
83 | * |
---|
84 | * @see php_user_filter::onCreate() |
---|
85 | * |
---|
86 | * @access public |
---|
87 | * @return boolean |
---|
88 | */ |
---|
89 | function onCreate() { |
---|
90 | $delim = strrpos($this->filtername, '.'); |
---|
91 | if ($delim !== false) { |
---|
92 | $padding = substr($this->filtername, $delim + 1); |
---|
93 | if (is_numeric($padding)) |
---|
94 | $this->padding = $padding; |
---|
95 | } |
---|
96 | return true; |
---|
97 | } |
---|
98 | } |
---|
99 | |
---|
100 | stream_filter_register("padding.*", "padding_filter"); |
---|
101 | ?> |
---|