Programmer's Reference Guide

導入

標準のフィルタクラス群

Zend Framework には、すぐに使える標準のフィルタ群が同梱されています。

Alnum

文字列 $value から、アルファベットおよび数字以外を取り除いたものを返します。 このフィルタでは、オプションとして空白文字を認めることもできます。

注意: アルファベットとは各言語で単語を構成する文字を意味します。 ただし、以下の言語では英語のアルファベットをアルファベットとして扱います:中国語、日本語、韓国語。 言語はZend_Localeで指定されます。

Alpha

文字列 $value から、アルファベット以外を取り除いたものを返します。 このフィルタでは、オプションとして空白文字を認めることもできます。

BaseName

ファイルへのパスを含む文字列を受け取り、 ファイルのベース名の部分のみを返します。

Callback

このフィルタにより、Zend_Filterとともに自分自身のメソッドを使うことができます。 機能を果たすメソッドすでにあるとき、新しいフィルタを生成する必要はありません。

文字列を逆にするフィルタを生成したいとしましょう。

  1. $filter = new Zend_Filter_Callback('strrev');
  2.  
  3. print $filter->filter('Hello!');
  4. // "!olleH"を返します

おわかりのように、自分自身のフィルタを定義するために本当に簡単にコールバックを使えます。 メソッド(それはクラス内で定義されます)をコールバックとして配列を与えることによって使うことも可能です。

  1. // クラスの定義
  2. class MyClass
  3. {
  4.     public function Reverse($param);
  5. }
  6.  
  7. // フィルター定義
  8. $filter = new Zend_Filter_Callback(array('MyClass', 'Reverse'));
  9. print $filter->filter('Hello!');

実際に設定されているコールバックを取得するにはgetCallback()を使い、 他のコールバックを設定するにはsetCallback()を使います。

フィルタが実行されるとき、 呼ばれるメソッドに配列として与えられるデフォルト・パラメータを定義できます。 この配列は、フィルターされた値で結合されます。

  1. $filter = new Zend_Filter_Callback(
  2.     'MyMethod',
  3.     array('key' => 'param1', 'key2' => 'param2')
  4. );
  5. $filter->filter(array('value' => 'Hello'));

手動で上記のメソッド定義を呼ぶと、それはこのように見えます:

  1. $value = MyMethod('Hello', 'param1', 'param2');

注意: 呼ばれることができないコールバック・メソッドを定義すると、 例外が発生する点に注意しなければなりません。

Decrypt

このフィルタは、指定した文字列を指定した設定で復号します。 復号の際に、アダプタを使用します。実際には、PHP の Mcrypt および OpenSSL 拡張モジュール用のアダプタを使用します。

コンテンツの暗号化方法の詳細については Encrypt フィルタを参照ください。 基本的な内容は Encrypt フィルタで網羅されているので、 ここでは追加のメソッドや復号時に固有のことなどについてのみ説明します。

Mcrypt の復号

Mcrypt で暗号化したコンテンツを復号するには、 暗号化を行った際に指定したオプションが必要です。

ここでひとつ暗号化時との大きな違いがあります。 暗号化の際にベクトルを指定しなかった場合は、コンテンツを暗号化した後で フィルタの getVector() メソッドを使用してベクトルを取得する必要があります。 正しいベクトルがなければ、コンテンツを復号することはできません。

オプションを指定しさえすれば、復号は暗号化と同じくらい単純なことです。

  1. // デフォルト設定の blowfish を使用します
  2. $filter = new Zend_Filter_Decrypt('myencryptionkey');
  3.  
  4. // コンテンツの暗号化用ベクトルを設定します
  5. $filter->setVector('myvector');
  6.  
  7. $decrypted = $filter->filter('encoded_text_normally_unreadable');
  8. print $decrypted;

注意: mcrypt 拡張モジュールが使用できない場合は例外が発生することに注意しましょう。

注意: また、インスタンス作成時あるいは setEncryption() をコールした際にすべての設定がチェックされることにも注意しましょう。 設定内容に問題があることを mcrypt が検知すると、例外がスローされます。

OpenSSL の復号

OpenSSL での復号は、暗号化と同様にシンプルです。 しかし、コンテンツを暗号化した人からすべてのデータを受け取る必要があります。

