From 4f5eb0877e96f41a4165c2bea9db0e4c24729ce2 Mon Sep 17 00:00:00 2001 From: jamescowie Date: Tue, 18 Nov 2014 00:24:52 +0000 Subject: [PATCH 1/3] Initial image upload --- .gitignore | 1 + code/Controller/PostEdit.php | 32 ++++-- composer.json | 3 +- composer.lock | 188 +++++++++++++++++++++++++++++++---- template/post_edit.html.twig | 6 +- 5 files changed, 199 insertions(+), 31 deletions(-) diff --git a/.gitignore b/.gitignore index 605499c..763ef29 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,4 @@ /data/*.json /skin/.sass-cache /.vagrant +bower_components/ diff --git a/code/Controller/PostEdit.php b/code/Controller/PostEdit.php index daaa4c5..5768b2e 100644 --- a/code/Controller/PostEdit.php +++ b/code/Controller/PostEdit.php @@ -1,5 +1,8 @@ base64_encode($image)); + + $curl = curl_init(); + curl_setopt($curl, CURLOPT_URL, 'https://api.imgur.com/3/image.json'); + curl_setopt($curl, CURLOPT_TIMEOUT, 30); + curl_setopt($curl, CURLOPT_HTTPHEADER, array('Authorization: Client-ID ' . 'becc794036ea803')); + curl_setopt($curl, CURLOPT_POST, 1); + curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); + curl_setopt($curl, CURLOPT_POSTFIELDS, $imagePost); + $out = curl_exec($curl); + curl_close ($curl); + + $returnData = json_decode($out,true); + $imageUrl = $returnData['data']['link']; + $post->set('subject', $subject) ->set('body', $body) ->set('tag_ids', $tagIds) @@ -48,5 +65,4 @@ public function post($postId) header("Location: " . $post->getUrl()); } - -} \ No newline at end of file +} diff --git a/composer.json b/composer.json index 93ad6de..8e20077 100644 --- a/composer.json +++ b/composer.json @@ -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", + "guzzle/guzzle": "3.9.*@dev" }, "autoload": {"psr-0": {"": "code"}} } diff --git a/composer.lock b/composer.lock index 3cf1ec6..85414e0 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at http://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file", "This file is @generated automatically" ], - "hash": "d87132a53a07b93478d3d3e8873e5616", + "hash": "c2b5a25ff4c5a9ce38f8fe655c0fe309", "packages": [ { "name": "container-interop/container-interop", @@ -228,12 +228,12 @@ "source": { "type": "git", "url": "https://github.com/erusev/parsedown.git", - "reference": "da5d75e97e1ed19e57bd54fa6cb595a6a0879a67" + "reference": "7000cbc2d29ba0ab303496300600d66fc2bc98ac" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/erusev/parsedown/zipball/da5d75e97e1ed19e57bd54fa6cb595a6a0879a67", - "reference": "da5d75e97e1ed19e57bd54fa6cb595a6a0879a67", + "url": "https://api.github.com/repos/erusev/parsedown/zipball/7000cbc2d29ba0ab303496300600d66fc2bc98ac", + "reference": "7000cbc2d29ba0ab303496300600d66fc2bc98ac", "shasum": "" }, "type": "library", @@ -259,7 +259,7 @@ "markdown", "parser" ], - "time": "2014-10-29 20:29:46" + "time": "2014-11-12 20:27:29" }, { "name": "ezyang/htmlpurifier", @@ -305,6 +305,98 @@ ], "time": "2014-10-23 22:36:02" }, + { + "name": "guzzle/guzzle", + "version": "dev-master", + "source": { + "type": "git", + "url": "https://github.com/guzzle/guzzle3.git", + "reference": "5e6fb7097efac7180bd75006079144025255eb63" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/guzzle/guzzle3/zipball/5e6fb7097efac7180bd75006079144025255eb63", + "reference": "5e6fb7097efac7180bd75006079144025255eb63", + "shasum": "" + }, + "require": { + "ext-curl": "*", + "php": ">=5.3.3", + "symfony/event-dispatcher": "~2.1" + }, + "replace": { + "guzzle/batch": "self.version", + "guzzle/cache": "self.version", + "guzzle/common": "self.version", + "guzzle/http": "self.version", + "guzzle/inflection": "self.version", + "guzzle/iterator": "self.version", + "guzzle/log": "self.version", + "guzzle/parser": "self.version", + "guzzle/plugin": "self.version", + "guzzle/plugin-async": "self.version", + "guzzle/plugin-backoff": "self.version", + "guzzle/plugin-cache": "self.version", + "guzzle/plugin-cookie": "self.version", + "guzzle/plugin-curlauth": "self.version", + "guzzle/plugin-error-response": "self.version", + "guzzle/plugin-history": "self.version", + "guzzle/plugin-log": "self.version", + "guzzle/plugin-md5": "self.version", + "guzzle/plugin-mock": "self.version", + "guzzle/plugin-oauth": "self.version", + "guzzle/service": "self.version", + "guzzle/stream": "self.version" + }, + "require-dev": { + "doctrine/cache": "~1.3", + "monolog/monolog": "~1.0", + "phpunit/phpunit": "3.7.*", + "psr/log": "~1.0", + "symfony/class-loader": "~2.1", + "zendframework/zend-cache": "2.*,<2.3", + "zendframework/zend-log": "2.*,<2.3" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "3.9-dev" + } + }, + "autoload": { + "psr-0": { + "Guzzle": "src/", + "Guzzle\\Tests": "tests/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Michael Dowling", + "email": "mtdowling@gmail.com", + "homepage": "https://github.com/mtdowling" + }, + { + "name": "Guzzle Community", + "homepage": "https://github.com/guzzle/guzzle/contributors" + } + ], + "description": "Guzzle is a PHP HTTP client library and framework for building RESTful web service clients", + "homepage": "http://guzzlephp.org/", + "keywords": [ + "client", + "curl", + "framework", + "http", + "http client", + "rest", + "web service" + ], + "time": "2014-10-29 23:08:23" + }, { "name": "j7mbo/twitter-api-php", "version": "dev-master", @@ -408,16 +500,16 @@ }, { "name": "mnapoli/php-di", - "version": "4.4.0", + "version": "4.4.2", "source": { "type": "git", "url": "https://github.com/mnapoli/PHP-DI.git", - "reference": "556e3be63a821bbad0585a34f634080305392252" + "reference": "0e267e20226589105ac3c6ae9d893cfbc350a193" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/mnapoli/PHP-DI/zipball/556e3be63a821bbad0585a34f634080305392252", - "reference": "556e3be63a821bbad0585a34f634080305392252", + "url": "https://api.github.com/repos/mnapoli/PHP-DI/zipball/0e267e20226589105ac3c6ae9d893cfbc350a193", + "reference": "0e267e20226589105ac3c6ae9d893cfbc350a193", "shasum": "" }, "require": { @@ -454,7 +546,7 @@ "dependency injection", "di" ], - "time": "2014-10-14 06:13:26" + "time": "2014-11-10 01:37:46" }, { "name": "mnapoli/phpdocreader", @@ -631,6 +723,63 @@ ], "time": "2014-09-28 14:18:11" }, + { + "name": "symfony/event-dispatcher", + "version": "v2.5.6", + "target-dir": "Symfony/Component/EventDispatcher", + "source": { + "type": "git", + "url": "https://github.com/symfony/EventDispatcher.git", + "reference": "804eb28dbbfba9ffdab21fe2066744906cea2212" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/symfony/EventDispatcher/zipball/804eb28dbbfba9ffdab21fe2066744906cea2212", + "reference": "804eb28dbbfba9ffdab21fe2066744906cea2212", + "shasum": "" + }, + "require": { + "php": ">=5.3.3" + }, + "require-dev": { + "psr/log": "~1.0", + "symfony/config": "~2.0", + "symfony/dependency-injection": "~2.0,<2.6.0", + "symfony/stopwatch": "~2.2" + }, + "suggest": { + "symfony/dependency-injection": "", + "symfony/http-kernel": "" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "2.5-dev" + } + }, + "autoload": { + "psr-0": { + "Symfony\\Component\\EventDispatcher\\": "" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Symfony Community", + "homepage": "http://symfony.com/contributors" + }, + { + "name": "Fabien Potencier", + "email": "fabien@symfony.com" + } + ], + "description": "Symfony EventDispatcher Component", + "homepage": "http://symfony.com", + "time": "2014-10-01 15:43:05" + }, { "name": "torophp/torophp", "version": "dev-master", @@ -712,12 +861,12 @@ "source": { "type": "git", "url": "https://github.com/twigphp/Twig.git", - "reference": "f8b6e918a7d8e073efe14cbed7ae02df40ef19ed" + "reference": "b87cf88f88feee256c1e1ca85ec6677f38fcc238" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/twigphp/Twig/zipball/f8b6e918a7d8e073efe14cbed7ae02df40ef19ed", - "reference": "f8b6e918a7d8e073efe14cbed7ae02df40ef19ed", + "url": "https://api.github.com/repos/twigphp/Twig/zipball/b87cf88f88feee256c1e1ca85ec6677f38fcc238", + "reference": "b87cf88f88feee256c1e1ca85ec6677f38fcc238", "shasum": "" }, "require": { @@ -761,7 +910,7 @@ "keywords": [ "templating" ], - "time": "2014-10-23 15:29:03" + "time": "2014-11-10 14:48:15" }, { "name": "zendframework/zend-code", @@ -913,12 +1062,12 @@ "source": { "type": "git", "url": "https://github.com/zendframework/zf1.git", - "reference": "9b9fda8329782926742d125bac2868748a299d0a" + "reference": "81f12348822ded3811b26ed38afef8cb6995fd1f" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/zendframework/zf1/zipball/9b9fda8329782926742d125bac2868748a299d0a", - "reference": "9b9fda8329782926742d125bac2868748a299d0a", + "url": "https://api.github.com/repos/zendframework/zf1/zipball/81f12348822ded3811b26ed38afef8cb6995fd1f", + "reference": "81f12348822ded3811b26ed38afef8cb6995fd1f", "shasum": "" }, "require": { @@ -952,7 +1101,7 @@ "ZF1", "framework" ], - "time": "2014-10-30 22:01:31" + "time": "2014-11-14 10:32:25" } ], "packages-dev": [], @@ -964,7 +1113,8 @@ "zendframework/zendframework1": 20, "j7mbo/twitter-api-php": 20, "erusev/parsedown": 20, - "ezyang/htmlpurifier": 20 + "ezyang/htmlpurifier": 20, + "guzzle/guzzle": 20 }, "prefer-stable": false, "platform": [], diff --git a/template/post_edit.html.twig b/template/post_edit.html.twig index 1ee3abb..5fb1fb3 100644 --- a/template/post_edit.html.twig +++ b/template/post_edit.html.twig @@ -1,7 +1,7 @@ {% extends 'base.html.twig' %} {% block content %} -
+
+
@@ -39,4 +39,4 @@
-{% endblock %} \ No newline at end of file +{% endblock %} From 17972c9b0b9ac299b558bc3c61e5ea2c02abcf49 Mon Sep 17 00:00:00 2001 From: jamescowie Date: Tue, 18 Nov 2014 18:39:03 +0000 Subject: [PATCH 2/3] Remove todo comment from another issue --- template/post.html.twig | 1 - 1 file changed, 1 deletion(-) diff --git a/template/post.html.twig b/template/post.html.twig index ed7aaff..0c0619e 100644 --- a/template/post.html.twig +++ b/template/post.html.twig @@ -29,7 +29,6 @@ {% endfor %} -
{{ post.getBodyAsHtml() | raw }} {% if post.getImageUrl() %} From f9e0d09a14bb0f668171080a559d2ef85c47d158 Mon Sep 17 00:00:00 2001 From: jamescowie Date: Tue, 18 Nov 2014 20:23:36 +0000 Subject: [PATCH 3/3] Remove CURL and implement config lookup for settings --- code/Controller/PostEdit.php | 52 +++++++++++++++++++------------- composer.json | 2 +- composer.lock | 58 +++++++++++++++++++++++++++++++----- etc/config.json.sample | 4 ++- 4 files changed, 86 insertions(+), 30 deletions(-) diff --git a/code/Controller/PostEdit.php b/code/Controller/PostEdit.php index 5768b2e..04bb3c7 100644 --- a/code/Controller/PostEdit.php +++ b/code/Controller/PostEdit.php @@ -1,7 +1,7 @@ 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; @@ -37,24 +36,12 @@ public function post($postId) die("Permission denied"); } - $fileName = $_FILES['image_url']['tmp_name']; - $handle = fopen($fileName, 'r'); - $image = fread($handle, filesize($fileName)); - $imagePost = array('image' => base64_encode($image)); - - $curl = curl_init(); - curl_setopt($curl, CURLOPT_URL, 'https://api.imgur.com/3/image.json'); - curl_setopt($curl, CURLOPT_TIMEOUT, 30); - curl_setopt($curl, CURLOPT_HTTPHEADER, array('Authorization: Client-ID ' . 'becc794036ea803')); - curl_setopt($curl, CURLOPT_POST, 1); - curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); - curl_setopt($curl, CURLOPT_POSTFIELDS, $imagePost); - $out = curl_exec($curl); - curl_close ($curl); - - $returnData = json_decode($out,true); - $imageUrl = $returnData['data']['link']; - + 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) @@ -65,4 +52,27 @@ 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; + } + } diff --git a/composer.json b/composer.json index 8e20077..f876b18 100644 --- a/composer.json +++ b/composer.json @@ -9,7 +9,7 @@ "j7mbo/twitter-api-php": "dev-master", "erusev/parsedown": "dev-master", "ezyang/htmlpurifier": "dev-master", - "guzzle/guzzle": "3.9.*@dev" + "adyg/php-imgur-api-client": "dev-master" }, "autoload": {"psr-0": {"": "code"}} } diff --git a/composer.lock b/composer.lock index 85414e0..b0573fe 100644 --- a/composer.lock +++ b/composer.lock @@ -4,8 +4,52 @@ "Read more about it at http://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file", "This file is @generated automatically" ], - "hash": "c2b5a25ff4c5a9ce38f8fe655c0fe309", + "hash": "2b3fb4d6092eab27fcf0c23d9078f1fb", "packages": [ + { + "name": "adyg/php-imgur-api-client", + "version": "dev-master", + "source": { + "type": "git", + "url": "https://github.com/Adyg/php-imgur-api-client.git", + "reference": "aa292b47b10aa555e4678e3637054a2dbb9ec53a" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/Adyg/php-imgur-api-client/zipball/aa292b47b10aa555e4678e3637054a2dbb9ec53a", + "reference": "aa292b47b10aa555e4678e3637054a2dbb9ec53a", + "shasum": "" + }, + "require": { + "ext-curl": "*", + "guzzle/guzzle": ">=3.7", + "php": ">=5.3.2" + }, + "type": "library", + "autoload": { + "psr-0": { + "Imgur\\": "lib/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Adrian Ghiuta", + "email": "adrian.ghiuta@gmail.com", + "homepage": "http://adyg.github.com" + } + ], + "description": "Imgur API v3 client", + "homepage": "https://github.com/Adyg/php-imgur-api-client", + "keywords": [ + "api", + "imgur" + ], + "time": "2014-07-11 18:00:11" + }, { "name": "container-interop/container-interop", "version": "1.0.0", @@ -307,16 +351,16 @@ }, { "name": "guzzle/guzzle", - "version": "dev-master", + "version": "v3.9.2", "source": { "type": "git", "url": "https://github.com/guzzle/guzzle3.git", - "reference": "5e6fb7097efac7180bd75006079144025255eb63" + "reference": "54991459675c1a2924122afbb0e5609ade581155" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/guzzle/guzzle3/zipball/5e6fb7097efac7180bd75006079144025255eb63", - "reference": "5e6fb7097efac7180bd75006079144025255eb63", + "url": "https://api.github.com/repos/guzzle/guzzle3/zipball/54991459675c1a2924122afbb0e5609ade581155", + "reference": "54991459675c1a2924122afbb0e5609ade581155", "shasum": "" }, "require": { @@ -395,7 +439,7 @@ "rest", "web service" ], - "time": "2014-10-29 23:08:23" + "time": "2014-08-11 04:32:36" }, { "name": "j7mbo/twitter-api-php", @@ -1114,7 +1158,7 @@ "j7mbo/twitter-api-php": 20, "erusev/parsedown": 20, "ezyang/htmlpurifier": 20, - "guzzle/guzzle": 20 + "adyg/php-imgur-api-client": 20 }, "prefer-stable": false, "platform": [], diff --git a/etc/config.json.sample b/etc/config.json.sample index 26d55da..873ac99 100644 --- a/etc/config.json.sample +++ b/etc/config.json.sample @@ -14,5 +14,7 @@ "twitter_consumer_api_key" : "", "twitter_consumer_api_secret" : "", "twitter_oauth_access_token" : "", - "twitter_oauth_access_token_secret" : "" + "twitter_oauth_access_token_secret" : "", + "imgur_client_id" : "", + "imgur_client_secret" : "" }