Example #1
0
function main()
{
    if (isset($_FILES['picturefile']['name'])) {
        $referersplit = preg_split("/[?]/", $_SERVER['HTTP_REFERER']);
        $referer = $referersplit[0];
        try {
            if ($_FILES["picturefile"]["size"] > 5 * 1024 * 1024 || $_FILES['picturefile']['tmp_name'] == null) {
                throw new Exception('File too large!');
            } else {
                if (getContentType($_FILES['picturefile']['name']) == null) {
                    throw new Exception('File type not supported!');
                } else {
                    $filename = generateUniqueId() . "-" . $_FILES['picturefile']['name'];
                    $tmpName = $_FILES['picturefile']['tmp_name'];
                    $image = new SimpleImage();
                    $image->load($tmpName);
                    $imageWasResized = false;
                    if ($image->getHeight() > 1024) {
                        $image->resizeToHeight(1024);
                    }
                    if ($image->getWidth() > 1024) {
                        $image->resizeToWidth(1024);
                    }
                    $image->save($tmpName);
                    // Saving even if not resized, to reduce compression level of file
                    $fp = fopen($tmpName, 'r');
                    $content = fread($fp, filesize($tmpName));
                    fclose($fp);
                    updateOrInsertImage($filename, $content);
                }
            }
            header('Location: ' . $referer . "?uploadresult=true&filelocation=php/io.php?file=" . $filename);
            return true;
        } catch (Exception $e) {
            header('Location: ' . $referer . "?uploadresult=false&errormsg=" . $e->getMessage());
            return true;
        }
    }
    if (isset($_GET['id'])) {
        $slideshowId = $_GET['id'];
        $slideshowSrc = getSlideshow($slideshowId);
        $slideshow = array('id' => $slideshowId, 'src' => $slideshowSrc);
        sendJSONResponse(json_encode($slideshow));
        return true;
    }
    if (isset($_POST['id'], $_POST['key'], $_POST['src'])) {
        $slideshowId = $_POST['id'];
        $slideshowKey = $_POST['key'];
        $slideshowToSave = $_POST['src'];
        if (isCorrectKey($slideshowId, $slideshowKey)) {
            updateSlideshow($slideshowId, $slideshowToSave);
        } else {
            throw new Exception("ERROR key is wrong");
        }
        $result = array('id' => $slideshowId);
        sendJSONResponse(json_encode($result));
        return true;
    }
    if (isset($_POST['create'])) {
        $id = generateUniqueId();
        $key = generateRandomLegibleString();
        createEmptySlideshow($id, $key);
        $idAndKey = array('id' => $id, 'key' => $key);
        sendJSONResponse(json_encode($idAndKey));
        return true;
    }
    if (isset($_GET['file'])) {
        $imageId = $_GET['file'];
        $image = getImage($imageId);
        header("Content-type: " . getContentType($imageId));
        print $image;
        return true;
    }
    return false;
}
Example #2
0
function main()
{
    if (isset($_FILES['picturefile']['name'])) {
        $referersplit = preg_split("/[?]/", $_SERVER['HTTP_REFERER']);
        $referer = $referersplit[0];
        try {
            if ($_FILES["picturefile"]["size"] > 5 * 1024 * 1024) {
                throw new Exception('File too large!');
            } else {
                $filename = generateUniqueId() . "-" . $_FILES['picturefile']['name'];
                $filelocation = "uploaded_files/" . $filename;
                $uploadresult = move_uploaded_file($_FILES['picturefile']['tmp_name'], "../" . $filelocation);
                if (!$uploadresult) {
                    throw new Exception('Error when saving file!');
                }
                $image = new SimpleImage();
                $image->load("../" . $filelocation);
                $imageWasResized = false;
                if ($image->getHeight() > 1024) {
                    $image->resizeToHeight(1024);
                }
                if ($image->getWidth() > 1024) {
                    $image->resizeToWidth(1024);
                }
                $image->save("../" . $filelocation);
                // Saving even if not resized, to reduce compression level of file
            }
            header('Location: ' . $referer . "?uploadresult=true&filelocation=" . $filelocation);
            return true;
        } catch (Exception $e) {
            header('Location: ' . $referer . "?uploadresult=false&errormsg=" . $e->getMessage());
            return true;
        }
    }
    if (isset($_GET['id'])) {
        $slideshowId = $_GET['id'];
        $slideshowSrc = getSlideshow($slideshowId);
        $slideshow = array('id' => $slideshowId, 'src' => $slideshowSrc);
        sendJSONResponse(json_encode($slideshow));
        return true;
    }
    if (isset($_POST['id'], $_POST['key'], $_POST['src'])) {
        $slideshowId = $_POST['id'];
        $slideshowKey = $_POST['key'];
        $slideshowToSave = $_POST['src'];
        if (isCorrectKey($slideshowId, $slideshowKey)) {
            updateSlideshow($slideshowId, $slideshowToSave);
        } else {
            throw new Exception("ERROR key is wrong");
        }
        $result = array('id' => $slideshowId);
        sendJSONResponse(json_encode($result));
        return true;
    }
    if (isset($_POST['create'])) {
        $id = generateUniqueId();
        $key = generateRandomLegibleString();
        createEmptySlideshow($id, $key);
        $idAndKey = array('id' => $id, 'key' => $key);
        sendJSONResponse(json_encode($idAndKey));
        return true;
    }
    return false;
}