OpenSSL の復号には、以下が必要となります。

  • private: コンテンツの復号に使用する秘密鍵。 鍵ファイルのパスとファイル名を指定するか、 あるいは単に鍵ファイルの内容そのものを指定することもできます。

  • envelope: コンテンツを暗号化した人から受け取った、 暗号化されたエンベロープ鍵。 鍵ファイルのパスとファイル名を指定するか、 あるいは単に鍵ファイルの内容そのものを指定することもできます。

  1. // openssl を使用し、秘密鍵を指定します
  2. $filter = new Zend_Filter_Decrypt(array(
  3.     'adapter' => 'openssl',
  4.     'private' => '/path/to/mykey/private.pem'
  5. ));
  6.  
  7. // もちろん、初期化時にエンベロープ鍵を指定することもできます
  8. $filter->setEnvelopeKey(array(
  9.     '/key/from/encoder/first.pem',
  10.     '/key/from/encoder/second.pem'
  11. ));

注意: OpenSSL アダプタは、正しい鍵を渡さないと動作しないことに注意しましょう。

オプションで、パスフレーズを渡さなければ鍵を復号できないようにすることもできます。 そのために使用するのが setPassphrase() メソッドです。

  1. // openssl を使用し、秘密鍵を指定します
  2. $filter = new Zend_Filter_Decrypt(array(
  3.     'adapter' => 'openssl',
  4.     'private' => '/path/to/mykey/private.pem'
  5. ));
  6.  
  7. // もちろん、初期化時にエンベロープ鍵を指定することもできます
  8. $filter->setEnvelopeKey(array(
  9.     '/key/from/encoder/first.pem',
  10.     '/key/from/encoder/second.pem'
  11. ));
  12. $filter->setPassphrase('mypassphrase');

最後に、コンテンツを復号します。 暗号化したコンテンツの復号を行う完全な例は、このようになります。

  1. // openssl を使用し、秘密鍵を指定します
  2. $filter = new Zend_Filter_Decrypt(array(
  3.     'adapter' => 'openssl',
  4.     'private' => '/path/to/mykey/private.pem'
  5. ));
  6.  
  7. // もちろん、初期化時にエンベロープ鍵を指定することもできます
  8. $filter->setEnvelopeKey(array(
  9.     '/key/from/encoder/first.pem',
  10.     '/key/from/encoder/second.pem'
  11. ));
  12. $filter->setPassphrase('mypassphrase');
  13.  
  14. $decrypted = $filter->filter('encoded_text_normally_unreadable');
  15. print $decrypted;

Digits

文字列 $value から、数字以外を取り除いたものを返します。

Dir

パス文字列からディレクトリ名を返します。

Encrypt

このフィルタは、指定した設定で任意の文字列を暗号化します。 暗号化の際に、アダプタを使用します。実際には、PHP の Mcrypt および OpenSSL 拡張モジュール用のアダプタを使用します。

これら 2 つの暗号化手法はまったく異なる方式なので、 アダプタの使用法もそれぞれ異なります。 フィルタを初期化する際に、どのアダプタを使うかを選ばなければなりません。

  1. // Mcrypt アダプタを使用します
  2. $filter1 = new Zend_Filter_Encrypt(array('adapter' => 'mcrypt'));
  3.  
  4. // OpenSSL アダプタを使用します
  5. $filter2 = new Zend_Filter_Encrypt(array('adapter' => 'openssl'));

別のアダプタを設定するために setAdapter() を使用することもできます。また getAdapter() メソッドで、実際に設定されているアダプタを取得することができます。

  1. // Mcrypt アダプタを使用します
  2. $filter = new Zend_Filter_Encrypt();
  3. $filter->setAdapter('openssl');

注意: adapter オプションを指定しなかったり setAdapter を使用しなかったりした場合は、デフォルトで Mcrypt アダプタを使用します。

Mcrypt での暗号化

