From 050781d75575a2c04b355cf13eb52f79114a5ef5 Mon Sep 17 00:00:00 2001 From: Tyler Date: Sun, 14 Oct 2018 23:39:38 -0400 Subject: [PATCH] Initial commit --- .gitignore | 1 + composer.json | 21 +++++ src/Model/Paste.php | 98 +++++++++++++++++++++++ src/Model/PasteSection.php | 21 +++++ src/Pastee.php | 160 +++++++++++++++++++++++++++++++++++++ src/PasteeException.php | 8 ++ 6 files changed, 309 insertions(+) create mode 100644 .gitignore create mode 100644 composer.json create mode 100644 src/Model/Paste.php create mode 100644 src/Model/PasteSection.php create mode 100644 src/Pastee.php create mode 100644 src/PasteeException.php diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..a725465 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +vendor/ \ No newline at end of file diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..816fcdc --- /dev/null +++ b/composer.json @@ -0,0 +1,21 @@ +{ + "name": "pastee/php-pastee", + "description": "PHP API for Paste.ee", + "license": "ISC License", + "authors": [ + { + "name": "Paste.ee", + "email": "admin@paste.ee" + } + ], + "require": { + "guzzlehttp/guzzle": "~6.0", + "nesbot/carbon": "~1.18.0", + "blocktrail/cryptojs-aes-php": "^0.1.0" + }, + "autoload" : { + "psr-4": { + "Pastee\\" : "src/" + } + } +} diff --git a/src/Model/Paste.php b/src/Model/Paste.php new file mode 100644 index 0000000..76c51a0 --- /dev/null +++ b/src/Model/Paste.php @@ -0,0 +1,98 @@ +description = $description; + $this->sections = $sections; + } + + public function withDescription($description) { + $this->description = $description; + return $this; + } + + public function withSection($section, $contents = '', $syntax = 'autodetect') { + if (!($section instanceof PasteSection)) { + $section = new PasteSection($section, $syntax, $contents); + } + $this->sections[] = $section; + return $this; + } + + public function withSections($sections) { + $this->sections = $sections; + return $this; + } + + /** + * Decrypt the paste using CryptoJS AES compatible code. + * + * @param $key + * @return $this + * @throws \Exception + */ + public function decrypt($key) { + if (strlen($key) < 32) { + throw new \Exception('Invalid key length!'); + } + + foreach ($this->sections as $section) { + $section->contents = CryptoJSAES::decrypt($section->contents, $key); + } + + return $this; + } + + public function encrypt($key = null) { + if (is_null($key)) { + // TODO generate key + } + + if (strlen($key) < 32) { + throw new \Exception('Invalid key length!'); + } + + foreach ($this->sections as $section) { + $section->contents = CryptoJSAES::encrypt($section->contents, $key); + } + + return $this; + } + + public static function fromObject($json) { + $paste = new Paste(); + + $paste->id = $json->paste->id; + $paste->description = $json->paste->description; + $paste->encrypted = $json->paste->encrypted; + $paste->views = $json->paste->views; + $paste->created_at = Carbon::parse($json->paste->created_at); + + if ($json->paste->expires_at != null) { + $paste->expires_at = Carbon::parse($json->paste->expires_at); + } + + foreach ($json->paste->sections as $section) { + $paste->sections[$section->id] = new PasteSection($section->name, $section->syntax, $section->contents); + } + + return $paste; + } +} \ No newline at end of file diff --git a/src/Model/PasteSection.php b/src/Model/PasteSection.php new file mode 100644 index 0000000..9eb66d1 --- /dev/null +++ b/src/Model/PasteSection.php @@ -0,0 +1,21 @@ +name = $name; + $this->syntax = $syntax; + $this->contents = $contents; + } + + public function toArray() { + return [ + 'name' => $this->name, + 'syntax' => $this->syntax, + 'contents' => $this->contents + ]; + } +} \ No newline at end of file diff --git a/src/Pastee.php b/src/Pastee.php new file mode 100644 index 0000000..27ec0a7 --- /dev/null +++ b/src/Pastee.php @@ -0,0 +1,160 @@ +key = $key; + + $this->client = new GuzzleClient([ + 'base_url' => self::API_HOST . '/' . self::API_VERSION . '/', + 'headers' => [ + 'X-Auth-Token' => $this->key + ] + ]); + } + + /** + * Authenticate a user for use with the api to attach pastes to their account. + * + * @param $username The user's username. + * @param $password The user's password. + * @param bool|Flag $updateKey Flag whether to update the API Key in this object with the authenticated key. + * @return string The newly created or retrieved application key. + * @throws PasteeException + */ + public function authenticate($username, $password, $updateKey = false) { + try { + $res = $this->client->post(sprintf('users/authenticate', self::API_VERSION), [ + 'json' => [ + 'username' => $username, + 'password' => $password + ] + ]); + + $res = $this->json($res); + + if ($res->success && $updateKey) { + $this->key = $res->key; + } + + return $res->success ? $res->key : false; + } catch (RequestException $e) { + throw new PasteeException($this->json($e->getResponse())->errors); + } + } + + /** + * Submit a new paste to the service. + * + * @param $paste Paste The paste to submit. + * @return string The id returned by the submission. + * @throws PasteeException + */ + public function submit(Paste $paste) { + try { + $arr = []; + + foreach ($paste->sections as $section) { + $arr[] = $section->toArray(); + } + + $res = $this->client->post('pastes', [ + 'json' => [ + 'description' => $paste->description, + 'sections' => $arr + ] + ]); + + $res = $this->json($res); + + return $res->success ? $res->id : false; + } catch (RequestException $e) { + throw new PasteeException($this->json($e->getResponse())->errors); + } + } + + /** + * Get a paste from the API. + * + * @param $id string The paste id. + * @param null $key + * @return Paste The paste information. + * @throws PasteeException + */ + public function get($id, $key = null) { + try { + $res = $this->client->get('pastes/' . $id); + + $paste = Paste::fromObject($this->json($res)); + + if ($key) { + $paste->decrypt($key); + } + + return $paste; + } catch (RequestException $e) { + throw new PasteeException($this->json($e->getResponse())->errors); + } + } + + /** + * Delete a paste which the UserApplication has access to. + * + * @param $id string The paste to delete. + * @return bool Whether the paste was deleted. + * @throws PasteeException + */ + public function delete($id) { + try { + $res = $this->client->delete('pastes/' . $id); + + $res = $this->json($res); + + return $res->success; + } catch (RequestException $e) { + throw new PasteeException($this->json($e->getResponse())->errors); + } + } + + /** + * Transform a guzzle response to JSON. + * + * @param $res \Psr\Http\Message\ResponseInterface The response to convert to json. + * @return object The JSON Object. + */ + private function json($res) { + return json_decode($res->getBody()); + } +} \ No newline at end of file diff --git a/src/PasteeException.php b/src/PasteeException.php new file mode 100644 index 0000000..4187c19 --- /dev/null +++ b/src/PasteeException.php @@ -0,0 +1,8 @@ +errors = $errors; + } +} \ No newline at end of file