<?php
    ini_set('display_errors', 1);
    ini_set('display_startup_errors', 1);
    error_reporting(E_ALL);
/* Scripting by Okhotnikov Oleg, Pavel Chagovsky, contact - svoboda200786@gmail.com */
    if(!is_dir("images/")) {
        mkdir("images/", 0777, true);
    }
    if(!is_dir("images/source/")) {
        mkdir("images/source/", 0777, true);
    }
    if(!is_dir("images/saved/")) {
        mkdir("images/saved/", 0777, true);
    }
    if(!is_dir("images/preview/")) {
        mkdir("images/preview/", 0777, true);
    }
    if(!is_dir("images/tmp/")) {
        mkdir("images/tmp/", 0777, true);
    } 
    require_once("lib.php");     
    $files = scandir("images/saved/");
    $names = [];
    unset($files[0], $files[1]);
    sort($files);   
    foreach($files as $file){
        $name = $file;
        array_push($names, $name);
    }
    $noeditfiles = scandir("images/source");
    $noeditnames = [];
    unset($noeditfiles[0], $noeditfiles[1]);
    sort($noeditfiles);
    $flag = false;
    foreach($noeditfiles as $noeditefile){
        $noeditname = substr($noeditefile, 0, -4);
        foreach($names as $name){
          if(str_replace("-", "", pathinfo($noeditefile)["filename"]) == $name){
            $flag = true;
          }
        }
        if(!$flag){
            array_push($noeditnames, $noeditefile);
            $imgstring = file_get_contents("images/source/".$noeditefile);
            savePreview($imgstring, $noeditefile, 1, 0); 
        }
        $flag = false;
    }
?>
<div class="items">
    <div class="gallery">
        <?php for($i = 0; $i < count($files); $i++):?>
        <div>
            <div class="img-preview">
                <img onclick="<?="document.body.style.overflowY = 'hidden'; document.querySelector('.mfp-container').style.display='block';";?><?="document.querySelector('.mfp-iframe').src='editor.php?top_panel_display=none&pointer_events=none&background=images/preview/".$files[$i].'_big.png'."'";?>" src="<?
                        $path = "images/preview/".$files[$i].".jpg";
                        $type = pathinfo($path, PATHINFO_EXTENSION);
                        $data = file_get_contents($path);
                        $base64 = 'data:image/' . $type . ';base64,' . base64_encode($data);
                        echo $base64;
                    ?>">
                <a href='#' onclick="<?="document.body.style.overflowY = 'hidden'; document.querySelector('.mfp-container').style.display='block'; document.querySelector('.mfp-iframe').src='editor.php?file_name=".$files[$i]."'";?>">Редактировать</a>
                <div><?=$files[$i];?></div>
            </div>
        </div>
        <?php endfor;?>
        <?php for($i = 0; $i < count($noeditnames); $i++):?>
            <div>
                <div class="img-preview">
                    <img onclick="<?="document.body.style.overflowY = 'hidden'; document.querySelector('.mfp-container').style.display='block';document.querySelector('.mfp-iframe').src='editor.php?top_panel_display=none&pointer_events=none&background=images/source/".$noeditnames[$i]."'";?>" src="<?
                        $path = "images/preview/".pathinfo($noeditnames[$i])['filename'].".jpg";
                        $type = pathinfo($path, PATHINFO_EXTENSION);
                        $data = file_get_contents($path);
                        $base64 = 'data:image/' . $type . ';base64,' . base64_encode($data);
                        echo $base64;
                    ?>">
                    <a href='#' onclick="<?="document.body.style.overflowY = 'hidden'; document.querySelector('.mfp-container').style.display='block'; document.querySelector('.mfp-iframe').src='editor.php?background=images/source/".$noeditnames[$i]."'";?>">Редактировать</a>
                    <div><?=$noeditnames[$i];?></div>
                </div>
            </div>
        <?php endfor;?>
    </div>
</div>