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