<?php
/****************************************************************************************
 * SMI (Services Maintenance Interventions) est une gestion libre de maintenance et de SAV
 *
 * Version 1.0.2
 *
 * Copyright (C) 2006-2015  Sylvain FATOME
 * This program is free software; you can redistribute it and/or modify it under the terms
 * of the GNU General Public License as published by the Free Software Foundation;
 * either version 2 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with this program
 * (in the directory docs);
 * if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA  02110-1301, USA.
 *
 * Contact : galawa@free.fr
 *
 * Web : http://smi.no-ip.org
 ****************************************************************************************/


// vérification des tentatives de hack
$pos = strpos($_SERVER['PHP_SELF'], "inc.php");
if ($pos === FALSE)
    $ok_inc = TRUE;
else
    $ok_inc = FALSE;
if ($ok_inc == FALSE) header("Location: ../index.php");


include_once("../inc/config.inc.php");
include_once("../inc/fic_cli.inc.php");
include_once("../inc/fic_fici.inc.php");
include_once("../inc/fic_hum.inc.php");
include_once("../inc/fic_int.inc.php");
include_once("../inc/fic_txt.inc.php");
include_once("../inc/func.inc.php");
include_once("../inc/func_date.inc.php");
include_once("../inc/func_log.inc.php");
include_once("../inc/func_geo.inc.php");
include_once("../inc/func_mail.inc.php");
include_once("../inc/func_txt.inc.php");
include_once("../inc/func_txtcm.inc.php");
include_once("../classes/phpmailer/class.phpmailer.php");
include_once("../classes/phpmailer/class.smtp.php");
include_once("mail.inc.php");
include_once("../classes/ephpu/upload_class.php");
include_once("../classes/ezSQL/shared/ez_sql_core.php");
include_once("../classes/ezSQL/mysqli/ez_sql_mysqli.php");
include_once("../inc/func_sql.inc.php");
Archivelog(@$_SESSION["code_log"]);
// Variables Formulaire
fix_magic_quotes();
$_POST = clean_str($_POST);

$retourliste = TRUE;
$tx = '';
$codeint = '';
$codecli = '';
$rep_actif = '';

