Programmer's Reference Guide
| Introduction |
Classes de filtre standards
Zend Framework est fourni avec un jeu de filtres standards, qui sont directement utilisables.
Alnum
Retourne la chaîne $value, en retirant tout sauf les caractères alphabétiques et numériques.
Ce filtre inclue une option permettant d'autoriser ou non les caractères espace.
Note: Les caractères alphabétiques comprennent les caractères destinés à constituer des mots dans chaque langue. Cependant l'alphabet anglais est aussi utilisé pour les langues suivantes : chinois, japonais et coréen. La langue est spécifiée par
Zend_Locale.
Alpha
Retourne la chaîne $value, en retirant tout sauf les caractères alphabétiques. Ce filtre
inclue une option permettant d'autoriser ou non les caractères espace.
BaseName
En passant une chaîne contenant un chemin vers un fichier, ce filtre retourne le nom de base du fichier.
Decrypt
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.
Decryption with Mcrypt
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
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
OpenSSLadapter will not work when you do not provide valid keys.
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;
Digits
Retourne la chaîne $value, en retirant tout sauf les caractères numériques.
Dir
Retourne la partie correspondant au nom de dossier dans le chemin spécifié.
Encrypt
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
adapteroption or do not use setAdapter, then theMcryptadapter will be used per default.
Encryption with Mcrypt
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
mcryptextension. -
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
Encryption with OpenSSL
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
OpenSSLadapter will not work when you do not provide valid keys.
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 decode 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
HtmlEntities
Retourne la chaîne $value, en convertissant les caractères en leurs entités HTML
correspondantes quand elles existent.
Int
Retourne la valeur (int) $value.
Int
Retourne la valeur $value en enlevant les caractères représentant une nouvelle ligne.
RealPath
Étend tous les liens symboliques et résout les références à '/./', '/../' et les caractères supplémentaires '/' dans le chemin fourni et retourne le nom de chemin absolu. Le chemin retourné ne contient plus de liens symboliques, ou de composantes '/./' ou '/../'.
Zend_Filter_RealPath retournera FALSE en cas d'échec, par exemple si un fichier
n'existe pas. Sur les systèmes BSD Zend_Filter_RealPath n'échouera pas si seul le dernier composant
n'existe pas, tandis que tous les autres systèmes retourneront FALSE.
StringToLower
Retourne la chaîne $value, en convertissant les caractères alphabétiques en minuscules si
nécessaire.
StringToUpper
Retourne la chaîne $value, en convertissant les caractères alphabétiques en majuscules si
nécessaire.
StringTrim
Retourne la chaîne $value en supprimant les caractères vides en début et fin de
chaîne.
StripTags
Ce filtre retourne une chaîne, où toutes les balises HTML et PHP sont supprimées, exceptées celles qui
sont explicitement autorisées. En plus de pouvoir spécifier quelles balises sont autorisées, les développeurs
peuvent spécifier quels attributs sont autorisés soit pour toutes les balises autorisées soit pour des balises
spécifiques seulement. Pour finir, ce filtre permet de contrôler si les commentaires (par exemple <!--
... -->) sont refusés ou autorisés.
| Introduction |
