<?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");

/************************************/
/* Gestion des fichiers/répertoires */
/************************************/

/**
 * Donne le chemin parent d'un fichier ou répertoire
 *
 * @param $nom_ficdir
 * @return string
 */
function donne_chemin_parent($nom_ficdir)
{
    $chemin_parent = "";
    $tab_chemin = explode("/", $nom_ficdir);
    for ($i = 0; $i < count($tab_chemin) - 1; $i++) {
        $chemin_parent .= "/" . $tab_chemin[$i];
    }

    return $chemin_parent;
}


/**
 * Donne le nom du chemin parent d'un fichier ou répertoire
 *
 * @param $nom_ficdir
 * @return string
 */
function donne_nom_chemin_parent($nom_ficdir)
{
    if ($nom_ficdir <> "") {
        $row = lit_enr_fic($nom_ficdir);
        if (is_array($row)) {
            $row = encode_str($row);
            extract($row);
            $nom_chemin_parent = $fic_desc;
        } else
            $nom_chemin_parent = _("Non référencé");
    } else
        $nom_chemin_parent = _("Répertoire principal");

    return $nom_chemin_parent;
}


/**
 * Donne la liste des dossiers d'un répertoire
 *
 * @param $dir
 * @return array
 */
function Recup_dossiers($dir)
{
    $tab_dir = [];

    if (is_dir($dir)) {
        $d = dir($dir);
        while (false !== ($entry = $d->read())) {
            if (($entry != '.') AND ($entry != '..') AND (is_dir($dir . '/' . $entry))) {
                $ssrep = $dir . '/' . $entry;
                if (is_dir($ssrep)) {
                    $subdirs = Recup_dossiers($ssrep);
                    if ($subdirs) {
                        $tab_dir[] = $ssrep;
                        $tab_dir = array_merge($tab_dir, $subdirs);
                    } else
                        $tab_dir[] = $ssrep;
                }
            }
        }
        $d->close();
    }
    return $tab_dir;
}

/**
 * Renvoie vrai si le répertoire est vide
 * Faux s'il contient des répertoires ou des fichiers
 *
 * @param $dir
 * @return bool
 */
function is_empty_folder($dir)
{
    if (false !== ($dl = opendir($dir))) {
        while (false !== ($file = readdir($dl))) {
            if (($file <> ".") AND ($file <> "..") AND ($file <> ".htaccess")) {
                if (is_dir("$dir/$file") OR is_file("$dir/$file")) {
                    return false;
                    break;
                }
            }
        }
        closedir($dl);
    }
    return true;
}
?>