<?php
/****************************************************************************************
 * SMI (Services Maintenance Interventions) est une gestion libre de maintenance et de SAV
 *
 * Version 1.0.0
 *
 * Copyright (C) 2006-2014  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/fic_age.inc.php");
include_once("../inc/fic_cli.inc.php");
include_once("../inc/fic_droits.inc.php");
include_once("../inc/fic_fici.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_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("../inc/func_fic.inc.php");
include_once("../inc/lst.inc.php");
include_once("../classes/phpmailer/class.phpmailer.php");
include_once("../classes/phpmailer/class.smtp.php");
include_once("mail.inc.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();
$_GET = clean_str($_GET);
/* On récupère l'action à effectuer demandée depuis formulaire appelant */
$todo_from = '';
if (isset($_POST["multiple"])) {
    if (trim(@$_POST["multiple"]) == _("Supprimer"))
        $todo_from = "del";
    if (trim(@$_POST["multiple"]) == _("Déplacer"))
        $todo_from = "move";
}
$_POST = clean_str($_POST);

$msg = "";
$niveau = -1;
$cc = "";
$tx = "";
$pbm_pj = FALSE;

/* On récupère l'action à effectuer demandée depuis formulaire appelant */
$todo_ok = '';
if (isset($_POST["nbutton"])) {
    if (trim(@$_POST["nbutton"]) == "delok")
        $todo_ok = "del";
    if (trim(@$_POST["nbutton"]) == "moveok")
        $todo_ok = "move";
}

if (isset($_GET['cc']))
    $cc = $_GET['cc'];
else {
    if (isset($_POST['cc']))
        $cc = $_POST['cc'];
}

if ($codecli <> "") {
    $row = lit_enr_cli($codecli, TRUE);
    if (is_array($row))
        extract($row);
    else
        $pbm_pj = FALSE;
}

if (isset($_GET['tx'])) {
    $tx = $_GET['tx'];
    switch ($tx) {
        case "cli" :
            $mode_pj = TRUE;
            $rep_pj = $url_gfc . "/clients/" . strtolower($codecli) . "/PJ";
            break;
        default :
            $pbm_pj = TRUE;
            break;
    }
} else {
    if (isset($_POST['tx'])) {
        $tx = $_POST['tx'];
        switch ($tx) {
            case "cli" :
            case "int" :
                $mode_pj = TRUE;
                $rep_pj = $url_gfc . "/clients/" . strtolower($codecli) . "/PJ";
                break;
            default :
                $pbm_pj = TRUE;
                break;
        }
    }
}
?>
<!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 _("Suppressions/Déplacements Fichiers"); ?></title>

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

    <script language="javascript">
    <!--
    var tab1_champ = [];
    //-->
    </script>

    <script type="text/javascript">
    <!--
    function init_all() {
        init_Body();
        init_autohref_classe('falselnkimg','');
        <?PHP
        switch ($todo_from)
        {
            case "del" :
                echo "init_valid_form('delok','fddmm_maj','".$_SESSION['date_fmtshort']."',tab1_champ,'','');";
                break;
            case "move" :
                echo "init_valid_form('moveok','fddmm_maj','".$_SESSION['date_fmtshort']."',tab1_champ,'','');";
                break;
        }
        ?>
        init_back('annul','click');
        Focus_first();
    }

    $(document).ready(function () {
        init_all();
    });
    //-->
    </script>

</head>

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

<p align="center">&nbsp;</p>
<?PHP
$chemin_parent = $_SESSION["rb"];
$nom_chemin_parent = donne_nom_chemin_parent($chemin_parent);

