Project

General

Profile

Paste
Download (22.2 KB) Statistics
| Branch: | Revision:

root / drupal7 / includes / unicode.inc @ 582db59d

1
<?php
2

    
3
/**
4
* @file
5
* Provides Unicode-related conversions and operations.
6
*/
7

    
8
/**
9
 * Indicates an error during check for PHP unicode support.
10
 */
11
define('UNICODE_ERROR', -1);
12

    
13
/**
14
 * Indicates that standard PHP (emulated) unicode support is being used.
15
 */
16
define('UNICODE_SINGLEBYTE', 0);
17

    
18
/**
19
 * Indicates that full unicode support with the PHP mbstring extension is being
20
 * used.
21
 */
22
define('UNICODE_MULTIBYTE', 1);
23

    
24
/**
25
 * Matches Unicode characters that are word boundaries.
26
 *
27
 * Characters with the following General_category (gc) property values are used
28
 * as word boundaries. While this does not fully conform to the Word Boundaries
29
 * algorithm described in http://unicode.org/reports/tr29, as PCRE does not
30
 * contain the Word_Break property table, this simpler algorithm has to do.
31
 * - Cc, Cf, Cn, Co, Cs: Other.
32
 * - Pc, Pd, Pe, Pf, Pi, Po, Ps: Punctuation.
33
 * - Sc, Sk, Sm, So: Symbols.
34
 * - Zl, Zp, Zs: Separators.
35
 *
36
 * Non-boundary characters include the following General_category (gc) property
37
 * values:
38
 * - Ll, Lm, Lo, Lt, Lu: Letters.
39
 * - Mc, Me, Mn: Combining Marks.
40
 * - Nd, Nl, No: Numbers.
41
 *
42
 * Note that the PCRE property matcher is not used because we wanted to be
43
 * compatible with Unicode 5.2.0 regardless of the PCRE version used (and any
44
 * bugs in PCRE property tables).
45
 *
46
 * @see http://unicode.org/glossary
47
 */
