aboutsummaryrefslogtreecommitdiff
path: root/services/bookmarkservice.php
blob: 635d8edc3f961ce8352df099f2a1625755bd0930 (plain)
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
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
<?php
class BookmarkService {
	var $db;
	var $tablename;

	function & getInstance(& $db) {
		static $instance;
		if (!isset ($instance))
		$instance = & new BookmarkService($db);
		return $instance;
	}

	function BookmarkService(& $db) {
		$this->db = & $db;
		$this->tablename = $GLOBALS['tableprefix'] .'bookmarks';
	}

	function _getbookmark($fieldname, $value, $all = false) {
		if (!$all) {
			$userservice = & ServiceFactory :: getServiceInstance('UserService');
			$sId = $userservice->getCurrentUserId();
			$range = ' AND uId = '. $sId;
		} else {
			$range = '';
		}

		$query = 'SELECT * FROM '. $this->getTableName() .' WHERE '. $fieldname .' = "'. $this->db->sql_escape($value) .'"'. $range;

		if (!($dbresult = & $this->db->sql_query_limit($query, 1, 0))) {
			message_die(GENERAL_ERROR, 'Could not get bookmark', '', __LINE__, __FILE__, $query, $this->db);
			return false;
		}

		if ($row =& $this->db->sql_fetchrow($dbresult)) {
			$output = $row;
		} else {
			$output =  false;
		}
		$this->db->sql_freeresult($dbresult);
		return $output;
	}

	function & getBookmark($bid, $include_tags = false) {
		if (!is_numeric($bid))
		return;

		$sql = 'SELECT * FROM '. $this->getTableName() .' WHERE bId = '. $this->db->sql_escape($bid);

		if (!($dbresult = & $this->db->sql_query($sql)))
		message_die(GENERAL_ERROR, 'Could not get vars', '', __LINE__, __FILE__, $sql, $this->db);

		if ($row = & $this->db->sql_fetchrow($dbresult)) {
			if ($include_tags) {
				$b2tservice = & ServiceFactory :: getServiceInstance('Bookmark2TagService');
				$row['tags'] = $b2tservice->getTagsForBookmark($bid);
			}
			$output = $row;			
		} else {
			$output = false;
		}
		$this->db->sql_freeresult($dbresult);
		return $output;
	}

	function getBookmarkByAddress($address) {
		$hash = md5($address);
		return $this->getBookmarkByHash($hash);
	}

	function getBookmarkByHash($hash) {
		return $this->_getbookmark('bHash', $hash, true);
	}

	/* Counts bookmarks for a user. $range = {'public', 'shared', 'private', 'all'}*/
	function countBookmarks($uId, $range = 'public') {
		$sql = 'SELECT COUNT(*) FROM '. $GLOBALS['tableprefix'] .'bookmarks';
		$sql.= ' WHERE uId = '.$uId;
		switch ($range) {
			case 'all':
			//no constraints
			break;
			case 'private':
			$sql.= ' AND bStatus = 2';
			break;
			case 'shared':
			$sql.= ' AND bStatus = 1';
			break;			
			case 'public':
			default:
			$sql.= ' AND bStatus = 0';
			break;
		}			
		
		if (!($dbresult = & $this->db->sql_query($sql))) {
			message_die(GENERAL_ERROR, 'Could not get vars', '', __LINE__, __FILE__, $sql, $this->db);
		}
		return $this->db->sql_fetchfield(0, 0);
	}

	function editAllowed($bookmark) {
		if (!is_numeric($bookmark) && (!is_array($bookmark) || !is_numeric($bookmark['bId'])))
		return false;

		if (!is_array($bookmark))
		if (!($bookmark = $this->getBookmark($bookmark)))
		return false;

		$userservice = & ServiceFactory :: getServiceInstance('UserService');
		$userid = $userservice->getCurrentUserId();
		if ($userservice->isAdmin($userid))
		return true;
		else
		return ($bookmark['uId'] == $userid);
	}

