increased description size to 1000 characters
[scuttle] / bookmarks.php
1 <?php
2 /***************************************************************************
3 Copyright (c) 2004 - 2010 Marcus Campbell
4 http://scuttle.org/
5
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.
10
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.
15
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 ***************************************************************************/
20
21 require_once 'header.inc.php';
22
23 $bookmarkservice =& ServiceFactory::getServiceInstance('BookmarkService');
24 $templateservice =& ServiceFactory::getServiceInstance('TemplateService');
25 $userservice     =& ServiceFactory::getServiceInstance('UserService');
26 $cacheservice    =& ServiceFactory::getServiceInstance('CacheService');
27
28 $tplVars = array();
29
30 if (isset($_GET['action']) && ($_GET['action'] == "add") && !$userservice->isLoggedOn()) {
31     $loginqry = str_replace("'", '%27', stripslashes($_SERVER['QUERY_STRING']));
32     header('Location: '. createURL('login', '?'. $loginqry));
33     exit();
34 }
35
36 @list($url, $user, $cat) = isset($_SERVER['PATH_INFO']) ? explode('/', $_SERVER['PATH_INFO']) : NULL;
37
38 $loggedon = false;
39 if ($userservice->isLoggedOn()) {
40     $loggedon = true;
41     $currentUser = $userservice->getCurrentUser();
42     $currentUserID = $userservice->getCurrentUserId();
43     $currentUsername = $currentUser[$userservice->getFieldName('username')];
44 }
45
46 $endcache = false;
47 if ($usecache) {
48     // Generate hash for caching on
49     $hash = md5($_SERVER['REQUEST_URI'] . $user);
50
51     // Don't cache if its users' own bookmarks
52     if ($loggedon) {
53         if ($currentUsername != $user) {
54             // Cache for 5 minutes
55             $cacheservice->Start($hash);
56             $endcache = true;
57         }
58     } else {
59         // Cache for 30 minutes
60         $cacheservice->Start($hash, 1800);
61         $endcache = true;
62     }
63 }
64
65 $pagetitle = $rssCat = $catTitle = '';
66 if ($user) {
67     if (is_int($user)) {
68         $userid = intval($user);
69     } else {
70         if (!($userinfo = $userservice->getUserByUsername($user))) {
71             $tplVars['error'] = sprintf(T_('User with username %s was not found'), $user);
72             $templateservice->loadTemplate('error.404.tpl', $tplVars);
73             exit();
74         } else {
75             $userid =& $userinfo['uId'];
76         }
77     }
78     $pagetitle .= ': '. $user;
79 }
80 if ($cat) {
81     $catTitle = ': '. str_replace('+', ' + ', $cat);
82     $pagetitle .= $catTitle;
83 }
84 $pagetitle = substr($pagetitle, 2);
85
86 // Header variables
87 $tplVars['loadjs'] = true;
88
89 // ADD A BOOKMARK
90 $saved = false;
91 $templatename = 'bookmarks.tpl';
92 if ($loggedon && isset($_POST['submitted'])) {
93     if (!$_POST['title'] || !$_POST['address']) {
94         $tplVars['error'] = T_('Your bookmark must have a title and an address');
95         $templatename = 'editbookmark.tpl';
96     } else {
97         $address = trim($_POST['address']);
98
99         // If the bookmark exists already, edit the original
100         if ($bookmarkservice->bookmarkExists($address, $currentUserID)) {
101             $bookmark =& $bookmarkservice->getBookmarkByAddress($address);
102             header('Location: '. createURL('edit', $bookmark['bId']));
103             exit();
104
105         // If it's new, save it
106         } else {
107             $title = trim($_POST['title']);
108             $description = trim($_POST['description']);
109             $status = intval($_POST['status']);
110             $categories = trim($_POST['tags']);
111             $saved = true;
112             if ($bookmarkservice->addBookmark($address, $title, $description, $status, $categories)) {
113                 if (isset($_POST['popup'])) {
114                     $tplVars['msg'] = '<script type="text/javascript">window.close();</script>';
115                 } else {
116                     $tplVars['msg'] = T_('Bookmark saved');
117                 }
118             } else {
119                 $tplVars['error'] = T_('There was an error saving your bookmark. Please try again or contact the administrator.');
120                 $templatename = 'editbookmark.tpl';
121                 $saved = false;
122             }
123         }
124     }
125 }
126
127 if (isset($_GET['action']) && ($_GET['action'] == "add")) {
128     // If the bookmark exists already, edit the original
129     if ($bookmarkservice->bookmarkExists(stripslashes($_GET['address']), $currentUserID)) {
130         $bookmark =& $bookmarkservice->getBookmarkByAddress(stripslashes($_GET['address']));
131         $popup = (isset($_GET['popup'])) ? '?popup=1' : '';
132         header('Location: '. createURL('edit', $bookmark['bId'] . $popup));
133         exit();
134     }
135     $templatename = 'editbookmark.tpl';
136 }
137  
138 if ($templatename == 'editbookmark.tpl') {
139     if ($loggedon) {
140         $tplVars['formaction']  = createURL('bookmarks', $currentUsername);
141         if (isset($_POST['submitted'])) {
142             $tplVars['row'] = array(
143                 'bTitle' => stripslashes($_POST['title']),
144                 'bAddress' => stripslashes($_POST['address']),
145                 'bDescription' => stripslashes($_POST['description']),
146                 'tags' => ($_POST['tags'] ? explode(',', stripslashes($_POST['tags'])) : array())
147             );
148             $tplVars['tags'] = $_POST['tags'];
149         } else {
150             $tplVars['row'] = array(
151                 'bTitle' => stripslashes($_GET['title']),
152                 'bAddress' => stripslashes($_GET['address']),
153                 'bDescription' => stripslashes($_GET['description']),
154                 'tags' => ($_GET['tags'] ? explode(',', stripslashes($_GET['tags'])) : array())
155             );
156         }
157         $title = T_('Add a Bookmark');
158         $tplVars['pagetitle'] = $title;
159         $tplVars['subtitle'] = $title;
160         $tplVars['btnsubmit'] = T_('Add Bookmark');
161         $tplVars['popup'] = (isset($_GET['popup'])) ? $_GET['popup'] : null;
162     } else {
163         $tplVars['error'] = T_('You must be logged in before you can add bookmarks.');
164     }
165 } else if ($user && !isset($_GET['popup'])) {
166         
167     $tplVars['sidebar_blocks'] = array('profile', 'watchstatus');
168
169     if (!$cat) {
170         $cat = NULL;
171         $tplVars['currenttag'] = NULL; 
172     } else {
173         $rssCat = '/'. filter($cat, 'url');
174         $tplVars['currenttag'] = $cat;
175         $tplVars['sidebar_blocks'][] = 'related';
176         $tplVars['sidebar_blocks'][] = 'tagactions';
177     }
178     $tplVars['popCount'] = 30;
179     $tplVars['sidebar_blocks'][] = 'popular';
180     
181     $tplVars['userid'] = $userid;
182     $tplVars['userinfo'] =& $userinfo;
183     $tplVars['user'] = $user;
184     $tplVars['range'] = 'user';
185     
186     // Pagination
187     $perpage = getPerPageCount();
188     if (isset($_GET['page']) && intval($_GET['page']) > 1) {
189         $page = $_GET['page'];
190         $start = ($page - 1) * $perpage;
191     } else {
192         $page = 0;
193         $start = 0;
194     }
195     
196     // Set template vars
197     $tplVars['rsschannels'] = array(
198         array(filter($sitename .': '. $pagetitle), createURL('rss', filter($user, 'url') . $rssCat))
199     );
200
201     $tplVars['page'] = $page;
202     $tplVars['start'] = $start;
203     $tplVars['bookmarkCount'] = $start + 1;
204     
205     $bookmarks =& $bookmarkservice->getBookmarks($start, $perpage, $userid, $cat, $terms, getSortOrder());
206     $tplVars['total'] = $bookmarks['total'];
207     $tplVars['bookmarks'] =& $bookmarks['bookmarks'];
208     $tplVars['cat_url'] = createURL('bookmarks', '%s/%s');
209     $tplVars['nav_url'] = createURL('bookmarks', '%s/%s%s');
210     if ($user == $currentUsername) {
211         $title = T_('My Bookmarks') . filter($catTitle);
212     } else {
213         $title = filter($pagetitle);
214     }
215     $tplVars['pagetitle'] = $title;
216     $tplVars['subtitle'] = $title;
217 }
218 $templateservice->loadTemplate($templatename, $tplVars);
219
220 if ($usecache && $endcache) {
221     // Cache output if existing copy has expired
222     $cacheservice->End($hash);
223 }

Benjamin Mako Hill || Want to submit a patch?