Swat/SwatForm.php
protected function processEncoding()
    {
        $raw_data = &$this->getFormData();

        if (
            $this->_8bit_encoding !== null &&
            isset($raw_data[self::ENCODING_FIELD])
        ) {
            $value = $raw_data[self::ENCODING_FIELD];

            if ($value === self::ENCODING_8BIT_VALUE) {
                // convert from our 8-bit encoding to utf-8
                foreach ($raw_data as $key => &$value) {
                    if (!mb_check_encoding($value, $this->_8bit_encoding)) {
                        // The 8-bit data is not valid for the 8-bit character
                        // encoding. This can happen, for example, if robots
                        // submit the 8-bit detection value and then submit
                        // UTF-8 encoded values.
                        throw new SwatInvalidCharacterEncodingException(
                            sprintf(
                                "Form submitted with 8-bit encoding but form " .
                                    "data is not valid %s. If this form data is " .
                                    "expected, use the " .
                                    "SwatForm::set8BitEncoding() method to set an " .
                                    "appropriate 8-bit encoding value.\n\nForm " .
                                    "data: \n%s",
                                $this->_8bit_encoding,
                                file_get_contents('php://input')
                            )
                        );
                    }

                    $value = iconv($this->_8bit_encoding, 'utf-8', $value);
                }

                foreach ($_FILES as &$file) {
                    $filename = $file['name'];
                    if (!mb_check_encoding($filename, $this->_8bit_encoding)) {
                        // The 8-bit data is not valid for the 8-bit character
                        // encoding. This can happen, for example, if robots
                        // submit the 8-bit detection value and then submit
                        // UTF-8 encoded values.
                        throw new SwatInvalidCharacterEncodingException(
                            sprintf(
                                "Form submitted with 8-bit encoding but form " .
                                    "data is not valid %s. If this form data is " .
                                    "expected, use the " .
                                    "SwatForm::set8BitEncoding() method to set an " .
                                    "appropriate 8-bit encoding value.\n\nForm " .
                                    "data: \n%s",
                                $this->_8bit_encoding,
                                file_get_contents('php://input')
                            )
                        );
                    }
                    $file['name'] = iconv(
                        $this->_8bit_encoding,
                        'utf-8',
                        $filename
                    );
                }
            } elseif ($value !== self::ENCODING_UTF8_VALUE) {
                // it's not 8-bit or UTF-8. Time to panic!
                throw new SwatInvalidCharacterEncodingException(
                    "Unknown form data character encoding. Form data: \n" .
                        file_get_contents('php://input')
                );
            }
        }
    }
protected function processEncoding()
    {
        $raw_data = &$this->getFormData();

        if (
            $this->_8bit_encoding !== null &&
            isset($raw_data[self::ENCODING_FIELD])
        ) {
            $value = $raw_data[self::ENCODING_FIELD];

            if ($value === self::ENCODING_8BIT_VALUE) {
                // convert from our 8-bit encoding to utf-8
                foreach ($raw_data as $key => &$value) {
                    if (!mb_check_encoding($value, $this->_8bit_encoding)) {
                        // The 8-bit data is not valid for the 8-bit character
                        // encoding. This can happen, for example, if robots
                        // submit the 8-bit detection value and then submit
                        // UTF-8 encoded values.
                        throw new SwatInvalidCharacterEncodingException(
                            sprintf(
                                "Form submitted with 8-bit encoding but form " .
                                    "data is not valid %s. If this form data is " .
                                    "expected, use the " .
                                    "SwatForm::set8BitEncoding() method to set an " .
                                    "appropriate 8-bit encoding value.\n\nForm " .
                                    "data: \n%s",
                                $this->_8bit_encoding,
                                file_get_contents('php://input')
                            )
                        );
                    }

                    $value = iconv($this->_8bit_encoding, 'utf-8', $value);
                }

                foreach ($_FILES as &$file) {
                    $filename = $file['name'];
                    if (!mb_check_encoding($filename, $this->_8bit_encoding)) {
                        // The 8-bit data is not valid for the 8-bit character
                        // encoding. This can happen, for example, if robots
                        // submit the 8-bit detection value and then submit
                        // UTF-8 encoded values.
                        throw new SwatInvalidCharacterEncodingException(
                            sprintf(
                                "Form submitted with 8-bit encoding but form " .
                                    "data is not valid %s. If this form data is " .
                                    "expected, use the " .
                                    "SwatForm::set8BitEncoding() method to set an " .
                                    "appropriate 8-bit encoding value.\n\nForm " .
                                    "data: \n%s",
                                $this->_8bit_encoding,
                                file_get_contents('php://input')
                            )
                        );
                    }
                    $file['name'] = iconv(
                        $this->_8bit_encoding,
                        'utf-8',
                        $filename
                    );
                }
            } elseif ($value !== self::ENCODING_UTF8_VALUE) {
                // it's not 8-bit or UTF-8. Time to panic!
                throw new SwatInvalidCharacterEncodingException(
                    "Unknown form data character encoding. Form data: \n" .
                        file_get_contents('php://input')
                );
            }
        }
    }
