summaryrefslogtreecommitdiffstatshomepage
path: root/api
diff options
context:
space:
mode:
Diffstat (limited to 'api')
-rw-r--r--api/.htaccess10
-rw-r--r--api/httpauth.inc.php22
-rw-r--r--api/posts_add.php83
-rw-r--r--api/posts_all.php50
-rw-r--r--api/posts_dates.php42
-rw-r--r--api/posts_delete.php33
-rw-r--r--api/posts_get.php62
-rw-r--r--api/posts_recent.php63
-rw-r--r--api/posts_update.php26
-rw-r--r--api/tags_get.php25
-rw-r--r--api/tags_rename.php37
11 files changed, 453 insertions, 0 deletions
diff --git a/api/.htaccess b/api/.htaccess
new file mode 100644
index 0000000..8c48221
--- /dev/null
+++ b/api/.htaccess
@@ -0,0 +1,10 @@
+RewriteEngine On
+RewriteRule ^tags/get tags_get.php
+RewriteRule ^posts/dates posts_dates.php
+RewriteRule ^posts/get posts_get.php
+RewriteRule ^posts/recent posts_recent.php
+RewriteRule ^posts/all posts_all.php
+RewriteRule ^posts/update posts_update.php
+RewriteRule ^posts/add posts_add.php
+RewriteRule ^posts/delete posts_delete.php
+RewriteRule ^tags/rename tags_rename.php \ No newline at end of file
diff --git a/api/httpauth.inc.php b/api/httpauth.inc.php
new file mode 100644
index 0000000..bc26582
--- /dev/null
+++ b/api/httpauth.inc.php
@@ -0,0 +1,22 @@
+<?php
+// Provides HTTP Basic authentication of a user, and sets two variables, sId and username,
+// with the user's info.
+
+function authenticate() {
+ header('WWW-Authenticate: Basic realm="del.icio.us API"');
+ header('HTTP/1.0 401 Unauthorized');
+ die("Use of the API calls requires authentication.");
+}
+
+if (!isset($_SERVER['PHP_AUTH_USER'])) {
+ authenticate();
+} else {
+ require_once('../header.inc.php');
+ $userservice =& ServiceFactory::getServiceInstance('UserService');
+
+ $login = $userservice->login($_SERVER['PHP_AUTH_USER'], $_SERVER['PHP_AUTH_PW']);
+ if (!$login) {
+ authenticate();
+ }
+}
+?> \ No newline at end of file
diff --git a/api/posts_add.php b/api/posts_add.php
new file mode 100644
index 0000000..77c3288
--- /dev/null
+++ b/api/posts_add.php
@@ -0,0 +1,83 @@
+<?php
+// Implements the del.icio.us API request to add a new post.
+
+// del.icio.us behavior:
+// - tags can't have spaces
+// - address and description are mandatory
+
+// Scuttle behavior:
+// - Additional 'status' variable for privacy
+// - No support for 'replace' variable
+
+// Force HTTP authentication
+require_once('httpauth.inc.php');
+require_once('../header.inc.php');
+
+$bookmarkservice =& ServiceFactory::getServiceInstance('BookmarkService');
+$userservice =& ServiceFactory::getServiceInstance('UserService');
+
+// Get all the bookmark's passed-in information
+if (isset($_REQUEST['url']) && (trim($_REQUEST['url']) != ''))
+ $url = trim(urldecode($_REQUEST['url']));
+else
+ $url = NULL;
+
+if (isset($_REQUEST['description']) && (trim($_REQUEST['description']) != ''))
+ $description = trim($_REQUEST['description']);
+else
+ $description = NULL;
+
+if (isset($_REQUEST['extended']) && (trim($_REQUEST['extended']) != ""))
+ $extended = trim($_REQUEST['extended']);
+else
+ $extended = NULL;
+
+if (isset($_REQUEST['tags']) && (trim($_REQUEST['tags']) != '') && (trim($_REQUEST['tags']) != ','))
+ $tags = trim($_REQUEST['tags']);
+else
+ $tags = NULL;
+
+if (isset($_REQUEST['dt']) && (trim($_REQUEST['dt']) != ''))
+ $dt = trim($_REQUEST['dt']);
+else
+ $dt = NULL;
+
+$status = 0;
+if (isset($_REQUEST['status'])) {
+ $status_str = trim($_REQUEST['status']);
+ if (is_numeric($status_str)) {
+ $status = intval($status_str);
+ if($status < 0 || $status > 2) {
+ $status = 0;
+ }
+ } else {
+ switch ($status_str) {
+ case 'private':
+ $status = 2;
+ break;
+ case 'shared':
+ $status = 1;
+ break;
+ default:
+ $status = 0;
+ break;
+ }
+ }
+}
+
+// Error out if there's no address or description
+if (is_null($url) || is_null($description)) {
+ $added = false;
+} else {
+// We're good with info; now insert it!
+ if ($bookmarkservice->bookmarkExists($url, $userservice->getCurrentUserId()))
+ $added = false;
+ else
+ $added = $bookmarkservice->addBookmark($url, $description, $extended, $status, $tags, $dt, true);
+}
+
+// Set up the XML file and output the result.
+header('Content-Type: text/xml');
+echo '<?xml version="1.0" standalone="yes" ?'.">\r\n";
+echo '<result code="'. ($added ? 'done' : 'something went wrong') .'" />';
+?> \ No newline at end of file
diff --git a/api/posts_all.php b/api/posts_all.php
new file mode 100644
index 0000000..03026c4
--- /dev/null
+++ b/api/posts_all.php
@@ -0,0 +1,50 @@
+<?php
+// Implements the del.icio.us API request for all a user's posts, optionally filtered by tag.
+
+// del.icio.us behavior:
+// - doesn't include the filtered tag as an attribute on the root element (we do)
+
+// Force HTTP authentication first!
+require_once('httpauth.inc.php');
+require_once('../header.inc.php');
+
+$bookmarkservice =& ServiceFactory::getServiceInstance('BookmarkService');
+$userservice =& ServiceFactory::getServiceInstance('UserService');
+
+// Check to see if a tag was specified.
+if (isset($_REQUEST['tag']) && (trim($_REQUEST['tag']) != ''))
+ $tag = trim($_REQUEST['tag']);
+else
+ $tag = NULL;
+
+// Get the posts relevant to the passed-in variables.
+$bookmarks =& $bookmarkservice->getBookmarks(0, NULL, $userservice->getCurrentUserId(), $tag);
+
+$currentuser = $userservice->getCurrentUser();
+$currentusername = $currentuser[$userservice->getFieldName('username')];
+
+// Set up the XML file and output all the posts.
+header('Content-Type: text/xml');
+echo '<?xml version="1.0" standalone="yes" ?'.">\r\n";
+echo '<posts update="'. gmdate('Y-m-d\TH:i:s\Z') .'" user="'. htmlspecialchars($currentusername) .'"'. (is_null($tag) ? '' : ' tag="'. htmlspecialchars($tag) .'"') .">\r\n";
+
+foreach($bookmarks['bookmarks'] as $row) {
+ if (is_null($row['bDescription']) || (trim($row['bDescription']) == ''))
+ $description = '';
+ else
+ $description = 'extended="'. filter($row['bDescription'], 'xml') .'" ';
+
+ $taglist = '';
+ if (count($row['tags']) > 0) {
+ foreach($row['tags'] as $tag)
+ $taglist .= convertTag($tag) .' ';
+ $taglist = substr($taglist, 0, -1);
+ } else {
+ $taglist = 'system:unfiled';
+ }
+
+ echo "\t<post href=\"". filter($row['bAddress'], 'xml') .'" description="'. filter($row['bTitle'], 'xml') .'" '. $description .'hash="'. md5($row['bAddress']) .'" tag="'. filter($taglist, 'xml') .'" time="'. gmdate('Y-m-d\TH:i:s\Z', strtotime($row['bDatetime'])) ."\" />\r\n";
+}
+
+echo '</posts>';
+?> \ No newline at end of file
diff --git a/api/posts_dates.php b/api/posts_dates.php
new file mode 100644
index 0000000..7098756
--- /dev/null
+++ b/api/posts_dates.php
@@ -0,0 +1,42 @@
+<?php
+// Implements the del.icio.us API request for a user's post counts by date (and optionally
+// by tag).
+
+// Force HTTP authentication first!
+require_once('httpauth.inc.php');
+require_once('../header.inc.php');
+
+$bookmarkservice =& ServiceFactory::getServiceInstance('BookmarkService');
+$userservice =& ServiceFactory::getServiceInstance('UserService');
+
+// Check to see if a tag was specified.
+if (isset($_REQUEST['tag']) && (trim($_REQUEST['tag']) != ''))
+ $tag = trim($_REQUEST['tag']);
+else
+ $tag = NULL;
+
+// Get the posts relevant to the passed-in variables.
+$bookmarks =& $bookmarkservice->getBookmarks(0, NULL, $userservice->getCurrentUserId(), $tag);
+
+$currentuser = $userservice->getCurrentUser();
+$currentusername = $currentuser[$userservice->getFieldName('username')];
+
+// Set up the XML file and output all the tags.
+header('Content-Type: text/xml');
+echo '<?xml version="1.0" standalone="yes" ?'.">\r\n";
+echo '<dates tag="'. (is_null($tag) ? '' : filter($tag, 'xml')) .'" user="'. filter($currentusername, 'xml') ."\">\r\n";
+
+$lastdate = NULL;
+foreach($bookmarks['bookmarks'] as $row) {
+ $thisdate = gmdate('Y-m-d', strtotime($row['bDatetime']));
+ if ($thisdate != $lastdate && $lastdate != NULL) {
+ echo "\t<date count=\"". $count .'" date="'. $lastdate ."\" />\r\n";
+ $count = 1;
+ } else {
+ $count = $count + 1;
+ }
+ $lastdate = $thisdate;
+}
+
+echo "</dates>";
+?> \ No newline at end of file
diff --git a/api/posts_delete.php b/api/posts_delete.php
new file mode 100644
index 0000000..737a4fb
--- /dev/null
+++ b/api/posts_delete.php
@@ -0,0 +1,33 @@
+<?php
+// Implements the del.icio.us API request to delete a post.
+
+// del.icio.us behavior:
+// - returns "done" even if the bookmark doesn't exist;
+// - does NOT allow the hash for the url parameter;
+// - doesn't set the Content-Type to text/xml (we do).
+
+// Force HTTP authentication first!
+require_once('httpauth.inc.php');
+require_once('../header.inc.php');
+
+$bookmarkservice =& ServiceFactory::getServiceInstance('BookmarkService');
+$userservice =& ServiceFactory::getServiceInstance('UserService');
+
+// Note that del.icio.us only errors out if no URL was passed in; there's no error on attempting
+// to delete a bookmark you don't have.
+
+// Error out if there's no address
+if (is_null($_REQUEST['url'])) {
+ $deleted = false;
+} else {
+ $bookmark = $bookmarkservice->getBookmarkByAddress($_REQUEST['url']);
+ $bid = $bookmark['bId'];
+ $delete = $bookmarkservice->deleteBookmark($bid);
+ $deleted = true;
+}
+
+// Set up the XML file and output the result.
+header('Content-Type: text/xml');
+echo '<?xml version="1.0" standalone="yes" ?'.">\r\n";
+echo '<result code="'. ($deleted ? 'done' : 'something went wrong') .'" />';
+?> \ No newline at end of file
diff --git a/api/posts_get.php b/api/posts_get.php
new file mode 100644
index 0000000..8be067b
--- /dev/null
+++ b/api/posts_get.php
@@ -0,0 +1,62 @@
+<?php
+// Implements the del.icio.us API request for a user's posts, optionally filtered by tag and/or
+// date. Note that when using a date to select the posts returned, del.icio.us uses GMT dates --
+// so we do too.
+
+// del.icio.us behavior:
+// - includes an empty tag attribute on the root element when it hasn't been specified
+
+// Scuttle behavior:
+// - Uses today, instead of the last bookmarked date, if no date is specified
+
+// Force HTTP authentication first!
+require_once('httpauth.inc.php');
+require_once('../header.inc.php');
+
+$bookmarkservice =& ServiceFactory::getServiceInstance('BookmarkService');
+$userservice =& ServiceFactory::getServiceInstance('UserService');
+
+// Check to see if a tag was specified.
+if (isset($_REQUEST['tag']) && (trim($_REQUEST['tag']) != ''))
+ $tag = trim($_REQUEST['tag']);
+else
+ $tag = NULL;
+
+// Check to see if a date was specified; the format should be YYYY-MM-DD
+if (isset($_REQUEST['dt']) && (trim($_REQUEST['dt']) != ""))
+ $dtstart = trim($_REQUEST['dt']);
+else
+ $dtstart = date('Y-m-d H:i:s');
+$dtend = date('Y-m-d H:i:s', strtotime($dtstart .'+1 day'));
+
+// Get the posts relevant to the passed-in variables.
+$bookmarks =& $bookmarkservice->getBookmarks(0, NULL, $userservice->getCurrentUserId(), $tag, NULL, NULL, NULL, $dtstart, $dtend);
+
+$currentuser = $userservice->getCurrentUser();
+$currentusername = $currentuser[$userservice->getFieldName('username')];
+
+// Set up the XML file and output all the tags.
+header('Content-Type: text/xml');
+echo '<?xml version="1.0" standalone="yes" ?'.">\r\n";
+echo '<posts'. (is_null($dtstart) ? '' : ' dt="'. $dtstart .'"') .' tag="'. (is_null($tag) ? '' : filter($tag, 'xml')) .'" user="'. filter($currentusername, 'xml') ."\">\r\n";
+
+foreach($bookmarks['bookmarks'] as $row) {
+ if (is_null($row['bDescription']) || (trim($row['bDescription']) == ''))
+ $description = '';
+ else
+ $description = 'extended="'. filter($row['bDescription'], 'xml') .'" ';
+
+ $taglist = '';
+ if (count($row['tags']) > 0) {
+ foreach($row['tags'] as $tag)
+ $taglist .= convertTag($tag) .' ';
+ $taglist = substr($taglist, 0, -1);
+ } else {
+ $taglist = 'system:unfiled';
+ }
+
+ echo "\t<post href=\"". filter($row['bAddress'], 'xml') .'" description="'. filter($row['bTitle'], 'xml') .'" '. $description .'hash="'. $row['bHash'] .'" others="'. $bookmarkservice->countOthers($row['bAddress']) .'" tag="'. filter($taglist, 'xml') .'" time="'. gmdate('Y-m-d\TH:i:s\Z', strtotime($row['bDatetime'])) ."\" />\r\n";
+}
+
+echo '</posts>';
+?> \ No newline at end of file
diff --git a/api/posts_recent.php b/api/posts_recent.php
new file mode 100644
index 0000000..22fc2bd
--- /dev/null
+++ b/api/posts_recent.php
@@ -0,0 +1,63 @@
+<?php
+// Implements the del.icio.us API request for a user's recent posts, optionally filtered by
+// tag and/or number of posts (default 15, max 100, just like del.icio.us).
+
+// Set default and max number of posts
+$countDefault = 15;
+$countMax = 100;
+
+// Force HTTP authentication first!
+require_once('httpauth.inc.php');
+require_once('../header.inc.php');
+
+$bookmarkservice =& ServiceFactory::getServiceInstance('BookmarkService');
+$userservice =& ServiceFactory::getServiceInstance('UserService');
+
+// Check to see if a tag was specified.
+if (isset($_REQUEST['tag']) && (trim($_REQUEST['tag']) != ''))
+ $tag = trim($_REQUEST['tag']);
+else
+ $tag = NULL;
+
+// Check to see if the number of items was specified.
+if (isset($_REQUEST['count']) && (intval($_REQUEST['count']) != 0)) {
+ $count = intval($_REQUEST['count']);
+ if ($count > $countMax)
+ $count = $countMax;
+ elseif ($count < 0)
+ $count = 0;
+} else {
+ $count = $countDefault;
+}
+
+// Get the posts relevant to the passed-in variables.
+$bookmarks =& $bookmarkservice->getBookmarks(0, $count, $userservice->getCurrentUserId(), $tag);
+
+$currentuser = $userservice->getCurrentUser();
+$currentusername = $currentuser[$userservice->getFieldName('username')];
+
+// Set up the XML file and output all the tags.
+header('Content-Type: text/xml');
+echo '<?xml version="1.0" standalone="yes" ?'.">\r\n";
+echo '<posts tag="'. (is_null($tag) ? '' : filter($tag, 'xml')) .'" user="'. filter($currentusername, 'xml') ."\">\r\n";
+
+foreach($bookmarks['bookmarks'] as $row) {
+ if (is_null($row['bDescription']) || (trim($row['bDescription']) == ''))
+ $description = '';
+ else
+ $description = 'extended="'. filter($row['bDescription'], 'xml') .'" ';
+
+ $taglist = '';
+ if (count($row['tags']) > 0) {
+ foreach($row['tags'] as $tag)
+ $taglist .= convertTag($tag) .' ';
+ $taglist = substr($taglist, 0, -1);
+ } else {
+ $taglist = 'system:unfiled';
+ }
+
+ echo "\t<post href=\"". filter($row['bAddress'], 'xml') .'" description="'. filter($row['bTitle'], 'xml') .'" '. $description .'hash="'. $row['bHash'] .'" tag="'. filter($taglist, 'xml') .'" time="'. gmdate('Y-m-d\TH:i:s\Z', strtotime($row['bDatetime'])) ."\" />\r\n";
+}
+
+echo '</posts>';
+?> \ No newline at end of file
diff --git a/api/posts_update.php b/api/posts_update.php
new file mode 100644
index 0000000..de379d2
--- /dev/null
+++ b/api/posts_update.php
@@ -0,0 +1,26 @@
+<?php
+// Implements the del.icio.us API request for a user's last update time and date.
+
+// del.icio.us behavior:
+// - doesn't set the Content-Type to text/xml (we do).
+
+// Force HTTP authentication first!
+require_once('httpauth.inc.php');
+require_once('../header.inc.php');
+
+$bookmarkservice =& ServiceFactory::getServiceInstance('BookmarkService');
+$userservice =& ServiceFactory::getServiceInstance('UserService');
+
+// Get the posts relevant to the passed-in variables.
+$bookmarks =& $bookmarkservice->getBookmarks(0, 1, $userservice->getCurrentUserId());
+
+$currentuser = $userservice->getCurrentUser();
+$currentusername = $currentuser[$userservice->getFieldName('username')];
+
+// Set up the XML file and output all the tags.
+header('Content-Type: text/xml');
+echo '<?xml version="1.0" standalone="yes" ?'.">\r\n";
+foreach($bookmarks['bookmarks'] as $row) {
+ echo '<update time="'. gmdate('Y-m-d\TH:i:s\Z', strtotime($row['bDatetime'])) .'" />';
+}
+?> \ No newline at end of file
diff --git a/api/tags_get.php b/api/tags_get.php
new file mode 100644
index 0000000..2584566
--- /dev/null
+++ b/api/tags_get.php
@@ -0,0 +1,25 @@
+<?php
+// Implements the del.icio.us API request for all a user's tags.
+
+// del.icio.us behavior:
+// - tags can't have spaces
+
+// Force HTTP authentication first!
+require_once('httpauth.inc.php');
+require_once('../header.inc.php');
+
+$tagservice =& ServiceFactory::getServiceInstance('TagService');
+$userservice =& ServiceFactory::getServiceInstance('UserService');
+
+// Get the tags relevant to the passed-in variables.
+$tags =& $tagservice->getTags($userservice->getCurrentUserId());
+
+// Set up the XML file and output all the tags.
+header('Content-Type: text/xml');
+echo '<?xml version="1.0" standalone="yes" ?'.">\r\n";
+echo "<tags>\r\n";
+foreach($tags as $row) {
+ echo "\t<tag count=\"". $row['bCount'] .'" tag="'. filter(convertTag($row['tag'], 'out'), 'xml') ."\" />\r\n";
+}
+echo "</tags>";
+?> \ No newline at end of file
diff --git a/api/tags_rename.php b/api/tags_rename.php
new file mode 100644
index 0000000..20831e7
--- /dev/null
+++ b/api/tags_rename.php
@@ -0,0 +1,37 @@
+<?php
+// Implements the del.icio.us API request to rename a user's tag.
+
+// del.icio.us behavior:
+// - oddly, returns an entirely different result (<result></result>) than the other API calls.
+
+// Force HTTP authentication first!
+require_once('httpauth.inc.php');
+require_once('../header.inc.php');
+
+$tagservice =& ServiceFactory::getServiceInstance('TagService');
+$userservice =& ServiceFactory::getServiceInstance('UserService');
+
+// Get the tag info.
+if (isset($_REQUEST['old']) && (trim($_REQUEST['old']) != ''))
+ $old = trim($_REQUEST['old']);
+else
+ $old = NULL;
+
+if (isset($_REQUEST['new']) && (trim($_REQUEST['new']) != ''))
+ $new = trim($_REQUEST['new']);
+else
+ $new = NULL;
+
+if (is_null($old) || is_null($new)) {
+ $renamed = false;
+} else {
+ // Rename the tag.
+ $result = $tagservice->renameTag($userservice->getCurrentUserId(), $old, $new, true);
+ $renamed = $result;
+}
+
+// Set up the XML file and output the result.
+header('Content-Type: text/xml');
+echo '<?xml version="1.0" standalone="yes" ?'.">\r\n";
+echo '<result>'. ($renamed ? 'done' : 'something went wrong') .'</result>';
+?>