<?php
session_start();
include_once "check_auth.php";
include_once "db_fns.php";
include_once "mail_fns.php";

function microtime_float() {
    list($usec, $sec) = explode(" ", microtime());
    return ((float)$usec + (float)$sec);
}

function xss($stringa) {
    return htmlspecialchars($stringa, ENT_QUOTES, 'UTF-8');
}

if (isset($_GET['rep'])) {
    include('getreport.php');
    $xml = simplexml_load_string($xmlOutput);
    $key = 'ABQIAAAATixuPrJs49wC5TrYfUU37RTNRc7Mi-Opu4R0U57c_LNEOnOFFxR_cE_I0abipwHXt2E73qqnW61IuA';
    $logo = '<a  href="/"><img src="http://www.meteonetwork.it//sites/all/themes/meteonetwork/images/tt.png" /></a>';
    $titolo = "Storm Report Pretemp";
    $css = '<link href="asset/css/style.css?v1.2" rel="stylesheet" type="text/css" />
<link type="text/css" href="asset/css/humanity/jquery-ui-1.8rc3.custom.css" rel="stylesheet" />
<link href="asset/css/jquery.lightbox-0.5.css" rel="stylesheet" type="text/css" />
';
    $script = '<script type="text/javascript" src="asset/js/jquery-1.4.2.min.js"></script>
<script type="text/javascript" src="asset/js/jquery-ui-1.8rc3.custom.js"></script>
<script type="text/javascript" src="asset/js/jquery.form.js"></script>
<script type="text/javascript" src="https://maps.google.com/maps/api/js?key=AIzaSyDV78zID-bPbE-53iD91PF2rgReYRRNFxU&sensor=false&region=IT&v=3"></script>
<script type="text/javascript" src="asset/js/jquery.MultiFile.js?t"></script>
<script type="text/javascript" src="asset/js/jquery.lightbox-0.5.js"></script>
<script type="text/javascript" src="asset/js/report.js?v2.1"></script>';
    include('header.php');
//<script type="text/javascript" src="https://maps.google.com/maps/api/js?key=AIzaSyC7qyFE6NsvJVRO0u3ctVQ15nwkig0ytr8&sensor=false&region=IT&v=3"></script>

    ?>

    <div id="content">
        <div id="menubar">
            <?php include "output_menu.php"; ?>
        </div>

        <div id="tablecontainer">
            <div id="reportleft">
                <h3><a>Report n° <?php echo xss($xml->report_id); ?> - Evento <?php echo xss($xml->eventname); ?></a></h3>
                <div id="reportinfo">
                    <ul>
                        <li><strong>Data e Ora : </strong><?php echo substr(xss($xml->datetime), 0, -3) ?></li>
                        <li><strong>Precisione Data/Ora : </strong><?php
                            switch ($xml->dtrange) {
                                case '1M' :
                                    echo '+/- 1 Minuto';
                                    break;
                                case '5M' :
                                    echo '+/- 5 Minuti';
                                    break;
                                case '15M' :
                                    echo '+/- 15 Minuti';
                                    break;
                                case '30M' :
                                    echo '+/- 30 Minuti';
                                    break;
                                case '1H' :
                                    echo '+/- 1 Ora';
                                    break;
                                case '3H' :
                                    echo '+/- 3 Ore';
                                    break;
                                case '6H' :
                                    echo '+/- 6 Ore';
                                    break;
                                case 'HDY' :
                                    echo '+/- Metà Giornata';
                                    break;
                                case '1D' :
                                    echo '+/- 1 Giorno';
                                    break;
                                case 'HMN' :
                                    echo '+/- Metà Mese';
                                    break;
                                case 'HYE' :
                                    echo '+/- Metà Anno';
                                    break;
                                case 'ND' :
                                    echo 'Data/Ora Incerta';
                                    break;
                                default :
                                    '';
                            }
                            ?></li>
                        <li><strong>Regione : </strong><?php echo xss($xml->regione) ?></li>
                        <li><strong>Comune : </strong><?php echo xss($xml->place) ?></li>
                        <li><strong>Località : </strong><?php echo xss($xml->locality) ?></li>
                        <li><strong>Latitudine : </strong><?php echo xss($xml->latitude) ?></li>
                        <li><strong>Longitudine : </strong><?php echo xss($xml->longitude) ?></li>
                        <li><strong>Segnalato da : </strong><?php echo xss($xml->username); ?></li>
                        <li><strong>Validità : </strong><?php
                            if ($xml->valid == 'Y') {
                                echo "Approvato";
                            }
                            if ($xml->valid == 'N') {
                                echo "Non valido";
                            }
                            if ($xml->valid == 'W') {
                                echo "In attesa";
                            }
                            ?></li>
                        <?php
                        if ((isset($_SESSION['tt_master']) || isset($_SESSION['tt_admin'])) && isset($xml->validator)) {
                            echo "<li><strong>Validato da : </strong> $xml->validator</li>";
                            echo "<li><strong>Email : </strong><a href='mailto:" . $xml->validatormail . "'>" . $xml->validatormail . "</a></li>";
                        }
                        if ((isset($_SESSION['tt_master']) || isset($_SESSION['tt_admin']))) {

                            if ($xml->eswd_valid == 'Y') {
                                $eswd = 'Si';
                            }
                            if ($xml->eswd_valid == 'N') {
                                $eswd = 'No';
                            }
                            echo "<li><strong>Inviato a ESWD ? : </strong>" . $eswd . "</li>";
                        }
                        ?>
                    </ul>
                    <script type="text/javascript">var latitude =<?php echo xss($xml->latitude) ?>;
                        var longitude = <?php echo xss($xml->longitude) ?>; </script>
                    <div id="reportmap"></div>
                    <div class="clear"></div>
                </div>
            </div>
            <div id="reportright">
                <h3><a>Scheda Evento</a></h3>
                <div style="margin:0px; padding:0px;">
                    <ul>
                        <li>
                            <strong>Descrizione : </strong><?php echo($xml->notes == '' ? 'n.d' : htmlspecialchars($xml->notes)) ?>
                        </li>
                        <?php
                        if ($xml->eventname == 'Tornado') {
                            echo "<li><strong>Condensato : </strong>" . ($xml->condensato == 'Y' ? 'Si' : 'No') . "</li> \n";
                            echo "<li><strong>Debriscloud : </strong>" . ($xml->debriscloud == 'Y' ? 'Si' : 'No') . "</li> \n";
                            echo "<li><strong>Scala (International) Fujita : </strong>" . ($xml->scalafujita == '' ? 'n.d' : $xml->scalafujita) . "</li>  \n";
                        }
                        if ($xml->eventname == 'Funnel') {
                            echo "<li><strong>Durata : </strong>" . ($xml->durata == '' ? 'n.d.' : $xml->durata . " Min") . "</li>  \n";
                            echo "<li><strong>Rotazione : </strong>" . ($xml->rotazione == 'Y' ? 'Si' : 'No') . "</li> \n";
                        }
                        if ($xml->eventname == 'Tromba Marina') {

                            echo "<li><strong>Durata : </strong>" . ($xml->durata == '' ? 'n.d.' : $xml->durata . " Min") . "</li> \n";
                            if ($xml->landfall == 'Y') {
                                $xml->landfall = 'Si';
                            }
                            if ($xml->landfall == 'N') {
                                $xml->landfall = 'No';
                            }
                            if ($xml->landfall == 'X') {
                                $xml->landfall = 'Non Noto';
                            }
                            echo "<li><strong>Landfall : </strong>" . $xml->landfall . "</li> \n";
                        }
                        if ($xml->eventname == 'Grandine') {
                            echo "<li><strong>Durata : </strong>" . ($xml->durata == '' ? 'n.d.' : $xml->durata . " Min") . "</li> \n";
                            echo "<li><strong>Accumulo : </strong>" . ($xml->accumulo == 'Y' ? 'Si' : 'No') . " </li> \n";
                            echo "<li><strong>Quantità : </strong>" . ($xml->amount == '' ? 'n.d.' : $xml->amount . " cm") . "</li> \n";
                            echo "<li><strong>Accompagnata da pioggia : </strong>" . ($xml->con_pioggia == 'Y' ? 'Si' : 'No') . "</li> \n";
                            echo "<li><strong>Diametro chicchi : </strong>" . ($xml->size_number == '' ? 'n.d.' : $xml->size_number . " cm") . "</li> \n";
                            switch ($xml->size_type) {
                                case 'B' :
                                    $text = 'Biglia';
                                    break;
                                case 'PT' :
                                    $text = 'Palla da Tennis';
                                    break;
                                case 'PG' :
                                    $text = 'Palla da Golf';
                                    break;
                                default:
                                    $text = 'n.d.';
                            }
                            echo "<li><strong>Misura simile : </strong>" . $text . " </li> \n";
                            switch ($xml->forma) {
                                case 'S' :
                                    $text = 'Sferica';
                                    break;
                                case 'LP' :
                                    $text = 'Lobi e punte';
                                    break;
                                case 'I' :
                                    $text = 'Irregolare';
                                    break;
                                case 'D' :
                                    $text = 'Discoidale';
                                    break;
                                default:
                                    $text = 'n.d.';
                            }
                            echo "<li><strong>Forma : </strong>" . $text . " </li> \n";
                            echo "<li><strong>Consistenza : </strong>" . ($xml->consistenza == 'D' ? 'Dura' : 'Molle') . " </li> \n";
                        }
                        if ($xml->eventname == 'Vento forte') {
                            echo "<li><strong>Durata : </strong>" . ($xml->durata == '' ? 'n.d.' : $xml->durata . " Min") . "</li> \n";
                            echo "<li><strong>Downburst : </strong>" . ($xml->downburst == 'Y' ? 'Si' : 'No') . " </li> \n";
                            switch ($xml->speed) {
                                case 1 :
                                    $text = '50-80 km/h';
                                    break;
                                case 2 :
                                    $text = '80-120 km/h';
                                    break;
                                case 3 :
                                    $text = 'Oltre 120 km/h';
                                    break;
                                default:
                                    $text = 'n.d.';
                            }
                            echo "<li><strong>Velocità : </strong>" . $text . "</li> \n";
                        }
                        if ($xml->eventname == 'Precipitazioni') {
                            echo "<li><strong>Precipitazione convettiva? : </strong>" . ($xml->prec_conv == 'Y' ? 'Si' : 'No') . "</li> \n";
                            echo "<li><strong>Accumulo : </strong>" . ($xml->accumulo == '' ? 'n.d.' : $xml->accumulo . " mm") . "</li> \n";
                            echo "<li><strong>Durata : </strong>" . ($xml->durata == '' ? 'n.d.' : $xml->durata . " Min") . "</li> \n";
                            echo "<li><strong>Allagamenti : </strong>" . ($xml->allagamenti == 'Y' ? 'Si' : 'No') . "</li> \n";
//	echo "<li><strong>Smottamenti : </strong>".($xml->smottamenti == 'Y' ? 'Si' : 'No')."</li> \n" ;
                            echo "<li><strong>Frane : </strong>" . ($xml->frane == 'Y' ? 'Si' : 'No') . "</li> \n";
                            echo "<li><strong>RainRate Max : </strong>" . ($xml->rainratemax == '' ? 'n.d.' : $xml->rainratemax . " mm/h") . " verificatosi alle ore " . ($xml->rainratemax_time == '' ? 'n.d.' : substr($xml->rainratemax_time, 0, -3)) . "</li> \n";
                        }
                        if ($xml->eventname == 'Attività elettrica') {
                            echo "<li><strong>Massimo fulmini  : </strong>" . ($xml->fulmini_max == '' ? 'n.d.' : $xml->fulmini_max . " /min") . "</li> \n";
                            echo "<li><strong>Tempo minimo tra scariche : </strong>" . ($xml->tempo_min == '' ? 'n.d.' : $xml->tempo_min . " secondi") . " </li> \n";
                            switch ($xml->intensity) {
                                case 'E' :
                                    $text = 'Elevata';
                                    break;
                                case 'D' :
                                    $text = 'Debole';
                                    break;
                                case 'M' :
                                    $text = 'Moderata';
                                    break;
                                default:
                                    $text = 'n.d.';
                            }
                            echo "<li><strong>Intensità : </strong>" . $text . "</li> \n";
                        }
                        //////////////////
                        echo "<li><strong>Feriti : </strong>";
                        if ($xml->feriti != '')
                            echo xss($xml->feriti) . "</li> \n";
                        else echo " Nessuno </li> \n";
                        echo "<li><strong>Morti : </strong>";
                        if ($xml->morti != '')
                            echo xss($xml->morti) . "</li> \n";
                        else echo " Nessuno </li> \n";


                        ?>

                    </ul>
                </div>
            </div>
            <div id="reportbottom">
                <h3><a>Risorse</a></h3>
                <div>
                    <div id="reportbottom1">
                        <label><strong>Links</strong></label>
                        <ul>
                            <?php
                            foreach ($xml->links->link as $link) {
                                $link->name = trim($link->name);
                                $search = 'https://';
                                $pos = strpos($link->name, $search);
                                $add = 'http://';
                                if ($pos !== false) {
                                    $linkname = str_replace('https://', '', $link->name);
                                    $add = 'https://';

                                } else {
                                    $linkname = str_replace('http://', '', $link->name);
                                }

                                echo "<li><a target='_blank' href='" . $add . htmlspecialchars($linkname) . "'><img src=\"asset/images/link.png\" /></a></li>";
                            }
                            ?>

                        </ul>
                    </div>
                    <div id="reportbottom2">
                        <label><strong>Documenti Allegati</strong></label>
                        <ul>
                            <?php
                            foreach ($xml->files->documento as $documento) {
                                if (substr($documento->name, -3) == 'pdf') {
                                    echo "<li><a href='" . $documento->name . "'><img src=\"asset/images/pdf.png\" /></a></li>";
                                }
                                if (substr($documento->name, -3) == 'doc') {
                                    echo "<li><a href='" . $documento->name . "'><img src=\"asset/images/doc.png\" /></a></li>";
                                }
                            }

                            ?>

                        </ul>
                    </div>
                    <div id="reportbottom3">
                        <label><strong>Fotografie</strong></label>
                        <ul>
                            <?php
                            foreach ($xml->files->foto as $foto) {
                                echo "<li><a href='" . $foto->fotobig . "'><img width='200px' height='200px' src='" . $foto->fotobig . "'/></a></li>";
                            }
                            ?>
                        </ul>
                    </div>
                </div>
            </div>
        </div>

        <div style="clear:both">.</div>
    </div>

    <div id="dialogs"></div>

    <script> var id = <?php echo xss($xml->report_id); ?> ; </script>

    </div>
    </div>
    <?php


    include('footer.php');
// fine if su richiesta report via get
}

