upload_icon.php

File Type: php File Location: monofiles/ File name: upload_icon.php
<?php

    
// Upload images and show them in settings as userimage
    // and in theme as background image. 
    
    
include_once 'data_connect.php';
    include_once 
'log.php';
    
    
//mypanel stuff included here
    
libxml_clear_errors();
    
libxml_use_internal_errors(true);
        
    if (!
file_exists('filesinfo/settings.xml'))    
        exit(
'Could not load page. Settings file is missing');
        
    
$settingsXML simplexml_load_file('filesinfo/settings.xml');
        
    if (
$settingsXML === false)
        exit(
'Error: Settings file is corrupt');
    
    
// Use this parameter to reach the right page after an action
    // i.e upload an image as usericon and return to user icon upload form
    
$prm = (isset($_GET['backgroundset'])) ? '?backgroundset=1' '';
    
    
    if (isset(
$_POST['MAX_FILE_SIZE'])) {

        
$tname time();
    
        
$target_path "../images/".basename$_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$_FILES['usericon']['name']);
                        break;
                    }    
                }
            }        
        
            if (
move_uploaded_file($_FILES['usericon']['tmp_name'], $target_path)) {
            
                if (isset(
$_GET['backgroundset']))
                    
$_SESSION['backgroundicon'] = '../images/'.basename$_FILES['usericon']['name']);
                
                else
                    
$_SESSION['usericon'] = '../images/'.basename$_FILES['usericon']['name']);
                
                
header("Location:upload_icon.php$prm");
                exit();
            }
        
            else {
            
$_SESSION['imgupload'] = '<div id="error-confirm"><span style="position:relative; top:-5px">An Error was found while uploading a file.</span></div>';
            
header("Location:upload_icon.php$prm");
            exit();
            }
        }

        else {
        
$_SESSION['imgupload'] = '<div id="error-confirm"><span style="position:relative; top:-5px">A file type was incorrect. File extensions allowed: .gif, .jpg, .png</span></div>';
        
header("Location:upload_icon.php$prm");
        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' ?>
</head>
<body>
<?php

        
if (isset($_SESSION['imgupload'])) {
            echo 
$_SESSION['imgupload'];            
            unset (
$_SESSION['imgupload']);
        }
        
        
        if (isset(
$message))
            echo 
$message
?>
<img src="<?php 
// display userimage(settings) or background image(theme)
// root is not needed when file name is saved in session.
    
$imgrot '../images/';
    if (isset(
$_GET['backgroundset'])) {
        
        if (isset(
$_SESSION['backgroundicon']))
            print 
$_SESSION['backgroundicon'];
        else{if (
is_file($imgrot.$settingsXML->general->monoback))
        print 
$imgrot.$settingsXML->general->monoback;
        else print 
'images/nofile.jpg';
        }
    }
    
    else 
        
        if (isset(
$_SESSION['usericon']))
            print 
$_SESSION['usericon'];
        else{if (
is_file($imgrot.get_account($_SESSION['userID'])['icon']))
            print 
$imgrot.get_account($_SESSION['userID'])['icon'];
            else print 
'images/nofile.jpg';
        }
?>" class="userimage">
<form enctype="multipart/form-data" action="upload_icon.php<?php print $prm ?>" 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>