	function bookmarkExists($address = false, $uid = NULL) {
		if (!$address) {
			return;
		}

		$address = $this->normalize($address);

		$crit = array ('bHash' => md5($address));
		if (isset ($uid)) {
			$crit['uId'] = $uid;
		}

		$sql = 'SELECT COUNT(*) FROM '. $GLOBALS['tableprefix'] .'bookmarks WHERE '. $this->db->sql_build_array('SELECT', $crit);
		if (!($dbresult = & $this->db->sql_query($sql))) {
			message_die(GENERAL_ERROR, 'Could not get vars', '', __LINE__, __FILE__, $sql, $this->db);
		}
		if($this->db->sql_fetchfield(0, 0) > 0) {
			$output = true; 
		} else {
			$output = false;
		}
		$this->db->sql_freeresult($dbresult);
		return $output;
	}

	// Adds a bookmark to the database.
	// Note that date is expected to be a string that's interpretable by strtotime().
	function addBookmark($address, $title, $description, $privateNote, $status, $categories, $date = NULL, $fromApi = false, $fromImport = false, $sId = -1) {
		if($sId == -1) {
			$userservice = & ServiceFactory :: getServiceInstance('UserService');
			$sId = $userservice->getCurrentUserId();
		}

		$address = $this->normalize($address);

		// Get the client's IP address and the date; note that the date is in GMT.
		if (getenv('HTTP_CLIENT_IP'))
		$ip = getenv('HTTP_CLIENT_IP');
		else
		if (getenv('REMOTE_ADDR'))
		$ip = getenv('REMOTE_ADDR');
		else
		$ip = getenv('HTTP_X_FORWARDED_FOR');

		// Note that if date is NULL, then it's added with a date and time of now, and if it's present,
		// it's expected to be a string that's interpretable by strtotime().
		if (is_null($date) || $date == '')
		$time = time();
		else
		$time = strtotime($date);
		$datetime = gmdate('Y-m-d H:i:s', $time);

		// Set up the SQL insert statement and execute it.
		$values = array('uId' => intval($sId), 'bIp' => $ip, 'bDatetime' => $datetime, 'bModified' => $datetime, 'bTitle' => $title, 'bAddress' => $address, 'bDescription' => $description, 'bPrivateNote' => $privateNote, 'bStatus' => intval($status), 'bHash' => md5($address));
		$sql = 'INSERT INTO '. $this->getTableName() .' '. $this->db->sql_build_array('INSERT', $values);
		$this->db->sql_transaction('begin');
		if (!($dbresult = & $this->db->sql_query($sql))) {
			$this->db->sql_transaction('rollback');
			message_die(GENERAL_ERROR, 'Could not insert bookmark', '', __LINE__, __FILE__, $sql, $this->db);
			return false;
		}
		// Get the resultant row ID for the bookmark.
		$bId = $this->db->sql_nextid($dbresult);
		if (!isset($bId) || !is_int($bId)) {
			$this->db->sql_transaction('rollback');
			message_die(GENERAL_ERROR, 'Could not insert bookmark', '', __LINE__, __FILE__, $sql, $this->db);
			return false;
		}

		$uriparts = explode('.', $address);
		$extension = end($uriparts);
		unset($uriparts);

		$b2tservice = & ServiceFactory :: getServiceInstance('Bookmark2TagService');
		if (!$b2tservice->attachTags($bId, $categories, $fromApi, $extension, false, $fromImport)) {
			$this->db->sql_transaction('rollback');
			message_die(GENERAL_ERROR, 'Could not insert bookmark', '', __LINE__, __FILE__, $sql, $this->db);
			return false;
		}
		$this->db->sql_transaction('commit');
		// Everything worked out, so return the new bookmark's bId.
		return $bId;
	}

