From d58e9b4c7aeefe7d791117adb2a97bd536db7c00 Mon Sep 17 00:00:00 2001 From: Sara Golemon Date: Sat, 4 Jan 2003 07:17:18 +0000 Subject: [PATCH] Defined stream_register_filter, stream_filter_prepend, and stream_filter_prepend git-svn-id: https://svn.php.net/repository/phpdoc/en/trunk@110292 c90b9560-bf6c-de11-be94-00142212c4b1 --- .../stream/functions/stream-filter-append.xml | 56 +++++ .../functions/stream-filter-prepend.xml | 56 +++++ .../functions/stream-register-filter.xml | 196 ++++++++++++++++++ 3 files changed, 308 insertions(+) create mode 100644 reference/stream/functions/stream-filter-append.xml create mode 100644 reference/stream/functions/stream-filter-prepend.xml create mode 100644 reference/stream/functions/stream-register-filter.xml diff --git a/reference/stream/functions/stream-filter-append.xml b/reference/stream/functions/stream-filter-append.xml new file mode 100644 index 0000000000..03eee77bd2 --- /dev/null +++ b/reference/stream/functions/stream-filter-append.xml @@ -0,0 +1,56 @@ + + + + + stream_filter_append + Attach a filter to a stream. + + + Description + + boolstream_filter_append + resourcestream + stringfiltername + + + Adds filtername to the list of filters + attached to stream. This filter will be + added to the end of the list and + will therefore be called last during stream opperations. To + add a filter to the beginning of the list, use + stream_filter_prepend. + + + + stream_register_filter must be called first + in order to register the desired user filter to filtername. + + + + See Also: + stream_register_filter, and + stream_filter_prepend + + + + + diff --git a/reference/stream/functions/stream-filter-prepend.xml b/reference/stream/functions/stream-filter-prepend.xml new file mode 100644 index 0000000000..0b10ae10ed --- /dev/null +++ b/reference/stream/functions/stream-filter-prepend.xml @@ -0,0 +1,56 @@ + + + + + stream_filter_prepend + Attach a filter to a stream. + + + Description + + boolstream_filter_prepend + resourcestream + stringfiltername + + + Adds filtername to the list of filters + attached to stream. This filter will be + added to the beginning of the list and + will therefore be called first during stream opperations. To + add a filter to the end of the list, use + stream_filter_append. + + + + stream_register_filter must be called first + in order to register the desired user filter to filtername. + + + + See Also: + stream_register_filter, and + stream_filter_append + + + + + diff --git a/reference/stream/functions/stream-register-filter.xml b/reference/stream/functions/stream-register-filter.xml new file mode 100644 index 0000000000..92011dcc57 --- /dev/null +++ b/reference/stream/functions/stream-register-filter.xml @@ -0,0 +1,196 @@ + + + + + stream_register_filter + Register a stream filter implemented as a PHP class derived from php_user_filter + + + Description + + booleanstream_register_filter + stringfiltername + stringclassname + + + stream_register_filter allows you to implement + your own filter on any registered stream used with all the other + filesystem functions (such as fopen, + fread etc.). + + + To implement a filter, you need to define a class as an extension of + php_user_fitler with a number of member functions + as defined below. When performing read/write opperations on the stream + to which your filter is attached, PHP will pass the data through your + filter (and any other filters attached to that stream) so that the + data may be modified as desired. You must implement the methods + exactly as described below - doing otherwise will lead to undefined + behaviour. + + + stream_register_filter will return &false; if the + filtername is already defined. + + + + intwrite + stringdata + + + This method is called whenever data is written to the attached + stream (such as with fwrite). After + modifying data as needed your + filter should issue: return parent::write($data); + so that the next filter in the chain can perform its filter. + When no filters remain, the stream will write data + in its final form. + + + + stringread + intmaxlength + + + This method is called whenever data is read from the attached + stream (such as with fread). A filter + should first call parent::read($maxlength); to + retrieve the data from the previous filter who, ultimately, + retrieved it from the stream. Your filter may then modify the + data as needed and return it. + Your filter should never return more than maxlength + bytes. Since parent::read($maxlength); will also + not return more than maxlength bytes this + will ordinarily be a non-issue. However, if your filter + increases the size of the data being returned, you should either + call parent::read($maxlength-$x); where + x is the most your filter will grow + the size of the data read. Alternatively, you can build a + read-buffer into your class. + + + + intflush + boolclosing + + + This method is called in response to a request to flush the + attached stream (such as with fflush or + fclose). The closing + parameter tells you whether the stream is, in fact, in the + process of closing. The default action is to simply call: + return parent::flush($closing); , your + filter may wish to perform additional writes and/or cleanup + calls prior to or directly after a successful flush. + + + + voidoncreate + + + + + + voidonclose + + + + + + The example below implements a filter named rot13 + on the foo-bar.txt stream which will perform + ROT-13 encryption on all letter characters written to/read from + that stream. + + + Filter for ROT13 encoding data on foo-bar.txt stream + += 'A' AND $tempstr[$i] <= 'M') OR + ($tempstr[$i] >= 'a' AND $tempstr[$i] <= 'm')) $tempstr[$i] = chr(ord($tempstr[$i]) + 13); + else if (($tempstr[$i] >= 'N' AND $tempstr[$i] <= 'Z') OR + ($tempstr[$i] >= 'n' AND $tempstr[$i] <= 'z')) $tempstr[$i] = chr(ord($tempstr[$i]) - 13); + return $tempstr; + } + + function write($data) { + for($i = 0; $i < strlen($data); $i++) + if (($data[$i] >= 'A' AND $data[$i] <= 'M') OR + ($data[$i] >= 'a' AND $data[$i] <= 'm')) $data[$i] = chr(ord($data[$i]) + 13); + else if (($data[$i] >= 'N' AND $data[$i] <= 'Z') OR + ($data[$i] >= 'n' AND $data[$i] <= 'z')) $data[$i] = chr(ord($data[$i]) - 13); + return parent::write($data); + } +} + +/* Register our filter with PHP */ +stream_register_filter("rot13", "rot13_filter") + or die("Failed to register filter"); + +$fp = fopen("foo-bar.txt", "w"); + +/* Attach the registered filter to the stream just opened */ +stream_filter_append($fp, "rot13"); + +fwrite($fp, "Line1\n"); +fwrite($fp, "Word - 2\n"); +fwrite($fp, "Easy As 123\n"); + +fclose($fp); + +/* The filter only applies to the $fp stream + * so this readfile will read -without- applying + * a second pass of rot13 encoding + */ +readfile("foo-bar.txt"); + +/* Output + * ------ + +Yvar1 +Jbeq - 2 +Rnfl Nf 123 + + */ +?> +]]> + + + + + See Also: + stream_register_wrapper, + stream_filter_prepend, and + stream_filter_append + + + + + +