<?xml version="1.0" encoding="UTF-8"?>
<!-- Reviewed: no -->
<sect1 id="zend.file.transfer.filters">
    <title>Filters for Zend_File_Transfer</title>

    <para>
        <classname>Zend_File_Transfer</classname> is delivered with several file related filters
        which can be used to automate several tasks which are often done on files. Note that file
        filters are applied after validation. Also file filters behave slightly different that other
        filters. They will always return the file name and not the changed content (which would be a
        bad idea when working on 1GB files). All filters which are provided with
        <classname>Zend_File_Transfer</classname> can be found in the
        <classname>Zend_Filter</classname> component and are named
        <classname>Zend_Filter_File_*</classname>. The following filters are actually available:
    </para>

    <itemizedlist>
        <listitem>
            <para>
                <classname>Decrypt</classname>: This filter can decrypt a encrypted file.
            </para>
        </listitem>

        <listitem>
            <para>
                <classname>Encrypt</classname>: This filter can encrypt a file.
            </para>
        </listitem>

        <listitem>
            <para>
                <classname>LowerCase</classname>: This filter can lowercase the content of a
                textfile.
            </para>
        </listitem>

        <listitem>
            <para>
                <classname>Rename</classname>: This filter can rename files, change the location and
                even force overwriting of existing files.
            </para>
        </listitem>

        <listitem>
            <para>
                <classname>UpperCase</classname>: This filter can uppercase the content of a
                textfile.
            </para>
        </listitem>
    </itemizedlist>

    <sect2 id="zend.file.transfer.filters.usage">
        <title>Using filters with Zend_File_Transfer</title>

        <para>
            The usage of filters is quite simple. There are several methods for adding and
            manipulating filters.
        </para>

        <itemizedlist>
            <listitem>
                <para>
                    <methodname>addFilter($filter, $options = null, $files =
                        null)</methodname>: Adds the given filter to the filter
                    stack (optionally only to the file(s) specified).
                    <varname>$filter</varname> may be either an actual filter
                    instance, or a short name specifying the filter type
                    (e.g., 'Rename').
                </para>
            </listitem>

            <listitem>
                <para>
                    <methodname>addFilters(array $filters, $files =
                        null)</methodname>: Adds the given filters to the stack of
                    filters. Each entry may be either a filter
                    type/options pair, or an array with the key 'filter'
                    specifying the filter (all other options will be
                    considered filter options for instantiation).
                </para>
            </listitem>

            <listitem>
                <para>
                    <methodname>setFilters(array $filters, $files =
                        null)</methodname>: Overwrites any existing filters with
                    the filters specified. The filters should follow the
                    syntax for <methodname>addFilters()</methodname>.
                </para>
            </listitem>

            <listitem>
                <para>
                    <methodname>hasFilter($name)</methodname>: Indicates if a
                    filter has been registered.
                </para>
            </listitem>

            <listitem>
                <para>
                    <methodname>getFilter($name)</methodname>: Returns a previously
                    registered filter.
                </para>
            </listitem>

            <listitem>
                <para>
                    <methodname>getFilters($files = null)</methodname>: Returns
                    registered filters; if <varname>$files</varname> is passed,
                    returns filters for that particular file or set of
                    files.
                </para>
            </listitem>

            <listitem>
                <para>
                    <methodname>removeFilter($name)</methodname>: Removes a previously
                    registered filter.
                </para>
            </listitem>

            <listitem>
                <para>
                    <methodname>clearFilters()</methodname>: Clears all
                    registered filters.
                </para>
            </listitem>
        </itemizedlist>

        <example id="zend.file.transfer.filters.usage.example">
            <title>Add filters to a file transfer</title>

            <programlisting language="php"><![CDATA[
$upload = new Zend_File_Transfer();

// Set a new destination path
$upload->addFilter('Rename', 'C:\picture\uploads');

// Set a new destination path and overwrites existing files
$upload->addFilter('Rename',
                   array('target' => 'C:\picture\uploads',
                         'overwrite' => true));
]]></programlisting>
        </example>

        <example id="zend.file.transfer.filters.usage.exampletwo">
            <title>Limit filters to single files</title>

            <para>
                <methodname>addFilter()</methodname>, <methodname>addFilters()</methodname>, and
                <methodname>setFilters()</methodname> each accept a final
                <varname>$files</varname> argument. This argument can be used to
                specify a particular file or array of files on which to set the
                given filter.
            </para>

            <programlisting language="php"><![CDATA[
$upload = new Zend_File_Transfer();

// Set a new destination path and limits it only to 'file2'
$upload->addFilter('Rename', 'C:\picture\uploads', 'file2');
]]></programlisting>
        </example>

        <para>
            Generally you should simply use the <methodname>addFilters()</methodname> method, which
            can be called multiple times.
        </para>

        <example id="zend.file.transfer.filters.usage.examplemultiple">
            <title>Add multiple filters</title>

            <para>
                Often it's simpler just to call <methodname>addFilter()</methodname> multiple times.
                One call for each filter. This also increases the readability and makes your code
                more maintainable. As all methods provide a fluent interface you can couple the
                calls as shown below:
            </para>

            <programlisting language="php"><![CDATA[
$upload = new Zend_File_Transfer();

// Set a filesize with 20000 bytes
$upload->addFilter('Rename', 'C:\picture\newjpg', 'file1')
       ->addFilter('Rename', 'C:\picture\newgif', 'file2');
]]></programlisting>
        </example>

        <note>
            <para>
                Note that even though setting the same filter
                multiple times is allowed, doing so can lead to issues when using
                different options for the same filter.
            </para>
        </note>
    </sect2>

    <sect2 id="zend.file.transfer.filters.decrypt">
        <title>Decrypt filter</title>

        <para>
            The <classname>Decrypt</classname> filter allows to decrypt a encrypted file.
        </para>

        <para>
            This filter makes use of <classname>Zend_Filter_Decrypt</classname>. It supports the
            <classname>Mcrypt</classname> and <classname>OpenSSL</classname> extensions from
            <acronym>PHP</acronym>. Please read the related section for details about how to set the
            options for decryption and which options are supported.
        </para>

        <para>
            This filter supports one additional option which can be used to save the decrypted file
            with another filename. Set the <property>filename</property> option to change the
            filename where the decrypted file will be stored. If you suppress this option, the
            decrypted file will overwrite the original encrypted file.
        </para>

        <example id="zend.file.transfer.filters.decrypt.example1">
            <title>Using the Decrypt filter with Mcrypt</title>

            <programlisting language="php"><![CDATA[
$upload = new Zend_File_Transfer_Adapter_Http();

// Adds a filter to decrypt the uploaded encrypted file
// with mcrypt and the key mykey
$upload->addFilter('Decrypt',
    array('adapter' => 'mcrypt', 'key' => 'mykey'));
]]></programlisting>
        </example>

        <example id="zend.file.transfer.filters.decrypt.example2">
            <title>Using the Decrypt filter with OpenSSL</title>

            <programlisting language="php"><![CDATA[
$upload = new Zend_File_Transfer_Adapter_Http();

// Adds a filter to decrypt the uploaded encrypted file
// with openssl and the provided keys
$upload->addFilter('Decrypt',
    array('adapter' => 'openssl',
          'private' => '/path/to/privatekey.pem',
          'envelope' => '/path/to/envelopekey.pem'));
]]></programlisting>
        </example>
    </sect2>

    <sect2 id="zend.file.transfer.filters.encrypt">
        <title>Encrypt filter</title>

        <para>
            The <classname>Encrypt</classname> filter allows to encrypt a file.
        </para>

        <para>
            This filter makes use of <classname>Zend_Filter_Encrypt</classname>. It supports the
            <classname>Mcrypt</classname> and <classname>OpenSSL</classname> extensions from
            <acronym>PHP</acronym>. Please read the related section for details about how to set the
            options for encryption and which options are supported.
        </para>

        <para>
            This filter supports one additional option which can be used to save the encrypted file
            with another filename. Set the <property>filename</property> option to change the
            filename where the encrypted file will be stored. If you suppress this option, the
            encrypted file will overwrite the original file.
        </para>

        <example id="zend.file.transfer.filters.encrypt.example1">
            <title>Using the Encrypt filter with Mcrypt</title>

            <programlisting language="php"><![CDATA[
$upload = new Zend_File_Transfer_Adapter_Http();

// Adds a filter to encrypt the uploaded file
// with mcrypt and the key mykey
$upload->addFilter('Encrypt',
    array('adapter' => 'mcrypt', 'key' => 'mykey'));
]]></programlisting>
        </example>

        <example id="zend.file.transfer.filters.encrypt.example2">
            <title>Using the Encrypt filter with OpenSSL</title>

            <programlisting language="php"><![CDATA[
$upload = new Zend_File_Transfer_Adapter_Http();

// Adds a filter to encrypt the uploaded file
// with openssl and the provided keys
$upload->addFilter('Encrypt',
    array('adapter' => 'openssl',
          'public' => '/path/to/publickey.pem'));
]]></programlisting>
        </example>
    </sect2>

    <sect2 id="zend.file.transfer.filters.lowercase">
        <title>LowerCase filter</title>

        <para>
            The <classname>LowerCase</classname> filter allows to change the content of a file to
            lowercase. You should use this filter only on textfiles.
        </para>

        <para>
            At initiation you can give a string which will then be used as encoding. Or you can use
            the <methodname>setEncoding()</methodname> method to set it afterwards.
        </para>

        <example id="zend.file.transfer.filters.lowercase.example">
            <title>Using the LowerCase filter</title>

            <programlisting language="php"><![CDATA[
$upload = new Zend_File_Transfer_Adapter_Http();
$upload->addValidator('MimeType', 'text');

// Adds a filter to lowercase the uploaded textfile
$upload->addFilter('LowerCase');

// Adds a filter to lowercase the uploaded file but only for uploadfile1
$upload->addFilter('LowerCase', null, 'uploadfile1');

// Adds a filter to lowercase with encoding set to ISO-8859-1
$upload->addFilter('LowerCase', 'ISO-8859-1');
]]></programlisting>
        </example>

        <note>
            <para>
                Note that due to the fact that the options for the LowerCase filter are optional,
                you must give a <constant>NULL</constant> as second parameter (the options) when you
                want to limit it to a single file element.
            </para>
        </note>
    </sect2>

    <sect2 id="zend.file.transfer.filters.rename">
        <title>Rename filter</title>

        <para>
            The <classname>Rename</classname> filter allows to change the destination of the upload,
            the filename and also to overwrite existing files. It supports the following options:
        </para>

        <itemizedlist>
            <listitem>
                <para>
                    <property>source</property>: The name and destination of the old file which
                    shall be renamed.
                </para>
            </listitem>

            <listitem>
                <para>
                    <property>target</property>: The new directory, or filename of the file.
                </para>
            </listitem>

            <listitem>
                <para>
                    <property>overwrite</property>: Sets if the old file overwrites the new one if
                    it already exists. The default value is <constant>FALSE</constant>.
                </para>
            </listitem>
        </itemizedlist>

        <para>
            Additionally you can also use the method <methodname>setFile()</methodname> to set
            files, which erases all previous set, <methodname>addFile()</methodname> to add a new
            file to existing ones, and <methodname>getFile()</methodname> to get all actually set
            files. To simplify things, this filter understands several notations and that methods
            and constructor understand the same notations.
        </para>

        <example id="zend.file.transfer.filters.rename.example">
            <title>Using the Rename filter</title>

            <programlisting language="php"><![CDATA[
$upload = new Zend_File_Transfer_Adapter_Http();

// Set a new destination path for all files
$upload->addFilter('Rename', 'C:\mypics\new');

// Set a new destination path only for uploadfile1
$upload->addFilter('Rename', 'C:\mypics\newgifs', 'uploadfile1');
]]></programlisting>
        </example>

        <para>
            You can use different notations. Below is a table where you will find a description and
            the intention for the supported notations. Note that when you use the Adapter or the
            Form Element you will not be able to use all described notations.
        </para>

        <table id="zend.file.transfer.filters.rename.notations">
            <title>Different notations of the rename filter and their meaning</title>

            <tgroup cols="2" align="left">
                <thead>
                    <row>
                        <entry>notation</entry>
                        <entry>description</entry>
                    </row>
                </thead>

                <tbody>
                    <row>
                        <entry>
                            <methodname>addFile('C:\uploads')</methodname>
                        </entry>

                        <entry>
                            Specifies a new location for all files when the given string is a
                            directory. Note that you will get an exception when the file already
                            exists, see the overwriting parameter.
                        </entry>
                    </row>

                    <row>
                        <entry>
                            <methodname>addFile('C:\uploads\file.ext')</methodname>
                        </entry>

                        <entry>
                            Specifies a new location and filename for all files when the given
                            string is not detected as directory. Note that you will get an exception
                            when the file already exists, see the overwriting parameter.
                        </entry>
                    </row>

                    <row>
                        <entry>
                            <methodname>addFile(array('C:\uploads\file.ext', 'overwrite' =>
                                true)</methodname>)
                        </entry>

                        <entry>
                            Specifies a new location and filename for all files when the given
                            string is not detected as directory and overwrites an existing file with
                            the same target name. Note, that you will get no notification that a
                            file was overwritten.
                        </entry>
                    </row>

                    <row>
                        <entry>
                            <methodname>addFile(array('source' => 'C:\temp\uploads', 'target' =>
                                'C:\uploads')</methodname>)
                        </entry>

                        <entry>
                            Specifies a new location for all files in the old location when the
                            given strings are detected as directory. Note that you will get an
                            exception when the file already exists, see the overwriting parameter.
                        </entry>
                    </row>

                    <row>
                        <entry>
                            addFile(array('source' => 'C:\temp\uploads', 'target' => 'C:\uploads',
                            'overwrite' => true))
                        </entry>

                        <entry>
                            Specifies a new location for all files in the old location when the
                            given strings are detected as directory and overwrites and existing file
                            with the same target name. Note, that you will get no notification that
                            a file was overwritten.
                        </entry>
                    </row>
                </tbody>
            </tgroup>
        </table>
    </sect2>

    <sect2 id="zend.file.transfer.filters.uppercase">
        <title>UpperCase filter</title>

        <para>
            The <classname>UpperCase</classname> filter allows to change the content of a file to
            uppercase. You should use this filter only on textfiles.
        </para>

        <para>
            At initiation you can give a string which will then be used as encoding. Or you can use
            the <methodname>setEncoding()</methodname> method to set it afterwards.
        </para>

        <example id="zend.file.transfer.filters.uppercase.example">
            <title>Using the UpperCase filter</title>

            <programlisting language="php"><![CDATA[
$upload = new Zend_File_Transfer_Adapter_Http();
$upload->addValidator('MimeType', 'text');

// Adds a filter to uppercase the uploaded textfile
$upload->addFilter('UpperCase');

// Adds a filter to uppercase the uploaded file but only for uploadfile1
$upload->addFilter('UpperCase', null, 'uploadfile1');

// Adds a filter to uppercase with encoding set to ISO-8859-1
$upload->addFilter('UpperCase', 'ISO-8859-1');
]]></programlisting>
        </example>

        <note>
            <para>
                Note that due to the fact that the options for the UpperCase filter are optional,
                you must give a <constant>NULL</constant> as second parameter (the options) when you
                want to limit it to a single file element.
            </para>
        </note>
    </sect2>
</sect1>
<!--
vim:se ts=4 sw=4 tw=80 et:
-->