	function updateBookmark($bId, $address, $title, $description, $privateNote, $status, $categories, $date = NULL, $fromApi = false) {
		if (!is_numeric($bId))
		return false;

		// Get the client's IP address and the date; note that the date is in GMT.
		if (getenv('HTTP_CLIENT_IP'))
		$ip = getenv('HTTP_CLIENT_IP');
		else
		if (getenv('REMOTE_ADDR'))
		$ip = getenv('REMOTE_ADDR');
		else
		$ip = getenv('HTTP_X_FORWARDED_FOR');

		$moddatetime = gmdate('Y-m-d H:i:s', time());

		// Set up the SQL update statement and execute it.
		$updates = array('bModified' => $moddatetime, 'bTitle' => $title, 'bAddress' => $address, 'bDescription' => $description, 'bPrivateNote' => $privateNote, 'bStatus' => $status, 'bHash' => md5($address));

		if (!is_null($date)) {
			$datetime = gmdate('Y-m-d H:i:s', strtotime($date));
			$updates[] = array('bDateTime' => $datetime);
		}

		$sql = 'UPDATE '. $GLOBALS['tableprefix'] .'bookmarks SET '. $this->db->sql_build_array('UPDATE', $updates) .' WHERE bId = '. intval($bId);
		$this->db->sql_transaction('begin');

		if (!($dbresult = & $this->db->sql_query($sql))) {
			$this->db->sql_transaction('rollback');
			message_die(GENERAL_ERROR, 'Could not update bookmark', '', __LINE__, __FILE__, $sql, $this->db);
			return false;
		}

		$uriparts = explode('.', $address);
		$extension = end($uriparts);
		unset($uriparts);

		$b2tservice = & ServiceFactory :: getServiceInstance('Bookmark2TagService');
		if (!$b2tservice->attachTags($bId, $categories, $fromApi, $extension)) {
			$this->db->sql_transaction('rollback');
			message_die(GENERAL_ERROR, 'Could not update bookmark', '', __LINE__, __FILE__, $sql, $this->db);
			return false;
		}

		$this->db->sql_transaction('commit');
		// Everything worked out, so return true.
		return true;
	}

