From 2acd9835704e162303569fc8f6c836003483bb93 Mon Sep 17 00:00:00 2001 From: Daku Date: Wed, 7 Mar 2012 15:15:48 +0000 Subject: [PATCH 1/4] get user info via json api --- contrib/shimmie_api/main.php | 41 ++++++++++++++++++++++++++++++++---- 1 file changed, 37 insertions(+), 4 deletions(-) diff --git a/contrib/shimmie_api/main.php b/contrib/shimmie_api/main.php index b8b9bc0d..7a466798 100644 --- a/contrib/shimmie_api/main.php +++ b/contrib/shimmie_api/main.php @@ -30,11 +30,11 @@ class ShimmieApi extends Extension { public function onPageRequest(PageRequestEvent $event) { global $database, $page; - if($event->page_matches("api/shimmie")) { + if($event->page_matches("api")) { $page->set_mode("data"); $page->set_type("text/plain"); - if($event->page_matches("api/shimmie/get_tags")) { + if($event->page_matches("api/get_tags")) { if($event->count_args() == 2) { $all = $database->get_all( "SELECT tag FROM tags WHERE tag LIKE ?", @@ -48,14 +48,14 @@ class ShimmieApi extends Extension { $page->set_data(json_encode($res)); } - if($event->page_matches("api/shimmie/get_image")) { + if($event->page_matches("api/get_image")) { $image = Image::by_id(int_escape($event->get_arg(0))); $image->get_tag_array(); // tag data isn't loaded into the object until necessary $safe_image = new _SafeImage($image); $page->set_data(json_encode($safe_image)); } - if($event->page_matches("api/shimmie/find_images")) { + if($event->page_matches("api/find_images")) { $search_terms = $event->get_search_terms(); $page_number = $event->get_page_number(); $page_size = $event->get_page_size(); @@ -67,6 +67,39 @@ class ShimmieApi extends Extension { } $page->set_data(json_encode($safe_images)); } + + if($event->page_matches("api/get_user")) { + if(isset($_GET['name'])){ + $all = $database->get_all( + "SELECT id,name,joindate,class FROM users WHERE name=?", + array($_GET['name'])); + } + + if(isset($_GET['id'])){ + $all = $database->get_all( + "SELECT id,name,joindate,class FROM users WHERE id=?", + array($_GET['id'])); + } + + if(!isset($_GET['id']) && !isset($_GET['name'])){ + $all = $database->get_all( + "SELECT id,name,joindate,class FROM users WHERE id=?", + array("2")); //In 99% of cases, this will be the admin. + } + + $all = $all[0]; + //FIXME?: For some weird reason, get_all seems to return twice. Unsetting second value to make things look nice.. + /*TODO: Might be worth making it possible just to get a certain stat (Using &stat=uploadcount or something) + This would lessen strain on DB? */ + for($i=0; $i<4; $i++) unset($all[$i]); + $all['uploadcount'] = Image::count_images(array("user_id=".$all['id'])); + $all['uploadperday'] = sprintf("%.1f", ($all['uploadcount'] / (((time() - strtotime($all['joindate'])) / 86400) + 1))); + $all['commentcount'] = $database->get_one( + "SELECT COUNT(*) AS count FROM comments WHERE owner_id=:owner_id", + array("owner_id"=>$all['id'])); + $all['commentperday'] = sprintf("%.1f", ($all['commentcount'] / (((time() - strtotime($all['joindate'])) / 86400) + 1))); + $page->set_data(json_encode($all)); + } } } } From 4a51c734358d6de87ee1eec8fcf2985ba7476b99 Mon Sep 17 00:00:00 2001 From: Daku Date: Wed, 7 Mar 2012 17:15:13 +0000 Subject: [PATCH 2/4] some more api docs --- contrib/shimmie_api/main.php | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/contrib/shimmie_api/main.php b/contrib/shimmie_api/main.php index 7a466798..7483fbcb 100644 --- a/contrib/shimmie_api/main.php +++ b/contrib/shimmie_api/main.php @@ -6,6 +6,15 @@ * Documentation: * Admin Warning: this exposes private data, eg IP addresses *

Developer Warning: the API is unstable; notably, private data may get hidden + *

Usage: + *

get_tags - List of all tags. (May contain unused tags) + *

+ *

get_image - Get image via id. + *

+ *

find_images - List of latest 12(?) images. + *

get_user - Get user info. (Defaults to id=2 if both are empty) + *

+ * */ @@ -33,12 +42,16 @@ class ShimmieApi extends Extension { if($event->page_matches("api")) { $page->set_mode("data"); $page->set_type("text/plain"); + if(!$event->page_matches("api/get_tags") && !$event->page_matches("api/get_image") && !$event->page_matches("api/find_images") && !$event->page_matches("api/get_user")){ + $page->set_mode("redirect"); + $page->set_redirect(make_link("ext_doc/shimmie_api")); + } if($event->page_matches("api/get_tags")) { - if($event->count_args() == 2) { + if(isset($_GET['tag'])) { $all = $database->get_all( "SELECT tag FROM tags WHERE tag LIKE ?", - array($event->get_arg(0)."%")); + array($_GET['tag']."%")); } else { $all = $database->get_all("SELECT tag FROM tags"); @@ -49,7 +62,11 @@ class ShimmieApi extends Extension { } if($event->page_matches("api/get_image")) { - $image = Image::by_id(int_escape($event->get_arg(0))); + if(isset($_GET['id'])){ + $image = Image::by_id(int_escape($_GET['id'])); + }else{ + $image = Image::by_id(int_escape("1")); //Default to id=1 + } $image->get_tag_array(); // tag data isn't loaded into the object until necessary $safe_image = new _SafeImage($image); $page->set_data(json_encode($safe_image)); @@ -89,9 +106,9 @@ class ShimmieApi extends Extension { $all = $all[0]; //FIXME?: For some weird reason, get_all seems to return twice. Unsetting second value to make things look nice.. + for($i=0; $i<4; $i++) unset($all[$i]); /*TODO: Might be worth making it possible just to get a certain stat (Using &stat=uploadcount or something) This would lessen strain on DB? */ - for($i=0; $i<4; $i++) unset($all[$i]); $all['uploadcount'] = Image::count_images(array("user_id=".$all['id'])); $all['uploadperday'] = sprintf("%.1f", ($all['uploadcount'] / (((time() - strtotime($all['joindate'])) / 86400) + 1))); $all['commentcount'] = $database->get_one( From f76955123f0ee9ce8c031dc117fb8d5e71e95410 Mon Sep 17 00:00:00 2001 From: Daku Date: Thu, 8 Mar 2012 19:13:53 +0000 Subject: [PATCH 3/4] update source & rating if &update is set --- ext/image/main.php | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/ext/image/main.php b/ext/image/main.php index c999a5bc..255b6242 100644 --- a/ext/image/main.php +++ b/ext/image/main.php @@ -304,6 +304,12 @@ class ImageIO extends Extension { if($handler == "merge" || isset($_GET['update'])) { $merged = array_merge($image->get_tag_array(), $existing->get_tag_array()); send_event(new TagSetEvent($existing, $merged)); + if(isset($_GET['rating']) && isset($_GET['update']) && file_exists("ext/rating")){ + send_event(new RatingSetEvent($existing, $user, $_GET['rating'])); + } + if(isset($_GET['source']) && isset($_GET['update'])){ + send_event(new SourceSetEvent($existing, $_GET['source'])); + } return null; } else { From 3c11feae1c5aaf6718d3802cb029fa25624f0c74 Mon Sep 17 00:00:00 2001 From: Daku Date: Fri, 9 Mar 2012 21:09:15 +0000 Subject: [PATCH 4/4] changing back to api/shimmie & allow $_GET or args while using api --- contrib/shimmie_api/main.php | 30 +++++++++++++++++++++--------- 1 file changed, 21 insertions(+), 9 deletions(-) diff --git a/contrib/shimmie_api/main.php b/contrib/shimmie_api/main.php index 7483fbcb..a3150372 100644 --- a/contrib/shimmie_api/main.php +++ b/contrib/shimmie_api/main.php @@ -39,16 +39,23 @@ class ShimmieApi extends Extension { public function onPageRequest(PageRequestEvent $event) { global $database, $page; - if($event->page_matches("api")) { + if($event->page_matches("api/shimmie")) { $page->set_mode("data"); $page->set_type("text/plain"); - if(!$event->page_matches("api/get_tags") && !$event->page_matches("api/get_image") && !$event->page_matches("api/find_images") && !$event->page_matches("api/get_user")){ + if(!$event->page_matches("api/shimmie/get_tags") && !$event->page_matches("api/shimmie/get_image") && !$event->page_matches("api/shimmie/find_images") && !$event->page_matches("api/shimmie/get_user")){ $page->set_mode("redirect"); $page->set_redirect(make_link("ext_doc/shimmie_api")); } - if($event->page_matches("api/get_tags")) { - if(isset($_GET['tag'])) { + if($event->page_matches("api/shimmie/get_tags")){ + $arg = $event->get_arg(0); + + if(!empty($arg)){ + $all = $database->get_all( + "SELECT tag FROM tags WHERE tag LIKE ?", + array($arg."%")); + } + elseif(isset($_GET['id'])){ $all = $database->get_all( "SELECT tag FROM tags WHERE tag LIKE ?", array($_GET['tag']."%")); @@ -61,10 +68,15 @@ class ShimmieApi extends Extension { $page->set_data(json_encode($res)); } - if($event->page_matches("api/get_image")) { - if(isset($_GET['id'])){ + if($event->page_matches("api/shimmie/get_image")) { + $arg = $event->get_arg(0); + if(!empty($arg)){ + $image = Image::by_id(int_escape($event->get_arg(0))); + } + elseif(isset($_GET['id'])){ $image = Image::by_id(int_escape($_GET['id'])); - }else{ + } + else{ $image = Image::by_id(int_escape("1")); //Default to id=1 } $image->get_tag_array(); // tag data isn't loaded into the object until necessary @@ -72,7 +84,7 @@ class ShimmieApi extends Extension { $page->set_data(json_encode($safe_image)); } - if($event->page_matches("api/find_images")) { + if($event->page_matches("api/shimmie/find_images")) { $search_terms = $event->get_search_terms(); $page_number = $event->get_page_number(); $page_size = $event->get_page_size(); @@ -85,7 +97,7 @@ class ShimmieApi extends Extension { $page->set_data(json_encode($safe_images)); } - if($event->page_matches("api/get_user")) { + if($event->page_matches("api/shimmie/get_user")) { if(isset($_GET['name'])){ $all = $database->get_all( "SELECT id,name,joindate,class FROM users WHERE name=?",