Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initial image upload #79

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 33 additions & 7 deletions code/Controller/PostEdit.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
<?php


use Imgur\Client;

class Controller_PostEdit extends Controller_Abstract
{
public function get($postId)
Expand All @@ -17,17 +20,12 @@ public function get($postId)

public function post($postId)
{
$imageUrl = isset($_POST['image_url']) ? $_POST['image_url'] : null;
$imageUrl = ($_FILES['image_url']['size'] > 0) ? $_FILES['image_url'] : null;
$subject = isset($_POST['subject']) ? $_POST['subject'] : null;
$body = isset($_POST['body']) ? $_POST['body'] : null;
$tagIds = isset($_POST['tag_ids']) ? $_POST['tag_ids'] : null;
$isActive = isset($_POST['is_active']) ? $_POST['is_active'] : null;

if ($imageUrl) {
if (strpos($imageUrl, "javascript:") !== false || strpos($imageUrl, "data:") !== false) {
die("Looks like an injection attempt");
}
}

if (! $tagIds || empty($tagIds)) {
die("You have to pick at least one tag");
Expand All @@ -38,6 +36,12 @@ public function post($postId)
die("Permission denied");
}

if ($imageUrl) {
$fileName = $_FILES['image_url']['tmp_name'];
$response = $this->uploadImage($fileName);
$imageUrl = $response['link'];
}

$post->set('subject', $subject)
->set('body', $body)
->set('tag_ids', $tagIds)
Expand All @@ -49,4 +53,26 @@ public function post($postId)
header("Location: " . $post->getUrl());
}

}
/**
* @param $fileName
* @return mixed
* @throws \Imgur\InvalidArgumentException
*/
private function uploadImage($fileName)
{
$client = new Client();
$client->setOption('client_id', $this->_getConfigData('imgur_client_id'));
$client->setOption('client_secret', $this->_getConfigData('imgur_client_secret'));

$imageData = array(
'image' => $fileName,
'type' => 'file'
);

$basic = $client->api('image')->upload($imageData);
$response = $basic->getData();

return $response;
}

}
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
"zendframework/zendframework1": "dev-master",
"j7mbo/twitter-api-php": "dev-master",
"erusev/parsedown": "dev-master",
"ezyang/htmlpurifier": "dev-master"
"ezyang/htmlpurifier": "dev-master",
"adyg/php-imgur-api-client": "dev-master"
},
"autoload": {"psr-0": {"": "code"}}
}
Loading