Revert r24105, r24106, r24107 'security fix' forbidden text/css and text/javascript...
[lhc/web/wiklou.git] / includes / normal / UtfNormal.php
index 02fa268..557b8e5 100644 (file)
 #
 # You should have received a copy of the GNU General Public License along
 # with this program; if not, write to the Free Software Foundation, Inc.,
-# 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 # http://www.gnu.org/copyleft/gpl.html
 
-/**
- * Unicode normalization routines for working with UTF-8 strings.
- * Currently assumes that input strings are valid UTF-8!
- *
- * Not as fast as I'd like, but should be usable for most purposes.
- * UtfNormal::toNFC() will bail early if given ASCII text or text
- * it can quickly deterimine is already normalized.
- *
- * All functions can be called static.
- *
- * See description of forms at http://www.unicode.org/reports/tr15/
- *
- * @package UtfNormal
- */
-
 /** */
-require_once 'UtfNormalUtil.php';
+require_once dirname(__FILE__).'/UtfNormalUtil.php';
 
 global $utfCombiningClass, $utfCanonicalComp, $utfCanonicalDecomp;
 $utfCombiningClass = NULL;
@@ -44,59 +29,6 @@ $utfCanonicalDecomp = NULL;
 global $utfCompatibilityDecomp;
 $utfCompatibilityDecomp = NULL;
 
-define( 'UNICODE_HANGUL_FIRST', 0xac00 );
-define( 'UNICODE_HANGUL_LAST',  0xd7a3 );
-
-define( 'UNICODE_HANGUL_LBASE', 0x1100 );
-define( 'UNICODE_HANGUL_VBASE', 0x1161 );
-define( 'UNICODE_HANGUL_TBASE', 0x11a7 );
-
-define( 'UNICODE_HANGUL_LCOUNT', 19 );
-define( 'UNICODE_HANGUL_VCOUNT', 21 );
-define( 'UNICODE_HANGUL_TCOUNT', 28 );
-define( 'UNICODE_HANGUL_NCOUNT', UNICODE_HANGUL_VCOUNT * UNICODE_HANGUL_TCOUNT );
-
-define( 'UNICODE_HANGUL_LEND', UNICODE_HANGUL_LBASE + UNICODE_HANGUL_LCOUNT - 1 );
-define( 'UNICODE_HANGUL_VEND', UNICODE_HANGUL_VBASE + UNICODE_HANGUL_VCOUNT - 1 );
-define( 'UNICODE_HANGUL_TEND', UNICODE_HANGUL_TBASE + UNICODE_HANGUL_TCOUNT - 1 );
-
-define( 'UNICODE_SURROGATE_FIRST', 0xd800 );
-define( 'UNICODE_SURROGATE_LAST', 0xdfff );
-define( 'UNICODE_MAX', 0x10ffff );
-define( 'UNICODE_REPLACEMENT', 0xfffd );
-
-
-define( 'UTF8_HANGUL_FIRST', codepointToUtf8( UNICODE_HANGUL_FIRST ) );
-define( 'UTF8_HANGUL_LAST', codepointToUtf8( UNICODE_HANGUL_LAST ) );
-
-define( 'UTF8_HANGUL_LBASE', codepointToUtf8( UNICODE_HANGUL_LBASE ) );
-define( 'UTF8_HANGUL_VBASE', codepointToUtf8( UNICODE_HANGUL_VBASE ) );
-define( 'UTF8_HANGUL_TBASE', codepointToUtf8( UNICODE_HANGUL_TBASE ) );
-
-define( 'UTF8_HANGUL_LEND', codepointToUtf8( UNICODE_HANGUL_LEND ) );
-define( 'UTF8_HANGUL_VEND', codepointToUtf8( UNICODE_HANGUL_VEND ) );
-define( 'UTF8_HANGUL_TEND', codepointToUtf8( UNICODE_HANGUL_TEND ) );
-
-define( 'UTF8_SURROGATE_FIRST', codepointToUtf8( UNICODE_SURROGATE_FIRST ) );
-define( 'UTF8_SURROGATE_LAST', codepointToUtf8( UNICODE_SURROGATE_LAST ) );
-define( 'UTF8_MAX', codepointToUtf8( UNICODE_MAX ) );
-define( 'UTF8_REPLACEMENT', codepointToUtf8( UNICODE_REPLACEMENT ) );
-#define( 'UTF8_REPLACEMENT', '!' );
-
-define( 'UTF8_OVERLONG_A', "\xc1\xbf" );
-define( 'UTF8_OVERLONG_B', "\xe0\x9f\xbf" );
-define( 'UTF8_OVERLONG_C', "\xf0\x8f\xbf\xbf" );
-
-# These two ranges are illegal
-define( 'UTF8_FDD0', codepointToUtf8( 0xfdd0 ) );
-define( 'UTF8_FDEF', codepointToUtf8( 0xfdef ) );
-define( 'UTF8_FFFE', codepointToUtf8( 0xfffe ) );
-define( 'UTF8_FFFF', codepointToUtf8( 0xffff ) );
-
-define( 'UTF8_HEAD', false );
-define( 'UTF8_TAIL', true );
-
-
 /**
  * For using the ICU wrapper
  */
