<?
    
require_once('include/functions/image_directory.php');
    require_once(
'include/functions/filename_extension.php');
    
    
set_time_limit(0);
    
ignore_user_abort(1);

    
// PLACE 1 : 0 = NOT NEW | # = NEW FILE
    // PLACE 2 : 0 = NOT DUPE | # = DUPE FILE NUMBER
    // PLACE 3 : NOTICE TO DISPLAY

    
function process_uploads()
    {
        if (!isset(
$_FILES['picture']) || !isset($_FILES['picture']['name']) || !isset($_FILES['picture']['tmp_name']))
        {
            return array(
00'No File');
        }
        
$extension filename_extension($_FILES['picture']['name']);
        
$allow_mimes = array('image/jpeg''image/gif''image/png');
        if (
$getimagesize = @getimagesize($_FILES['picture']['tmp_name']))
        {
            if (
in_array($getimagesize['mime'], $allow_mimes))
            {
                
$hash sha1_file($_FILES['picture']['tmp_name']);
                
$size filesize($_FILES['picture']['tmp_name']);
                
$dupe mysql_query("SELECT image_id FROM images_hashes WHERE full_hash = '" $hash "' AND full_size = '" $size "'");
                if (
$dupe mysql_fetch_array($dupe))
                {
                    return array(
0$dupe['image_id'], 'Duplicate Image - Filename: ' $_FILES['picture']['name']);
                }
                
$dupe mysql_query("SELECT image_id FROM images_hashes WHERE thumb_hash = '" $hash "' AND thumb_size = '" $size "'");
                if (
$dupe mysql_fetch_array($dupe))
                {
                    return array(
0$dupe['image_id'], 'Duplicate Image - Filename: ' $_FILES['picture']['name']);
                }
                
$dupe mysql_query("SELECT image_id FROM images_hashes WHERE original_hash = '" $hash "' AND original_size = '" $size "'");
                if (
$dupe mysql_fetch_array($dupe))
                {
                    return array(
0$dupe['image_id'], 'Duplicate Image - Filename: ' $_FILES['picture']['name']);
                }
                if (
$getimagesize['mime'] == 'image/jpeg')
                {
                    
$handle = @imagecreatefromjpeg($_FILES['picture']['tmp_name']);
                }
                elseif (
$getimagesize['mime'] == 'image/png')
                {
                    
$handle = @imagecreatefrompng($_FILES['picture']['tmp_name']);
                }
                elseif (
$getimagesize['mime'] == 'image/gif')
                {
                    
$handle = @imagecreatefromgif($_FILES['picture']['tmp_name']);
                }
                if (isset(
$handle) && $handle)
                {
                    if (
$getimagesize[0] >= THUMB_WIDTH && $getimagesize[1] >= THUMB_HEIGHT)
                    {
                        
mysql_query("INSERT INTO images SET user_id = '" $GLOBALS['auth']['id'] . "', posted_on = NOW()");
                        
$picture_id mysql_insert_id();
                        
mysql_query("CREATE TABLE z_" binaries_path($picture_id) . " (`image_id` mediumint(8) unsigned NOT NULL, `full` mediumblob NOT NULL, `thumb` mediumblob NOT NULL, `original` mediumblob NOT NULL, PRIMARY KEY (`image_id`)) ENGINE = MYISAM DEFAULT CHARSET = latin1");
                        
mysql_query("INSERT INTO z_" binaries_path($picture_id) . " SET image_id = '" $picture_id "'");
                        
mysql_query("INSERT INTO images_hashes SET image_id = '" $picture_id "', full_hash = '" $hash "', full_size = '" $size "', original_hash = '" $hash "', original_size = '" $size "'");
                        
$shrink_size shrink_image($handle$_FILES['picture']['tmp_name'], $getimagesize['mime'], IMAGE_MAX_WIDTHIMAGE_MAX_HEIGHT$picture_id);
                        
$thumb_size thumb_image($handle$getimagesize['mime'], THUMB_WIDTHTHUMB_HEIGHT$picture_id);
                        list(
$shrink_width$shrink_height) = $shrink_size;
                        list(
$thumb_width$thumb_height) = $thumb_size;
                        if (
$shrink_width && $shrink_height && $thumb_width && $thumb_height)
                        {
                            
mysql_query("UPDATE images SET width = '" $shrink_width "', height = '" $shrink_height "', file_type = '" $getimagesize['mime'] . "' WHERE id = '" $picture_id "'");
                            return array(
$picture_id0'Image Accepted - Filename: ' $_FILES['picture']['name']);
                        }
                        
mysql_query("DELETE FROM images WHERE id = '" $picture_id "'");
                        
mysql_query("DELETE FROM z_" binaries_path($picture_id) . " WHERE image_id = '" $picture_id "'");
                        
mysql_query("DELETE FROM images_hashes WHERE image_id = '" $picture_id "'");
                        
mysql_query("ALTER TABLE images PACK_KEYS=1 CHECKSUM=0 DELAY_KEY_WRITE=0 AUTO_INCREMENT=1");
                        return array(
00'Corrupt Image - Filename: ' $_FILES['picture']['name']);
                    }
                    return array(
00'Image Too Small - Filename: ' $_FILES['picture']['name']);
                }
                return array(
00'Can Not Create Handle - Filename: ' $_FILES['picture']['name']);
            }
            return array(
00'Invalid Mime Type: ' $getimagesize['mime'] . ' - Filename: ' $_FILES['picture']['name']);
        }
        elseif (
$extension == 'zip')
        {
            if (
function_exists('process_image'))
            {
                
$za = new ZipArchive();
                
$res $za->open($_FILES['picture']['tmp_name']);
                if (
$res === TRUE)
                {
                    
$full_notice '';
                    for (
$i 0$i $za->numFiles$i++)
                    {
                        
$info $za->statIndex($i);
                        if (
$info['size'] < 7500000)
                        {
                            
$im_string $za->getFromIndex($i);
                            
file_put_contents('../temp/' $za->getNameIndex($i), $im_string);
                            if (
$getimagesize = @getimagesize('../temp/' $za->getNameIndex($i)))
                            {
                                if (
in_array($getimagesize['mime'], $allow_mimes))
                                {
                                    list(
$new$dupe$notice) = process_file('../temp/' $za->getNameIndex($i));
                                    
process_image($new$dupe);
                                    
$full_notice .= $notice '<br />';
                                }
                                else
                                {
                                    
$full_notice .= 'Invalid File Type - Filename: ' $za->getNameIndex($i) . ' Mime: ' $getimagesize['mime'] . '<br />';
                                }
                            }
                            else
                            {
                                
$full_notice .= 'Corrupt Image - Filename: ' $za->getNameIndex($i) . '<br />';
                            }
                            @
unlink('../temp/' $za->getNameIndex($i));
                        }
                        else
                        {
                            
$full_notice .= 'File Too Large - Filename: ' $za->getNameIndex($i) . '<br />';
                        }
                    }
                    
$za->close();
                    return array(
00str_replace('../temp/'''$full_notice));
                   }
                return array(
00'Corrupt Zip Archive - Filename: ' $_FILES['picture']['name']);
               }
               return array(
00'You\'re not allowed to upload zip files to this section.');
        }
        return array(
00'Corrupt Image - Filename: ' $_FILES['picture']['name']);
    }
    
    function 
