Merge branch 'long-descriptions'
[scuttle] / rss.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 $cacheservice    =& ServiceFactory::getServiceInstance('CacheService');
25 $templateservice =& ServiceFactory::getServiceInstance('TemplateService');
26 $userservice     =& ServiceFactory::getServiceInstance('UserService');
27
28 $tplVars = array();
29 header('Content-Type: application/xml');
30 list($url, $user, $cat) = explode('/', $_SERVER['PATH_INFO']);
31
32 if ($usecache) {
33   // Generate hash for caching on
34   $hashtext = $_SERVER['REQUEST_URI'];
35   if ($userservice->isLoggedOn()) {
36     $hashtext .= $userservice->getCurrentUserID();
37     $currentUser = $userservice->getCurrentUser();
38     $currentUsername = $currentUser[$userservice->getFieldName('username')];
39     if ($currentUsername == $user) {
40       $hashtext .= $user;
41     }
42   }
43   $hash = md5($hashtext);
44
45   // Cache for an hour
46   $cacheservice->Start($hash, 3600);
47 }
48
49 $watchlist = NULL;
50 if ($user && $user != 'all') {
51   if ($user == 'watchlist') {
52     $user = $cat;
53     $cat = NULL;
54     $watchlist = TRUE;
55   }
56   if (is_int($user)) {
57     $userid = intval($user);
58   } else {
59     if ($userinfo = $userservice->getUserByUsername($user)) {
60       $userid =& $userinfo[$userservice->getFieldName('primary')];
61     } else {
62       $tplVars['error'] = sprintf(T_('User with username %s was not found'), $user);
63       $templateservice->loadTemplate('error.404.tpl', $tplVars);
64       //throw a 404 error
65       exit();
66     }
67   }
68   $pagetitle .= ": ". $user;
69 } else {
70   $userid = NULL;
71 }
72
73 if ($cat) {
74   $pagetitle .= ": ". str_replace('+', ' + ', $cat);
75 }
76
77 $tplVars['feedtitle'] = filter($GLOBALS['sitename'] . (isset($pagetitle) ? $pagetitle : ''));
78 $tplVars['feedlink'] = $GLOBALS['root'];
79 $tplVars['feeddescription'] = sprintf(T_('Recent bookmarks posted to %s'), $GLOBALS['sitename']);
80
81 $bookmarks     =& $bookmarkservice->getBookmarks(0, 15, $userid, $cat, NULL, getSortOrder(), $watchlist);
82 $bookmarks_tmp =& filter($bookmarks['bookmarks']);
83
84 $bookmarks_tpl = array();
85 foreach(array_keys($bookmarks_tmp) as $key) {
86   $row =& $bookmarks_tmp[$key];
87
88   $_link = $row['bAddress'];
89   // Redirection option
90   if ($GLOBALS['useredir']) {
91     $_link = $GLOBALS['url_redir'] . $_link;
92   }
93   $_pubdate = date("r", strtotime($row['bDatetime']));
94
95   $uriparts  = explode('.', $_link);
96   $extension = end($uriparts);
97   unset($uriparts);
98
99   $enclosure = array();
100   if ($keys = multi_array_search($extension, $GLOBALS['filetypes'])) {
101     $enclosure['mime']   = file_get_mimetype($_link);
102     $enclosure['length'] = file_get_filesize($_link);
103   }
104
105   $bookmarks_tpl[] = array(
106     'title'            => $row['bTitle'],
107     'link'             => $_link,
108     'description'      => $row['bDescription'],
109     'creator'          => $row['username'],
110     'pubdate'          => $_pubdate,
111     'tags'             => $row['tags'],
112     'enclosure_mime'   => $enclosure['mime'],
113     'enclosure_length' => $enclosure['length']
114   );
115 }
116 unset($bookmarks_tmp);
117 unset($bookmarks);
118 $tplVars['bookmarks'] =& $bookmarks_tpl;
119
120 $templateservice->loadTemplate('rss.tpl', $tplVars);
121
122 if ($usecache) {
123   // Cache output if existing copy has expired
124   $cacheservice->End($hash);
125 }

Benjamin Mako Hill || Want to submit a patch?