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