1
0
mirror of https://github.com/owncloud/bookmarks.git synced 2024-11-29 04:24:11 +01:00
OwncloudBookmarksOfficial/controller/rest/publiccontroller.php
ganomi ae81b9dcd2 This is a refactored / rewritten version of the bookmarks app using the app frameworks design and controller features.
Dependency Injection for user and db is used througout the controllers
The Routing features a consistent rest api
The Routing provides some legacy routes, so that for exampe the Android Bookmarks App still works.

There is a publicly available api that provides access to bookmarks per user.
(This is usefull in connection with the WP Plugin https://github.com/mario-nolte/oc2wp-bookmarks)
2014-11-26 12:24:18 +01:00

82 lines
1.9 KiB
PHP

<?php
namespace OCA\Bookmarks\Controller\Rest;
use \OCP\AppFramework\ApiController;
use \OCP\IRequest;
use \OCP\IDb;
use \OCP\AppFramework\Http\JSONResponse;
use \OC\User\Manager;
use OCA\Bookmarks\Controller\Lib\Bookmarks;
class PublicController extends ApiController {
private $db;
private $userManager;
public function __construct($appName, IRequest $request, IDb $db, Manager $userManager) {
parent::__construct(
$appName, $request);
$this->db = $db;
$this->userManager = $userManager;
}
/**
* @CORS
* @NoAdminRequired
* @NoCSRFRequired
* @PublicPage
*/
public function returnAsJson($user, $password = null, $tags = array(), $conjunction = "or", $select = null, $sortby = "") {
if ($user == null || $this->userManager->userExists($user) == false) {
return $this->newJsonErrorMessage("User could not be identified");
}
if ($tags[0] == "") {
$tags = array();
}
$public = true;
if ($password != null) {
$public = false;
}
if (!$public && !$this->userManager->checkPassword($user, $password)) {
$msg = 'REST API accessed with wrong password';
\OCP\Util::writeLog('bookmarks', $msg, \OCP\Util::WARN);
return $this->newJsonErrorMessage("Wrong password for user " . $user);
}
$attributesToSelect = array('url', 'title');
if ($select != null) {
$attributesToSelect = array_merge($attributesToSelect, $select);
$attributesToSelect = array_unique($attributesToSelect);
}
$output = Bookmarks::findBookmarks($user, $this->db, 0, $sortby, $tags, true, -1, $public, $attributesToSelect, $conjunction);
if (count($output) == 0) {
$output["status"] = 'error';
$output["message"] = "No results from this query";
return new JSONResponse($output);
}
return new JSONResponse($output);
}
public function newJsonErrorMessage($message) {
$output = array();
$output["status"] = 'error';
$output["message"] = $message;
return new JSONResponse($output);
}
}