2 class BookmarkService {
5 function &getInstance(&$db) {
7 if (!isset($instance)) {
8 $instance = new BookmarkService($db);
13 function BookmarkService(&$db) {
17 function _getbookmark($fieldname, $value, $all = false) {
19 $userservice = & ServiceFactory :: getServiceInstance('UserService');
20 $sId = $userservice->getCurrentUserId();
21 $range = ' AND uId = '. $sId;
24 $query = 'SELECT * FROM '. $GLOBALS['tableprefix'] .'bookmarks WHERE '. $fieldname .' = "'. $this->db->sql_escape($value) .'"'. $range;
26 if (!($dbresult = & $this->db->sql_query_limit($query, 1, 0))) {
27 message_die(GENERAL_ERROR, 'Could not get bookmark', '', __LINE__, __FILE__, $query, $this->db);
31 if ($row =& $this->db->sql_fetchrow($dbresult)) {
38 function & getBookmark($bid, $include_tags = false) {
39 if (!is_numeric($bid))
42 $sql = 'SELECT * FROM '. $GLOBALS['tableprefix'] .'bookmarks WHERE bId = '. $this->db->sql_escape($bid);
44 if (!($dbresult = & $this->db->sql_query($sql)))
45 message_die(GENERAL_ERROR, 'Could not get vars', '', __LINE__, __FILE__, $sql, $this->db);
47 if ($row = & $this->db->sql_fetchrow($dbresult)) {
49 $tagservice = & ServiceFactory :: getServiceInstance('TagService');
50 $row['tags'] = $tagservice->getTagsForBookmark($bid);
58 function getBookmarkByAddress($address) {
59 $hash = md5($address);
60 return $this->getBookmarkByHash($hash);
63 function getBookmarkByHash($hash) {
64 return $this->_getbookmark('bHash', $hash, true);
67 function editAllowed($bookmark) {
68 if (!is_numeric($bookmark) && (!is_array($bookmark) || !is_numeric($bookmark['bId'])))
71 if (!is_array($bookmark))
72 if (!($bookmark = $this->getBookmark($bookmark)))
75 $userservice = & ServiceFactory :: getServiceInstance('UserService');
76 $userid = $userservice->getCurrentUserId();
77 if ($userservice->isAdmin($userid))
80 return ($bookmark['uId'] == $userid);
83 function bookmarkExists($address = false, $uid = NULL) {
88 // If address doesn't contain ":", add "http://" as the default protocol
89 if (strpos($address, ':') === false) {
90 $address = 'http://'. $address;
93 $crit = array ('bHash' => md5($address));
98 $sql = 'SELECT COUNT(*) FROM '. $GLOBALS['tableprefix'] .'bookmarks WHERE '. $this->db->sql_build_array('SELECT', $crit);
99 if (!($dbresult = & $this->db->sql_query($sql))) {
100 message_die(GENERAL_ERROR, 'Could not get vars', '', __LINE__, __FILE__, $sql, $this->db);
102 return ($this->db->sql_fetchfield(0, 0) > 0);
105 // Adds a bookmark to the database.
106 // Note that date is expected to be a string that's interpretable by strtotime().
107 function addBookmark($address, $title, $description, $status, $categories, $date = NULL, $fromApi = false, $fromImport = false) {
108 $userservice = & ServiceFactory :: getServiceInstance('UserService');
109 $sId = $userservice->getCurrentUserId();
111 // If bookmark address doesn't contain ":", add "http://" to the start as a default protocol
112 if (strpos($address, ':') === false) {
113 $address = 'http://'. $address;
116 // Get the client's IP address and the date; note that the date is in GMT.
117 if (getenv('HTTP_CLIENT_IP'))
118 $ip = getenv('HTTP_CLIENT_IP');
120 if (getenv('REMOTE_ADDR'))
121 $ip = getenv('REMOTE_ADDR');
123 $ip = getenv('HTTP_X_FORWARDED_FOR');
125 // Note that if date is NULL, then it's added with a date and time of now, and if it's present,
126 // it's expected to be a string that's interpretable by strtotime().
130 $time = strtotime($date);
131 $datetime = gmdate('Y-m-d H:i:s', $time);
133 // Set up the SQL insert statement and execute it.
134 $values = array('uId' => intval($sId), 'bIp' => $ip, 'bDatetime' => $datetime, 'bModified' => $datetime, 'bTitle' => $title, 'bAddress' => $address, 'bDescription' => $description, 'bStatus' => intval($status), 'bHash' => md5($address));
135 $sql = 'INSERT INTO '. $GLOBALS['tableprefix'] .'bookmarks '. $this->db->sql_build_array('INSERT', $values);
136 $this->db->sql_transaction('begin');
137 if (!($dbresult = & $this->db->sql_query($sql))) {
138 $this->db->sql_transaction('rollback');
139 message_die(GENERAL_ERROR, 'Could not insert bookmark', '', __LINE__, __FILE__, $sql, $this->db);
142 // Get the resultant row ID for the bookmark.
143 $bId = $this->db->sql_nextid($dbresult);
144 if (!isset($bId) || !is_int($bId)) {
145 $this->db->sql_transaction('rollback');
146 message_die(GENERAL_ERROR, 'Could not insert bookmark', '', __LINE__, __FILE__, $sql, $this->db);
150 $uriparts = explode('.', $address);
151 $extension = end($uriparts);
154 $tagservice = & ServiceFactory :: getServiceInstance('TagService');
155 if (!$tagservice->attachTags($bId, $categories, $fromApi, $extension, false, $fromImport)) {
156 $this->db->sql_transaction('rollback');
157 message_die(GENERAL_ERROR, 'Could not insert bookmark', '', __LINE__, __FILE__, $sql, $this->db);
160 $this->db->sql_transaction('commit');
161 // Everything worked out, so return the new bookmark's bId.
165 function updateBookmark($bId, $address, $title, $description, $status, $categories, $date = NULL, $fromApi = false) {
166 if (!is_numeric($bId))
169 // Get the client's IP address and the date; note that the date is in GMT.
170 if (getenv('HTTP_CLIENT_IP'))
171 $ip = getenv('HTTP_CLIENT_IP');
173 if (getenv('REMOTE_ADDR'))
174 $ip = getenv('REMOTE_ADDR');
176 $ip = getenv('HTTP_X_FORWARDED_FOR');
178 $moddatetime = gmdate('Y-m-d H:i:s', time());
180 // Set up the SQL update statement and execute it.
181 $updates = array('bModified' => $moddatetime, 'bTitle' => $title, 'bAddress' => $address, 'bDescription' => $description, 'bStatus' => $status, 'bHash' => md5($address));
183 if (!is_null($date)) {
184 $updates['bDateTime'] = gmdate('Y-m-d H:i:s', strtotime($date));
187 $sql = 'UPDATE '. $GLOBALS['tableprefix'] .'bookmarks SET '. $this->db->sql_build_array('UPDATE', $updates) .' WHERE bId = '. intval($bId);
188 $this->db->sql_transaction('begin');
190 if (!($dbresult = & $this->db->sql_query($sql))) {
191 $this->db->sql_transaction('rollback');
192 message_die(GENERAL_ERROR, 'Could not update bookmark', '', __LINE__, __FILE__, $sql, $this->db);
196 $uriparts = explode('.', $address);
197 $extension = end($uriparts);
200 $tagservice = & ServiceFactory :: getServiceInstance('TagService');
201 if (!$tagservice->attachTags($bId, $categories, $fromApi, $extension)) {
202 $this->db->sql_transaction('rollback');
203 message_die(GENERAL_ERROR, 'Could not update bookmark', '', __LINE__, __FILE__, $sql, $this->db);
207 $this->db->sql_transaction('commit');
208 // Everything worked out, so return true.
212 function & getBookmarks($start = 0, $perpage = NULL, $user = NULL, $tags = NULL, $terms = NULL, $sortOrder = NULL, $watched = NULL, $startdate = NULL, $enddate = NULL, $hash = NULL) {
213 // Only get the bookmarks that are visible to the current user. Our rules:
214 // - if the $user is NULL, that means get bookmarks from ALL users, so we need to make
215 // sure to check the logged-in user's watchlist and get the contacts-only bookmarks from
216 // those users. If the user isn't logged-in, just get the public bookmarks.
217 // - if the $user is set and isn't the logged-in user, then get that user's bookmarks, and
218 // if that user is on the logged-in user's watchlist, get the public AND contacts-only
219 // bookmarks; otherwise, just get the public bookmarks.
220 // - if the $user is set and IS the logged-in user, then get all bookmarks.
221 $userservice =& ServiceFactory::getServiceInstance('UserService');
222 $tagservice =& ServiceFactory::getServiceInstance('TagService');
223 $sId = $userservice->getCurrentUserId();
225 if ($userservice->isLoggedOn()) {
226 // All public bookmarks, user's own bookmarks and any shared with user
227 $privacy = ' AND ((B.bStatus = 0) OR (B.uId = '. $sId .')';
228 $watchnames = $userservice->getWatchNames($sId, true);
229 foreach($watchnames as $watchuser) {
230 $privacy .= ' OR (U.username = "'. $watchuser .'" AND B.bStatus = 1)';
234 // Just public bookmarks
235 $privacy = ' AND B.bStatus = 0';
238 // Set up the tags, if need be.
239 if (!is_array($tags) && !is_null($tags)) {
240 $tags = explode('+', trim($tags));
243 $tagcount = count($tags);
244 for ($i = 0; $i < $tagcount; $i ++) {
245 $tags[$i] = trim($tags[$i]);
248 // Set up the SQL query.
249 $query_1 = 'SELECT DISTINCT ';
250 if (SQL_LAYER == 'mysql4') {
251 $query_1 .= 'SQL_CALC_FOUND_ROWS ';
253 $query_1 .= 'B.*, U.'. $userservice->getFieldName('username');
255 $query_2 = ' FROM '. $userservice->getTableName() .' AS U, '. $GLOBALS['tableprefix'] .'bookmarks AS B';
257 $query_3 = ' WHERE B.uId = U.'. $userservice->getFieldName('primary') . $privacy;
258 if (is_null($watched)) {
259 if (!is_null($user)) {
260 $query_3 .= ' AND B.uId = '. $user;
263 $arrWatch = $userservice->getWatchlist($user);
264 if (count($arrWatch) > 0) {
265 foreach($arrWatch as $row) {
266 $query_3_1 .= 'B.uId = '. intval($row) .' OR ';
268 $query_3_1 = substr($query_3_1, 0, -3);
270 $query_3_1 = 'B.uId = -1';
272 $query_3 .= ' AND ('. $query_3_1 .') AND B.bStatus IN (0, 1)';
277 $query_5 = ' ORDER BY B.bDatetime ASC ';
279 case 'mod_date_desc':
280 $query_5 = ' ORDER BY B.bModified DESC ';
283 $query_5 = ' ORDER BY B.bModified ASC ';
286 $query_5 = ' ORDER BY B.bTitle DESC ';
289 $query_5 = ' ORDER BY B.bTitle ASC ';
292 $query_5 = ' ORDER BY B.bAddress DESC ';
295 $query_5 = ' ORDER BY B.bAddress ASC ';
298 $query_5 = ' ORDER BY B.bDatetime DESC ';
301 // Handle the parts of the query that depend on any tags that are present.
303 for ($i = 0; $i < $tagcount; $i ++) {
304 $query_2 .= ', '. $GLOBALS['tableprefix'] .'tags AS T'. $i;
305 $query_4 .= ' AND T'. $i .'.tag = "'. $this->db->sql_escape($tags[$i]) .'" AND T'. $i .'.bId = B.bId';
310 // Multiple search terms okay
311 $aTerms = explode(' ', $terms);
312 $aTerms = array_map('trim', $aTerms);
314 // Search terms in tags as well when none given
316 $query_2 .= ' LEFT JOIN '. $GLOBALS['tableprefix'] .'tags AS T ON B.bId = T.bId';
323 for ($i = 0; $i < count($aTerms); $i++) {
324 $query_4 .= ' AND (B.bTitle LIKE "%'. $this->db->sql_escape($aTerms[$i]) .'%"';
325 $query_4 .= ' OR B.bDescription LIKE "%'. $this->db->sql_escape($aTerms[$i]) .'%"';
327 $query_4 .= ' OR T.tag = "'. $this->db->sql_escape($aTerms[$i]) .'"';
333 // Start and end dates
335 $query_4 .= ' AND B.bDatetime > "'. $startdate .'"';
338 $query_4 .= ' AND B.bDatetime < "'. $enddate .'"';
343 $query_4 .= ' AND B.bHash = "'. $hash .'"';
346 $query = $query_1 . $query_2 . $query_3 . $query_4 . $query_5;
347 if (!($dbresult = & $this->db->sql_query_limit($query, intval($perpage), intval($start)))) {
348 message_die(GENERAL_ERROR, 'Could not get bookmarks', '', __LINE__, __FILE__, $query, $this->db);
352 if (SQL_LAYER == 'mysql4') {
353 $totalquery = 'SELECT FOUND_ROWS() AS total';
355 $totalquery = 'SELECT COUNT(*) AS total'. $query_2 . $query_3 . $query_4;
358 if (!($totalresult = & $this->db->sql_query($totalquery)) || (!($row = & $this->db->sql_fetchrow($totalresult)))) {
359 message_die(GENERAL_ERROR, 'Could not get total bookmarks', '', __LINE__, __FILE__, $totalquery, $this->db);
363 $total = $row['total'];
365 $bookmarks = array();
366 while ($row = & $this->db->sql_fetchrow($dbresult)) {
367 $row['tags'] = $tagservice->getTagsForBookmark(intval($row['bId']));
370 return array ('bookmarks' => $bookmarks, 'total' => $total);
373 function deleteBookmark($bookmarkid) {
374 $query = 'DELETE FROM '. $GLOBALS['tableprefix'] .'bookmarks WHERE bId = '. intval($bookmarkid);
375 $this->db->sql_transaction('begin');
376 if (!($dbresult = & $this->db->sql_query($query))) {
377 $this->db->sql_transaction('rollback');
378 message_die(GENERAL_ERROR, 'Could not delete bookmarks', '', __LINE__, __FILE__, $query, $this->db);
382 $query = 'DELETE FROM '. $GLOBALS['tableprefix'] .'tags WHERE bId = '. intval($bookmarkid);
383 $this->db->sql_transaction('begin');
384 if (!($dbresult = & $this->db->sql_query($query))) {
385 $this->db->sql_transaction('rollback');
386 message_die(GENERAL_ERROR, 'Could not delete bookmarks', '', __LINE__, __FILE__, $query, $this->db);
390 $this->db->sql_transaction('commit');
394 function countOthers($address) {
399 $userservice = & ServiceFactory :: getServiceInstance('UserService');
400 $sId = $userservice->getCurrentUserId();
402 if ($userservice->isLoggedOn()) {
403 // All public bookmarks, user's own bookmarks and any shared with user
404 $privacy = ' AND ((B.bStatus = 0) OR (B.uId = '. $sId .')';
405 $watchnames = $userservice->getWatchNames($sId, true);
406 foreach($watchnames as $watchuser) {
407 $privacy .= ' OR (U.username = "'. $watchuser .'" AND B.bStatus = 1)';
411 // Just public bookmarks
412 $privacy = ' AND B.bStatus = 0';
415 $sql = 'SELECT COUNT(*) FROM '. $userservice->getTableName() .' AS U, '. $GLOBALS['tableprefix'] .'bookmarks AS B WHERE U.'. $userservice->getFieldName('primary') .' = B.uId AND B.bHash = "'. md5($address) .'"'. $privacy;
416 if (!($dbresult = & $this->db->sql_query($sql))) {
417 message_die(GENERAL_ERROR, 'Could not get vars', '', __LINE__, __FILE__, $sql, $this->db);
419 return $this->db->sql_fetchfield(0, 0) - 1;