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