Skip to content

Commit

Permalink
nicht-UTF8 Inhalt zu UTF8 konvertieren
Browse files Browse the repository at this point in the history
Konvertiert custom.json und categories.json in UTF-8 wenn diese nicht im entsprechenden Format abgespeichert wurden.
  • Loading branch information
christian1180 committed Dec 13, 2024
1 parent 94b6807 commit 6d17b2c
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 37 deletions.
21 changes: 20 additions & 1 deletion diagramm_collect.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,24 @@
// */1 * * * * /usr/bin/php -f /pfad-zu-homehub/diagramm_collect.php >/dev/null 2>&1


function read_config($file) {
if (!is_file($file)) return false;
$config = file_get_contents($file);

// BOM erkennen und entfernen
if (strncmp($config, pack("CCC", 0xef, 0xbb, 0xbf), 3) === 0) $config = substr($config, 3);

// nicht-UTF8 Inhalt zu UTF8 konvertieren
if (extension_loaded('mbstring')) return mb_convert_encoding($config, 'UTF-8', mb_detect_encoding($config, 'UTF-8, ISO-8859-1', true));
else {
if (!preg_match('/(*UTF8)[äöüÄÖÜß]/', $config)) {
return html_entity_decode(htmlentities($config, ENT_QUOTES, 'ISO-8859-1'), ENT_QUOTES , 'UTF-8');
} else {
return $config;
}
}
}

require_once(__DIR__.'/interface.php');


Expand All @@ -17,7 +35,8 @@


// Lese aus custom.json die diagramm ise_id welche geloggt werden sollen
$data = file_get_contents(__DIR__.'/config/custom.json');
#$data = file_get_contents(__DIR__.'/config/custom.json');
$data = read_config(__DIR__.'/config/custom.json');
$json = json_decode($data, true);


Expand Down
87 changes: 51 additions & 36 deletions index.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,24 @@
//ini_set('display_errors', 'on');
// php8

function read_config($file) {
if (!is_file($file)) return false;
$config = file_get_contents($file);

// BOM erkennen und entfernen
if (strncmp($config, pack("CCC", 0xef, 0xbb, 0xbf), 3) === 0) $config = substr($config, 3);

// nicht-UTF8 Inhalt zu UTF8 konvertieren
if (extension_loaded('mbstring')) return mb_convert_encoding($config, 'UTF-8', mb_detect_encoding($config, 'UTF-8, ISO-8859-1', true));
else {
if (!preg_match('/(*UTF8)[äöüÄÖÜß]/', $config)) {
return html_entity_decode(htmlentities($config, ENT_QUOTES, 'ISO-8859-1'), ENT_QUOTES , 'UTF-8');
} else {
return $config;
}
}
}

$page = $_SERVER['PHP_SELF'];
$sec = "500";
//header("Refresh: $sec; url=".$page."?".$_SERVER['QUERY_STRING']);
Expand Down Expand Up @@ -53,19 +71,20 @@

// MENU # json_decode Config files
$categories = array();
$menu = array();
if(file_exists('config/categories.json'))
$menu = array();
#if(file_exists('config/categories.json'))
if ($str = read_config('config/categories.json'))
{
$str = file_get_contents('config/categories.json');
# $str = file_get_contents('config/categories.json');

// Prüfe UTF-8
if (extension_loaded("mbstring")) {
if (!mb_check_encoding($str, 'UTF-8'))
{
echo "Datei 'config/categories.json' entspricht nicht dem UTF-8 Format. Bitte als UTF-8 speichern.";
exit();
}
}
#if (extension_loaded("mbstring")) {
# if (!mb_check_encoding($str, 'UTF-8'))
# {
# echo "Datei 'config/categories.json' entspricht nicht dem UTF-8 Format. Bitte als UTF-8 speichern.";
# exit();
# }
#}

$json = json_decode($str, true);
$menu = $json['categories'];
Expand Down Expand Up @@ -122,19 +141,19 @@

// CUSTOM # Anpassungen des Benutzers
$custom = array();
if(file_exists('config/custom.json'))
#if(file_exists('config/custom.json'))
if($str = read_config('config/custom.json'))
{
$str = file_get_contents('config/custom.json');
#$str = file_get_contents('config/custom.json');

// Prüfe UTF-8
if (extension_loaded("mbstring")) {
if (!mb_check_encoding($str, 'UTF-8'))
{
echo "Datei 'config/config/custom.json' entspricht nicht dem UTF-8 Format. Bitte als UTF-8 speichern.";
exit();
}
}

#if (extension_loaded("mbstring")) {
# if (!mb_check_encoding($str, 'UTF-8'))
# {
# echo "Datei 'config/config/custom.json' entspricht nicht dem UTF-8 Format. Bitte als UTF-8 speichern.";
# exit();
# }
#}

$json = json_decode($str, true);
if(isset($json['custom']))
Expand All @@ -151,9 +170,10 @@

// MAPPING # Voreingestellte Icon für Komponenten
$mapping = array();
if(file_exists('config/mapping.json'))
#if(file_exists('config/mapping.json'))
if($str = read_config('config/mapping.json'))
{
$str = file_get_contents('config/mapping.json');
#$str = file_get_contents('config/mapping.json');
$json = json_decode($str, true);
if(isset($json['mapping']))
{
Expand All @@ -165,22 +185,21 @@
"Konfigurationsdatei 'config/mapping.json' nicht gefunden!";
exit();
}



// EXPORT # Lade Exportdatei der Homematic
$export = array();
if(file_exists('config/export.json'))
#if(file_exists('config/export.json'))
if($str = read_config('config/export.json'))
{
$str = file_get_contents('config/export.json');
#$str = file_get_contents('config/export.json');
$export = json_decode($str, true);
}
else
{
"Konfigurationsdatei 'config/export.json' nicht gefunden!";
exit();
}



// Komponenten einlesen
Expand Down Expand Up @@ -421,13 +440,14 @@
if($Page == $Servicemeldungen)
{

if(file_exists("dev/export.json")) {
#if(file_exists("dev/export.json")) {
if($str = read_config("dev/export.json")) {
$xml = simplexml_load_file('dev/systemNotification.php');
$str = file_get_contents('dev/export.json');
#$str = file_get_contents('dev/export.json');
} else {

$xml = simplexml_load_string(api_systemNotification($ccu));
$str = file_get_contents('config/export.json');
#$str = file_get_contents('config/export.json');
$str = read_config('config/export.json');
}
// Für Devices

Expand Down Expand Up @@ -624,11 +644,6 @@

<?php if(file_exists("custom/js/custom.js")) { echo "<script src='custom/js/custom.js?id=".rand(1,100)."'></script>"; } ?>
<?php if(isset($ioBrokerComponent)) { echo "<script src='js/ioBroker.js?id=".rand(1,100)."'></script>"; } ?>






</body>
</html>

0 comments on commit 6d17b2c

Please sign in to comment.