<?php
/****************************************************************************************
 * SMI (Services Maintenance Interventions) est une gestion libre de maintenance et de SAV
 *
 * Version 1.0.4
 *
 * Copyright (C) 2006-2016  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) {
    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;
}

/**
 * @param $rep
 * @param bool|TRUE $chkexist
 * @return bool
 */
function cree_dir($rep, $chkexist = TRUE)
{
    $cree_rep = TRUE;

    $to_create = TRUE;
    if ($chkexist == TRUE) {
        if (file_exists($rep)) {
            $to_create = FALSE;
        }
    }

    if ($to_create == TRUE) {
        if (!is_dir($rep)) {
            if (is_writable(dirname($rep))) {
                $cree_rep = mkdir($rep);
            }
        }
    }

    return $cree_rep;
}

/**
 * @param $file
 * @return array
 */
function file_imgsize($file)
{
    if (file_exists($file)) {
        $infos_img = getimagesize($file);
    }

    return $infos_img;
}

/**
 * @param $file
 * @param $mode
 * @return resource
 */
function openfile($file, $mode)
{
    if (file_exists($file)) {
        $handle = fopen($file, $mode);
    }

    return $handle;
}
