<?php
require_once("../../base/php/startSession.php");
require_once("../../base/php//classDatabase.php");
require_once("../../base/php/Functions.php");
require_once("../../base/php/WebAppLog.php");

global $database;

set_time_limit(3600);
ini_set('memory_limit', '512M');

$returnCode = "OK";
$returnTexte = "";

$database = new Database("iCTI");

$dataOperation = @$_POST["dataOperation"];
if ($dataOperation == "") {
    $dataOperation = @$_GET["dataOperation"];
}

$importTexte = @$_POST["importTexte"];
if ($importTexte == "") {
    $importTexte = @$_GET["importTexte"];
}

$dataFile = @$_POST["dataFile"];
if ($dataFile == "") {
    $dataFile = @$_GET["dataFile"];
}

$dataType = @$_POST["dataType"];
if ($dataType == "") {
    $dataType = @$_GET["dataType"];
}

$uploadType = @$_POST["uploadType"];
if ($uploadType == "") {
    $uploadType = @$_GET["uploadType"];
}

$data = @$_POST["data"];
if ($data == "") {
    $data = @$_GET["data"];
}

$import_id = @$_POST["import_id"];
if ($import_id == "") {
    $import_id = @$_GET["import_id"];
}
$import_id = $import_id + 0;


$tmp_path = rootDir() . "temp";
$tmp_dataFile = "$dataType" . "_data.tmp";
$tmp_pathFile = "$dataType" . "_path.tmp";


if ($dataOperation == "start") {
    @unlink("$tmp_path/zin_data.tmp");
    @unlink("$tmp_path/zin_path.tmp");
    @unlink("$tmp_path/zou_data.tmp");
    @unlink("$tmp_path/zou_path.tmp");
    @unlink("$tmp_path/zif_data.tmp");
    @unlink("$tmp_path/zif_path.tmp");
    @unlink("$tmp_path/rss_data.tmp");
    @unlink("$tmp_path/rss_path.tmp");
    @unlink("$tmp_path/rsf_data.tmp");
    @unlink("$tmp_path/rsf_path.tmp");
    @unlink("$tmp_path/fcp_data.tmp");
    @unlink("$tmp_path/fcp_path.tmp");
    @unlink("$tmp_path/med_data.tmp");
    @unlink("$tmp_path/med_path.tmp");
    @unlink("$tmp_path/rssmed_data.tmp");
    @unlink("$tmp_path/rssmed_path.tmp");
    @unlink("$tmp_path/rssnom_data.tmp");
    @unlink("$tmp_path/rssnom_path.tmp");
    @unlink("$tmp_path/actmed_data.tmp");
    @unlink("$tmp_path/actmed_path.tmp");

}

if ($dataOperation == "startfile") {
    $handle = fopen("$tmp_path/$tmp_pathFile", 'a');
    fwrite($handle, $dataFile);
    fclose($handle);
}
if ($dataOperation == "addfile") {
    $data = uncompress64($data);
    if ($uploadType == "zip") {
        $data = decodeURIComponent($data);
    }
    $handle = fopen("$tmp_path/$tmp_dataFile", 'a');
    fwrite($handle, $data);
    fclose($handle);
}
if ($dataOperation == "endfile") {

    $httpString = $httpString . "\n<FILE ";
    $httpString = $httpString . "filePath=\"$tmp_path/$tmp_dataFile\" ";
    $httpString = $httpString . "fileSize=\"" . filesize("$tmp_path/$tmp_dataFile") . "\" ";
    $httpString = $httpString . ">";
    $httpString = $httpString . "\n</FILE>";
}