48
define('PREG_CLASS_UNICODE_WORD_BOUNDARY',
49
  '\x{0}-\x{2F}\x{3A}-\x{40}\x{5B}-\x{60}\x{7B}-\x{A9}\x{AB}-\x{B1}\x{B4}' .
50
  '\x{B6}-\x{B8}\x{BB}\x{BF}\x{D7}\x{F7}\x{2C2}-\x{2C5}\x{2D2}-\x{2DF}' .
51
  '\x{2E5}-\x{2EB}\x{2ED}\x{2EF}-\x{2FF}\x{375}\x{37E}-\x{385}\x{387}\x{3F6}' .
52
  '\x{482}\x{55A}-\x{55F}\x{589}-\x{58A}\x{5BE}\x{5C0}\x{5C3}\x{5C6}' .
53
  '\x{5F3}-\x{60F}\x{61B}-\x{61F}\x{66A}-\x{66D}\x{6D4}\x{6DD}\x{6E9}' .
54
  '\x{6FD}-\x{6FE}\x{700}-\x{70F}\x{7F6}-\x{7F9}\x{830}-\x{83E}' .
55
  '\x{964}-\x{965}\x{970}\x{9F2}-\x{9F3}\x{9FA}-\x{9FB}\x{AF1}\x{B70}' .
56
  '\x{BF3}-\x{BFA}\x{C7F}\x{CF1}-\x{CF2}\x{D79}\x{DF4}\x{E3F}\x{E4F}' .
57
  '\x{E5A}-\x{E5B}\x{F01}-\x{F17}\x{F1A}-\x{F1F}\x{F34}\x{F36}\x{F38}' .
58
  '\x{F3A}-\x{F3D}\x{F85}\x{FBE}-\x{FC5}\x{FC7}-\x{FD8}\x{104A}-\x{104F}' .
59
  '\x{109E}-\x{109F}\x{10FB}\x{1360}-\x{1368}\x{1390}-\x{1399}\x{1400}' .
60
  '\x{166D}-\x{166E}\x{1680}\x{169B}-\x{169C}\x{16EB}-\x{16ED}' .
61
  '\x{1735}-\x{1736}\x{17B4}-\x{17B5}\x{17D4}-\x{17D6}\x{17D8}-\x{17DB}' .
62
  '\x{1800}-\x{180A}\x{180E}\x{1940}-\x{1945}\x{19DE}-\x{19FF}' .
63
  '\x{1A1E}-\x{1A1F}\x{1AA0}-\x{1AA6}\x{1AA8}-\x{1AAD}\x{1B5A}-\x{1B6A}' .
64
  '\x{1B74}-\x{1B7C}\x{1C3B}-\x{1C3F}\x{1C7E}-\x{1C7F}\x{1CD3}\x{1FBD}' .
65
  '\x{1FBF}-\x{1FC1}\x{1FCD}-\x{1FCF}\x{1FDD}-\x{1FDF}\x{1FED}-\x{1FEF}' .
66
  '\x{1FFD}-\x{206F}\x{207A}-\x{207E}\x{208A}-\x{208E}\x{20A0}-\x{20B8}' .
67
  '\x{2100}-\x{2101}\x{2103}-\x{2106}\x{2108}-\x{2109}\x{2114}' .
68
  '\x{2116}-\x{2118}\x{211E}-\x{2123}\x{2125}\x{2127}\x{2129}\x{212E}' .
69
  '\x{213A}-\x{213B}\x{2140}-\x{2144}\x{214A}-\x{214D}\x{214F}' .
70
  '\x{2190}-\x{244A}\x{249C}-\x{24E9}\x{2500}-\x{2775}\x{2794}-\x{2B59}' .
71
  '\x{2CE5}-\x{2CEA}\x{2CF9}-\x{2CFC}\x{2CFE}-\x{2CFF}\x{2E00}-\x{2E2E}' .
72
  '\x{2E30}-\x{3004}\x{3008}-\x{3020}\x{3030}\x{3036}-\x{3037}' .
73
  '\x{303D}-\x{303F}\x{309B}-\x{309C}\x{30A0}\x{30FB}\x{3190}-\x{3191}' .
74
  '\x{3196}-\x{319F}\x{31C0}-\x{31E3}\x{3200}-\x{321E}\x{322A}-\x{3250}' .
75
  '\x{3260}-\x{327F}\x{328A}-\x{32B0}\x{32C0}-\x{33FF}\x{4DC0}-\x{4DFF}' .
76
  '\x{A490}-\x{A4C6}\x{A4FE}-\x{A4FF}\x{A60D}-\x{A60F}\x{A673}\x{A67E}' .
77
  '\x{A6F2}-\x{A716}\x{A720}-\x{A721}\x{A789}-\x{A78A}\x{A828}-\x{A82B}' .
78
  '\x{A836}-\x{A839}\x{A874}-\x{A877}\x{A8CE}-\x{A8CF}\x{A8F8}-\x{A8FA}' .
79
  '\x{A92E}-\x{A92F}\x{A95F}\x{A9C1}-\x{A9CD}\x{A9DE}-\x{A9DF}' .
80
  '\x{AA5C}-\x{AA5F}\x{AA77}-\x{AA79}\x{AADE}-\x{AADF}\x{ABEB}' .
81
  '\x{E000}-\x{F8FF}\x{FB29}\x{FD3E}-\x{FD3F}\x{FDFC}-\x{FDFD}' .
82
  '\x{FE10}-\x{FE19}\x{FE30}-\x{FE6B}\x{FEFF}-\x{FF0F}\x{FF1A}-\x{FF20}' .
83
  '\x{FF3B}-\x{FF40}\x{FF5B}-\x{FF65}\x{FFE0}-\x{FFFD}');
84

    
85
/**
86
 * Wrapper around _unicode_check().
87
 */
88
function unicode_check() {
89
  list($GLOBALS['multibyte']) = _unicode_check();
90
}
91

    
92
/**
93
 * Perform checks about Unicode support in PHP, and set the right settings if
94
 * needed.
95
 *
96
 * Because Drupal needs to be able to handle text in various encodings, we do
97
 * not support mbstring function overloading. HTTP input/output conversion must
98
 * be disabled for similar reasons.
99
 *
100
 * @param $errors
101
 *   Whether to report any fatal errors with form_set_error().
102
 */
