Derek Jones | 98badc1 | 2010-03-02 13:08:02 -0600 | [diff] [blame] | 1 | <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); |
| 2 | /** |
| 3 | * CodeIgniter |
| 4 | * |
Greg Aker | 741de1c | 2010-11-10 14:52:57 -0600 | [diff] [blame] | 5 | * An open source application development framework for PHP 5.1.6 or newer |
Derek Jones | 98badc1 | 2010-03-02 13:08:02 -0600 | [diff] [blame] | 6 | * |
| 7 | * @package CodeIgniter |
| 8 | * @author ExpressionEngine Dev Team |
Greg Aker | 0711dc8 | 2011-01-05 10:49:40 -0600 | [diff] [blame] | 9 | * @copyright Copyright (c) 2008 - 2011, EllisLab, Inc. |
Derek Jones | 98badc1 | 2010-03-02 13:08:02 -0600 | [diff] [blame] | 10 | * @license http://codeigniter.com/user_guide/license.html |
| 11 | * @link http://codeigniter.com |
Pascal Kriete | 5b2d2da | 2010-11-04 17:23:40 -0400 | [diff] [blame] | 12 | * @since Version 2.0 |
Derek Jones | 98badc1 | 2010-03-02 13:08:02 -0600 | [diff] [blame] | 13 | * @filesource |
| 14 | */ |
| 15 | |
| 16 | // ------------------------------------------------------------------------ |
| 17 | |
| 18 | /** |
Pascal Kriete | aaec1e4 | 2011-01-20 00:01:21 -0500 | [diff] [blame] | 19 | * Utf8 Class |
Derek Jones | 98badc1 | 2010-03-02 13:08:02 -0600 | [diff] [blame] | 20 | * |
Pascal Kriete | aaec1e4 | 2011-01-20 00:01:21 -0500 | [diff] [blame] | 21 | * Provides support for UTF-8 environments |
Derek Jones | 98badc1 | 2010-03-02 13:08:02 -0600 | [diff] [blame] | 22 | * |
| 23 | * @package CodeIgniter |
| 24 | * @subpackage Libraries |
Pascal Kriete | aaec1e4 | 2011-01-20 00:01:21 -0500 | [diff] [blame] | 25 | * @category UTF-8 |
Derek Jones | 98badc1 | 2010-03-02 13:08:02 -0600 | [diff] [blame] | 26 | * @author ExpressionEngine Dev Team |
Pascal Kriete | aaec1e4 | 2011-01-20 00:01:21 -0500 | [diff] [blame] | 27 | * @link http://codeigniter.com/user_guide/libraries/utf8.html |
Derek Jones | 98badc1 | 2010-03-02 13:08:02 -0600 | [diff] [blame] | 28 | */ |
Pascal Kriete | aaec1e4 | 2011-01-20 00:01:21 -0500 | [diff] [blame] | 29 | class CI_Utf8 { |
Derek Jones | 98badc1 | 2010-03-02 13:08:02 -0600 | [diff] [blame] | 30 | |
| 31 | /** |
| 32 | * Constructor |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 33 | * |
Derek Jones | 98badc1 | 2010-03-02 13:08:02 -0600 | [diff] [blame] | 34 | * Determines if UTF-8 support is to be enabled |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 35 | * |
Derek Jones | 98badc1 | 2010-03-02 13:08:02 -0600 | [diff] [blame] | 36 | */ |
Greg Aker | a926328 | 2010-11-10 15:26:43 -0600 | [diff] [blame] | 37 | function __construct() |
Derek Jones | 98badc1 | 2010-03-02 13:08:02 -0600 | [diff] [blame] | 38 | { |
Pascal Kriete | aaec1e4 | 2011-01-20 00:01:21 -0500 | [diff] [blame] | 39 | log_message('debug', "Utf8 Class Initialized"); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 40 | |
Derek Jones | 98badc1 | 2010-03-02 13:08:02 -0600 | [diff] [blame] | 41 | global $CFG; |
| 42 | |
| 43 | if ( |
| 44 | preg_match('/./u', 'é') === 1 // PCRE must support UTF-8 |
| 45 | AND function_exists('iconv') // iconv must be installed |
| 46 | AND ini_get('mbstring.func_overload') != 1 // Multibyte string function overloading cannot be enabled |
| 47 | AND $CFG->item('charset') == 'UTF-8' // Application charset must be UTF-8 |
| 48 | ) |
| 49 | { |
Pascal Kriete | aaec1e4 | 2011-01-20 00:01:21 -0500 | [diff] [blame] | 50 | log_message('debug', "UTF-8 Support Enabled"); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 51 | |
Derek Jones | 98badc1 | 2010-03-02 13:08:02 -0600 | [diff] [blame] | 52 | define('UTF8_ENABLED', TRUE); |
| 53 | |
| 54 | // set internal encoding for multibyte string functions if necessary |
| 55 | // and set a flag so we don't have to repeatedly use extension_loaded() |
| 56 | // or function_exists() |
| 57 | if (extension_loaded('mbstring')) |
| 58 | { |
| 59 | define('MB_ENABLED', TRUE); |
| 60 | mb_internal_encoding('UTF-8'); |
| 61 | } |
| 62 | else |
| 63 | { |
| 64 | define('MB_ENABLED', FALSE); |
| 65 | } |
| 66 | } |
| 67 | else |
| 68 | { |
Pascal Kriete | aaec1e4 | 2011-01-20 00:01:21 -0500 | [diff] [blame] | 69 | log_message('debug', "UTF-8 Support Disabled"); |
Derek Jones | 98badc1 | 2010-03-02 13:08:02 -0600 | [diff] [blame] | 70 | define('UTF8_ENABLED', FALSE); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 71 | } |
Derek Jones | 98badc1 | 2010-03-02 13:08:02 -0600 | [diff] [blame] | 72 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 73 | |
Derek Jones | 98badc1 | 2010-03-02 13:08:02 -0600 | [diff] [blame] | 74 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 75 | |
Derek Jones | 98badc1 | 2010-03-02 13:08:02 -0600 | [diff] [blame] | 76 | /** |
| 77 | * Clean UTF-8 strings |
| 78 | * |
| 79 | * Ensures strings are UTF-8 |
| 80 | * |
| 81 | * @access public |
| 82 | * @param string |
| 83 | * @return string |
| 84 | */ |
| 85 | function clean_string($str) |
| 86 | { |
| 87 | if ($this->_is_ascii($str) === FALSE) |
| 88 | { |
| 89 | $str = @iconv('UTF-8', 'UTF-8//IGNORE', $str); |
| 90 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 91 | |
Derek Jones | 98badc1 | 2010-03-02 13:08:02 -0600 | [diff] [blame] | 92 | return $str; |
| 93 | } |
| 94 | |
| 95 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 96 | |
Derek Jones | 98badc1 | 2010-03-02 13:08:02 -0600 | [diff] [blame] | 97 | /** |
| 98 | * Remove ASCII control characters |
| 99 | * |
| 100 | * Removes all ASCII control characters except horizontal tabs, |
| 101 | * line feeds, and carriage returns, as all others can cause |
| 102 | * problems in XML |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 103 | * |
Derek Jones | 98badc1 | 2010-03-02 13:08:02 -0600 | [diff] [blame] | 104 | * @access public |
| 105 | * @param string |
| 106 | * @return string |
| 107 | */ |
| 108 | function safe_ascii_for_xml($str) |
| 109 | { |
Pascal Kriete | 14a0ac6 | 2011-04-05 14:55:56 -0400 | [diff] [blame^] | 110 | return remove_invisible_characters($str, FALSE); |
Derek Jones | 98badc1 | 2010-03-02 13:08:02 -0600 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 114 | |
Derek Jones | 98badc1 | 2010-03-02 13:08:02 -0600 | [diff] [blame] | 115 | /** |
| 116 | * Convert to UTF-8 |
| 117 | * |
| 118 | * Attempts to convert a string to UTF-8 |
| 119 | * |
| 120 | * @access public |
| 121 | * @param string |
| 122 | * @param string - input encoding |
| 123 | * @return string |
| 124 | */ |
| 125 | function convert_to_utf8($str, $encoding) |
| 126 | { |
| 127 | if (function_exists('iconv')) |
| 128 | { |
| 129 | $str = @iconv($encoding, 'UTF-8', $str); |
| 130 | } |
| 131 | elseif (function_exists('mb_convert_encoding')) |
| 132 | { |
| 133 | $str = @mb_convert_encoding($str, 'UTF-8', $encoding); |
| 134 | } |
| 135 | else |
| 136 | { |
| 137 | return FALSE; |
| 138 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 139 | |
Derek Jones | 98badc1 | 2010-03-02 13:08:02 -0600 | [diff] [blame] | 140 | return $str; |
| 141 | } |
| 142 | |
| 143 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 144 | |
Derek Jones | 98badc1 | 2010-03-02 13:08:02 -0600 | [diff] [blame] | 145 | /** |
| 146 | * Is ASCII? |
| 147 | * |
| 148 | * Tests if a string is standard 7-bit ASCII or not |
| 149 | * |
| 150 | * @access public |
| 151 | * @param string |
| 152 | * @return bool |
| 153 | */ |
| 154 | function _is_ascii($str) |
| 155 | { |
| 156 | return (preg_match('/[^\x00-\x7F]/S', $str) == 0); |
| 157 | } |
| 158 | |
| 159 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 160 | |
Derek Jones | 98badc1 | 2010-03-02 13:08:02 -0600 | [diff] [blame] | 161 | } |
Pascal Kriete | aaec1e4 | 2011-01-20 00:01:21 -0500 | [diff] [blame] | 162 | // End Utf8 Class |
Derek Jones | 98badc1 | 2010-03-02 13:08:02 -0600 | [diff] [blame] | 163 | |
Pascal Kriete | aaec1e4 | 2011-01-20 00:01:21 -0500 | [diff] [blame] | 164 | /* End of file Utf8.php */ |
| 165 | /* Location: ./system/core/Utf8.php */ |