| 68 |
lars |
1 |
<?php
|
|
|
2 |
/**
|
|
|
3 |
* CodeIgniter
|
|
|
4 |
*
|
|
|
5 |
* An open source application development framework for PHP
|
|
|
6 |
*
|
|
|
7 |
* This content is released under the MIT License (MIT)
|
|
|
8 |
*
|
| 2257 |
lars |
9 |
* Copyright (c) 2014 - 2018, British Columbia Institute of Technology
|
| 68 |
lars |
10 |
*
|
|
|
11 |
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
12 |
* of this software and associated documentation files (the "Software"), to deal
|
|
|
13 |
* in the Software without restriction, including without limitation the rights
|
|
|
14 |
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
15 |
* copies of the Software, and to permit persons to whom the Software is
|
|
|
16 |
* furnished to do so, subject to the following conditions:
|
|
|
17 |
*
|
|
|
18 |
* The above copyright notice and this permission notice shall be included in
|
|
|
19 |
* all copies or substantial portions of the Software.
|
|
|
20 |
*
|
|
|
21 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
22 |
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
23 |
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
24 |
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
25 |
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
26 |
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
27 |
* THE SOFTWARE.
|
|
|
28 |
*
|
|
|
29 |
* @package CodeIgniter
|
|
|
30 |
* @author EllisLab Dev Team
|
|
|
31 |
* @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (https://ellislab.com/)
|
| 2257 |
lars |
32 |
* @copyright Copyright (c) 2014 - 2018, British Columbia Institute of Technology (http://bcit.ca/)
|
| 68 |
lars |
33 |
* @license http://opensource.org/licenses/MIT MIT License
|
|
|
34 |
* @link https://codeigniter.com
|
|
|
35 |
* @since Version 2.0.0
|
|
|
36 |
* @filesource
|
|
|
37 |
*/
|
|
|
38 |
defined('BASEPATH') OR exit('No direct script access allowed');
|
|
|
39 |
|
|
|
40 |
/**
|
|
|
41 |
* Utf8 Class
|
|
|
42 |
*
|
|
|
43 |
* Provides support for UTF-8 environments
|
|
|
44 |
*
|
|
|
45 |
* @package CodeIgniter
|
|
|
46 |
* @subpackage Libraries
|
|
|
47 |
* @category UTF-8
|
|
|
48 |
* @author EllisLab Dev Team
|
|
|
49 |
* @link https://codeigniter.com/user_guide/libraries/utf8.html
|
|
|
50 |
*/
|
|
|
51 |
class CI_Utf8 {
|
|
|
52 |
|
|
|
53 |
/**
|
|
|
54 |
* Class constructor
|
|
|
55 |
*
|
|
|
56 |
* Determines if UTF-8 support is to be enabled.
|
|
|
57 |
*
|
|
|
58 |
* @return void
|
|
|
59 |
*/
|
|
|
60 |
public function __construct()
|
|
|
61 |
{
|
|
|
62 |
if (
|
|
|
63 |
defined('PREG_BAD_UTF8_ERROR') // PCRE must support UTF-8
|
|
|
64 |
&& (ICONV_ENABLED === TRUE OR MB_ENABLED === TRUE) // iconv or mbstring must be installed
|
|
|
65 |
&& strtoupper(config_item('charset')) === 'UTF-8' // Application charset must be UTF-8
|
|
|
66 |
)
|
|
|
67 |
{
|
|
|
68 |
define('UTF8_ENABLED', TRUE);
|
|
|
69 |
log_message('debug', 'UTF-8 Support Enabled');
|
|
|
70 |
}
|
|
|
71 |
else
|
|
|
72 |
{
|
|
|
73 |
define('UTF8_ENABLED', FALSE);
|
|
|
74 |
log_message('debug', 'UTF-8 Support Disabled');
|
|
|
75 |
}
|
|
|
76 |
|
|
|
77 |
log_message('info', 'Utf8 Class Initialized');
|
|
|
78 |
}
|
|
|
79 |
|
|
|
80 |
// --------------------------------------------------------------------
|
|
|
81 |
|
|
|
82 |
/**
|
|
|
83 |
* Clean UTF-8 strings
|
|
|
84 |
*
|
|
|
85 |
* Ensures strings contain only valid UTF-8 characters.
|
|
|
86 |
*
|
|
|
87 |
* @param string $str String to clean
|
|
|
88 |
* @return string
|
|
|
89 |
*/
|
|
|
90 |
public function clean_string($str)
|
|
|
91 |
{
|
|
|
92 |
if ($this->is_ascii($str) === FALSE)
|
|
|
93 |
{
|
|
|
94 |
if (MB_ENABLED)
|
|
|
95 |
{
|
|
|
96 |
$str = mb_convert_encoding($str, 'UTF-8', 'UTF-8');
|
|
|
97 |
}
|
|
|
98 |
elseif (ICONV_ENABLED)
|
|
|
99 |
{
|
|
|
100 |
$str = @iconv('UTF-8', 'UTF-8//IGNORE', $str);
|
|
|
101 |
}
|
|
|
102 |
}
|
|
|
103 |
|
|
|
104 |
return $str;
|
|
|
105 |
}
|
|
|
106 |
|
|
|
107 |
// --------------------------------------------------------------------
|
|
|
108 |
|
|
|
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
|
|
|
114 |
* problems in XML.
|
|
|
115 |
*
|
|
|
116 |
* @param string $str String to clean
|
|
|
117 |
* @return string
|
|
|
118 |
*/
|
|
|
119 |
public function safe_ascii_for_xml($str)
|
|
|
120 |
{
|
|
|
121 |
return remove_invisible_characters($str, FALSE);
|
|
|
122 |
}
|
|
|
123 |
|
|
|
124 |
// --------------------------------------------------------------------
|
|
|
125 |
|
|
|
126 |
/**
|
|
|
127 |
* Convert to UTF-8
|
|
|
128 |
*
|
|
|
129 |
* Attempts to convert a string to UTF-8.
|
|
|
130 |
*
|
|
|
131 |
* @param string $str Input string
|
|
|
132 |
* @param string $encoding Input encoding
|
|
|
133 |
* @return string $str encoded in UTF-8 or FALSE on failure
|
|
|
134 |
*/
|
|
|
135 |
public function convert_to_utf8($str, $encoding)
|
|
|
136 |
{
|
|
|
137 |
if (MB_ENABLED)
|
|
|
138 |
{
|
|
|
139 |
return mb_convert_encoding($str, 'UTF-8', $encoding);
|
|
|
140 |
}
|
|
|
141 |
elseif (ICONV_ENABLED)
|
|
|
142 |
{
|
|
|
143 |
return @iconv($encoding, 'UTF-8', $str);
|
|
|
144 |
}
|
|
|
145 |
|
|
|
146 |
return FALSE;
|
|
|
147 |
}
|
|
|
148 |
|
|
|
149 |
// --------------------------------------------------------------------
|
|
|
150 |
|
|
|
151 |
/**
|
|
|
152 |
* Is ASCII?
|
|
|
153 |
*
|
|
|
154 |
* Tests if a string is standard 7-bit ASCII or not.
|
|
|
155 |
*
|
|
|
156 |
* @param string $str String to check
|
|
|
157 |
* @return bool
|
|
|
158 |
*/
|
|
|
159 |
public function is_ascii($str)
|
|
|
160 |
{
|
|
|
161 |
return (preg_match('/[^\x00-\x7F]/S', $str) === 0);
|
|
|
162 |
}
|
|
|
163 |
|
|
|
164 |
}
|