103
function _unicode_check() {
104
  // Ensure translations don't break during installation.
105
  $t = get_t();
106

    
107
  // Check for mbstring extension
108
  if (!function_exists('mb_strlen')) {
109
    return array(UNICODE_SINGLEBYTE, $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.', array('@url' => 'http://www.php.net/mbstring')));
110
  }
111

    
112
  // Check mbstring configuration
113
  if (ini_get('mbstring.func_overload') != 0) {
114
    return array(UNICODE_ERROR, $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.', array('@url' => 'http://www.php.net/mbstring')));
115
  }
116
  if (ini_get('mbstring.encoding_translation') != 0) {
117
    return array(UNICODE_ERROR, $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.', array('@url' => 'http://www.php.net/mbstring')));
118
  }
119
  // mbstring.http_input and mbstring.http_output are deprecated and empty by
120
  // default in PHP 5.6.
121
  if (version_compare(PHP_VERSION, '5.6.0') == -1) {
122
    if (ini_get('mbstring.http_input') != 'pass') {
123
      return array(UNICODE_ERROR, $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.', array('@url' => 'http://www.php.net/mbstring')));
124
    }
125
    if (ini_get('mbstring.http_output') != 'pass') {
126
      return array(UNICODE_ERROR, $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.', array('@url' => 'http://www.php.net/mbstring')));
127
    }
128
  }
129

    
130
  // Set appropriate configuration
131
  mb_internal_encoding('utf-8');
132
  mb_language('uni');
133
  return array(UNICODE_MULTIBYTE, '');
134
}
135

    
136
/**
137
 * Returns Unicode library status and errors.
138
 */
139
function unicode_requirements() {
140
  // Ensure translations don't break during installation.
141
  $t = get_t();
142

    
143
  $libraries = array(
144
    UNICODE_SINGLEBYTE => $t('Standard PHP'),
145
    UNICODE_MULTIBYTE => $t('PHP Mbstring Extension'),
146
    UNICODE_ERROR => $t('Error'),
147
  );
148
  $severities = array(
149
    UNICODE_SINGLEBYTE => REQUIREMENT_WARNING,
150
    UNICODE_MULTIBYTE => REQUIREMENT_OK,
151
    UNICODE_ERROR => REQUIREMENT_ERROR,
152
  );
153
  list($library, $description) = _unicode_check();
154

    
155
  $requirements['unicode'] = array(
156
    'title' => $t('Unicode library'),
157
    'value' => $libraries[$library],
158
  );
159
  if ($description) {
160
    $requirements['unicode']['description'] = $description;
161
  }
162

    
163
  $requirements['unicode']['severity'] = $severities[$library];
164

    
165
  return $requirements;
166
}
167

    
168
/**
169
 * Prepares a new XML parser.
170
 *
171
 * This is a wrapper around xml_parser_create() which extracts the encoding
172
 * from the XML data first and sets the output encoding to UTF-8. This function
173
 * should be used instead of xml_parser_create(), because PHP 4's XML parser
174
 * doesn't check the input encoding itself. "Starting from PHP 5, the input
175
 * encoding is automatically detected, so that the encoding parameter specifies
176
 * only the output encoding."
177
 *
178
 * This is also where unsupported encodings will be converted. Callers should
179
 * take this into account: $data might have been changed after the call.
180
 *
181
 * @param $data
182
 *   The XML data which will be parsed later.
183
 *
184
 * @return
185
 *   An XML parser object or FALSE on error.
186
 *
187
 * @ingroup php_wrappers
188
 */
189
function drupal_xml_parser_create(&$data) {
190
  // Default XML encoding is UTF-8
191
  $encoding = 'utf-8';
192
  $bom = FALSE;
193

    
194
  // Check for UTF-8 byte order mark (PHP5's XML parser doesn't handle it).
195
  if (!strncmp($data, "\xEF\xBB\xBF", 3)) {
196
    $bom = TRUE;
197
    $data = substr($data, 3);
198
  }
199

    
200
  // Check for an encoding declaration in the XML prolog if no BOM was found.
201
  if (!$bom && preg_match('/^<\?xml[^>]+encoding="(.+?)"/', $data, $match)) {
202
    $encoding = $match[1];
203
  }
204

    
205
  // Unsupported encodings are converted here into UTF-8.
206
  $php_supported = array('utf-8', 'iso-8859-1', 'us-ascii');
207
  if (!in_array(strtolower($encoding), $php_supported)) {
208
    $out = drupal_convert_to_utf8($data, $encoding);
209
    if ($out !== FALSE) {
210
      $encoding = 'utf-8';
211
      $data = preg_replace('/^(<\?xml[^>]+encoding)="(.+?)"/', '\\1="utf-8"', $out);
212
    }
213
    else {
214
      watchdog('php', 'Could not convert XML encoding %s to UTF-8.', array('%s' => $encoding), WATCHDOG_WARNING);
215
      return FALSE;
216
    }
217
  }
218

    
219
  $xml_parser = xml_parser_create($encoding);
220
  xml_parser_set_option($xml_parser, XML_OPTION_TARGET_ENCODING, 'utf-8');
221
  return $xml_parser;
222
}
223

    
224
/**
225
 * Converts data to UTF-8.
226
 *
227
 * Requires the iconv, GNU recode or mbstring PHP extension.
228
 *
229
 * @param $data
230
 *   The data to be converted.
231
 * @param $encoding
232
 *   The encoding that the data is in.
233
 *
234
 * @return
235
 *   Converted data or FALSE.
236
 */
237
function drupal_convert_to_utf8($data, $encoding) {
238
  if (function_exists('iconv')) {
239
    $out = @iconv($encoding, 'utf-8', $data);
240
  }
241
  elseif (function_exists('mb_convert_encoding')) {
242
    $out = @mb_convert_encoding($data, 'utf-8', $encoding);
243
  }
244
  elseif (function_exists('recode_string')) {
245
    $out = @recode_string($encoding . '..utf-8', $data);
246
  }
247
  else {
248
    watchdog('php', 'Unsupported encoding %s. Please install iconv, GNU recode or mbstring for PHP.', array('%s' => $encoding), WATCHDOG_ERROR);
249
    return FALSE;
250
  }
251

    
252
  return $out;
253
}
254

    
255
/**
256
 * Truncates a UTF-8-encoded string safely to a number of bytes.
257
 *
258
 * If the end position is in the middle of a UTF-8 sequence, it scans backwards
259
 * until the beginning of the byte sequence.
260
 *
261
 * Use this function whenever you want to chop off a string at an unsure
262
 * location. On the other hand, if you're sure that you're splitting on a
263
 * character boundary (e.g. after using strpos() or similar), you can safely
264
 * use substr() instead.
265
 *
266
 * @param $string
267
 *   The string to truncate.
268
 * @param $len
269
 *   An upper limit on the returned string length.
270
 *
271
 * @return
272
 *   The truncated string.
273
 */
274
function drupal_truncate_bytes($string, $len) {
275
  if (strlen($string) <= $len) {
276
    return $string;
277
  }
278
  if ((ord($string[$len]) < 0x80) || (ord($string[$len]) >= 0xC0)) {
279
    return substr($string, 0, $len);
280
  }
281
  // Scan backwards to beginning of the byte sequence.
282
  while (--$len >= 0 && ord($string[$len]) >= 0x80 && ord($string[$len]) < 0xC0);
283

    
284
  return substr($string, 0, $len);
285
}
286

    
287
/**
288
 * Truncates a UTF-8-encoded string safely to a number of characters.
289
 *
290
 * @param $string
291
 *   The string to truncate.
292
 * @param $max_length
293
 *   An upper limit on the returned string length, including trailing ellipsis
294
 *   if $add_ellipsis is TRUE.
295
 * @param $wordsafe
296
 *   If TRUE, attempt to truncate on a word boundary. Word boundaries are
297
 *   spaces, punctuation, and Unicode characters used as word boundaries in
298
 *   non-Latin languages; see PREG_CLASS_UNICODE_WORD_BOUNDARY for more
299
 *   information. If a word boundary cannot be found that would make the length
300
 *   of the returned string fall within length guidelines (see parameters
301
 *   $max_length and $min_wordsafe_length), word boundaries are ignored.
302
 * @param $add_ellipsis
303
 *   If TRUE, add t('...') to the end of the truncated string (defaults to
304
 *   FALSE). The string length will still fall within $max_length.
305
 * @param $min_wordsafe_length
306
 *   If $wordsafe is TRUE, the minimum acceptable length for truncation (before
307
 *   adding an ellipsis, if $add_ellipsis is TRUE). Has no effect if $wordsafe
308
 *   is FALSE. This can be used to prevent having a very short resulting string
309
 *   that will not be understandable. For instance, if you are truncating the
310
 *   string "See myverylongurlexample.com for more information" to a word-safe
311
 *   return length of 20, the only available word boundary within 20 characters
312
 *   is after the word "See", which wouldn't leave a very informative string. If
313
 *   you had set $min_wordsafe_length to 10, though, the function would realise
314
 *   that "See" alone is too short, and would then just truncate ignoring word
315
 *   boundaries, giving you "See myverylongurl..." (assuming you had set
316
 *   $add_ellipses to TRUE).
317
 *
318
 * @return string
319
 *   The truncated string.
320
 */
321
function truncate_utf8($string, $max_length, $wordsafe = FALSE, $add_ellipsis = FALSE, $min_wordsafe_length = 1) {
322
  $ellipsis = '';
323
  $max_length = max($max_length, 0);
324
  $min_wordsafe_length = max($min_wordsafe_length, 0);
325

    
326
  if (drupal_strlen($string) <= $max_length) {
327
    // No truncation needed, so don't add ellipsis, just return.
328
    return $string;
329
  }
330

    
331
  if ($add_ellipsis) {
332
    // Truncate ellipsis in case $max_length is small.
333
    $ellipsis = drupal_substr(t('...'), 0, $max_length);
334
    $max_length -= drupal_strlen($ellipsis);
335
    $max_length = max($max_length, 0);
336
  }
337

    
338
  if ($max_length <= $min_wordsafe_length) {
339
    // Do not attempt word-safe if lengths are bad.
340
    $wordsafe = FALSE;
341
  }
342

    
343
  if ($wordsafe) {
344
    $matches = array();
345
    // Find the last word boundary, if there is one within $min_wordsafe_length
346
    // to $max_length characters. preg_match() is always greedy, so it will
347
    // find the longest string possible.
348
    $found = preg_match('/^(.{' . $min_wordsafe_length . ',' . $max_length . '})[' . PREG_CLASS_UNICODE_WORD_BOUNDARY . ']/u', $string, $matches);
349
    if ($found) {
350
      $string = $matches[1];
351
    }
352
    else {
353
      $string = drupal_substr($string, 0, $max_length);
354
    }
355
  }
356
  else {
357
    $string = drupal_substr($string, 0, $max_length);
358
  }
359

    
360
  if ($add_ellipsis) {
361
    $string .= $ellipsis;
362
  }
363

    
364
  return $string;
365
}
366

    
367
/**
368
 * Encodes MIME/HTTP header values that contain incorrectly encoded characters.
369
 *
370
 * For example, mime_header_encode('tést.txt') returns "=?UTF-8?B?dMOpc3QudHh0?=".
371
 *
372
 * See http://www.rfc-editor.org/rfc/rfc2047.txt for more information.
373
 *
374
 * Notes:
375
 * - Only encode strings that contain non-ASCII characters.
376
 * - We progressively cut-off a chunk with truncate_utf8(). This is to ensure
377
 *   each chunk starts and ends on a character boundary.
378
 * - Using \n as the chunk separator may cause problems on some systems and may
379
 *   have to be changed to \r\n or \r.
380
 *
381
 * @param $string
382
 *   The header to encode.
383
 *
384
 * @return string
385
 *   The mime-encoded header.
386
 *
387
 * @see mime_header_decode()
388
 */
389
function mime_header_encode($string) {
390
  if (preg_match('/[^\x20-\x7E]/', $string)) {
391
    $chunk_size = 47; // floor((75 - strlen("=?UTF-8?B??=")) * 0.75);
392
    $len = strlen($string);
393
    $output = '';
394
    while ($len > 0) {
395
      $chunk = drupal_truncate_bytes($string, $chunk_size);
396
      $output .= ' =?UTF-8?B?' . base64_encode($chunk) . "?=\n";
397
      $c = strlen($chunk);
398
      $string = substr($string, $c);
399
      $len -= $c;
400
    }
401
    return trim($output);
402
  }
403
  return $string;
404
}
405

    
406
/**
407
 * Decodes MIME/HTTP encoded header values.
408
 *
409
 * @param $header
410
 *   The header to decode.
411
 *
412
 * @return string
413
 *   The mime-decoded header.
414
 *
415
 * @see mime_header_encode()
416
 */
417
function mime_header_decode($header) {
418
  // First step: encoded chunks followed by other encoded chunks (need to collapse whitespace)
419
  $header = preg_replace_callback('/=\?([^?]+)\?(Q|B)\?([^?]+|\?(?!=))\?=\s+(?==\?)/', '_mime_header_decode', $header);
420
  // Second step: remaining chunks (do not collapse whitespace)
421
  return preg_replace_callback('/=\?([^?]+)\?(Q|B)\?([^?]+|\?(?!=))\?=/', '_mime_header_decode', $header);
422
}
423

    
424
/**
425
 * Decodes encoded header data passed from mime_header_decode().
426
 *
427
 * Callback for preg_replace_callback() within mime_header_decode().
428
 *
429
 * @param $matches
430
 *   The array of matches from preg_replace_callback().
431
 *
432
 * @return string
433
 *   The mime-decoded string.
434
 *
435
 * @see mime_header_decode()
436
 */
437
function _mime_header_decode($matches) {
438
  // Regexp groups:
439
  // 1: Character set name
440
  // 2: Escaping method (Q or B)
441
  // 3: Encoded data
442
  $data = ($matches[2] == 'B') ? base64_decode($matches[3]) : str_replace('_', ' ', quoted_printable_decode($matches[3]));
443
  if (strtolower($matches[1]) != 'utf-8') {
444
    $data = drupal_convert_to_utf8($data, $matches[1]);
445
  }
446
  return $data;
447
}
448

    
449
/**
450
 * Decodes all HTML entities (including numerical ones) to regular UTF-8 bytes.
451
 *
452
 * Double-escaped entities will only be decoded once ("&amp;lt;" becomes "&lt;"
453
 * , not "<"). Be careful when using this function, as decode_entities can
454
 * revert previous sanitization efforts (&lt;script&gt; will become <script>).
455
 *
456
 * @param $text
457
 *   The text to decode entities in.
458
 *
459
 * @return
460
 *   The input $text, with all HTML entities decoded once.
461
 */
462
function decode_entities($text) {
463
  return html_entity_decode($text, ENT_QUOTES, 'UTF-8');
464
}
465

    
466
/**
467
 * Counts the number of characters in a UTF-8 string.
468
 *
469
 * This is less than or equal to the byte count.
470
 *
471
 * @param $text
472
 *   The string to run the operation on.
473
 *
474
 * @return integer
475
 *   The length of the string.
476
 *
477
 * @ingroup php_wrappers
478
 */
479
function drupal_strlen($text) {
480
  global $multibyte;
481
  if ($multibyte == UNICODE_MULTIBYTE) {
482
    return mb_strlen($text);
483
  }
484
  else {
485
    // Do not count UTF-8 continuation bytes.
486
    return strlen(preg_replace("/[\x80-\xBF]/", '', $text));
487
  }
488
}
489

    
490
/**
491
 * Uppercase a UTF-8 string.
492
 *
493
 * @param $text
494
 *   The string to run the operation on.
495
 *
496
 * @return string
497
 *   The string in uppercase.
498
 *
499
 * @ingroup php_wrappers
500
 */
501
function drupal_strtoupper($text) {
502
  global $multibyte;
503
  if ($multibyte == UNICODE_MULTIBYTE) {
504
    return mb_strtoupper($text);
505
  }
506
  else {
507
    // Use C-locale for ASCII-only uppercase
508
    $text = strtoupper($text);
509
    // Case flip Latin-1 accented letters
510
    $text = preg_replace_callback('/\xC3[\xA0-\xB6\xB8-\xBE]/', '_unicode_caseflip', $text);
511
    return $text;
512
  }
513
}
514

    
515
/**
516
 * Lowercase a UTF-8 string.
517
 *
518
 * @param $text
519
 *   The string to run the operation on.
520
 *
521
 * @return string
522
 *   The string in lowercase.
523
 *
524
 * @ingroup php_wrappers
525
 */
526
function drupal_strtolower($text) {
527
  global $multibyte;
528
  if ($multibyte == UNICODE_MULTIBYTE) {
529
    return mb_strtolower($text);
530
  }
531
  else {
532
    // Use C-locale for ASCII-only lowercase
533
    $text = strtolower($text);
534
    // Case flip Latin-1 accented letters
535
    $text = preg_replace_callback('/\xC3[\x80-\x96\x98-\x9E]/', '_unicode_caseflip', $text);
536
    return $text;
537
  }
538
}
539

    
540
/**
541
 * Flips U+C0-U+DE to U+E0-U+FD and back.
542
 *
543
 * @param $matches
544
 *   An array of matches.
545
 *
546
 * @return array
547
 *   The Latin-1 version of the array of matches.
548
 *
549
 * @see drupal_strtolower()
550
 */
551
function _unicode_caseflip($matches) {
552
  return $matches[0][0] . chr(ord($matches[0][1]) ^ 32);
553
}
554

    
555
/**
556
 * Capitalizes the first letter of a UTF-8 string.
557
 *
558
 * @param $text
559
 *   The string to convert.
560
 *
561
 * @return
562
 *   The string with the first letter as uppercase.
563
 *
564
 * @ingroup php_wrappers
565
 */
566
function drupal_ucfirst($text) {
567
  // Note: no mbstring equivalent!
568
  return drupal_strtoupper(drupal_substr($text, 0, 1)) . drupal_substr($text, 1);
569
}
570

    
571
/**
572
 * Cuts off a piece of a string based on character indices and counts.
573
 *
574
 * Follows the same behavior as PHP's own substr() function. Note that for
575
 * cutting off a string at a known character/substring location, the usage of
576
 * PHP's normal strpos/substr is safe and much faster.
577
 *
578
 * @param $text
579
 *   The input string.
580
 * @param $start
581
 *   The position at which to start reading.
582
 * @param $length
583
 *   The number of characters to read.
584
 *
585
 * @return
586
 *   The shortened string.
587
 *
588
 * @ingroup php_wrappers
589
 */
590
function drupal_substr($text, $start, $length = NULL) {
591
  global $multibyte;
592
  if ($multibyte == UNICODE_MULTIBYTE) {
593
    return $length === NULL ? mb_substr($text, $start) : mb_substr($text, $start, $length);
594
  }
595
  else {
596
    $strlen = strlen($text);
597
    // Find the starting byte offset.
598
    $bytes = 0;
599
    if ($start > 0) {
600
      // Count all the continuation bytes from the start until we have found
601
      // $start characters or the end of the string.
602
      $bytes = -1; $chars = -1;
603
      while ($bytes < $strlen - 1 && $chars < $start) {
604
        $bytes++;
605
        $c = ord($text[$bytes]);
606
        if ($c < 0x80 || $c >= 0xC0) {
607
          $chars++;
608
        }
609
      }
610
    }
611
    elseif ($start < 0) {
612
      // Count all the continuation bytes from the end until we have found
613
      // abs($start) characters.
614
      $start = abs($start);
615
      $bytes = $strlen; $chars = 0;
616
      while ($bytes > 0 && $chars < $start) {
617
        $bytes--;
618
        $c = ord($text[$bytes]);
619
        if ($c < 0x80 || $c >= 0xC0) {
620
          $chars++;
621
        }
622
      }
623
    }
624
    $istart = $bytes;
625

    
626
    // Find the ending byte offset.
627
    if ($length === NULL) {
628
      $iend = $strlen;
629
    }
630
    elseif ($length > 0) {
631
      // Count all the continuation bytes from the starting index until we have
632
      // found $length characters or reached the end of the string, then
633
      // backtrace one byte.
634
      $iend = $istart - 1;
635
      $chars = -1;
636
      $last_real = FALSE;
637
      while ($iend < $strlen - 1 && $chars < $length) {
638
        $iend++;
639
        $c = ord($text[$iend]);
640
        $last_real = FALSE;
641
        if ($c < 0x80 || $c >= 0xC0) {
642
          $chars++;
643
          $last_real = TRUE;
644
        }
645
      }
646
      // Backtrace one byte if the last character we found was a real character
647
      // and we don't need it.
648
      if ($last_real && $chars >= $length) {
649
        $iend--;
650
      }
651
    }
652
    elseif ($length < 0) {
653
      // Count all the continuation bytes from the end until we have found
654
      // abs($start) characters, then backtrace one byte.
655
      $length = abs($length);
656
      $iend = $strlen; $chars = 0;
657
      while ($iend > 0 && $chars < $length) {
658
        $iend--;
659
        $c = ord($text[$iend]);
660
        if ($c < 0x80 || $c >= 0xC0) {
661
          $chars++;
662
        }
663
      }
664
      // Backtrace one byte if we are not at the beginning of the string.
665
      if ($iend > 0) {
666
        $iend--;
667
      }
668
    }
669
    else {
670
      // $length == 0, return an empty string.
671
      return '';
672
    }
673

    
674
    return substr($text, $istart, max(0, $iend - $istart + 1));
675
  }
676
}