* Updated isValidEmail regex to match plussed addresses
[scuttle] / bookmarks.php
1 <?php
2 /***************************************************************************
3 Copyright (C) 2004 - 2006 Scuttle project
4 http://sourceforge.net/projects/scuttle/
5 http://scuttle.org/
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20 ***************************************************************************/
21
22 require_once('header.inc.php');
23
24 $bookmarkservice =& ServiceFactory::getServiceInstance('BookmarkService');
25 $templateservice =& ServiceFactory::getServiceInstance('TemplateService');
26 $userservice =& ServiceFactory::getServiceInstance('UserService');
27 $cacheservice =& ServiceFactory::getServiceInstance('CacheService');
28
29 $tplVars = array();
30
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));
34     exit();
35 }
36
37 @list($url, $user, $cat) = isset($_SERVER['PATH_INFO']) ? explode('/', $_SERVER['PATH_INFO']) : NULL;
38
39 $loggedon = false;
40 if ($userservice->isLoggedOn()) {
41     $loggedon = true;
42     $currentUser = $userservice->getCurrentUser();
43     $currentUserID = $userservice->getCurrentUserId();
44     $currentUsername = $currentUser[$userservice->getFieldName('username')];
45 }
46
47 $endcache = false;
48 if ($usecache) {
49     // Generate hash for caching on
50     $hash = md5($_SERVER['REQUEST_URI'] . $user);
51
52     // Don't cache if its users' own bookmarks
53     if ($loggedon) {
54         if ($currentUsername != $user) {
55             // Cache for 5 minutes
56             $cacheservice->Start($hash);
57             $endcache = true;
58         }
59     } else {
60         // Cache for 30 minutes
61         $cacheservice->Start($hash, 1800);
62         $endcache = true;
63     }
64 }
65
66 $pagetitle = $rssCat = $catTitle = '';
67 if ($user) {
68     if (is_int($user)) {
69         $userid = intval($user);
70     } else {
71         if (!($userinfo = $userservice->getUserByUsername($user))) {
72             $tplVars['error'] = sprintf(T_('User with username %s was not found'), $user);
73             $templateservice->loadTemplate('error.404.tpl', $tplVars);
74             exit();
75         } else {
76             $userid =& $userinfo['uId'];
77         }
78     }
79     $pagetitle .= ': '. $user;
80 }
81 if ($cat) {
82     $catTitle = ': '. str_replace('+', ' + ', $cat);
83     $pagetitle .= $catTitle;
84 }
85 $pagetitle = substr($pagetitle, 2);
86
87 // Header variables
88 $tplVars['loadjs'] = true;
89
90 // ADD A BOOKMARK
91 $saved = false;
92 $templatename = 'bookmarks.tpl';
93 if ($loggedon && isset($_POST['submitted'])) {
94     if (!$_POST['title'] || !$_POST['address']) {
95         $tplVars['error'] = T_('Your bookmark must have a title and an address');
96         $templatename = 'editbookmark.tpl';
97     } else {
98         $address = trim($_POST['address']);
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         // If it's new, save it
105         } else {
106             $title = trim($_POST['title']);
107             $description = trim($_POST['description']);
108             $status = intval($_POST['status']);
109             $categories = trim($_POST['tags']);
110             $saved = true;
111             if ($bookmarkservice->addBookmark($address, $title, $description, $status, $categories)) {
112                 if (isset($_POST['popup'])) {
113                     $tplVars['msg'] = '<script type="text/javascript">window.close();</script>';
114                 } else {
115                     $tplVars['msg'] = T_('Bookmark saved');
116                     // Redirection option
117                     if ($GLOBALS['useredir']) {
118                         $address = $GLOBALS['url_redir'] . $address;
119                     }
120                     header('Location: '. $address);
121                 }
122             } else {
123                 $tplVars['error'] = T_('There was an error saving your bookmark. Please try again or contact the administrator.');
124                 $templatename = 'editbookmark.tpl';
125                 $saved = false;
126             }
127         }
128     }
129 }
130
131 if (isset($_GET['action']) && ($_GET['action'] == "add")) {
132     // If the bookmark exists already, edit the original
133     if ($bookmarkservice->bookmarkExists(stripslashes($_GET['address']), $currentUserID)) {
134         $bookmark =& $bookmarkservice->getBookmarkByAddress(stripslashes($_GET['address']));
135         $popup = (isset($_GET['popup'])) ? '?popup=1' : '';
136         header('Location: '. createURL('edit', $bookmark['bId'] . $popup));
137         exit();
138     }
139     $templatename = 'editbookmark.tpl';
140 }
141  
142 if ($templatename == 'editbookmark.tpl') {
143     if ($loggedon) {
144         $tplVars['formaction']  = createURL('bookmarks', $currentUsername);
145         if (isset($_POST['submitted'])) {
146             $tplVars['row'] = array(
147                 'bTitle' => stripslashes($_POST['title']),
148                 'bAddress' => stripslashes($_POST['address']),
149                 'bDescription' => stripslashes($_POST['description']),
150                 'tags' => ($_POST['tags'] ? explode(',', stripslashes($_POST['tags'])) : array())
151             );
152             $tplVars['tags'] = $_POST['tags'];
153         } else {
154             $tplVars['row'] = array(
155                 'bTitle' => stripslashes($_GET['title']),
156                 'bAddress' => stripslashes($_GET['address']),
157                 'bDescription' => stripslashes($_GET['description']),
158                 'tags' => ($_GET['tags'] ? explode(',', stripslashes($_GET['tags'])) : array())
159             );
160         }
161         $title = T_('Add a Bookmark');
162         $tplVars['pagetitle'] = $title;
163         $tplVars['subtitle'] = $title;
164         $tplVars['btnsubmit'] = T_('Add Bookmark');
165         $tplVars['popup'] = (isset($_GET['popup'])) ? $_GET['popup'] : null;
166     } else {
167         $tplVars['error'] = T_('You must be logged in before you can add bookmarks.');
168     }
169 } else if ($user && !isset($_GET['popup'])) {
170         
171     $tplVars['sidebar_blocks'] = array('profile', 'watchstatus');
172
173     if (!$cat) {
174         $cat = NULL;
175         $tplVars['currenttag'] = NULL; 
176     } else {
177         $rssCat = '/'. filter($cat, 'url');
178         $tplVars['currenttag'] = $cat;
179         $tplVars['sidebar_blocks'][] = 'related';
180         $tplVars['sidebar_blocks'][] = 'tagactions';
181     }
182     $tplVars['popCount'] = 30;
183     $tplVars['sidebar_blocks'][] = 'popular';
184     
185     $tplVars['userid'] = $userid;
186     $tplVars['userinfo'] =& $userinfo;
187     $tplVars['user'] = $user;
188     $tplVars['range'] = 'user';
189     
190     // Pagination
191     $perpage = getPerPageCount();
192     if (isset($_GET['page']) && intval($_GET['page']) > 1) {
193         $page = $_GET['page'];
194         $start = ($page - 1) * $perpage;
195     } else {
196         $page = 0;
197         $start = 0;
198     }
199     
200     // Set template vars
201     $tplVars['rsschannels'] = array(
202         array(filter($sitename .': '. $pagetitle), createURL('rss', filter($user, 'url') . $rssCat))
203     );
204
205     $tplVars['page'] = $page;
206     $tplVars['start'] = $start;
207     $tplVars['bookmarkCount'] = $start + 1;
208     
209     $bookmarks =& $bookmarkservice->getBookmarks($start, $perpage, $userid, $cat, $terms, getSortOrder());
210     $tplVars['total'] = $bookmarks['total'];
211     $tplVars['bookmarks'] =& $bookmarks['bookmarks'];
212     $tplVars['cat_url'] = createURL('bookmarks', '%s/%s');
213     $tplVars['nav_url'] = createURL('bookmarks', '%s/%s%s');
214     if ($user == $currentUsername) {
215         $title = T_('My Bookmarks') . filter($catTitle);
216     } else {
217         $title = filter($pagetitle);
218     }
219     $tplVars['pagetitle'] = $title;
220     $tplVars['subtitle'] = $title;
221 }
222 $templateservice->loadTemplate($templatename, $tplVars);
223
224 if ($usecache && $endcache) {
225     // Cache output if existing copy has expired
226     $cacheservice->End($hash);
227 }
228 ?>

Benjamin Mako Hill || Want to submit a patch?