This repository has been archived on 2024-09-05. You can view files and clone it, but cannot push or open issues or pull requests.
shimmie2/ext/shimmie_api/main.php

168 lines
5.1 KiB
PHP
Raw Normal View History

2010-04-26 01:34:08 +00:00
<?php
/*
* Name: [Beta] Shimmie JSON API
* Author: Shish <webmaster@shishnet.org>
* Description: A JSON interface to shimmie data [WARNING]
* Documentation:
2012-03-11 02:27:16 +00:00
* <b>Admin Warning -</b> this exposes private data, eg IP addresses
* <p><b>Developer Warning -</b> the API is unstable; notably, private data may get hidden
2012-03-07 17:15:13 +00:00
* <p><b><u>Usage:</b></u>
* <p><b>get_tags</b> - List of all tags. (May contain unused tags)
* <br><ul>tags - <i>Optional</i> - Search for more specific tags (Searchs TAG*)</ul>
* <p><b>get_image</b> - Get image via id.
* <br><ul>id - <i>Required</i> - User id. (Defaults to id=1 if empty)</ul>
* <p><b>find_images</b> - List of latest 12(?) images.
* <p><b>get_user</b> - Get user info. (Defaults to id=2 if both are empty)
* <br><ul>id - <i>Optional</i> - User id.</ul>
* <ul>name - <i>Optional</i> - User name.</ul>
2010-04-26 01:34:08 +00:00
*/
2010-07-09 12:50:26 +00:00
class _SafeImage {
public $id;
public $height;
public $width;
public $hash;
public $filesize;
public $ext;
public $posted;
public $source;
public $owner_id;
public $tags;
2010-07-09 12:50:26 +00:00
function __construct(Image $img) {
$this->id = $img->id;
$this->height = $img->height;
$this->width = $img->width;
$this->hash = $img->hash;
$this->filesize = $img->filesize;
$this->ext = $img->ext;
2015-09-27 20:09:27 +00:00
$this->posted = strtotime($img->posted);
2010-07-09 12:50:26 +00:00
$this->source = $img->source;
$this->owner_id = $img->owner_id;
2014-04-28 06:23:45 +00:00
$this->tags = $img->get_tag_array();
2010-07-09 12:50:26 +00:00
}
}
class ShimmieApi extends Extension {
2010-04-26 01:34:08 +00:00
public function onPageRequest(PageRequestEvent $event) {
2015-09-27 01:00:02 +00:00
global $page, $user;
2010-04-26 01:34:08 +00:00
if($event->page_matches("api/shimmie")) {
2010-04-26 01:34:08 +00:00
$page->set_mode("data");
$page->set_type("text/plain");
if($event->page_matches("api/shimmie/get_tags")){
2015-09-27 01:00:02 +00:00
$tag = $event->get_arg(0);
if(empty($tag) && isset($_GET['tag'])) $tag = $_GET['tag'];
$res = $this->api_get_tags($tag);
2010-04-26 01:34:08 +00:00
$page->set_data(json_encode($res));
}
2015-09-27 01:00:02 +00:00
elseif($event->page_matches("api/shimmie/get_image")) {
$arg = $event->get_arg(0);
2015-09-27 01:00:02 +00:00
if(empty($arg) && isset($_GET['id'])) $arg = $_GET['id'];
$image = Image::by_id(int_escape($arg));
2012-03-10 09:52:52 +00:00
// FIXME: handle null image
2010-04-26 01:34:08 +00:00
$image->get_tag_array(); // tag data isn't loaded into the object until necessary
2010-07-09 12:50:26 +00:00
$safe_image = new _SafeImage($image);
$page->set_data(json_encode($safe_image));
2010-04-26 01:34:08 +00:00
}
2015-09-27 01:00:02 +00:00
elseif($event->page_matches("api/shimmie/find_images")) {
2010-04-26 01:34:08 +00:00
$search_terms = $event->get_search_terms();
$page_number = $event->get_page_number();
$page_size = $event->get_page_size();
$images = Image::find_images(($page_number-1)*$page_size, $page_size, $search_terms);
2010-07-09 12:50:26 +00:00
$safe_images = array();
foreach($images as $image) {
$image->get_tag_array();
$safe_images[] = new _SafeImage($image);
}
$page->set_data(json_encode($safe_images));
2010-04-26 01:34:08 +00:00
}
2012-03-07 15:15:48 +00:00
2015-09-27 01:00:02 +00:00
elseif($event->page_matches("api/shimmie/get_user")) {
2012-03-10 09:52:52 +00:00
$query = $user->id;
2012-03-12 08:10:36 +00:00
$type = "id";
2012-03-10 09:52:52 +00:00
if($event->count_args() == 1) {
$query = $event->get_arg(0);
2015-08-09 11:14:28 +00:00
$type = "name";
2012-03-07 15:15:48 +00:00
}
2012-03-12 08:10:36 +00:00
elseif(isset($_GET['id'])) {
2012-03-10 09:52:52 +00:00
$query = $_GET['id'];
2012-03-07 15:15:48 +00:00
}
2012-03-12 08:10:36 +00:00
elseif(isset($_GET['name'])) {
$query = $_GET['name'];
$type = "name";
}
2012-03-07 15:15:48 +00:00
2015-09-27 01:00:02 +00:00
$all = $this->api_get_user($type, $query);
2012-03-07 15:15:48 +00:00
$page->set_data(json_encode($all));
}
2015-09-27 01:00:02 +00:00
else {
$page->set_mode("redirect");
$page->set_redirect(make_link("ext_doc/shimmie_api"));
}
}
}
/**
2019-05-28 16:31:20 +00:00
* #return string[]
2015-09-27 01:00:02 +00:00
*/
2019-05-28 16:31:20 +00:00
private function api_get_tags(string $arg): array {
2015-09-27 01:00:02 +00:00
global $database;
if (!empty($arg)) {
$all = $database->get_all("SELECT tag FROM tags WHERE tag LIKE ?", array($arg . "%"));
} else {
$all = $database->get_all("SELECT tag FROM tags");
}
$res = array();
foreach ($all as $row) {
$res[] = $row["tag"];
}
return $res;
}
2017-09-19 17:55:43 +00:00
private function api_get_user(string $type, string $query): array {
2015-09-27 01:00:02 +00:00
global $database;
$all = $database->get_row(
"SELECT id, name, joindate, class FROM users WHERE $type=?",
array($query)
);
if (!empty($all)) {
//FIXME?: For some weird reason, get_all seems to return twice. Unsetting second value to make things look nice..
// - it returns data as eg array(0=>1234, 'id'=>1234, 1=>'bob', 'name'=>bob, ...);
for ($i = 0; $i < 4; $i++) unset($all[$i]);
$all['uploadcount'] = Image::count_images(array("user_id=" . $all['id']));
$all['commentcount'] = $database->get_one(
"SELECT COUNT(*) AS count FROM comments WHERE owner_id=:owner_id",
array("owner_id" => $all['id']));
if (isset($_GET['recent'])) {
$recent = $database->get_all(
"SELECT * FROM images WHERE owner_id=? ORDER BY id DESC LIMIT 0, 5",
array($all['id']));
$i = 0;
foreach ($recent as $all['recentposts'][$i]) {
unset($all['recentposts'][$i]['owner_id']); //We already know the owners id..
unset($all['recentposts'][$i]['owner_ip']);
for ($x = 0; $x < 14; $x++) unset($all['recentposts'][$i][$x]);
if (empty($all['recentposts'][$i]['author'])) unset($all['recentposts'][$i]['author']);
if ($all['recentposts'][$i]['notes'] > 0) $all['recentposts'][$i]['has_notes'] = "Y";
else $all['recentposts'][$i]['has_notes'] = "N";
unset($all['recentposts'][$i]['notes']);
$i += 1;
}
}
2010-04-26 01:34:08 +00:00
}
2015-09-27 01:00:02 +00:00
return $all;
2010-04-26 01:34:08 +00:00
}
}