OSDN Git Service

Remove PHP closing tag
[ethna/ethna.git] / class / Plugin / Validator / Strmaxcompat.php
1 <?php
2 // vim: foldmethod=marker
3 /**
4  *  Strmaxcompat.php
5  *
6  *  @author     Yoshinari Takaoka <takaoka@beatcraft.com>
7  *  @license    http://www.opensource.org/licenses/bsd-license.php The BSD License
8  *  @package    Ethna
9  *  @version    $Id$
10  */
11
12 // {{{ Ethna_Plugin_Validator_Strmaxcompat
13 /**
14  *  最大値チェックプラグイン
15  *  (マルチバイト文字列(EUC_JP)用. Ethna 2.3.x までの互換性保持用)
16  *
17  *  NOTE: 
18  *    - EUC_JP 専用のプラグインです。
19  *    - クライアントエンコーディングがEUC_JP以外の場合は、入力を無条件でEUC_JPに変換します 
20  *      (但し mbstringが入っていない場合は除く) 
21  *    - エラーメッセージは、全角半角を区別したものが出力されます。
22  *
23  *  @author     Yoshinari Takaoka <takaoka@beatcraft.com>
24  *  @access     public
25  *  @package    Ethna
26  */
27 class Ethna_Plugin_Validator_Strmaxcompat extends Ethna_Plugin_Validator
28 {
29     /** @var    bool    配列を受け取るかフラグ */
30     var $accept_array = false;
31
32     /**
33      *  最大値のチェックを行う
34      *
35      *  @access public
36      *  @param  string  $name       フォームの名前
37      *  @param  mixed   $var        フォームの値
38      *  @param  array   $params     プラグインのパラメータ
39      *  @return true: 成功  Ethna_Error: エラー
40      */
41     function &validate($name, $var, $params)
42     {
43         $true = true;
44         $type = $this->getFormType($name);
45         if (isset($params['strmaxcompat']) == false || $this->isEmpty($var, $type)) {
46             return $true;
47         }
48
49         $ctl = $this->backend->getController();
50         $client_enc = $ctl->getClientEncoding();
51         if (mb_enabled()
52         && (strcasecmp('EUC-JP', $client_enc) != 0
53          && strcasecmp('eucJP-win', $client_enc) != 0)) {
54             $var = mb_convert_encoding($var, 'EUC-JP', $client_enc);
55         }
56
57         if ($type == VAR_TYPE_STRING) {
58             $max_param = $params['strmaxcompat'];
59             if (strlen($var) > $max_param) {
60                 if (isset($params['error'])) {
61                     $msg = $params['error'];
62                 } else {
63                     $msg = _et('Please input less than %d full-size (%d half-size) characters to {form}.');
64                 }
65                 return Ethna::raiseNotice($msg, E_FORM_MAX_STRING,
66                             array(intval($max_param/2), $max_param));
67             }
68         }
69
70         return $true;
71     }
72 }
73 // }}}
74