<?php


    # Uploaded images



    # login
    include_once 'data_connect.php';
    include 'command.php';


    
    # user rights - user info
    # no admin rights or user info is needed

    

    # upload an image file
    if (isset($_POST['MAX_FILE_SIZE'])) {
    
        $tname = time();
    
        $target_path = '../images/'.basename(str_replace(' ','',$_FILES['uploaded']['name']));
    
        if (strpos('.png .jpg .gif',substr($target_path,-4)) !== false) { 
    
            $directory = new DirectoryIterator('../images/');    
            foreach ($directory as $fileinfo) {
                
                if ($fileinfo->isFile()) {
                    
                    $name = $fileinfo->getFilename();
                    $xt = $fileinfo->getExtension();
                                            
                    if ($name == basename( $_FILES['uploaded']['name'])) {
                        
                        $target_path = '../images/'.$tname.'-'.basename(str_replace(' ','',$_FILES['uploaded']['name']));
                        break;
                    }        
                }
            }        
        
            if (move_uploaded_file($_FILES['uploaded']['tmp_name'], $target_path)) {
            
                $_SESSION['MONimg_upload_success'] = true;
                header('Location:uploads.php');
                exit();
            }
        
            else {
        
                $_SESSION['MONimg_upload_error'] = true;
                header('Location:uploads.php');
                exit();
            }
                            
        }

        else {
    
            $_SESSION['MONimg_upload_file_type'] = true;
            header('Location:uploads.php');
            exit();
        }

    }


    if ( isset($_GET['delimg'])) {
        
        $img = $_GET['delimg'];
        if (is_file('../images/'.basename($img))) {
            unlink($img);
            $_SESSION['MONimg_removed'] = true;
            header('Location:uploads.php');
            exit();
        }
    }



    # extensions
    include 'command.php'

?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Images</title>
<link rel="stylesheet" type="text/css" href="cmstyle.css">
<?php include 'csstheme.php' ?>
<script type="text/javascript"> function conf(form){ return confirm('Delete file?'); }</script>
</head>
<body>
    <style>
        #images{<?php include 'cmcurrentpage.css' ?>}
    </style>
<?php
    
        
    # panel    
    include 'mypanel.php';
    action_confirm()



    ?>
    <div class="main">
        <h1>Images</h1>
        <div id="main-container">
            <div style="height:30px"></div>
            
            <!-- FORM -->
             <form id="imgform" enctype="multipart/form-data" action="uploads.php" method="POST">
                        <input type="hidden" name="MAX_FILE_SIZE" value="<?php
                        

                            $settingsXML = simplexml_load_file('filesinfo/settings.xml');
                            echo $xmls->maximagesize*1000
                        
                        ?>
">
                        <input name="uploaded" type="file" id="uploaded">
                        <input type="submit" id="submitimage">
               </form>
            <!-- END FORM


            
            
            
            Results per page -->
            <div class="results-set">
                            <div class="normal-res">
                                <span style="font-size:12px">Results per page &nbsp;</span>
                                    <?php 
                                        pageResults('images',null)
                                    ?>
                            </div>    
                        </div>
                        
                        
                        
                        
                        
            <!-- Upload images -->            
            <div id="uploadedimages">
            <?php
                
                
                $array_img = array();
                $array_mtime = array();
                
                
                $directory = new DirectoryIterator('../images/');
                
                
                foreach ($directory as $fileinfo) {
                
                        if ($fileinfo->isFile()) {
                
                            $name = $fileinfo->getFilename();
                            $ext = $fileinfo->getExtension();
                            $mtime = $fileinfo->getMtime();
                
                        
                            if (strpos('.png .jpg .gif',$ext) !== false) {
                                
                                $array_img[]= $name;
                                $array_mtime[] = $mtime;
                            }
                        }    
                }
                
                
                rsort($array_mtime);
                $cwd = 'images';
                include 'navinfo.php';
                    
                $c=0;
                $r=0;
                
                foreach($array_mtime as $mt) {
                        
                        foreach($array_img as $imgkey=>$img) {
                                
                            if (filemtime('../images/'.$img) == $mt) {
                                
                                if (($c >= $first) && ($c < $last)){
                                
                                    $r++;
                            
                                    $im = '../images/'.$img;
                                    echo '<div class="wrapper">'.PHP_EOL.' <div class="container"><a href="'.$im.'"><img src="'.$im.'"></a></div>'.PHP_EOL.'<div class="img-options"><a class="img-delete" href="?delimg='.$im.'&amp;max='.$ofp.'" onclick="return conf(this);"><span class="imd">[x]</span>&nbsp; Delete image</a>'.PHP_EOL.'</div></div>'.PHP_EOL;
                                }            
                                $c++;
                                unset ($array_img[$imgkey]);
                            }    
                        }
                }
            ?>
            </div>
            <!-- Upload images END -->
            
            
            <!-- Res nav -->
            <div id="res-nav">
            <?php include 'pagenav.php' ?>
            </div>
            
            
        </div>
    </div>
    <?php
    
    
    # plugins
    include 'execute.php'        
    
    
    
    ?>
</body>
</html>