if ($dataOperation == "validation") {

    // déjà récupéré
    $zinoid = 0;
    $zouoid = 0;
    $zifoid = 0;
    $rssoid = 0;
    $rsfoid = 0;
    $fcpoid = 0;
    $medoid = 0;
    $rssmedoid = 0;
    $rssnomoid = 0;
    $actmedoid = 0;

    $zin_crc = "";
    $zou_crc = "";
    $zif_crc = "";
    $rss_crc = "";
    $rsf_crc = "";
    $fcp_crc = "";
    $med_crc = "";
    $rssmed_crc = "";
    $rssnom_crc = "";
    $actmed_crc = "";


    // Fichier in.zip correct ?
    if ($returnCode == "OK") {
        if (file_exists("$tmp_path/zin_data.tmp")) {
            $error = checkInZip("$tmp_path/zin_data.tmp");
            if ($error != "") {
                $returnCode = "KO";
                $returnTexte = "Fichiers RSS IN.ZIP incorrect : $error";
            }
        }
    }

    // Fichier out.zip correct ?
    if ($returnCode == "OK") {
        if (file_exists("$tmp_path/zou_data.tmp")) {
            $error = checkOutZip("$tmp_path/zou_data.tmp");
            if ($error != "") {
                $returnCode = "KO";
                $returnTexte = "Fichiers RSS OUT.ZIP incorrect : $error";
            }
        }
    }

    // Fichier out.zip correct ?
    if ($returnCode == "OK") {
        if (file_exists("$tmp_path/zif_data.tmp")) {
            $error = checkRsfInZip("$tmp_path/zif_data.tmp");
            if ($error != "") {
                $returnCode = "KO";
                $returnTexte = "Fichiers RSF OUT.ZIP incorrect : $error";
            }
        }
    }


    // Déjà importé ?
    if ($returnCode == "OK") {
        if (file_exists("$tmp_path/zin_data.tmp")) {
            $zin_crc = @md5_file("$tmp_path/zin_data.tmp");
        }
        if (file_exists("$tmp_path/zou_data.tmp")) {
            $zou_crc = @md5_file("$tmp_path/zou_data.tmp");
        }
        if (file_exists("$tmp_path/zif_data.tmp")) {
            $zif_crc = @md5_file("$tmp_path/zif_data.tmp");
        }
        if (file_exists("$tmp_path/rss_data.tmp")) {
            $rss_crc = @md5_file("$tmp_path/rss_data.tmp");
        }
        if (file_exists("$tmp_path/rsf_data.tmp")) {
            $rsf_crc = @md5_file("$tmp_path/rsf_data.tmp");
        }
        if (file_exists("$tmp_path/fcp_data.tmp")) {
            $fcp_crc = @md5_file("$tmp_path/fcp_data.tmp");
        }

        if (file_exists("$tmp_path/med_data.tmp")) {
            $med_crc = @md5_file("$tmp_path/med_data.tmp");
        }

        if (file_exists("$tmp_path/rssmed_data.tmp")) {
            $rssmed_crc = @md5_file("$tmp_path/rssmed_data.tmp");
        }

        if (file_exists("$tmp_path/rssnom_data.tmp")) {
            $rssnom_crc = @md5_file("$tmp_path/rssnom_data.tmp");
        }

        if (file_exists("$tmp_path/actmed_data.tmp")) {
            $actmed_crc = @md5_file("$tmp_path/actmed_data.tmp");
        }

        $result = $database->exec("SELECT MAX(import_id) FROM pmsi.p_imports_data WHERE data_crc = '$zin_crc' ");
        if ($result != false) {
            $record = $database->nextRecordInto();
            if ($record != FALSE) {
                $zinoid = $record[0] + 0;
            }
        }

        $result = $database->exec("SELECT MAX(import_id) FROM pmsi.p_imports_data WHERE data_crc = '$zou_crc' ");
        if ($result != false) {
            $record = $database->nextRecordInto();
            if ($record != FALSE) {
                $zouoid = $record[0] + 0;
            }
        }

        $result = $database->exec("SELECT MAX(import_id) FROM pmsi.p_imports_data WHERE data_crc = '$zif_crc' ");
        if ($result != false) {
            $record = $database->nextRecordInto();
            if ($record != FALSE) {
                $zifoid = $record[0] + 0;
            }
        }

        $result = $database->exec("SELECT MAX(import_id) FROM pmsi.p_imports_data WHERE data_crc = '$rss_crc' ");
        if ($result != false) {
            $record = $database->nextRecordInto();
            if ($record != FALSE) {
                $rssoid = $record[0] + 0;
            }
        }

        $result = $database->exec("SELECT MAX(import_id) FROM pmsi.p_imports_data WHERE data_crc = '$rsf_crc' ");
        if ($result != false) {
            $record = $database->nextRecordInto();
            if ($record != FALSE) {
                $rsfoid = $record[0] + 0;
            }
        }

        $result = $database->exec("SELECT MAX(import_id) FROM pmsi.p_imports_data WHERE data_crc = '$fcp_crc' ");
        if ($result != false) {
            $record = $database->nextRecordInto();
            if ($record != FALSE) {
                $fcpoid = $record[0] + 0;
            }
        }


        $result = $database->exec("SELECT MAX(import_id) FROM pmsi.p_imports_data WHERE data_crc = '$med_crc' ");
        if ($result != false) {
            $record = $database->nextRecordInto();
            if ($record != FALSE) {
                $medoid = $record[0] + 0;
            }
        }

        $result = $database->exec("SELECT MAX(import_id) FROM pmsi.p_imports_data WHERE data_crc = '$rssmed_crc' ");
        if ($result != false) {
            $record = $database->nextRecordInto();
            if ($record != FALSE) {
                $rssmedoid = $record[0] + 0;
            }
        }

        $result = $database->exec("SELECT MAX(import_id) FROM pmsi.p_imports_data WHERE data_crc = '$rssnom_crc' ");
        if ($result != false) {
            $record = $database->nextRecordInto();
            if ($record != FALSE) {
                $rssnomoid = $record[0] + 0;
            }
        }

        $result = $database->exec("SELECT MAX(import_id) FROM pmsi.p_imports_data WHERE data_crc = '$actmed_crc' ");
        if ($result != false) {
            $record = $database->nextRecordInto();
            if ($record != FALSE) {
                $actmedoid = $record[0] + 0;
            }
        }

        if ($rss_crc != "") {
            if ($rssoid != 0 && $rsfoid == $rssoid && $fcpoid == $rssoid && $medoid == $rssoid && $rssmedoid == $rssoid && $rssnomoid == $rssoid && $actmedoid == $rssoid) {
                $returnCode = "KO";
                $returnTexte = "Fichiers RSS/RSF déjà importés avec le n° $rssoid";
            }
        }

        if ($zin_crc != "") {
            if ($zinoid != 0 && $zouoid == $zinoid && $zifoid == $zinoid && $medoid == $zinoid && $rssmedoid == $zinoid && $rssnomoid == $zinoid && $actmedoid == $zinoid) {
                $returnCode = "KO";
                $returnTexte = "Fichiers RSS/RSF déjà importés avec le n° $zinoid";
            }
        }
    }

    if ($returnCode == "OK") {
        $texte = toSQL($importTexte);


        $sqlcmd = "INSERT INTO pmsi.p_imports (texte, etat, date_import, etat_en_cours) ";
        $sqlcmd = $sqlcmd . "VALUES('$texte', '', CURRENT_DATE, 'I')";


        $result = $database->exec($sqlcmd);
        if ($result === FALSE) {

        } else {
            $result = $database->exec("SELECT max(oid) FROM pmsi.p_imports WHERE etat = '' AND etat_en_cours = 'I' ");
            if ($result != false) {
                $record = $database->nextRecordInto();
                if ($record != FALSE) {
                    $oid = $record[0] + 0;

                    $import_id = $oid;

                    addFile($oid, "ZIN", "$tmp_path/zin_data.tmp", "$tmp_path/zin_path.tmp", $zin_crc);
                    addFile($oid, "ZOU", "$tmp_path/zou_data.tmp", "$tmp_path/zou_path.tmp", $zou_crc);
                    addFile($oid, "ZIF", "$tmp_path/zif_data.tmp", "$tmp_path/zif_path.tmp", $zif_crc);
                    addFile($oid, "RSS", "$tmp_path/rss_data.tmp", "$tmp_path/rss_path.tmp", $rss_crc);
                    addFile($oid, "RSF", "$tmp_path/rsf_data.tmp", "$tmp_path/rsf_path.tmp", $rsf_crc);
                    addFile($oid, "FCP", "$tmp_path/fcp_data.tmp", "$tmp_path/fcp_path.tmp", $fcp_crc);
                    addFile($oid, "MED", "$tmp_path/med_data.tmp", "$tmp_path/med_path.tmp", $med_crc);
                    addFile($oid, "RSSMED", "$tmp_path/rssmed_data.tmp", "$tmp_path/rssmed_path.tmp", $rssmed_crc);
                    addFile($oid, "RSSNOM", "$tmp_path/rssnom_data.tmp", "$tmp_path/rssnom_path.tmp", $rssnom_crc);
                    addFile($oid, "ACTMED", "$tmp_path/actmed_data.tmp", "$tmp_path/actmed_path.tmp", $actmed_crc);

                    $sqlcmd = "UPDATE pmsi.p_imports SET etat = 'I', etat_en_cours = '', traitement_a_faire = 'C' WHERE oid = $oid ";
                    $result = $database->exec($sqlcmd);


                    @unlink("$tmp_path/zin_data.tmp");
                    @unlink("$tmp_path/zin_path.tmp");
                    @unlink("$tmp_path/zou_data.tmp");
                    @unlink("$tmp_path/zou_path.tmp");
                    @unlink("$tmp_path/zif_data.tmp");
                    @unlink("$tmp_path/zif_path.tmp");
                    @unlink("$tmp_path/rss_data.tmp");
                    @unlink("$tmp_path/rss_path.tmp");
                    @unlink("$tmp_path/rsf_data.tmp");
                    @unlink("$tmp_path/rsf_path.tmp");
                    @unlink("$tmp_path/fcp_data.tmp");
                    @unlink("$tmp_path/fcp_path.tmp");
                    @unlink("$tmp_path/rsa_data.tmp");
                    @unlink("$tmp_path/rsa_path.tmp");
                    @unlink("$tmp_path/med_data.tmp");
                    @unlink("$tmp_path/med_path.tmp");
                    @unlink("$tmp_path/rssmed_data.tmp");
                    @unlink("$tmp_path/rssmed_path.tmp");
                    @unlink("$tmp_path/rssnom_data.tmp");
                    @unlink("$tmp_path/rssnom_path.tmp");
                    @unlink("$tmp_path/actmed_data.tmp");
                    @unlink("$tmp_path/actmed_path.tmp");

                }

            }
        }

        if ($import_id == 0) {
            $returnCode = "KO";
            $returnTexte = "La validation a échoué";
        }

    }

}


