]> git.wh0rd.org - tt-rss.git/blob - backend.php
implement pubsubhubbub subscriber (update schema)
[tt-rss.git] / backend.php
1 <?php
2 /* remove ill effects of magic quotes */
3
4 if (get_magic_quotes_gpc()) {
5 function stripslashes_deep($value) {
6 $value = is_array($value) ?
7 array_map('stripslashes_deep', $value) : stripslashes($value);
8 return $value;
9 }
10
11 $_POST = array_map('stripslashes_deep', $_POST);
12 $_GET = array_map('stripslashes_deep', $_GET);
13 $_COOKIE = array_map('stripslashes_deep', $_COOKIE);
14 $_REQUEST = array_map('stripslashes_deep', $_REQUEST);
15 }
16
17 require_once "functions.php";
18 require_once "sessions.php";
19 require_once "modules/backend-rpc.php";
20 require_once "sanity_check.php";
21 require_once "config.php";
22 require_once "db.php";
23 require_once "db-prefs.php";
24
25 no_cache_incantation();
26
27 startup_gettext();
28
29 $script_started = getmicrotime();
30
31 $link = db_connect(DB_HOST, DB_USER, DB_PASS, DB_NAME);
32
33 if (!$link) {
34 if (DB_TYPE == "mysql") {
35 print mysql_error();
36 }
37 // PG seems to display its own errors just fine by default.
38 return;
39 }
40
41 init_connection($link);
42
43 $op = $_REQUEST["op"];
44 $subop = $_REQUEST["subop"];
45 $mode = $_REQUEST["mode"];
46
47 $print_exec_time = false;
48
49 if ((!$op || $op == "rss" || $op == "dlg") && !$_REQUEST["noxml"]) {
50 header("Content-Type: application/xml; charset=utf-8");
51 } else {
52 header("Content-Type: text/plain; charset=utf-8");
53 }
54
55 if (ENABLE_GZIP_OUTPUT) {
56 ob_start("ob_gzhandler");
57 }
58
59 if (SINGLE_USER_MODE) {
60 authenticate_user($link, "admin", null);
61 }
62
63 if (!($_SESSION["uid"] && validate_session($link)) && $op != "globalUpdateFeeds" &&
64 $op != "rss" && $op != "getUnread" && $op != "getProfiles" &&
65 $op != "pubsub") {
66
67 header("Content-Type: text/plain");
68 print json_encode(array("error" => array("code" => 6)));
69 return;
70 }
71
72 $purge_intervals = array(
73 0 => __("Use default"),
74 -1 => __("Never purge"),
75 5 => __("1 week old"),
76 14 => __("2 weeks old"),
77 31 => __("1 month old"),
78 60 => __("2 months old"),
79 90 => __("3 months old"));
80
81 $update_intervals = array(
82 0 => __("Default interval"),
83 -1 => __("Disable updates"),
84 15 => __("Each 15 minutes"),
85 30 => __("Each 30 minutes"),
86 60 => __("Hourly"),
87 240 => __("Each 4 hours"),
88 720 => __("Each 12 hours"),
89 1440 => __("Daily"),
90 10080 => __("Weekly"));
91
92 $update_intervals_nodefault = array(
93 -1 => __("Disable updates"),
94 15 => __("Each 15 minutes"),
95 30 => __("Each 30 minutes"),
96 60 => __("Hourly"),
97 240 => __("Each 4 hours"),
98 720 => __("Each 12 hours"),
99 1440 => __("Daily"),
100 10080 => __("Weekly"));
101
102 $update_methods = array(
103 0 => __("Default"),
104 1 => __("Magpie"),
105 2 => __("SimplePie"),
106 3 => __("Twitter OAuth"));
107
108 if (DEFAULT_UPDATE_METHOD == "1") {
109 $update_methods[0] .= ' (SimplePie)';
110 } else {
111 $update_methods[0] .= ' (Magpie)';
112 }
113
114 $access_level_names = array(
115 0 => __("User"),
116 5 => __("Power User"),
117 10 => __("Administrator"));
118
119 require_once "modules/pref-prefs.php";
120 require_once "modules/popup-dialog.php";
121 require_once "modules/help.php";
122 require_once "modules/pref-feeds.php";
123 require_once "modules/pref-filters.php";
124 require_once "modules/pref-labels.php";
125 require_once "modules/pref-users.php";
126
127 $error = sanity_check($link);
128
129 if ($error['code'] != 0) {
130 print json_encode(array("error" => $error));
131 return;
132 }
133
134 switch($op) { // Select action according to $op value.
135 case "rpc":
136 // Handle remote procedure calls.
137 handle_rpc_request($link);
138 break; // rpc
139
140 case "feeds":
141 $subop = $_REQUEST["subop"];
142 $root = (bool)$_REQUEST["root"];
143
144 switch($subop) {
145 case "catchupAll":
146 db_query($link, "UPDATE ttrss_user_entries SET
147 last_read = NOW(),unread = false WHERE owner_uid = " . $_SESSION["uid"]);
148 ccache_zero_all($link, $_SESSION["uid"]);
149
150 break;
151
152 case "collapse":
153 $cat_id = db_escape_string($_REQUEST["cid"]);
154 $mode = (int) db_escape_string($_REQUEST['mode']);
155 toggle_collapse_cat($link, $cat_id, $mode);
156 return;
157 break;
158 }
159
160 if (!$root) {
161 print json_encode(outputFeedList($link));
162 } else {
163
164 $feeds = outputFeedList($link, false);
165
166 $root = array();
167 $root['id'] = 'root';
168 $root['name'] = __('Feeds');
169 $root['items'] = $feeds['items'];
170
171 $fl = array();
172 $fl['identifier'] = 'id';
173 $fl['label'] = 'name';
174 $fl['items'] = array($root);
175
176 print json_encode($fl);
177 }
178
179 break; // feeds
180
181 case "la":
182 $id = db_escape_string($_REQUEST['id']);
183
184 $result = db_query($link, "SELECT link FROM ttrss_entries, ttrss_user_entries
185 WHERE id = '$id' AND id = ref_id AND owner_uid = '".$_SESSION['uid']."'");
186
187 if (db_num_rows($result) == 1) {
188 $article_url = db_fetch_result($result, 0, 'link');
189 $article_url = str_replace("\n", "", $article_url);
190
191 header("Location: $article_url");
192 return;
193
194 } else {
195 print_error(__("Article not found."));
196 }
197 break;
198
199 case "view":
200
201 $id = db_escape_string($_REQUEST["id"]);
202 $cids = split(",", db_escape_string($_REQUEST["cids"]));
203 $mode = db_escape_string($_REQUEST["mode"]);
204 $omode = db_escape_string($_REQUEST["omode"]);
205
206 // in prefetch mode we only output requested cids, main article
207 // just gets marked as read (it already exists in client cache)
208
209 $articles = array();
210
211 if ($mode == "") {
212 array_push($articles, format_article($link, $id, false));
213 } else if ($mode == "zoom") {
214 array_push($articles, format_article($link, $id, false, true, true));
215 } else {
216 catchupArticleById($link, $id, 0);
217 }
218
219 if (!$_SESSION["bw_limit"]) {
220 foreach ($cids as $cid) {
221 if ($cid) {
222 array_push($articles, format_article($link, $cid, false, false));
223 }
224 }
225 }
226
227 print json_encode($articles);
228
229 break; // view
230
231 case "viewfeed":
232
233 $timing_info = getmicrotime();
234
235 $reply = array();
236
237 if ($_REQUEST["debug"]) $timing_info = print_checkpoint("0", $timing_info);
238
239 $omode = db_escape_string($_REQUEST["omode"]);
240
241 $feed = db_escape_string($_REQUEST["feed"]);
242 $subop = db_escape_string($_REQUEST["subop"]);
243 $view_mode = db_escape_string($_REQUEST["view_mode"]);
244 $limit = (int) get_pref($link, "DEFAULT_ARTICLE_LIMIT");
245 @$cat_view = db_escape_string($_REQUEST["cat"]);
246 @$next_unread_feed = db_escape_string($_REQUEST["nuf"]);
247 @$offset = db_escape_string($_REQUEST["skip"]);
248 @$vgroup_last_feed = db_escape_string($_REQUEST["vgrlf"]);
249 $order_by = db_escape_string($_REQUEST["order_by"]);
250
251 /* Feed -5 is a special case: it is used to display auxiliary information
252 * when there's nothing to load - e.g. no stuff in fresh feed */
253
254 if ($feed == -5) {
255 print json_encode(generate_dashboard_feed($link));
256 return;
257 }
258
259 $result = false;
260
261 if ($feed < -10) {
262 $label_feed = -11-$feed;
263 $result = db_query($link, "SELECT id FROM ttrss_labels2 WHERE
264 id = '$label_feed' AND owner_uid = " . $_SESSION['uid']);
265 } else if (!$cat_view && $feed > 0) {
266 $result = db_query($link, "SELECT id FROM ttrss_feeds WHERE
267 id = '$feed' AND owner_uid = " . $_SESSION['uid']);
268 } else if ($cat_view && $feed > 0) {
269 $result = db_query($link, "SELECT id FROM ttrss_feed_categories WHERE
270 id = '$feed' AND owner_uid = " . $_SESSION['uid']);
271 }
272
273 if ($result && db_num_rows($result) == 0) {
274 print json_encode(generate_error_feed($link, __("Feed not found.")));
275 return;
276 }
277
278 /* Updating a label ccache means recalculating all of the caches
279 * so for performance reasons we don't do that here */
280
281 if ($feed >= 0) {
282 ccache_update($link, $feed, $_SESSION["uid"], $cat_view);
283 }
284
285 set_pref($link, "_DEFAULT_VIEW_MODE", $view_mode);
286 set_pref($link, "_DEFAULT_VIEW_LIMIT", $limit);
287 set_pref($link, "_DEFAULT_VIEW_ORDER_BY", $order_by);
288
289 if (!$cat_view && preg_match("/^[0-9][0-9]*$/", $feed)) {
290 db_query($link, "UPDATE ttrss_feeds SET last_viewed = NOW()
291 WHERE id = '$feed' AND owner_uid = ".$_SESSION["uid"]);
292 }
293
294 $reply['headlines'] = array();
295
296 if (!$next_unread_feed)
297 $reply['headlines']['id'] = $feed;
298 else
299 $reply['headlines']['id'] = $next_unread_feed;
300
301 $reply['headlines']['is_cat'] = (bool) $cat_view;
302
303 $override_order = false;
304
305 if (get_pref($link, "SORT_HEADLINES_BY_FEED_DATE", $owner_uid)) {
306 $date_sort_field = "updated";
307 } else {
308 $date_sort_field = "date_entered";
309 }
310
311 switch ($order_by) {
312 case "date":
313 if (get_pref($link, 'REVERSE_HEADLINES', $owner_uid)) {
314 $override_order = "$date_sort_field";
315 } else {
316 $override_order = "$date_sort_field DESC";
317 }
318 break;
319
320 case "title":
321 if (get_pref($link, 'REVERSE_HEADLINES', $owner_uid)) {
322 $override_order = "title DESC, $date_sort_field";
323 } else {
324 $override_order = "title, $date_sort_field DESC";
325 }
326 break;
327
328 case "score":
329 if (get_pref($link, 'REVERSE_HEADLINES', $owner_uid)) {
330 $override_order = "score, $date_sort_field";
331 } else {
332 $override_order = "score DESC, $date_sort_field DESC";
333 }
334 break;
335 }
336
337 if ($_REQUEST["debug"]) $timing_info = print_checkpoint("04", $timing_info);
338
339 $ret = format_headlines_list($link, $feed, $subop,
340 $view_mode, $limit, $cat_view, $next_unread_feed, $offset,
341 $vgroup_last_feed, $override_order);
342
343 $topmost_article_ids = $ret[0];
344 $headlines_count = $ret[1];
345 $returned_feed = $ret[2];
346 $disable_cache = $ret[3];
347 $vgroup_last_feed = $ret[4];
348
349 $reply['headlines']['content'] = $ret[5];
350 $reply['headlines']['toolbar'] = $ret[6];
351
352 if ($_REQUEST["debug"]) $timing_info = print_checkpoint("05", $timing_info);
353
354 $headlines_unread = ccache_find($link, $returned_feed, $_SESSION["uid"],
355 $cat_view, true);
356
357 if ($headlines_unread == -1) {
358 $headlines_unread = getFeedUnread($link, $returned_feed, $cat_view);
359 }
360
361 $reply['headlines-info'] = array("count" => (int) $headlines_count,
362 "vgroup_last_feed" => $vgroup_last_feed,
363 "unread" => (int) $headlines_unread,
364 "disable_cache" => (bool) $disable_cache);
365
366 if ($_REQUEST["debug"]) $timing_info = print_checkpoint("20", $timing_info);
367
368 if (is_array($topmost_article_ids) && !get_pref($link, 'COMBINED_DISPLAY_MODE') && !$_SESSION["bw_limit"]) {
369 $articles = array();
370
371 foreach ($topmost_article_ids as $id) {
372 array_push($articles, format_article($link, $id, $feed, false));
373 }
374
375 $reply['articles'] = $articles;
376 }
377
378 if ($subop) {
379 $reply['counters'] = getAllCounters($link, $omode, $feed);
380 }
381
382 if ($_REQUEST["debug"]) $timing_info = print_checkpoint("30", $timing_info);
383
384 $reply['runtime-info'] = make_runtime_info($link);
385
386 print json_encode($reply);
387
388 break; // viewfeed
389
390 case "pref-feeds":
391 module_pref_feeds($link);
392 break; // pref-feeds
393
394 case "pref-filters":
395 module_pref_filters($link);
396 break; // pref-filters
397
398 case "pref-labels":
399 module_pref_labels($link);
400 break; // pref-labels
401
402 case "pref-prefs":
403 module_pref_prefs($link);
404 break; // pref-prefs
405
406 case "pref-users":
407 module_pref_users($link);
408 break; // prefs-users
409
410 case "help":
411 module_help($link);
412 break; // help
413
414 case "dlg":
415 module_popup_dialog($link);
416 break; // dlg
417
418 case "pref-pub-items":
419 module_pref_pub_items($link);
420 break; // pref-pub-items
421
422 case "globalUpdateFeeds":
423 // Update all feeds needing a update.
424 update_daemon_common($link, 0, true, true);
425 break; // globalUpdateFeeds
426
427 case "pref-feed-browser":
428 module_pref_feed_browser($link);
429 break; // pref-feed-browser
430
431 case "rss":
432 $feed = db_escape_string($_REQUEST["id"]);
433 $key = db_escape_string($_REQUEST["key"]);
434 $is_cat = $_REQUEST["is_cat"] != false;
435 $limit = (int)db_escape_string($_REQUEST["limit"]);
436
437 $search = db_escape_string($_REQUEST["q"]);
438 $match_on = db_escape_string($_REQUEST["m"]);
439 $search_mode = db_escape_string($_REQUEST["smode"]);
440 $view_mode = db_escape_string($_REQUEST["view-mode"]);
441
442 if (SINGLE_USER_MODE) {
443 authenticate_user($link, "admin", null);
444 }
445
446 $owner_id = false;
447
448 if ($key) {
449 $result = db_query($link, "SELECT owner_uid FROM
450 ttrss_access_keys WHERE access_key = '$key' AND feed_id = '$feed'");
451
452 if (db_num_rows($result) == 1)
453 $owner_id = db_fetch_result($result, 0, "owner_uid");
454 }
455
456 if ($owner_id) {
457 $_SESSION['uid'] = $owner_id;
458
459 generate_syndicated_feed($link, 0, $feed, $is_cat, $limit,
460 $search, $search_mode, $match_on, $view_mode);
461 } else {
462 header('HTTP/1.1 403 Forbidden');
463 }
464 break; // rss
465
466 case "getUnread":
467 $login = db_escape_string($_REQUEST["login"]);
468 $fresh = $_REQUEST["fresh"] == "1";
469
470 $result = db_query($link, "SELECT id FROM ttrss_users WHERE login = '$login'");
471
472 if (db_num_rows($result) == 1) {
473 $uid = db_fetch_result($result, 0, "id");
474
475 print getGlobalUnread($link, $uid);
476
477 if ($fresh) {
478 print ";";
479 print getFeedArticles($link, -3, false, true, $uid);
480 }
481
482 } else {
483 print "-1;User not found";
484 }
485
486 $print_exec_time = false;
487 break; // getUnread
488
489 case "digestTest":
490 print_r(prepare_headlines_digest($link, $_SESSION["uid"]));
491 $print_exec_time = false;
492 break; // digestTest
493
494 case "digestSend":
495 send_headlines_digests($link);
496 $print_exec_time = false;
497 break; // digestSend
498
499 case "loading":
500 print __("Loading, please wait...") . " " .
501 "<img src='images/indicator_tiny.gif'>";
502
503 case "getProfiles":
504 $login = db_escape_string($_REQUEST["login"]);
505 $password = db_escape_string($_REQUEST["password"]);
506
507 if (authenticate_user($link, $login, $password)) {
508 $result = db_query($link, "SELECT * FROM ttrss_settings_profiles
509 WHERE owner_uid = " . $_SESSION["uid"] . " ORDER BY title");
510
511 print "<select style='width: 100%' name='profile'>";
512
513 print "<option value='0'>" . __("Default profile") . "</option>";
514
515 while ($line = db_fetch_assoc($result)) {
516 $id = $line["id"];
517 $title = $line["title"];
518
519 print "<option value='$id'>$title</option>";
520 }
521
522 print "</select>";
523
524 $_SESSION = array();
525 }
526 break;
527
528 case "pubsub":
529 $mode = db_escape_string($_REQUEST['hub_mode']);
530 $feed_id = db_escape_string($_REQUEST['id']);
531 $feed_url = db_escape_string($_REQUEST['hub_topic']);
532
533 // TODO: implement hub_verifytoken checking
534
535 $result = db_query($link, "SELECT feed_url FROM ttrss_feeds
536 WHERE id = '$feed_id'");
537
538 $check_feed_url = db_fetch_result($result, 0, "feed_url");
539
540 if ($check_feed_url && ($check_feed_url == $feed_url || !$feed_url)) {
541 if ($mode == "subscribe") {
542
543 db_query($link, "UPDATE ttrss_feeds SET pubsub_state = 2
544 WHERE id = '$feed_id'");
545
546 echo $_REQUEST['hub_challenge'];
547 } else if ($mode == "unsubscribe") {
548
549 db_query($link, "UPDATE ttrss_feeds SET pubsub_state = 0
550 WHERE id = '$feed_id'");
551
552 echo $_REQUEST['hub_challenge'];
553 } else if (!$mode) {
554
555 // Received update ping, schedule feed update.
556
557 update_rss_feed($link, $feed_id, true, true);
558
559 }
560 } else {
561 header('HTTP/1.0 404 Not Found');
562 }
563
564 break;
565 } // Select action according to $op value.
566
567 // We close the connection to database.
568 db_close($link);
569 ?>
570
571 <?php if ($print_exec_time) { ?>
572 <!-- <?php echo sprintf("Backend execution time: %.4f seconds", getmicrotime() - $script_started) ?> -->
573 <?php } ?>