// On vient de la liste des fichiers
if ($todo_from <> '')
{
    if (count($_POST["fic_choisi"]) <> 0) {
        $row = lit_enr_fic($chemin_complet);
        if (is_array($row)) {
            $row = encode_str($row);
            extract($row);
            $ex_id = $fic_id;
        }

        // On récupère la liste des répertoires accessibles
        switch ($_SESSION["niv_log"]) {
            case 3 :
            case 4 :
                $lst_rep = Recup_dossiers($url_gfc . "/agences/" . strtolower($_SESSION["code_log"]));
                break;
            case 5 :
            case 9 :
                $lst_rep = Recup_dossiers($url_gfc);
                break;
            default :
                $lst_rep = [];
                break;
        }

        echo '<form name="fddmm_maj" id="fddmm_maj" method="post" action="fddmm.php">';
        echo '<input name="cc" type="hidden" id="cc" value="' . $cc . '">';
        echo '<input name="tx" type="hidden" id="tx" value="' . $tx . '">';
        echo '<input name="nbutton" type="hidden" id="nbutton" value="">';
        echo '<div align="center" class="annot_std_gras">';
        switch ($todo_from) {
            case "del" :
                echo '<p>'._("Vous êtes dans le répertoire").'</p>';
                echo '<div class="annot_std_it">' . utf8_encode($_POST["txtrep"]) . '</div>';
                // echo '<div class="annot_std_it">' . $nom_chemin_parent . '</div>';
                echo '<p>'._("Merci de confirmer la suppression des fichiers ou répertoires suivants").'</p>';
                break;
            case "move" :
                echo _("Merci de confirmer le déplacement des fichiers ou répertoires suivants").'<br />';
                echo _("et de choisir le répertoire de destination");
                echo '</p>';
                echo '<table width="90%" border="0" cellpadding="0" cellspacing="0" align="center">';
                echo '<tr>';
                echo '<td nowrap class="rubfrm" align="right" valign="middle" id="f_chemin">';
                echo _("Ancien dossier parent").' : ';
                echo '</td>';
                echo '<td nowrap valign="middle">';
                echo '<input style="margin-left:5px;" name="nom" type="text" id="nom" value="' . $nom_chemin_parent . '"  size="80" readonly="1">';
                echo '<br />';
                echo '</td>';
                echo '</tr>';
                echo '<tr>';
                echo '<td nowrap class="rubfrm" align="right" valign="middle" id="f_mve">';
                echo _("Déplacer vers").' : ';
                echo '</td>';
                echo '<td nowrap valign="middle">';
                echo '<select style="margin-left:5px;" name="nrep" size="1" id="nrep">';
                echo '<option>'._("-- Faites votre choix --").'</option>';
                switch ($_SESSION["niv_log"]) {
                    case 3 :
                    case 4 :
                        $chem_comp = "/agences/" . strtolower($_SESSION["code_log"]);
                        if ($chemin_parent <> $chem_comp)
                            echo '<option value="0">'._("Votre répertoire principal").'</option>';
                        break;
                    case 5 :
                    case 9 :
                        if ($chemin_parent <> "")
                            echo '<option value="0">'._("Votre répertoire principal").'</option>';
                        break;
                    default :
                        break;
                }

                for ($i = 0; $i < count($lst_rep); $i++) {
                    if ($lst_rep[$i] <> $chemin_complet) {
                        $row = lit_enr_fic($lst_rep[$i]);
                        if (is_array($row)) {
                            $row = encode_str($row);
                            extract($row);
                            if ($fic_nom <> ($url_gfc . $chemin_parent))
                                echo '<option value="' . $fic_id . '">' . $fic_desc . ' (' . str_replace($url_gfc, "", $fic_nom) . ')</option>';
                        }
                    }
                }
                echo '</select>';
                echo '</td>';
                echo '</tr>';
                echo '</table>';
                echo '<br />';
                break;
        }
        echo '<table class="Mtable" border="0" width="100%" cellpadding="0" cellspacing="0">';
        echo '<th>';
        echo "&nbsp;";
        echo '</th>';
        echo '<th align="center">';
        echo _("Fichier");
        echo '</th>';
        for ($i = 0; $i < count($_POST["fic_choisi"]); $i++) {
            // Création de l'entête de ligne (TR)
            creetrlst($i);

            $nom_fic = $_POST["fic_choisi"][$i];
            echo '<td align="center" width="30">';
            if ($i % 2 == 0)
                $classe_aff = "TRalter1";
            else
                $classe_aff = "TRalter2";
            echo '<input type="checkbox" name="fic_choisi[]" value="' . $nom_fic . '" class="' . $classe_aff . '" checked="checked" onclick="return false">';
            echo '</td>';
            echo '<td align="center">';
            $chemin_complet = $chemin_parent. "/" . $_POST["fic_choisi"][$i];
            $row = lit_enr_fic($chemin_complet);
            if (is_array($row)) {
                $row = encode_str($row);
                extract($row);
                if ($fic_desc == "")
                    $fic_desc = $nom_fic;
            } else {
                $fic_desc = $nom_fic;
            }
            echo '<span class="annot_std_gras">';
            echo $fic_desc;
            echo '</span>';
            echo '</td>';
        }
        echo '</tr>';
        echo '</table>';
        echo '<p align="center">';
        switch ($todo_from) {
            case "del" :
                echo '<input type="button" name="delok" id="delok" value="' . _("Supprimer") . '">';
                break;
            case "move" :
                echo '<input type="button" name="moveok" id="moveok" value="' . _("Déplacer") . '">';
                break;
        }
        echo ' <input type="button" name="annul" id="annul" value="' . _("Annuler") . '">';
        echo '</p>';
        echo '</form>';
    } else
        $msg = _("Vous n'avez rien sélectionné");
}

