Initial commit
This commit is contained in:
77
src/APnutI.php
Normal file
77
src/APnutI.php
Normal file
@ -0,0 +1,77 @@
|
||||
<?php
|
||||
|
||||
namespace APnutI;
|
||||
|
||||
use APnutI\Entities\Post;
|
||||
use APnutI\Entities\User;
|
||||
use APnutI\Meta;
|
||||
use Monolog\Logger;
|
||||
use Monolog\Handler\RotatingFileHandler;
|
||||
use Psr\Log\LoggerInterface;
|
||||
use Psr\Log\NullLogger;
|
||||
|
||||
class APnutI
|
||||
{
|
||||
protected string $api_url = 'https://api.pnut.io/v1/';
|
||||
protected string $auth_url = 'https://pnut.io/oauth/authenticate';
|
||||
protected string $client_secret;
|
||||
protected string $client_id;
|
||||
protected string $scope;
|
||||
protected string $redirect_uri;
|
||||
protected $rate_limit = null;
|
||||
protected $rate_limit_remaining = null;
|
||||
protected $rate_limit_rset = null;
|
||||
protected $scopes;
|
||||
protected ?string $needed_scope;
|
||||
protected ?string $redirect_target = null;
|
||||
protected array $headers = [];
|
||||
protected string $app_name = 'Abstract API';
|
||||
protected ?string $server_token;
|
||||
protected ?string $access_token;
|
||||
protected LoggerInterface $logger;
|
||||
protected string $token_session_key;
|
||||
protected ?string $server_token_file_path = null;
|
||||
|
||||
public ?Meta $meta = null;
|
||||
|
||||
/*
|
||||
* Error codes:
|
||||
* 3XX: Pnut error
|
||||
* - 300: Cannot fetch post creator
|
||||
* - 301: Missing field of post creator (e.g. has no avatar)
|
||||
* 4XX: User error (post not found, yada yada)
|
||||
* - 400: Missing parameter
|
||||
* - 404: Not found
|
||||
* 5XX: Generic server error
|
||||
*/
|
||||
|
||||
public static $ERROR_FETCHING_CREATOR = 300;
|
||||
public static $ERROR_FETCHING_CREATOR_FIELD = 301;
|
||||
public static $ERROR_MISSING_PARAMETER = 400;
|
||||
public static $ERROR_NOT_FOUND = 404;
|
||||
public static $ERROR_UNKNOWN_SERVER_ERROR = 500;
|
||||
|
||||
public static $POST_MAX_LENGTH;
|
||||
public static $POST_MAX_LENGTH_REPOST;
|
||||
public static $POST_SECONDS_BETWEEN_DUPLICATES;
|
||||
public static $MESSAGE_MAX_LENGTH;
|
||||
public static $RAW_MAX_LENGTH;
|
||||
public static $USER_DESCRIPTION_MAX_LENGTH;
|
||||
public static $USER_USERNAME_MAX_LENGTH;
|
||||
|
||||
public function __construct(?string $log_path = null)
|
||||
{
|
||||
$this->logger = empty($log_path) ? new NullLogger() : new Logger($this->app_name);
|
||||
$this->token_session_key = $this->app_name.'access_token';
|
||||
$handler = new RotatingFileHandler($log_path, 5, Logger::DEBUG, true);
|
||||
$this->logger->pushHandler($handler);
|
||||
$this->server_token = null;
|
||||
$this->logger->debug('__construct API');
|
||||
if (isset($_SESSION[$this->token_session_key])) {
|
||||
$this->access_token = $_SESSION[$this->token_session_key];
|
||||
$this->logger->debug('Access token in session');
|
||||
} else {
|
||||
$this->logger->debug('No access token in session');
|
||||
}
|
||||
}
|
||||
}
|
14
src/Entities/Badge.php
Normal file
14
src/Entities/Badge.php
Normal file
@ -0,0 +1,14 @@
|
||||
<?php
|
||||
namespace APnutI\Entities;
|
||||
|
||||
class Badge
|
||||
{
|
||||
public int $id;
|
||||
public string $name;
|
||||
|
||||
public function __construct(array $data)
|
||||
{
|
||||
$this->id = (int)$data['id'];
|
||||
$this->name = $data['name'];
|
||||
}
|
||||
}
|
22
src/Entities/Image.php
Normal file
22
src/Entities/Image.php
Normal file
@ -0,0 +1,22 @@
|
||||
<?php
|
||||
namespace APnutI\Entities;
|
||||
|
||||
class Image
|
||||
{
|
||||
public bool $is_default;
|
||||
public int $height;
|
||||
public int $width;
|
||||
public string $link;
|
||||
|
||||
public function __construct(array $data)
|
||||
{
|
||||
$this->is_default = $data['is_default'];
|
||||
$this->height = $data['height'];
|
||||
$this->width = $data['width'];
|
||||
if (isset($data['link'])) {
|
||||
$this->link = $data['link']; //API v0
|
||||
} else {
|
||||
$this->link = $data['url']; //API v1
|
||||
}
|
||||
}
|
||||
}
|
85
src/Entities/Post.php
Normal file
85
src/Entities/Post.php
Normal file
@ -0,0 +1,85 @@
|
||||
<?php
|
||||
namespace APnutI\Entities;
|
||||
|
||||
use APnutI\Entities\User;
|
||||
use APnutI\Entities\Source;
|
||||
use APnutI\Entities\PostCounts;
|
||||
use APnutI\Entities\PostContent;
|
||||
|
||||
class Post
|
||||
{
|
||||
public DateTime $created_at;
|
||||
public int $id;
|
||||
public bool $is_deleted = false;
|
||||
public bool $is_nsfw = false;
|
||||
public bool $is_revised = false;
|
||||
public ?int $revision = null;
|
||||
public ?User $user = null;
|
||||
public int $thread_id;
|
||||
public ?int $reply_to = null;
|
||||
public ?int $repost_of = null;
|
||||
public ?PostCounts $counts = null;
|
||||
public ?Source $source = null;
|
||||
public ?PostContent $content = null;
|
||||
public bool $you_bookmarked = false;
|
||||
public bool $you_reposted = false;
|
||||
|
||||
public function __construct(array $data)
|
||||
{
|
||||
$this->created_at = new \DateTime($data['created_at']);
|
||||
$this->id = (int)$data['id'];
|
||||
if (!empty($data['is_deleted'])) {
|
||||
$this->is_deleted = (bool)$data['is_deleted'];
|
||||
}
|
||||
if (!empty($data['is_nsfw'])) {
|
||||
$this->is_nsfw = (bool)$data['is_nsfw'];
|
||||
}
|
||||
if (!empty($data['is_revised'])) {
|
||||
$this->is_revised = (bool)$data['is_revised'];
|
||||
}
|
||||
if (!empty($data['revision'])) {
|
||||
$this->revision = (bool)$data['revision'];
|
||||
}
|
||||
#file_put_contents(__DIR__."/post_log.log", json_encode($data['user']), FILE_APPEND | LOCK_EX);
|
||||
if (!empty($data['user'])) {
|
||||
$this->user = new User($data['user']);
|
||||
}
|
||||
#file_put_contents(__DIR__."/post_log.log", json_encode($this->user), FILE_APPEND | LOCK_EX);
|
||||
$this->thread_id = (int)$data['thread_id'];
|
||||
if (!empty($data['reply_to'])) {
|
||||
$this->reply_to = (int)$data['reply_to'];
|
||||
}
|
||||
if (!empty($data['repost_of'])) {
|
||||
$this->repost_of = (int)$data['repost_of'];
|
||||
}
|
||||
if (!empty($data['counts'])) {
|
||||
$this->counts = new PostCounts($data['counts']);
|
||||
}
|
||||
|
||||
if (!empty($data['source']) && is_array($data['source'])) {
|
||||
$this->source = new Source($data['source']);
|
||||
}
|
||||
if (!empty($data['content'])) {
|
||||
$this->content = new PostContent($data['content']);
|
||||
}
|
||||
if (!empty($data['you_bookmarked'])) {
|
||||
$this->you_bookmarked = (bool)$data['you_bookmarked'];
|
||||
}
|
||||
if (!empty($data['you_reposted'])) {
|
||||
$this->you_reposted = (bool)$data['you_reposted'];
|
||||
}
|
||||
}
|
||||
|
||||
public function getText(bool $html_if_present = true): string
|
||||
{
|
||||
$txt = '';
|
||||
if (!empty($this->content)) {
|
||||
if ($html_if_present && !empty($this->content->html)) {
|
||||
$txt = $this->content->html;
|
||||
} elseif (!empty($this->content->text)) {
|
||||
$txt = $this->content->text;
|
||||
}
|
||||
}
|
||||
return $txt;
|
||||
}
|
||||
}
|
18
src/Entities/PostContents.php
Normal file
18
src/Entities/PostContents.php
Normal file
@ -0,0 +1,18 @@
|
||||
<?php
|
||||
namespace APnutI\Entities;
|
||||
|
||||
class PostCounts
|
||||
{
|
||||
public int $bookmarks;
|
||||
public int $replies;
|
||||
public int $reposts;
|
||||
public int $threads;
|
||||
|
||||
public function __construct(array $data)
|
||||
{
|
||||
$this->bookmarks = (int)$data['bookmarks'];
|
||||
$this->replies = (int)$data['replies'];
|
||||
$this->reposts = (int)$data['reposts'];
|
||||
$this->threads = (int)$data['threads'];
|
||||
}
|
||||
}
|
22
src/Entities/PostCounts.php
Normal file
22
src/Entities/PostCounts.php
Normal file
@ -0,0 +1,22 @@
|
||||
<?php
|
||||
namespace APnutI\Entities;
|
||||
|
||||
class PostContent
|
||||
{
|
||||
public string $text;
|
||||
public ?string $html = null;
|
||||
public array $entities;
|
||||
public bool $links_not_parsed = false;
|
||||
|
||||
public function __construct(array $data)
|
||||
{
|
||||
$this->text = $data['text'];
|
||||
if (!empty($data['html'])) {
|
||||
$this->html = $data['html'];
|
||||
}
|
||||
$this->entities = $data['entities'];
|
||||
if (!empty($data['links_not_parsed'])) {
|
||||
$this->links_not_parsed = (bool)$data['links_not_parsed'];
|
||||
}
|
||||
}
|
||||
}
|
20
src/Entities/Source.php
Normal file
20
src/Entities/Source.php
Normal file
@ -0,0 +1,20 @@
|
||||
<?php
|
||||
namespace APnutI\Entities;
|
||||
|
||||
class Source
|
||||
{
|
||||
public string $name;
|
||||
public string $link;
|
||||
public int $id;
|
||||
|
||||
public function __construct($data)
|
||||
{
|
||||
$this->name = $data['name'];
|
||||
if (isset($data['link'])) {
|
||||
$this->link = $data['link']; //v0
|
||||
} else {
|
||||
$this->link = $data['url']; //v1
|
||||
}
|
||||
$this->id = (int)$data['id'];
|
||||
}
|
||||
}
|
62
src/Entities/User.php
Normal file
62
src/Entities/User.php
Normal file
@ -0,0 +1,62 @@
|
||||
<?php
|
||||
namespace APnutI\Entities;
|
||||
|
||||
use APnutI\Entities\Image;
|
||||
use APnutI\Entities\Badge;
|
||||
|
||||
class User
|
||||
{
|
||||
public ?Badge $badge = null;
|
||||
public ?Image $avatar_image = null;
|
||||
public ?Image $cover_image = null;
|
||||
public int $id;
|
||||
public string $username;
|
||||
public ?string $name = null;
|
||||
public ?bool $presence = null;
|
||||
#private LoggerInterface $log;
|
||||
|
||||
public function __construct(array $data)
|
||||
{
|
||||
#$this->log = new Logger('User');
|
||||
$this->id = (int)$data['id'];
|
||||
$this->username = $data['username'];
|
||||
if (!empty($data['badge'])) {
|
||||
$this->badge = new Badge($data['badge']);
|
||||
}
|
||||
if (!empty($data['content'])) {
|
||||
$this->avatar_image = new Image($data['content']['avatar_image']);
|
||||
$this->cover_image = new Image($data['content']['cover_image']);
|
||||
}
|
||||
|
||||
if (!empty($data['name'])) {
|
||||
$this->name = $data['name'];
|
||||
}
|
||||
if (!empty($data['presence'])) {
|
||||
if (is_string($data['presence']) || is_int($data['presence'])) {
|
||||
$this->presence = !($data['presence'] == "offline" || $data['presence'] === 0);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public function getPresenceInt(): int
|
||||
{
|
||||
if ($this->presence === true) {
|
||||
return 1;
|
||||
} elseif ($this->presence === false) {
|
||||
return 0;
|
||||
} else {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
public function getPresenceString(): string
|
||||
{
|
||||
if ($this->presence === true) {
|
||||
return "online";
|
||||
} elseif ($this->presence === false) {
|
||||
return "offline";
|
||||
} else {
|
||||
return "presence unknown";
|
||||
}
|
||||
}
|
||||
}
|
9
src/Exceptions/HttpPnutException.php
Normal file
9
src/Exceptions/HttpPnutException.php
Normal file
@ -0,0 +1,9 @@
|
||||
<?php
|
||||
namespace APnutI\Exceptions;
|
||||
|
||||
use APnutI\Exceptions\PnutException;
|
||||
|
||||
class HttpPnutException extends PnutException
|
||||
{
|
||||
|
||||
}
|
15
src/Exceptions/HttpPnutRedirectException.php
Normal file
15
src/Exceptions/HttpPnutRedirectException.php
Normal file
@ -0,0 +1,15 @@
|
||||
<?php
|
||||
namespace APnutI\Exceptions;
|
||||
|
||||
use APnutI\Exceptions\HttpPnutException;
|
||||
|
||||
class HttpPnutRedirectException extends HttpPnutException
|
||||
{
|
||||
public $response;
|
||||
|
||||
public function __construct($response)
|
||||
{
|
||||
parent::__construct("Redirect");
|
||||
$this->response = $response;
|
||||
}
|
||||
}
|
9
src/Exceptions/NotAuthorizedException.php
Normal file
9
src/Exceptions/NotAuthorizedException.php
Normal file
@ -0,0 +1,9 @@
|
||||
<?php
|
||||
namespace APnutI\Exceptions;
|
||||
|
||||
use APnutI\Exceptions\PnutException;
|
||||
|
||||
class NotAuthorizedException extends Exception
|
||||
{
|
||||
|
||||
}
|
9
src/Exceptions/NotFoundException.php
Normal file
9
src/Exceptions/NotFoundException.php
Normal file
@ -0,0 +1,9 @@
|
||||
<?php
|
||||
namespace APnutI\Exceptions;
|
||||
|
||||
use APnutI\Exceptions\PnutException;
|
||||
|
||||
class NotFoundException extends HttpPnutException
|
||||
{
|
||||
|
||||
}
|
7
src/Exceptions/PnutException.php
Normal file
7
src/Exceptions/PnutException.php
Normal file
@ -0,0 +1,7 @@
|
||||
<?php
|
||||
namespace APnutI\Exceptions;
|
||||
|
||||
class PnutException extends Exception
|
||||
{
|
||||
|
||||
}
|
46
src/Meta.php
Normal file
46
src/Meta.php
Normal file
@ -0,0 +1,46 @@
|
||||
<?php
|
||||
|
||||
namespace APnutI;
|
||||
|
||||
use APnutI\Exceptions\PnutException;
|
||||
use APnutI\Exceptions\NotAuthorizedException;
|
||||
use APnutI\Exceptions\NotFoundException;
|
||||
|
||||
class Meta
|
||||
{
|
||||
public bool $more = false;
|
||||
public ?int $max_id = null;
|
||||
public ?int $min_id = null;
|
||||
public ?int $code = -1;
|
||||
|
||||
public function __construct(array $json)
|
||||
{
|
||||
if (empty($json['meta'])) {
|
||||
return;
|
||||
}
|
||||
$meta = (array)$json['meta'];
|
||||
if (!empty($meta['more'])) {
|
||||
$this->more = (bool)$meta['more'];
|
||||
}
|
||||
if (!empty($meta['max_id'])) {
|
||||
$this->max_id = (int)$meta['max_id'];
|
||||
}
|
||||
if (!empty($meta['min_id'])) {
|
||||
$this->min_id = (int)$meta['min_id'];
|
||||
}
|
||||
if (!empty($meta['code'])) {
|
||||
$this->code = $meta['code'];
|
||||
if ($this->code === 400) {
|
||||
throw new PnutException($meta['error_message']);
|
||||
}if ($this->code === 401) {
|
||||
throw new NotAuthorizedException($meta['error_message']);
|
||||
}
|
||||
if ($this->code === 404) {
|
||||
throw new NotFoundException();
|
||||
}
|
||||
if ($meta['code'] < 200 || $meta['code'] >= 300) {
|
||||
throw new PnutException($meta['error_message']);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user