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