	function & getBookmarks($start = 0, $perpage = NULL, $user = NULL, $tags = NULL, $terms = NULL, $sortOrder = NULL, $watched = NULL, $startdate = NULL, $enddate = NULL, $hash = NULL) {
		// Only get the bookmarks that are visible to the current user.  Our rules:
		//  - if the $user is NULL, that means get bookmarks from ALL users, so we need to make
		//    sure to check the logged-in user's watchlist and get the contacts-only bookmarks from
		//    those users. If the user isn't logged-in, just get the public bookmarks.
		//  - if the $user is set and isn't the logged-in user, then get that user's bookmarks, and
		//    if that user is on the logged-in user's watchlist, get the public AND contacts-only
		//    bookmarks; otherwise, just get the public bookmarks.
		//  - if the $user is set and IS the logged-in user, then get all bookmarks.

		$userservice =& ServiceFactory::getServiceInstance('UserService');
		$b2tservice =& ServiceFactory::getServiceInstance('Bookmark2TagService');
		$tag2tagservice =& ServiceFactory::getServiceInstance('Tag2TagService');
		$sId = $userservice->getCurrentUserId();

		if ($userservice->isLoggedOn()) {
			// All public bookmarks, user's own bookmarks and any shared with user
			$privacy = ' AND ((B.bStatus = 0) OR (B.uId = '. $sId .')';
			$watchnames = $userservice->getWatchNames($sId, true);
			foreach($watchnames as $watchuser) {
				$privacy .= ' OR (U.username = "'. $watchuser .'" AND B.bStatus = 1)';
			}
			$privacy .= ')';
		} else {
			// Just public bookmarks
			$privacy = ' AND B.bStatus = 0';
		}

		// Set up the tags, if need be.
		if (!is_array($tags) && !is_null($tags)) {
			$tags = explode('+', trim($tags));
		}

		$tagcount = count($tags);
		for ($i = 0; $i < $tagcount; $i ++) {
			$tags[$i] = trim($tags[$i]);
		}

		// Set up the SQL query.
		$query_1 = 'SELECT DISTINCT ';
		if (SQL_LAYER == 'mysql4') {
			$query_1 .= 'SQL_CALC_FOUND_ROWS ';
		}
		$query_1 .= 'B.*, U.'. $userservice->getFieldName('username');

		$query_2 = ' FROM '. $userservice->getTableName() .' AS U, '. $this->getTableName() .' AS B';

		$query_3 = ' WHERE B.uId = U.'. $userservice->getFieldName('primary') . $privacy;
		if (is_null($watched)) {
			if (!is_null($user)) {
				$query_3 .= ' AND B.uId = '. $user;
			}
		} else {
			$arrWatch = $userservice->getWatchlist($user);
			if (count($arrWatch) > 0) {
				$query_3_1 = '';
				foreach($arrWatch as $row) {
					$query_3_1 .= 'B.uId = '. intval($row) .' OR ';
				}
				$query_3_1 = substr($query_3_1, 0, -3);
			} else {
				$query_3_1 = 'B.uId = -1';
			}
			$query_3 .= ' AND ('. $query_3_1 .') AND B.bStatus IN (0, 1)';
		}

		$query_5 = '';
		if($hash == null) {
			$query_5.= ' GROUP BY B.bHash';
		}

		switch($sortOrder) {
			case 'date_asc':
				$query_5.= ' ORDER BY B.bModified ASC ';
				break;
			case 'title_desc':
				$query_5.= ' ORDER BY B.bTitle DESC ';
				break;
			case 'title_asc':
				$query_5.= ' ORDER BY B.bTitle ASC ';
				break;
			case 'url_desc':
				$query_5.= ' ORDER BY B.bAddress DESC ';
				break;
			case 'url_asc':
				$query_5.= ' ORDER BY B.bAddress ASC ';
				break;
			default:
				$query_5.= ' ORDER BY B.bModified DESC ';
		}

		// Handle the parts of the query that depend on any tags that are present.
		$query_4 = '';
		for ($i = 0; $i < $tagcount; $i ++) {
			$query_2 .= ', '. $b2tservice->getTableName() .' AS T'. $i;
			$query_4 .= ' AND (';

			$allLinkedTags = $tag2tagservice->getAllLinkedTags($this->db->sql_escape($tags[$i]), '>', $user);

			while (is_array($allLinkedTags) && count($allLinkedTags)>0) {
				$query_4 .= ' T'. $i .'.tag = "'. array_pop($allLinkedTags) .'"';
				$query_4 .= ' OR';
			}

			$query_4 .= ' T'. $i .'.tag = "'. $this->db->sql_escape($tags[$i]) .'"';

			$query_4 .= ') AND T'. $i .'.bId = B.bId';
			//die($query_4);
		}

		// Search terms
		if ($terms) {
			// Multiple search terms okay
			$aTerms = explode(' ', $terms);
			$aTerms = array_map('trim', $aTerms);	

			// Search terms in tags as well when none given
			if (!count($tags)) {
				$query_2 .= ' LEFT JOIN '. $b2tservice->getTableName() .' AS T ON B.bId = T.bId';
				$dotags = true;
			} else {
				$dotags = false;
			}

			$query_4 = '';
			for ($i = 0; $i < count($aTerms); $i++) {
				$query_4 .= ' AND (B.bTitle LIKE "%'. $this->db->sql_escape($aTerms[$i]) .'%"';
				$query_4 .= ' OR B.bDescription LIKE "%'. $this->db->sql_escape($aTerms[$i]) .'%"';
				$query_4 .= ' OR U.username = "'. $this->db->sql_escape($aTerms[$i]) .'"'; //exact match for username
				if ($dotags) {
					$query_4 .= ' OR T.tag = "'. $this->db->sql_escape($aTerms[$i]) .'"';
				}
				$query_4 .= ')';
			}
		}

		// Start and end dates
		if ($startdate) {
			$query_4 .= ' AND B.bDatetime > "'. $startdate .'"';
		}
		if ($enddate) {
			$query_4 .= ' AND B.bDatetime < "'. $enddate .'"';
		}

		// Hash
		if ($hash) {
			$query_4 .= ' AND B.bHash = "'. $hash .'"';
		}
		$query = $query_1 . $query_2 . $query_3 . $query_4 . $query_5;
		//die($query);
		if (!($dbresult = & $this->db->sql_query_limit($query, intval($perpage), intval($start)))) {
			message_die(GENERAL_ERROR, 'Could not get bookmarks', '', __LINE__, __FILE__, $query, $this->db);
			return false;
		}

		if (SQL_LAYER == 'mysql4') {
			$totalquery = 'SELECT FOUND_ROWS() AS total';
		} else {
			if ($hash) {
				$totalquery = 'SELECT COUNT(*) AS total'. $query_2 . $query_3 . $query_4;
			} else {
				$totalquery = 'SELECT COUNT(DISTINCT bAddress) AS total'. $query_2 . $query_3 . $query_4;
			}
		}

		if (!($totalresult = & $this->db->sql_query($totalquery)) || (!($row = & $this->db->sql_fetchrow($totalresult)))) {
			message_die(GENERAL_ERROR, 'Could not get total bookmarks', '', __LINE__, __FILE__, $totalquery, $this->db);
			return false;
		}

		$total = $row['total'];

		$bookmarks = array();
		while ($row = & $this->db->sql_fetchrow($dbresult)) {
			$row['tags'] = $b2tservice->getTagsForBookmark(intval($row['bId']));
			$bookmarks[] = $row;
		}

		$this->db->sql_freeresult($dbresult);
		$output = array ('bookmarks' => $bookmarks, 'total' => $total);
		return $output;
	}

