/** * Decode a JSON encoded structure into a PHP variable. * * JSON data is always UTF-8. In case Phorum is not using UTF-8 as the * default charset, the data is converted from UTF-8 back to Phorum's * charset automatically. * * @param string $json * The JSON structure to decode. * * @return string * The PHP variable representation of the JSON structure. */ function phorum_api_json_decode($json) { global $PHORUM; $var = json_decode($json, TRUE); if (strtoupper($PHORUM['DATA']['CHARSET']) != 'UTF-8') { $var = phorum_api_charset_convert_from_utf8($var); } return $var; }
/** * A helper function that converts a PHP variable from UTF-8 to * the active Phorum charset (stored in $PHORUM["DATA"]["CHARSET"]). * * @param mixed $var * The variable to convert from UTF-8. * * @return mixed * The converted variable. */ function phorum_api_charset_convert_from_utf8($var) { global $PHORUM; // Don't convert if Phorum is in UTF-8 mode already. if (strtoupper($PHORUM['DATA']['CHARSET']) == 'UTF-8') { return $var; } if (is_array($var)) { $new = array(); foreach ($var as $key => $value) { $key = iconv('UTF-8', $PHORUM['DATA']['CHARSET'], $key); if (is_string($value)) { $value = iconv('UTF-8', $PHORUM['DATA']['CHARSET'], $value); } elseif (is_array($value) || is_object($value)) { $value = phorum_api_charset_convert_from_utf8($value); } $new[$key] = $value; } $var = $new; } elseif (is_object($var)) { $var = clone $var; $vars = get_object_vars($var); foreach ($vars as $property => $value) { if (is_string($value)) { $value = iconv('UTF-8', $PHORUM['DATA']['CHARSET'], $value); } elseif (is_array($value) || is_object($value)) { $value = phorum_api_charset_convert_from_utf8($value); } $var->{$property} = $value; } } elseif (is_string($var)) { $var = iconv('UTF-8', $PHORUM['DATA']['CHARSET'], $var); } return $var; }