blob: 122020aeae117fedf806f9ba68309604be5df1df [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
Derek Jones98badc12010-03-02 13:08:02 -060045 */
Greg Akerd2c4ec62011-12-25 22:52:57 -060046 public function __construct()
Derek Jones98badc12010-03-02 13:08:02 -060047 {
Andrey Andreevc123e112012-01-08 00:17:34 +020048 log_message('debug', 'Utf8 Class Initialized');
Barry Mienydd671972010-10-04 16:33:58 +020049
Derek Jones98badc12010-03-02 13:08:02 -060050 global $CFG;
51
52 if (
Greg Akerd2c4ec62011-12-25 22:52:57 -060053 @preg_match('/./u', 'é') === 1 // PCRE must support UTF-8
54 && function_exists('iconv') // iconv must be installed
Andrey Andreevc123e112012-01-08 00:17:34 +020055 && @ini_get('mbstring.func_overload') != 1 // Multibyte string function overloading cannot be enabled
56 && $CFG->item('charset') === 'UTF-8' // Application charset must be UTF-8
Derek Jones98badc12010-03-02 13:08:02 -060057 )
58 {
Derek Jones98badc12010-03-02 13:08:02 -060059 define('UTF8_ENABLED', TRUE);
Andrey Andreevc123e112012-01-08 00:17:34 +020060 log_message('debug', 'UTF-8 Support Enabled');
Derek Jones98badc12010-03-02 13:08:02 -060061
62 // set internal encoding for multibyte string functions if necessary
63 // and set a flag so we don't have to repeatedly use extension_loaded()
64 // or function_exists()
65 if (extension_loaded('mbstring'))
66 {
67 define('MB_ENABLED', TRUE);
68 mb_internal_encoding('UTF-8');
69 }
70 else
71 {
72 define('MB_ENABLED', FALSE);
73 }
74 }
75 else
76 {
Derek Jones98badc12010-03-02 13:08:02 -060077 define('UTF8_ENABLED', FALSE);
Andrey Andreevc123e112012-01-08 00:17:34 +020078 log_message('debug', 'UTF-8 Support Disabled');
Barry Mienydd671972010-10-04 16:33:58 +020079 }
Derek Jones98badc12010-03-02 13:08:02 -060080 }
Barry Mienydd671972010-10-04 16:33:58 +020081
Derek Jones98badc12010-03-02 13:08:02 -060082 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +020083
Derek Jones98badc12010-03-02 13:08:02 -060084 /**
85 * Clean UTF-8 strings
86 *
87 * Ensures strings are UTF-8
88 *
Derek Jones98badc12010-03-02 13:08:02 -060089 * @param string
90 * @return string
91 */
Greg Akerd2c4ec62011-12-25 22:52:57 -060092 public function clean_string($str)
Derek Jones98badc12010-03-02 13:08:02 -060093 {
94 if ($this->_is_ascii($str) === FALSE)
95 {
96 $str = @iconv('UTF-8', 'UTF-8//IGNORE', $str);
97 }
Barry Mienydd671972010-10-04 16:33:58 +020098
Derek Jones98badc12010-03-02 13:08:02 -060099 return $str;
100 }
101
102 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200103
Derek Jones98badc12010-03-02 13:08:02 -0600104 /**
105 * Remove ASCII control characters
106 *
107 * Removes all ASCII control characters except horizontal tabs,
108 * line feeds, and carriage returns, as all others can cause
109 * problems in XML
Barry Mienydd671972010-10-04 16:33:58 +0200110 *
Derek Jones98badc12010-03-02 13:08:02 -0600111 * @param string
112 * @return string
113 */
Greg Akerd2c4ec62011-12-25 22:52:57 -0600114 public function safe_ascii_for_xml($str)
Derek Jones98badc12010-03-02 13:08:02 -0600115 {
Pascal Kriete14a0ac62011-04-05 14:55:56 -0400116 return remove_invisible_characters($str, FALSE);
Derek Jones98badc12010-03-02 13:08:02 -0600117 }
118
119 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200120
Derek Jones98badc12010-03-02 13:08:02 -0600121 /**
122 * Convert to UTF-8
123 *
124 * Attempts to convert a string to UTF-8
125 *
Derek Jones98badc12010-03-02 13:08:02 -0600126 * @param string
127 * @param string - input encoding
128 * @return string
129 */
Greg Akerd2c4ec62011-12-25 22:52:57 -0600130 public function convert_to_utf8($str, $encoding)
Derek Jones98badc12010-03-02 13:08:02 -0600131 {
132 if (function_exists('iconv'))
133 {
Andrey Andreevc123e112012-01-08 00:17:34 +0200134 return @iconv($encoding, 'UTF-8', $str);
Derek Jones98badc12010-03-02 13:08:02 -0600135 }
136 elseif (function_exists('mb_convert_encoding'))
137 {
Andrey Andreevc123e112012-01-08 00:17:34 +0200138 return @mb_convert_encoding($str, 'UTF-8', $encoding);
Derek Jones98badc12010-03-02 13:08:02 -0600139 }
Barry Mienydd671972010-10-04 16:33:58 +0200140
Andrey Andreevc123e112012-01-08 00:17:34 +0200141 return FALSE;
Derek Jones98badc12010-03-02 13:08:02 -0600142 }
143
144 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200145
Derek Jones98badc12010-03-02 13:08:02 -0600146 /**
147 * Is ASCII?
148 *
149 * Tests if a string is standard 7-bit ASCII or not
150 *
Derek Jones98badc12010-03-02 13:08:02 -0600151 * @param string
152 * @return bool
153 */
Greg Akerd2c4ec62011-12-25 22:52:57 -0600154 protected function _is_ascii($str)
Derek Jones98badc12010-03-02 13:08:02 -0600155 {
Greg Akerd2c4ec62011-12-25 22:52:57 -0600156 return (preg_match('/[^\x00-\x7F]/S', $str) === 0);
Derek Jones98badc12010-03-02 13:08:02 -0600157 }
158
Derek Jones98badc12010-03-02 13:08:02 -0600159}
Derek Jones98badc12010-03-02 13:08:02 -0600160
Pascal Krieteaaec1e42011-01-20 00:01:21 -0500161/* End of file Utf8.php */
Timothy Warren40403d22012-04-19 16:38:50 -0400162/* Location: ./system/core/Utf8.php */