if ($dataOperation == "controle") {

    $submitCmd = "..\import_rss_rsf\iCTI_import_rss_rsf.exe";
    $return = exec($submitCmd);

    $result = $database->exec("SELECT nb_erreurs, nb_avertissements FROM pmsi.p_imports WHERE oid = $import_id ");
    if ($result != false) {
        $record = $database->nextRecordInto();
        if ($record != FALSE) {
            $nb_erreurs = $record[0] + 0;
            $nb_avertissements = $record[1] + 0;

            if ($nb_erreurs > 0) {
                $returnCode = "KO";
                $returnTexte = "$nb_erreurs erreur(s) grave(s)";
                if ($nb_avertissements > 0) {
                    $returnTexte = $returnTexte . ", $nb_avertissements avertissement(s)";
                }
            } else {
                if ($nb_avertissements > 0) {
                    $returnCode = "ATT";
                    $returnTexte = "$nb_avertissements avertissement(s)";
                }
            }
        }
    }

}

if ($dataOperation == "end") {

}


$httpString = "";
$httpString = $httpString . "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>";
$httpString = $httpString . "\n<HTTP_SERVICE import_id=\"$import_id\" returnCode=\"$returnCode\" returnTexte=\"$returnTexte\" >";
$httpString = $httpString . "\n</HTTP_SERVICE>";


