2012-01-29 19:32:33 +01:00
|
|
|
|
<?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{
|
|
|
|
|
|
2012-06-26 17:43:03 +02:00
|
|
|
|
/**
|
|
|
|
|
* @brief Finds all tags for bookmarks
|
2012-10-12 23:06:19 +02:00
|
|
|
|
* @param filterTags array of tag to look for if empty then every tag
|
2014-08-19 14:16:08 +02:00
|
|
|
|
* @param offset integer offset
|
|
|
|
|
* @param limit integer of item to return
|
2012-06-26 17:43:03 +02:00
|
|
|
|
*/
|
2012-06-27 23:44:20 +02:00
|
|
|
|
public static function findTags($filterTags = array(), $offset = 0, $limit = 10){
|
2012-10-10 21:05:34 +02:00
|
|
|
|
$params = array_merge($filterTags, $filterTags);
|
2012-08-13 21:25:00 +02:00
|
|
|
|
array_unshift($params, OCP\USER::getUser());
|
2012-06-27 23:44:20 +02:00
|
|
|
|
$not_in = '';
|
|
|
|
|
if(!empty($filterTags) ) {
|
2013-04-23 21:44:04 +02:00
|
|
|
|
$exist_clause = " AND exists (select 1 from `*PREFIX*bookmarks_tags`
|
|
|
|
|
`t2` where `t2`.`bookmark_id` = `t`.`bookmark_id` and `tag` = ?) ";
|
2012-10-10 21:05:34 +02:00
|
|
|
|
|
2013-04-23 21:44:04 +02:00
|
|
|
|
$not_in = ' AND `tag` not in ('. implode(',', array_fill(0, count($filterTags), '?') ) .')'.
|
2012-10-10 21:05:34 +02:00
|
|
|
|
str_repeat($exist_clause, count($filterTags));
|
2012-06-27 23:44:20 +02:00
|
|
|
|
}
|
2012-08-13 21:25:00 +02:00
|
|
|
|
$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.
|
2013-04-23 21:44:04 +02:00
|
|
|
|
' GROUP BY `tag` ORDER BY `nbr` DESC ';
|
2012-10-11 16:45:00 +02:00
|
|
|
|
|
2012-10-11 22:27:25 +02:00
|
|
|
|
$query = OCP\DB::prepare($sql, $limit, $offset);
|
2012-06-27 23:44:20 +02:00
|
|
|
|
$tags = $query->execute($params)->fetchAll();
|
2012-06-26 17:43:03 +02:00
|
|
|
|
return $tags;
|
|
|
|
|
}
|
2013-01-16 23:47:26 +01:00
|
|
|
|
|
|
|
|
|
public static function findOneBookmark($id) {
|
2013-01-30 23:17:08 +01:00
|
|
|
|
$CONFIG_DBTYPE = OCP\Config::getSystemValue( 'dbtype', 'sqlite' );
|
2013-01-16 23:47:26 +01:00
|
|
|
|
if($CONFIG_DBTYPE == 'pgsql') {
|
2013-04-23 21:44:04 +02:00
|
|
|
|
$group_fct = 'array_agg(`tag`)';
|
2013-01-16 23:47:26 +01:00
|
|
|
|
}
|
|
|
|
|
else {
|
2013-04-23 21:44:04 +02:00
|
|
|
|
$group_fct = 'GROUP_CONCAT(`tag`)';
|
2013-01-16 23:47:26 +01:00
|
|
|
|
}
|
2013-04-23 21:44:04 +02:00
|
|
|
|
$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` = ?";
|
2013-01-16 23:47:26 +01:00
|
|
|
|
$query = OCP\DB::prepare($sql);
|
|
|
|
|
$result = $query->execute(array(OCP\USER::getUser(), $id))->fetchRow();
|
|
|
|
|
$result['tags'] = explode(',', $result['tags']);
|
|
|
|
|
return $result;
|
|
|
|
|
}
|
|
|
|
|
|
2012-01-29 19:32:33 +01:00
|
|
|
|
/**
|
|
|
|
|
* @brief Finds all bookmarks, matching the filter
|
2014-08-19 14:16:08 +02:00
|
|
|
|
* @param offset integer offset
|
|
|
|
|
* @param sqlSortColumn string result with this column
|
2012-07-02 12:41:40 +02:00
|
|
|
|
* @param filters can be: empty -> no filter, a string -> filter this, a string array -> filter for all strings
|
2014-08-19 14:16:08 +02:00
|
|
|
|
* @param filterTagOnly boolean true, filter affects only tags, else filter affects url, title and tags
|
|
|
|
|
* @param limit integer of item to return (default 10) if -1 or false then all item are returned
|
2012-01-29 19:32:33 +01:00
|
|
|
|
* @return void
|
|
|
|
|
*/
|
2012-09-18 22:27:24 +02:00
|
|
|
|
public static function findBookmarks($offset, $sqlSortColumn, $filters, $filterTagOnly, $limit = 10) {
|
2012-07-02 09:43:37 +02:00
|
|
|
|
$CONFIG_DBTYPE = OCP\Config::getSystemValue( 'dbtype', 'sqlite' );
|
2012-07-02 12:41:40 +02:00
|
|
|
|
if(is_string($filters)) $filters = array($filters);
|
2013-04-23 21:59:52 +02:00
|
|
|
|
if(! in_array($sqlSortColumn, array('id', 'url', 'title', 'user_id',
|
|
|
|
|
'description', 'public', 'added', 'lastmodified','clickcount',))) {
|
|
|
|
|
$sqlSortColumn = 'bookmarks_sorting_recent';
|
|
|
|
|
}
|
2012-05-01 18:50:31 +02:00
|
|
|
|
$params=array(OCP\USER::getUser());
|
2012-07-02 09:43:37 +02:00
|
|
|
|
|
|
|
|
|
if($CONFIG_DBTYPE == 'pgsql') {
|
2014-07-17 20:43:16 +02:00
|
|
|
|
$sql = "SELECT * FROM (SELECT *, (select array_to_string(array_agg(`tag`),',') from `*PREFIX*bookmarks_tags` where `bookmark_id` = `b2`.`id`) as `tags`
|
|
|
|
|
FROM `*PREFIX*bookmarks` `b2`
|
|
|
|
|
WHERE `user_id` = ? ) as `b` WHERE true ";
|
2012-07-02 09:43:37 +02:00
|
|
|
|
}
|
|
|
|
|
else {
|
2013-04-23 21:44:04 +02:00
|
|
|
|
$sql = "SELECT *, (SELECT GROUP_CONCAT(`tag`) from `*PREFIX*bookmarks_tags` WHERE `bookmark_id` = `b`.`id`) as `tags`
|
|
|
|
|
FROM `*PREFIX*bookmarks` `b`
|
|
|
|
|
WHERE `user_id` = ? ";
|
2013-03-25 22:49:53 +01:00
|
|
|
|
}
|
2012-06-27 14:14:45 +02:00
|
|
|
|
|
2012-06-27 23:44:20 +02:00
|
|
|
|
if($filterTagOnly) {
|
2013-04-23 21:44:04 +02:00
|
|
|
|
$exist_clause = " AND exists (SELECT `id` FROM `*PREFIX*bookmarks_tags`
|
|
|
|
|
`t2` WHERE `t2`.`bookmark_id` = `b`.`id` AND `tag` = ?) ";
|
2012-10-10 21:05:34 +02:00
|
|
|
|
$sql .= str_repeat($exist_clause, count($filters));
|
2012-07-02 12:41:40 +02:00
|
|
|
|
$params = array_merge($params, $filters);
|
|
|
|
|
} else {
|
2012-10-11 23:43:01 +02:00
|
|
|
|
if($CONFIG_DBTYPE == 'mysql') { //Dirty hack to allow usage of alias in where
|
2013-04-23 21:44:04 +02:00
|
|
|
|
$sql .= ' HAVING true ';
|
2012-10-11 23:43:01 +02:00
|
|
|
|
}
|
2012-07-02 12:41:40 +02:00
|
|
|
|
foreach($filters as $filter) {
|
2013-10-12 19:31:20 +02:00
|
|
|
|
if($CONFIG_DBTYPE == 'mysql')
|
|
|
|
|
$sql .= ' AND lower( concat(url,title,description,tags )) like ? ';
|
|
|
|
|
else
|
|
|
|
|
$sql .= ' AND lower(url || title || description || tags ) like ? ';
|
2012-07-02 12:41:40 +02:00
|
|
|
|
$params[] = '%' . strtolower($filter) . '%';
|
|
|
|
|
}
|
2012-01-29 19:32:33 +01:00
|
|
|
|
}
|
2013-10-12 19:31:20 +02:00
|
|
|
|
|
2012-09-18 22:27:24 +02:00
|
|
|
|
$sql .= " ORDER BY ".$sqlSortColumn." DESC ";
|
2012-10-11 22:27:25 +02:00
|
|
|
|
if($limit == -1 || $limit === false) {
|
|
|
|
|
$limit = null;
|
|
|
|
|
$offset = null;
|
2012-09-18 22:27:24 +02:00
|
|
|
|
}
|
2012-01-29 19:32:33 +01:00
|
|
|
|
|
2012-10-11 22:27:25 +02:00
|
|
|
|
$query = OCP\DB::prepare($sql, $limit, $offset);
|
2012-06-27 14:14:45 +02:00
|
|
|
|
$results = $query->execute($params)->fetchAll();
|
|
|
|
|
$bookmarks = array();
|
|
|
|
|
foreach($results as $result){
|
|
|
|
|
$result['tags'] = explode(',', $result['tags']);
|
|
|
|
|
$bookmarks[] = $result;
|
|
|
|
|
}
|
2012-01-29 19:32:33 +01:00
|
|
|
|
return $bookmarks;
|
|
|
|
|
}
|
2012-05-03 17:29:08 +02:00
|
|
|
|
|
|
|
|
|
public static function deleteUrl($id)
|
|
|
|
|
{
|
|
|
|
|
$user = OCP\USER::getUser();
|
|
|
|
|
|
|
|
|
|
$query = OCP\DB::prepare("
|
2012-07-30 20:46:14 +02:00
|
|
|
|
SELECT `id` FROM `*PREFIX*bookmarks`
|
|
|
|
|
WHERE `id` = ?
|
|
|
|
|
AND `user_id` = ?
|
2012-05-03 17:29:08 +02:00
|
|
|
|
");
|
|
|
|
|
|
|
|
|
|
$result = $query->execute(array($id, $user));
|
|
|
|
|
$id = $result->fetchOne();
|
|
|
|
|
if ($id === false) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$query = OCP\DB::prepare("
|
2012-07-30 20:46:14 +02:00
|
|
|
|
DELETE FROM `*PREFIX*bookmarks`
|
2013-04-23 21:59:52 +02:00
|
|
|
|
WHERE `id` = ?
|
2012-05-03 17:29:08 +02:00
|
|
|
|
");
|
|
|
|
|
|
2013-04-23 21:59:52 +02:00
|
|
|
|
$result = $query->execute(array($id));
|
2012-05-03 17:29:08 +02:00
|
|
|
|
|
|
|
|
|
$query = OCP\DB::prepare("
|
2012-07-30 20:46:14 +02:00
|
|
|
|
DELETE FROM `*PREFIX*bookmarks_tags`
|
2013-04-23 21:59:52 +02:00
|
|
|
|
WHERE `bookmark_id` = ?
|
2012-05-03 17:29:08 +02:00
|
|
|
|
");
|
|
|
|
|
|
2013-04-23 21:59:52 +02:00
|
|
|
|
$result = $query->execute(array($id));
|
2012-05-03 17:29:08 +02:00
|
|
|
|
return true;
|
|
|
|
|
}
|
2012-06-29 15:50:19 +02:00
|
|
|
|
|
|
|
|
|
public static function renameTag($old, $new)
|
|
|
|
|
{
|
|
|
|
|
$user_id = OCP\USER::getUser();
|
2012-09-03 23:41:52 +02:00
|
|
|
|
$CONFIG_DBTYPE = OCP\Config::getSystemValue( 'dbtype', 'sqlite' );
|
2012-06-29 15:50:19 +02:00
|
|
|
|
|
2012-09-03 23:41:52 +02:00
|
|
|
|
|
2012-10-10 21:05:34 +02:00
|
|
|
|
if( $CONFIG_DBTYPE == 'sqlite' or $CONFIG_DBTYPE == 'sqlite3' ) {
|
2012-09-03 23:41:52 +02:00
|
|
|
|
// Update tags to the new label unless it already exists a tag like this
|
2012-10-10 21:05:34 +02:00
|
|
|
|
$query = OCP\DB::prepare("
|
2013-04-23 21:44:04 +02:00
|
|
|
|
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`)
|
2012-10-10 21:05:34 +02:00
|
|
|
|
");
|
2012-06-29 15:50:19 +02:00
|
|
|
|
|
2012-10-10 21:05:34 +02:00
|
|
|
|
$params=array(
|
|
|
|
|
$new,
|
|
|
|
|
$old,
|
|
|
|
|
$user_id,
|
|
|
|
|
);
|
2012-06-29 15:50:19 +02:00
|
|
|
|
|
2012-10-10 21:05:34 +02:00
|
|
|
|
$result = $query->execute($params);
|
2012-09-03 23:41:52 +02:00
|
|
|
|
} else {
|
|
|
|
|
|
|
|
|
|
// Remove potentialy duplicated tags
|
|
|
|
|
$query = OCP\DB::prepare("
|
2013-04-23 21:44:04 +02:00
|
|
|
|
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` = `t`.`bookmark_id`");
|
2012-09-03 23:41:52 +02:00
|
|
|
|
|
|
|
|
|
$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("
|
2013-04-23 21:44:04 +02:00
|
|
|
|
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`)
|
2012-09-03 23:41:52 +02:00
|
|
|
|
");
|
|
|
|
|
|
|
|
|
|
$params=array(
|
|
|
|
|
$new,
|
|
|
|
|
$old,
|
|
|
|
|
$user_id,
|
|
|
|
|
$old,
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$result = $query->execute($params);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2012-06-29 15:50:19 +02:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static function deleteTag($old)
|
|
|
|
|
{
|
|
|
|
|
$user_id = OCP\USER::getUser();
|
|
|
|
|
|
|
|
|
|
// Update the record
|
|
|
|
|
$query = OCP\DB::prepare("
|
2013-04-23 21:44:04 +02:00
|
|
|
|
DELETE FROM `*PREFIX*bookmarks_tags`
|
|
|
|
|
WHERE `tag` = ?
|
|
|
|
|
AND exists( SELECT `id` FROM `*PREFIX*bookmarks` WHERE `user_id` = ? AND `bookmark_id` = `id`)
|
2012-06-29 15:50:19 +02:00
|
|
|
|
");
|
|
|
|
|
|
|
|
|
|
$params=array(
|
|
|
|
|
$old,
|
|
|
|
|
$user_id,
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$result = $query->execute($params);
|
2012-10-10 21:05:34 +02:00
|
|
|
|
return $result;
|
2012-06-29 15:50:19 +02:00
|
|
|
|
}
|
2012-07-01 17:45:37 +02:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 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" );
|
2012-10-10 21:05:34 +02:00
|
|
|
|
if( $CONFIG_DBTYPE == 'sqlite' or $CONFIG_DBTYPE == 'sqlite3' ) {
|
2012-07-01 17:45:37 +02:00
|
|
|
|
$_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("
|
2013-04-23 21:44:04 +02:00
|
|
|
|
UPDATE `*PREFIX*bookmarks` SET
|
|
|
|
|
`url` = ?, `title` = ?, `public` = ?, `description` = ?,
|
|
|
|
|
`lastmodified` = ".self::getNowValue() ."
|
|
|
|
|
WHERE `id` = ?
|
|
|
|
|
AND `user_id` = ?
|
2012-07-01 17:45:37 +02:00
|
|
|
|
");
|
|
|
|
|
|
|
|
|
|
$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)
|
2013-07-28 15:19:46 +02:00
|
|
|
|
if ($result == 0) exit();
|
2012-07-01 17:45:37 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Remove old tags
|
2013-04-23 21:44:04 +02:00
|
|
|
|
$sql = "DELETE FROM `*PREFIX*bookmarks_tags` WHERE `bookmark_id` = ?";
|
2012-07-01 17:45:37 +02:00
|
|
|
|
$query = OCP\DB::prepare($sql);
|
|
|
|
|
$query->execute(array($id));
|
|
|
|
|
|
|
|
|
|
// Add New Tags
|
|
|
|
|
self::addTags($id, $tags);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2012-10-10 21:05:34 +02:00
|
|
|
|
* Add a bookmark
|
2012-07-01 17:45:37 +02:00
|
|
|
|
* @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;
|
2013-01-16 23:47:26 +01:00
|
|
|
|
$enc_url = htmlspecialchars_decode($url);
|
2012-07-01 17:45:37 +02:00
|
|
|
|
$_ut = self::getNowValue();
|
2013-01-16 23:47:26 +01:00
|
|
|
|
// Change lastmodified date if the record if already exists
|
2013-04-23 21:44:04 +02:00
|
|
|
|
$sql = "SELECT * from `*PREFIX*bookmarks` WHERE `url` = ? AND `user_id` = ?";
|
2013-01-16 23:47:26 +01:00
|
|
|
|
$query = OCP\DB::prepare($sql, 1);
|
|
|
|
|
$result = $query->execute(array($enc_url, OCP\USER::getUser()));
|
2013-01-30 23:28:51 +01:00
|
|
|
|
if ($row = $result->fetchRow()){
|
2013-01-27 15:49:46 +01:00
|
|
|
|
$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;
|
|
|
|
|
}
|
2013-04-23 21:44:04 +02:00
|
|
|
|
$sql = "UPDATE `*PREFIX*bookmarks` SET `lastmodified` = $_ut $title_str $desc_str WHERE `url` = ? and `user_id` = ?";
|
2013-01-27 15:49:46 +01:00
|
|
|
|
$params[] = $enc_url;
|
|
|
|
|
$params[] = OCP\USER::getUser();
|
2013-01-16 23:47:26 +01:00
|
|
|
|
$query = OCP\DB::prepare($sql);
|
2013-01-27 15:49:46 +01:00
|
|
|
|
$query->execute($params);
|
2013-01-16 23:47:26 +01:00
|
|
|
|
return $row['id'];
|
|
|
|
|
}
|
2012-07-01 17:45:37 +02:00
|
|
|
|
$query = OCP\DB::prepare("
|
2013-04-23 21:44:04 +02:00
|
|
|
|
INSERT INTO `*PREFIX*bookmarks`
|
|
|
|
|
(`url`, `title`, `user_id`, `public`, `added`, `lastmodified`, `description`)
|
2012-07-01 17:45:37 +02:00
|
|
|
|
VALUES (?, ?, ?, ?, $_ut, $_ut, ?)
|
|
|
|
|
");
|
|
|
|
|
|
|
|
|
|
$params=array(
|
2013-01-16 23:47:26 +01:00
|
|
|
|
$enc_url,
|
2012-07-01 17:45:37 +02:00
|
|
|
|
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
|
2014-08-20 15:26:02 +02:00
|
|
|
|
*
|
|
|
|
|
* @param int $bookmarkId The bookmark reference
|
2012-07-01 17:45:37 +02:00
|
|
|
|
* @param array $tags Set of tags to add to the bookmark
|
|
|
|
|
* @return null
|
|
|
|
|
**/
|
2014-08-20 15:26:02 +02:00
|
|
|
|
private static function addTags($bookmarkId, $tags) {
|
|
|
|
|
$query = OCP\DB::prepare("INSERT INTO `*PREFIX*bookmarks_tags` (`bookmark_id`, `tag`)
|
|
|
|
|
select ?, ? where not exists(select * from oc_bookmarks_tags where bookmark_id = ? and tag = ?)");
|
2012-07-01 17:45:37 +02:00
|
|
|
|
|
|
|
|
|
foreach ($tags as $tag) {
|
2014-08-20 15:26:02 +02:00
|
|
|
|
$tag = trim($tag);
|
2012-07-01 17:45:37 +02:00
|
|
|
|
if(empty($tag)) {
|
2014-08-20 15:26:02 +02:00
|
|
|
|
//avoid saving white spaces
|
2012-07-01 17:45:37 +02:00
|
|
|
|
continue;
|
|
|
|
|
}
|
2014-08-20 15:26:02 +02:00
|
|
|
|
$params = array($bookmarkId, $tag, $bookmarkId, $tag);
|
2012-07-01 17:45:37 +02:00
|
|
|
|
$query->execute($params);
|
|
|
|
|
}
|
|
|
|
|
}
|
2012-07-02 12:41:40 +02:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 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);
|
|
|
|
|
}
|
2012-07-03 15:55:51 +02:00
|
|
|
|
|
|
|
|
|
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");
|
2012-10-10 21:05:34 +02:00
|
|
|
|
$tags = explode(',', $tag_str);
|
2012-07-03 15:55:51 +02:00
|
|
|
|
|
2013-03-26 00:01:53 +01:00
|
|
|
|
$desc_str = '';
|
|
|
|
|
if($link->hasAttribute("description"))
|
|
|
|
|
$desc_str = $link->getAttribute("description");
|
|
|
|
|
|
|
|
|
|
self::addBookmark($ref, $title, $tags,$desc_str );
|
2012-07-03 15:55:51 +02:00
|
|
|
|
}
|
|
|
|
|
OCP\DB::commit();
|
|
|
|
|
return array();
|
|
|
|
|
}
|
2012-12-18 22:31:11 +01:00
|
|
|
|
|
|
|
|
|
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)
|
2013-03-30 17:17:06 +01:00
|
|
|
|
if(isset($match[1])) {
|
2013-06-28 23:54:54 +02:00
|
|
|
|
$metadata['title'] = html_entity_decode($match[1], ENT_QUOTES , 'UTF-8');
|
2013-03-30 17:17:06 +01:00
|
|
|
|
//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']);
|
|
|
|
|
}
|
2012-12-18 22:31:11 +01:00
|
|
|
|
}
|
|
|
|
|
return $metadata;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static function analyzeTagRequest($line) {
|
|
|
|
|
$tags = explode(',', $line);
|
|
|
|
|
$filterTag = array();
|
|
|
|
|
foreach($tags as $tag){
|
|
|
|
|
if(trim($tag) != '')
|
|
|
|
|
$filterTag[] = trim($tag);
|
|
|
|
|
}
|
|
|
|
|
return $filterTag;
|
|
|
|
|
}
|
2012-01-29 19:32:33 +01:00
|
|
|
|
}
|
2012-12-18 22:31:11 +01:00
|
|
|
|
|