@@ -111,8 +43,18 @@ define( 'UNORM_FCD',  6 );
 define( 'NORMALIZE_ICU', function_exists( 'utf8_normalize' ) );
 
 /**
+ * Unicode normalization routines for working with UTF-8 strings.
+ * Currently assumes that input strings are valid UTF-8!
  *
- * @package MediaWiki
+ * Not as fast as I'd like, but should be usable for most purposes.
+ * UtfNormal::toNFC() will bail early if given ASCII text or text
+ * it can quickly deterimine is already normalized.
+ *
+ * All functions can be called static.
+ *
+ * See description of forms at http://www.unicode.org/reports/tr15/
+ *
+ * @addtogroup UtfNormal
  */
 class UtfNormal {
        /**
@@ -124,8 +66,9 @@ class UtfNormal {
         *
         * @param string $string a UTF-8 string
         * @return string a clean, shiny, normalized UTF-8 string
+        * @static
         */
-       function cleanUp( $string ) {
+       static function cleanUp( $string ) {
                if( NORMALIZE_ICU ) {
                        # We exclude a few chars that ICU would not.
                        $string = preg_replace(
@@ -153,8 +96,9 @@ class UtfNormal {
         *
         * @param string $string a valid UTF-8 string. Input is not validated.
         * @return string a UTF-8 string in normal form C
+        * @static
         */
-       function toNFC( $string ) {
+       static function toNFC( $string ) {
                if( NORMALIZE_ICU )
                        return utf8_normalize( $string, UNORM_NFC );
                elseif( UtfNormal::quickIsNFC( $string ) )
@@ -169,8 +113,9 @@ class UtfNormal {
         *
         * @param string $string a valid UTF-8 string. Input is not validated.
         * @return string a UTF-8 string in normal form D
+        * @static
         */
-       function toNFD( $string ) {
+       static function toNFD( $string ) {
                if( NORMALIZE_ICU )
                        return utf8_normalize( $string, UNORM_NFD );
                elseif( preg_match( '/[\x80-\xff]/', $string ) )
@@ -186,8 +131,9 @@ class UtfNormal {
         *
         * @param string $string a valid UTF-8 string. Input is not validated.
         * @return string a UTF-8 string in normal form KC
+        * @static
         */
-       function toNFKC( $string ) {
+       static function toNFKC( $string ) {
                if( NORMALIZE_ICU )
                        return utf8_normalize( $string, UNORM_NFKC );
                elseif( preg_match( '/[\x80-\xff]/', $string ) )
@@ -203,8 +149,9 @@ class UtfNormal {
         *
         * @param string $string a valid UTF-8 string. Input is not validated.
         * @return string a UTF-8 string in normal form KD
+        * @static
         */
-       function toNFKD( $string ) {
+       static function toNFKD( $string ) {
                if( NORMALIZE_ICU )
                        return utf8_normalize( $string, UNORM_NFKD );
                elseif( preg_match( '/[\x80-\xff]/', $string ) )
@@ -215,12 +162,13 @@ class UtfNormal {
 
        /**
         * Load the basic composition data if necessary
-        * @access private
+        * @private
+        * @static
         */
-       function loadData() {
-               global $utfCombiningClass, $utfCanonicalComp, $utfCanonicalDecomp;
+       static function loadData() {
+               global $utfCombiningClass;
                if( !isset( $utfCombiningClass ) ) {
-                       require_once( 'UtfNormalData.inc' );
+                       require_once( dirname(__FILE__) . '/UtfNormalData.inc' );
                }
        }
 
@@ -229,8 +177,9 @@ class UtfNormal {
         * Returns false if not or uncertain.
         * @param string $string a valid UTF-8 string. Input is not validated.
         * @return bool
+        * @static
         */
-       function quickIsNFC( $string ) {
+       static function quickIsNFC( $string ) {
                # ASCII is always valid NFC!
                # If it's pure ASCII, let it through.
                if( !preg_match( '/[\x80-\xff]/', $string ) ) return true;
@@ -269,8 +218,9 @@ class UtfNormal {
         * Returns true if the string is _definitely_ in NFC.
         * Returns false if not or uncertain.
         * @param string $string a UTF-8 string, altered on output to be valid UTF-8 safe for XML.
+        * @static
         */
-       function quickIsNFCVerify( &$string ) {
+       static function quickIsNFCVerify( &$string ) {
                # Screen out some characters that eg won't be allowed in XML
                $string = preg_replace( '/[\x00-\x08\x0b\x0c\x0e-\x1f]/', UTF8_REPLACEMENT, $string );
 
@@ -320,6 +270,7 @@ class UtfNormal {
                # large ASCII parts can be handled much more quickly.
                # Don't chop up Unicode areas for punctuation, though,
                # that wastes energy.
+               $matches = array();
                preg_match_all(
                        '/([\x00-\x7f]+|[\x80-\xff][\x00-\x40\x5b-\x5f\x7b-\xff]*)/',
                        $string, $matches );
@@ -486,18 +437,20 @@ class UtfNormal {
        /**
         * @param string $string
         * @return string
-        * @access private
+        * @private
+        * @static
         */
-       function NFC( $string ) {
+       static function NFC( $string ) {
                return UtfNormal::fastCompose( UtfNormal::NFD( $string ) );
        }
 
        /**
         * @param string $string
         * @return string
-        * @access private
+        * @private
+        * @static
         */
-       function NFD( $string ) {
+       static function NFD( $string ) {
                UtfNormal::loadData();
                global $utfCanonicalDecomp;
                return UtfNormal::fastCombiningSort(
@@ -507,18 +460,20 @@ class UtfNormal {
        /**
         * @param string $string
         * @return string
-        * @access private
+        * @private
+        * @static
         */
-       function NFKC( $string ) {
+       static function NFKC( $string ) {
                return UtfNormal::fastCompose( UtfNormal::NFKD( $string ) );
        }
 
        /**
         * @param string $string
         * @return string
-        * @access private
+        * @private
+        * @static
         */
-       function NFKD( $string ) {
+       static function NFKD( $string ) {
                global $utfCompatibilityDecomp;
                if( !isset( $utfCompatibilityDecomp ) ) {
                        require_once( 'UtfNormalDataK.inc' );
@@ -532,12 +487,13 @@ class UtfNormal {
         * Perform decomposition of a UTF-8 string into either D or KD form
         * (depending on which decomposition map is passed to us).
         * Input is assumed to be *valid* UTF-8. Invalid code will break.
-        * @access private
+        * @private
         * @param string $string Valid UTF-8 string
         * @param array $map hash of expanded decomposition map
         * @return string a UTF-8 string decomposed, not yet normalized (needs sorting)
+        * @static
         */
-       function fastDecompose( $string, &$map ) {
+       static function fastDecompose( $string, $map ) {
                UtfNormal::loadData();
                $len = strlen( $string );
                $out = '';
@@ -593,11 +549,12 @@ class UtfNormal {
        /**
         * Sorts combining characters into canonical order. This is the
         * final step in creating decomposed normal forms D and KD.
-        * @access private
+        * @private
         * @param string $string a valid, decomposed UTF-8 string. Input is not validated.
         * @return string a UTF-8 string with combining characters sorted in canonical order
+        * @static
         */
-       function fastCombiningSort( $string ) {
+       static function fastCombiningSort( $string ) {
                UtfNormal::loadData();
                global $utfCombiningClass;
                $len = strlen( $string );
@@ -620,7 +577,11 @@ class UtfNormal {
                                }
                                if( isset( $utfCombiningClass[$c] ) ) {
                                        $lastClass = $utfCombiningClass[$c];
-                                       @$combiners[$lastClass] .= $c;
+                                       if( isset( $combiners[$lastClass] ) ) {
+                                               $combiners[$lastClass] .= $c;
+                                       } else {
+                                               $combiners[$lastClass] = $c;
+                                       }
                                        continue;
                                }
                        }
@@ -642,11 +603,12 @@ class UtfNormal {
        /**
         * Produces canonically composed sequences, i.e. normal form C or KC.
         *
-        * @access private
+        * @private
         * @param string $string a valid UTF-8 string in sorted normal form D or KD. Input is not validated.
         * @return string a UTF-8 string with canonical precomposed characters used where possible
+        * @static
         */
-       function fastCompose( $string ) {
+       static function fastCompose( $string ) {
                UtfNormal::loadData();
                global $utfCanonicalComp, $utfCombiningClass;
                $len = strlen( $string );
@@ -777,8 +739,9 @@ class UtfNormal {
         * interate through a string without really doing anything of substance.
         * @param string $string
         * @return string
+        * @static
         */
-       function placebo( $string ) {
+       static function placebo( $string ) {
                $len = strlen( $string );
                $out = '';
                for( $i = 0; $i < $len; $i++ ) {
@@ -788,4 +751,4 @@ class UtfNormal {
        }
 }
 
-?>
+