<?php
$folder=  'images'.DIRECTORY_SEPARATOR;          //nome della cartella da cui prendere le immagini
$sitoweb=getFullUrl();
$sitoweb=str_replace("admin/editor","",$sitoweb);

$immagini = isset($_GET['immagini']) ? $_GET['immagini'] : '';
$op = isset($_GET['op']) ? $_GET['op'] : '';


if($op=="newfile"){
    $ds          = DIRECTORY_SEPARATOR;  //1
    $storeFolder = 'images';   //2
    if (!empty($_FILES)) {

        $filename = basename($_FILES['nomefile']['name']);
        $ext = substr($filename, strrpos($filename, '.') + 1);
        if (($ext == "jpeg") || ($ext == "jpg") || ($ext == "JPG") || ($ext == "gif") || ($ext == "GIF") || ($ext == "png") || ($ext == "PNG")) {

            $tempFile = $_FILES['nomefile']['tmp_name'];          //3
            $targetPath = dirname( __FILE__ ) . $ds. $storeFolder . $ds;  //4
            $targetFile =  $targetPath. $_FILES['nomefile']['name'];  //
            move_uploaded_file($tempFile,$targetFile); //6

        }
    }
}


if($immagini==1){
//apre la directory della variabile $folder e mette tutti i file letti in un array
$i=0;
$handle=opendir($folder);
while ($file = readdir ($handle)){
    if ($file != "." && $file != ".."  && $file != ".DS_Store" && $file != "index.php")     {

        if(strlen($nome)>0){
            if(trovaStringa($file,$nome)){
                $files[$i] = $file;
                $i++;
            }
        }else{
            $files[$i] = $file;
            $i++;
        }

    }
}
closedir($handle);

    $folder2=str_replace("../../","",$folder);
    echo "<table class=\"table table-bordered\" width=\"100%\">\n";

    echo "<tr>\n";
    $i=1;
    for($s=0;$s<count($files);$s++){

        echo "
         <td style=\"background:#ffffff;margin:5px\">
            <center>
            <img src=\"$folder/".$files[$s]."\" style='boder: 0; width: 100px; height: 100px;' >
            </center>

            <span style=\"font-size:11px\">".substr($files[$s],0,25)."</span>
            <br>
            <a href=\"javascript:void(0);\" onclick=\"inserisci(this);\" class=\"insert-image\" data-image=\"{$sitoweb}/{$folder2}/{$files[$s]}\" title=\"Mettre cette photo\"><span class=\"glyphicon glyphicon-check\"></span></a>
            <a href=\"javascript:void(0);\" onclick=\"deleteImg(id);\" class=\"insert-image\" data-image=\"{$sitoweb}/{$folder2}/{$files[$s]}\" id=\"{$folder2}{$files[$s]}\" title=\"Supprimer cette photo\"><span style='color: red;' class=\"glyphicon glyphicon-trash\"></span></a>
            <br>

         </td>\n
     ";
        if($i==3){$i=0;echo"</tr><tr>";}
        $i++;

    }
    echo "</tr>";
    echo"</table>";

}else{
?>


<!--

  <input type="text" name="nome" value="<?php echo $nome;?>" placeholder="name of images"><input class="btn btn-info" type="submit" value="search">

-->

<div id="contenutoimmagini"></div>


    <style>
        .file-upload-container {
            overflow: hidden;
            /*position: relative;*/
        }
        .file-upload-container [type=file] {
            cursor: inherit;
            /*display: block;*/
            display: none;
            font-size: 17px;
            filter: alpha(opacity=0);
            /*min-height: 100%;
            min-width: 100%;*/
            opacity: 0;
            /*position: absolute;
            right: 0;
            text-align: right;
            top: 50%;
            translateX: -50%;
            translateY: -50%;*/
        }
        label.file-upload-container {
            height: 34px;
            font-size: 18px;
            border: 1px solid #ccc;
            border-radius: 4px;
            padding: 6px 12px;
            line-height: 1;
        }
        .file-upload-container [type=file] {
            cursor: pointer;
        }
        #specialBoutonUploadImg:hover{
            background-color: grey !important;
            font-size: 19px !important;
        }
    </style>
