summaryrefslogtreecommitdiff
path: root/bookmark_lib.php
blob: 3e36be25e6cfb8c0dc5ea1c4635c8234bf07a7f4 (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
<?php
class BookmarkLib extends BitBase {
	function BookmarkLib() {
	BitBase::BitBase();
	}
	function get_folder_path($folder_id, $user_id) {
		$path = '';
		$info = $this->get_folder($folder_id, $user_id);
		$path = '<a href='.USERS_PKG_URL.'bookmarks.php?parent_id="' . $info["folder_id"] . '">' . $info["name"] . '</a>';
		while ($info["parent_id"] != 0) {
			$info = $this->get_folder($info["parent_id"], $user_id);
			$path
				= $path = '<a href='.USERS_PKG_URL.'bookmarks.php?parent_id="' . $info["folder_id"] . '">' . $info["name"] . '</a>' . '>' . $path;
		}
		return $path;
	}
	function get_folder($folder_id, $user_id) {
		$query = "select * from `".BIT_DB_PREFIX."tiki_user_bookmarks_folders` where `folder_id`=? and `user_id`=?";
		$result = $this->query($query,array($folder_id,$user_id));
		if (!$result->numRows())
			return false;
		$res = $result->fetchRow();
		return $res;
	}
	function get_url($url_id) {
		$query = "select * from `".BIT_DB_PREFIX."tiki_user_bookmarks_urls` where `url_id`=?";
		$result = $this->query($query,array($url_id));
		if (!$result->numRows())
			return false;
		$res = $result->fetchRow();
		return $res;
	}
	function remove_url($url_id, $user_id) {
		$query = "delete from `".BIT_DB_PREFIX."tiki_user_bookmarks_urls` where `url_id`=? and `user_id`=?";
		$result = $this->query($query,array($url_id,$user_id));
		return true;
	}
	function remove_folder($folder_id, $user_id) {
		// Delete the category
		$query = "delete from `".BIT_DB_PREFIX."tiki_user_bookmarks_folders` where `folder_id`=? and `user_id`=?";
		$result = $this->query($query,array($folder_id,$user_id));
		// Remove objects for this category
		$query = "delete from `".BIT_DB_PREFIX."tiki_user_bookmarks_urls` where `folder_id`=? and `user_id`=?";
		$result = $this->query($query,array($folder_id,$user_id));
		// SUbfolders
		$query = "select `folder_id` from `".BIT_DB_PREFIX."tiki_user_bookmarks_folders` where `parent_id`=? and `user_id`=?";
		$result = $this->query($query,array($folder_id,$user_id));
		while ($res = $result->fetchRow()) {
			// Recursively remove the subcategory
			$this->remove_folder($res["folder_id"], $user_id);
		}
		return true;
	}
	function update_folder($folder_id, $name, $user_id) {
		$query = "update `".BIT_DB_PREFIX."tiki_user_bookmarks_folders` set `name`=? where `folder_id`=? and `user_id`=?";
		$result = $this->query($query,array($name,$folder_id,$user_id));
	}
	function add_folder($parent_id, $name, $user_id) {
		// Don't allow empty/blank folder names.
		if (empty($name))
			return false;
		$query = "insert into `".BIT_DB_PREFIX."tiki_user_bookmarks_folders`(`name`,`parent_id`,`user_id`) values(?,?,?)";
		$result = $this->query($query,array($name,$parent_id,$user_id));
	}
	function replace_url($url_id, $folder_id, $name, $url, $user_id) {
		$id = NULL;
		if( strlen( $url ) < 250 ) {
			$now = date("U");
			if ($url_id) {
				$query = "update `".BIT_DB_PREFIX."tiki_user_bookmarks_urls` set `user_id`=?,`last_updated`=?,`folder_id`=?,`name`=?,`url`=? where `url_id`=?";
				$bindvars=array($user_id,(int) $now,$folder_id,$name,$url,$url_id);
			} else {
				$query = " insert into `".BIT_DB_PREFIX."tiki_user_bookmarks_urls`(`name`,`url`,`data`,`last_updated`,`folder_id`,`user_id`)
		  values(?,?,?,?,?,?)";
					$bindvars=array($name,$url,'',(int) $now,$folder_id,$user_id);
			}
			$result = $this->query($query,$bindvars);
			$id = $this->getOne("select max(`url_id`) from `".BIT_DB_PREFIX."tiki_user_bookmarks_urls` where `url`=? and `last_updated`=?",array($url,(int) $now));
		}
		return $id;
	}
	function refresh_url($url_id) {
		$info = $this->get_url($url_id);
		if (strstr($info["url"], 'tiki_') || strstr($info["url"], 'messu_'))
			return false;
		@$fp = fopen($info["url"], "r");
		if (!$fp)
			return;
		$data = '';
		while (!feof($fp)) {
			$data .= fread($fp, 4096);
		}
		fclose ($fp);
		$now = date("U");
		$query = "update `".BIT_DB_PREFIX."tiki_user_bookmarks_urls` set `last_updated`=?, `data`=? where `url_id`=?";
		$result = $this->query($query,array((int) $now,BitDb::db_byte_encode( $data ),$url_id));
		return true;
	}
	function list_folder($folder_id, $offset, $maxRecords, $sort_mode = 'name_asc', $find, $user_id) {
		if ($find) {
			$findesc = '%' . strtoupper( $find ) . '%';
			$mid = " and UPPER(`name`) like ? or UPPER(`url`) like ?";
			$bindvars=array($folder_id,$user_id,$findesc,$findesc);
		} else {
			$mid = "";
			$bindvars=array($folder_id,$user_id);
		}
		$query = "select * from `".BIT_DB_PREFIX."tiki_user_bookmarks_urls` where `folder_id`=? and `user_id`=? $mid order by ".$this->convert_sortmode($sort_mode);
		$query_cant = "select count(*) from `".BIT_DB_PREFIX."tiki_user_bookmarks_urls` where `folder_id`=? and `user_id`=? $mid";
		$result = $this->query($query,$bindvars,$maxRecords,$offset);
		$cant = $this->getOne($query_cant,$bindvars);
		$ret = array();
		while ($res = $result->fetchRow()) {
			$res["datalen"] = strlen($res["data"]);
			$ret[] = $res;
		}
		$retval = array();
		$retval["data"] = $ret;
		$retval["cant"] = $cant;
		return $retval;
	}
	function get_child_folders($folder_id, $user_id) {
		$ret = array();
		$query = "select * from `".BIT_DB_PREFIX."tiki_user_bookmarks_folders` where `parent_id`=? and `user_id`=?";
		$result = $this->query($query,array($folder_id,$user_id));
		while ($res = $result->fetchRow()) {
			$cant = $this->getOne("select count(*) from `".BIT_DB_PREFIX."tiki_user_bookmarks_urls` where `folder_id`=?",array($res["folder_id"]));
			$res["urls"] = $cant;
			$ret[] = $res;
		}
		return $ret;
	}
}
$bookmarklib = new BookmarkLib();
?>