<?php
    define
('ROOT','../');
    require_once(
ROOT.'common.php');
    require_once(
ROOT.'member/common.php');
    
$sitename "Member / Images";
    
$breadcrumbs ' -> <a href="'.ROOT.'member/index.php">Control Panel</a> -> <a href="'.ROOT.'member/image_sub.php">Images</a>';
    if(
$_POST['form1sent'] == '1')
    {
        
$catid $_POST['id'];
        
$imagedesc $_POST['imagedesc'];
        
$imagename $_POST['imagename'];
        if((
strlen($imagename) < 3) || (strlen($imagename) > 30) )
        {
            
$length strlen($imagename);
            
badrequest("Your tutorial name must be between 3 and 30 characters. Your title was ".$length." characters long.");
        }
        else
        {
            if(
strlen($imagedesc) > 65535)
            {
                
$length strlen($imagedesc);
                
badrequest("Your tutorial description must be less than 65535 characters. Your description was ".$length." characters long.");
            }
            else
            {
                
clearstatcache();
                
$size filesize($_FILES['image']['tmp_name']);
                list(
$width,$height,$type)=getimagesize($_FILES['image']['tmp_name']);
                if(
$type != (1||2||3||6) || $size >= 1572864)
                {
                    
badrequest("Your image may only be in the formats gif, jpg, png, or bmp. Your file must also be less then 1.5 megabytes.");
                }
                else
                {
                    
$types = array(
                       
=> 'GIF',
                       
=> 'JPG',
                       
=> 'PNG',
                       
=> 'SWF',
                       
=> 'PSD',
                       
=> 'BMP',
                       
=> 'TIFF(intel byte order)',
                       
=> 'TIFF(motorola byte order)',
                       
=> 'JPC',
                       
10 => 'JP2',
                       
11 => 'JPX',
                       
12 => 'JB2',
                       
13 => 'SWC',
                       
14 => 'IFF',
                       
15 => 'WBMP',
                       
16 => 'XBM'
                   
);
                    
$ext $types[$type];
                    
$uploaddir ROOT."images/";
                    
$time date('ymdHisB');
                    
$uploadfile $uploaddir."/".$time.".".$ext;
                    
$location "http://images.image-space.net/".$time.".".$ext;
                    
move_uploaded_file($_FILES['image']['tmp_name'], $uploadfile);

                    
$catid2 $db->escape($catid);
                    
$imagedesc2 =  $db->escape($imagedesc);
                    
$imagename2 =  $db->escape($imagename);

                    
$db->query("INSERT INTO images
                    (`parentid`,`dateuploaded`,`name`,`description`,`location`, `posterid`)
                    VALUES
                    ('"
.$catid2."',now(),'".$imagename2."','".$imagedesc2."','".$location."','".$_SESSION['id']."')") or badrequest("An error has occured uploading your image");
                    
redirect(ROOT.'member/image_sub.php'"Image uploaded successfully..");
                }
            }
        }
    }
    if (
$_GET['act'] == "delete")
    {
        
$id intval($_GET['id']);
        
$id $db->escape($id);
        
$name $db->query("SELECT id, posterid FROM `images` WHERE `id`='".$id."'") or die($db->error());
        
$array=$db->fetch_array($name) or die($db->error());
        if (
$array['posterid'] != $_SESSION['id'])
        {
            die(
"You cannot delete other users images");
        }
        else
        {
            
$db->query("DELETE FROM `images` WHERE id = '".$id."'");
            
redirect(ROOT.'member/images.php'"Image deleted successfully.");
        }
    }
    require_once(
ROOT.'include/header.php');
    
generate_user_menu("images");
?>


<div class="blockform">
    <div class="tab-page" id="imagePane">
        <script type="text/javascript">
            var tabPane1 = new WebFXTabPane( document.getElementById( "imagePane" ), 1 )
        </script>
        <div class="tab-page" id="help-page">
            <h2 class="tab">Info</h2>
            <script type="text/javascript">
                tabPane1.addTabPage( document.getElementById( "help-page" ) );
            </script>
            <div id="adintro">
                <div class="inbox">
                    <p>
                        You can use this page to add images to your profile and the site. Please make sure you put your images in the correct category.
                    </p>
                </div>
            </div>
        </div>
        <div class="tab-page" id="add-page">
            <h2 class="tab">Add</h2>
            <script type="text/javascript">
                tabPane1.addTabPage( document.getElementById( "add-page" ) );
            </script>
            <div id="adintro">
                <div class="inbox">
                    <form action="image_sub.php" enctype="multipart/form-data" method="post">
                        <input type="hidden" name="form1sent" value="1" />
                        <p>
                            <b>Image Title:</b><br />
                            <input type="text" name="imagename" /><br /><br />
                            <b>Choose the category:</b><br />
                            <?php
    $sql 
'SELECT id, left_id, right_id, level, title FROM testcat ORDER BY left_id';
    
$result mysql_query($sql) or die(mysql_error());
    
$level 1;
    
$classes = array('cat' => array(=> 'maincat''subcat'), 'element' => array(=> 'sub''sub''subcatsub'));
    
$first false;

    print 
'<select class="select" name="id">' "\n";
    while(
$row mysql_fetch_assoc($result))
    {
        while(
$level $row['level'])
        {
            
$level--;
            print 
str_repeat("\t"$level) . '</optgroup>' "\n";
        }
        if(
has_children($row['left_id'], $row['right_id']))
        {
            print 
str_repeat("\t"$row['level']) . '<optgroup class="' $classes['cat'][$row['level']] . '" label="' $row['title'] . '">' "\n";
            
$level++;
            
$first true;
            continue;
        }
        print 
str_repeat("\t"$row['level']) . '<option class="' $classes['element'][$row['level']] . (($first) ? ' first' '') . '" value="' $row['id'] . '">' $row['title'] . '</option>' "\n";
        
$first false;
    }
    while(
$level 1)
    {
        
$level--;
        print 
str_repeat("\t"$level) . '</optgroup>' "\n";
    }

    print 
'</select>&nbsp;<img src="'.ROOT.'img/tooltip.png" alt="" onMouseOver="return overlib(\'Select the category that best fits your image.\');" onMouseOut="return nd();">' "<br />\n";
    
?> <br />
                            <b>Select your file:</b><br />
                            <input type="file" class="fileupload" name="image" />&nbsp;<img src="<?php echo ROOT?>img/tooltip.png" alt="" onMouseOver="return overlib('Browse your computer for your image. This may be in the formats .png, .jpg, .gif, .bmp. File size is limited to 1 megabyte.');" onMouseOut="return nd();"><br />
                            <br />
                            <b>Description:</b><br />
                            <textarea name="imagedesc" cols="40" rows="6"></textarea><br />
                            <br />
                            <input type="submit" value="Upload" />
                        </p>
                    </form>
                </div>
            </div>
        </div>
        <div class="tab-page" id="delete-page">
            <h2 class="tab">Delete</h2>
            <script type="text/javascript">
                tabPane1.addTabPage( document.getElementById( "delete-page" ) );
            </script>
            <div id="adintro">
                <div class="inbox">
                    <p>
                        <?php
                            $mostpage 
"SELECT * FROM images where posterid='".$_SESSION['id']."' ORDER BY dateuploaded DESC";
                            
$result $db->query($mostpage) or die('Queryproblem');
                            if (
$db->num_rows($result) >=1)
                            {
                                echo 
"Please click on the image title to delete it. Make sure you are certain you want to delete the image. Once it is deleted, it is gone forever and can not be returned.<br /><br /><b>Image Title:</b><br />";
                                while (
$array=$db->fetch_assoc($result))
                                {
                                    
$array['description'] = stripslashes($array['description']);
                                    
$array['name'] = stripslashes($array['name']);
                                    
$shortmess dotdotdot ($array['description'],100);
                                    
$shortmess addslashes($shortmess);
                                    
?>
                                    <a href="image_sub.php?act=delete&amp;id=<?php echo $array['id'];?>" onMouseOver="return overlib('<?php echo $shortmess?>');" onMouseOut="return nd();"><?php echo $array['name']?></a><br />
                                    <?php
                                
}
                            }
                            else
                            {
                                echo 
'No news found to delete, please add some.';
                            }
                            
?>
                    </p>
                </div>
            </div>
        </div>
    </div>
</div>
        <?php
        
require_once(ROOT.'include/footer.php');
        
?>