Initial commit

This commit is contained in:
Tyler 2018-10-14 23:39:38 -04:00
commit 050781d755
6 changed files with 309 additions and 0 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
vendor/

21
composer.json Normal file
View File

@ -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/"
}
}
}

98
src/Model/Paste.php Normal file
View File

@ -0,0 +1,98 @@
<?php namespace Pastee\Model;
use Blocktrail\CryptoJSAES\CryptoJSAES;
use Carbon\Carbon;
class Paste {
public $id;
public $description;
public $encrypted;
public $views;
public $sections;
public $created_at;
public $expires_at = null;
public function __construct($description = '', $sections = [], $options = []) {
$this->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;
}
}

View File

@ -0,0 +1,21 @@
<?php namespace Pastee\Model;
class PasteSection {
public $name;
public $syntax = 'autodetect';
public $contents;
public function __construct($name, $syntax = 'autodetect', $contents = '') {
$this->name = $name;
$this->syntax = $syntax;
$this->contents = $contents;
}
public function toArray() {
return [
'name' => $this->name,
'syntax' => $this->syntax,
'contents' => $this->contents
];
}
}

160
src/Pastee.php Normal file
View File

@ -0,0 +1,160 @@
<?php namespace Pastee;
use GuzzleHttp\Client as GuzzleClient;
use GuzzleHttp\Exception\RequestException;
use Pastee\Model\Paste;
/**
* Pastee PHP API.
*
* TODO handle errors correctly and give meaningful responses. THIS IS IMPORTANT, we're just returning the responses right now.
*
* @package Pastee
*/
class Pastee {
/**
* The API Base URL.
*/
const API_HOST = 'http://api.beta.paste.ee';
/**
* The API Version to use.
*/
const API_VERSION = 'v1';
/**
* @var Client The guzzle client instance.
*/
private $client;
/**
* Construct a new Pastee API instance.
*
* @param $key The Application or User Application key.
*/
public function __construct($key) {
$this->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());
}
}

8
src/PasteeException.php Normal file
View File

@ -0,0 +1,8 @@
<?php namespace Pastee;
class PasteeException extends \Exception {
public $errors;
public function __construct($errors) {
$this->errors = $errors;
}
}