<?php

    # Upload and set a user icon
    include_once 'data_connect.php';
    include_once 'log.php';
    
        
        
    $settingsXML = simplexml_object('settings.xml','l',null,null);
        
    
    if (isset($_POST['MAX_FILE_SIZE'])) {
        
        
        $tname = time();
        $target_path = "../images/".basename(str_replace(' ','',$_FILES['usericon']['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['usericon']['name'])) {
                        $target_path = "../images/".$tname."-".basename(str_replace(' ','',$_FILES['usericon']['name']));
                        break;
                    }    
                }
            }        
        
            
            if (move_uploaded_file($_FILES['usericon']['tmp_name'], $target_path)) {
            
                $_SESSION['usericon'] = '../images/'.basename(str_replace(' ','',$_FILES['usericon']['name']));
                header('Location:upload_icon.php');
                exit();
            }
        
            else {
                $_SESSION['iconerror'] = true;
                header('Location:upload_icon.php');
                exit();
            }
            
            
        }

        
        
        else {
            
            $_SESSION['iconfiletype'] = true;
            header('Location:upload_icon.php');
            exit();

        }

    }
    
?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Images</title>
<link rel="stylesheet" type="text/css" href="cmstyle.css">
<?php include 'csstheme.php' ?>
<style>
    html,body{min-height:initial}
    #action-confirm{left:30%;right:30%}
</style>
</head>
<body>
<?php
action_confirm()
?>
    <div style="background-image:url('<?php 
        
        
    # display userimage(settings)        
    if (isset($_SESSION['usericon']))
        print $_SESSION['usericon'];
            
            
    else {
        
        $im = get_account($_SESSION['userID']);
        if (is_file('../images/'.$im['urlicon']))
            print '../images/'.$im['urlicon'];
        
        else
            if (is_file($im['icon']))
                print $im['icon'];
        
    }
            ?>
');background-repeat:no-repeat;background-size:cover" class="userimage">
    </div><br>
    <p class="sublab">Upload image</p>
    <div style="background:url('<?php print $im['icon'] ?>');background-size:cover;width:28px;height:32px;float:left;margin-right:10px"></div>    
    <form enctype="multipart/form-data" action="upload_icon.php" method="POST">
            <input type="hidden" name="MAX_FILE_SIZE" value="<?php print $settingsXML->general->maximagesize*1000 ?>">
            <input name="usericon" type="file" id="usericon">
            <input type="submit" id="submitimage">
    </form>
</body>
</html>