Mcrypt 拡張モジュールをインストールすると、 Mcrypt アダプタが使えるようになります。 このアダプタは、初期化時のオプションとして以下をサポートしています。

  • key: 暗号化用の鍵。 入力を暗号化する際に使用します。 復号する際にも同じ鍵が必要です。

  • algorithm: 使用するアルゴリズム。 » PHP マニュアルの mcrypt のページ であげられている暗号化アルゴリズムのいずれかでなければなりません。 省略した場合のデフォルトは blowfish です。

  • algorithm_directory: アルゴリズムが存在するディレクトリ。 省略した場合のデフォルトは、mcrypt 拡張モジュールで設定されているパスです。

  • mode: 使用する暗号化モード。 » PHP マニュアルの mcrypt のページ であげられているモードのいずれかでなければなりません。 省略した場合のデフォルトは cbc です。

  • mode_directory: モードが存在するディレクトリ。 省略した場合のデフォルトは、mcrypt 拡張モジュールで設定されているパスです。

  • vector: 使用する初期化ベクトル。 省略した場合はランダムなベクトルとなります。

  • salt: キーを salt 値として使用するかどうか。 使用すると、暗号化に使用するキー自体も暗号化されます。 デフォルトは false です。

配列ではなく文字列を指定した場合は、その文字列を鍵として使用します。

初期化した後で暗号化の値を取得したり設定したりするには、それぞれ getEncryption() および setEncryption() メソッドを使用します。

注意: mcrypt 拡張モジュールが使用できない場合は例外が発生することに注意しましょう。

注意: また、インスタンス作成時あるいは setEncryption() をコールした際にすべての設定がチェックされることにも注意しましょう。 設定内容に問題があることを mcrypt が検知すると、例外がスローされます。

暗号化ベクトルの取得や設定には、それぞれ getVector() および setVector() を使用可能です。指定した文字列が、 そのアルゴリズムに必要なベクトルのサイズに応じて切り詰められたり伸ばされたりします。

注意: 自前のベクトル以外を使用する場合は、 そのベクトルを取得してどこかに保存しておかなければならないことに注意しましょう。 そうしないと、文字列が復号できなくなります。

  1. // デフォルト設定の blowfish を使用します
  2. $filter = new Zend_Filter_Encrypt('myencryptionkey');
  3.  
  4. // 自前のベクトルを設定します。それ以外の場合は getVector()
  5. // をコールしてベクトルを保存しておかないと、後で復号できなくなります
  6. $filter->setVector('myvector');
  7. // $filter->getVector();
  8.  
  9. $encrypted = $filter->filter('text_to_be_encoded');
  10. print $encrypted;
  11.  
  12. // 復号の方法は Decrypt フィルタを参照ください

OpenSSL での暗号化

OpenSSL 拡張モジュールをインストールすると、 OpenSSL アダプタが使えるようになります。 このアダプタは、初期化時のオプションとして以下をサポートしています。

  • public: 暗号化したコンテンツを渡したい相手の公開鍵。 複数の公開鍵を指定するには、配列を使用します。 鍵ファイルのパスとファイル名を指定するか、 あるいは単に鍵ファイルの内容そのものを指定することもできます。

  • private: コンテンツの暗号化に使用する、あなたの秘密鍵。 鍵ファイルのパスとファイル名を指定するか、 あるいは単に鍵ファイルの内容そのものを指定することもできます。

後から公開鍵を取得あるいは設定するには、getPublicKey() および setPublicKey() メソッドを使用します。 秘密鍵についても、getPrivateKey() および setPrivateKey() メソッドで取得あるいは設定することができます。

  1. // openssl を使用し、秘密鍵を指定します
  2. $filter = new Zend_Filter_Encrypt(array(
  3.     'adapter' => 'openssl',
  4.     'private' => '/path/to/mykey/private.pem'
  5. ));
  6.  
  7. // もちろん、初期化時に公開鍵を指定することもできます
  8. $filter->setPublicKey(array(
  9.     '/public/key/path/first.pem',
  10.     '/public/key/path/second.pem'
  11. ));

注意: OpenSSL アダプタは、正しい鍵を渡さないと動作しないことに注意しましょう。

鍵自体も暗号化したい場合は、パスフレーズを setPassphrase() メソッドで渡します。 パスフレーズつきで暗号化したコンテンツを復号したい場合は、 公開鍵だけではなく (暗号化された鍵を復号するための) パスフレーズも必要となります。

  1. // openssl を使用し、秘密鍵を指定します
  2. $filter = new Zend_Filter_Encrypt(array(
  3.     'adapter' => 'openssl',
  4.     'private' => '/path/to/mykey/private.pem'
  5. ));
  6.  
  7. // もちろん、初期化時に公開鍵を指定することもできます
  8. $filter->setPublicKey(array(
  9.     '/public/key/path/first.pem',
  10.     '/public/key/path/second.pem'
  11. ));
  12. $filter->setPassphrase('mypassphrase');

