Zend Framework comes with a standard set of filters, which are ready for you to use.
Returns the string $value
, removing all but alphabetic and digit characters. This filter
includes an option to also allow white space characters.
![]() |
Note |
---|---|
The alphabetic characters mean characters that makes up words in each language. However, the english alphabet is treated as the alphabetic characters in following languages: Chinese, Japanese, Korean. The language is specified by Zend_Locale. |
Returns the string $value
, removing all but alphabetic characters. This filter includes an
option to also allow white space characters.
Given a string containing a path to a file, this filter will return the base name of the file
This filter will decrypt any given string with the provided setting. Therefor it makes use of
Adapters. Actually there are adapters for the Mcrypt
and OpenSSL
extensions from php.
For details about how to encrypt content look at the Encrypt
filter. As the basics are
covered within the Encrypt
filter, we will describe here only the needed additional
methods and changes for decryption.
For decrypting content which was previously encrypted with Mcrypt
you need to have
the options with which the encryption has been called.
There is one emminent difference for you. When you did not provide a vector at encryption you need
to get it after you encrypted the content by using the getVector()
method on the
encryption filter. Without the correct vector you will not be able to decrypt the content.
As soon as you have provided all options decryption is as simple as encryption.
// Use the default blowfish settings $filter = new Zend_Filter_Decrypt('myencryptionkey'); // Set the vector with which the content was encrypted $filter->setVector('myvector'); $decrypted = $filter->filter('encoded_text_normally_unreadable'); print $decrypted;
![]() |
Note |
---|---|
Note that you will get an exception if the mcrypt extension is not available in your environment. |
![]() |
Note |
---|---|
You should also note that all settings which be checked when you create the instance or when you call setEncryption(). If mcrypt detects problem with your settings an exception will be thrown. |
Decryption with OpenSSL
is as simple as encryption. But you need to have all data
from the person who encrypted the content.
For decryption with OpenSSL
you need:
private: Your private key which will be used for decrypting the content. The private key can be eighter a filename with path of the key file, or just the content of the key file itself.
envelope: The encrypted envelope key from the user who encrypted the content. You can eigther provide the path and filename of the key file, or just the content of the key file itself.
// Use openssl and provide a private key $filter = new Zend_Filter_Decrypt(array('adapter' => 'openssl', 'private' => '/path/to/mykey/private.pem'); // of course you can also give the envelope keys at initiation $filter->setEnvelopeKey(array('/key/from/encoder/first.pem', '/key/from/encoder/second.pem');
![]() |
Note |
---|---|
Note that the |
Optionally it could be necessary to provide the passphrase for decrypting the keys themself by
using the setPassphrase()
method.
// Use openssl and provide a private key $filter = new Zend_Filter_Decrypt(array('adapter' => 'openssl', 'private' => '/path/to/mykey/private.pem'); // of course you can also give the envelope keys at initiation $filter->setEnvelopeKey(array('/key/from/encoder/first.pem', '/key/from/encoder/second.pem'); ->setPassphrase('mypassphrase');
At last, decode the content. Our complete example for decrypting the previously encrypted content looks like this.
// Use openssl and provide a private key $filter = new Zend_Filter_Decrypt(array('adapter' => 'openssl', 'private' => '/path/to/mykey/private.pem'); // of course you can also give the envelope keys at initiation $filter->setEnvelopeKey(array('/key/from/encoder/first.pem', '/key/from/encoder/second.pem'); ->setPassphrase('mypassphrase'); $decrypted = $filter->filter('encoded_text_normally_unreadable'); print $decrypted;
This filter will encrypt any given string with the provided setting. Therefor it makes use of
Adapters. Actually there are adapters for the Mcrypt
and OpenSSL
extensions from php.
As these two encryption methodologies work completly different, also the usage of the adapters differ. You have to select the adapter you want to use when initiating the filter.
As these two encryption methodologies work completly different, also the usage of the adapters differ. You have to select the adapter you want to use when initiating the filter.
// Use the Mcrypt adapter $filter1 = new Zend_Filter_Encrypt(array('adapter' => 'mcrypt')); // Use the OpenSSL adapter $filter2 = new Zend_Filter_Encrypt(array('adapter' => 'openssl'));
To set another adapter you can also use setAdapter()
, and the getAdapter()
method to receive the actual set adapter.
// Use the Mcrypt adapter $filter = new Zend_Filter_Encrypt(); $filter->setAdapter('openssl');
![]() |
Note |
---|---|
When you do not supply the |
When you have installed the Mcrypt
extension you can use the
Mcrypt
adapter. This adapter supports the following options at initiation:
key: The encryption key with which the input will be encrypted. You need the same key for decryption.
algorithm: The algorithm which has to be used. It should be one of
the algorithm ciphers which can be found under
PHP's mcrypt ciphers. If not set it defaults to
blowfish
.
algorithm_directory: The directory where the algorithm can be found. If not set it defaults to the path set within the mcrypt extension.
mode: The encryption mode which has to be used. It should be one of
the modes which can be found under
PHP's mcrypt modes. If not set it defaults to
cbc
.
mode_directory: The directory where the mode can be found. If not set
it defaults to the path set within the mcrypt
extension.
vector: The initialization vector which shall be used. If not set it will be a random vector.
If you give a string instead of an array, this string will be used as key.
You can get/set the encryption values also afterwards with the getEncryption()
and setEncryption()
methods.
![]() |
Note |
---|---|
Note that you will get an exception if the mcrypt extension is not available in your environment. |
![]() |
Note |
---|---|
You should also note that all settings which be checked when you create the instance or when you call setEncryption(). If mcrypt detects problem with your settings an exception will be thrown. |
You can get/set the encryption vector by calling getVector()
and setVector()
.
A given string will be truncated or padded to the needed vector size of the used algorithm.
![]() |
Note |
---|---|
Note that when you are not using an own vector, you must get the vector and store it. Otherwise you will not be able to decode the encoded string. |
// Use the default blowfish settings $filter = new Zend_Filter_Encrypt('myencryptionkey'); // Set a own vector, otherwise you must call getVector() // and store this vector for later decryption $filter->setVector('myvector'); // $filter->getVector(); $encrypted = $filter->filter('text_to_be_encoded'); print $encrypted; // For decryption look at the Decrypt filter
When you have installed the OpenSSL
extension you can use the
OpenSSL
adapter. This adapter supports the following options at initiation:
public: The public key of the user whom you want to provide the encrpted content. You can give multiple public keys by using an array. You can eigther provide the path and filename of the key file, or just the content of the key file itself.
private: Your private key which will be used for encrypting the content. Also the private key can be eighter a filename with path of the key file, or just the content of the key file itself.
You can get/set the public keys also afterwards with the getPublicKey()
and setPublicKey()
methods. The private key can also be get and set with the related
getPrivateKey()
and setPrivateKey()
methods.
// Use openssl and provide a private key $filter = new Zend_Filter_Encrypt(array('adapter' => 'openssl', 'private' => '/path/to/mykey/private.pem'); // of course you can also give the public keys at initiation $filter->setPublicKey(array('/public/key/path/first.pem', '/public/key/path/second.pem');
![]() |
Note |
---|---|
Note that the |
When you want to encode also the keys, then you have to provide a passphrase with the
setPassphrase()
method. When you want to decode content which was encoded with
a passphrase you will not only need the public key, but also the passphrase to encode the
encrypted key.
// Use openssl and provide a private key $filter = new Zend_Filter_Encrypt(array('adapter' => 'openssl', 'private' => '/path/to/mykey/private.pem'); // of course you can also give the public keys at initiation $filter->setPublicKey(array('/public/key/path/first.pem', '/public/key/path/second.pem') ->setPassphrase('mypassphrase');
At last, when you use OpenSSL you need to give the receiver the encrypted content, the passphrase when have provided one, and the envelope keys for decryption.
This means for you, that you have to get the envelope keys after the encryption with the
getEnvelopeKey()
method.
So our complete example for encrypting content with OpenSSL
look like this.
// Use openssl and provide a private key $filter = new Zend_Filter_Encrypt(array('adapter' => 'openssl', 'private' => '/path/to/mykey/private.pem'); // of course you can also give the public keys at initiation $filter->setPublicKey(array('/public/key/path/first.pem', '/public/key/path/second.pem') ->setPassphrase('mypassphrase'); $encrypted = $filter->filter('text_to_be_encoded'); $envelope = $filter->getEnvelopeKey(); print $encrypted; // For decryption look at the Decrypt filter
Returns the string $value
, converting characters to their corresponding
HTML entity equivalents where they exist.
Expands all symbolic links and resolves references to '/./', '/../' and extra '/' characters in the input path and return the canonicalized absolute pathname. The resulting path will have no symbolic link, '/./' or '/../' components.
Zend_Filter_RealPath
will return FALSE
on failure, e.g. if the file does not exist. On BSD systems Zend_Filter_RealPath
doesn't fail if only the last path component doesn't exist, while other systems will return
FALSE
.
Returns the string $value
, converting alphabetic characters to lowercase as necessary.
Returns the string $value
, converting alphabetic characters to uppercase as necessary.
This filter returns the input string, with all HTML and PHP tags stripped from it, except those that have
been explicitly allowed. In addition to the ability to specify which tags are allowed, developers can
specify which attributes are allowed across all allowed tags and for specific tags only. Finally, this
filter offers control over whether comments (e.g., <!-- ... -->
) are removed or
allowed.