2016-10-20 22:09:36 -04:00
|
|
|
<?php declare(strict_types=1);
|
2015-11-16 19:30:04 -05:00
|
|
|
/**
|
2017-02-16 11:09:37 -05:00
|
|
|
* Hummingbird Anime List Client
|
2015-11-16 19:30:04 -05:00
|
|
|
*
|
2018-08-22 13:48:27 -04:00
|
|
|
* An API client for Kitsu to manage anime and manga watch lists
|
2015-11-16 19:30:04 -05:00
|
|
|
*
|
2021-02-04 11:57:01 -05:00
|
|
|
* PHP version 8
|
2016-08-30 10:01:18 -04:00
|
|
|
*
|
2015-11-16 19:30:04 -05:00
|
|
|
* @package HummingbirdAnimeClient
|
2016-08-30 10:01:18 -04:00
|
|
|
* @author Timothy J. Warren <tim@timshomepage.net>
|
2021-01-13 01:52:03 -05:00
|
|
|
* @copyright 2015 - 2021 Timothy J. Warren
|
2016-08-30 10:01:18 -04:00
|
|
|
* @license http://www.opensource.org/licenses/mit-license.html MIT License
|
2020-12-10 17:06:50 -05:00
|
|
|
* @version 5.2
|
2017-03-07 20:53:58 -05:00
|
|
|
* @link https://git.timshomepage.net/timw4mail/HummingBirdAnimeClient
|
2015-11-16 19:30:04 -05:00
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Aviat\AnimeClient;
|
|
|
|
|
2020-08-26 15:22:14 -04:00
|
|
|
use Aviat\AnimeClient\Kitsu;
|
2020-05-08 19:15:21 -04:00
|
|
|
use Psr\SimpleCache\CacheInterface;
|
|
|
|
use Psr\SimpleCache\InvalidArgumentException;
|
2018-11-29 11:00:50 -05:00
|
|
|
|
2020-03-11 22:11:00 -04:00
|
|
|
use Amp\Http\Client\Request;
|
2020-03-11 16:26:17 -04:00
|
|
|
use Amp\Http\Client\Response;
|
|
|
|
use Amp\Http\Client\HttpClient;
|
|
|
|
use Amp\Http\Client\HttpClientBuilder;
|
2018-11-29 11:00:50 -05:00
|
|
|
|
2018-08-16 12:10:24 -04:00
|
|
|
use Aviat\Ion\ConfigInterface;
|
2018-08-20 13:01:16 -04:00
|
|
|
use Yosymfony\Toml\{Toml, TomlBuilder};
|
2016-02-10 17:30:45 -05:00
|
|
|
|
2020-05-08 19:15:21 -04:00
|
|
|
use Throwable;
|
|
|
|
|
2021-02-03 09:45:18 -05:00
|
|
|
use function Amp\Promise\wait;
|
|
|
|
use function Aviat\Ion\_dir;
|
2018-10-19 09:30:27 -04:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
//! TOML Functions
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
2018-08-16 12:10:24 -04:00
|
|
|
/**
|
|
|
|
* Load configuration options from .toml files
|
|
|
|
*
|
2020-12-11 15:37:55 -05:00
|
|
|
* @codeCoverageIgnore
|
2018-08-16 12:10:24 -04:00
|
|
|
* @param string $path - Path to load config
|
|
|
|
* @return array
|
|
|
|
*/
|
2020-12-11 15:37:55 -05:00
|
|
|
function loadConfig(string $path): array
|
2017-12-06 14:40:13 -05:00
|
|
|
{
|
2018-08-16 12:10:24 -04:00
|
|
|
$output = [];
|
|
|
|
$files = glob("{$path}/*.toml");
|
|
|
|
|
2021-02-10 13:59:37 -05:00
|
|
|
if ( ! is_array($files))
|
|
|
|
{
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
2018-08-16 12:10:24 -04:00
|
|
|
foreach ($files as $file)
|
2016-02-10 17:30:45 -05:00
|
|
|
{
|
2018-08-16 12:10:24 -04:00
|
|
|
$key = str_replace('.toml', '', basename($file));
|
2018-10-08 15:45:46 -04:00
|
|
|
if ($key === 'admin-override')
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2018-08-16 12:10:24 -04:00
|
|
|
$config = Toml::parseFile($file);
|
2016-02-10 17:30:45 -05:00
|
|
|
|
2018-08-16 12:10:24 -04:00
|
|
|
if ($key === 'config')
|
2016-02-10 17:30:45 -05:00
|
|
|
{
|
2018-08-16 12:10:24 -04:00
|
|
|
foreach($config as $name => $value)
|
2016-02-10 17:30:45 -05:00
|
|
|
{
|
2018-08-16 12:10:24 -04:00
|
|
|
$output[$name] = $value;
|
2016-02-10 17:30:45 -05:00
|
|
|
}
|
|
|
|
|
2018-08-16 12:10:24 -04:00
|
|
|
continue;
|
2016-02-10 17:30:45 -05:00
|
|
|
}
|
|
|
|
|
2018-08-16 12:10:24 -04:00
|
|
|
$output[$key] = $config;
|
2016-02-10 17:30:45 -05:00
|
|
|
}
|
2018-08-16 12:10:24 -04:00
|
|
|
|
|
|
|
return $output;
|
2017-12-06 14:40:13 -05:00
|
|
|
}
|
2018-08-16 12:10:24 -04:00
|
|
|
|
2018-10-08 15:45:46 -04:00
|
|
|
/**
|
|
|
|
* Load config from one specific TOML file
|
|
|
|
*
|
2020-12-11 15:37:55 -05:00
|
|
|
* @codeCoverageIgnore
|
2018-10-08 15:45:46 -04:00
|
|
|
* @param string $filename
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
function loadTomlFile(string $filename): array
|
|
|
|
{
|
|
|
|
return Toml::parseFile($filename);
|
|
|
|
}
|
|
|
|
|
2021-02-11 19:54:22 -05:00
|
|
|
function _iterateToml(TomlBuilder $builder, iterable $data, mixed $parentKey = NULL): void
|
2018-08-20 13:01:16 -04:00
|
|
|
{
|
2020-12-11 15:37:55 -05:00
|
|
|
foreach ($data as $key => $value)
|
2018-08-20 13:01:16 -04:00
|
|
|
{
|
2020-12-11 15:37:55 -05:00
|
|
|
// Skip unsupported empty value
|
|
|
|
if ($value === NULL)
|
2018-08-20 13:01:16 -04:00
|
|
|
{
|
2020-12-11 15:37:55 -05:00
|
|
|
continue;
|
|
|
|
}
|
2018-08-20 13:01:16 -04:00
|
|
|
|
|
|
|
|
2020-12-11 15:37:55 -05:00
|
|
|
if (is_scalar($value) || isSequentialArray($value))
|
|
|
|
{
|
|
|
|
$builder->addValue($key, $value);
|
|
|
|
continue;
|
|
|
|
}
|
2018-08-20 13:01:16 -04:00
|
|
|
|
2020-12-11 15:37:55 -05:00
|
|
|
$newKey = ($parentKey !== NULL)
|
|
|
|
? "{$parentKey}.{$key}"
|
|
|
|
: $key;
|
2018-08-20 13:01:16 -04:00
|
|
|
|
2020-12-11 15:37:55 -05:00
|
|
|
if ( ! isSequentialArray($value))
|
|
|
|
{
|
|
|
|
$builder->addTable($newKey);
|
2020-08-26 15:22:14 -04:00
|
|
|
}
|
2020-12-11 15:37:55 -05:00
|
|
|
|
|
|
|
_iterateToml($builder, $value, $newKey);
|
2018-08-20 13:01:16 -04:00
|
|
|
}
|
2020-12-11 15:37:55 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Serialize config data into a Toml file
|
|
|
|
*
|
2021-02-12 10:53:07 -05:00
|
|
|
* @param iterable $data
|
2020-12-11 15:37:55 -05:00
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
function arrayToToml(iterable $data): string
|
|
|
|
{
|
|
|
|
$builder = new TomlBuilder();
|
2018-08-20 13:01:16 -04:00
|
|
|
|
|
|
|
_iterateToml($builder, $data);
|
|
|
|
|
|
|
|
return $builder->getTomlString();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Serialize toml back to an array
|
|
|
|
*
|
|
|
|
* @param string $toml
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
function tomlToArray(string $toml): array
|
|
|
|
{
|
|
|
|
return Toml::parse($toml);
|
|
|
|
}
|
|
|
|
|
2018-10-19 09:30:27 -04:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
//! Misc Functions
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Is the array sequential, not associative?
|
|
|
|
*
|
|
|
|
* @param mixed $array
|
|
|
|
* @return bool
|
|
|
|
*/
|
2021-02-12 10:53:07 -05:00
|
|
|
function isSequentialArray(mixed $array): bool
|
2018-10-19 09:30:27 -04:00
|
|
|
{
|
|
|
|
if ( ! is_array($array))
|
|
|
|
{
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
$i = 0;
|
|
|
|
foreach ($array as $k => $v)
|
|
|
|
{
|
|
|
|
if ($k !== $i++)
|
|
|
|
{
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2018-08-16 12:10:24 -04:00
|
|
|
/**
|
|
|
|
* Check that folder permissions are correct for proper operation
|
|
|
|
*
|
|
|
|
* @param ConfigInterface $config
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
function checkFolderPermissions(ConfigInterface $config): array
|
|
|
|
{
|
|
|
|
$errors = [];
|
|
|
|
$publicDir = $config->get('asset_dir');
|
|
|
|
|
2021-02-03 09:45:18 -05:00
|
|
|
$APP_DIR = _dir(dirname(__DIR__, 2), '/app');
|
|
|
|
|
2018-08-16 12:10:24 -04:00
|
|
|
$pathMap = [
|
2021-02-03 09:45:18 -05:00
|
|
|
'app/config' => "{$APP_DIR}/config",
|
|
|
|
'app/logs' => "{$APP_DIR}/logs",
|
2018-08-16 12:10:24 -04:00
|
|
|
'public/images/avatars' => "{$publicDir}/images/avatars",
|
|
|
|
'public/images/anime' => "{$publicDir}/images/anime",
|
|
|
|
'public/images/characters' => "{$publicDir}/images/characters",
|
|
|
|
'public/images/manga' => "{$publicDir}/images/manga",
|
|
|
|
'public/images/people' => "{$publicDir}/images/people",
|
|
|
|
];
|
|
|
|
|
|
|
|
foreach ($pathMap as $pretty => $actual)
|
|
|
|
{
|
|
|
|
// Make sure the folder exists first
|
|
|
|
if ( ! is_dir($actual))
|
|
|
|
{
|
|
|
|
$errors['missing'][] = $pretty;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$writable = is_writable($actual) && is_executable($actual);
|
|
|
|
|
|
|
|
if ( ! $writable)
|
|
|
|
{
|
|
|
|
$errors['writable'][] = $pretty;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $errors;
|
2018-10-19 09:30:27 -04:00
|
|
|
}
|
|
|
|
|
2018-12-07 10:24:42 -05:00
|
|
|
/**
|
|
|
|
* Get an API Client, with better defaults
|
|
|
|
*
|
2020-04-21 19:22:56 -04:00
|
|
|
* @return HttpClient
|
2018-12-07 10:24:42 -05:00
|
|
|
*/
|
2020-04-21 19:22:56 -04:00
|
|
|
function getApiClient (): HttpClient
|
2018-12-07 10:24:42 -05:00
|
|
|
{
|
|
|
|
static $client;
|
|
|
|
|
|
|
|
if ($client === NULL)
|
|
|
|
{
|
2020-03-11 16:26:17 -04:00
|
|
|
$client = HttpClientBuilder::buildDefault();
|
2018-12-07 10:24:42 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return $client;
|
|
|
|
}
|
|
|
|
|
2018-11-29 11:00:50 -05:00
|
|
|
/**
|
2020-03-11 16:26:17 -04:00
|
|
|
* Simplify making a request with Http\Client
|
2018-11-29 11:00:50 -05:00
|
|
|
*
|
2020-03-11 22:11:00 -04:00
|
|
|
* @param string|Request $request
|
2018-11-29 11:00:50 -05:00
|
|
|
* @return Response
|
2020-05-08 19:15:21 -04:00
|
|
|
* @throws Throwable
|
2018-11-29 11:00:50 -05:00
|
|
|
*/
|
2021-02-03 09:46:36 -05:00
|
|
|
function getResponse (Request|string $request): Response
|
2018-11-29 11:00:50 -05:00
|
|
|
{
|
2018-12-07 10:24:42 -05:00
|
|
|
$client = getApiClient();
|
2020-03-11 22:11:00 -04:00
|
|
|
|
|
|
|
if (is_string($request))
|
|
|
|
{
|
|
|
|
$request = new Request($request);
|
|
|
|
}
|
|
|
|
|
2018-12-06 16:21:02 -05:00
|
|
|
return wait($client->request($request));
|
2018-11-29 11:00:50 -05:00
|
|
|
}
|
|
|
|
|
2018-10-19 09:30:27 -04:00
|
|
|
/**
|
2018-11-01 22:15:20 -04:00
|
|
|
* Generate the path for the cached image from the original image
|
2018-10-19 09:30:27 -04:00
|
|
|
*
|
|
|
|
* @param string $kitsuUrl
|
2018-11-01 22:15:20 -04:00
|
|
|
* @param bool $webp
|
2018-10-19 09:30:27 -04:00
|
|
|
* @return string
|
|
|
|
*/
|
2020-12-10 15:59:37 -05:00
|
|
|
function getLocalImg (string $kitsuUrl, $webp = TRUE): string
|
2018-10-19 09:30:27 -04:00
|
|
|
{
|
2020-05-08 19:15:21 -04:00
|
|
|
if (empty($kitsuUrl) || ( ! is_string($kitsuUrl)))
|
2018-10-19 09:30:27 -04:00
|
|
|
{
|
2018-11-01 22:15:20 -04:00
|
|
|
return 'images/placeholder.webp';
|
2018-10-19 09:30:27 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
$parts = parse_url($kitsuUrl);
|
|
|
|
|
2021-02-12 10:53:07 -05:00
|
|
|
if ($parts === FALSE || ! array_key_exists('path', $parts))
|
2018-10-19 09:30:27 -04:00
|
|
|
{
|
2018-11-01 22:15:20 -04:00
|
|
|
return 'images/placeholder.webp';
|
2018-10-19 09:30:27 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
$file = basename($parts['path']);
|
|
|
|
$fileParts = explode('.', $file);
|
|
|
|
$ext = array_pop($fileParts);
|
2018-11-01 22:15:20 -04:00
|
|
|
$ext = $webp ? 'webp' : $ext;
|
|
|
|
|
2018-10-19 09:30:27 -04:00
|
|
|
$segments = explode('/', trim($parts['path'], '/'));
|
|
|
|
|
|
|
|
$type = $segments[0] === 'users' ? $segments[1] : $segments[0];
|
|
|
|
|
|
|
|
$id = $segments[count($segments) - 2];
|
|
|
|
|
|
|
|
return implode('/', ['images', $type, "{$id}.{$ext}"]);
|
2018-10-26 13:08:45 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a transparent placeholder image
|
|
|
|
*
|
|
|
|
* @param string $path
|
2021-02-03 09:45:18 -05:00
|
|
|
* @param int|null $width
|
|
|
|
* @param int|null $height
|
2018-10-26 13:08:45 -04:00
|
|
|
* @param string $text
|
2021-02-12 10:53:07 -05:00
|
|
|
* @return bool
|
2018-10-26 13:08:45 -04:00
|
|
|
*/
|
2021-02-12 10:53:07 -05:00
|
|
|
function createPlaceholderImage (string $path, ?int $width, ?int $height, $text = 'Image Unavailable'): bool
|
2018-10-26 13:08:45 -04:00
|
|
|
{
|
|
|
|
$width = $width ?? 200;
|
|
|
|
$height = $height ?? 200;
|
|
|
|
|
2018-11-01 22:15:20 -04:00
|
|
|
$img = imagecreatetruecolor($width, $height);
|
2021-02-12 10:53:07 -05:00
|
|
|
if ($img === FALSE)
|
|
|
|
{
|
|
|
|
return FALSE;
|
|
|
|
}
|
2018-11-01 22:15:20 -04:00
|
|
|
imagealphablending($img, TRUE);
|
2018-10-26 13:08:45 -04:00
|
|
|
|
|
|
|
$path = rtrim($path, '/');
|
|
|
|
|
|
|
|
// Background is the first color by default
|
2018-11-01 22:15:20 -04:00
|
|
|
$fillColor = imagecolorallocatealpha($img, 255, 255, 255, 127);
|
2021-02-12 10:53:07 -05:00
|
|
|
if ($fillColor === FALSE)
|
|
|
|
{
|
|
|
|
return FALSE;
|
|
|
|
}
|
2018-11-01 22:15:20 -04:00
|
|
|
imagefill($img, 0, 0, $fillColor);
|
2018-11-07 14:29:21 -05:00
|
|
|
|
2018-10-26 13:08:45 -04:00
|
|
|
$textColor = imagecolorallocate($img, 64, 64, 64);
|
2021-02-12 10:53:07 -05:00
|
|
|
if ($textColor === FALSE)
|
|
|
|
{
|
|
|
|
return FALSE;
|
|
|
|
}
|
2018-10-26 13:08:45 -04:00
|
|
|
|
|
|
|
imagealphablending($img, TRUE);
|
|
|
|
|
|
|
|
// Generate placeholder text
|
|
|
|
$fontSize = 10;
|
|
|
|
$fontWidth = imagefontwidth($fontSize);
|
|
|
|
$fontHeight = imagefontheight($fontSize);
|
2018-11-09 10:38:35 -05:00
|
|
|
$length = \strlen($text);
|
2018-10-26 13:08:45 -04:00
|
|
|
$textWidth = $length * $fontWidth;
|
|
|
|
$fxPos = (int) ceil((imagesx($img) - $textWidth) / 2);
|
|
|
|
$fyPos = (int) ceil((imagesy($img) - $fontHeight) / 2);
|
|
|
|
|
|
|
|
// Add the image text
|
|
|
|
imagestring($img, $fontSize, $fxPos, $fyPos, $text, $textColor);
|
|
|
|
|
|
|
|
// Save the images
|
|
|
|
imagesavealpha($img, TRUE);
|
|
|
|
imagepng($img, $path . '/placeholder.png', 9);
|
|
|
|
imagedestroy($img);
|
2018-11-07 14:29:21 -05:00
|
|
|
|
2018-11-01 22:15:20 -04:00
|
|
|
$pngImage = imagecreatefrompng($path . '/placeholder.png');
|
2021-02-12 10:53:07 -05:00
|
|
|
if ($pngImage === FALSE)
|
|
|
|
{
|
|
|
|
return FALSE;
|
|
|
|
}
|
2018-11-01 22:15:20 -04:00
|
|
|
imagealphablending($pngImage, TRUE);
|
|
|
|
imagesavealpha($pngImage, TRUE);
|
2018-11-07 14:29:21 -05:00
|
|
|
|
2018-11-01 22:15:20 -04:00
|
|
|
imagewebp($pngImage, $path . '/placeholder.webp');
|
|
|
|
|
|
|
|
imagedestroy($pngImage);
|
2021-02-12 10:53:07 -05:00
|
|
|
|
|
|
|
return TRUE;
|
2020-04-30 15:33:16 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check that there is a value for at least one item in a collection with the specified key
|
|
|
|
*
|
|
|
|
* @param array $search
|
|
|
|
* @param string $key
|
|
|
|
* @return bool
|
|
|
|
*/
|
2020-08-26 15:22:14 -04:00
|
|
|
function colNotEmpty(array $search, string $key): bool
|
2020-04-30 15:33:16 -04:00
|
|
|
{
|
2020-12-11 15:37:55 -05:00
|
|
|
$items = array_filter(array_column($search, $key), static fn ($x) => ( ! empty($x)));
|
2020-04-30 15:33:16 -04:00
|
|
|
return count($items) > 0;
|
2020-05-08 19:15:21 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Clear the cache, but save user auth data
|
|
|
|
*
|
|
|
|
* @param CacheInterface $cache
|
|
|
|
* @return bool
|
2021-02-11 19:54:22 -05:00
|
|
|
* @throws Throwable
|
2020-05-08 19:15:21 -04:00
|
|
|
*/
|
|
|
|
function clearCache(CacheInterface $cache): bool
|
|
|
|
{
|
|
|
|
// Save the user data, if it exists, for priming the cache
|
|
|
|
$userData = $cache->getMultiple([
|
|
|
|
Kitsu::AUTH_USER_ID_KEY,
|
|
|
|
Kitsu::AUTH_TOKEN_CACHE_KEY,
|
|
|
|
Kitsu::AUTH_TOKEN_EXP_CACHE_KEY,
|
|
|
|
Kitsu::AUTH_TOKEN_REFRESH_CACHE_KEY,
|
2020-12-11 15:37:55 -05:00
|
|
|
]);
|
2020-05-08 19:15:21 -04:00
|
|
|
|
2020-12-10 15:59:37 -05:00
|
|
|
$userData = array_filter((array)$userData, static fn ($value) => $value !== NULL);
|
2020-05-08 19:15:21 -04:00
|
|
|
$cleared = $cache->clear();
|
|
|
|
|
|
|
|
$saved = ( ! empty($userData))
|
|
|
|
? $cache->setMultiple($userData)
|
|
|
|
: TRUE;
|
|
|
|
|
|
|
|
return $cleared && $saved;
|
2020-08-26 17:26:42 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Render a PHP code template as a string
|
|
|
|
*
|
2020-12-11 15:37:55 -05:00
|
|
|
* @codeCoverageIgnore
|
2020-08-26 17:26:42 -04:00
|
|
|
* @param string $path
|
|
|
|
* @param array $data
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
function renderTemplate(string $path, array $data): string
|
|
|
|
{
|
|
|
|
ob_start();
|
|
|
|
extract($data, EXTR_OVERWRITE);
|
|
|
|
include $path;
|
2021-02-12 11:14:45 -05:00
|
|
|
$rawOutput = ob_get_clean();
|
|
|
|
return (is_string($rawOutput)) ? $rawOutput : '';
|
2018-08-16 12:10:24 -04:00
|
|
|
}
|