mirror of
https://github.com/librenms/librenms.git
synced 2024-10-07 16:52:45 +00:00
Infer character encoding for ifAlias and sysLocation (#13248)
* Infer character encoding for ifAlias and sysLocation Tries to convert character encoding for non-UTF-8 encoded strings. This will only work for snmp strings that are type-hinted, not ones forced to ASCII with -Oa or similar Only works for your default charset or Windows-1251 or LATIN1. You can set your character encoding in .env with CHARSET Please save us all and just use UTF-8 * style fixes * less Yoda * ensure return type * fall back to passed string * don't convert strings with any unprintable characters (such as line return) * Update LibreNMS/Util/StringHelpers.php Co-authored-by: Jellyfrog <Jellyfrog@users.noreply.github.com> * Fix CP850 * fix space Co-authored-by: Jellyfrog <Jellyfrog@users.noreply.github.com>
This commit is contained in:
@@ -93,4 +93,36 @@ class StringHelpers
|
||||
{
|
||||
return ucwords(implode(' ', preg_split('/(?=[A-Z])/', $string)));
|
||||
}
|
||||
|
||||
/**
|
||||
* Sometimes devices store strings as non-unicode strings and return them directly.
|
||||
* NetSnmp parses those as UTF-8, try to convert the string if it contains non-printable ascii characters.
|
||||
*
|
||||
* @param string|null $string
|
||||
* @return string
|
||||
*/
|
||||
public static function inferEncoding(?string $string): ?string
|
||||
{
|
||||
if (empty($string) || preg_match('//u', $string) || ! function_exists('iconv')) {
|
||||
return $string;
|
||||
}
|
||||
|
||||
$charset = config('app.charset');
|
||||
|
||||
if (($converted = @iconv($charset, 'UTF-8', $string)) !== false) {
|
||||
return (string) $converted;
|
||||
}
|
||||
|
||||
if ($charset !== 'Windows-1252' && ($converted = @iconv('Windows-1252', 'UTF-8', $string)) !== false) {
|
||||
return (string) $converted;
|
||||
}
|
||||
|
||||
if ($charset !== 'CP850' && ($converted = @iconv('CP850', 'UTF-8', $string)) !== false) {
|
||||
return (string) $converted;
|
||||
}
|
||||
|
||||
\Log::debug('Failed to convert string: ' . $string);
|
||||
|
||||
return $string;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user