/** * Returns the supplied input string after removing any characters not * present in the supplied whitelist. * * @param string $input string input to be filtered. * @param string $whitelist array or string of whitelist characters. * * @return string returns characters from $input that are present in $whitelist. */ public function whitelist($input, $whitelist) { // Sanity check if (!is_string($input) || $input == '') { $input = ''; } if (is_string($whitelist)) { $charEnc = Codec::detectEncoding($whitelist); $limit = mb_strlen($whitelist, $charEnc); $ary = array(); for ($i = 0; $i < $limit; $i++) { $ary[] = mb_substr($whitelist, $i, 1, $charEnc); } $whitelist = $ary; } $filtered = ''; $initialCharEnc = Codec::detectEncoding($input); $_4ByteCharacterString = Codec::normalizeEncoding($input); $limit = mb_strlen($_4ByteCharacterString, 'UTF-32'); for ($i = 0; $i < $limit; $i++) { $c = mb_substr($_4ByteCharacterString, $i, 1, 'UTF-32'); if (Codec::containsCharacter($c, $whitelist)) { $filtered .= $c; } } if ($filtered != '') { $filtered = mb_convert_encoding($filtered, $initialCharEnc, 'UTF-32'); } if (!is_string($filtered)) { $filtered = ''; } return $filtered; }
public function encodeCharacter($dummy_place_holder, $input) { $detectedCharacterEncoding = Codec::detectEncoding($input); $c = mb_substr($input, 0, 1, $detectedCharacterEncoding); return $this->encode($c, false); }
/** * Helper function. * * Helper method to replace carriage return and line feed characters in the * supplied message with the supplied substitute character(s). The sequence * CRLF (\r\n) is treated as one character. * * @param string $message message to process. * @param string $substitute Replacement for CR, LF or CRLF. * * @return string message with characters replaced. */ private function _replaceCRLF($message, $substitute) { if ($message === null || $substitute === null) { return $message; } $detectedEncoding = Codec::detectEncoding($message); $len = mb_strlen($message, $detectedEncoding); $crlfEncoded = ''; $nextChar = null; $index = 0; for ($i = 0; $i < $len; $i++) { if ($i < $index) { continue; } if ($nextChar === null) { $thisChar = mb_substr($message, $i, 1, $detectedEncoding); } else { $thisChar = $nextChar; } if ($i + 1 < $len) { $nextChar = mb_substr($message, $i + 1, 1, $detectedEncoding); } else { $nextChar = null; } if ($thisChar == "\r" && $nextChar == "\n") { $index = $i + 2; $nextChar = null; $crlfEncoded .= $substitute; } elseif ($thisChar == "\r" || $thisChar == "\n") { $crlfEncoded .= $substitute; } else { $crlfEncoded .= $thisChar; } } return $crlfEncoded; }
/** * Encodes a single character to Base64. * * @param string $immune: not used, but needs to be here to be compatible with Codec::encodeCharacter * @param string $input the character to encode * * @return string the base64 encoded character */ public function encodeCharacter($immune = '', $c) { $detectedCharacterEncoding = Codec::detectEncoding($c); $c = mb_substr($c, 0, 1, $detectedCharacterEncoding); return $this->encode($c, false); }