Ignore:
Timestamp:
2013/01/11 00:21:16 (11 years ago)
Author:
Seasoft
Message:

#1988 (国際化テンプレート:エスケープ処理) 現状コミット。

  • PHP 関数の t() の動作に影響が及んでいたので回避。
  • 文字列エスケープ処理を分離。SC_Helper_Locale#escape
  • デフォルトのエスケープに nl2br を追加。
  • 吉本様からご指示いただきました仕様を適用。
    後述されている仕様での実装をお願いしたく思います。
    懸念されています「po の中で HTML エスケープを意識する」といった必要が生じる箇所は
    それほど多くない事、現時点ではβ版リリースという事を踏まえて
    今後修正を掛けていければと考えております。
    
    取り急ぎ最小限の実装で対応できればと考えておりますので
    懸念は残す形となりますが、やはり以下の様な実装で対応できればと思います。
    
    <!--{t string="tpl_465" escape="none"}-->
    <!--{t string="tpl_465"}-->
    
    escape=""は「指定なし」と同様の振る舞いの方が分かりやすいかと思います。
    
Location:
branches/version-2_12-multilang/data
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • branches/version-2_12-multilang/data/Smarty/templates/admin/ownersstore/plugin.tpl

    r22186 r22226  
    128128    <h2><!--{t string="tpl_464"}--></h2> 
    129129    <table class="form"> 
    130         <!--{* 案1 *}--> 
    131130        <tr> 
    132             <th><!--{t string="tpl_465"}--> <!--{t string="<require>"}--></th> 
    133             <td> 
    134                 <!--{assign var=key value="plugin_file"}--> 
    135                 <span class="attention"><!--{$arrErr[$key]}--></span> 
    136                 <input type="file" name="<!--{ $key }-->" class="box45" size="43"  style="<!--{$arrErr[$key]|sfGetErrorColor}--> <!--{if $arrErr[$key]}--> background-color:<!--{$smarty.const.ERR_COLOR|h}--><!--{/if}-->"> 
    137                 <a class="btn-action" href="javascript:;" onclick="install(); return false;"><span class="btn-next"><!--{t string="tpl_466"}--></span></a> 
    138             </td> 
    139         </tr> 
    140         <!--{* 案2 *}--> 
    141         <tr> 
    142             <th><!--{t string="tpl_465"}--> <span class='attention'><!--{t string="require_mark"}--></span></th> 
     131            <th><!--{t string="tpl_465" escape="none"}--></th> 
    143132            <td> 
    144133                <!--{assign var=key value="plugin_file"}--> 
  • branches/version-2_12-multilang/data/class/helper/SC_Helper_Locale.php

    r22186 r22226  
    3939     * @var SC_Helper_Locale 
    4040     */ 
    41      static $_instance = NULL; 
    42  
    43      public $_translations = array(); 
     41    static $_instance = NULL; 
     42 
     43    public $_translations = array(); 
    4444 
    4545     /** 
     
    5050     * @return  string  a string corresponding with message alias 
    5151     */ 
    52      public static function get_locale($string, &$options) { 
     52    public static function get_locale($string, &$options) { 
    5353        is_null(SC_Helper_Locale_Ex::$_instance) and SC_Helper_Locale_Ex::$_instance = new SC_Helper_Locale_Ex(); 
    5454 
     
    7171 
    7272        $return = $string; 
     73 
    7374        // Get string list of specified language. 
    7475        if ($lang_code != 'en') { 
     
    8081        } 
    8182 
    82         $esc_types = $options['escape']; 
    83         if (is_null($esc_types) && $string[0] !== '<') { 
    84             $esc_types = 'h'; 
    85         } 
    86         foreach (explode(',', $esc_types) as $esc_type) { 
    87             switch ($esc_type) { 
    88                 case 'h': 
    89                 case 'html': 
    90                     $return = htmlspecialchars($return, ENT_QUOTES); 
    91                     break; 
    92  
    93                 case 'j': 
    94                 case 'javascript': 
    95                     // escape quotes and backslashes, newlines, etc. 
    96                     $return = strtr($return, array('\\'=>'\\\\',"'"=>"\\'",'"'=>'\\"',"\r"=>'\\r',"\n"=>'\\n','</'=>'<\/')); 
    97                     break; 
    98  
    99                 case 'nl2br': 
    100                     $return = nl2br($return, true); 
    101                     break; 
    102  
    103                 case '': 
    104                 case 'none': 
    105                     break; 
    106  
    107                 case 'htmlall': 
    108                     $return = htmlentities($return, ENT_QUOTES); 
    109                     break; 
    110  
    111                 case 'u': 
    112                 case 'url': 
    113                     $return = rawurlencode($return); 
    114                     break; 
    115  
    116                 case 'urlpathinfo': 
    117                     $return = str_replace('%2F','/',rawurlencode($return)); 
    118                     break; 
    119  
    120                 case 'quotes': 
    121                     // escape unescaped single quotes 
    122                     $return = preg_replace("%(?<!\\\\)'%", "\\'", $return); 
    123                     break; 
    124  
    125                 case 'hex': 
    126                     // escape every character into hex 
    127                     $text = ''; 
    128                     for ($x=0; $x < strlen($return); $x++) { 
    129                         $text .= '%' . bin2hex($return[$x]); 
    130                     } 
    131                     $return = $text; 
    132                     break; 
    133  
    134                 case 'hexentity': 
    135                     $text = ''; 
    136                     for ($x=0; $x < strlen($return); $x++) { 
    137                         $text .= '&#x' . bin2hex($return[$x]) . ';'; 
    138                     } 
    139                     $return = $text; 
    140                     break; 
    141  
    142                 case 'decentity': 
    143                     $text = ''; 
    144                     for ($x=0; $x < strlen($return); $x++) { 
    145                         $text .= '&#' . ord($return[$x]) . ';'; 
    146                     } 
    147                     $return = $text; 
    148                     break; 
    149  
    150                 case 'mail': 
    151                     // safe way to display e-mail address on a web page 
    152                     $return = str_replace(array('@', '.'),array(' [AT] ', ' [DOT] '), $return); 
    153                     break; 
    154  
    155                 case 'nonstd': 
    156                    // escape non-standard chars, such as ms document quotes 
    157                    $_res = ''; 
    158                    for($_i = 0, $_len = strlen($return); $_i < $_len; $_i++) { 
    159                        $_ord = ord(substr($return, $_i, 1)); 
    160                        // non-standard char, escape it 
    161                        if($_ord >= 126){ 
    162                            $_res .= '&#' . $_ord . ';'; 
    163                        } 
    164                        else { 
    165                            $_res .= substr($return, $_i, 1); 
    166                        } 
    167                    } 
    168                    $return = $_res; 
    169                     break; 
    170  
    171                 default: 
    172                     trigger_error('unknown escape type. ' . var_export(func_get_args(), true), E_USER_WARNING); 
    173                     break; 
     83        if (is_array($options['escape'])) { 
     84            foreach ($options['escape'] as $esc_type) { 
     85                $return = SC_Helper_Locale_Ex::escape($return, $esc_type); 
    17486            } 
    17587        } 
     
    272184        return $file_list; 
    273185    } 
     186 
     187    /** 
     188     * 文字列のエスケープを行う 
     189     * 
     190     * @param   string  $string     エスケープを行う文字列 
     191     * @param   string  $esc_type   エスケープの種類 
     192     * @return  string  エスケープを行った文字列 
     193     */ 
     194    static function escape($string, $esc_type) { 
     195        $return = $string; 
     196 
     197        switch ($esc_type) { 
     198            case 'h': 
     199            case 'html': 
     200                $return = htmlspecialchars($return, ENT_QUOTES); 
     201                break; 
     202 
     203            case 'j': 
     204            case 'javascript': 
     205                // escape quotes and backslashes, newlines, etc. 
     206                $return = strtr($return, array('\\'=>'\\\\',"'"=>"\\'",'"'=>'\\"',"\r"=>'\\r',"\n"=>'\\n','</'=>'<\/')); 
     207                break; 
     208 
     209            case 'nl2br': 
     210                $return = nl2br($return, true); 
     211                break; 
     212 
     213            case '': 
     214            case 'none': 
     215                break; 
     216 
     217            case 'htmlall': 
     218                $return = htmlentities($return, ENT_QUOTES); 
     219                break; 
     220 
     221            case 'u': 
     222            case 'url': 
     223                $return = rawurlencode($return); 
     224                break; 
     225 
     226            case 'urlpathinfo': 
     227                $return = str_replace('%2F','/',rawurlencode($return)); 
     228                break; 
     229 
     230            case 'quotes': 
     231                // escape unescaped single quotes 
     232                $return = preg_replace("%(?<!\\\\)'%", "\\'", $return); 
     233                break; 
     234 
     235            case 'hex': 
     236                // escape every character into hex 
     237                $text = ''; 
     238                for ($x=0; $x < strlen($return); $x++) { 
     239                    $text .= '%' . bin2hex($return[$x]); 
     240                } 
     241                $return = $text; 
     242                break; 
     243 
     244            case 'hexentity': 
     245                $text = ''; 
     246                for ($x=0; $x < strlen($return); $x++) { 
     247                    $text .= '&#x' . bin2hex($return[$x]) . ';'; 
     248                } 
     249                $return = $text; 
     250                break; 
     251 
     252            case 'decentity': 
     253                $text = ''; 
     254                for ($x=0; $x < strlen($return); $x++) { 
     255                    $text .= '&#' . ord($return[$x]) . ';'; 
     256                } 
     257                $return = $text; 
     258                break; 
     259 
     260            case 'mail': 
     261                // safe way to display e-mail address on a web page 
     262                $return = str_replace(array('@', '.'),array(' [AT] ', ' [DOT] '), $return); 
     263                break; 
     264 
     265            case 'nonstd': 
     266                // escape non-standard chars, such as ms document quotes 
     267                $_res = ''; 
     268                for($_i = 0, $_len = strlen($return); $_i < $_len; $_i++) { 
     269                    $_ord = ord(substr($return, $_i, 1)); 
     270                    // non-standard char, escape it 
     271                    if($_ord >= 126){ 
     272                        $_res .= '&#' . $_ord . ';'; 
     273                    } 
     274                    else { 
     275                        $_res .= substr($return, $_i, 1); 
     276                    } 
     277                } 
     278                $return = $_res; 
     279                break; 
     280 
     281            default: 
     282                trigger_error('unknown escape type. ' . var_export(func_get_args(), true), E_USER_WARNING); 
     283                break; 
     284        } 
     285 
     286        return $return; 
     287    } 
    274288} 
  • branches/version-2_12-multilang/data/locales/ja.po

    r22209 r22226  
    33"Project-Id-Version: EC-CUBE Core\n" 
    44"POT-Creation-Date: 2012-05-07 13T_39+0900\n" 
    5 "PO-Revision-Date: 2013-01-08 15:42+0900\n" 
     5"PO-Revision-Date: 2013-01-11 00:11+0900\n" 
    66"Last-Translator: MATSUDA Terutaka <matsudaterutaka@gmail.com>\n" 
    77"Language-Team: \n" 
     
    50325032 
    50335033msgid "tpl_465" 
    5034 msgstr "プラグイン" 
     5034msgstr "プラグイン<span class='attention'> *</span>" 
    50355035 
    50365036msgid "tpl_466" 
     
    61326132msgid "pt_suffix" 
    61336133msgstr "pt" 
    6134  
    6135 msgid "<require>" 
    6136 msgstr "<span class='attention'>*</span>" 
    6137  
    6138 msgid "require_mark" 
    6139 msgstr "*" 
  • branches/version-2_12-multilang/data/smarty_extends/function.t.php

    r22186 r22226  
    4646    } 
    4747    // エスケープの指定がある場合、オプションに移す 
    48     if (isset($params['escape'])) { 
    49         $options['escape'] = $params['escape']; 
    50         unset($params['escape']); 
     48    if (strlen($params['escape']) >= 1) { 
     49        $options['escape'] = explode(',', $params['escape']); 
     50    } else { 
     51        $options['escape'] = array('h', 'nl2br'); 
    5152    } 
     53    unset($params['escape']); 
    5254 
    5355    return t($string, $params, $options); 
Note: See TracChangeset for help on using the changeset viewer.