summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorEvan Prodromou <evan@status.net>2010-10-12 11:16:14 -0400
committerEvan Prodromou <evan@status.net>2010-10-12 11:16:14 -0400
commitf11c1c77cab7d7310ec0d2c17bc6f35c491f2871 (patch)
tree7d0741b5d1b68cfbfa21ed974abab04e7b721b0b /lib
parent1e3d5f80258811ce1c2154fcd971297e24264894 (diff)
parent1cd60579f5ac99a2c8bfb12d35093f5c74f14b04 (diff)
Merge remote branch 'gitorious/0.9.x' into 0.9.x
Diffstat (limited to 'lib')
-rw-r--r--lib/util.php47
1 files changed, 40 insertions, 7 deletions
diff --git a/lib/util.php b/lib/util.php
index dc853f657..c05fcf15a 100644
--- a/lib/util.php
+++ b/lib/util.php
@@ -906,6 +906,33 @@ function common_shorten_links($text, $always = false)
return common_replace_urls_callback($text, array('File_redirection', 'makeShort'));
}
+/**
+ * Very basic stripping of invalid UTF-8 input text.
+ *
+ * @param string $str
+ * @return mixed string or null if invalid input
+ *
+ * @todo ideally we should drop bad chars, and maybe do some of the checks
+ * from common_xml_safe_str. But we can't strip newlines, etc.
+ * @todo Unicode normalization might also be useful, but not needed now.
+ */
+function common_validate_utf8($str)
+{
+ // preg_replace will return NULL on invalid UTF-8 input.
+ //
+ // Note: empty regex //u also caused NULL return on some
+ // production machines, but none of our test machines.
+ //
+ // This should be replaced with a more reliable check.
+ return preg_replace('/\x00/u', '', $str);
+}
+
+/**
+ * Make sure an arbitrary string is safe for output in XML as a single line.
+ *
+ * @param string $str
+ * @return string
+ */
function common_xml_safe_str($str)
{
// Replace common eol and extra whitespace input chars
@@ -1663,19 +1690,25 @@ function common_config($main, $sub)
array_key_exists($sub, $config[$main])) ? $config[$main][$sub] : false;
}
+/**
+ * Pull arguments from a GET/POST/REQUEST array with first-level input checks:
+ * strips "magic quotes" slashes if necessary, and kills invalid UTF-8 strings.
+ *
+ * @param array $from
+ * @return array
+ */
function common_copy_args($from)
{
$to = array();
$strip = get_magic_quotes_gpc();
foreach ($from as $k => $v) {
- if($strip) {
- if(is_array($v)) {
- $to[$k] = common_copy_args($v);
- } else {
- $to[$k] = stripslashes($v);
- }
+ if(is_array($v)) {
+ $to[$k] = common_copy_args($v);
} else {
- $to[$k] = $v;
+ if ($strip) {
+ $v = stripslashes($v);
+ }
+ $to[$k] = strval(common_validate_utf8($v));
}
}
return $to;