<form enctype="multipart/form-data" id="form-id">
    <div class="row">
        <div class="col-sm-12">
            <label class=file-upload-container>
                Choisir un fichier
                <input name="nomefile" type="file" />
            </label>
        </div>
    </div>
    <div class="row">
        <div class="col-sm-12">
            <input id="specialBoutonUploadImg" class="button" type="button" value="Upload" style="width: 120px; height: 45px; background-color: #9acd32; color: #fff; font-size: 18px; transition: all 0.5s ease;"/>
        </div>
    </div>
</form>
<progress value="0"></progress>

    <br>

<script>
    $(document).ready(function(){

    //html5 uploader
    $('.button').click(function(){
        var formx = document.getElementById('form-id');
        var formData = new FormData(formx);
        $.ajax({
            url: 'media-popup.php?op=newfile',  //Server script to process data
            type: 'POST',
            xhr: function() {  // Custom XMLHttpRequest
                var myXhr = $.ajaxSettings.xhr();
                if(myXhr.upload){ // Check if upload property exists
                    myXhr.upload.addEventListener('progress',progressHandlingFunction, false); // For handling the progress of the upload
                }
                return myXhr;
            },
            //Ajax events
            success: completeHandler,
            error: errorHandler,
            // Form data
            data: formData,
            //Options to tell jQuery not to process data or worry about content-type.
            cache: false,
            contentType: false,
            processData: false
        });

        function completeHandler(){
            loadimages();
        }
        function errorHandler(){
            alert('errore caricamento');
        }
        function progressHandlingFunction(e){
            if(e.lengthComputable){
                $('progress').attr({value:e.loaded,max:e.total});
            }
        }
    });

        loadimages();

    });

    function inserisci(elemento){
        var link = $(elemento);
        var image = link.data('image');
        $('#image-url').val(image);


        var id= $('#image-url').data('id');
        $('#'+id).attr('src', $('#image-url').val()) ;
        $('#'+id).attr('width', $('#image-w').val()) ;
        $('#'+id).attr('height', $('#image-h').val()) ;

        $('#previewimg').modal('hide');
    }

    function deleteImg(elem){
        if (confirm('Etes-vous sûr de vouloir supprimer cette image ?')){
            event.preventDefault();
            var link = document.getElementById(elem).id;
            console.log(link);

            $.ajax({
                type: "POST",
                url: "supp-img.php",
                data: 'link='+ link,
            });
            alert('Votre image à bien été supprimé.');

        loadimages();
					    
		}
    }

    function loadimages(){

        var request = $.ajax({
            url: "media-popup.php?immagini=1",
            method: "POST",
            data: { nome : '' },
            dataType: "html"
        });

        request.done(function( msg ) {
            $( "#contenutoimmagini" ).html( msg );
        });
        
        
        //Remise à 0 de la barre de progression de l'upload d'images
        var maxProgress = $('progress').attr('max');
        var valueProgress = $('progress').attr('value');
        if (valueProgress == maxProgress) {
            $('progress').attr('value', '0');  
        }
        

    }
</script>


<?php }   //else di immagini=1    ?>


<?php

function trovaStringa($text,$wordToSearch)
{
    $offset=0;
    $pos=0;
    while (is_integer($pos)){
        $pos = strpos($text,$wordToSearch,$offset);
        if (is_integer($pos)) {
            $arrPos[] = $pos;
            $offset = $pos+strlen($wordToSearch);
        }
    }
    if (isset($arrPos)) {
        return 1;
    }
    else {
        return 0;
    }
}

function makeSafe( $file ) {
    return str_replace( '..', '', urldecode( $file ) );
}


function getFullUrl() {
    if (isset($_SERVER['HTTPS']) && !empty($_SERVER['HTTPS'])){
        $https = strlen($_SERVER['HTTPS']) != 0 && $_SERVER['HTTPS'] !== 'off';
    }$https="";
    return
        ($https ? 'https://' : 'http://').
        (isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : ($_SERVER['SERVER_NAME'].
        ($https && $_SERVER['SERVER_PORT'] === 443 ||
        $_SERVER['SERVER_PORT'] === 80 ? '' : ':'.$_SERVER['SERVER_PORT']))).
        substr($_SERVER['SCRIPT_NAME'],0, strrpos($_SERVER['SCRIPT_NAME'], '/'));
}
?>
