]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/Irc/extlib/phergie/Phergie/Plugin/Encoding.php
Merge branch '0.9.x' into 1.0.x
[quix0rs-gnu-social.git] / plugins / Irc / extlib / phergie / Phergie / Plugin / Encoding.php
1 <?php
2 /**
3  * Phergie
4  *
5  * PHP version 5
6  *
7  * LICENSE
8  *
9  * This source file is subject to the new BSD license that is bundled
10  * with this package in the file LICENSE.
11  * It is also available through the world-wide-web at this URL:
12  * http://phergie.org/license
13  *
14  * @category  Phergie
15  * @package   Phergie_Plugin_Encoding
16  * @author    Phergie Development Team <team@phergie.org>
17  * @copyright 2008-2010 Phergie Development Team (http://phergie.org)
18  * @license   http://phergie.org/license New BSD License
19  * @link      http://pear.phergie.org/package/Phergie_Plugin_Encoding
20  */
21
22 /**
23  * Handles decoding markup entities and converting text between character
24  * encodings.
25  *
26  * @category Phergie
27  * @package  Phergie_Plugin_Encoding
28  * @author   Phergie Development Team <team@phergie.org>
29  * @license  http://phergie.org/license New BSD License
30  * @link     http://pear.phergie.org/package/Phergie_Plugin_Encoding
31  */
32 class Phergie_Plugin_Encoding extends Phergie_Plugin_Abstract
33 {
34     /**
35      * Lookup table for entity conversions not supported by
36      * html_entity_decode()
37      *
38      * @var array
39      * @link http://us.php.net/manual/en/function.get-html-translation-table.php#73409
40      * @link http://us.php.net/manual/en/function.get-html-translation-table.php#73410
41      */
42     protected static $entities = array(
43         '&alpha;' => 913,
44         '&apos;' => 39,
45         '&beta;' => 914,
46         '&bull;' => 149,
47         '&chi;' => 935,
48         '&circ;' => 94,
49         '&delta;' => 916,
50         '&epsilon;' => 917,
51         '&eta;' => 919,
52         '&fnof;' => 402,
53         '&gamma;' => 915,
54         '&iota;' => 921,
55         '&kappa;' => 922,
56         '&lambda;' => 923,
57         '&ldquo;' => 147,
58         '&lsaquo;' => 139,
59         '&lsquo;' => 145,
60         '&mdash;' => 151,
61         '&minus;' => 45,
62         '&mu;' => 924,
63         '&ndash;' => 150,
64         '&nu;' => 925,
65         '&oelig;' => 140,
66         '&omega;' => 937,
67         '&omicron;' => 927,
68         '&phi;' => 934,
69         '&pi;' => 928,
70         '&piv;' => 982,
71         '&psi;' => 936,
72         '&rdquo;' => 148,
73         '&rho;' => 929,
74         '&rsaquo;' => 155,
75         '&rsquo;' => 146,
76         '&scaron;' => 138,
77         '&sigma;' => 931,
78         '&sigmaf;' => 962,
79         '&tau;' => 932,
80         '&theta;' => 920,
81         '&thetasym;' => 977,
82         '&tilde;' => 126,
83         '&trade;' => 153,
84         '&upsih;' => 978,
85         '&upsilon;' => 933,
86         '&xi;' => 926,
87         '&yuml;' => 159,
88         '&zeta;' => 918,
89     );
90
91     /**
92      * Decodes markup entities in a given string.
93      *
94      * @param string $string  String containing markup entities
95      * @param string $charset Optional character set name to use in decoding
96      *        entities, defaults to UTF-8
97      *
98      * @return string String with markup entities decoded
99      */
100     public function decodeEntities($string, $charset = 'UTF-8')
101     {
102         $string = str_ireplace(
103             array_keys(self::$entities),
104             array_map('chr', self::$entities),
105             $string
106         );
107         $string = html_entity_decode($string, ENT_QUOTES, $charset);
108         $string = preg_replace(
109             array('/&#0*([0-9]+);/me', '/&#x0*([a-f0-9]+);/mei'),
110             array('$this->codeToUtf(\\1)', '$this->codeToUtf(hexdec(\\1))'),
111             $string
112         );
113         return $string;
114     }
115
116     /**
117      * Converts a given unicode to its UTF-8 equivalent.
118      *
119      * @param int $code Code to convert
120      * @return string Character corresponding to code
121      */
122     public function codeToUtf8($code)
123     {
124         $code = (int) $code;
125         switch ($code) {
126             // 1 byte, 7 bits
127             case 0:
128                 return chr(0);
129             case ($code & 0x7F):
130                 return chr($code);
131
132             // 2 bytes, 11 bits
133             case ($code & 0x7FF):
134                 return chr(0xC0 | (($code >> 6) & 0x1F)) .
135                        chr(0x80 | ($code & 0x3F));
136
137             // 3 bytes, 16 bits
138             case ($code & 0xFFFF):
139                 return chr(0xE0 | (($code >> 12) & 0x0F)) .
140                        chr(0x80 | (($code >> 6) & 0x3F)) .
141                        chr(0x80 | ($code & 0x3F));
142
143             // 4 bytes, 21 bits
144             case ($code & 0x1FFFFF):
145                 return chr(0xF0 | ($code >> 18)) .
146                        chr(0x80 | (($code >> 12) & 0x3F)) .
147                        chr(0x80 | (($code >> 6) & 0x3F)) .
148                        chr(0x80 | ($code & 0x3F));
149         }
150     }
151
152     /**
153      * Transliterates characters in a given string where possible.
154      *
155      * @param string $string      String containing characters to
156      *        transliterate
157      * @param string $charsetFrom Optional character set of the string,
158      *        defaults to UTF-8
159      * @param string $charsetTo   Optional character set to which the string
160      *        should be converted, defaults to ISO-8859-1
161      *
162      * @return string String with characters transliterated or the original
163      *         string if transliteration was not possible
164      */
165     public function transliterate($string, $charsetFrom = 'UTF-8', $charsetTo = 'ISO-8859-1')
166     {
167         // @link http://pecl.php.net/package/translit
168         if (function_exists('transliterate')) {
169             $string = transliterate($string, array('han_transliterate', 'diacritical_remove'), $charsetFrom, $charsetTo);
170         } elseif (function_exists('iconv')) {
171             $string = iconv($charsetFrom, $charsetTo . '//TRANSLIT', $string);
172         } else {
173             // @link http://stackoverflow.com/questions/1284535/php-transliteration/1285491#1285491
174             $string = preg_replace(
175                 '~&([a-z]{1,2})(acute|cedil|circ|grave|lig|orn|ring|slash|th|tilde|uml);~i',
176                 '$1',
177                 htmlentities($string, ENT_COMPAT, $charsetFrom)
178             );
179         }
180         return $string;
181     }
182 }