protected function processEncoding ( ) { $ raw_data = & $ this -> getFormData ( ) ; if ( $ this -> _8bit_encoding !== null && isset ( $ raw_data [ self :: ENCODING_FIELD ] ) ) { $ value = $ raw_data [ self :: ENCODING_FIELD ] ; if ( $ value === self :: ENCODING_8BIT_VALUE ) { // convert from our 8-bit encoding to utf-8 foreach ( $ raw_data as $ key => & $ value ) { if ( ! mb_check_encoding ( $ value , $ this -> _8bit_encoding ) ) { // The 8-bit data is not valid for the 8-bit character // encoding. This can happen, for example, if robots // submit the 8-bit detection value and then submit // UTF-8 encoded values. throw new SwatInvalidCharacterEncodingException ( sprintf ( "Form submitted with 8-bit encoding but form " . "data is not valid %s. If this form data is " . "expected, use the " . "SwatForm::set8BitEncoding() method to set an " . "appropriate 8-bit encoding value.\n\nForm " . "data: \n%s" , $ this -> _8bit_encoding , file_get_contents ( 'php://input' ) ) ) ; } $ value = iconv ( $ this -> _8bit_encoding , 'utf-8' , $ value ) ; } foreach ( $ _FILES as & $ file ) { $ filename = $ file [ 'name' ] ; if ( ! mb_check_encoding ( $ filename , $ this -> _8bit_encoding ) ) { // The 8-bit data is not valid for the 8-bit character // encoding. This can happen, for example, if robots // submit the 8-bit detection value and then submit // UTF-8 encoded values. throw new SwatInvalidCharacterEncodingException ( sprintf ( "Form submitted with 8-bit encoding but form " . "data is not valid %s. If this form data is " . "expected, use the " . "SwatForm::set8BitEncoding() method to set an " . "appropriate 8-bit encoding value.\n\nForm " . "data: \n%s" , $ this -> _8bit_encoding , file_get_contents ( 'php://input' ) ) ) ; } $ file [ 'name' ] = iconv ( $ this -> _8bit_encoding , 'utf-8' , $ filename ) ; } } elseif ( $ value !== self :: ENCODING_UTF8_VALUE ) { // it's not 8-bit or UTF-8. Time to panic! throw new SwatInvalidCharacterEncodingException ( "Unknown form data character encoding. Form data: \n" . file_get_contents ( 'php://input' ) ) ; } } }
Detects 8-bit character encoding in form data and converts data to UTF-8

Conversion is only performed if this form's 8-bit encoding is set. This
form's 8-bit encoding may be set automatically if the SwatForm default
8-bit encoding is set.

This algorithm is adapted from a blog post at
{@link http://blogs.sun.com/shankar/entry/how_to_handle_utf_8}.

@throws SwatException if an 8-bit encoding is set and the form data is
neither 8-bit nor UTF-8.
Detects 8 - bit character encoding in form data and converts data to UTF - 8