process_file($file)
    {
        if (
$getimagesize = @getimagesize($file))
        {
            if (
$getimagesize['mime'] != 'image/jpeg' && $getimagesize['mime'] != 'image/png' && $getimagesize['mime'] != 'image/gif')
            {
                return array(
00'Invalid File Type - Mime: ' $getimagesize['mime'] . ')');
            }
            
$hash sha1_file($file);
            
$size filesize($file);
            
$dupe mysql_query("SELECT image_id FROM images_hashes WHERE full_hash = '" $hash "' AND full_size = '" $size "'");
            if (
$dupe mysql_fetch_array($dupe))
            {
                return array(
0$dupe['image_id'], 'Duplicate Image - Filename: ' basename($file));
            }
            
$dupe mysql_query("SELECT image_id FROM images_hashes WHERE thumb_hash = '" $hash "' AND thumb_size = '" $size "'");
            if (
$dupe mysql_fetch_array($dupe))
            {
                return array(
0$dupe['image_id'], 'Duplicate Image - Filename: ' basename($file));
            }
            
$dupe mysql_query("SELECT image_id FROM images_hashes WHERE original_hash = '" $hash "' AND original_size = '" $size "'");
            if (
$dupe mysql_fetch_array($dupe))
            {
                return array(
0$dupe['image_id'], 'Duplicate Image - Filename: ' basename($file));
            }
            if (
$getimagesize['mime'] == 'image/jpeg')
            {
                
$handle = @imagecreatefromjpeg($file);
            }
            elseif (
$getimagesize['mime'] == 'image/png')
            {
                
$handle = @imagecreatefrompng($file);
            }
            elseif (
$getimagesize['mime'] == 'image/gif')
            {
                
$handle = @imagecreatefromgif($file);
            }
            if (isset(
$handle) && $handle)
            {
                if (
$getimagesize[0] >= THUMB_WIDTH && $getimagesize[1] >= THUMB_HEIGHT)
                {
                    
mysql_query("INSERT INTO images SET user_id = '" $GLOBALS['auth']['id'] . "', posted_on = NOW()");
                    
$picture_id mysql_insert_id();
                    
mysql_query("CREATE TABLE z_" binaries_path($picture_id) . " (`image_id` mediumint(8) unsigned NOT NULL, `full` mediumblob NOT NULL, `thumb` mediumblob NOT NULL, `original` mediumblob NOT NULL, PRIMARY KEY (`image_id`)) ENGINE = MYISAM DEFAULT CHARSET = latin1");
                    
mysql_query("INSERT INTO z_" binaries_path($picture_id) . " SET image_id = '" $picture_id "'");
                    
mysql_query("INSERT INTO images_hashes SET image_id = '" $picture_id "', full_hash = '" $hash "', full_size = '" $size "', original_hash = '" $hash "', original_size = '" $size "'");
                    
$shrink_size shrink_image($handle$file$getimagesize['mime'], IMAGE_MAX_WIDTHIMAGE_MAX_HEIGHT$picture_id);
                    
$thumb_size thumb_image($handle$getimagesize['mime'], THUMB_WIDTHTHUMB_HEIGHT$picture_id);
                    list(
$shrink_width$shrink_height) = $shrink_size;
                    list(
$thumb_width$thumb_height) = $thumb_size;
                    if (
$shrink_width && $shrink_height && $thumb_width && $thumb_height)
                    {
                        
mysql_query("UPDATE images SET width = '" $shrink_width "', height = '" $shrink_height "', file_type = '" $getimagesize['mime'] . "' WHERE id = '" $picture_id "'");
                        return array(
$picture_id0'Image Accepted (' basename($file) . ')');
                    }
                    
mysql_query("DELETE FROM images WHERE id = '" $picture_id "'");
                    
mysql_query("DELETE FROM z_" binaries_path($picture_id) . " WHERE image_id = '" $picture_id "'");
                    
mysql_query("DELETE FROM images_hashes WHERE image_id = '" $picture_id "'");
                    
mysql_query("ALTER TABLE images PACK_KEYS=1 CHECKSUM=0 DELAY_KEY_WRITE=0 AUTO_INCREMENT=1");
                    return array(
00'Corrupt Image - Filename: ' basename($file));
                }
                return array(
00'Image Too Small - Filename: ' basename($file));
            }
            return array(
00'Corrupt Image - Filename: ' basename($file));
        }
        return array(
00'Invalid File: ' basename($file));
    }

    function 
