From c05ccc5d490812b7ccdc5e6dead204f06b21c13e Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Sat, 11 Jun 2011 21:30:19 -0500 Subject: [PATCH] moved entities method to html class and added encoding configuration option. --- application/config/application.php | 12 ++++++++++++ system/form.php | 12 ++++++------ system/html.php | 27 +++++++++++++++++++-------- system/str.php | 15 ++++----------- 4 files changed, 41 insertions(+), 25 deletions(-) diff --git a/application/config/application.php b/application/config/application.php index efe0b20a..adc39017 100644 --- a/application/config/application.php +++ b/application/config/application.php @@ -29,6 +29,18 @@ 'language' => 'en', + /* + |-------------------------------------------------------------------------- + | Application Character Encoding + |-------------------------------------------------------------------------- + | + | This default character encoding used by your application. This is the + | character encoding that will be used by the Str, Text, and Form classes. + | + */ + + 'encoding' => 'UTF-8', + /* |-------------------------------------------------------------------------- | Application Timezone diff --git a/system/form.php b/system/form.php index ddbe2b5c..b596216c 100644 --- a/system/form.php +++ b/system/form.php @@ -22,12 +22,12 @@ public static function open($action = null, $method = 'POST', $attributes = arra $action = URL::to($action); - $attributes['action'] = $action; + $attributes['action'] = HTML::entities($action); $attributes['method'] = ($method == 'GET' or $method == 'POST') ? $method : 'POST'; if ( ! array_key_exists('accept-charset', $attributes)) { - $attributes['accept-charset'] = 'UTF-8'; + $attributes['accept-charset'] = Config::get('application.encoding'); } $html = ''; @@ -142,7 +142,7 @@ public static function submit($value, $attributes = array()) */ public static function button($value, $attributes = array()) { - return ''.$value.''.PHP_EOL; + return ''.HTML::entities($value).''.PHP_EOL; } /** @@ -221,7 +221,7 @@ public static function textarea($name, $value = '', $attributes = array()) $attributes['cols'] = 50; } - return ''.Str::entities($value).''.PHP_EOL; + return ''.HTML::entities($value).''.PHP_EOL; } /** @@ -243,10 +243,10 @@ public static function select($name, $options = array(), $selected = null, $attr { $option_attributes = array(); - $option_attributes['value'] = $value; + $option_attributes['value'] = HTML::entities($value); $option_attributes['selected'] = ($value == $selected) ? 'selected' : null; - $html_options[] = ''.$display.''; + $html_options[] = ''.HTML::entities($display).''; } return ''.implode('', $html_options).''.PHP_EOL; diff --git a/system/html.php b/system/html.php index fa8a35f7..f044234b 100644 --- a/system/html.php +++ b/system/html.php @@ -2,6 +2,17 @@ class HTML { + /** + * Convert HTML characters to entities. + * + * @param string $value + * @return string + */ + public static function entities($value) + { + return htmlentities($value, ENT_QUOTES, Config::get('application.encoding'), false); + } + /** * Generate a JavaScript reference. * @@ -10,7 +21,7 @@ class HTML { */ public static function script($url) { - return ''.PHP_EOL; + return ''.PHP_EOL; } /** @@ -21,7 +32,7 @@ public static function script($url) */ public static function style($url, $media = 'all') { - return ''.PHP_EOL; + return ''.PHP_EOL; } /** @@ -35,7 +46,7 @@ public static function style($url, $media = 'all') */ public static function link($url, $title, $attributes = array(), $https = false) { - return ''.Str::entities($title).''; + return ''.static::entities($title).''; } /** @@ -71,7 +82,7 @@ public static function mailto($email, $title = null, $attributes = array()) $title = $email; } - return ''.$title.''; + return ''.static::entities($title).''; } /** @@ -95,8 +106,8 @@ public static function email($email) */ public static function image($url, $alt = '', $attributes = array()) { - $attributes['alt'] = Str::entities($alt); - return ''; + $attributes['alt'] = static::entities($alt); + return ''; } /** @@ -164,7 +175,7 @@ private static function list_elements($type, $list, $attributes) foreach ($list as $key => $value) { - $html .= '
  • '.Str::entities($value).'
  • '; + $html .= '
  • '.static::entities($value).'
  • '; } return '<'.$type.static::attributes($attributes).'>'.$html.''; @@ -184,7 +195,7 @@ public static function attributes($attributes) { if ( ! is_null($value)) { - $html[] = $key.'="'.Str::entities($value).'"'; + $html[] = $key.'="'.static::entities($value).'"'; } } diff --git a/system/str.php b/system/str.php index 5a0415eb..17370e7f 100644 --- a/system/str.php +++ b/system/str.php @@ -2,13 +2,6 @@ class Str { - /** - * The default encoding. - * - * @var string - */ - private static $encoding = 'UTF-8'; - /** * Convert HTML characters to entities. * @@ -17,7 +10,7 @@ class Str { */ public static function entities($value) { - return htmlentities($value, ENT_QUOTES, static::$encoding, false); + return htmlentities($value, ENT_QUOTES, Config::get('application.encoding'), false); } /** @@ -28,7 +21,7 @@ public static function entities($value) */ public static function lower($value) { - return function_exists('mb_strtolower') ? mb_strtolower($value, static::$encoding) : strtolower($value); + return function_exists('mb_strtolower') ? mb_strtolower($value, Config::get('application.encoding')) : strtolower($value); } /** @@ -39,7 +32,7 @@ public static function lower($value) */ public static function upper($value) { - return function_exists('mb_strtoupper') ? mb_strtoupper($value, static::$encoding) : strtoupper($value); + return function_exists('mb_strtoupper') ? mb_strtoupper($value, Config::get('application.encoding')) : strtoupper($value); } /** @@ -50,7 +43,7 @@ public static function upper($value) */ public static function title($value) { - return (function_exists('mb_convert_case')) ? mb_convert_case($value, MB_CASE_TITLE, static::$encoding) : ucwords(strtolower($value)); + return (function_exists('mb_convert_case')) ? mb_convert_case($value, MB_CASE_TITLE, Config::get('application.encoding')) : ucwords(strtolower($value)); } /**