// On a confirmé les suppressions/déplacements
if ($todo_ok <> '')
{
    $niveau = -2;
    if (count($_POST["fic_choisi"]) <> 0) {
        echo '<table class="Mtable" border="0" width="100%" cellpadding="0" cellspacing="0">';
        echo '<th>';
        echo _("Fichier");
        echo '</th>';
        echo '<th>';
        echo _("Opération");
        echo '</th>';
        switch ($todo_ok) {
            case "del" :
                for ($i = 0; $i < count($_POST["fic_choisi"]); $i++) {
                    $chemin_complet = $chemin_parent. "/" . $_POST["fic_choisi"][$i];

                    // Création de l'entête de ligne (TR)
                    creetrlst($i);

                    $nom_fic = $_POST["fic_choisi"][$i];
                    echo '<td align="center" width="80%">';
                    $row = lit_enr_fic($chemin_complet);
                    if (is_array($row)) {
                        $row = encode_str($row);
                        extract($row);
                        if ($fic_desc == "")
                            $fic_desc = $nom_fic;
                    } else {
                        $fic_desc = $nom_fic;
                    }
                    echo '<span class="annot_std_gras">';
                    echo $fic_desc;
                    echo '</span>';
                    echo '</td>';
                    echo '<td align="center">';
                    if ((substr($nom_fic, 0, 1) <> '.') AND (substr($nom_fic, 0, 2) <> '..') AND (trim($nom_fic) <> '.htaccess')) // Contrôle de hack
                    {
                        if ((est_createur_ficdir($chemin_complet, @$_SESSION["code_log"]) == TRUE) OR (@$_SESSION["niv_log"] == 9)) {
                            $existe = FALSE;
                            $effacer = FALSE;
                            if (is_file($chemin_complet)) {
                                $existe = TRUE;
                                if (unlink($chemin_complet)) {
                                    // Envoi d'email vers l'administrateur pour la suppression du fichier
                                    envoi_mailwm($chemin_complet, _("Suppression de fichier"), "fic", "del", "xxx", "wm", $chemin_complet, "", "", FALSE);

                                    $effacer = TRUE;

                                } else
                                    echo _("Suppression impossible");
                            }

                            if (is_dir($chemin_complet)) {
                                $existe = TRUE;
                                if (is_empty_folder($chemin_complet)) {
                                    if (rmdir($chemin_complet)) {
                                        // Envoi d'email vers l'administrateur pour la suppression du répertoire
                                        envoi_mailwm($chemin_complet, _("Suppression de répertoire"), "dir", "del", "xxx", "wm", $chemin_complet, "", "", FALSE);

                                        $effacer = TRUE;
                                    } else {
                                        echo _("Suppression impossible");
                                        $retourliste = FALSE;
                                    }
                                } else {
                                    echo "Non vide";
                                    $retourliste = FALSE;
                                }
                            }

                            if ($effacer == TRUE) {
                                $id = donne_id_fic($chemin_complet);
                                if ($id <> -1) {
                                    $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST);
                                    $rech = "DELETE FROM `" . TBFIC . "` WHERE `fic_id` = '$id'";
                                    $db->query($rech);
                                    $db->disconnect();
                                }

                                echo _("Supprimé");
                            }

                            if ($existe == FALSE)
                                echo _("N'existe pas");
                        } else
                            echo _("Interdit");
                    } else
                        echo _("Interdit");
                    echo '</td>';
                    echo '</tr>';
                }
                break;
            case "move" :
                echo "ok <br>";
                for ($i = 0; $i < count($_POST["fic_choisi"]); $i++) {
                    $chemin_complet = $chemin_parent. "/" . $_POST["fic_choisi"][$i];

                    // Création de l'entête de ligne (TR)
                    creetrlst($i);

                    $nom_fic = $_POST["fic_choisi"][$i];
                    $existe = FALSE;
                    echo '<td align="center" width="80%">';
                    $row = lit_enr_fic($chemin_complet);
                    if (is_array($row)) {
                        $row = encode_str($row);
                        extract($row);
                        if ($fic_desc == "")
                            $fic_desc = $nom_fic;
                        $ex_id = $fic_id;
                        $existe = TRUE;
                    } else
                        $fic_desc = $nom_fic;

                    $ex_chemin = $chemin_complet;
                    echo '<span class="annot_std_gras">';
                    echo $fic_desc;
                    echo '</span>';
                    echo '</td>';
                    echo '<td align="center">';
                    if ((substr($nom_fic, 0, 1) <> '.') AND (substr($nom_fic, 0, 2) <> '..') AND (trim($nom_fic) <> '.htaccess')) // Contrôle de hack
                    {
                        if ((est_createur_ficdir($chemin_complet, @$_SESSION["code_log"]) == TRUE) OR (@$_SESSION["niv_log"] == 9)) {
                            if ((is_file($chemin_complet)) OR (is_dir($chemin_complet))) {
                                $mve_ok = FALSE;

                                $nouv_id = intval(@$_POST['nrep']);
                                if ($nouv_id <> 0)
                                    $row = lit_enr_ficid($nouv_id);
                                if ((is_array($row)) OR ($nouv_id == 0)) {
                                    if ($nouv_id <> 0) {
                                        extract($row);
                                        if (is_dir($fic_nom))
                                            $mve_ok = TRUE;
                                    } else
                                        $mve_ok = TRUE;

                                    if ($mve_ok == TRUE) {
                                        $tab_ex_chemin = explode("/", $ex_chemin);
                                        if ($nouv_id <> 0)
                                            $nouv_chemin = $fic_nom . "/" . $tab_ex_chemin[count($tab_ex_chemin) - 1];
                                        else {
                                            if ($_SESSION["niv_log"] >= 5)
                                                $nouv_chemin = $url_gfc . "/" . $tab_ex_chemin[count($tab_ex_chemin) - 1];
                                            else
                                                $nouv_chemin = $url_gfc . "/agences/" . strtolower($_SESSION["code_log"]) . "/" . $tab_ex_chemin[count($tab_ex_chemin) - 1];
                                        }
                                        if (rename($ex_chemin, $nouv_chemin)) {
                                            if ($existe == TRUE) {
                                                $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST);
                                                $rech = "UPDATE `" . TBFIC . "`";
                                                $rech .= " SET `fic_nom`='" . $nouv_chemin . "'";
                                                $rech .= "  WHERE `fic_id` = '" . $ex_id . "'";
                                                $db->query($rech);
                                                $db->disconnect();
                                            }
                                            echo _("Déplacé");
                                        }
                                    }
                                } else {
                                    echo _("Problème de répertoire");
                                }
                            } else {
                                echo _("N'existe pas");
                            }
                        } else
                            echo _("Interdit");
                    } else
                        echo _("Interdit");
                    echo '</td>';
                    echo '</tr>';
                }
                break;
        }
        echo '</table>';
    } else
        $msg = _("Vous n'avez rien sélectionné");
}