// Téléchargement réservé à l'admin, le centre de gestion, les agences, et les techniciens
if ((@$_SESSION["niv_log"] == 9) OR (@$_SESSION["niv_log"] == 5) OR (@$_SESSION["niv_log"] == 4) OR (@$_SESSION["niv_log"] == 3)) {
    $pbm_pj = FALSE;
    $mode_pj = FALSE;
    $rep_pj = "";
    $ss_titre = "";

    if (isset($_POST['tx'])) {
        $tx = $_POST['tx'];
        switch ($tx) {
            case "cli" :
            case "int" :
                $mode_pj = TRUE;
                break;
            default :
                $pbm_pj = TRUE;
                break;
        }
    } else
        $pbm_pj = TRUE;

    if (isset($_POST['cc'])) {
        $codecli = $_POST['cc'];
        $row = lit_enr_cli($codecli, TRUE);
        if (is_array($row))
            extract($row);
        else
            $pbm_pj = FALSE;
    } else
        $pbm_pj = TRUE;

    if (isset($_POST['ci'])) {
        $codeint = $_POST['ci'];
        $row = lit_enr_int($codeint, TRUE);
        if (is_array($row))
            extract($row);
        else
            $pbm_pj = FALSE;
    } else {
        if ($tx == "int")
            $pbm_pj = TRUE;
    }

    if (isset($_POST['dir'])) {
        $rep_actif = $_POST['dir'];
        if ($rep_actif <> "clients/" . strtolower($codecli) . "/PJ")
            $pbm_pj = FALSE;
    } else
        $pbm_pj = TRUE;

    switch ($tx) {
        case "cli" :
            $rep_pj = $url_gfc . "/clients/" . strtolower($codecli) . "/PJ";
            $ss_titre = _("Pour le client").' ' . $codecli;
            break;
        case "int" :
            $rep_pj = $url_gfc . "/clients/" . strtolower($codecli) . "/PJ";
            $ss_titre = _("Pour l'intervention"). ' ' . $codeint;
            break;
        default :
            $pbm_pj = TRUE;
            break;
    }

    if ($pbm_pj == TRUE) {
        $retourliste = FALSE;
        $msg = _("Problème de paramètres");
    }
} else {
    $retourliste = FALSE;
    $msg = _("Vous n'avez pas le droit de rattacher des pièces jointes");
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8">
    <title>SMI - <?PHP echo _("ESPACE PRIVE"); ?> : <?PHP echo _("Pièces jointes"); ?></title>

    <?PHP include_once("../inc/header.inc.php"); ?>

</head>
<body>

<?php
switch (@$_POST['tx']) {
    case "cli" :
    case "int" :
        break;
    default :
        include_once("entete.inc.php");
        break;
}
?>
<p align="center"><span class="titrepage"><?PHP echo _("PIECES JOINTES"); ?></span></p>

<p align="center"><span class="annot_gras"><?PHP echo $ss_titre; ?></span></p>

<p align="center"><?php echo $msg; ?></p>
<?PHP
if ($pbm_pj == FALSE) {
    $typrat = -1;
    $coderat = "";
    switch (@$_POST['tx']) {
        case "cli" :
            break;
        case "int" :
            $typrat = 1;
            $coderat = $codeint;
            break;
        default :
            break;
    }

    if ($typrat <> -1) {
        // On construit le tableau des ID des fichiers choisis
        $tab_ficlie = @$_POST["fic_lie"];
        $tab_ficlieid = [];

        $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST);

        // On crée le tableau des ID
        if (count($tab_ficlie) > 0) {
            foreach ($tab_ficlie as $key => $value) {
                $id = donne_id_fic($url_gfc . "/" . $value, FALSE);
                $tab_ficlieid[$key] = $id;
            }
        }

        // On vérifie si déjà enregistré
        if (count($tab_ficlie) > 0) {
            foreach ($tab_ficlie as $key => $value) {
                $deja = est_PJ_code($value, $coderat, FALSE);
                // Si pas encore enregistre, on ajoute
                if ($deja == FALSE) {
                    $rech = "INSERT INTO `" . TBFICPJ . "`";
                    $rech .= " (ficpj_typrat,ficpj_coderat,ficpj_idfic)";
                    $rech .= " VALUES ('$typrat','$coderat','" . $tab_ficlieid[$key] . "')";
                    $db->query($rech);
                }
            }
        }

        // On supprimer tout ce qui était rattaché et qui ne l'est plus
        $rech = "DELETE FROM `" . TBFICPJ . "` WHERE `ficpj_coderat` = '$coderat'";
        if (count($tab_ficlieid) > 0) {
            $rech .= " AND (";
            foreach ($tab_ficlieid as $key => $value) {
                $rech .= " ficpj_idfic<>'" . $value . "' AND";
            }
            // Suppression du dernier 'AND'
            $rech = substr($rech, 0, strlen($rech) - 4);
            $rech .= ")";
        }
        $db->query($rech);

        $db->disconnect();
    }

    switch (@$_POST['tx']) {
        case "cli" :
            $chemin_retour = "fic_pj.php?dir=" . $rep_actif . "&cc=" . $codecli . "&tx=" . $tx;
            break;
        case "int" :
            $chemin_retour = "fic_pj.php?dir=" . $rep_actif . "&cc=" . $codecli . "&ci=" . $codeint . "&tx=" . $tx;
            break;
        default :
            $chemin_retour = "index.php";
            break;
    }
} else
    $chemin_retour = "index.php";

if ($retourliste == FALSE)
    aff_ret_idx_man($td, $chemin_retour, _("Retour à la liste"), TRUE);

else
    pop_ret_auto('', '', $chemin_retour);

switch (@$_POST['tx']) {
    case "cli" :
    case "int" :
        break;
    default :
        include_once("pied.php");
        break;
}
?>
