Completed
Pull Request — master (#218)
by Eli
07:28
created

PublicController::returnAddAsJson()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 20
Code Lines 11

Duplication

Lines 20
Ratio 100 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 4
Bugs 0 Features 1
Metric Value
c 4
b 0
f 1
dl 20
loc 20
ccs 0
cts 12
cp 0
rs 9.4286
cc 3
eloc 11
nc 4
nop 4
crap 12
1
<?php
2
3
namespace OCA\Bookmarks\Controller\Rest;
4
5
use \OCP\AppFramework\ApiController;
6
use \OCP\IRequest;
7
use \OCP\IDb;
8
use \OCP\AppFramework\Http\JSONResponse;
9
use \OC\User\Manager;
10
use OCA\Bookmarks\Controller\Lib\Bookmarks;
11
12
class PublicController extends ApiController
13
{
14
15
    private $db;
16
    private $userManager;
17
18 3
    public function __construct($appName, IRequest $request, IDb $db, Manager $userManager)
19
    {
20 3
        parent::__construct(
21 3
            $appName, $request);
22
23 3
        $this->db = $db;
24 3
        $this->userManager = $userManager;
25 3
    }
26
27
28
    /**
29
     * @CORS
30
     * @NoAdminRequired
31
     * @NoCSRFRequired
32
     * @PublicPage
33
     */
34 3
    public function returnAsJson($user, $password = null, $tags = array(), $conjunction = "or", $select = null, $sortby = "")
35
    {
36
37 3
        if ($user == null || $this->userManager->userExists($user) == false) {
38 3
            return $this->newJsonErrorMessage("User could not be identified");
39
        }
40
41
        if ($tags[0] == "") {
42
            $tags = array();
43
        }
44
45
        $public = true;
46
47
        if ($password != null) {
48
            $public = false;
49
        }
50
51
52
        if (!$public && !$this->userManager->checkPassword($user, $password)) {
53
54
            $msg = 'REST API accessed with wrong password';
55
            \OCP\Util::writeLog('bookmarks', $msg, \OCP\Util::WARN);
56
57
            return $this->newJsonErrorMessage("Wrong password for user " . $user);
58
        }
59
60
        $attributesToSelect = array('url', 'title');
61
62 View Code Duplication
        if ($select != null) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
63
            $attributesToSelect = array_merge($attributesToSelect, $select);
64
            $attributesToSelect = array_unique($attributesToSelect);
65
        }
66
67
        $output = Bookmarks::findBookmarks($user, $this->db, 0, $sortby, $tags, true, -1, $public, $attributesToSelect, $conjunction);
68
69
        if (count($output) == 0) {
70
            $output["status"] = 'error';
71
            $output["message"] = "No results from this query";
72
            return new JSONResponse($output);
73
        }
74
75
        return new JSONResponse($output);
76
    }
77
78
79
    /**
80
     * @CORS
81
     * @NoAdminRequired
82
     * @NoCSRFRequired
83
     */
84
    public function returnPrivateAsJson($tags = array(), $conjunction = "or", $select = null, $sortby = "")
85
    {
86
87
	    $user = \OCP\User::getUser();
88
89
        if ($tags != null && $tags[0] == "") {
90
            $tags = array();
91
        }
92
93
        $attributesToSelect = array('url', 'title');
94
95 View Code Duplication
        if ($select != null) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
96
            $attributesToSelect = array_merge($attributesToSelect, $select);
97
            $attributesToSelect = array_unique($attributesToSelect);
98
        }
99
100
        $output = Bookmarks::findBookmarks($user, $this->db, 0, $sortby, $tags, true, -1, false, $attributesToSelect, $conjunction);
101
102
        if (count($output) == 0) {
103
            $output["status"] = 'error';
104
            $output["message"] = "No results from this query";
105
            return new JSONResponse($output);
106
        }
107
108
        return new JSONResponse($output);
109
    }
110
111
112
    /**
113
     * @CORS
114
     * @NoAdminRequired
115
     * @NoCSRFRequired
116
     */
117 View Code Duplication
    public function returnAddAsJson($url = "", $tags = array(), $title = "", $description = "")
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
118
    {
119
	    $user = \OCP\User::getUser();
120
121
        if ($tags[0] == "") {
122
            $tags = array();
123
        }
124
125
        $output = Bookmarks::addBookmark($user, $this->db, $url, $title, $tags, $description, false);
126
127
        if (count($output) == 0) {
128
            $output["status"] = 'error';
129
            $output["message"] = "No results from this query";
130
            return new JSONResponse($output);
131
        }
132
133
        $output = Bookmarks::findUniqueBookmark($output, $user, $this->db);
134
135
        return new JSONResponse($output);
136
    }
137
138
    /**
139
     * @CORS
140
     * @NoAdminRequired
141
     * @NoCSRFRequired
142
     */
143 View Code Duplication
    public function returnUpdateAsJson($id, $url = "", $tags = array(), $title = "", $description = "")
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
144
    {
145
	    $user = \OCP\User::getUser();
146
147
        if ($tags[0] == "") {
148
            $tags = array();
149
        }
150
151
        $output = Bookmarks::editBookmark($user, $this->db, $id, $url, $title, $tags, $description, false);
152
153
        if (count($output) == 0) {
154
            $output["status"] = 'error';
155
            $output["message"] = "No results from this query";
156
            return new JSONResponse($output);
157
        }
158
159
        $output = Bookmarks::findUniqueBookmark($output, $user, $this->db);
160
161
        return new JSONResponse($output);
162
    }
163
164
    /**
165
     * @CORS
166
     * @NoAdminRequired
167
     * @NoCSRFRequired
168
     */
169
    public function returnDeleteAsJson($id)
170
    {
171
	    $user = \OCP\User::getUser();
172
173
        $output = Bookmarks::deleteUrl($user, $this->db, $id);
174
175
        if (!$output) {
176
            $output = array();
177
            $output["status"] = 'error';
178
            $output["message"] = "Cannot delete bookmark";
179
            return new JSONResponse($output);
180
        } else {
181
            $output = array();
182
            $output["status"] = 'success';
183
            $output["message"] = "Bookmark deleted";
184
            return new JSONResponse($output);
185
        }
186
    }
187
188
    /**
189
     * @CORS
190
     * @NoAdminRequired
191
     * @NoCSRFRequired
192
     */
193
    public function returnClickBookmarkAsJson($url)
194
    {
195
	    $user = \OCP\User::getUser();
196
197
        // Check if it is a valid URL
198
        if (filter_var($url, FILTER_VALIDATE_URL) === FALSE) {
199
            return new JSONResponse(array(), Http::STATUS_BAD_REQUEST);
200
        }
201
202
        $query = $this->db->prepareQuery('
203
            UPDATE `*PREFIX*bookmarks`
204
            SET `clickcount` = `clickcount` + 1
205
            WHERE `user_id` = ?
206
                AND `url` LIKE ?
207
            ');
208
209
        $params = array($user, htmlspecialchars_decode($url));
210
        $query->execute($params);
211
212
        $output = array();
213
        $output["status"] = "success";
214
        return new JSONResponse($output);
215
    }
216
217 3
    public function newJsonErrorMessage($message)
218
    {
219 3
        $output = array();
220 3
        $output["status"] = 'error';
221 3
        $output["message"] = $message;
222 3
        return new JSONResponse($output);
223
    }
224
225
}
226