$httpString = gzcompress($httpString, 9);
$httpString = "_c_" . base64_encode($httpString);

echo "<CTICONTENT>$httpString</CTICONTENT>";


function addFile($import_id, $data_type, $data_path, $path_path, $data_crc)
{

    global $database;

    if (file_exists($data_path)) {
        $file_size = filesize($data_path);
        $file_path = toSQL(file_get_contents($path_path));

        $start = 0;
        $data_num = 0;

        while ($start <= $file_size) {
            $filebuff = file_get_contents($data_path, FALSE, NULL, $start, 10000000);

            $filebuff = pg_escape_bytea(compress($filebuff));

            $sqlcmd = "INSERT INTO pmsi.p_imports_data (import_id, data_type, date_creation, data_num, data, file_path, file_size, data_crc) ";
            $sqlcmd = $sqlcmd . "VALUES($import_id, '$data_type', '2099-12-31', $data_num, '$filebuff', '$file_path', $file_size, '$data_crc' )";
            $result = $database->exec($sqlcmd);

            $start = $start + 10000000;
            $data_num++;
        }
    }

}


function checkInZip($file)
{

    global $finess;

    $error = "";
    $qrss = false;

    if (file_exists($file)) {
        $zip = @zip_open(realpath($file));
        if ($zip) {
            while ($zip_entry = zip_read($zip)) {

                if (zip_entry_open($zip, $zip_entry)) {

                    $filename = trim(zip_entry_name($zip_entry));
                    $filesuffix = substr($filename, strlen($filename) - 7, 7);

                    if ($filesuffix == "rss.txt") {
                        $qrss = true;
                        if (strpos($filename, $finess) === false) {
                            $error = "Un fichier RSS trouvé mais pour un FINESS différent";
                        }
                    }
                    zip_entry_close($zip_entry);
                }
            }
            zip_close($zip);
        }
    }

    if ($qrss == false) {
        $error = "Pas de fichier RSS trouvé";
    }

    return $error;

}