	function deleteBookmark($bookmarkid) {
		$query = 'DELETE FROM '. $GLOBALS['tableprefix'] .'bookmarks WHERE bId = '. intval($bookmarkid);
		$this->db->sql_transaction('begin');
		if (!($dbresult = & $this->db->sql_query($query))) {
			$this->db->sql_transaction('rollback');
			message_die(GENERAL_ERROR, 'Could not delete bookmarks', '', __LINE__, __FILE__, $query, $this->db);
			return false;
		}

		$query = 'DELETE FROM '. $GLOBALS['tableprefix'] .'tags WHERE bId = '. intval($bookmarkid);
		$this->db->sql_transaction('begin');
		if (!($dbresult = & $this->db->sql_query($query))) {
			$this->db->sql_transaction('rollback');
			message_die(GENERAL_ERROR, 'Could not delete bookmarks', '', __LINE__, __FILE__, $query, $this->db);
			return false;
		}

		$this->db->sql_transaction('commit');
		return true;
	}

	function deleteBookmarksForUser($uId) {
		$query = 'DELETE FROM '. $GLOBALS['tableprefix'] .'bookmarks WHERE uId = '. intval($uId);

		if (!($dbresult = & $this->db->sql_query($query))) {
			message_die(GENERAL_ERROR, 'Could not delete bookmarks', '', __LINE__, __FILE__, $query, $this->db);
			return false;
		}

		return true;
	}

	function countOthers($address) {
		if (!$address) {
			return false;
		}

		$userservice = & ServiceFactory :: getServiceInstance('UserService');
		$sId = $userservice->getCurrentUserId();

		if ($userservice->isLoggedOn()) {
			// All public bookmarks, user's own bookmarks and any shared with user
			$privacy = ' AND ((B.bStatus = 0) OR (B.uId = '. $sId .')';
			$watchnames = $userservice->getWatchNames($sId, true);
			foreach($watchnames as $watchuser) {
				$privacy .= ' OR (U.username = "'. $watchuser .'" AND B.bStatus = 1)';
			}
			$privacy .= ')';
		} else {
			// Just public bookmarks
			$privacy = ' AND B.bStatus = 0';
		}

		$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;
		if (!($dbresult = & $this->db->sql_query($sql))) {
			message_die(GENERAL_ERROR, 'Could not get vars', '', __LINE__, __FILE__, $sql, $this->db);
		}
		
		$output = $this->db->sql_fetchfield(0, 0) - 1;
		$this->db->sql_freeresult($dbresult);
		return $output;
	}

	function normalize($address) {
		// If bookmark address doesn't contain ":", add "http://" to the start as a default protocol
		if (strpos($address, ':') === false) {
			$address = 'http://'. $address;
		}

		// Delete final /
		if (substr($address, -1) == '/') {
			$address = substr($address, 0, count($address)-2);
		}

		return $address;
	}

	function deleteAll() {
		$query = 'TRUNCATE TABLE `'. $this->getTableName() .'`';
		$this->db->sql_query($query);
	}

	// Properties
	function getTableName()       { return $this->tablename; }
	function setTableName($value) { $this->tablename = $value; }

}



?>