<?php
    require_once('include/functions/create_quilt_image_cache.php');

    $_id = isset($_GET['i']) ? intval($_GET['i']) : '';
    $_x = isset($_GET['x']) ? intval($_GET['x']) : '';
    $_y = isset($_GET['y']) ? intval($_GET['y']) : '';
    $_comment = isset($_POST['comment']) ? trim(strval($_POST['comment'])) : '';

    $tiles_pending = mysqli_query_logged("SELECT * FROM tiles_pending WHERE quilt_id = " . sq($_id) . " AND matrix_x = " . sq($_x) . " AND matrix_y = " . sq($_y) . " AND user_id = '" . $GLOBALS['auth']['id'] . "'");
    if ($tiles_pending_row = mysqli_fetch_assoc($tiles_pending))
    {
        $quilts = mysqli_query_logged("SELECT * FROM quilts WHERE id = " . sq($_id));
        if ($quilts_row = mysqli_fetch_assoc($quilts))
        {
            if (!isset($_FILES['tile']))
            {
                make_cookie('notice', 'No File');
            }
            if ($_FILES['tile']['type'] == '')
            {
                make_cookie('notice', 'Invalid File Type');
            }
            elseif ($_FILES['tile']['type'] != 'image/png')
            {
                make_cookie('notice', 'Invalid File Type');
            }
            $handle = @imagecreatefrompng($_FILES['tile']['tmp_name']);
            if ($handle)
            {
                $srcWidth = imagesx($handle);
                $srcHeight = imagesy($handle);
                if ($srcWidth && $srcHeight)
                {
                    if ($srcWidth == $quilts_row['tile_width'] + $quilts_row['side_pixels'] * 2)
                    {
                        if ($srcHeight == $quilts_row['tile_height'] + $quilts_row['side_pixels'] * 2)
                        {
                            $newHandle = imagecreatetruecolor($quilts_row['tile_width'], $quilts_row['tile_height']);
                            imagecopy($newHandle, $handle, 0, 0, $quilts_row['side_pixels'], $quilts_row['side_pixels'], $quilts_row['tile_width'], $quilts_row['tile_height']);
                            if (1)
                            {
                                $temp = make_cache_code();
                                imagepng($newHandle, 'temp/' . $temp);
                                $imgData = file_get_contents('temp/' . $temp);
                                unlink('temp/' . $temp);
                            }
                            else
                            {
                                ob_start();
                                imagepng($newHandle);
                                $imgData = ob_get_contents();
                                ob_end_clean();
                            }
                            list($date, $hours) = split(' ', $tiles_pending_row['started_on']);
                            list($year, $month, $day) = split('-', $date);
                            list($hour, $min, $sec) = split(':', $hours);
                            $date_start = mktime($hour, $min, $sec, $month, $day, $year);
                            $submit_date = date('Y-m-d H:i:s');
                            list($date, $hours) = split(' ', $submit_date);
                            list($year, $month, $day) = split('-', $date);
                            list($hour, $min, $sec) = split(':', $hours);                        
                            $date_end = mktime($hour, $min, $sec, $month, $day, $year);
                            $seconds = $date_end - $date_start;
                            mysqli_query_logged("INSERT INTO tiles SET quilt_id = " . sq($_id) . ", matrix_x = " . sq($_x) . ", matrix_y = " . sq($_y) . ", user_id = '" . $GLOBALS['auth']['id'] . "', comment = " . sq($_comment) . ", started_on = '" . $tiles_pending_row['started_on'] . "', posted_on = '" . $submit_date . "', seconds = '" . $seconds . "', borders = '" . $tiles_pending_row['borders'] . "'," . ($quilts_row['moderated'] ? " visibility = '-1'," : "") . " data_tile = '" . addslashes($imgData) . "'");
                            mysqli_query_logged("DELETE FROM tiles_pending WHERE quilt_id = " . sq($_id) . " AND matrix_x = " . sq($_x) . " AND matrix_y = " . sq($_y) . " AND user_id = '" . $GLOBALS['auth']['id'] . "'");
                            mysqli_query_logged("UPDATE quilts SET modified_on = NOW() WHERE id = " . sq($_id));
                            create_quilt_image_cache($_id);
                            make_cookie('notice', 'Tile Uploaded');
                        }
                        else
                        {
                            make_cookie('notice', 'The height of the tile you are trying to upload is the wrong size.');
                        }
                    }
                    else
                    {
                        make_cookie('notice', 'The width of the tile you are trying to upload is the wrong size.');
                    }
                }
                else
                {
                    make_cookie('notice', 'Corrupt Image');
                }
            }
            else
            {
                make_cookie('notice', 'Corrupt Image');
            }
        }
    }
    if ($_back)
    {
        header('Location: ./?' . $_back);
        die;
    }
    else
    {
        header('Location: ./?s=quilt&i=' . $_id);
        die;
    }
?>