-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathfeed.php
225 lines (214 loc) · 8.98 KB
/
feed.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* This page generates blog RSS and ATOM feeds
*
* @author Matt Clarkson <[email protected]>
* @package oublog
*/
require_once("../../config.php");
require_once("locallib.php");
require_once($CFG->libdir.'/rsslib.php');
require_once('atomlib.php');
$format = required_param('format', PARAM_TEXT);
$blogid = optional_param('blog', 0, PARAM_INT);
$bloginstancesid = optional_param('bloginstance', 0, PARAM_INT);
$comments = optional_param('comments', 0, PARAM_INT);
$postid = optional_param('post', 0, PARAM_INT);
$loggedin = optional_param('loggedin', '', PARAM_TEXT);
$full = optional_param('full', '', PARAM_TEXT);
$viewer = optional_param('viewer', 0, PARAM_INT);
$groupid = optional_param('group', 0, PARAM_INT);
$individualid = optional_param('individual', 0, PARAM_INT);
$childblogid = optional_param('childblog', 0, PARAM_INT);
$url = new moodle_url('/mod/oublog/feed.php', array('format'=>$format, 'blog'=>$blogid,
'bloginstance'=>$bloginstancesid, 'post'=>$postid));
$PAGE->set_url($url);
$PAGE->set_context(context_system::instance());
// Validate Parameters.
$format = strtolower($format);
if (empty($CFG->enablerssfeeds)) {
throw new moodle_exception('feedsnotenabled', 'oublog');
}
if ($format != 'atom' && $format != 'rss') {
throw new moodle_exception('invalidformat', 'oublog');
}
if (!$blogid && !$bloginstancesid && !$postid) {
throw new moodle_exception('missingrequiredfield');
}
if (($loggedin || $full) && !$viewer) {
throw new moodle_exception('missingrequiredfield');
}
if ($groupid && !$viewer) {
throw new moodle_exception('missingrequiredfield');
}
if (isset($bloginstancesid) && $bloginstancesid!='all') {
$bloginstance = $DB->get_record('oublog_instances', array('id'=>$bloginstancesid));
$blog = $DB->get_record('oublog', array('id'=>$bloginstance->oublogid));
} else if ($blogid) {
$blog = $DB->get_record('oublog', array('id'=>$blogid));
} else if ($postid) {
$post = $DB->get_record('oublog_posts', array('id'=>$postid));
$bloginstance = $DB->get_record('oublog_instances', array('id'=>$post->oubloginstancesid));
$blog = $DB->get_record('oublog', array('id'=>$bloginstance->oublogid));
}
if (!isset($blog->id) || !$cm = get_coursemodule_from_instance('oublog', $blog->id)) {
if (file_exists($CFG->dirroot . '/local/error/http-error.php')) {
redirect(new moodle_url('/local/error/http-error.php', ['test' => '404']));
} else {
throw new moodle_exception('invalidcoursemodule');
}
}
// Get child blog.
if (!empty($childblogid)) {
$childblog = $DB->get_record('oublog', array('id' => $childblogid), '*', MUST_EXIST);
// Get cm of child blog.
if (!$cmchildblog = get_coursemodule_from_instance('oublog', $childblog->id)) {
throw new moodle_exception('invalidcoursemodule');
}
}
$feedcm = !empty($cmchildblog) ? $cmchildblog : $cm;
$feedblog = !empty($childblog) ? $childblog : $blog;
// Work out link for ordinary web page equivalent to requested feed
if ($feedblog->global) {
if ($bloginstancesid == 'all') {
$url = $CFG->wwwroot . '/mod/oublog/allposts.php';
} else {
$url = $CFG->wwwroot . '/mod/oublog/view.php?user=' .
$bloginstance->userid;
}
$groupmode = 0;
} else {
$url = $CFG->wwwroot . '/mod/oublog/view.php?id=' . $cm->id .
($groupid ? '&group=' . $groupid : '') .
($individualid ? '&individual=' . $individualid : '');
if (!($course = $DB->get_record('course', array('id'=>$feedcm->course)))) {
throw new moodle_exception('coursemisconf');
}
$groupmode = oublog_get_activity_groupmode($feedcm, $course);
}
// Check browser compatibility.
if (core_useragent::check_browser_version('MSIE', 0) || core_useragent::check_browser_version('Firefox', 0)) {
if (!core_useragent::check_browser_version('MSIE', '7') && !core_useragent::check_browser_version('Firefox', '2')) {
if ($feedblog->global) {
$url='view.php?user='.$bloginstance->userid;
} else {
$url='view.php?id='.$cm->id.($groupid ? '&group='.$groupid : '');
}
throw new moodle_exception('unsupportedbrowser', 'oublog', $url);
}
}
// Determine if feed has changed since the if-modified-since HTTP header and exit if it hasn't.
// Override default Moodle behaviour which prevents all caching (ouch).
header('Cache-Control:');
header('Pragma: ');
header('Expires: ');
if ($mtime = oublog_feed_last_changed($blogid, $bloginstancesid, $postid, $comments)) {
$mtimegm = gmdate('D, d M Y H:i:s', $mtime) . ' GMT';
header("Last-Modified: $mtimegm");
}
if ($mtime && isset($_SERVER['HTTP_IF_MODIFIED_SINCE'])) {
$iftime = strtotime(preg_replace('/;.*$/', '',
$_SERVER['HTTP_IF_MODIFIED_SINCE']));
if ($mtime <= $iftime) {
header("HTTP/1.0 304 Not Modified");
exit;
}
}
if ($feedblog->global && $bloginstancesid != 'all') {
$accesstoken = $bloginstance->accesstoken;
} else {
$accesstoken = $feedblog->accesstoken;
}
if ($full) {
// We had an issue where the system leaked 'full' view tokens to users
// who should not get them. To resolve this, I changed the view tokens
// to use 'v2' in the hash
if ($full == md5($accesstoken.$viewer.OUBLOG_VISIBILITY_COURSEUSER.'v2') && $user =
$DB->get_record('user', array('id'=>$viewer))) {
$allowedvisibility = OUBLOG_VISIBILITY_COURSEUSER;
} else if ($full == md5($accesstoken.$viewer.OUBLOG_VISIBILITY_COURSEUSER) && $user =
$DB->get_record('user', array('id'=>$viewer))) {
// This is the old token. Ooops. We know that at least users were
// logged in, so they get that version...
$allowedvisibility = OUBLOG_VISIBILITY_LOGGEDINUSER;
if (!$feedblog->global) {
// For course blogs, security was actually correct, so let's
// keep allowing them to read the whole blog
$allowedvisibility = OUBLOG_VISIBILITY_COURSEUSER;
}
} else {
throw new moodle_exception('nopermissiontoshow');
}
} else if ($loggedin) {
if ($loggedin == md5($accesstoken.$viewer.OUBLOG_VISIBILITY_LOGGEDINUSER) && $user =
$DB->get_record('user', array('id'=>$viewer))) {
$allowedvisibility = OUBLOG_VISIBILITY_LOGGEDINUSER;
} else {
throw new moodle_exception('nopermissiontoshow');
}
} else {
$allowedvisibility = OUBLOG_VISIBILITY_PUBLIC;
$user = $USER;
}
// Check individual
if ($feedblog->individual) {
if (!oublog_individual_has_permissions($feedcm, $feedblog, $groupid, $individualid, $user->id)) {
throw new moodle_exception('nopermissiontoshow');
}
}
// Check separate groups
if ($groupmode == SEPARATEGROUPS) {
// If you are a member of the group and viewing a specified group then
// you are OK
if ($groupid && groups_is_member($groupid, $user->id)) {
// Group members are OK
} else {
// Must have access all groups
require_capability('moodle/site:accessallgroups',
context_module::instance($feedcm->id), $user->id);
}
}
// Get data for feed in a standard form.
if ($comments) {
$feeddata = oublog_get_feed_comments($blogid, $bloginstancesid, $postid, $user,
$allowedvisibility, $groupid, $feedcm, $blog, $individualid);
$feedname = strip_tags($feedblog->name) . ': ' . get_string('commentsfeed', 'oublog');
$feedsummary='';
} else {
$feeddata = oublog_get_feed_posts($blogid,
isset($bloginstance) ? $bloginstance : null, $user,
$allowedvisibility, $groupid, $feedcm, $blog, $individualid);
$feedname=strip_tags($feedblog->name);
if ($bloginstancesid=='all') {
$feedsummary=strip_tags($feedblog->intro);
} else {
$feedsummary=strip_tags($bloginstance->summary);
}
}
// Generate feed in RSS or ATOM format.
if ($format == 'rss') {
header('Content-type: application/rss+xml');
echo rss_standard_header($feedname, $url, $feedsummary);
echo rss_add_items($feeddata);
echo rss_standard_footer();
} else {
header('Content-type: application/atom+xml');
$updated=count($feeddata)==0 ? time() : reset($feeddata)->pubdate;
echo atom_standard_header($FULLME, $FULLME, $updated, $feedname, $feedsummary);
echo atom_add_items($feeddata);
echo atom_standard_footer();
}