if ($msg <> "") {
    if ($retourliste == FALSE)
        exec_func_js("warn_txt('" . str_escape($msg) . "')");
    else
        exec_func_js("pop_txt('" . str_escape($msg) . "')");
}

if (($msg <> "") OR ($niveau == -2)) {
    ?>
    <table width="100%" border="0">
        <tr>
            <td width="3%">&nbsp;</td>
            <td width="44%">
                <div align="right">
                    <a href="javascript:history.go(<?PHP echo $niveau; ?>);">
                        <img src="../img/liste.png" border="0" align="absmiddle">
                        <span class="annot_gras_it"><?PHP echo _("Retour à la liste"); ?></span></a>
                </div>
            </td>
            <td width="7%">&nbsp;</td>
            <td width="43%">
                <?PHP
                switch ($tx) {
                    case "cli" :
                    case "int" :
                        break;
                    default :
                        echo '<a href="index.php"><em><img src="../img/index.png" border="0" align="absmiddle"> </em><span class="annot_gras_it">'._("Revenir à l'accueil").'</span></a>';
                        break;
                }
                ?>
            </td>
            <td width="3%">&nbsp;</td>
        </tr>
    </table>
<?PHP
}
switch (@$_POST['tx']) {
    case "cli" :
    case "int" :
        break;
    default :
        include_once("pied.php");
        break;
}
?>