if (isset($_SESSION['tt_admin']) || isset($_SESSION['tt_master']) || isset($_SESSION['tt_normal'])) {


    if (isset($_GET['action']) && $_GET['action'] == 'new') {
        $conn = @db_connect();

        //$_POST = array_map('pg_escape_string', $_POST);

        $query = "INSERT INTO tt_report (user_id, datetime, place_id";

        if ($_POST['locality'] != '') {
            $query .= ",locality";
        }

        $query .= ",event_id, latitude,longitude,is_valid";

        if ($_POST['notes'] != '') {
            $query .= ", notes";
        }

        $query .= ", dtrange ";

        $query .= ") VALUES (" . $_SESSION['tt_user_id'] . ",'" . pg_escape_string($_POST['datetime']) . "'," . pg_escape_string($_POST['place']) . "";

        $locality = pg_escape_string($_POST['locality']);
        if ($_POST['locality'] != '') {
            $query .= ",'$locality'";
        }

//		$query .= ",$_POST[event], $_POST[latitude],$_POST[longitude],'W'";

        if (isset($_SESSION['tt_master']) || isset($_SESSION['tt_admin'])) {
            $query .= "," . pg_escape_string($_POST['event']) . ", " . pg_escape_string($_POST['latitude']) . "," . pg_escape_string($_POST['longitude']) . ",'Y'";
        } else {
            $query .= "," . pg_escape_string($_POST['event']) . ", " . pg_escape_string($_POST['latitude']) . "," . pg_escape_string($_POST['longitude']) . ",'W'";
        }
        $notes = pg_escape_string($_POST['notes']);
        if ($_POST['notes'] != '') {
            $query .= ",'$notes'";
        }

        if ($_POST['dtrange'] != '') {
            $query .= ",'" . pg_escape_string($_POST['dtrange']) . "'";
        }

        $query .= ")";

        //print_r($_POST) ;
        //print_r($query) ;


        @pg_query($conn, "set datestyle to 'Postgres, DMY'");

        @pg_query($conn, "BEGIN WORK");

        $result = @pg_query($conn, $query);
        if (!$result) {
            @pg_query($conn, "ROLLBACK");
            echo 'errore';
            return false;
        }

        $query_event = "Select table_name from tt_event where event_id = " . (int)$_POST['event'];

        $result = @pg_query($conn, $query_event);
        if (!$result) {
            @pg_query($conn, "ROLLBACK");
            return false;
        }

        $row = @pg_fetch_row($result);
        $tablename = $row[0];

        $query_id = "SELECT currval('tt_report_report_id_seq')";
        $result = @pg_query($conn, $query_id);
        if (!$result) {
            @pg_query($conn, "ROLLBACK");
            echo 'errore';
            return false;
        }

        $row = @pg_fetch_row($result);
        $report_id = $row[0];


        //////////// Impacts
        if ($_POST['morti'] != '' || $_POST['feriti'] != '') {
            $query_imp = "INSERT INTO tt_impact (report_id , morti , feriti ) VALUES (";
            $query_imp .= $report_id;
            if ($_POST['morti'] != '') {
                $query_imp .= " , " . pg_escape_string($_POST['morti']);
            } else $query_imp .= ", NULL ";
            if ($_POST['feriti'] != '') {
                $query_imp .= " , " . pg_escape_string($_POST['feriti']);
            } else $query_imp .= ",NULL ";
            $query_imp .= ")";

            $result = @pg_query($conn, $query_imp);
            if (!$result) {
                @pg_query($conn, "ROLLBACK");
                echo 'errore';
                return false;
            }
        }
        //////////////

        $query_rep = "INSERT INTO " . $tablename . " (report_id";

        switch ($_POST['event']) {
            case '1':

                $query_rep .= ",condensato,debriscloud";

                if ($_POST['scalafujita'] != '') {
                    $query_rep .= ",scalafujita";
                }

                if ($_POST['durata'] != '') {
                    $query_rep .= ",durata";
                }


                $query_rep .= " ) VALUES ($report_id,'" . pg_escape_string($_POST['condensato']) . "','" . pg_escape_string($_POST['debriscloud']) . "'";

                if ($_POST['scalafujita'] != '') {
                    $query_rep .= ",'" . pg_escape_string($_POST['scalafujita']) . "'";
                }

                if ($_POST['durata'] != '') {
                    $query_rep .= "," . pg_escape_string($_POST['durata']) . "";
                }


                $query_rep .= " )";

                break;
            case '2':
                if ($_POST['durata'] != '') {
                    $query_rep .= ",durata";
                }

                $query_rep .= ",rotazione ) VALUES ($report_id ";

                if ($_POST['durata'] != '') {
                    $query_rep .= "," . $_POST['durata'];
                }

                $query_rep .= ",'" . pg_escape_string($_POST['rotazione']) . "')";
                //echo $query_rep ;

                break;
            case '3':
                $query_rep .= ",landfall";
                if ($_POST['durata'] != '') {
                    $query_rep .= ",durata";
                }
                $query_rep .= ") VALUES ($report_id";
                $query_rep .= ",'" . $_POST['landfall'] . "'";
                if ($_POST['durata'] != '') {
                    $query_rep .= "," . pg_escape_string($_POST['durata']);
                }
                $query_rep .= " )";

                break;
            case '4':

                if ($_POST['durata'] != '') {
                    $query_rep .= ",durata";
                }
                $query_rep .= ",accumulo";
                if ($_POST['amount'] != '') {
                    $query_rep .= ",amount";
                }
                $query_rep .= ",con_pioggia";
                if ($_POST['size_number'] != '') {
                    $query_rep .= ",size_number";
                }
                if ($_POST['size_type'] != '') {
                    $query_rep .= ",size_type";
                }
                if ($_POST['forma'] != '') {
                    $query_rep .= ",forma";
                }
                if ($_POST['consistenza'] != '') {
                    $query_rep .= ",consistenza";
                }

                $query_rep .= ") VALUES ($report_id ";

                if ($_POST['durata'] != '') {
                    $query_rep .= "," . pg_escape_string($_POST['durata']);
                }
                $query_rep .= ",'" . $_POST['accumulo_gra'] . "'";
                if ($_POST['amount'] != '') {
                    $query_rep .= "," . pg_escape_string($_POST['amount']);
                }
                $query_rep .= ",'" . $_POST['con_pioggia'] . "'";
                if ($_POST['size_number'] != '') {
                    $query_rep .= "," . pg_escape_string($_POST['size_number']);
                }
                if ($_POST['size_type'] != '') {
                    $query_rep .= ",'" . pg_escape_string($_POST['size_type']) . "'";
                }
                if ($_POST['forma'] != '') {
                    $query_rep .= ",'" . pg_escape_string($_POST['forma']) . "'";
                }
                if ($_POST['consistenza'] != '') {
                    $query_rep .= ",'" . pg_escape_string($_POST['consistenza']) . "'";
                }
                $query_rep .= " )";

                break;
            case '5':
                if ($_POST['durata'] != '') {
                    $query_rep .= ",durata";
                }
                $query_rep .= ",downburst";
                if ($_POST['speed'] != '') {
                    $query_rep .= ",speed";
                }
                $query_rep .= ") VALUES ($report_id";
                if ($_POST['durata'] != '') {
                    $query_rep .= "," . pg_escape_string($_POST['durata']);
                }
                $query_rep .= ",'" . $_POST['downburst'] . "'";
                if ($_POST['speed'] != '') {
                    $query_rep .= "," . pg_escape_string($_POST['speed']);
                }
                $query_rep .= " )";

                break;
            case '6':
                if ($_POST['accumulo'] != '') {
                    $query_rep .= ",accumulo";
                }
                if ($_POST['durata'] != '') {
                    $query_rep .= ",durata";
                }
                if ($_POST['rainratemax'] != '') {
                    $query_rep .= ",rainratemax";
                }
                if ($_POST['rainratemax_time'] != '') {
                    $query_rep .= ",rainratemax_time";
                }
                $query_rep .= ",allagamenti,smottamenti,frane,prec_conv";
                $query_rep .= ") VALUES ($report_id";
                if ($_POST['accumulo'] != '') {
                    $query_rep .= "," . pg_escape_string($_POST['accumulo']);
                }
                if ($_POST['durata'] != '') {
                    $query_rep .= "," . pg_escape_string($_POST['durata']);
                }
                if ($_POST['rainratemax'] != '') {
                    $query_rep .= "," . pg_escape_string($_POST['rainratemax']);
                }
                if ($_POST['rainratemax_time'] != '') {
                    $query_rep .= ",'" . pg_escape_string($_POST['rainratemax_time']) . "'";
                }
                $query_rep .= ",'" . pg_escape_string($_POST['allagamenti']) . "','" . pg_escape_string($_POST['smottamenti']) . "','" . pg_escape_string($_POST['frane']) . "','" . pg_escape_string($_POST['prec_conv']) . "'";
                $query_rep .= " )";

                break;
            case '7':
                if ($_POST['fulmini_max'] != '') {
                    $query_rep .= ",fulmini_max";
                }
                if ($_POST['tempo_min'] != '') {
                    $query_rep .= ",tempo_min";
                }
                if ($_POST['intensity'] != '') {
                    $query_rep .= ",intensity";
                }
                $query_rep .= ") VALUES ($report_id";
                if ($_POST['fulmini_max'] != '') {
                    $query_rep .= "," . pg_escape_string($_POST['fulmini_max']);
                }
                if ($_POST['tempo_min'] != '') {
                    $query_rep .= "," . pg_escape_string($_POST['tempo_min']);
                }
                if ($_POST['intensity'] != '') {
                    $query_rep .= ",'" . pg_escape_string($_POST['intensity']) . "'";
                }
                $query_rep .= " )";

                break;


        }

        $result = pg_query($conn, $query_rep);
        if (!$result) {
            @pg_query($conn, "ROLLBACK");
            echo 'errore';
            return false;
        }

////// Links
        $links = $_POST['links'] ?? [];
        for ($i = 0; $i < count($links); $i++) {
            if ($links[$i] != '') {
                $query_links = "INSERT INTO tt_link (report_id, web_link)
                    VALUES ($report_id, '" . pg_escape_string($links[$i]) . "')";
                $result = @pg_query($conn, $query_links);
                if (!$result) {
                    @pg_query($conn, "ROLLBACK");
                    echo 'errore';
                    return false;
                }
            }
        }

//////// upload files

        $fotoNames = $_FILES['foto']['name'] ?? [];
        for ($i = 0; $i < count($fotoNames); $i++) {
            if ($_FILES['foto']['size'][$i] > 0) {
                if ($_FILES['foto']['size'][$i] < 2048000) {
                    $microtime = microtime_float();
                    $name = 'asset/photo/' . $report_id . '_' . $microtime . '.jpg';
                    $smallname = 'asset/photo/' . $report_id . '_small_' . $microtime . '.jpg';
                    move_uploaded_file($_FILES['foto']['tmp_name'][$i], $name);
                    //$_sys = "gm convert -size 200x200 ".$name." -resize 200x200 +profile \"*\" ".$smallname;
                    //exec($_sys);
                    $query_file1 = "INSERT INTO tt_file ( report_id,type,filename,small_filename) VALUES ($report_id,'J','$name','$smallname')";
                    $result = pg_query($conn, $query_file1);
                    if (!$result) {
                        @pg_query($conn, "ROLLBACK");
                        echo 'errore';
                        return false;
                    }
                }
            }

        }


        $extraNames = $_FILES['extra']['name'] ?? [];
        for ($i = 0; $i < count($extraNames); $i++) {
            if ($_FILES['extra']['size'][$i] > 0) {
                if ($_FILES['extra']['size'][$i] < 1024000) {
                    $microtime = microtime_float();
                    $filename = $_FILES['extra']['name'][$i];
                    $ext = strtolower(pathinfo($filename, PATHINFO_EXTENSION));
                    if ($ext === 'doc') {
                        $type = 'D';
                    } elseif ($ext === 'pdf') {
                        $type = 'P';
                    } else {
                        continue;
                    }
                    $safename = 'asset/files/' . $report_id . '_' . $microtime . '.' . $ext;
                    move_uploaded_file($_FILES['extra']['tmp_name'][$i], $safename);
                    $query_file2 = "INSERT INTO tt_file ( report_id,type,filename) VALUES ($report_id,'" . pg_escape_string($type) . "','" . pg_escape_string($safename) . "')";
                    $result = pg_query($conn, $query_file2);
                    if (!$result) {
                        @pg_query($conn, "ROLLBACK");
                        echo 'errore';
                        return false;
                    }
                }
            }
        }


        if (isset($_SESSION['tt_normal'])) {
            $query = "SELECT email, firstname, lastname from meteouser, tt_report where report_id = $report_id and tt_report.user_id = meteouser.user_id";
            $result = @pg_query($conn, $query);
            if (!$result) {
                @pg_query($conn, "ROLLBACK");
                @pg_close($conn);
                echo 'errore';
                return false;
            }
            $row = @pg_fetch_row($result);
            report_inserito($row[0], $row[1], $row[2], $report_id);


            $query = "SELECT email, firstname, lastname, member_of from meteouser where ( is_admin='Y' ) AND member_of = 'T'";
            $result = @pg_query($conn, $query);
            if (!$result) {
                @pg_query($conn, "ROLLBACK");
                @pg_close($conn);
                echo 'errore';
                return false;
            }

            while ($row = @pg_fetch_row($result)) {
                $member_of = explode(",", $row[3]);
                $control = in_array("T", $member_of);
                if ($control == true) {
                    report_new($row[0], $row[1], $row[2], $report_id);
                }
            }
        }
        @pg_query($conn, "COMMIT");
        @pg_close($conn);
        echo 'success';
    }

    if (isset($_GET['action']) && $_GET['action'] == 'edit') {
        $conn = @db_connect();
        $query = "SELECT is_valid , i.report_id  from tt_report r LEFT JOIN tt_impact i ON ( i.report_id = r.report_id ) WHERE r.report_id = " . $_POST['report_id'];
        $result = pg_query($conn, $query);

        if (!$result) {
            echo 'errore';
            return false;
        }
        $row = @pg_fetch_row($result);
        $valido = $row[0];
        $query_imp = "";
        if (!is_null($row[1])) {
            // aggiorno impact ;
            if ($_POST['morti'] != '' || $_POST['feriti'] != '') {
                $query_imp = "UPDATE tt_impact SET morti = ";
                if ($_POST['morti'] != '') $query_imp .= pg_escape_string($_POST['morti']) . ",";
                else  $query_imp .= "NULL ,";
                $query_imp .= " feriti = ";
                if ($_POST['feriti'] != '') $query_imp .= pg_escape_string($_POST['feriti']);
                else  $query_imp .= "NULL";
                $query_imp .= " WHERE report_id = " . pg_escape_string($_POST['report_id']);
            } else
                $query_imp = "DELETE FROM tt_impact WHERE report_id = " . pg_escape_string($_POST['report_id']);


        } else {
            // inserisco impact ;

            if ($_POST['morti'] != '' || $_POST['feriti'] != '') {
                $query_imp = "INSERT INTO tt_impact (report_id , morti , feriti ) VALUES (";
                $query_imp .= pg_escape_string($_POST['report_id']);
                if ($_POST['morti'] != '') {
                    $query_imp .= " , " . pg_escape_string($_POST['morti']);
                } else $query_imp .= ", NULL ";
                if ($_POST['feriti'] != '') {
                    $query_imp .= " , " . pg_escape_string($_POST['feriti']);
                } else $query_imp .= ",NULL ";
                $query_imp .= ")";
            }


        }


        $reportchange = '';
        $query = "UPDATE tt_report SET datetime = '" . pg_escape_string($_POST['datetime']) . "' , place_id = " . (int)$_POST['place'];

        $locality = pg_escape_string($_POST['locality']);
        if ($_POST['locality'] != '') {
            $query .= ", locality = '$locality'";
        } else {
            $query .= ",locality = null ";
        }

        $query .= ", latitude = " . pg_escape_string($_POST['latitude']) . " , longitude = " . pg_escape_string($_POST['longitude']) . " ";

        if (isset($_SESSION['tt_normal']) && ($valido == 'Y' || $valido == 'N')) {
            $reportchange = 'Y';
            $query .= " , is_valid = 'W' , validator = null ";
        }
        $notes = pg_escape_string($_POST['notes']);
        if ($_POST['notes'] != '') {
            $query .= ", notes = '$notes' ";
        } else {
            $query .= ",notes = null ";
        }

        if ($_POST['dtrange'] != '') {
            $query .= ", dtrange = '" . pg_escape_string($_POST['dtrange']) . "' ";
        }


        if (isset($_SESSION['tt_normal'])) {
            $query .= "WHERE user_id = " . $_SESSION['tt_user_id'] . " AND report_id = " . pg_escape_string($_POST['report_id']) . " ";
        } else if (isset($_SESSION['tt_admin']) || isset($_SESSION['tt_master'])) {
            $query .= "WHERE report_id = " . pg_escape_string($_POST['report_id']) . " ";
        }

        //echo $query ;

        @pg_query($conn, "set datestyle to 'Postgres, DMY'");
        @pg_query($conn, "BEGIN WORK");

        $result = pg_query($conn, $query);
        if (!$result) {
            @pg_query($conn, "ROLLBACK");
            echo 'errore';
            return false;
        }

        if ($query_imp) {

            $result = pg_query($conn, $query_imp);
            if (!$result) {
                @pg_query($conn, "ROLLBACK");
                echo 'errore';
                return false;
            }
        }

        $allowed_tables = ['tt_trombamarina' , 'tt_tornado', 'tt_ventoforte', 'tt_rain', 'tt_funnel', 'tt_grandine', 'tt_fulmini'];
        if (!in_array($_POST['tabella'], $allowed_tables, true)) {
            @pg_query($conn, "ROLLBACK");
            echo 'errore';
            return false;
        }
        $query_rep = "UPDATE " . $_POST['tabella'] . " SET ";

        switch ($_POST['event']) {
            case '1':
                if ($_POST['durata'] != '') {
                    $query_rep .= "durata = " . pg_escape_string($_POST['durata']) . " ,";
                } else {
                    $query_rep .= "durata = null , ";
                }

                $query_rep .= "condensato = '" . pg_escape_string($_POST['condensato']) . "' ,debriscloud = '" . pg_escape_string($_POST['debriscloud']) . "'";

                if ($_POST['scalafujita'] != '') {
                    $query_rep .= ",scalafujita = '" . pg_escape_string($_POST['scalafujita']) . "'";
                } else {
                    $query_rep .= ",scalafujita = null ";
                }

                $query_rep .= "WHERE report_id = " . pg_escape_string($_POST['report_id']) . " ";

                break;
            case '2':

                $query_rep .= "rotazione = '" . pg_escape_string($_POST['rotazione']) . "'";


                if ($_POST['durata'] != '') {
                    $query_rep .= ",durata = " . pg_escape_string($_POST['durata']) . " ";
                } else {
                    $query_rep .= ",durata = null ";
                }

                $query_rep .= "WHERE report_id = " . pg_escape_string($_POST['report_id']) . " ";

                break;
            case '3':
                $query_rep .= "landfall = '" . pg_escape_string($_POST['landfall']) . "' ";
                if ($_POST['durata'] != '') {
                    $query_rep .= ",durata = " . pg_escape_string($_POST['durata']) . " ";
                } else {
                    $query_rep .= ", durata = null ";
                }


                $query_rep .= "WHERE report_id = " . pg_escape_string($_POST['report_id']) . " ";

                break;
            case '4':

                $query_rep .= " accumulo = '" . pg_escape_string($_POST['accumulo_gra']) . "'";

                if ($_POST['durata'] != '') {
                    $query_rep .= ",durata = " . pg_escape_string($_POST['durata']) . " ";
                } else {
                    $query_rep .= ",durata = null ";
                }

                if ($_POST['amount'] != '') {
                    $query_rep .= ",amount = " . pg_escape_string($_POST['amount']) . " ";
                } else {
                    $query_rep .= ",amount = null ";
                }

                $query_rep .= ",con_pioggia = '" . pg_escape_string($_POST['con_pioggia']) . "' ";

                if ($_POST['size_number'] != '') {
                    $query_rep .= ",size_number = " . pg_escape_string($_POST['size_number']) . " ";
                } else {
                    $query_rep .= ",size_number = null ";
                }

                if ($_POST['size_type'] != '') {
                    $query_rep .= ",size_type = '" . pg_escape_string($_POST['size_type']) . "'";
                } else {
                    $query_rep .= ",size_type = null ";
                }

                if ($_POST['forma'] != '') {
                    $query_rep .= ",forma = '" . pg_escape_string($_POST['forma']) . "' ";
                } else {
                    $query_rep .= ",forma = null ";
                }

                if ($_POST['consistenza'] != '') {
                    $query_rep .= ",consistenza = '" . pg_escape_string($_POST['consistenza']) . "'";
                } else {
                    $query_rep .= ",consistenza = null ";
                }

                $query_rep .= "WHERE report_id = " . pg_escape_string($_POST['report_id']) . " ";

                break;
            case '5':
                if ($_POST['durata'] != '') {
                    $query_rep .= "durata = " . pg_escape_string($_POST['durata']) . " ";
                } else {
                    $query_rep .= "durata = null ";
                }

                $query_rep .= ",downburst = '" . pg_escape_string($_POST['downburst']) . "'";

                if ($_POST['speed'] != '') {
                    $query_rep .= ",speed = " . pg_escape_string($_POST['speed']) . " ";
                } else {
                    $query_rep .= ",speed = null ";
                }

                $query_rep .= "WHERE report_id = " . pg_escape_string($_POST['report_id']) . " ";


                break;
            case '6':
                if ($_POST['accumulo'] != '') {
                    $query_rep .= "accumulo = " . pg_escape_string($_POST['accumulo']) . " ";
                } else {
                    $query_rep .= "accumulo = null ";
                }

                if ($_POST['durata'] != '') {
                    $query_rep .= ",durata = " . pg_escape_string($_POST['durata']) . " ";
                } else {
                    $query_rep .= ",durata = null ";
                }

                if ($_POST['rainratemax'] != '') {
                    $query_rep .= ",rainratemax = " . pg_escape_string($_POST['rainratemax']) . " ";
                } else {
                    $query_rep .= ",rainratemax = null ";
                }

                if ($_POST['rainratemax_time'] != '') {
                    $query_rep .= ",rainratemax_time = '" . pg_escape_string($_POST['rainratemax_time']) . "'";
                } else {
                    $query_rep .= ",rainratemax_time = null ";
                }

                $query_rep .= ",allagamenti = '" . pg_escape_string($_POST['allagamenti']) . "' ,smottamenti = '" . pg_escape_string($_POST['smottamenti']) . "' ,frane = '" . pg_escape_string($_POST['frane']) . "' ,prec_conv = '" . pg_escape_string($_POST['prec_conv']) . "'";

                $query_rep .= "WHERE report_id = " . pg_escape_string($_POST['report_id']) . " ";


                break;
            case '7':
                if ($_POST['fulmini_max'] != '') {
                    $query_rep .= "fulmini_max = " . pg_escape_string($_POST['fulmini_max']) . " ";
                } else {
                    $query_rep .= "fulmini_max = null ";
                }

                if ($_POST['tempo_min'] != '') {
                    $query_rep .= ",tempo_min = " . pg_escape_string($_POST['tempo_min']) . " ";
                } else {
                    $query_rep .= ",tempo_min = null ";
                }

                if ($_POST['intensity'] != '') {
                    $query_rep .= ",intensity = '" . pg_escape_string($_POST['intensity']) . "'";
                }

                $query_rep .= "WHERE report_id = " . pg_escape_string($_POST['report_id']) . " ";

                break;


        }
        //	echo $query_rep ;


        $result = pg_query($conn, $query_rep);
        if (!$result) {
            @pg_query($conn, "ROLLBACK");
            echo 'errore';
            return false;
        }

        $query = "delete from tt_link
        where report_id = " . pg_escape_string($_POST['report_id']) . " ";

        $result = pg_query($conn, $query);
        if (!$result) {
            @pg_query($conn, "ROLLBACK");
            @pg_close($conn);
            echo 'errore';
            return false;
        }

        ////// Links

        $report_id = $_POST['report_id'];
        $links = $_POST['links'] ?? [];
        for ($i = 0; $i < count($links); $i++) {
            if ($links[$i] != '') {
                $query_links = "INSERT INTO tt_link (report_id, web_link)
                    VALUES ($report_id, '" . pg_escape_string($links[$i]) . "')";

                $result = pg_query($conn, $query_links);
                if (!$result) {
                    @pg_query($conn, "ROLLBACK");
                    echo 'errore';
                    return false;

                }
            }
        }


        //////// upload files

        $delete = $_POST['deletefoto'] ?? [];
        for ($i = 0; $i < count($delete); $i++) {

            $query = "select filename,small_filename from tt_file where file_id = " . pg_escape_string($delete[$i]) . " ";
            $result = pg_query($conn, $query);
            if (!$result) {
                @pg_query($conn, "ROLLBACK");
                @pg_close($conn);
                echo 'errore';
                return false;
            }
            $rowfile = @pg_fetch_row($result);
            $filename = $rowfile[0];

            if (is_file($filename)) {
                unlink($filename);
            }
            $smallfilename = $rowfile[1];

            if (is_file($smallfilename)) {
                unlink($smallfilename);
            }


            $query = "delete from tt_file where file_id = " . pg_escape_string($delete[$i]) . " ";

            $result = pg_query($conn, $query);
            if (!$result) {
                @pg_query($conn, "ROLLBACK");
                @pg_close($conn);
                echo 'errore';
                return false;
            }
//echo $query;
        }


        $fotoNames = $_FILES['foto']['name'] ?? [];
        for ($i = 0; $i < count($fotoNames); $i++) {
            if ($_FILES['foto']['size'][$i] > 0) {
                if ($_FILES['foto']['size'][$i] < 2048000) {
                    $microtime = microtime_float();
                    $name = 'asset/photo/' . $report_id . '_' . $microtime . '.jpg';
                    //$smallname = 'asset/photo/' . $report_id . '_small_' . $microtime . '.jpg';
                    move_uploaded_file($_FILES['foto']['tmp_name'][$i], $name);
                    //$_sys = "gm convert -size 200x200 " . $name . " -resize 200x200 +profile \"*\" " . $smallname;
                    //exec($_sys);
                    //$query_file1 = "INSERT INTO tt_file ( report_id,type,filename,small_filename) VALUES ($report_id,'J','$name','$smallname')";
                    $query_file1 = "INSERT INTO tt_file ( report_id,type,filename) VALUES ($report_id,'J','$name')";
                    $result = pg_query($conn, $query_file1);
                    if (!$result) {
                        @pg_query($conn, "ROLLBACK");
                        echo 'errore';
                        return false;
                    }
                }
                //echo $query_file1 ;
            }
        }


        $delete = $_POST['deletedoc'] ?? [];

        for ($i = 0; $i < count($delete); $i++) {
            //--------------> aggiungere il delete del file <-----------------------////////
            $query = "select filename from tt_file where file_id = " . pg_escape_string($delete[$i]) . " ";
            $result = pg_query($conn, $query);
            if (!$result) {
                @pg_query($conn, "ROLLBACK");
                @pg_close($conn);
                echo 'errore';
                return false;
            }
            $rowfile = @pg_fetch_row($result);
            $filename = $rowfile[0];

            if (is_file($filename)) {
                unlink($filename);
            }

            $query = "delete from tt_file where file_id = " . pg_escape_string($delete[$i]) . " ";


            $result = @pg_query($conn, $query);
            if (!$result) {
                @pg_query($conn, "ROLLBACK");
                @pg_close($conn);
                echo 'errore';
                return false;
            }
//			echo $query;
        }


        $extraNames = $_FILES['extra']['name'] ?? [];
        for ($i = 0; $i < count($extraNames); $i++) {
            if ($_FILES['extra']['size'][$i] > 0) {
                if ($_FILES['extra']['size'][$i] < 1024000) {
                    $microtime = microtime_float();
                    $filename = $_FILES['extra']['name'][$i];
                    $ext = strtolower(pathinfo($filename, PATHINFO_EXTENSION));
                    if ($ext === 'doc') {
                        $type = 'D';
                    } elseif ($ext === 'pdf') {
                        $type = 'P';
                    } else {
                        continue;
                    }
                    $safename = 'asset/files/' . $report_id . '_' . $microtime . '.' . $ext;
                    move_uploaded_file($_FILES['extra']['tmp_name'][$i], $safename);
                    $query_file2 = "INSERT INTO tt_file ( report_id,type,filename) VALUES ($report_id,'" . pg_escape_string($type) . "','" . pg_escape_string($safename) . "')";
                    $result = pg_query($conn, $query_file2);
                    if (!$result) {
                        @pg_query($conn, "ROLLBACK");
                        echo 'errore';
                        return false;
                    }
                }
            }
        }

        if ($reportchange == 'Y') {
            $query = "SELECT email, firstname, lastname, member_of from meteouser where ( is_admin='Y' or is_admin='M' ) AND member_of = 'T'";
            $result = @pg_query($conn, $query);
            if (!$result) {
                @pg_query($conn, "ROLLBACK");
                @pg_close($conn);
                echo 'errore';
                return false;
            }

            while ($row = @pg_fetch_row($result)) {
                $member_of = explode(",", $row[3]);
                $control = in_array("T", $member_of);
                if ($control == true) {
                    report_change($row[0], $row[1], $row[2], $report_id);
                }
            }
        }

        @pg_query($conn, "COMMIT");
        @pg_close($conn);
        echo 'success';


    }
    if (isset($_POST['action']) && $_POST['action'] == 'accept' && (isset($_SESSION['tt_admin']) || isset($_SESSION['tt_master']))) {
        $query = "UPDATE tt_report SET is_valid = 'Y' , validator = " . (int)$_SESSION['tt_user_id'] . " WHERE report_id = " . (int)$_POST['report_id'] . " ";
        $conn = @db_connect();
        @pg_query($conn, "set datestyle to 'Postgres, DMY'");
        @pg_query($conn, "BEGIN WORK");
        $result = pg_query($conn, $query);
        if (!$result) {
            @pg_query($conn, "ROLLBACK");
            echo 'errore';
            return false;
        }
        $message = pg_escape_string($_POST['message']);

        $query = "SELECT email, firstname, lastname from meteouser, tt_report where report_id = " . $_POST['report_id'] . " and tt_report.user_id = meteouser.user_id";
        $result = @pg_query($conn, $query);
        if (!$result) {
            @pg_query($conn, "ROLLBACK");
            @pg_close($conn);
            echo 'errore';
            return false;
        }
        while ($row = @pg_fetch_row($result)) {
            $email_address_to = $row[0];
            $firstname = $row[1];
            $lastname = $row[2];

            $query = "SELECT email, firstname, lastname from meteouser where user_id = " . $_SESSION['tt_user_id'];
            $result = @pg_query($conn, $query);

            $row = @pg_fetch_row($result);

            report_approvato($email_address_to, $firstname, $lastname, $_POST['report_id'], $row[1], $row[2], $row[0], $message);
        }

        @pg_query($conn, "COMMIT");
        @pg_close($conn);
        echo 'success';

    }
    if (isset($_POST['action']) && $_POST['action'] == 'emailuser' && (isset($_SESSION['tt_admin']) || isset($_SESSION['tt_master']))) {
        $message = pg_escape_string($_POST['message']);
        $conn = @db_connect();
        $query = "SELECT email, firstname, lastname from meteouser, tt_report where report_id = " . pg_escape_string($_POST['report_id']) . " and tt_report.user_id = meteouser.user_id";
        $result = @pg_query($conn, $query);
        if (!$result) {
            @pg_query($conn, "ROLLBACK");
            @pg_close($conn);
            echo 'errore';
            return false;
        }
        while ($row = @pg_fetch_row($result)) {
            $email_address_to = $row[0];
            $firstname = $row[1];
            $lastname = $row[2];

            $query = "SELECT email, firstname, lastname from meteouser where user_id = " . $_SESSION['tt_user_id'];
            $result = @pg_query($conn, $query);

            $row = @pg_fetch_row($result);

            emailuser($email_address_to, $firstname, $lastname, $_POST['report_id'], $row[1], $row[2], $row[0], $message);
        }

        @pg_close($conn);
        echo 'success';

    }
    if (isset($_POST['action']) && $_POST['action'] == 'reject' && (isset($_SESSION['tt_admin']) || isset($_SESSION['tt_master']))) {
        $query = "UPDATE tt_report SET is_valid = 'N' , validator = " . (int)$_SESSION['tt_user_id'] . " WHERE report_id = " . (int)$_POST['report_id'];
        $conn = @db_connect();
        @pg_query($conn, "set datestyle to 'Postgres, DMY'");
        @pg_query($conn, "BEGIN WORK");
        $result = pg_query($conn, $query);
        if (!$result) {
            @pg_query($conn, "ROLLBACK");
            echo 'errore';
            return false;
        }
        $message = pg_escape_string($_POST['message']);
        $query = "SELECT email, firstname, lastname from meteouser, tt_report where report_id = " . pg_escape_string($_POST['report_id']) . " and tt_report.user_id = meteouser.user_id";
        $result = @pg_query($conn, $query);
        if (!$result) {
            @pg_query($conn, "ROLLBACK");
            @pg_close($conn);
            echo 'errore';
            return false;
        }
        while ($row = @pg_fetch_row($result)) {
            $email_address_to = $row[0];
            $firstname = $row[1];
            $lastname = $row[2];

            $query = "SELECT email, firstname, lastname from meteouser where user_id = " . $_SESSION['tt_user_id'];
            $result = @pg_query($conn, $query);

            $row = @pg_fetch_row($result);

            report_rifiutato($email_address_to, $firstname, $lastname, $_POST['report_id'], $row[1], $row[2], $row[0], $message);
        }


        @pg_query($conn, "COMMIT");
        @pg_close($conn);
        echo 'success';

    }
    if (isset($_POST['action']) && $_POST['action'] == 'delete' && isset($_SESSION['tt_master'])) {
        $query = "DELETE from tt_report WHERE report_id = " . pg_escape_string($_POST['report_id']) . " ";
        $conn = @db_connect();
        @pg_query($conn, "BEGIN WORK");
        $result = pg_query($conn, $query);
        if (!$result) {
            @pg_query($conn, "ROLLBACK");
            echo 'errore';
            return false;
        }
        @pg_query($conn, "COMMIT");
        @pg_close($conn);
        echo 'success';

    }
    if (isset($_POST['action']) && $_POST['action'] == 'valideswd' && (isset($_SESSION['tt_admin']) || isset($_SESSION['tt_master']))) {
        $query = "UPDATE tt_report SET eswd_valid = 'Y' , validator = " . (int)$_SESSION['tt_user_id'] . " WHERE report_id = " . (int)$_POST['report_id'] . " ";
        $conn = @db_connect();
        @pg_query($conn, "set datestyle to 'Postgres, DMY'");
        @pg_query($conn, "BEGIN WORK");
        $result = pg_query($conn, $query);
        if (!$result) {
            @pg_query($conn, "ROLLBACK");
            echo 'errore';
            return false;
        }
        @pg_query($conn, "COMMIT");
        @pg_close($conn);
        echo 'success';

    }
    /*if ( $_POST['action'] == 'invalideswd' && ( isset($_SESSION['tt_admin']) || isset($_SESSION['tt_master'])) ) {
        $query = "UPDATE tt_report SET eswd_valid = 'N' , validator = ".$_SESSION['tt_user_id']." WHERE report_id = $_POST[report_id] ";
        $conn = @db_connect();
        @pg_query ($conn, "set datestyle to 'Postgres, DMY'");
        @pg_query ($conn, "BEGIN WORK");
        $result = pg_query( $conn , $query);
        if (!$result) {
            @pg_query ($conn, "ROLLBACK");
            echo 'errore';
            return false;
        }
        @pg_query ($conn, "COMMIT");
        @pg_close ($conn);
        echo 'success';

    }
    */


}


?>