Skip to content
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