function checkOutZip($file)
{

    global $finess;

    $error = "";
    $qrsa = false;

    if (file_exists($file)) {
        $zip = @zip_open(realpath($file));
        if ($zip) {
            while ($zip_entry = zip_read($zip)) {

                if (zip_entry_open($zip, $zip_entry)) {

                    $filename = trim(zip_entry_name($zip_entry));
                    $filesuffix = substr($filename, strlen($filename) - 4, 4);
                    if ($filesuffix == ".rsa") {
                        $qrsa = true;
                        if (strpos($filename, $finess) === false) {
                            $error = "Un fichier RSA trouvé mais pour un FINESS différent";
                        }
                    }
                    zip_entry_close($zip_entry);
                }
            }
            zip_close($zip);
        }
    }

    if ($qrsa == false) {
        $error = "Pas de fichier RSA trouvé";
    }

    return $error;

}

function checkRsfInZip($file)
{

    global $finess;

    $error = "";
    $qrss = false;

    if (file_exists($file)) {
        $zip = @zip_open(realpath($file));
        if ($zip) {
            while ($zip_entry = zip_read($zip)) {

                if (zip_entry_open($zip, $zip_entry)) {

                    $filename = trim(zip_entry_name($zip_entry));
                    $filesuffix = substr($filename, strlen($filename) - 7, 7);

                    if ($filesuffix == "rsf.txt") {
                        $qrss = true;
                        if (strpos($filename, $finess) === false) {
                            $error = "Un fichier RSF trouvé mais pour un FINESS différent";
                        }
                    }
                    zip_entry_close($zip_entry);
                }
            }
            zip_close($zip);
        }
    }

    if ($qrss == false) {
        $error = "Pas de fichier RSF trouvé";
    }

    return $error;

}


