bookmarks.php
13.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
<?php
/**
* ownCloud - bookmarks plugin
*
* @author Arthur Schiwon
* @copyright 2011 Arthur Schiwon blizzz@arthur-schiwon.de
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
* License as published by the Free Software Foundation; either
* version 3 of the License, or any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU AFFERO GENERAL PUBLIC LICENSE for more details.
*
* You should have received a copy of the GNU Affero General Public
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
*
*/
/**
* This class manages bookmarks
*/
class OC_Bookmarks_Bookmarks{
/**
* @brief Finds all tags for bookmarks
* @param filterTags array of tag to look for if empty then every tag
* @param offset result offset
* @param limit number of item to return
*/
public static function findTags($filterTags = array(), $offset = 0, $limit = 10){
$params = array_merge($filterTags, $filterTags);
array_unshift($params, OCP\USER::getUser());
$not_in = '';
if(!empty($filterTags) ) {
$exist_clause = " AND exists (select 1 from *PREFIX*bookmarks_tags
t2 where t2.bookmark_id = t.bookmark_id and tag = ?) ";
$not_in = ' AND tag not in ('. implode(',', array_fill(0, count($filterTags), '?') ) .')'.
str_repeat($exist_clause, count($filterTags));
}
$sql = 'SELECT tag, count(*) as nbr from *PREFIX*bookmarks_tags t '.
' WHERE EXISTS( SELECT 1 from *PREFIX*bookmarks bm where t.bookmark_id = bm.id and user_id = ?) '.
$not_in.
' group by tag Order by nbr DESC ';
$query = OCP\DB::prepare($sql, $limit, $offset);
$tags = $query->execute($params)->fetchAll();
return $tags;
}
public static function findOneBookmark($id) {
$CONFIG_DBTYPE = OCP\Config::getSystemValue( 'dbtype', 'sqlite' );
if($CONFIG_DBTYPE == 'pgsql') {
$group_fct = 'array_agg(tag)';
}
else {
$group_fct = 'GROUP_CONCAT(tag)';
}
$sql = "SELECT *, (select $group_fct from *PREFIX*bookmarks_tags where bookmark_id = b.id) as tags
FROM *PREFIX*bookmarks b
WHERE user_id = ? and id = ?";
$query = OCP\DB::prepare($sql);
$result = $query->execute(array(OCP\USER::getUser(), $id))->fetchRow();
$result['tags'] = explode(',', $result['tags']);
return $result;
}
/**
* @brief Finds all bookmarks, matching the filter
* @param offset result offset
* @param sqlSortColumn sort result with this column
* @param filters can be: empty -> no filter, a string -> filter this, a string array -> filter for all strings
* @param filterTagOnly if true, filter affects only tags, else filter affects url, title and tags
* @param limit number of item to return (default 10) if -1 or false then all item are returned
* @return void
*/
public static function findBookmarks($offset, $sqlSortColumn, $filters, $filterTagOnly, $limit = 10) {
$CONFIG_DBTYPE = OCP\Config::getSystemValue( 'dbtype', 'sqlite' );
if(is_string($filters)) $filters = array($filters);
if(! in_array($sqlSortColumn, array('id', 'url', 'title', 'user_id',
'description', 'public', 'added', 'lastmodified','clickcount',))) {
$sqlSortColumn = 'bookmarks_sorting_recent';
}
$params=array(OCP\USER::getUser());
if($CONFIG_DBTYPE == 'pgsql') {
$sql = "select * from (SELECT *, (select array_to_string(array_agg(tag),'') from *PREFIX*bookmarks_tags where bookmark_id = b.id) as tags
FROM *PREFIX*bookmarks b
WHERE user_id = ? ) as x WHERE true ";
}
else {
$sql = "SELECT *, (select GROUP_CONCAT(tag) from *PREFIX*bookmarks_tags where bookmark_id = b.id) as tags
FROM *PREFIX*bookmarks b
WHERE user_id = ? ";
}
if($filterTagOnly) {
$exist_clause = " AND exists (select id from *PREFIX*bookmarks_tags
t2 where t2.bookmark_id = b.id and tag = ?) ";
$sql .= str_repeat($exist_clause, count($filters));
$params = array_merge($params, $filters);
} else {
if($CONFIG_DBTYPE == 'mysql') { //Dirty hack to allow usage of alias in where
$sql .= ' having true ';
}
foreach($filters as $filter) {
if($CONFIG_DBTYPE == 'mysql')
$sql .= ' AND lower( concat(url,title,description,tags )) like ? ';
else
$sql .= ' AND lower(url || title || description || tags ) like ? ';
$params[] = '%' . strtolower($filter) . '%';
}
}
$sql .= " ORDER BY ".$sqlSortColumn." DESC ";
if($limit == -1 || $limit === false) {
$limit = null;
$offset = null;
}
$query = OCP\DB::prepare($sql, $limit, $offset);
$results = $query->execute($params)->fetchAll();
$bookmarks = array();
foreach($results as $result){
$result['tags'] = explode(',', $result['tags']);
$bookmarks[] = $result;
}
return $bookmarks;
}
public static function deleteUrl($id)
{
$user = OCP\USER::getUser();
$query = OCP\DB::prepare("
SELECT `id` FROM `*PREFIX*bookmarks`
WHERE `id` = ?
AND `user_id` = ?
");
$result = $query->execute(array($id, $user));
$id = $result->fetchOne();
if ($id === false) {
return false;
}
$query = OCP\DB::prepare("
DELETE FROM `*PREFIX*bookmarks`
WHERE `id` = ?
");
$result = $query->execute(array($id));
$query = OCP\DB::prepare("
DELETE FROM `*PREFIX*bookmarks_tags`
WHERE `bookmark_id` = ?
");
$result = $query->execute(array($id));
return true;
}
public static function renameTag($old, $new)
{
$user_id = OCP\USER::getUser();
$CONFIG_DBTYPE = OCP\Config::getSystemValue( 'dbtype', 'sqlite' );
if( $CONFIG_DBTYPE == 'sqlite' or $CONFIG_DBTYPE == 'sqlite3' ) {
// Update tags to the new label unless it already exists a tag like this
$query = OCP\DB::prepare("
UPDATE OR REPLACE *PREFIX*bookmarks_tags
SET tag = ?
WHERE tag = ?
AND exists( select b.id from *PREFIX*bookmarks b where b.user_id = ? and bookmark_id = b.id)
");
$params=array(
$new,
$old,
$user_id,
);
$result = $query->execute($params);
} else {
// Remove potentialy duplicated tags
$query = OCP\DB::prepare("
DELETE FROM *PREFIX*bookmarks_tags as tgs where tgs.tag = ?
AND exists( select id from *PREFIX*bookmarks where user_id = ? and tgs.bookmark_id = id)
AND exists( select t.tag from *PREFIX*bookmarks_tags t where t.tag=? and tgs.bookmark_id = tbookmark_id");
$params=array(
$new,
$user_id,
);
$result = $query->execute($params);
// Update tags to the new label unless it already exists a tag like this
$query = OCP\DB::prepare("
UPDATE *PREFIX*bookmarks_tags
SET tag = ?
WHERE tag = ?
AND exists( select b.id from *PREFIX*bookmarks b where b.user_id = ? and bookmark_id = b.id)
");
$params=array(
$new,
$old,
$user_id,
$old,
);
$result = $query->execute($params);
}
return true;
}
public static function deleteTag($old)
{
$user_id = OCP\USER::getUser();
// Update the record
$query = OCP\DB::prepare("
DELETE FROM *PREFIX*bookmarks_tags
WHERE tag = ?
AND exists( select id from *PREFIX*bookmarks where user_id = ? and bookmark_id = id)
");
$params=array(
$old,
$user_id,
);
$result = $query->execute($params);
return $result;
}
/**
* get a string corresponding to the current time depending
* of the OC database system
* @return string
*/
protected static function getNowValue() {
$CONFIG_DBTYPE = OCP\Config::getSystemValue( "dbtype", "sqlite" );
if( $CONFIG_DBTYPE == 'sqlite' or $CONFIG_DBTYPE == 'sqlite3' ) {
$_ut = "strftime('%s','now')";
} elseif($CONFIG_DBTYPE == 'pgsql') {
$_ut = 'date_part(\'epoch\',now())::integer';
} else {
$_ut = "UNIX_TIMESTAMP()";
}
return $_ut;
}
/**
* Edit a bookmark
* @param int $id The id of the bookmark to edit
* @param string $url
* @param string $title Name of the bookmark
* @param array $tags Simple array of tags to qualify the bookmark (different tags are taken from values)
* @param string $description A longer description about the bookmark
* @param boolean $is_public True if the bookmark is publishable to not registered users
* @return null
*/
public static function editBookmark($id, $url, $title, $tags = array(), $description='', $is_public=false) {
$is_public = $is_public ? 1 : 0;
$user_id = OCP\USER::getUser();
// Update the record
$query = OCP\DB::prepare("
UPDATE *PREFIX*bookmarks SET
url = ?, title = ?, public = ?, description = ?,
lastmodified = ".self::getNowValue() ."
WHERE id = ?
AND user_id = ?
");
$params=array(
htmlspecialchars_decode($url),
htmlspecialchars_decode($title),
$is_public,
htmlspecialchars_decode($description),
$id,
$user_id,
);
$result = $query->execute($params);
// Abort the operation if bookmark couldn't be set
// (probably because the user is not allowed to edit this bookmark)
if ($result == 0) exit();
// Remove old tags
$sql = "DELETE from *PREFIX*bookmarks_tags WHERE bookmark_id = ?";
$query = OCP\DB::prepare($sql);
$query->execute(array($id));
// Add New Tags
self::addTags($id, $tags);
}
/**
* Add a bookmark
* @param string $url
* @param string $title Name of the bookmark
* @param array $tags Simple array of tags to qualify the bookmark (different tags are taken from values)
* @param string $description A longer description about the bookmark
* @param boolean $is_public True if the bookmark is publishable to not registered users
* @return int The id of the bookmark created
*/
public static function addBookmark($url, $title, $tags=array(), $description='', $is_public=false) {
$is_public = $is_public ? 1 : 0;
$enc_url = htmlspecialchars_decode($url);
$_ut = self::getNowValue();
// Change lastmodified date if the record if already exists
$sql = "SELECT * from *PREFIX*bookmarks WHERE url = ? and user_id = ?";
$query = OCP\DB::prepare($sql, 1);
$result = $query->execute(array($enc_url, OCP\USER::getUser()));
if ($row = $result->fetchRow()){
$params = array();
$title_str = '';
if(trim($title) != '') { // Do we replace the old title
$title_str = ' , title = ?';
$params[] = $title;
}
$desc_str = '';
if(trim($title) != '') { // Do we replace the old description
$desc_str = ' , description = ?';
$params[] = $description;
}
$sql = "UPDATE *PREFIX*bookmarks SET lastmodified = $_ut $title_str $desc_str WHERE url = ? and user_id = ?";
$params[] = $enc_url;
$params[] = OCP\USER::getUser();
$query = OCP\DB::prepare($sql);
$query->execute($params);
return $row['id'];
}
$query = OCP\DB::prepare("
INSERT INTO *PREFIX*bookmarks
(url, title, user_id, public, added, lastmodified, description)
VALUES (?, ?, ?, ?, $_ut, $_ut, ?)
");
$params=array(
$enc_url,
htmlspecialchars_decode($title),
OCP\USER::getUser(),
$is_public,
$description,
);
$query->execute($params);
$b_id = OCP\DB::insertid('*PREFIX*bookmarks');
if($b_id !== false) {
self::addTags($b_id, $tags);
return $b_id;
}
}
/**
* Add a set of tags for a bookmark
* @param int $bookmark_id The bookmark reference
* @param array $tags Set of tags to add to the bookmark
* @return null
**/
private static function addTags($bookmark_id, $tags) {
$query = OCP\DB::prepare("
INSERT INTO *PREFIX*bookmarks_tags
(bookmark_id, tag)
VALUES (?, ?)");
foreach ($tags as $tag) {
if(empty($tag)) {
//avoid saving blankspaces
continue;
}
$params = array($bookmark_id, trim($tag));
$query->execute($params);
}
}
/**
* Simple function to search for bookmark. call findBookmarks
* @param array $search_words Set of words to look for in bookmars fields
* @return array An Array of bookmarks
**/
public static function searchBookmarks($search_words) {
return self::findBookmarks(0, 'id', $search_words, false);
}
public static function importFile($file){
libxml_use_internal_errors(true);
$dom = new domDocument();
$dom->loadHTMLFile($file);
$links = $dom->getElementsByTagName('a');
OCP\DB::beginTransaction();
foreach($links as $link) {
$title = $link->nodeValue;
$ref = $link->getAttribute("href");
$tag_str = '';
if($link->hasAttribute("tags"))
$tag_str = $link->getAttribute("tags");
$tags = explode(',', $tag_str);
$desc_str = '';
if($link->hasAttribute("description"))
$desc_str = $link->getAttribute("description");
self::addBookmark($ref, $title, $tags,$desc_str );
}
OCP\DB::commit();
return array();
}
public static function getURLMetadata($url) {
//allow only http(s) and (s)ftp
$protocols = '/^[hs]{0,1}[tf]{0,1}tp[s]{0,1}\:\/\//i';
//if not (allowed) protocol is given, assume http
if(preg_match($protocols, $url) == 0) {
$url = 'http://' . $url;
}
$metadata['url'] = $url;
$page = OC_Util::getUrlContent($url);
if($page) {
if(preg_match( "/<title>(.*)<\/title>/sUi", $page, $match ) !== false)
if(isset($match[1])) {
$metadata['title'] = html_entity_decode($match[1], ENT_QUOTES , 'UTF-8');
//Not the best solution but....
$metadata['title'] = str_replace('™', chr(153), $metadata['title']);
$metadata['title'] = str_replace('‐', '‐', $metadata['title']);
$metadata['title'] = str_replace('–', '–', $metadata['title']);
}
}
return $metadata;
}
public static function analyzeTagRequest($line) {
$tags = explode(',', $line);
$filterTag = array();
foreach($tags as $tag){
if(trim($tag) != '')
$filterTag[] = trim($tag);
}
return $filterTag;
}
}