Revert "Merge tag 'v2.10.6'"
This reverts commit122ea4ab9e
, reversing changes made toc54a11e250
.
This commit is contained in:
@ -22,7 +22,6 @@ class _SafeOuroborosImage
|
||||
* Post Meta
|
||||
*/
|
||||
public ?int $change = null;
|
||||
/** @var array{n:int,s:int,json_class:string} */
|
||||
public ?array $created_at = null;
|
||||
public ?int $id = null;
|
||||
public ?int $parent_id = null;
|
||||
@ -69,18 +68,18 @@ class _SafeOuroborosImage
|
||||
// meta
|
||||
$this->change = intval($img->id); //DaFug is this even supposed to do? ChangeID?
|
||||
// Should be JSON specific, just strip this when converting to XML
|
||||
$this->created_at = ['n' => 123456789, 's' => strtotime_ex($img->posted), 'json_class' => 'Time'];
|
||||
$this->created_at = ['n' => 123456789, 's' => strtotime($img->posted), 'json_class' => 'Time'];
|
||||
$this->id = intval($img->id);
|
||||
$this->parent_id = null;
|
||||
|
||||
if (Extension::is_enabled(RatingsInfo::KEY) !== false) {
|
||||
if (Extension::is_enabled(RatingsInfo::KEY)!== false) {
|
||||
// 'u' is not a "valid" rating
|
||||
if ($img['rating'] == 's' || $img['rating'] == 'q' || $img['rating'] == 'e') {
|
||||
$this->rating = $img['rating'];
|
||||
if ($img->rating == 's' || $img->rating == 'q' || $img->rating == 'e') {
|
||||
$this->rating = $img->rating;
|
||||
}
|
||||
}
|
||||
if (Extension::is_enabled(NumericScoreInfo::KEY) !== false) {
|
||||
$this->score = $img['numeric_score'];
|
||||
if (Extension::is_enabled(NumericScoreInfo::KEY)!== false) {
|
||||
$this->score = $img->numeric_score;
|
||||
}
|
||||
|
||||
$this->source = $img->source;
|
||||
@ -104,8 +103,7 @@ class _SafeOuroborosImage
|
||||
|
||||
class OuroborosPost extends _SafeOuroborosImage
|
||||
{
|
||||
/** @var array{tmp_name:string,name:string} */
|
||||
public ?array $file = null;
|
||||
public array $file = [];
|
||||
public bool $is_rating_locked = false;
|
||||
public bool $is_note_locked = false;
|
||||
|
||||
@ -113,8 +111,6 @@ class OuroborosPost extends _SafeOuroborosImage
|
||||
* Initialize an OuroborosPost for creation
|
||||
* Mainly just acts as a wrapper and validation layer
|
||||
* @noinspection PhpMissingParentConstructorInspection
|
||||
*
|
||||
* @param array<string,mixed> $post
|
||||
*/
|
||||
public function __construct(array $post)
|
||||
{
|
||||
@ -139,19 +135,19 @@ class OuroborosPost extends _SafeOuroborosImage
|
||||
$this->rating = $post['rating'];
|
||||
}
|
||||
if (array_key_exists('source', $post)) {
|
||||
$this->file_url = filter_var_ex(
|
||||
$this->file_url = filter_var(
|
||||
urldecode($post['source']),
|
||||
FILTER_SANITIZE_URL
|
||||
);
|
||||
}
|
||||
if (array_key_exists('sourceurl', $post)) {
|
||||
$this->source = filter_var_ex(
|
||||
$this->source = filter_var(
|
||||
urldecode($post['sourceurl']),
|
||||
FILTER_SANITIZE_URL
|
||||
);
|
||||
}
|
||||
if (array_key_exists('description', $post)) {
|
||||
$this->description = filter_var_ex(
|
||||
$this->description = filter_var(
|
||||
$post['description'],
|
||||
FILTER_SANITIZE_STRING
|
||||
);
|
||||
@ -188,9 +184,6 @@ class _SafeOuroborosTag
|
||||
public string $name = '';
|
||||
public int $type = 0;
|
||||
|
||||
/**
|
||||
* @param array{id:int,tag:string,count:int} $tag
|
||||
*/
|
||||
public function __construct(array $tag)
|
||||
{
|
||||
$this->count = $tag['count'];
|
||||
@ -241,7 +234,7 @@ class OuroborosAPI extends Extension
|
||||
public const ERROR_POST_CREATE_DUPE = 'Duplicate';
|
||||
public const OK_POST_CREATE_UPDATE = 'Updated';
|
||||
|
||||
public function onPageRequest(PageRequestEvent $event): void
|
||||
public function onPageRequest(PageRequestEvent $event)
|
||||
{
|
||||
global $page, $user;
|
||||
|
||||
@ -260,7 +253,7 @@ class OuroborosAPI extends Extension
|
||||
if ($this->match('create')) {
|
||||
// Create
|
||||
if ($user->can(Permissions::CREATE_IMAGE)) {
|
||||
$md5 = !empty($_REQUEST['md5']) ? filter_var_ex($_REQUEST['md5'], FILTER_SANITIZE_STRING) : null;
|
||||
$md5 = !empty($_REQUEST['md5']) ? filter_var($_REQUEST['md5'], FILTER_SANITIZE_STRING) : null;
|
||||
$this->postCreate(new OuroborosPost($_REQUEST['post']), $md5);
|
||||
} else {
|
||||
$this->sendResponse(403, 'You cannot create new posts');
|
||||
@ -269,17 +262,17 @@ class OuroborosAPI extends Extension
|
||||
throw new SCoreException("update not implemented");
|
||||
} elseif ($this->match('show')) {
|
||||
// Show
|
||||
$id = !empty($_REQUEST['id']) ? (int)filter_var_ex($_REQUEST['id'], FILTER_SANITIZE_NUMBER_INT) : null;
|
||||
$id = !empty($_REQUEST['id']) ? filter_var($_REQUEST['id'], FILTER_SANITIZE_NUMBER_INT) : null;
|
||||
$this->postShow($id);
|
||||
} elseif ($this->match('index') || $this->match('list')) {
|
||||
// List
|
||||
$limit = !empty($_REQUEST['limit']) ? intval(
|
||||
filter_var_ex($_REQUEST['limit'], FILTER_SANITIZE_NUMBER_INT)
|
||||
filter_var($_REQUEST['limit'], FILTER_SANITIZE_NUMBER_INT)
|
||||
) : 45;
|
||||
$p = !empty($_REQUEST['page']) ? intval(
|
||||
filter_var_ex($_REQUEST['page'], FILTER_SANITIZE_NUMBER_INT)
|
||||
filter_var($_REQUEST['page'], FILTER_SANITIZE_NUMBER_INT)
|
||||
) : 1;
|
||||
$tags = !empty($_REQUEST['tags']) ? filter_var_ex($_REQUEST['tags'], FILTER_SANITIZE_STRING) : [];
|
||||
$tags = !empty($_REQUEST['tags']) ? filter_var($_REQUEST['tags'], FILTER_SANITIZE_STRING) : [];
|
||||
if (is_string($tags)) {
|
||||
$tags = Tag::explode($tags);
|
||||
}
|
||||
@ -288,23 +281,23 @@ class OuroborosAPI extends Extension
|
||||
} elseif ($event->page_matches('tag')) {
|
||||
if ($this->match('index') || $this->match('list')) {
|
||||
$limit = !empty($_REQUEST['limit']) ? intval(
|
||||
filter_var_ex($_REQUEST['limit'], FILTER_SANITIZE_NUMBER_INT)
|
||||
filter_var($_REQUEST['limit'], FILTER_SANITIZE_NUMBER_INT)
|
||||
) : 50;
|
||||
$p = !empty($_REQUEST['page']) ? intval(
|
||||
filter_var_ex($_REQUEST['page'], FILTER_SANITIZE_NUMBER_INT)
|
||||
filter_var($_REQUEST['page'], FILTER_SANITIZE_NUMBER_INT)
|
||||
) : 1;
|
||||
$order = (!empty($_REQUEST['order']) && ($_REQUEST['order'] == 'date' || $_REQUEST['order'] == 'count' || $_REQUEST['order'] == 'name')) ? filter_var_ex(
|
||||
$order = (!empty($_REQUEST['order']) && ($_REQUEST['order'] == 'date' || $_REQUEST['order'] == 'count' || $_REQUEST['order'] == 'name')) ? filter_var(
|
||||
$_REQUEST['order'],
|
||||
FILTER_SANITIZE_STRING
|
||||
) : 'date';
|
||||
$id = !empty($_REQUEST['id']) ? intval(
|
||||
filter_var_ex($_REQUEST['id'], FILTER_SANITIZE_NUMBER_INT)
|
||||
filter_var($_REQUEST['id'], FILTER_SANITIZE_NUMBER_INT)
|
||||
) : null;
|
||||
$after_id = !empty($_REQUEST['after_id']) ? intval(
|
||||
filter_var_ex($_REQUEST['after_id'], FILTER_SANITIZE_NUMBER_INT)
|
||||
filter_var($_REQUEST['after_id'], FILTER_SANITIZE_NUMBER_INT)
|
||||
) : null;
|
||||
$name = !empty($_REQUEST['name']) ? filter_var_ex($_REQUEST['name'], FILTER_SANITIZE_STRING) : '';
|
||||
$name_pattern = !empty($_REQUEST['name_pattern']) ? filter_var_ex(
|
||||
$name = !empty($_REQUEST['name']) ? filter_var($_REQUEST['name'], FILTER_SANITIZE_STRING) : '';
|
||||
$name_pattern = !empty($_REQUEST['name_pattern']) ? filter_var(
|
||||
$_REQUEST['name_pattern'],
|
||||
FILTER_SANITIZE_STRING
|
||||
) : '';
|
||||
@ -326,9 +319,9 @@ class OuroborosAPI extends Extension
|
||||
/**
|
||||
* Wrapper for post creation
|
||||
*/
|
||||
protected function postCreate(OuroborosPost $post, ?string $md5 = ''): void
|
||||
protected function postCreate(OuroborosPost $post, ?string $md5 = '')
|
||||
{
|
||||
global $config, $database;
|
||||
global $config;
|
||||
$handler = $config->get_string(ImageConfig::UPLOAD_COLLISION_HANDLER);
|
||||
if (!empty($md5) && !($handler == ImageConfig::COLLISION_MERGE)) {
|
||||
$img = Image::by_hash($md5);
|
||||
@ -338,24 +331,22 @@ class OuroborosAPI extends Extension
|
||||
}
|
||||
}
|
||||
$meta = [];
|
||||
$meta['tags'] = Tag::explode($post->tags);
|
||||
$meta['tags'] = is_array($post->tags) ? $post->tags : Tag::explode($post->tags);
|
||||
$meta['source'] = $post->source;
|
||||
if (Extension::is_enabled(RatingsInfo::KEY) !== false) {
|
||||
if (Extension::is_enabled(RatingsInfo::KEY)!== false) {
|
||||
$meta['rating'] = $post->rating;
|
||||
}
|
||||
// Check where we should try for the file
|
||||
if (empty($post->file) && !empty($post->file_url) && filter_var_ex(
|
||||
if (empty($post->file) && !empty($post->file_url) && filter_var(
|
||||
$post->file_url,
|
||||
FILTER_VALIDATE_URL
|
||||
) !== false
|
||||
) {
|
||||
// Transload from source
|
||||
$meta['file'] = shm_tempnam('transload_' . $config->get_string(UploadConfig::TRANSLOAD_ENGINE));
|
||||
$meta['file'] = tempnam(sys_get_temp_dir(), 'shimmie_transload_' . $config->get_string(UploadConfig::TRANSLOAD_ENGINE));
|
||||
$meta['filename'] = basename($post->file_url);
|
||||
try {
|
||||
fetch_url($post->file_url, $meta['file']);
|
||||
} catch (FetchException $e) {
|
||||
$this->sendResponse(500, "Transloading failed: $e");
|
||||
if (!fetch_url($post->file_url, $meta['file'])) {
|
||||
$this->sendResponse(500, 'Transloading failed');
|
||||
return;
|
||||
}
|
||||
$meta['hash'] = md5_file($meta['file']);
|
||||
@ -374,7 +365,7 @@ class OuroborosAPI extends Extension
|
||||
if (!is_null($img)) {
|
||||
$handler = $config->get_string(ImageConfig::UPLOAD_COLLISION_HANDLER);
|
||||
if ($handler == ImageConfig::COLLISION_MERGE) {
|
||||
$postTags = Tag::explode($post->tags);
|
||||
$postTags = is_array($post->tags) ? $post->tags : Tag::explode($post->tags);
|
||||
$merged = array_merge($postTags, $img->get_tag_array());
|
||||
send_event(new TagSetEvent($img, $merged));
|
||||
|
||||
@ -392,21 +383,27 @@ class OuroborosAPI extends Extension
|
||||
}
|
||||
$meta['extension'] = pathinfo($meta['filename'], PATHINFO_EXTENSION);
|
||||
try {
|
||||
$image = $database->with_savepoint(function () use ($meta) {
|
||||
$dae = send_event(new DataUploadEvent($meta['file'], $meta));
|
||||
return $dae->images[0];
|
||||
});
|
||||
$this->sendResponse(200, make_link('post/view/' . $image->id), true);
|
||||
send_event(new DataUploadEvent($meta['file'], $meta));
|
||||
$image = Image::by_hash($meta['hash']);
|
||||
if (!is_null($image)) {
|
||||
$this->sendResponse(200, make_link('post/view/' . $image->id), true);
|
||||
return;
|
||||
} else {
|
||||
// Fail, unsupported file?
|
||||
$this->sendResponse(500, 'Unknown error');
|
||||
return;
|
||||
}
|
||||
} catch (UploadException $e) {
|
||||
// Cleanup in case shit hit the fan
|
||||
$this->sendResponse(500, $e->getMessage());
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Wrapper for getting a single post
|
||||
*/
|
||||
protected function postShow(int $id = null): void
|
||||
protected function postShow(int $id = null)
|
||||
{
|
||||
if (!is_null($id)) {
|
||||
$post = new _SafeOuroborosImage(Image::by_id($id));
|
||||
@ -418,12 +415,12 @@ class OuroborosAPI extends Extension
|
||||
|
||||
/**
|
||||
* Wrapper for getting a list of posts
|
||||
* @param string[] $tags
|
||||
* #param string[] $tags
|
||||
*/
|
||||
protected function postIndex(int $limit, int $page, array $tags): void
|
||||
protected function postIndex(int $limit, int $page, array $tags)
|
||||
{
|
||||
$start = ($page - 1) * $limit;
|
||||
$results = Search::find_images(max($start, 0), min($limit, 100), $tags);
|
||||
$results = Image::find_images(max($start, 0), min($limit, 100), $tags);
|
||||
$posts = [];
|
||||
foreach ($results as $img) {
|
||||
if (!is_object($img)) {
|
||||
@ -438,7 +435,7 @@ class OuroborosAPI extends Extension
|
||||
* Tag
|
||||
*/
|
||||
|
||||
protected function tagIndex(int $limit, int $page, string $order, int $id, int $after_id, string $name, string $name_pattern): void
|
||||
protected function tagIndex(int $limit, int $page, string $order, int $id, int $after_id, string $name, string $name_pattern)
|
||||
{
|
||||
global $database, $config;
|
||||
$start = ($page - 1) * $limit;
|
||||
@ -459,17 +456,20 @@ class OuroborosAPI extends Extension
|
||||
default:
|
||||
$tag_data = $database->get_all(
|
||||
"
|
||||
SELECT id, tag, count
|
||||
FROM tags
|
||||
WHERE count >= :tags_min
|
||||
ORDER BY count DESC, tag ASC LIMIT :start, :max_items
|
||||
",
|
||||
SELECT id, tag, count
|
||||
FROM tags
|
||||
WHERE count >= :tags_min
|
||||
ORDER BY count DESC, tag ASC LIMIT :start, :max_items
|
||||
",
|
||||
['tags_min' => $config->get_int(TagListConfig::TAGS_MIN), 'start' => $start, 'max_items' => $limit]
|
||||
);
|
||||
break;
|
||||
}
|
||||
$tags = [];
|
||||
foreach ($tag_data as $tag) {
|
||||
if (!is_array($tag)) {
|
||||
continue;
|
||||
}
|
||||
$tags[] = new _SafeOuroborosTag($tag);
|
||||
}
|
||||
$this->sendData('tag', $tags, $start);
|
||||
@ -482,7 +482,7 @@ class OuroborosAPI extends Extension
|
||||
/**
|
||||
* Sends a simple {success,reason} message to browser
|
||||
*/
|
||||
private function sendResponse(int $code = 200, string $reason = '', bool $location = false): void
|
||||
private function sendResponse(int $code = 200, string $reason = '', bool $location = false)
|
||||
{
|
||||
global $page;
|
||||
if ($code == 200) {
|
||||
@ -514,7 +514,7 @@ class OuroborosAPI extends Extension
|
||||
$response['location'] = $response['reason'];
|
||||
unset($response['reason']);
|
||||
}
|
||||
$response = json_encode_ex($response);
|
||||
$response = json_encode($response);
|
||||
} elseif ($this->type == 'xml') {
|
||||
// Seriously, XML sucks...
|
||||
$xml = new \XMLWriter();
|
||||
@ -535,33 +535,32 @@ class OuroborosAPI extends Extension
|
||||
$page->set_data($response);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param list<_SafeOuroborosTag>|list<_SafeOuroborosImage> $data
|
||||
*/
|
||||
private function sendData(string $type = '', array $data = [], int $offset = 0): void
|
||||
private function sendData(string $type = '', array $data = [], int $offset = 0)
|
||||
{
|
||||
global $page;
|
||||
$response = '';
|
||||
if ($this->type == 'json') {
|
||||
$response = json_encode_ex($data);
|
||||
$response = json_encode($data);
|
||||
} elseif ($this->type == 'xml') {
|
||||
$xml = new \XMLWriter();
|
||||
$xml->openMemory();
|
||||
$xml->startDocument('1.0', 'utf-8');
|
||||
|
||||
$xml->startElement($type . 's');
|
||||
if ($type == 'post') {
|
||||
$xml->writeAttribute('count', (string)count($data));
|
||||
$xml->writeAttribute('offset', (string)$offset);
|
||||
if (array_key_exists(0, $data)) {
|
||||
$xml->startElement($type . 's');
|
||||
if ($type == 'post') {
|
||||
$xml->writeAttribute('count', (string)count($data));
|
||||
$xml->writeAttribute('offset', (string)$offset);
|
||||
}
|
||||
if ($type == 'tag') {
|
||||
$xml->writeAttribute('type', 'array');
|
||||
}
|
||||
foreach ($data as $item) {
|
||||
$this->createItemXML($xml, $type, $item);
|
||||
}
|
||||
$xml->endElement();
|
||||
} else {
|
||||
$this->createItemXML($xml, $type, $data);
|
||||
}
|
||||
if ($type == 'tag') {
|
||||
$xml->writeAttribute('type', 'array');
|
||||
}
|
||||
foreach ($data as $item) {
|
||||
$this->createItemXML($xml, $type, $item);
|
||||
}
|
||||
$xml->endElement();
|
||||
|
||||
$xml->endDocument();
|
||||
$response = $xml->outputMemory(true);
|
||||
unset($xml);
|
||||
@ -569,10 +568,10 @@ class OuroborosAPI extends Extension
|
||||
$page->set_data($response);
|
||||
}
|
||||
|
||||
private function createItemXML(\XMLWriter $xml, string $type, _SafeOuroborosTag|_SafeOuroborosImage $item): void
|
||||
private function createItemXML(\XMLWriter $xml, string $type, $item)
|
||||
{
|
||||
$xml->startElement($type);
|
||||
foreach (json_decode(json_encode_ex($item)) as $key => $val) {
|
||||
foreach ($item as $key => $val) {
|
||||
if ($key == 'created_at' && $type == 'post') {
|
||||
$xml->writeAttribute($key, $val['s']);
|
||||
} else {
|
||||
@ -591,7 +590,7 @@ class OuroborosAPI extends Extension
|
||||
* Currently checks for either user & session in request or cookies
|
||||
* and initializes a global User
|
||||
*/
|
||||
private function tryAuth(): void
|
||||
private function tryAuth()
|
||||
{
|
||||
global $config, $user;
|
||||
|
||||
|
Reference in New Issue
Block a user