shrink_image($handle$srcFile$dstType$dstWidth$dstHeight$picture_id$force 0)
    {
        
$srcWidth imagesx($handle);
        
$srcHeight imagesy($handle);
        if (
$srcWidth $dstWidth || $srcHeight $dstHeight || $force)
        {
            if (
$srcWidth $dstWidth)
            {
                
$dstHeight $srcHeight / ($srcWidth $dstWidth);
            }
            elseif (
$srcWidth $dstHeight)
            {
                
$dstWidth $srcWidth  / ($srcHeight $dstHeight);
            }
            else
            {
                
$dstHeight $srcHeight;
                
$dstWidth $srcWidth;
            }
            
$newHandle imagecreatetruecolor($dstWidth$dstHeight);
            if (!
$newHandle)
            {
                return array(
00);
            }
            if (!
imagecopyresampled($newHandle$handle0000$dstWidth$dstHeight$srcWidth$srcHeight))
            {
                return array(
00);
            }
            
$temp make_cache_code();
            if (
$dstType == 'image/jpeg')
            {
                
imagejpeg($newHandle'../temp/' $temp90);
                   
$imgData file_get_contents('../temp/' $temp);
                   
unlink('../temp/' $temp);
            }
            elseif (
$dstType == 'image/png')
            {
                
imagepng($newHandle'../temp/' $temp);
                   
$imgData file_get_contents('../temp/' $temp);
                   
unlink('../temp/' $temp);
            }
            elseif (
$dstType == 'image/gif')
            {
                
imagegif($newHandle'../temp/' $temp);
                   
$imgData file_get_contents('../temp/' $temp);
                   
unlink('../temp/' $temp);
            }
            else
            {
                return array(
00);
            }
            
mysql_query("UPDATE z_" binaries_path($picture_id) . " SET full = '" addslashes($imgData) . "' WHERE image_id = '" $picture_id "'");
            
mysql_query("UPDATE images_hashes SET full_hash = '" sha1($imgData) . "', full_size = '" strlen($imgData) . "' WHERE image_id = '" $picture_id "'");
            
imagedestroy($newHandle);
            return array(
$dstWidth$dstHeight);
        }
        
$imgData file_get_contents($srcFile);
        
mysql_query("UPDATE z_" binaries_path($picture_id) . " SET full = '" addslashes($imgData) . "' WHERE image_id = '" $picture_id "'");
        return array(
$srcWidth$srcHeight);
    }
    
    function 