最後に、OpenSSL を使用した場合に受け手に渡す必要があるものをまとめます。 暗号化されたコンテンツ、パスフレーズを使用した場合はそのパスフレーズ、 そして復号用のエンベロープ鍵。これらが必要となります。

つまり、暗号化を終えたらエンベロープ鍵を取得する必要があるということです。 取得するには getEnvelopeKey() メソッドを使用します。

OpenSSL でコンテンツの暗号化を行う完全な例は、このようになります。

  1. // openssl を使用し、秘密鍵を指定します
  2. $filter = new Zend_Filter_Encrypt(array(
  3.     'adapter' => 'openssl',
  4.     'private' => '/path/to/mykey/private.pem'
  5. ));
  6.  
  7. // もちろん、初期化時に公開鍵を指定することもできます
  8. $filter->setPublicKey(array(
  9.     '/public/key/path/first.pem',
  10.     '/public/key/path/second.pem'
  11. ));
  12. $filter->setPassphrase('mypassphrase');
  13.  
  14. $encrypted = $filter->filter('text_to_be_encoded');
  15. $envelope  = $filter->getEnvelopeKey();
  16. print $encrypted;
  17.  
  18. // 復号の方法は Decrypt フィルタを参照ください

HtmlEntities

文字列 $value について、 HTML エンティティが存在するものについてはそのエンティティに変換したものを返します。

Int

(int) $value を返します。

LocalizedToNormalized

This filter will change any given localized input to it's normalized representation. It uses in Background Zend_Locale to do this transformation for you.

This allows your user to enter informations in his own language notation, and you can then store the normalized value into your database for example.

注意: Please note that normalization is not equal to translation. This filter can not translate strings from one language into another like you could expect with months or names of days.

The following input types can be normalized:

  • integer: Integer numbers, which are localized, will be normalized to the english notation.

  • float: Float numbers, which are localized, will be normalized to the english notation.

  • numbers: Other numbers, like real, will be normalized to the english notation.

  • time: Time values, will be normalized to a named array.

  • date: Date values, will be normalized to a named array.

Any other input will be returned as it, without changing it.

注意: You should note that normalized output is always given as string. Otherwise your environment would transfor the normalized output automatically to the notation used by the locale your environment is set to.

Normalization for numbers

Any given number like integer, float or real value, can be normalized. Note, that numbers in scientific notation, can actually not be handled by this filter.

So how does this normalization work in detail for numbers:

// Initiate the filter
$filter = new Zend_Filter_LocalizedToNormalized();
$filter->filter('123.456,78');
// returns the value '123456.78'

Let's expect you have set the locale 'de' as application wide locale. Zend_Filter_LocalizedToNormalized will take the set locale and use it to detect which sort of input you gave. In our example it was a value with precision. Now the filter will return you the normalized representation for this value as string.

You can also control how your normalized number has to look like. Therefor you can give all options which are also used by Zend_Locale_Format. The most common are:

  • date_format

  • locale

  • precision

For details about how these options are used take a look into this Zend_Locale chapter.

Below is a example with defined precision so you can see how options work:

// Numeric Filter
$filter = new Zend_Filter_LocalizedToNormalized(array('precision' => 2));

$filter->filter('123.456');
// returns the value '123456.00'

$filter->filter('123.456,78901');
// returns the value '123456.79'

Normalization for date and time

Input for date and time values can also be normalized. All given date and time values will be returned as array, where each date part is given within a own key.

// Initiate the filter
$filter = new Zend_Filter_LocalizedToNormalized();
$filter->filter('12.April.2009');
// returns array('day' => '12', 'month' => '04', 'year' => '2009')

Let's expect you have set the locale 'de' again. Now the input is automatically detected as date, and you will get a named array in return.

Of course you can also control how your date input looks like with the date_format and the locale option.

// Date Filter
$filter = new Zend_Filter_LocalizedToNormalized(
    array('date_format' => 'ss:mm:HH')
);

$filter->filter('11:22:33');
// returns array('hour' => '33', 'minute' => '22', 'second' => '11')

NormalizedToLocalized

This filter is the reverse of the filter Zend_Filter_LocalizedToNormalized and will change any given normalized input to it's localized representation. It uses in Background Zend_Locale to do this transformation for you.

This allows you to give your user any stored normalised value in a localized manner, your user is more common to.

