From f5a781d4169734491079c4bf3ecd26be7d2a19de Mon Sep 17 00:00:00 2001 From: aymm Date: Mon, 8 Mar 2021 20:44:02 +0100 Subject: [PATCH] Initial commit --- .gitignore | 3 + .nova/Configuration.json | 8 + composer.json | 21 +++ composer.lock | 171 +++++++++++++++++++ phpcs.xml | 28 +++ src/APnutI.php | 77 +++++++++ src/Entities/Badge.php | 14 ++ src/Entities/Image.php | 22 +++ src/Entities/Post.php | 85 +++++++++ src/Entities/PostContents.php | 18 ++ src/Entities/PostCounts.php | 22 +++ src/Entities/Source.php | 20 +++ src/Entities/User.php | 62 +++++++ src/Exceptions/HttpPnutException.php | 9 + src/Exceptions/HttpPnutRedirectException.php | 15 ++ src/Exceptions/NotAuthorizedException.php | 9 + src/Exceptions/NotFoundException.php | 9 + src/Exceptions/PnutException.php | 7 + src/Meta.php | 46 +++++ 19 files changed, 646 insertions(+) create mode 100644 .gitignore create mode 100644 .nova/Configuration.json create mode 100644 composer.json create mode 100644 composer.lock create mode 100644 phpcs.xml create mode 100644 src/APnutI.php create mode 100644 src/Entities/Badge.php create mode 100644 src/Entities/Image.php create mode 100644 src/Entities/Post.php create mode 100644 src/Entities/PostContents.php create mode 100644 src/Entities/PostCounts.php create mode 100644 src/Entities/Source.php create mode 100644 src/Entities/User.php create mode 100644 src/Exceptions/HttpPnutException.php create mode 100644 src/Exceptions/HttpPnutRedirectException.php create mode 100644 src/Exceptions/NotAuthorizedException.php create mode 100644 src/Exceptions/NotFoundException.php create mode 100644 src/Exceptions/PnutException.php create mode 100644 src/Meta.php diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..6310a3e --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +index.php +vendor/ +log-* diff --git a/.nova/Configuration.json b/.nova/Configuration.json new file mode 100644 index 0000000..087c71e --- /dev/null +++ b/.nova/Configuration.json @@ -0,0 +1,8 @@ +{ + "com.thorlaksson.phpcs.runOnChange" : "onSave", + "com.thorlaksson.phpcs.standard" : "\/Volumes\/Alyx\/Users\/max\/Dev\/Pnut\/Untitled\/phpcs.xml", + "editor.default_syntax" : "php", + "php.validate" : "onSave", + "workspace.color" : 1, + "workspace.name" : "APnutI" +} diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..4c78f3c --- /dev/null +++ b/composer.json @@ -0,0 +1,21 @@ +{ + "name": "hutattedonmyarm/apnuti", + "type": "library", + "description": "PHP Pnut library", + "keywords": ["pnut", "api"], + "license": "MIT", + "require": { + "monolog/monolog": "^2.0", + "psr/log": "^1.1", + "php": "^7.4|^8.0", + "ext-pdo": "*", + "ext-curl": "*", + "ext-json": "*", + "ext-mbstring": "*" + }, + "autoload": { + "psr-4": { + "APnutI\\": "src" + } + } +} \ No newline at end of file diff --git a/composer.lock b/composer.lock new file mode 100644 index 0000000..309a6b5 --- /dev/null +++ b/composer.lock @@ -0,0 +1,171 @@ +{ + "_readme": [ + "This file locks the dependencies of your project to a known state", + "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", + "This file is @generated automatically" + ], + "content-hash": "3c041ce633113232f43e991a77d802f9", + "packages": [ + { + "name": "monolog/monolog", + "version": "2.2.0", + "source": { + "type": "git", + "url": "https://github.com/Seldaek/monolog.git", + "reference": "1cb1cde8e8dd0f70cc0fe51354a59acad9302084" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/Seldaek/monolog/zipball/1cb1cde8e8dd0f70cc0fe51354a59acad9302084", + "reference": "1cb1cde8e8dd0f70cc0fe51354a59acad9302084", + "shasum": "" + }, + "require": { + "php": ">=7.2", + "psr/log": "^1.0.1" + }, + "provide": { + "psr/log-implementation": "1.0.0" + }, + "require-dev": { + "aws/aws-sdk-php": "^2.4.9 || ^3.0", + "doctrine/couchdb": "~1.0@dev", + "elasticsearch/elasticsearch": "^7", + "graylog2/gelf-php": "^1.4.2", + "mongodb/mongodb": "^1.8", + "php-amqplib/php-amqplib": "~2.4", + "php-console/php-console": "^3.1.3", + "phpspec/prophecy": "^1.6.1", + "phpstan/phpstan": "^0.12.59", + "phpunit/phpunit": "^8.5", + "predis/predis": "^1.1", + "rollbar/rollbar": "^1.3", + "ruflin/elastica": ">=0.90 <7.0.1", + "swiftmailer/swiftmailer": "^5.3|^6.0" + }, + "suggest": { + "aws/aws-sdk-php": "Allow sending log messages to AWS services like DynamoDB", + "doctrine/couchdb": "Allow sending log messages to a CouchDB server", + "elasticsearch/elasticsearch": "Allow sending log messages to an Elasticsearch server via official client", + "ext-amqp": "Allow sending log messages to an AMQP server (1.0+ required)", + "ext-mbstring": "Allow to work properly with unicode symbols", + "ext-mongodb": "Allow sending log messages to a MongoDB server (via driver)", + "graylog2/gelf-php": "Allow sending log messages to a GrayLog2 server", + "mongodb/mongodb": "Allow sending log messages to a MongoDB server (via library)", + "php-amqplib/php-amqplib": "Allow sending log messages to an AMQP server using php-amqplib", + "php-console/php-console": "Allow sending log messages to Google Chrome", + "rollbar/rollbar": "Allow sending log messages to Rollbar", + "ruflin/elastica": "Allow sending log messages to an Elastic Search server" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-main": "2.x-dev" + } + }, + "autoload": { + "psr-4": { + "Monolog\\": "src/Monolog" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Jordi Boggiano", + "email": "j.boggiano@seld.be", + "homepage": "https://seld.be" + } + ], + "description": "Sends your logs to files, sockets, inboxes, databases and various web services", + "homepage": "https://github.com/Seldaek/monolog", + "keywords": [ + "log", + "logging", + "psr-3" + ], + "support": { + "issues": "https://github.com/Seldaek/monolog/issues", + "source": "https://github.com/Seldaek/monolog/tree/2.2.0" + }, + "funding": [ + { + "url": "https://github.com/Seldaek", + "type": "github" + }, + { + "url": "https://tidelift.com/funding/github/packagist/monolog/monolog", + "type": "tidelift" + } + ], + "time": "2020-12-14T13:15:25+00:00" + }, + { + "name": "psr/log", + "version": "1.1.3", + "source": { + "type": "git", + "url": "https://github.com/php-fig/log.git", + "reference": "0f73288fd15629204f9d42b7055f72dacbe811fc" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/php-fig/log/zipball/0f73288fd15629204f9d42b7055f72dacbe811fc", + "reference": "0f73288fd15629204f9d42b7055f72dacbe811fc", + "shasum": "" + }, + "require": { + "php": ">=5.3.0" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.1.x-dev" + } + }, + "autoload": { + "psr-4": { + "Psr\\Log\\": "Psr/Log/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "PHP-FIG", + "homepage": "http://www.php-fig.org/" + } + ], + "description": "Common interface for logging libraries", + "homepage": "https://github.com/php-fig/log", + "keywords": [ + "log", + "psr", + "psr-3" + ], + "support": { + "source": "https://github.com/php-fig/log/tree/1.1.3" + }, + "time": "2020-03-23T09:12:05+00:00" + } + ], + "packages-dev": [], + "aliases": [], + "minimum-stability": "stable", + "stability-flags": [], + "prefer-stable": false, + "prefer-lowest": false, + "platform": { + "php": "^7.4|^8.0", + "ext-pdo": "*", + "ext-curl": "*", + "ext-json": "*", + "ext-mbstring": "*" + }, + "platform-dev": [], + "plugin-api-version": "2.0.0" +} diff --git a/phpcs.xml b/phpcs.xml new file mode 100644 index 0000000..4bb20d7 --- /dev/null +++ b/phpcs.xml @@ -0,0 +1,28 @@ + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/APnutI.php b/src/APnutI.php new file mode 100644 index 0000000..a84c08d --- /dev/null +++ b/src/APnutI.php @@ -0,0 +1,77 @@ +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'); + } + } +} diff --git a/src/Entities/Badge.php b/src/Entities/Badge.php new file mode 100644 index 0000000..a13a7da --- /dev/null +++ b/src/Entities/Badge.php @@ -0,0 +1,14 @@ +id = (int)$data['id']; + $this->name = $data['name']; + } +} diff --git a/src/Entities/Image.php b/src/Entities/Image.php new file mode 100644 index 0000000..8a4bcb1 --- /dev/null +++ b/src/Entities/Image.php @@ -0,0 +1,22 @@ +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 + } + } +} diff --git a/src/Entities/Post.php b/src/Entities/Post.php new file mode 100644 index 0000000..0e7fad2 --- /dev/null +++ b/src/Entities/Post.php @@ -0,0 +1,85 @@ +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; + } +} diff --git a/src/Entities/PostContents.php b/src/Entities/PostContents.php new file mode 100644 index 0000000..acc56b6 --- /dev/null +++ b/src/Entities/PostContents.php @@ -0,0 +1,18 @@ +bookmarks = (int)$data['bookmarks']; + $this->replies = (int)$data['replies']; + $this->reposts = (int)$data['reposts']; + $this->threads = (int)$data['threads']; + } +} diff --git a/src/Entities/PostCounts.php b/src/Entities/PostCounts.php new file mode 100644 index 0000000..ad64a56 --- /dev/null +++ b/src/Entities/PostCounts.php @@ -0,0 +1,22 @@ +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']; + } + } +} diff --git a/src/Entities/Source.php b/src/Entities/Source.php new file mode 100644 index 0000000..eef8bfe --- /dev/null +++ b/src/Entities/Source.php @@ -0,0 +1,20 @@ +name = $data['name']; + if (isset($data['link'])) { + $this->link = $data['link']; //v0 + } else { + $this->link = $data['url']; //v1 + } + $this->id = (int)$data['id']; + } +} diff --git a/src/Entities/User.php b/src/Entities/User.php new file mode 100644 index 0000000..d4f2dd3 --- /dev/null +++ b/src/Entities/User.php @@ -0,0 +1,62 @@ +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"; + } + } +} diff --git a/src/Exceptions/HttpPnutException.php b/src/Exceptions/HttpPnutException.php new file mode 100644 index 0000000..4fb7c2b --- /dev/null +++ b/src/Exceptions/HttpPnutException.php @@ -0,0 +1,9 @@ +response = $response; + } +} diff --git a/src/Exceptions/NotAuthorizedException.php b/src/Exceptions/NotAuthorizedException.php new file mode 100644 index 0000000..c37c8fe --- /dev/null +++ b/src/Exceptions/NotAuthorizedException.php @@ -0,0 +1,9 @@ +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']); + } + } + } +}