blob: 9e6a4eb1bd9821d62aee8ba5d16833f461a89cb7 [file] [log] [blame]
Andrey Andreevc123e112012-01-08 00:17:34 +02001<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
Derek Jones98badc12010-03-02 13:08:02 -06002/**
3 * CodeIgniter
4 *
Phil Sturgeon07c1ac82012-03-09 17:03:37 +00005 * An open source application development framework for PHP 5.2.4 or newer
Derek Jones98badc12010-03-02 13:08:02 -06006 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05007 * NOTICE OF LICENSE
Andrey Andreevc123e112012-01-08 00:17:34 +02008 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05009 * Licensed under the Open Software License version 3.0
Andrey Andreevc123e112012-01-08 00:17:34 +020010 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -050011 * 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
Greg Aker0defe5d2012-01-01 18:46:41 -060021 * @copyright Copyright (c) 2008 - 2012, EllisLab, Inc. (http://ellislab.com/)
Derek Jonesf4a4bd82011-10-20 12:18:42 -050022 * @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
Derek Jones98badc12010-03-02 13:08:02 -060028/**
Pascal Krieteaaec1e42011-01-20 00:01:21 -050029 * Utf8 Class
Derek Jones98badc12010-03-02 13:08:02 -060030 *
Pascal Krieteaaec1e42011-01-20 00:01:21 -050031 * Provides support for UTF-8 environments
Derek Jones98badc12010-03-02 13:08:02 -060032 *
33 * @package CodeIgniter
34 * @subpackage Libraries
Pascal Krieteaaec1e42011-01-20 00:01:21 -050035 * @category UTF-8
Derek Jonesf4a4bd82011-10-20 12:18:42 -050036 * @author EllisLab Dev Team
Pascal Krieteaaec1e42011-01-20 00:01:21 -050037 * @link http://codeigniter.com/user_guide/libraries/utf8.html
Derek Jones98badc12010-03-02 13:08:02 -060038 */
Pascal Krieteaaec1e42011-01-20 00:01:21 -050039class CI_Utf8 {
Derek Jones98badc12010-03-02 13:08:02 -060040
41 /**
42 * Constructor
Barry Mienydd671972010-10-04 16:33:58 +020043 *
Derek Jones98badc12010-03-02 13:08:02 -060044 * Determines if UTF-8 support is to be enabled
Andrey Andreev92ebfb62012-05-17 12:49:24 +030045 *
46 * @return void
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 {
Andrey Andreevc123e112012-01-08 00:17:34 +020050 log_message('debug', 'Utf8 Class Initialized');
Barry Mienydd671972010-10-04 16:33:58 +020051
Andrey Andreev6c5f7512012-10-10 15:56:18 +030052 $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 Jones98badc12010-03-02 13:08:02 -060067
68 if (
Andrey Andreev6c5f7512012-10-10 15:56:18 +030069 @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 Jones98badc12010-03-02 13:08:02 -060073 )
74 {
Derek Jones98badc12010-03-02 13:08:02 -060075 define('UTF8_ENABLED', TRUE);
Andrey Andreevc123e112012-01-08 00:17:34 +020076 log_message('debug', 'UTF-8 Support Enabled');
Derek Jones98badc12010-03-02 13:08:02 -060077 }
78 else
79 {
Derek Jones98badc12010-03-02 13:08:02 -060080 define('UTF8_ENABLED', FALSE);
Andrey Andreevc123e112012-01-08 00:17:34 +020081 log_message('debug', 'UTF-8 Support Disabled');
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
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300130 * @param string input encoding
Derek Jones98badc12010-03-02 13:08:02 -0600131 * @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 {
Andrey Andreevc123e112012-01-08 00:17:34 +0200137 return @iconv($encoding, 'UTF-8', $str);
Derek Jones98badc12010-03-02 13:08:02 -0600138 }
139 elseif (function_exists('mb_convert_encoding'))
140 {
Andrey Andreevc123e112012-01-08 00:17:34 +0200141 return @mb_convert_encoding($str, 'UTF-8', $encoding);
Derek Jones98badc12010-03-02 13:08:02 -0600142 }
Barry Mienydd671972010-10-04 16:33:58 +0200143
Andrey Andreevc123e112012-01-08 00:17:34 +0200144 return FALSE;
Derek Jones98badc12010-03-02 13:08:02 -0600145 }
146
147 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200148
Derek Jones98badc12010-03-02 13:08:02 -0600149 /**
150 * Is ASCII?
151 *
152 * Tests if a string is standard 7-bit ASCII or not
153 *
Derek Jones98badc12010-03-02 13:08:02 -0600154 * @param string
155 * @return bool
156 */
Greg Akerd2c4ec62011-12-25 22:52:57 -0600157 protected function _is_ascii($str)
Derek Jones98badc12010-03-02 13:08:02 -0600158 {
Greg Akerd2c4ec62011-12-25 22:52:57 -0600159 return (preg_match('/[^\x00-\x7F]/S', $str) === 0);
Derek Jones98badc12010-03-02 13:08:02 -0600160 }
161
Derek Jones98badc12010-03-02 13:08:02 -0600162}
Derek Jones98badc12010-03-02 13:08:02 -0600163
Pascal Krieteaaec1e42011-01-20 00:01:21 -0500164/* End of file Utf8.php */
Timothy Warren40403d22012-04-19 16:38:50 -0400165/* Location: ./system/core/Utf8.php */