注意: Please note that localization is not equal to translation. This filter can not translate strings from one language into another like you could expect with months or names of days.

The following input types can be localized:

  • integer: Integer numbers, which are normalized, will be localized to the set notation.

  • float: Float numbers, which are normalized, will be localized to the set notation.

  • numbers: Other numbers, like real, will be localized to the set notation.

  • time: Time values, will be localized to a string.

  • date: Date values, will be normalized to a string.

Any other input will be returned as it, without changing it.

Localization for numbers

Any given number like integer, float or real value, can be localized. Note, that numbers in scientific notation, can actually not be handled by this filter.

So how does localization work in detail for numbers:

// Initiate the filter
$filter = new Zend_Filter_NormalizedToLocalized();
$filter->filter(123456.78);
// returns the value '123.456,78'

Let's expect you have set the locale 'de' as application wide locale. Zend_Filter_NormalizedToLocalized will take the set locale and use it to detect which sort of output you want to have. In our example it was a value with precision. Now the filter will return you the localized representation for this value as string.

You can also control how your localized number has to look like. Therefor you can give all options which are also used by Zend_Locale_Format. The most common are:

  • date_format

  • locale

  • precision

For details about how these options are used take a look into this Zend_Locale chapter.

Below is a example with defined precision so you can see how options work:

// Numeric Filter
$filter = new Zend_Filter_NormalizedToLocalized(array('precision' => 2));

$filter->filter(123456);
// returns the value '123.456,00'

$filter->filter(123456.78901);
// returns the value '123.456,79'

Localization for date and time

Normalized for date and time values can also be localized. All given date and time values will be returned as string, with the format defined by the set locale.

// Initiate the filter
$filter = new Zend_Filter_NormalizedToLocalized();
$filter->filter(array('day' => '12', 'month' => '04', 'year' => '2009');
// returns '12.04.2009'

Let's expect you have set the locale 'de' again. Now the input is automatically detected as date, and will be returned in the format defined by the locale 'de'.

Of course you can also control how your date input looks like with the date_format, and the locale option.

// Date Filter
$filter = new Zend_Filter_LocalizedToNormalized(
    array('date_format' => 'ss:mm:HH')
);

$filter->filter(array('hour' => '33', 'minute' => '22', 'second' => '11'));
// returns '11:22:33'

StripNewlines

文字列 $value から一切の改行制御文字を取り除いたものを返します。

RealPath

このフィルタは与えられたリンクとパス名を解決して、正規化された絶対パス名を返します。 '/./''/../'への参照、 及び、入力パスの余分な'/'記号は取り除かれます。 結果のパスにはいかなるシンボリックリンクも無く、 '/./''/../'文字もありません。

たとえばファイルが存在しない場合、 Zend_Filter_RealPathは失敗するとFALSEを返します。 もし最後のパスのコンポーネントだけが存在しない場合、 BSDシステムではZend_Filter_RealPathは失敗しますが、 他のシステムではFALSEを返します。

  1. $filter = new Zend_Filter_RealPath();
  2. $path   = '/www/var/path/../../mypath';
  3. $filtered = $filter->filter();
  4.  
  5. // '/www/mypath' を返します。

それらが存在しないとき、 たとえば、生成したいパスのために実際のパスを取得したいとき、 パスを得るためにしばしば役に立ちます。 初期化でFALSEを渡すこともできますし、 それを設定するために setExists()を使うこともできます。

  1. $filter = new Zend_Filter_RealPath(false);
  2. $path   = '/www/var/path/../../non/existing/path';
  3. $filtered = $filter->filter();
  4.  
  5. // file_exists または realpath が false を返すときでも '/www/non/existing/path' を返します。

StringToLower

文字列 $value の英字を小文字に変換したものを返します。

StringToUpper

文字列 $value の英字を大文字に変換したものを返します。

StringTrim

文字列 $value の先頭と末尾から文字を取り除いたものを返します。

StripTags

入力文字列からすべての HTML タグおよび PHP タグを取り除いた結果を返します。 ただし明示的に許可したタグは取り除きません。 どのタグを許可するかだけではなく、すべてのタグにおいてどの属性を許可するか、 特定のタグだけで許可する属性は何かなども指定できます。 また、コメント (<!-- ... -->) を除去するかそのまま残すかも指定できます。


導入
blog comments powered by Disqus

Select a Version

Languages Available

Components

Search the Manual