blob: 1bcf526a573ae65975c0f120ae8001c0a394d471 [file] [log] [blame]
Derek Jones37f4b9c2011-07-01 17:56:50 -05001<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
Derek Jones98badc12010-03-02 13:08:02 -06002/**
3 * CodeIgniter
4 *
Greg Aker741de1c2010-11-10 14:52:57 -06005 * An open source application development framework for PHP 5.1.6 or newer
Derek Jones98badc12010-03-02 13:08:02 -06006 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05007 * NOTICE OF LICENSE
8 *
9 * Licensed under the Open Software License version 3.0
10 *
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 Jones98badc12010-03-02 13:08:02 -060019 * @package CodeIgniter
Derek Jonesf4a4bd82011-10-20 12:18:42 -050020 * @author EllisLab Dev Team
21 * @copyright Copyright (c) 2008 - 2011, EllisLab, Inc. (http://ellislab.com/)
22 * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
Derek Jones98badc12010-03-02 13:08:02 -060023 * @link http://codeigniter.com
Pascal Kriete5b2d2da2010-11-04 17:23:40 -040024 * @since Version 2.0
Derek Jones98badc12010-03-02 13:08:02 -060025 * @filesource
26 */
27
28// ------------------------------------------------------------------------
29
30/**
Pascal Krieteaaec1e42011-01-20 00:01:21 -050031 * Utf8 Class
Derek Jones98badc12010-03-02 13:08:02 -060032 *
Pascal Krieteaaec1e42011-01-20 00:01:21 -050033 * Provides support for UTF-8 environments
Derek Jones98badc12010-03-02 13:08:02 -060034 *
35 * @package CodeIgniter
36 * @subpackage Libraries
Pascal Krieteaaec1e42011-01-20 00:01:21 -050037 * @category UTF-8
Derek Jonesf4a4bd82011-10-20 12:18:42 -050038 * @author EllisLab Dev Team
Pascal Krieteaaec1e42011-01-20 00:01:21 -050039 * @link http://codeigniter.com/user_guide/libraries/utf8.html
Derek Jones98badc12010-03-02 13:08:02 -060040 */
Pascal Krieteaaec1e42011-01-20 00:01:21 -050041class CI_Utf8 {
Derek Jones98badc12010-03-02 13:08:02 -060042
43 /**
44 * Constructor
Barry Mienydd671972010-10-04 16:33:58 +020045 *
Derek Jones98badc12010-03-02 13:08:02 -060046 * Determines if UTF-8 support is to be enabled
Derek Jones98badc12010-03-02 13:08:02 -060047 */
Greg Akerd2c4ec62011-12-25 22:52:57 -060048 public function __construct()
Derek Jones98badc12010-03-02 13:08:02 -060049 {
Pascal Krieteaaec1e42011-01-20 00:01:21 -050050 log_message('debug', "Utf8 Class Initialized");
Barry Mienydd671972010-10-04 16:33:58 +020051
Derek Jones98badc12010-03-02 13:08:02 -060052 global $CFG;
53
54 if (
Greg Akerd2c4ec62011-12-25 22:52:57 -060055 @preg_match('/./u', 'é') === 1 // PCRE must support UTF-8
56 && function_exists('iconv') // iconv must be installed
57 && ini_get('mbstring.func_overload') !== 1 // Multibyte string function overloading cannot be enabled
58 && $CFG->item('charset') == 'UTF-8' // Application charset must be UTF-8
Derek Jones98badc12010-03-02 13:08:02 -060059 )
60 {
Pascal Krieteaaec1e42011-01-20 00:01:21 -050061 log_message('debug', "UTF-8 Support Enabled");
Barry Mienydd671972010-10-04 16:33:58 +020062
Derek Jones98badc12010-03-02 13:08:02 -060063 define('UTF8_ENABLED', TRUE);
64
65 // set internal encoding for multibyte string functions if necessary
66 // and set a flag so we don't have to repeatedly use extension_loaded()
67 // or function_exists()
68 if (extension_loaded('mbstring'))
69 {
70 define('MB_ENABLED', TRUE);
71 mb_internal_encoding('UTF-8');
72 }
73 else
74 {
75 define('MB_ENABLED', FALSE);
76 }
77 }
78 else
79 {
Pascal Krieteaaec1e42011-01-20 00:01:21 -050080 log_message('debug', "UTF-8 Support Disabled");
Derek Jones98badc12010-03-02 13:08:02 -060081 define('UTF8_ENABLED', FALSE);
Barry Mienydd671972010-10-04 16:33:58 +020082 }
Derek Jones98badc12010-03-02 13:08:02 -060083 }
Barry Mienydd671972010-10-04 16:33:58 +020084
Derek Jones98badc12010-03-02 13:08:02 -060085 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +020086
Derek Jones98badc12010-03-02 13:08:02 -060087 /**
88 * Clean UTF-8 strings
89 *
90 * Ensures strings are UTF-8
91 *
Derek Jones98badc12010-03-02 13:08:02 -060092 * @param string
93 * @return string
94 */
Greg Akerd2c4ec62011-12-25 22:52:57 -060095 public function clean_string($str)
Derek Jones98badc12010-03-02 13:08:02 -060096 {
97 if ($this->_is_ascii($str) === FALSE)
98 {
99 $str = @iconv('UTF-8', 'UTF-8//IGNORE', $str);
100 }
Barry Mienydd671972010-10-04 16:33:58 +0200101
Derek Jones98badc12010-03-02 13:08:02 -0600102 return $str;
103 }
104
105 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200106
Derek Jones98badc12010-03-02 13:08:02 -0600107 /**
108 * Remove ASCII control characters
109 *
110 * Removes all ASCII control characters except horizontal tabs,
111 * line feeds, and carriage returns, as all others can cause
112 * problems in XML
Barry Mienydd671972010-10-04 16:33:58 +0200113 *
Derek Jones98badc12010-03-02 13:08:02 -0600114 * @param string
115 * @return string
116 */
Greg Akerd2c4ec62011-12-25 22:52:57 -0600117 public function safe_ascii_for_xml($str)
Derek Jones98badc12010-03-02 13:08:02 -0600118 {
Pascal Kriete14a0ac62011-04-05 14:55:56 -0400119 return remove_invisible_characters($str, FALSE);
Derek Jones98badc12010-03-02 13:08:02 -0600120 }
121
122 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200123
Derek Jones98badc12010-03-02 13:08:02 -0600124 /**
125 * Convert to UTF-8
126 *
127 * Attempts to convert a string to UTF-8
128 *
Derek Jones98badc12010-03-02 13:08:02 -0600129 * @param string
130 * @param string - input encoding
131 * @return string
132 */
Greg Akerd2c4ec62011-12-25 22:52:57 -0600133 public function convert_to_utf8($str, $encoding)
Derek Jones98badc12010-03-02 13:08:02 -0600134 {
135 if (function_exists('iconv'))
136 {
137 $str = @iconv($encoding, 'UTF-8', $str);
138 }
139 elseif (function_exists('mb_convert_encoding'))
140 {
141 $str = @mb_convert_encoding($str, 'UTF-8', $encoding);
142 }
143 else
144 {
145 return FALSE;
146 }
Barry Mienydd671972010-10-04 16:33:58 +0200147
Derek Jones98badc12010-03-02 13:08:02 -0600148 return $str;
149 }
150
151 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200152
Derek Jones98badc12010-03-02 13:08:02 -0600153 /**
154 * Is ASCII?
155 *
156 * Tests if a string is standard 7-bit ASCII or not
157 *
Derek Jones98badc12010-03-02 13:08:02 -0600158 * @param string
159 * @return bool
160 */
Greg Akerd2c4ec62011-12-25 22:52:57 -0600161 protected function _is_ascii($str)
Derek Jones98badc12010-03-02 13:08:02 -0600162 {
Greg Akerd2c4ec62011-12-25 22:52:57 -0600163 return (preg_match('/[^\x00-\x7F]/S', $str) === 0);
Derek Jones98badc12010-03-02 13:08:02 -0600164 }
165
166 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200167
Derek Jones98badc12010-03-02 13:08:02 -0600168}
Pascal Krieteaaec1e42011-01-20 00:01:21 -0500169// End Utf8 Class
Derek Jones98badc12010-03-02 13:08:02 -0600170
Pascal Krieteaaec1e42011-01-20 00:01:21 -0500171/* End of file Utf8.php */
172/* Location: ./system/core/Utf8.php */