-
Notifications
You must be signed in to change notification settings - Fork 2
/
FeedReader.php
386 lines (308 loc) · 11.7 KB
/
FeedReader.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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
<?php
// Copyright (c) 2007 - Marcus Lunzenauer <[email protected]>
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
require_once 'models/feed.php';
/**
* @author mlunzena
* @copyright (c) Authors
*/
class FeedReader extends StudipPlugin implements HomepagePlugin, PortalPlugin
{
public $factory;
public function __construct()
{
parent::__construct();
if (Navigation::hasItem('/profile') && $this->is_authorized() && $this->isActivated(null, 'user')) {
Navigation::addItem('/profile/feed_reader',
new AutoNavigation('Feed Reader',
PluginEngine::getUrl($this, null, '')));
}
$this->factory = new Flexi_TemplateFactory(dirname(__FILE__).'/templates');
PageLayout::addStylesheet($this->getPluginUrl().'/css/style.css');
PageLayout::addScript($this->getPluginUrl().'/js/feedreader.js');
}
public function is_authorized()
{
$name = Request::quoted('username');
$id = $name ? get_userid($name) : $GLOBALS['auth']->auth['uid'];
return $id === $GLOBALS['auth']->auth['uid'];
}
public function has_to_be_authorized()
{
if (!$this->is_authorized()) {
throw new Exception('Access denied.');
}
}
public function showList($message = '')
{
return $this->factory->render('show',
array(
'feeds' => FeedReader_Feed::find_all($GLOBALS['auth']->auth['uid']),
'message' => $message,
'plugin' => $this,
),
$GLOBALS['template_factory']->open('layouts/base')
);
}
public function show_action()
{
echo $this->showList();
}
public function insert_action()
{
$this->has_to_be_authorized();
Navigation::activateItem('/profile/feed_reader');
$message = '';
if (isset($_REQUEST['url']) && '' !== $_REQUEST['url']) {
$feed = new FeedReader_Feed();
$feed->user_id = $GLOBALS['auth']->auth['uid'];
$feed->url = $_REQUEST['url'];
$pie = @$this->get_simplepie_from_user_feed($feed);
$error = $pie->error();
if ($error) {
$message = 'Newsfeed konnte nicht abonniert werden. ('.$error.')';
} else {
$message = $feed->save()
? 'Newsfeed abonniert.' : 'Newsfeed konnte nicht abonniert werden.';
}
}
echo $this->showList($message);
}
public function edit_action()
{
$this->has_to_be_authorized();
Navigation::activateItem('/profile/feed_reader');
if (!isset($_REQUEST['feed_id']) || '' === $_REQUEST['feed_id']) {
echo $this->showList();
return;
}
$plugin = $this;
$id = (int) $_REQUEST['feed_id'];
$feed = FeedReader_Feed::find($id);
if (is_null($feed) || $feed->user_id !== $GLOBALS['auth']->auth['uid']) {
echo $this->showList('No such feed');
return;
}
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$feed->url = $_POST['url'];
$pie = $this->get_simplepie_from_user_feed($feed);
$error = $pie->error();
if ($error) {
$message = $error;
} else {
$message = $feed->save()
? 'Newsfeed wurde geändert.'
: 'Newsfeed konnte nicht geändert werden.';
}
echo $this->showList($message);
return;
} else {
echo $this->factory->render('edit',
compact('feed', 'plugin'),
$GLOBALS['template_factory']->open('layouts/base'));
}
}
public function delete_action()
{
$this->has_to_be_authorized();
if (isset($_REQUEST['feed_id']) && '' !== $_REQUEST['feed_id']) {
$feed = FeedReader_Feed::find($_REQUEST['feed_id']);
}
// AJAX
if (isset($_SERVER['HTTP_X_REQUESTED_WITH'])) {
ob_end_clean();
header('Content-Type: text/javascript');
if (is_null($feed)) {
header('HTTP/1.1 404 Not found', true, 404);
} else {
echo $this->factory->render('delete',
array(
'plugin' => $this,
'feed' => $feed,
'success' => $feed->delete(), ),
null);
}
ob_start(create_function('', 'return "";'));
}
// NON AJAX
else {
Navigation::activateItem('/profile/feed_reader');
if (is_null($feed)) {
$message = 'Newsfeed existiert nicht.';
} elseif ($feed->delete()) {
$message = 'Newsfeed wurde gelöscht.';
} else {
$message = 'Newsfeed konnte nicht gelöscht werden.';
}
echo $this->showList($message);
}
}
public function sort_action()
{
if (!$this->is_authorized()) {
header('HTTP/1.1 403 Forbidden', true, 403);
exit;
}
if (!FeedReader_Feed::sort($GLOBALS['auth']->auth['uid'],
$_REQUEST['feeds'])) {
header('HTTP/1.1 404 Not found', true, 404);
var_dump($this->error);
exit;
}
}
public function up_action()
{
$this->has_to_be_authorized();
if (!isset($_POST['feed_id']) || '' === $_POST['feed_id']) {
echo $this->showList();
return;
}
$feed_id = (int) $_POST['feed_id'];
$message = '';
try {
FeedReader_Feed::sort_up($GLOBALS['auth']->auth['uid'], $feed_id);
} catch (Exception $e) {
$message = $e->getMessage();
}
echo $this->showList($message);
}
public function down_action()
{
$this->has_to_be_authorized();
if (!isset($_POST['feed_id']) || '' === $_POST['feed_id']) {
echo $this->showList();
return;
}
$feed_id = (int) $_POST['feed_id'];
$message = '';
try {
FeedReader_Feed::sort_down($GLOBALS['auth']->auth['uid'], $feed_id);
} catch (Exception $e) {
$message = $e->getMessage();
}
echo $this->showList($message);
}
public function visibility_action()
{
$this->has_to_be_authorized();
Navigation::activateItem('/profile/feed_reader');
if (!isset($_REQUEST['feed_id']) || '' === $_REQUEST['feed_id']) {
echo $this->showList();
return;
}
$id = (int) $_REQUEST['feed_id'];
$feed = FeedReader_Feed::find($id);
if (is_null($feed) || $feed->user_id !== $GLOBALS['auth']->auth['uid']) {
echo $this->showList('No such feed');
return;
}
$feed->visibility = $feed->visibility ? false : true;
$message = $feed->save()
? 'Sichtbarkeit des Newsfeeds wurde geändert.'
: 'Sichtbarkeit des Newsfeeds konnte nicht geändert werden.';
echo $this->showList($message);
return;
}
public function get_simplepie_from_user_feed($user_feed)
{
if (!class_exists('SimplePie')) {
require_once dirname(__FILE__).'/vendor/simplepie.inc';
}
$feed = new SimplePie($user_feed->url, $GLOBALS['TMP_PATH']);
$feed->set_output_encoding('UTF-8');
$feed->init();
$feed->id = $user_feed->id;
return $feed;
}
public function shortdesc($string, $length)
{
if (strlen($string) <= $length) {
return $string;
}
$short_desc = trim(str_replace(array("\r", "\n", "\t"), ' ',
strip_tags($string)));
$desc = trim(substr($short_desc, 0, $length));
return $desc.(in_array(substr($desc, -1, 1), array('.', '!', '?'))
? '' : '...');
}
/**
* Return a template (an instance of the Flexi_Template class)
* to be rendered on the given user's home page. Return NULL to
* render nothing for this plugin.
*
* The template will automatically get a standard layout, which
* can be configured via attributes set on the template:
*
* title title to display, defaults to plugin name
* icon_url icon for this plugin (if any)
* admin_url admin link for this plugin (if any)
* admin_title title for admin link (default: Administration)
*
* @return object template object to render or NULL
*/
public function getHomepageTemplate($user_id)
{
$feeds = $this->getFeeds($user_id);
return (sizeof($feeds) || $this->is_authorized())
? $this->getFeedsTemplate($feeds)
: null;
}
/**
* Return a template (an instance of the Flexi_Template class)
* to be rendered on the start or portal page. Return NULL to
* render nothing for this plugin.
*
* The template will automatically get a standard layout, which
* can be configured via attributes set on the template:
*
* title title to display, defaults to plugin name
* icon_url icon for this plugin (if any)
* admin_url admin link for this plugin (if any)
* admin_title title for admin link (default: Administration)
*
* @return object template object to render or NULL
*/
public function getPortalTemplate()
{
$feeds = $this->getFeeds($GLOBALS['auth']->auth['uid']);
return (sizeof($feeds) || $this->is_authorized())
? $this->getFeedsTemplate($feeds)
: null;
return $this->getFeedsTemplate($GLOBALS['auth']->auth['uid']);
}
public function getFeeds($user_id)
{
$feeds = array();
foreach (FeedReader_Feed::find_all($user_id) as $f) {
if ($f->is_visible($GLOBALS['auth']->auth['uid'])) {
$feeds[] = self::get_simplepie_from_user_feed($f);
}
}
return $feeds;
}
public function getFeedsTemplate($feeds)
{
$limit = 5;
$plugin = $this;
$tmpl = $this->factory->open('overview/feeds');
$tmpl->set_attributes(compact('feeds', 'limit', 'plugin'));
return $tmpl;
}
}