Index: branches/5.3.x/core/kernel/globals.php =================================================================== diff -u -N -r15988 -r16111 --- branches/5.3.x/core/kernel/globals.php (.../globals.php) (revision 15988) +++ branches/5.3.x/core/kernel/globals.php (.../globals.php) (revision 16111) @@ -1,6 +1,6 @@ $field_value) { - // replaces an enclosure with two enclosures - $data[$field_index] = str_replace($enclosure, $enclosure.$enclosure, $field_value); + ob_start(); + $fp = fopen('php://output', 'w'); + fputcsv($fp, $data, $delimiter, $enclosure); + fclose($fp); + $ret = ob_get_clean(); + + if ( $recordSeparator != "\n" ) { + return substr($ret, 0, -1) . $recordSeparator; } - $line = $enclosure.implode($enclosure.$delimiter.$enclosure, $data).$enclosure.$recordSeparator; - $line = preg_replace('/'.preg_quote($enclosure, '/').'([0-9\.]+)'.preg_quote($enclosure, '/').'/', '$1', $line); - - return $line; + return $ret; } /** @@ -863,6 +865,64 @@ throw new InvalidArgumentException(sprintf('Unknown escape strategy "%s"', $strategy)); } + /** + * Unescapes a string. + * + * @param string $text Text to unescape. + * @param string $strategy Escape strategy. + * + * @return string + * @throws InvalidArgumentException When unknown escape strategy is given. + */ + public static function unescape($text, $strategy = null) + { + if ( !isset($strategy) ) { + $strategy = self::$escapeStrategy; + } + + if ( strpos($strategy, '+') !== false ) { + $previous_strategy = ''; + $strategies = explode('+', $strategy); + + foreach ($strategies as $current_strategy) { + // apply default strategy + if ( $current_strategy == '' ) { + $current_strategy = self::$escapeStrategy; + } + + // don't double-unescape + if ( $current_strategy != $previous_strategy ) { + $text = self::unescape($text, $current_strategy); + $previous_strategy = $current_strategy; + } + } + + return $text; + } + + if ( $strategy == self::ESCAPE_HTML ) { + return htmlspecialchars_decode($text, ENT_QUOTES); + } + + if ( $strategy == self::ESCAPE_JS ) { + // TODO: consider using "stripcslashes", because "stripslashes" isn't really for JavaScript unescaping (according to docs) + $text = str_replace("", '', $text); + $text = str_replace(array('\r', '\n'), array("\r", "\n"), $text); + $text = stripslashes($text); + + return $text; + } + + if ( $strategy == self::ESCAPE_URL ) { + return rawurldecode($text); + } + + if ( $strategy == self::ESCAPE_RAW ) { + return $text; + } + + throw new InvalidArgumentException(sprintf('Unknown escape strategy "%s"', $strategy)); + } } /** @@ -1008,4 +1068,4 @@ return $res; } -} \ No newline at end of file +}