/** * converter * * @param string $text */ protected function converter(&$text) { static $searchReplaceArrayKeys = null; static $searchReplaceArrayValues = null; static $endSearchReplaceArrayKeys = null; static $endSearchReplaceArrayValues = null; $searchReplaceArrayKeys = $searchReplaceArrayKeys === null ? array_keys($this->searchReplaceArray) : $searchReplaceArrayKeys; $searchReplaceArrayValues = $searchReplaceArrayValues === null ? array_values($this->searchReplaceArray) : $searchReplaceArrayValues; $endSearchReplaceArrayKeys = $endSearchReplaceArrayKeys === null ? array_keys($this->endSearchReplaceArray) : $endSearchReplaceArrayKeys; $endSearchReplaceArrayValues = $endSearchReplaceArrayValues === null ? array_values($this->endSearchReplaceArray) : $endSearchReplaceArrayValues; // convert <BLOCKQUOTE> (before PRE!) $this->convertBlockquotes($text); // convert <PRE> $this->convertPre($text); // run our defined tags search-and-replace $text = preg_replace($searchReplaceArrayKeys, $searchReplaceArrayValues, $text); // run our defined tags search-and-replace with callback $text = preg_replace_callback($this->callbackSearch, array($this, 'pregCallback'), $text); // strip any other HTML tags $text = preg_replace('/(<(\\/|!)?\\w+[^>]*>)|(<!--.*?-->)/s', '', $text); // run our defined entities/characters search-and-replace $text = preg_replace($endSearchReplaceArrayKeys, $endSearchReplaceArrayValues, $text); // replace known html entities $text = UTF8::html_entity_decode($text); // replace html entities which represent UTF-8 codepoints. $text = preg_replace_callback("/&#\\d{2,4};/", array($this, 'entityCallback'), $text); // remove unknown/unhandled entities (this cannot be done in search-and-replace block) $text = preg_replace('/&[a-zA-Z0-9]{2,6};/', '', $text); // convert "|+|amp|+|" into "&", need to be done after handling of unknown entities // this properly handles situation of "&quot;" in input string $text = str_replace('|+|amp|+|', '&', $text); // normalise empty lines $text = preg_replace("/\n\\s+\n/", "\n\n", $text); $text = preg_replace("/[\n]{3,}/", "\n\n", $text); // remove leading empty lines (can be produced by eg. P tag on the beginning) $text = UTF8::trim($text, "\n"); if ($this->options['width'] > 0) { $text = wordwrap($text, $this->options['width']); } }
/** * escape * * @param array|float|int|string|boolean $var boolean: convert into "integer"<br /> * int: convert into "integer"<br /> * float: convert into "float" and replace "," with "."<br /> * array: run escape() for every key => value<br /> * string: run UTF8::cleanup() and mysqli_real_escape_string()<br /> * @param bool $stripe_non_utf8 * @param bool $html_entity_decode * @param bool $array_to_string * * @return array|bool|float|int|string */ public function escape($var = '', $stripe_non_utf8 = true, $html_entity_decode = true, $array_to_string = false) { if (is_int($var) || is_bool($var)) { // int return (int) $var; } elseif (is_float($var)) { // float return number_format((double) str_replace(',', '.', $var), 8, '.', ''); } elseif (is_array($var)) { // array $varCleaned = array(); foreach ($var as $key => $value) { $key = (string) $this->escape($key, $stripe_non_utf8, $html_entity_decode); $value = (string) $this->escape($value, $stripe_non_utf8, $html_entity_decode); $varCleaned[$key] = $value; } if ($array_to_string === true) { $varCleaned = implode(',', $varCleaned); return $varCleaned; } else { return (array) $varCleaned; } } if (is_string($var)) { // string if ($stripe_non_utf8 === true) { $var = UTF8::cleanup($var); } if ($html_entity_decode === true) { // use no-html-entity for db $var = UTF8::html_entity_decode($var); } $var = get_magic_quotes_gpc() ? stripslashes($var) : $var; $var = mysqli_real_escape_string($this->getLink(), $var); return (string) $var; } else { return false; } }
/** * @param string $str * @param string $element * * @return string */ private function convertElement($str, $element) { $options = $this->getOptionsForElement($element); if (!$options) { return $str; } if (isset($options['case']) && $options['case'] != self::OPTION_NONE) { $mode = self::$caseModeMapping[$options['case']]; // string can contain HTML tags $chunks = preg_split('/(<[^>]*>)/', $str, -1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE); // convert only the text between HTML tags foreach ($chunks as $i => &$chunk) { if ($chunk[0] !== '<') { $chunk = UTF8::html_entity_decode($str); $chunk = mb_convert_case($chunk, $mode, 'UTF-8'); $chunk = htmlspecialchars_decode($chunk, ENT_QUOTES); } } $str = implode($chunks); if ($options['case'] == self::OPTION_UCFIRST) { $str = UTF8::ucfirst($str); } } if (isset($options['replace']) && $options['replace']) { if (isset($options['replace'][2])) { $delimiter = $options['replace'][2]; } else { $delimiter = '@'; } $str = preg_replace($delimiter . $options['replace'][0] . $delimiter, $options['replace'][1], $str); } if (isset($options['prepend']) && $options['prepend']) { $str = $options['prepend'] . $str; } if (isset($options['append']) && $options['append']) { $str .= $options['append']; } return $str; }
/** * Convert all HTML entities to their applicable characters. * * @param int|null $flags Optional flags * * @return Stringy Object with the resulting $str after being html decoded. */ public function htmlDecode($flags = ENT_COMPAT) { $str = UTF8::html_entity_decode($this->str, $flags, $this->encoding); return static::create($str, $this->encoding); }
/** * Escape: Use "mysqli_real_escape_string" and clean non UTF-8 chars + some extra optional stuff. * * @param mixed $var boolean: convert into "integer"<br /> * int: int (don't change it)<br /> * float: float (don't change it)<br /> * null: null (don't change it)<br /> * array: run escape() for every key => value<br /> * string: run UTF8::cleanup() and mysqli_real_escape_string()<br /> * @param bool $stripe_non_utf8 * @param bool $html_entity_decode * @param bool|null $convert_array <strong>false</strong> => Keep the array.<br /> * <strong>true</strong> => Convert to string var1,var2,var3...<br /> * <strong>null</strong> => Convert the array into null, every time. * * @return mixed */ public function escape($var = '', $stripe_non_utf8 = true, $html_entity_decode = false, $convert_array = false) { if ($var === null) { return null; } // save the current value as int (for later usage) if (!is_object($var)) { $varInt = (int) $var; } /** @noinspection TypeUnsafeComparisonInspection */ if (is_int($var) || is_bool($var) || isset($varInt, $var[0]) && $var[0] != '0' && "{$varInt}" == $var) { // "int" || int || bool return (int) $var; } elseif (is_float($var)) { // float return $var; } elseif (is_array($var)) { // array if ($convert_array === null) { return null; } $varCleaned = array(); foreach ((array) $var as $key => $value) { $key = $this->escape($key, $stripe_non_utf8, $html_entity_decode); $value = $this->escape($value, $stripe_non_utf8, $html_entity_decode); /** @noinspection OffsetOperationsInspection */ $varCleaned[$key] = $value; } if ($convert_array === true) { $varCleaned = implode(',', $varCleaned); return $varCleaned; } else { return (array) $varCleaned; } } if (is_string($var) || is_object($var) && method_exists($var, '__toString')) { // "string" $var = (string) $var; if ($stripe_non_utf8 === true) { $var = UTF8::cleanup($var); } if ($html_entity_decode === true) { // use no-html-entity for db $var = UTF8::html_entity_decode($var); } $var = get_magic_quotes_gpc() ? stripslashes($var) : $var; $var = \mysqli_real_escape_string($this->getLink(), $var); return (string) $var; } elseif ($var instanceof \DateTime) { // "DateTime"-object try { return $this->escape($var->format('Y-m-d H:i:s'), false); } catch (\Exception $e) { return null; } } else { return false; } }
/** * @param string $content * * @return string */ protected function fixHtmlOutput($content) { // INFO: DOMDocument will encapsulate plaintext into a paragraph tag (<p>), // so we try to remove it here again ... if ($this->isDOMDocumentCreatedWithoutHtmlWrapper === true) { $content = str_replace(array("\n", "\r\n", "\r", '<simpleHtmlDomP>', '</simpleHtmlDomP>', '<body>', '</body>', '<html>', '</html>'), '', $content); } if ($this->isDOMDocumentCreatedWithoutHtml === true) { $content = str_replace(array('<p>', '</p>', '<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">'), '', $content); } $content = UTF8::html_entity_decode($content); $content = trim($content); $content = UTF8::rawurldecode($content); $content = self::putReplacedBackToPreserveHtmlEntities($content); return $content; }