commiting uncommited changes on live site
[weblabels.fsf.org.git] / crm.fsf.org / 20131203 / files / sites / all / modules-new / civicrm / packages / ezc / Mail / src / internal / charset_convert.php
1 <?php
2 /**
3 * File containing the ezcMailCharsetConverter class.
4 *
5 * @package Mail
6 * @version 1.7beta1
7 * @copyright Copyright (C) 2005-2009 eZ Systems AS. All rights reserved.
8 * @license http://ez.no/licenses/new_bsd New BSD License
9 */
10
11 /**
12 * Class containing common character set conversion methods.
13 *
14 * By calling the static function ezcMailCharsetConverter::setConvertMethod()
15 * before doing mail parsing, another callback function can be used for
16 * character conversion to UTF-8 in place of the normal iconv() conversion.
17 *
18 * The callback function must have this signature:
19 * <code>
20 * public static function function_name( $text, $originalCharset );
21 * </code>
22 *
23 * where:
24 * - $text = string to convert to UTF-8
25 * - $originalCharset = in what charset is $text
26 *
27 * Example:
28 * <code>
29 * // specify another function for character set conversion
30 * ezcMailCharsetConverter::setConvertMethod( array( 'myConverter', 'convertToUTF8IconvIgnore' ) );
31 *
32 * // ...code for mail parsing...
33 * </code>
34 *
35 * where myConverter is (along with some other examples of charset conversion
36 * functions which can be used):
37 * <code>
38 * class myConverter
39 * {
40 * public static function convertToUTF8IconvIgnore( $text, $originalCharset )
41 * {
42 * if ( $originalCharset === 'unknown-8bit' || $originalCharset === 'x-user-defined' )
43 * {
44 * $originalCharset = "latin1";
45 * }
46 * return iconv( $originalCharset, 'utf-8//IGNORE', $text );
47 * }
48 *
49 * public static function convertToUTF8IconvTranslit( $text, $originalCharset )
50 * {
51 * if ( $originalCharset === 'unknown-8bit' || $originalCharset === 'x-user-defined' )
52 * {
53 * $originalCharset = "latin1";
54 * }
55 * return iconv( $originalCharset, 'utf-8//TRANSLIT', $text );
56 * }
57 *
58 * public static function convertToUTF8Mbstring( $text, $originalCharset )
59 * {
60 * return mb_convert_encoding( $text, "UTF-8", $originalCharset );
61 * }
62 * }
63 * </code>
64 *
65 * Developers can choose to use the error suppresion operator ('@') in front of
66 * the iconv() calls in the above examples, in order to ignore the notices thrown
67 * when processing broken text (issue #8369).
68 *
69 * @package Mail
70 * @version 1.7beta1
71 */
72 class ezcMailCharsetConverter
73 {
74 /**
75 * Callback function to use for character set conversion to UTF8.
76 *
77 * @var callback
78 */
79 private static $method = array( __CLASS__, 'convertToUTF8Iconv' );
80
81 /**
82 * Sets the callback function used for character set conversion to UTF-8.
83 *
84 * Call this method before doing mail parsing if you need a special way
85 * of converting the character set to UTF-8.
86 *
87 * @param callback $method
88 */
89 public static function setConvertMethod( $method )
90 {
91 self::$method = $method;
92 }
93
94 /**
95 * Converts the $text with the charset $originalCharset to UTF-8.
96 *
97 * It calls the function specified by using the static method
98 * setConvertMethod(). By default it calls convertToUTF8Iconv() defined
99 * in this class.
100 *
101 * @param string $text
102 * @param string $originalCharset
103 * @return string
104 */
105 public static function convertToUTF8( $text, $originalCharset )
106 {
107 return call_user_func( self::$method, $text, $originalCharset );
108 }
109
110 /**
111 * Converts the $text with the charset $originalCharset to UTF-8.
112 *
113 * In case $originalCharset is 'unknown-8bit' or 'x-user-defined' then
114 * it is assumed to be 'latin1' (ISO-8859-1).
115 *
116 * @param string $text
117 * @param string $originalCharset
118 * @return string
119 */
120 public static function convertToUTF8Iconv( $text, $originalCharset )
121 {
122 if ( $originalCharset === 'unknown-8bit' || $originalCharset === 'x-user-defined' )
123 {
124 $originalCharset = "latin1";
125 }
126 return iconv( $originalCharset, 'utf-8', $text );
127 }
128 }
129 ?>