function decodeURIComponent($string)
{
    $result = "";

    $hex00 = chr(00) . chr(00) . chr(00);


    //webAppLog("UPLOAD", "DEBUG", $string);

    if (strpos($string, "%E2%8") !== false) {

        $string = str_replace("%E2%82%AC", "%80$hex00$hex00", $string);
        $string = str_replace("%E2%80%9A", "%82$hex00$hex00", $string);
        $string = str_replace("%E2%80%9E", "%84$hex00$hex00", $string);
        $string = str_replace("%E2%80%A6", "%85$hex00$hex00", $string);
        $string = str_replace("%E2%80%A0", "%86$hex00$hex00", $string);
        $string = str_replace("%E2%80%A1", "%87$hex00$hex00", $string);
        $string = str_replace("%E2%80%B0", "%89$hex00$hex00", $string);
        $string = str_replace("%E2%80%B9", "%8B$hex00$hex00", $string);
        $string = str_replace("%E2%80%98", "%91$hex00$hex00", $string);
        $string = str_replace("%E2%80%99", "%92$hex00$hex00", $string);
        $string = str_replace("%E2%80%9C", "%93$hex00$hex00", $string);
        $string = str_replace("%E2%80%9D", "%94$hex00$hex00", $string);
        $string = str_replace("%E2%80%A2", "%95$hex00$hex00", $string);
        $string = str_replace("%E2%80%93", "%96$hex00$hex00", $string);
        $string = str_replace("%E2%80%94", "%97$hex00$hex00", $string);
        $string = str_replace("%E2%84%A2", "%99$hex00$hex00", $string);
        $string = str_replace("%E2%80%BA", "%9B$hex00$hex00", $string);
    }

    if (strpos($string, "%C2") !== false) {
        $string = str_replace("%C2%81", "%81$hex00", $string);
        $string = str_replace("%C2%8D", "%8D$hex00", $string);
        $string = str_replace("%C2%8F", "%8F$hex00", $string);
        $string = str_replace("%C2%90", "%90$hex00", $string);
        $string = str_replace("%C2%9D", "%9D$hex00", $string);
        if (strpos($string, "%C2%A") !== false) {
            $string = str_replace("%C2%A0", "%A0$hex00", $string);
            $string = str_replace("%C2%A1", "%A1$hex00", $string);
            $string = str_replace("%C2%A2", "%A2$hex00", $string);
            $string = str_replace("%C2%A3", "%A3$hex00", $string);
            $string = str_replace("%C2%A4", "%A4$hex00", $string);
            $string = str_replace("%C2%A5", "%A5$hex00", $string);
            $string = str_replace("%C2%A6", "%A6$hex00", $string);
            $string = str_replace("%C2%A7", "%A7$hex00", $string);
            $string = str_replace("%C2%A8", "%A8$hex00", $string);
            $string = str_replace("%C2%A9", "%A9$hex00", $string);
            $string = str_replace("%C2%AA", "%AA$hex00", $string);
            $string = str_replace("%C2%AB", "%AB$hex00", $string);
            $string = str_replace("%C2%AC", "%AC$hex00", $string);
            $string = str_replace("%C2%AD", "%AD$hex00", $string);
            $string = str_replace("%C2%AE", "%AE$hex00", $string);
            $string = str_replace("%C2%AF", "%AF$hex00", $string);
        }
        if (strpos($string, "%C2%B") !== false) {
            $string = str_replace("%C2%B0", "%B0$hex00", $string);
            $string = str_replace("%C2%B1", "%B1$hex00", $string);
            $string = str_replace("%C2%B2", "%B2$hex00", $string);
            $string = str_replace("%C2%B3", "%B3$hex00", $string);
            $string = str_replace("%C2%B4", "%B4$hex00", $string);
            $string = str_replace("%C2%B5", "%B5$hex00", $string);
            $string = str_replace("%C2%B6", "%B6$hex00", $string);
            $string = str_replace("%C2%B7", "%B7$hex00", $string);
            $string = str_replace("%C2%B8", "%B8$hex00", $string);
            $string = str_replace("%C2%B9", "%B9$hex00", $string);
            $string = str_replace("%C2%BA", "%BA$hex00", $string);
            $string = str_replace("%C2%BB", "%BB$hex00", $string);
            $string = str_replace("%C2%BC", "%BC$hex00", $string);
            $string = str_replace("%C2%BD", "%BD$hex00", $string);
            $string = str_replace("%C2%BE", "%BE$hex00", $string);
            $string = str_replace("%C2%BF", "%BF$hex00", $string);
        }
    }

    if (strpos($string, "%C3") !== false) {
        if (strpos($string, "%C3%8") !== false) {
            $string = str_replace("%C3%80", "%C0$hex00", $string);
            $string = str_replace("%C3%81", "%C1$hex00", $string);
            $string = str_replace("%C3%82", "%C2$hex00", $string);
            $string = str_replace("%C3%83", "%C3$hex00", $string);
            $string = str_replace("%C3%84", "%C4$hex00", $string);
            $string = str_replace("%C3%85", "%C5$hex00", $string);
            $string = str_replace("%C3%86", "%C6$hex00", $string);
            $string = str_replace("%C3%87", "%C7$hex00", $string);
            $string = str_replace("%C3%88", "%C8$hex00", $string);
            $string = str_replace("%C3%89", "%C9$hex00", $string);
            $string = str_replace("%C3%8A", "%CA$hex00", $string);
            $string = str_replace("%C3%8B", "%CB$hex00", $string);
            $string = str_replace("%C3%8C", "%CC$hex00", $string);
            $string = str_replace("%C3%8D", "%CD$hex00", $string);
            $string = str_replace("%C3%8E", "%CE$hex00", $string);
            $string = str_replace("%C3%8F", "%CF$hex00", $string);
        }
        if (strpos($string, "%C3%9") !== false) {
            $string = str_replace("%C3%90", "%D0$hex00", $string);
            $string = str_replace("%C3%91", "%D1$hex00", $string);
            $string = str_replace("%C3%92", "%D2$hex00", $string);
            $string = str_replace("%C3%93", "%D3$hex00", $string);
            $string = str_replace("%C3%94", "%D4$hex00", $string);
            $string = str_replace("%C3%95", "%D5$hex00", $string);
            $string = str_replace("%C3%96", "%D6$hex00", $string);
            $string = str_replace("%C3%97", "%D7$hex00", $string);
            $string = str_replace("%C3%98", "%D8$hex00", $string);
            $string = str_replace("%C3%99", "%D9$hex00", $string);
            $string = str_replace("%C3%9A", "%DA$hex00", $string);
            $string = str_replace("%C3%9B", "%DB$hex00", $string);
            $string = str_replace("%C3%9C", "%DC$hex00", $string);
            $string = str_replace("%C3%9D", "%DD$hex00", $string);
            $string = str_replace("%C3%9E", "%DE$hex00", $string);
            $string = str_replace("%C3%9F", "%DF$hex00", $string);
        }
        if (strpos($string, "%C3%A") !== false) {
            $string = str_replace("%C3%A0", "%E0$hex00", $string);
            $string = str_replace("%C3%A1", "%E1$hex00", $string);
            $string = str_replace("%C3%A2", "%E2$hex00", $string);
            $string = str_replace("%C3%A3", "%E3$hex00", $string);
            $string = str_replace("%C3%A4", "%E4$hex00", $string);
            $string = str_replace("%C3%A5", "%E5$hex00", $string);
            $string = str_replace("%C3%A6", "%E6$hex00", $string);
            $string = str_replace("%C3%A7", "%E7$hex00", $string);
            $string = str_replace("%C3%A8", "%E8$hex00", $string);
            $string = str_replace("%C3%A9", "%E9$hex00", $string);
            $string = str_replace("%C3%AA", "%EA$hex00", $string);
            $string = str_replace("%C3%AB", "%EB$hex00", $string);
            $string = str_replace("%C3%AC", "%EC$hex00", $string);
            $string = str_replace("%C3%AD", "%ED$hex00", $string);
            $string = str_replace("%C3%AE", "%EE$hex00", $string);
            $string = str_replace("%C3%AF", "%EF$hex00", $string);
        }
        if (strpos($string, "%C3%B") !== false) {
            $string = str_replace("%C3%B0", "%F0$hex00", $string);
            $string = str_replace("%C3%B1", "%F1$hex00", $string);
            $string = str_replace("%C3%B2", "%F2$hex00", $string);
            $string = str_replace("%C3%B3", "%F3$hex00", $string);
            $string = str_replace("%C3%B4", "%F4$hex00", $string);
            $string = str_replace("%C3%B5", "%F5$hex00", $string);
            $string = str_replace("%C3%B6", "%F6$hex00", $string);
            $string = str_replace("%C3%B7", "%F7$hex00", $string);
            $string = str_replace("%C3%B8", "%F8$hex00", $string);
            $string = str_replace("%C3%B9", "%F9$hex00", $string);
            $string = str_replace("%C3%BA", "%FA$hex00", $string);
            $string = str_replace("%C3%BB", "%FB$hex00", $string);
            $string = str_replace("%C3%BC", "%FC$hex00", $string);
            $string = str_replace("%C3%BD", "%FD$hex00", $string);
            $string = str_replace("%C3%BE", "%FE$hex00", $string);
            $string = str_replace("%C3%BF", "%FF$hex00", $string);
        }

    }

    if (strpos($string, "%C5") !== false) {
        $string = str_replace("%C5%A0", "%8A$hex00", $string);
        $string = str_replace("%C5%92", "%8C$hex00", $string);
        $string = str_replace("%C5%A1", "%9A$hex00", $string);
        $string = str_replace("%C5%93", "%9C$hex00", $string);
        $string = str_replace("%C5%BE", "%9E$hex00", $string);
        $string = str_replace("%C5%B8", "%9F$hex00", $string);
        $string = str_replace("%C5%BD", "%8E$hex00", $string);
    }

    $string = str_replace("%C6%92", "%83$hex00", $string);
    $string = str_replace("%CB%86", "%88$hex00", $string);
    $string = str_replace("%CB%9C", "%98$hex00", $string);


    $string = str_replace($hex00, "", $string);

    $string = str_replace("%20", "+", $string);
    $string = str_replace("!", "%21", $string);
    $string = str_replace("'", "%27", $string);
    $string = str_replace("(", "%28", $string);
    $string = str_replace(")", "%29", $string);
    $string = str_replace("*", "%2A", $string);
    $string = str_replace("~", "%7E", $string);

    //webAppLog("UPLOAD", "DEBUG", $string);

    $result = urldecode($string);

    return $result;
}


?>