1 | <?php |
||
13 | class PublicController extends ApiController { |
||
14 | |||
15 | private $db; |
||
16 | private $userManager; |
||
17 | |||
18 | 3 | public function __construct($appName, IRequest $request, IDb $db, Manager $userManager) { |
|
25 | |||
26 | /** |
||
27 | * @param string $user |
||
28 | * @param string $password |
||
29 | * @param array $tags |
||
30 | * @param string $conjunction |
||
31 | * @param array $select |
||
32 | * @param string $sortby |
||
33 | * @return JSONResponse |
||
34 | * |
||
35 | * @CORS |
||
36 | * @NoAdminRequired |
||
37 | * @NoCSRFRequired |
||
38 | * @PublicPage |
||
39 | */ |
||
40 | 3 | public function returnAsJson($user, $password = null, $tags = array(), $conjunction = "or", $select = null, $sortby = "") { |
|
41 | |||
42 | 3 | if ($user == null || $this->userManager->userExists($user) == false) { |
|
43 | 3 | return $this->newJsonErrorMessage("User could not be identified"); |
|
44 | } |
||
45 | |||
46 | if (!is_array($tags)) { |
||
47 | if(is_string($tags) && $tags !== '') { |
||
48 | $tags = [ $tags ]; |
||
49 | } else { |
||
50 | $tags = array(); |
||
51 | } |
||
52 | } |
||
53 | |||
54 | $public = true; |
||
55 | |||
56 | if ($password != null) { |
||
|
|||
57 | $public = false; |
||
58 | } |
||
59 | |||
60 | |||
61 | if (!$public && !$this->userManager->checkPassword($user, $password)) { |
||
62 | |||
63 | $msg = 'REST API accessed with wrong password'; |
||
64 | Util::writeLog('bookmarks', $msg, Util::WARN); |
||
65 | |||
66 | return $this->newJsonErrorMessage("Wrong password for user " . $user); |
||
67 | } |
||
68 | |||
69 | $attributesToSelect = array('url', 'title'); |
||
70 | |||
71 | if ($select != null) { |
||
72 | $attributesToSelect = array_merge($attributesToSelect, $select); |
||
73 | $attributesToSelect = array_unique($attributesToSelect); |
||
74 | } |
||
75 | |||
76 | $output = Bookmarks::findBookmarks($user, $this->db, 0, $sortby, $tags, true, -1, $public, $attributesToSelect, $conjunction); |
||
77 | |||
78 | if (count($output) == 0) { |
||
79 | $output["status"] = 'error'; |
||
80 | $output["message"] = "No results from this query"; |
||
81 | return new JSONResponse($output); |
||
82 | } |
||
83 | |||
84 | return new JSONResponse($output); |
||
85 | } |
||
86 | |||
87 | /** |
||
88 | * @param string $message |
||
89 | * @return JSONResponse |
||
90 | */ |
||
91 | 3 | public function newJsonErrorMessage($message) { |
|
97 | |||
98 | } |
||
99 |