<?php

    //$path = "/home/analogde/www/Fusion/File explorer";
    //$resultat_find_version = [];
    //$resultat_find_version = analyse_version($path);
    //echo "<pre>";
    //print_r($resultat_find_version);
    //echo "</pre>";

    //echo "Fin";

/*********************************************/
function analyse_version($path)    
{
    $combine = [];
    $combine = analyse_fichiers($path);

    $array_file_filter = [];
    $array_file_version = [];

    $array_file_filter = $combine[0]; 
    $array_file_version = $combine[1]; 

    $revision = [];
    $revision = detection_version($array_file_filter, $array_file_version);

    return $revision;
}   
/*********************************************/
function analyse_fichiers($path)
{
    $files = explore_folder($path);

    $array_file_filter = []; 
    $array_file_version = [];

    for ($i = 0; $i < count($files) ; $i++) 
    {
        if ( !strpos($files[$i] , '***rev')) 
        {
            $array_file_filter[] = $files[$i];
        }
        else
        {
            $array_file_version[] = $files[$i];
        }
    }

    return array($array_file_filter, $array_file_version);
}
/*********************************************/
function detection_version($array_file_filter, $array_file_version)
{    
    $nom = [];
    $version = [];
    $revision = [];

    for ($i = 0; $i < count($array_file_filter) ; $i++) 
    {
        $basename = pathinfo( $array_file_filter[$i] , PATHINFO_FILENAME); 

        for ($j = 0; $j < count($array_file_version) ; $j++) 
        {   
            if ( strpos( "_" .$array_file_version[$j] , $basename ."_"  )) 
            {
                if( sizeof($nom) == 0 )
                { 
                    $nom[] = $array_file_filter[$i];
                }   
                //echo " ----" .$source ."   ***    " .$array_file_filter[$i]. "<br><br>"; 
                $version[] = $array_file_version[$j];
            }   
        }

        if( sizeof($version) != 0 )
        {
            $nom[] = $version;
            $revision[] = $nom;
        }
            unset($nom);
            unset($version);
    }

   return $revision;
}
/****************************************************************/  
function explore_folder($path)
{
    $objects = is_readable($path) ? scandir($path) : array();
    $folders = array();
    $files = array();

    if (is_array($objects)) 
    {
        foreach ($objects as $file) 
        {
            if ($file == '.' || $file == '..') 
            {
                continue;
            }

            $new_path = $path . '/' . $file;

            if (is_file($new_path)) 
            {
                $files[] = $file;
            } 
            elseif (is_dir($new_path) && $file != '.' && $file != '..') 
            {
                $folders[] = $file;
            }
        }
    }

    return $files;
}    
/*********************************************/
?>