thumb_image($handle$dstType$dstWidth$dstHeight$picture_id)
    {
        
$srcWidth  imagesx($handle);
        
$srcHeight imagesy($handle);
        if (
$srcWidth >= $dstWidth && $srcHeight >= $dstHeight)
        {
            
$newHandle imagecreatetruecolor($dstWidth$dstHeight);
            if (!
$newHandle)
            {
                return array(
00);
            }
            if (
$srcHeight $srcWidth)
            {
                
$cpyWidth round($dstWidth $srcHeight $dstHeight);
                if (
$cpyWidth $srcWidth)
                {
                    
$cpyWidth $srcWidth;
                    
$cpyHeight round($dstHeight $srcWidth $dstWidth);
                    
$xOffset 0;
                    
$yOffset round(($srcHeight $cpyHeight) / 2);
                }
                else
                {
                    
$cpyHeight $srcHeight;
                    
$xOffset round(($srcWidth $cpyWidth) / 2);
                    
$yOffset 0;
                }
            }
            else
            {
                
$cpyHeight round($dstHeight $srcWidth $dstWidth);
                if (
$cpyHeight $srcHeight)
                {
                    
$cpyHeight $srcHeight;
                    
$cpyWidth round($dstWidth $srcHeight $dstHeight);
                    
$xOffset round(($srcWidth $cpyWidth) / 2);
                    
$yOffset 0;
                }
                else
                {
                    
$cpyWidth $srcWidth;
                    
$xOffset 0;
                    
$yOffset round(($srcHeight $cpyHeight) / 2);
                }
            }
            if (!
imagecopyresampled($newHandle$handle00$xOffset$yOffset$dstWidth$dstHeight$cpyWidth$cpyHeight))
            {
                return array(
00);
            }
            
$temp make_cache_code();
            if (
$dstType == 'image/jpeg')
            {
                
imagejpeg($newHandle'../temp/' $temp90);
                   
$imgData file_get_contents('../temp/' $temp);
                   
unlink('../temp/' $temp);
            }
            elseif (
$dstType == 'image/png')
            {
                
imagepng($newHandle'../temp/' $temp);
                   
$imgData file_get_contents('../temp/' $temp);
                   
unlink('../temp/' $temp);
            }
            elseif (
$dstType == 'image/gif')
            {
                
imagegif($newHandle'../temp/' $temp);
                   
$imgData file_get_contents('../temp/' $temp);
                   
unlink('../temp/' $temp);
            }
            else
            {
                return array(
00);
            }
            
mysql_query("UPDATE z_" binaries_path($picture_id) . " SET thumb = '" addslashes($imgData) . "' WHERE image_id = '" $picture_id "'");
            
mysql_query("UPDATE images_hashes SET thumb_hash = '" sha1($imgData) . "', thumb_size = '" strlen($imgData) . "' WHERE image_id = '" $picture_id "'");
            
imagedestroy($newHandle);
            return array(
$dstWidth$dstHeight);
        }
        else
        {
            return array(
00);
        }
    }
?>