2 /***************************************************************************
3 Copyright (c) 2004 - 2010 Marcus Campbell
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 ***************************************************************************/
21 require_once 'header.inc.php';
23 $sf = new ServiceFactory();
24 $bookmarkservice =& $sf->getServiceInstance('BookmarkService');
25 $templateservice =& $sf->getServiceInstance('TemplateService');
26 $userservice =& $sf->getServiceInstance('UserService');
27 $cacheservice =& $sf->getServiceInstance('CacheService');
31 if (isset($_GET['action']) && ($_GET['action'] == "add") && !$userservice->isLoggedOn()) {
32 $loginqry = str_replace("'", '%27', stripslashes($_SERVER['QUERY_STRING']));
33 header('Location: '. createURL('login', '?'. $loginqry));
37 # it would probably be better to not supress the errors here
38 @list($url, $user, $cat) = isset($_SERVER['PATH_INFO']) ? explode('/', $_SERVER['PATH_INFO']) : NULL;
41 if ($userservice->isLoggedOn()) {
43 $currentUser = $userservice->getCurrentUser();
44 $currentUserID = $userservice->getCurrentUserId();
45 $currentUsername = $currentUser[$userservice->getFieldName('username')];
50 // Generate hash for caching on
51 $hash = md5($_SERVER['REQUEST_URI'] . $user);
53 // Don't cache if its users' own bookmarks
55 if ($currentUsername != $user) {
56 // Cache for 5 minutes
57 $cacheservice->Start($hash);
61 // Cache for 30 minutes
62 $cacheservice->Start($hash, 1800);
67 $pagetitle = $rssCat = $catTitle = '';
70 $userid = intval($user);
72 if (!($userinfo = $userservice->getUserByUsername($user))) {
73 $tplVars['error'] = sprintf(T_('User with username %s was not found'), $user);
74 $templateservice->loadTemplate('error.404.tpl', $tplVars);
77 $userid =& $userinfo['uId'];
80 $pagetitle .= ': '. $user;
83 $catTitle = ': '. str_replace('+', ' + ', $cat);
84 $pagetitle .= $catTitle;
86 $pagetitle = substr($pagetitle, 2);
89 $tplVars['loadjs'] = true;
93 $templatename = 'bookmarks.tpl';
94 if ($loggedon && isset($_POST['submitted'])) {
95 if (!$_POST['title'] || !$_POST['address']) {
96 $tplVars['error'] = T_('Your bookmark must have a title and an address');
97 $templatename = 'editbookmark.tpl';
99 $address = trim($_POST['address']);
101 // If the bookmark exists already, edit the original
102 if ($bookmarkservice->bookmarkExists($address, $currentUserID)) {
103 $bookmark =& $bookmarkservice->getBookmarkByAddress($address);
104 header('Location: '. createURL('edit', $bookmark['bId']));
107 // If it's new, save it
109 $title = trim($_POST['title']);
110 $description = trim($_POST['description']);
111 $status = intval($_POST['status']);
112 $categories = trim($_POST['tags']);
114 if ($bookmarkservice->addBookmark($address, $title, $description, $status, $categories)) {
115 if (isset($_POST['popup'])) {
116 $tplVars['msg'] = '<script type="text/javascript">window.close();</script>';
118 $tplVars['msg'] = T_('Bookmark saved');
121 $tplVars['error'] = T_('There was an error saving your bookmark. Please try again or contact the administrator.');
122 $templatename = 'editbookmark.tpl';
129 if (isset($_GET['action']) && ($_GET['action'] == "add")) {
130 // If the bookmark exists already, edit the original
131 if ($bookmarkservice->bookmarkExists(stripslashes($_GET['address']), $currentUserID)) {
132 $bookmark =& $bookmarkservice->getBookmarkByAddress(stripslashes($_GET['address']));
133 $popup = (isset($_GET['popup'])) ? '?popup=1' : '';
134 header('Location: '. createURL('edit', $bookmark['bId'] . $popup));
137 $templatename = 'editbookmark.tpl';
140 if ($templatename == 'editbookmark.tpl') {
142 $tplVars['formaction'] = createURL('bookmarks', $currentUsername);
143 if (isset($_POST['submitted'])) {
144 $tplVars['row'] = array(
145 'bTitle' => stripslashes($_POST['title']),
146 'bAddress' => stripslashes($_POST['address']),
147 'bDescription' => stripslashes($_POST['description']),
148 'tags' => ($_POST['tags'] ? explode(',', stripslashes($_POST['tags'])) : array())
150 $tplVars['tags'] = $_POST['tags'];
152 if (isset($_GET['tags'])) {
153 $raw_tags = $_GET['tags'];
157 $tplVars['row'] = array(
158 'bTitle' => stripslashes($_GET['title']),
159 'bAddress' => stripslashes($_GET['address']),
160 'bDescription' => stripslashes($_GET['description']),
161 'tags' => ($raw_tags ? explode(',', stripslashes($raw_tags)) : array())
164 $title = T_('Add a Bookmark');
165 $tplVars['pagetitle'] = $title;
166 $tplVars['subtitle'] = $title;
167 $tplVars['btnsubmit'] = T_('Add Bookmark');
168 $tplVars['popup'] = (isset($_GET['popup'])) ? $_GET['popup'] : null;
170 $tplVars['error'] = T_('You must be logged in before you can add bookmarks.');
172 } else if ($user && !isset($_GET['popup'])) {
174 $tplVars['sidebar_blocks'] = array('profile', 'watchstatus');
178 $tplVars['currenttag'] = NULL;
180 $rssCat = '/'. filter($cat, 'url');
181 $tplVars['currenttag'] = $cat;
182 $tplVars['sidebar_blocks'][] = 'related';
183 $tplVars['sidebar_blocks'][] = 'tagactions';
185 $tplVars['popCount'] = 30;
186 $tplVars['sidebar_blocks'][] = 'popular';
188 $tplVars['userid'] = $userid;
189 $tplVars['userinfo'] =& $userinfo;
190 $tplVars['user'] = $user;
191 $tplVars['range'] = 'user';
194 $perpage = getPerPageCount();
195 if (isset($_GET['page']) && intval($_GET['page']) > 1) {
196 $page = $_GET['page'];
197 $start = ($page - 1) * $perpage;
204 $tplVars['rsschannels'] = array(
205 array(filter($sitename .': '. $pagetitle), createURL('rss', filter($user, 'url') . $rssCat))
208 $tplVars['page'] = $page;
209 $tplVars['start'] = $start;
210 $tplVars['bookmarkCount'] = $start + 1;
212 $bookmarks =& $bookmarkservice->getBookmarks($start, $perpage, $userid, $cat, '', getSortOrder());
213 $tplVars['total'] = $bookmarks['total'];
214 $tplVars['bookmarks'] =& $bookmarks['bookmarks'];
215 $tplVars['cat_url'] = createURL('bookmarks', '%s/%s');
216 $tplVars['nav_url'] = createURL('bookmarks', '%s/%s%s');
217 if ($user == $currentUsername) {
218 $title = T_('My Bookmarks') . filter($catTitle);
220 $title = filter($pagetitle);
222 $tplVars['pagetitle'] = $title;
223 $tplVars['subtitle'] = $title;
225 $templateservice->loadTemplate($templatename, $tplVars);
227 if ($usecache && $endcache) {
228 // Cache output if existing copy has expired
229 $cacheservice->End($hash);