src/Service/Misc/Encoding.php
public function encodeString($data, $code = false)
    {
        // We will not encode an empty string.
        if ($data === '') {
            return '';
        }

        // Initialize the encoding configuration.
        if ($code === true) {
            // We encoding @, because we need them for our chunks.
            // The { are needed in the marker of the skin.
            // We also replace tabs with two nbsp's.
            $sortingCallback = array($this, 'arrayMapCallbackCode');
            $search = array('@', '{', chr(9));
            $replace = array('&#64;', '&#123;', '&nbsp;&nbsp;');
        } else {
            // We encoding @, because we need them for our chunks.
            // The { are needed in the marker of the skin.
            $sortingCallback = array($this, 'arrayMapCallbackNormal');
            $search = array('@', '{', '  ');
            $replace = array('&#64;', '&#123;', '&nbsp;&nbsp;');
        }

        // There are several places here, that may throw a warning.
        set_error_handler(
            function () {
                // Do nothing.
            }
        );

        $result = str_replace($search, $replace, htmlentities($data));

        // Check if encoding was successful.
        // 99.99% of the time, the encoding works.
        if (empty($result) === true) {
            // Here we have another SPOF. When the string is large enough
            // we will run out of memory!
            // @see https://sourceforge.net/p/krexx/bugs/21/
            // We will *NOT* return the unescaped string. So we must check if it
            // is small enough for the unpack().
            // 100 kb should be save enough.
            if (strlen($data) > 102400) {
                $result = $this->pool->messages->getHelp('stringTooLarge');
            } else {
                // Something went wrong with the encoding, we need to
                // completely encode this one to be able to display it at all!
                $data = mb_convert_encoding($data, 'UTF-32', mb_detect_encoding($data));
                $result = implode("", array_map($sortingCallback, unpack("N*", $data)));
            }
        }

        // Reactivate whatever error handling we had previously.
        restore_error_handler();

        return $result;
    }
public function encodeString($data, $code = false)
    {
        // We will not encode an empty string.
        if ($data === '') {
            return '';
        }

        // Initialize the encoding configuration.
        if ($code === true) {
            // We encoding @, because we need them for our chunks.
            // The { are needed in the marker of the skin.
            // We also replace tabs with two nbsp's.
            $sortingCallback = array($this, 'arrayMapCallbackCode');
            $search = array('@', '{', chr(9));
            $replace = array('&#64;', '&#123;', '&nbsp;&nbsp;');
        } else {
            // We encoding @, because we need them for our chunks.
            // The { are needed in the marker of the skin.
            $sortingCallback = array($this, 'arrayMapCallbackNormal');
            $search = array('@', '{', '  ');
            $replace = array('&#64;', '&#123;', '&nbsp;&nbsp;');
        }

        // There are several places here, that may throw a warning.
        set_error_handler(
            function () {
                // Do nothing.
            }
        );

        $result = str_replace($search, $replace, htmlentities($data));

        // Check if encoding was successful.
        // 99.99% of the time, the encoding works.
        if (empty($result) === true) {
            // Here we have another SPOF. When the string is large enough
            // we will run out of memory!
            // @see https://sourceforge.net/p/krexx/bugs/21/
            // We will *NOT* return the unescaped string. So we must check if it
            // is small enough for the unpack().
            // 100 kb should be save enough.
            if (strlen($data) > 102400) {
                $result = $this->pool->messages->getHelp('stringTooLarge');
            } else {
                // Something went wrong with the encoding, we need to
                // completely encode this one to be able to display it at all!
                $data = mb_convert_encoding($data, 'UTF-32', mb_detect_encoding($data));
                $result = implode("", array_map($sortingCallback, unpack("N*", $data)));
            }
        }

        // Reactivate whatever error handling we had previously.
        restore_error_handler();

        return $result;
    }
public function encodeString ( $ data , $ code = false ) { // We will not encode an empty string. if ( $ data === '' ) { return '' ; } // Initialize the encoding configuration. if ( $ code === true ) { // We encoding @, because we need them for our chunks. // The { are needed in the marker of the skin. // We also replace tabs with two nbsp's. $ sortingCallback = array ( $ this , 'arrayMapCallbackCode' ) ; $ search = array ( '@' , '{' , chr ( 9 ) ) ; $ replace = array ( '&#64;' , '&#123;' , '&nbsp;&nbsp;' ) ; } else { // We encoding @, because we need them for our chunks. // The { are needed in the marker of the skin. $ sortingCallback = array ( $ this , 'arrayMapCallbackNormal' ) ; $ search = array ( '@' , '{' , '  ' ) ; $ replace = array ( '&#64;' , '&#123;' , '&nbsp;&nbsp;' ) ; } // There are several places here, that may throw a warning. set_error_handler ( function ( ) { // Do nothing. } ) ; $ result = str_replace ( $ search , $ replace , htmlentities ( $ data ) ) ; // Check if encoding was successful. // 99.99% of the time, the encoding works. if ( empty ( $ result ) === true ) { // Here we have another SPOF. When the string is large enough // we will run out of memory! // @see https://sourceforge.net/p/krexx/bugs/21/ // We will *NOT* return the unescaped string. So we must check if it // is small enough for the unpack(). // 100 kb should be save enough. if ( strlen ( $ data ) > 102400 ) { $ result = $ this -> pool -> messages -> getHelp ( 'stringTooLarge' ) ; } else { // Something went wrong with the encoding, we need to // completely encode this one to be able to display it at all! $ data = mb_convert_encoding ( $ data , 'UTF-32' , mb_detect_encoding ( $ data ) ) ; $ result = implode ( "" , array_map ( $ sortingCallback , unpack ( "N*" , $ data ) ) ) ; } } // Reactivate whatever error handling we had previously. restore_error_handler ( ) ; return $ result ; }
Sanitizes a string, by completely encoding it.

Should work with mixed encoding.

@param string $data
The data which needs to be sanitized.
@param boolean $code
Do we need to format the string as code?

@return string
The encoded string.
Sanitizes a string by completely encoding it .