2005-07-25 20:40:35 +00:00
< ? php
2013-01-10 23:50:55 +00:00
/**
* @ file
* Provides Unicode - related conversions and operations .
*/
2013-05-07 23:29:47 +00:00
use Drupal\Component\Utility\Unicode ;
2010-06-10 15:20:48 +00:00
2005-07-25 20:40:35 +00:00
/**
2013-01-10 23:50:55 +00:00
* Returns Unicode library status and errors .
2005-07-25 20:40:35 +00:00
*/
2006-09-01 08:44:53 +00:00
function unicode_requirements () {
$libraries = array (
2013-06-17 13:35:07 +00:00
Unicode :: STATUS_SINGLEBYTE => t ( 'Standard PHP' ),
Unicode :: STATUS_MULTIBYTE => t ( 'PHP Mbstring Extension' ),
Unicode :: STATUS_ERROR => t ( 'Error' ),
2006-09-01 08:44:53 +00:00
);
2006-12-01 16:47:58 +00:00
$severities = array (
2013-05-07 23:29:47 +00:00
Unicode :: STATUS_SINGLEBYTE => REQUIREMENT_WARNING ,
Unicode :: STATUS_MULTIBYTE => NULL ,
Unicode :: STATUS_ERROR => REQUIREMENT_ERROR ,
2006-12-01 16:47:58 +00:00
);
2013-05-07 23:29:47 +00:00
$failed_check = Unicode :: check ();
$library = Unicode :: getStatus ();
2005-07-25 20:40:35 +00:00
2006-09-01 08:44:53 +00:00
$requirements [ 'unicode' ] = array (
2013-06-17 13:35:07 +00:00
'title' => t ( 'Unicode library' ),
2006-09-01 08:44:53 +00:00
'value' => $libraries [ $library ],
2012-05-25 06:34:21 +00:00
'severity' => $severities [ $library ],
2006-09-01 08:44:53 +00:00
);
2012-05-25 06:34:21 +00:00
$t_args = array ( '@url' => 'http://www.php.net/mbstring' );
switch ( $failed_check ) {
case 'mb_strlen' :
2013-06-17 13:35:07 +00:00
$requirements [ 'unicode' ][ 'description' ] = t ( 'Operations on Unicode strings are emulated on a best-effort basis. Install the <a href="@url">PHP mbstring extension</a> for improved Unicode support.' , $t_args );
2012-05-25 06:34:21 +00:00
break ;
2006-09-01 08:44:53 +00:00
2012-05-25 06:34:21 +00:00
case 'mbstring.func_overload' :
2013-06-17 13:35:07 +00:00
$requirements [ 'unicode' ][ 'description' ] = t ( 'Multibyte string function overloading in PHP is active and must be disabled. Check the php.ini <em>mbstring.func_overload</em> setting. Please refer to the <a href="@url">PHP mbstring documentation</a> for more information.' , $t_args );
2012-05-25 06:34:21 +00:00
break ;
case 'mbstring.encoding_translation' :
2013-06-17 13:35:07 +00:00
$requirements [ 'unicode' ][ 'description' ] = t ( 'Multibyte string input conversion in PHP is active and must be disabled. Check the php.ini <em>mbstring.encoding_translation</em> setting. Please refer to the <a href="@url">PHP mbstring documentation</a> for more information.' , $t_args );
2012-05-25 06:34:21 +00:00
break ;
case 'mbstring.http_input' :
2013-06-17 13:35:07 +00:00
$requirements [ 'unicode' ][ 'description' ] = t ( 'Multibyte string input conversion in PHP is active and must be disabled. Check the php.ini <em>mbstring.http_input</em> setting. Please refer to the <a href="@url">PHP mbstring documentation</a> for more information.' , $t_args );
2012-05-25 06:34:21 +00:00
break ;
case 'mbstring.http_output' :
2013-06-17 13:35:07 +00:00
$requirements [ 'unicode' ][ 'description' ] = t ( 'Multibyte string output conversion in PHP is active and must be disabled. Check the php.ini <em>mbstring.http_output</em> setting. Please refer to the <a href="@url">PHP mbstring documentation</a> for more information.' , $t_args );
2012-05-25 06:34:21 +00:00
break ;
}
2006-12-01 16:47:58 +00:00
2006-09-01 08:44:53 +00:00
return $requirements ;
}
2007-10-21 18:59:02 +00:00
2005-07-25 20:40:35 +00:00
/**
2013-01-10 23:50:55 +00:00
* Prepares a new XML parser .
2005-07-25 20:40:35 +00:00
*
2013-01-10 23:50:55 +00:00
* This is a wrapper around xml_parser_create () which extracts the encoding
* from the XML data first and sets the output encoding to UTF - 8. This function
* should be used instead of xml_parser_create (), because PHP 4 ' s XML parser
* doesn ' t check the input encoding itself . " Starting from PHP 5, the input
* encoding is automatically detected , so that the encoding parameter specifies
* only the output encoding . "
2005-07-25 20:40:35 +00:00
*
2006-03-09 14:46:33 +00:00
* This is also where unsupported encodings will be converted . Callers should
* take this into account : $data might have been changed after the call .
2005-07-25 20:40:35 +00:00
*
2011-05-08 19:50:38 +00:00
* @ param $data
2005-07-25 20:40:35 +00:00
* The XML data which will be parsed later .
2010-07-16 11:17:25 +00:00
*
2005-07-25 20:40:35 +00:00
* @ return
2008-06-18 03:36:24 +00:00
* An XML parser object or FALSE on error .
2009-09-28 22:22:54 +00:00
*
* @ ingroup php_wrappers
2005-07-25 20:40:35 +00:00
*/
function drupal_xml_parser_create ( & $data ) {
// Default XML encoding is UTF-8
$encoding = 'utf-8' ;
2006-07-05 11:45:51 +00:00
$bom = FALSE ;
2005-07-25 20:40:35 +00:00
// Check for UTF-8 byte order mark (PHP5's XML parser doesn't handle it).
if ( ! strncmp ( $data , " \xEF \xBB \xBF " , 3 )) {
2006-07-05 11:45:51 +00:00
$bom = TRUE ;
2005-07-25 20:40:35 +00:00
$data = substr ( $data , 3 );
}
// Check for an encoding declaration in the XML prolog if no BOM was found.
2008-09-05 09:25:52 +00:00
if ( ! $bom && preg_match ( '/^<\?xml[^>]+encoding="(.+?)"/' , $data , $match )) {
2005-07-25 20:40:35 +00:00
$encoding = $match [ 1 ];
}
// Unsupported encodings are converted here into UTF-8.
$php_supported = array ( 'utf-8' , 'iso-8859-1' , 'us-ascii' );
if ( ! in_array ( strtolower ( $encoding ), $php_supported )) {
2014-10-27 08:31:35 +00:00
$out = Unicode :: convertToUtf8 ( $data , $encoding );
2006-07-05 11:45:51 +00:00
if ( $out !== FALSE ) {
2005-07-25 20:40:35 +00:00
$encoding = 'utf-8' ;
2008-09-05 09:25:52 +00:00
$data = preg_replace ( '/^(<\?xml[^>]+encoding)="(.+?)"/' , '\\1="utf-8"' , $out );
2005-07-25 20:40:35 +00:00
}
else {
2014-06-26 18:55:12 +00:00
\Drupal :: logger ( 'php' ) -> warning ( 'Could not convert XML encoding %s to UTF-8.' , array ( '%s' => $encoding ));
2008-06-18 03:36:24 +00:00
return FALSE ;
2005-07-25 20:40:35 +00:00
}
}
$xml_parser = xml_parser_create ( $encoding );
xml_parser_set_option ( $xml_parser , XML_OPTION_TARGET_ENCODING , 'utf-8' );
return $xml_parser ;
}