source: trunk/bookmarks/bookmarks.php @ 1937

Revision 1937, 10.6 KB checked in by Xiping.Wang, 8 months ago (diff)

[trunk] implement #321, we add a new configuration file for recaptcha keys.

Line 
1<?php
2/*
3       
4        This file is part of omCollab.
5       
6        Copyright (c) 2008 omCollab (Openmethodology Project)
7        http://www.openmethodology.org
8        http://mike2.openmethodology.org/wiki/OmCollab
9       
10        And parts copyright of Scuttle project
11        (http://sourceforge.net/projects/scuttle/, http://scuttle.org/)
12       
13        omCollab is free software: you can redistribute it and/or modify
14        it under the terms of the GNU General Public License as published by
15        the Free Software Foundation, either version 3 of the License, or
16        (at your option) any later version.
17       
18        omCollab is distributed in the hope that it will be useful,
19        but WITHOUT ANY WARRANTY; without even the implied warranty of
20        MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21        GNU General Public License for more details.
22
23*/
24
25
26require_once('header.inc.php');
27
28$bookmarkservice =& ServiceFactory::getServiceInstance('BookmarkService');
29$templateservice =& ServiceFactory::getServiceInstance('TemplateService');
30$userservice =& ServiceFactory::getServiceInstance('UserService');
31$cacheservice =& ServiceFactory::getServiceInstance('CacheService');
32
33$tplVars = array();
34
35if (isset($_GET['action']) && ($_GET['action'] == "add") && !$userservice->isLoggedOn()) {
36    $loginqry = str_replace("'", '%27', stripslashes($_SERVER['QUERY_STRING']));
37    header('Location: '. createURL('login', '?'. $loginqry));
38    exit();
39}
40
41@list($url, $user, $cat) = isset($_SERVER['PATH_INFO']) ? explode('/', $_SERVER['PATH_INFO']) : NULL;
42
43$loggedon        = false;
44$currentUser     = '';
45$currentUserID   = null;
46$currentUsername = '';
47   
48if ($userservice->isLoggedOn()) {
49    $loggedon = true;
50    $currentUser = $userservice->getCurrentUser();
51    $currentUserID = $userservice->getCurrentUserId();
52    $currentUsername = $currentUser[$userservice->getFieldName('username')];
53}
54
55$endcache = false;
56if ($usecache) {
57    // Generate hash for caching on
58    $hash = md5($_SERVER['REQUEST_URI'] . $user);
59
60    // Don't cache if its users' own bookmarks
61    if ($loggedon) {
62        if ($currentUsername != $user) {
63            // Cache for 5 minutes
64            $cacheservice->Start($hash);
65            $endcache = true;
66        }
67    } else {
68        // Cache for 30 minutes
69        $cacheservice->Start($hash, 1800);
70        $endcache = true;
71    }
72}
73
74$pagetitle = $rssCat = $catTitle = '';
75if ($user) {
76    if (is_int($user)) {
77        $userid = intval($user);
78    } else {
79        if (!($userinfo = $userservice->getUserByUsername($user))) {
80            $tplVars['error'] = sprintf(T_('User with username %s was not found'), $user);
81            $tplVars['sidebar_blocks'][] = 'profile';
82            $templateservice->loadTemplate('error.404.tpl', $tplVars);
83            exit();
84        } else {
85            $userid =& $userinfo['uId'];
86        }
87    }
88    $pagetitle .= ': '. $user;
89}
90if ($cat) {
91    $catTitle = ': '. str_replace('+', ' + ', $cat);
92    $pagetitle .= $catTitle;
93}
94
95$pagetitle = substr($pagetitle, 2);
96
97// Header variables
98$tplVars['loadjs'] = true;
99$tplVars['bLoadJqueryPhp'] = true;
100
101
102// ADD A BOOKMARK
103$tplVars['sidebar_blocks'] = array('profile','recent-tags','partners');
104$saved = false;
105$templatename = 'bookmarks.tpl';
106if ($loggedon && isset($_POST['submitted'])) {
107
108  require_once(dirname(__file__) . "/includes/recaptchalib.php");
109  require_once(dirname(__file__) . "/../common/recaptcha.php");
110  $privatekey = $recaptcha_private_key;
111  $resp = recaptcha_check_answer ($privatekey,
112                                  $_SERVER["REMOTE_ADDR"],
113                                  $_POST["recaptcha_challenge_field"],
114                                  $_POST["recaptcha_response_field"]);
115 
116  if (!$resp->is_valid) {
117     $tplVars['error'] = "The reCAPTCHA wasn't entered correctly. Go back and try it again.";
118     $templatename = 'editbookmark.tpl';
119     $saved = false;
120  }  else
121
122    if (!$_POST['title'] || !$_POST['address']) {
123        $tplVars['error'] = T_('Your bookmark must have a title and an address');
124        $templatename = 'editbookmark.tpl';
125    } else {
126        $address = trim($_POST['address']);
127        // If the bookmark exists already, edit the original
128        if ($bookmarkservice->bookmarkExists($address, $currentUserID)) {
129            $bookmark =& $bookmarkservice->getBookmarkByAddress($address);
130            header('Location: '. createURL('edit', $bookmark['bId']));
131            exit();
132        // If it's new, save it
133        } else {
134            $title = trim($_POST['title']);
135            $description = trim($_POST['description']);
136            $status = intval($_POST['status']);
137            $categories = trim($_POST['tags']);
138            $saved = true;
139            if ($bookmarkservice->addBookmark($address, $title, $description, $status, $categories)) {
140                if (isset($_POST['popup'])) {
141                    $tplVars['msg'] = '<script type="text/javascript">window.close();</script>';
142                } else {
143                    $tplVars['msg'] = T_('Bookmark saved');
144                    // Redirection option
145                    if ($GLOBALS['useredir']) {
146                        $address = $GLOBALS['url_redir'] . $address;
147                    }
148                                        //***Changed default location (bookmark website) to user homepage
149                                        if ($defaultredirect == 'userhomepage') {
150                                                //redirec to user homepage after add bookmark
151                                                header('Location: '.createURL('bookmarks', $currentUsername));
152                                        } else {
153                                                //default to bookmark address
154                                                header('Location: '. $address);
155                                        }
156
157                }
158            } else {
159                $tplVars['error'] = T_('There was an error saving your bookmark. Please try again or contact the administrator.');
160                $templatename = 'editbookmark.tpl';
161                $saved = false;
162            }
163        }
164    }
165}
166
167if (isset($_GET['action']) && ($_GET['action'] == "add")) {
168    // If the bookmark exists already, edit the original
169    if ($bookmarkservice->bookmarkExists(stripslashes($_GET['address']), $currentUserID)) {
170        $bookmark =& $bookmarkservice->getBookmarkByAddress(stripslashes($_GET['address']));
171        $popup = (isset($_GET['popup'])) ? '?popup=1' : '';
172        header('Location: '. createURL('edit', $bookmark['bId'] . $popup));
173        exit();
174    }
175    $templatename = 'editbookmark.tpl';
176}
177 
178if ($templatename == 'editbookmark.tpl') {
179    if ($loggedon) {
180        $tplVars['formaction']  = createURL('bookmarks', $currentUsername);
181        if (isset($_POST['submitted'])) {
182            $tplVars['row'] = array(
183                'bTitle' => stripslashes($_POST['title']),
184                'bAddress' => stripslashes($_POST['address']),
185                'bDescription' => stripslashes($_POST['description']),
186                'tags' => ($_POST['tags'] ? explode(';', stripslashes($_POST['tags'])) : array())
187            );
188            $tplVars['tags'] = $_POST['tags'];
189        } else {
190            $tplVars['row'] = array(
191                'bTitle' => stripslashes($_GET['title']),
192                'bAddress' => stripslashes($_GET['address']),
193                'bDescription' => stripslashes($_GET['description']),
194                'tags' => ($_GET['tags'] ? explode(';', stripslashes($_GET['tags'])) : array())
195            );
196        }
197        $title = T_('Add a Bookmark');
198        $tplVars['pagetitle'] = $title;
199        $tplVars['subtitle'] = $title;
200        $tplVars['btnsubmit'] = T_('Add Bookmark');
201        $tplVars['popup'] = (isset($_GET['popup'])) ? $_GET['popup'] : null;
202    } else {
203        $tplVars['error'] = T_('You must be logged in before you can add bookmarks.');
204    }
205} else if ($user && !isset($_GET['popup'])) {
206       
207    $tplVars['sidebar_blocks'] = array('profile', 'watchstatus');
208
209    if (!$cat) {
210        $cat = NULL;
211        $tplVars['currenttag'] = NULL;
212    } else {
213        $rssCat = '/'. filter($cat, 'url');
214        $tplVars['currenttag'] = $cat;
215        $tplVars['sidebar_blocks'][] = 'related';
216        $tplVars['sidebar_blocks'][] = 'tagactions';
217    }
218    $tplVars['popCount'] = 30;
219    $tplVars['sidebar_blocks'][] = 'popular';
220    $tplVars['sidebar_blocks'][] = 'partners';
221   
222    $tplVars['userid'] = $userid;
223    $tplVars['userinfo'] =& $userinfo;
224    $tplVars['user'] = $user;
225    $tplVars['range'] = 'user';
226   
227    // Pagination
228    $perpage = getPerPageCount();
229    if (isset($_GET['page']) && intval($_GET['page']) > 1) {
230        $page = $_GET['page'];
231        $start = ($page - 1) * $perpage;
232    } else {
233        $page = 0;
234        $start = 0;
235    }
236   
237    // Set template vars
238    $tplVars['rsschannels'] = array(
239        array(filter($sitename .': '. $pagetitle), createURL('rss', filter($user, 'url') . $rssCat))
240    );
241
242    $tplVars['page'] = $page;
243    $tplVars['start'] = $start;
244    $tplVars['bookmarkCount'] = $start + 1;
245   
246    $bookmarks =& $bookmarkservice->getBookmarks($start, $perpage, $userid, $cat, $terms, getSortOrder());
247    $tplVars['total'] = $bookmarks['total'];
248    $tplVars['bookmarks'] =& $bookmarks['bookmarks'];
249    $tplVars['cat_url'] = createURL('bookmarks', '%s/%s');
250    $tplVars['nav_url'] = createURL('bookmarks', '%s/%s%s');
251    if ($user == $currentUsername) {
252        $title = T_('My Bookmarks') . filter($catTitle);
253    } else {
254        $title = filter($pagetitle);
255    }
256   
257        if( $userservice->isAdmin($currentUserID) && $user != $currentUsername ){
258            $sAdminLinks  = ' <a href="#" '
259                              . 'onclick="javascript:if( confirm(\'Delete the user \\\'' . $user . '\\\',\nall their bookmarks, comments and ratings?\nThis can\\\'t be undone. Are you sure?\')){'
260                                          .                        ' $.php({ \'act\' : \'user-delete\''
261                                      .                               ', \'uid\' : \'' . $userid . '\''
262                                          .                               ' }); '
263                                          .                        'setTimeout(\'self.location.reload(true)\',3000);'
264                              .                      '} "'
265                              . 'style="text-decoration:none;">'
266                              . '<img src="' . RR_COMMON_SKIN_DIRECTORY . '/images/actions/delete-spam.png" '
267                              . 'style="position:relative;top:3px;border:none;" '
268                              . 'alt="Delete this user from omBookmarks" '
269                              . '></a>';
270        }else{
271                $sAdminLinks = '';
272        }
273   
274        $tplVars['pagetitle'] = $title;
275    $tplVars['subtitle'] = $title;
276    $tplVars['aAdmin']   = array( 'title_links' => $sAdminLinks );
277   
278        $tplVars['show_sort_controls'] = true;
279}
280
281$templateservice->loadTemplate($templatename, $tplVars);
282
283if ($usecache && $endcache) {
284    // Cache output if existing copy has expired
285    $cacheservice->End($hash);
286}
287?>
Note: See TracBrowser for help on using the repository browser.