Andrey Andreev | c123e11 | 2012-01-08 00:17:34 +0200 | [diff] [blame] | 1 | <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); |
Derek Jones | 98badc1 | 2010-03-02 13:08:02 -0600 | [diff] [blame] | 2 | /** |
| 3 | * CodeIgniter |
| 4 | * |
Phil Sturgeon | 07c1ac8 | 2012-03-09 17:03:37 +0000 | [diff] [blame] | 5 | * An open source application development framework for PHP 5.2.4 or newer |
Derek Jones | 98badc1 | 2010-03-02 13:08:02 -0600 | [diff] [blame] | 6 | * |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 7 | * NOTICE OF LICENSE |
Andrey Andreev | c123e11 | 2012-01-08 00:17:34 +0200 | [diff] [blame] | 8 | * |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 9 | * Licensed under the Open Software License version 3.0 |
Andrey Andreev | c123e11 | 2012-01-08 00:17:34 +0200 | [diff] [blame] | 10 | * |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 11 | * This source file is subject to the Open Software License (OSL 3.0) that is |
| 12 | * bundled with this package in the files license.txt / license.rst. It is |
| 13 | * also available through the world wide web at this URL: |
| 14 | * http://opensource.org/licenses/OSL-3.0 |
| 15 | * If you did not receive a copy of the license and are unable to obtain it |
| 16 | * through the world wide web, please send an email to |
| 17 | * licensing@ellislab.com so we can send you a copy immediately. |
| 18 | * |
Derek Jones | 98badc1 | 2010-03-02 13:08:02 -0600 | [diff] [blame] | 19 | * @package CodeIgniter |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 20 | * @author EllisLab Dev Team |
Greg Aker | 0defe5d | 2012-01-01 18:46:41 -0600 | [diff] [blame] | 21 | * @copyright Copyright (c) 2008 - 2012, EllisLab, Inc. (http://ellislab.com/) |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 22 | * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0) |
Derek Jones | 98badc1 | 2010-03-02 13:08:02 -0600 | [diff] [blame] | 23 | * @link http://codeigniter.com |
Pascal Kriete | 5b2d2da | 2010-11-04 17:23:40 -0400 | [diff] [blame] | 24 | * @since Version 2.0 |
Derek Jones | 98badc1 | 2010-03-02 13:08:02 -0600 | [diff] [blame] | 25 | * @filesource |
| 26 | */ |
| 27 | |
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 | * Utf8 Class |
Derek Jones | 98badc1 | 2010-03-02 13:08:02 -0600 | [diff] [blame] | 30 | * |
Pascal Kriete | aaec1e4 | 2011-01-20 00:01:21 -0500 | [diff] [blame] | 31 | * Provides support for UTF-8 environments |
Derek Jones | 98badc1 | 2010-03-02 13:08:02 -0600 | [diff] [blame] | 32 | * |
| 33 | * @package CodeIgniter |
| 34 | * @subpackage Libraries |
Pascal Kriete | aaec1e4 | 2011-01-20 00:01:21 -0500 | [diff] [blame] | 35 | * @category UTF-8 |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 36 | * @author EllisLab Dev Team |
Pascal Kriete | aaec1e4 | 2011-01-20 00:01:21 -0500 | [diff] [blame] | 37 | * @link http://codeigniter.com/user_guide/libraries/utf8.html |
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 | class CI_Utf8 { |
Derek Jones | 98badc1 | 2010-03-02 13:08:02 -0600 | [diff] [blame] | 40 | |
| 41 | /** |
Andrey Andreev | 3e9d2b8 | 2012-10-27 14:28:51 +0300 | [diff] [blame^] | 42 | * Class constructor |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 43 | * |
Andrey Andreev | 3e9d2b8 | 2012-10-27 14:28:51 +0300 | [diff] [blame^] | 44 | * Determines if UTF-8 support is to be enabled. |
Andrey Andreev | 92ebfb6 | 2012-05-17 12:49:24 +0300 | [diff] [blame] | 45 | * |
| 46 | * @return void |
Derek Jones | 98badc1 | 2010-03-02 13:08:02 -0600 | [diff] [blame] | 47 | */ |
Greg Aker | d2c4ec6 | 2011-12-25 22:52:57 -0600 | [diff] [blame] | 48 | public function __construct() |
Derek Jones | 98badc1 | 2010-03-02 13:08:02 -0600 | [diff] [blame] | 49 | { |
Andrey Andreev | c123e11 | 2012-01-08 00:17:34 +0200 | [diff] [blame] | 50 | log_message('debug', 'Utf8 Class Initialized'); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 51 | |
Andrey Andreev | 6c5f751 | 2012-10-10 15:56:18 +0300 | [diff] [blame] | 52 | $charset = strtoupper(config_item('charset')); |
| 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($charset); |
| 61 | } |
| 62 | else |
| 63 | { |
| 64 | define('MB_ENABLED', FALSE); |
| 65 | } |
| 66 | |
Derek Jones | 98badc1 | 2010-03-02 13:08:02 -0600 | [diff] [blame] | 67 | |
| 68 | if ( |
Andrey Andreev | 6c5f751 | 2012-10-10 15:56:18 +0300 | [diff] [blame] | 69 | @preg_match('/./u', 'é') === 1 // PCRE must support UTF-8 |
| 70 | && function_exists('iconv') // iconv must be installed |
| 71 | && MB_ENABLED === TRUE // mbstring must be enabled |
| 72 | && $charset === 'UTF-8' // Application charset must be UTF-8 |
Derek Jones | 98badc1 | 2010-03-02 13:08:02 -0600 | [diff] [blame] | 73 | ) |
| 74 | { |
Derek Jones | 98badc1 | 2010-03-02 13:08:02 -0600 | [diff] [blame] | 75 | define('UTF8_ENABLED', TRUE); |
Andrey Andreev | c123e11 | 2012-01-08 00:17:34 +0200 | [diff] [blame] | 76 | log_message('debug', 'UTF-8 Support Enabled'); |
Derek Jones | 98badc1 | 2010-03-02 13:08:02 -0600 | [diff] [blame] | 77 | } |
| 78 | else |
| 79 | { |
Derek Jones | 98badc1 | 2010-03-02 13:08:02 -0600 | [diff] [blame] | 80 | define('UTF8_ENABLED', FALSE); |
Andrey Andreev | c123e11 | 2012-01-08 00:17:34 +0200 | [diff] [blame] | 81 | log_message('debug', 'UTF-8 Support Disabled'); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 82 | } |
Derek Jones | 98badc1 | 2010-03-02 13:08:02 -0600 | [diff] [blame] | 83 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 84 | |
Derek Jones | 98badc1 | 2010-03-02 13:08:02 -0600 | [diff] [blame] | 85 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 86 | |
Derek Jones | 98badc1 | 2010-03-02 13:08:02 -0600 | [diff] [blame] | 87 | /** |
| 88 | * Clean UTF-8 strings |
| 89 | * |
Andrey Andreev | 3e9d2b8 | 2012-10-27 14:28:51 +0300 | [diff] [blame^] | 90 | * Ensures strings contain only valid UTF-8 characters. |
Derek Jones | 98badc1 | 2010-03-02 13:08:02 -0600 | [diff] [blame] | 91 | * |
Andrey Andreev | 3e9d2b8 | 2012-10-27 14:28:51 +0300 | [diff] [blame^] | 92 | * @uses CI_Utf8::_is_ascii() Decide whether a conversion is needed |
| 93 | * |
| 94 | * @param string $str String to clean |
Derek Jones | 98badc1 | 2010-03-02 13:08:02 -0600 | [diff] [blame] | 95 | * @return string |
| 96 | */ |
Greg Aker | d2c4ec6 | 2011-12-25 22:52:57 -0600 | [diff] [blame] | 97 | public function clean_string($str) |
Derek Jones | 98badc1 | 2010-03-02 13:08:02 -0600 | [diff] [blame] | 98 | { |
| 99 | if ($this->_is_ascii($str) === FALSE) |
| 100 | { |
| 101 | $str = @iconv('UTF-8', 'UTF-8//IGNORE', $str); |
| 102 | } |
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 | return $str; |
| 105 | } |
| 106 | |
| 107 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 108 | |
Derek Jones | 98badc1 | 2010-03-02 13:08:02 -0600 | [diff] [blame] | 109 | /** |
| 110 | * Remove ASCII control characters |
| 111 | * |
| 112 | * Removes all ASCII control characters except horizontal tabs, |
| 113 | * line feeds, and carriage returns, as all others can cause |
Andrey Andreev | 3e9d2b8 | 2012-10-27 14:28:51 +0300 | [diff] [blame^] | 114 | * problems in XML. |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 115 | * |
Andrey Andreev | 3e9d2b8 | 2012-10-27 14:28:51 +0300 | [diff] [blame^] | 116 | * @param string $str String to clean |
Derek Jones | 98badc1 | 2010-03-02 13:08:02 -0600 | [diff] [blame] | 117 | * @return string |
| 118 | */ |
Greg Aker | d2c4ec6 | 2011-12-25 22:52:57 -0600 | [diff] [blame] | 119 | public function safe_ascii_for_xml($str) |
Derek Jones | 98badc1 | 2010-03-02 13:08:02 -0600 | [diff] [blame] | 120 | { |
Pascal Kriete | 14a0ac6 | 2011-04-05 14:55:56 -0400 | [diff] [blame] | 121 | return remove_invisible_characters($str, FALSE); |
Derek Jones | 98badc1 | 2010-03-02 13:08:02 -0600 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 125 | |
Derek Jones | 98badc1 | 2010-03-02 13:08:02 -0600 | [diff] [blame] | 126 | /** |
| 127 | * Convert to UTF-8 |
| 128 | * |
Andrey Andreev | 3e9d2b8 | 2012-10-27 14:28:51 +0300 | [diff] [blame^] | 129 | * Attempts to convert a string to UTF-8. |
Derek Jones | 98badc1 | 2010-03-02 13:08:02 -0600 | [diff] [blame] | 130 | * |
Andrey Andreev | 3e9d2b8 | 2012-10-27 14:28:51 +0300 | [diff] [blame^] | 131 | * @param string $str Input string |
| 132 | * @param string $encoding Input encoding |
| 133 | * @return string $str encoded in UTF-8 or FALSE on failure |
Derek Jones | 98badc1 | 2010-03-02 13:08:02 -0600 | [diff] [blame] | 134 | */ |
Greg Aker | d2c4ec6 | 2011-12-25 22:52:57 -0600 | [diff] [blame] | 135 | public function convert_to_utf8($str, $encoding) |
Derek Jones | 98badc1 | 2010-03-02 13:08:02 -0600 | [diff] [blame] | 136 | { |
| 137 | if (function_exists('iconv')) |
| 138 | { |
Andrey Andreev | c123e11 | 2012-01-08 00:17:34 +0200 | [diff] [blame] | 139 | return @iconv($encoding, 'UTF-8', $str); |
Derek Jones | 98badc1 | 2010-03-02 13:08:02 -0600 | [diff] [blame] | 140 | } |
Andrey Andreev | 9f44c21 | 2012-10-10 16:07:17 +0300 | [diff] [blame] | 141 | elseif (MB_ENABLED === TRUE) |
Derek Jones | 98badc1 | 2010-03-02 13:08:02 -0600 | [diff] [blame] | 142 | { |
Andrey Andreev | c123e11 | 2012-01-08 00:17:34 +0200 | [diff] [blame] | 143 | return @mb_convert_encoding($str, 'UTF-8', $encoding); |
Derek Jones | 98badc1 | 2010-03-02 13:08:02 -0600 | [diff] [blame] | 144 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 145 | |
Andrey Andreev | c123e11 | 2012-01-08 00:17:34 +0200 | [diff] [blame] | 146 | return FALSE; |
Derek Jones | 98badc1 | 2010-03-02 13:08:02 -0600 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 150 | |
Derek Jones | 98badc1 | 2010-03-02 13:08:02 -0600 | [diff] [blame] | 151 | /** |
| 152 | * Is ASCII? |
| 153 | * |
Andrey Andreev | 3e9d2b8 | 2012-10-27 14:28:51 +0300 | [diff] [blame^] | 154 | * Tests if a string is standard 7-bit ASCII or not. |
Derek Jones | 98badc1 | 2010-03-02 13:08:02 -0600 | [diff] [blame] | 155 | * |
Andrey Andreev | 3e9d2b8 | 2012-10-27 14:28:51 +0300 | [diff] [blame^] | 156 | * @param string $str String to check |
Derek Jones | 98badc1 | 2010-03-02 13:08:02 -0600 | [diff] [blame] | 157 | * @return bool |
| 158 | */ |
Greg Aker | d2c4ec6 | 2011-12-25 22:52:57 -0600 | [diff] [blame] | 159 | protected function _is_ascii($str) |
Derek Jones | 98badc1 | 2010-03-02 13:08:02 -0600 | [diff] [blame] | 160 | { |
Greg Aker | d2c4ec6 | 2011-12-25 22:52:57 -0600 | [diff] [blame] | 161 | return (preg_match('/[^\x00-\x7F]/S', $str) === 0); |
Derek Jones | 98badc1 | 2010-03-02 13:08:02 -0600 | [diff] [blame] | 162 | } |
| 163 | |
Derek Jones | 98badc1 | 2010-03-02 13:08:02 -0600 | [diff] [blame] | 164 | } |
Derek Jones | 98badc1 | 2010-03-02 13:08:02 -0600 | [diff] [blame] | 165 | |
Pascal Kriete | aaec1e4 | 2011-01-20 00:01:21 -0500 | [diff] [blame] | 166 | /* End of file Utf8.php */ |
Timothy Warren | 40403d2 | 2012-04-19 16:38:50 -0400 | [diff] [blame] | 167 | /* Location: ./system/core/Utf8.php */ |