]> git.wh0rd.org - tt-rss.git/blame - backend.php
experimental split of public calls into public.php (refs #389)
[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
f9da2388
AD
17 $op = $_REQUEST["op"];
18
fb074239 19 require_once "functions.php";
f9da2388 20 if ($op != "share") require_once "sessions.php";
c339343b 21 require_once "modules/backend-rpc.php";
657770a0
AD
22 require_once "sanity_check.php";
23 require_once "config.php";
af106b0e
AD
24 require_once "db.php";
25 require_once "db-prefs.php";
657770a0 26
dc56b3b7 27 no_cache_incantation();
8d039718 28
7b26a148 29 startup_gettext();
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 44
d9084cf2 45 $subop = $_REQUEST["subop"];
fac5e7d1 46 $mode = $_REQUEST["mode"];
7f0acba7 47
565ca565
AD
48 if ((!$op || $op == "rss" || $op == "dlg") && !$_REQUEST["noxml"]) {
49 header("Content-Type: application/xml; charset=utf-8");
50 } else {
9fdf7824 51 header("Content-Type: text/plain; charset=utf-8");
262bd8ea
AD
52 }
53
bd202c3f
AD
54 if (ENABLE_GZIP_OUTPUT) {
55 ob_start("ob_gzhandler");
56 }
57
9c883208
AD
58 if (SINGLE_USER_MODE) {
59 authenticate_user($link, "admin", null);
60 }
61
e0d91d84
AD
62 $public_calls = array("globalUpdateFeeds", "rss", "getUnread", "getProfiles", "share",
63 "fbexport", "logout", "pubsub");
009646d2 64
e0d91d84
AD
65 if (array_search($op, $public_calls) !== false) {
66
67 handle_public_request($link, $op);
68 return;
69
70 } else if (!($_SESSION["uid"] && validate_session($link))) {
d0f73380
AD
71 if ($op == 'pref-feeds' && $_REQUEST['subop'] == 'add') {
72 header("Content-Type: text/html");
73 login_sequence($link);
74 render_login_form($link);
75 } else {
76 header("Content-Type: text/plain");
77 print json_encode(array("error" => array("code" => 6)));
78 }
009646d2 79 return;
262bd8ea 80 }
1c7f75ed 81
ad815c71 82 $purge_intervals = array(
d1db26aa
AD
83 0 => __("Use default"),
84 -1 => __("Never purge"),
85 5 => __("1 week old"),
86 14 => __("2 weeks old"),
87 31 => __("1 month old"),
88 60 => __("2 months old"),
89 90 => __("3 months old"));
ad815c71
AD
90
91 $update_intervals = array(
ecace165 92 0 => __("Default interval"),
d1db26aa 93 -1 => __("Disable updates"),
1e7cbe16 94 15 => __("Each 15 minutes"),
d1db26aa 95 30 => __("Each 30 minutes"),
505f2f0e
AD
96 60 => __("Hourly"),
97 240 => __("Each 4 hours"),
98 720 => __("Each 12 hours"),
99 1440 => __("Daily"),
100 10080 => __("Weekly"));
101
102 $update_intervals_nodefault = array(
103 -1 => __("Disable updates"),
104 15 => __("Each 15 minutes"),
105 30 => __("Each 30 minutes"),
d1db26aa
AD
106 60 => __("Hourly"),
107 240 => __("Each 4 hours"),
108 720 => __("Each 12 hours"),
109 1440 => __("Daily"),
110 10080 => __("Weekly"));
ad815c71 111
16211ddb 112 $update_methods = array(
ecace165 113 0 => __("Default"),
0dd9c0cf 114 1 => __("Magpie"),
009646d2 115 2 => __("SimplePie"),
b3009bcd 116 3 => __("Twitter OAuth"));
16211ddb 117
78a5c296 118 if (DEFAULT_UPDATE_METHOD == "1") {
16211ddb
AD
119 $update_methods[0] .= ' (SimplePie)';
120 } else {
121 $update_methods[0] .= ' (Magpie)';
122 }
123
3c5783b7 124 $access_level_names = array(
009646d2 125 0 => __("User"),
a88d37e5 126 5 => __("Power User"),
d1db26aa 127 10 => __("Administrator"));
3c5783b7 128
f27d955a 129 require_once "modules/pref-prefs.php";
ef8be8ea
AD
130 require_once "modules/popup-dialog.php";
131 require_once "modules/help.php";
132 require_once "modules/pref-feeds.php";
133 require_once "modules/pref-filters.php";
134 require_once "modules/pref-labels.php";
135 require_once "modules/pref-users.php";
373266eb 136 require_once "modules/pref-instances.php";
ef8be8ea 137
ebb948c2
AD
138 $error = sanity_check($link);
139
2376ad49 140 if ($error['code'] != 0 && $op != "logout") {
ebb948c2
AD
141 print json_encode(array("error" => $error));
142 return;
143 }
023fe037 144
45004d43
AD
145 switch($op) { // Select action according to $op value.
146 case "rpc":
147 // Handle remote procedure calls.
148 handle_rpc_request($link);
149 break; // rpc
150
151 case "feeds":
b4e75b2a 152 $subop = $_REQUEST["subop"];
1985a5e0 153 $root = (bool)$_REQUEST["root"];
45004d43
AD
154
155 switch($subop) {
156 case "catchupAll":
009646d2 157 db_query($link, "UPDATE ttrss_user_entries SET
45004d43 158 last_read = NOW(),unread = false WHERE owner_uid = " . $_SESSION["uid"]);
ad0056a8
AD
159 ccache_zero_all($link, $_SESSION["uid"]);
160
45004d43
AD
161 break;
162
163 case "collapse":
b4e75b2a 164 $cat_id = db_escape_string($_REQUEST["cid"]);
fcf70c51
AD
165 $mode = (int) db_escape_string($_REQUEST['mode']);
166 toggle_collapse_cat($link, $cat_id, $mode);
45004d43
AD
167 return;
168 break;
169 }
c3b81db0 170
1985a5e0
AD
171 if (!$root) {
172 print json_encode(outputFeedList($link));
173 } else {
174
175 $feeds = outputFeedList($link, false);
176
177 $root = array();
178 $root['id'] = 'root';
179 $root['name'] = __('Feeds');
180 $root['items'] = $feeds['items'];
181
182 $fl = array();
183 $fl['identifier'] = 'id';
184 $fl['label'] = 'name';
185 $fl['items'] = array($root);
186
187 print json_encode($fl);
188 }
13e785e0 189
45004d43 190 break; // feeds
1cd17194 191
b509d64e
AD
192 case "la":
193 $id = db_escape_string($_REQUEST['id']);
194
195 $result = db_query($link, "SELECT link FROM ttrss_entries, ttrss_user_entries
bfedfe69
AD
196 WHERE id = '$id' AND id = ref_id AND owner_uid = '".$_SESSION['uid']."'
197 LIMIT 1");
b509d64e
AD
198
199 if (db_num_rows($result) == 1) {
200 $article_url = db_fetch_result($result, 0, 'link');
d4dcf8e9 201 $article_url = str_replace("\n", "", $article_url);
b509d64e
AD
202
203 header("Location: $article_url");
204 return;
205
206 } else {
207 print_error(__("Article not found."));
208 }
209 break;
210
45004d43 211 case "view":
e097e8be 212
b4e75b2a 213 $id = db_escape_string($_REQUEST["id"]);
9949bd15 214 $cids = explode(",", db_escape_string($_REQUEST["cids"]));
b4e75b2a
AD
215 $mode = db_escape_string($_REQUEST["mode"]);
216 $omode = db_escape_string($_REQUEST["omode"]);
e097e8be 217
009646d2 218 // in prefetch mode we only output requested cids, main article
45004d43 219 // just gets marked as read (it already exists in client cache)
e097e8be 220
009646d2
AD
221 $articles = array();
222
45004d43 223 if ($mode == "") {
009646d2 224 array_push($articles, format_article($link, $id, false));
eedfb635 225 } else if ($mode == "zoom") {
9949bd15 226 array_push($articles, format_article($link, $id, true, true));
f4f0f80d 227 } else if ($mode == "raw") {
fcfa9ef1
AD
228 if ($_REQUEST['html']) {
229 header("Content-Type: text/html");
230 print '<link rel="stylesheet" type="text/css" href="tt-rss.css"/>';
231 }
f4f0f80d
AD
232
233 $article = format_article($link, $id, false);
f4f0f80d
AD
234 print $article['content'];
235 return;
45004d43 236 }
e097e8be 237
f7cffd2c
AD
238 catchupArticleById($link, $id, 0);
239
a598370d
AD
240 if (!$_SESSION["bw_limit"]) {
241 foreach ($cids as $cid) {
242 if ($cid) {
009646d2 243 array_push($articles, format_article($link, $cid, false, false));
a598370d 244 }
45004d43 245 }
e097e8be 246 }
e097e8be 247
009646d2 248 print json_encode($articles);
5a94a953 249
45004d43 250 break; // view
1cd17194 251
45004d43 252 case "viewfeed":
1cd17194 253
45004d43 254 $timing_info = getmicrotime();
46921916 255
bd202c3f 256 $reply = array();
3de0261a 257
b4e75b2a 258 if ($_REQUEST["debug"]) $timing_info = print_checkpoint("0", $timing_info);
46921916 259
b4e75b2a 260 $omode = db_escape_string($_REQUEST["omode"]);
3de0261a 261
b4e75b2a
AD
262 $feed = db_escape_string($_REQUEST["feed"]);
263 $subop = db_escape_string($_REQUEST["subop"]);
264 $view_mode = db_escape_string($_REQUEST["view_mode"]);
6f068202 265 $limit = (int) get_pref($link, "DEFAULT_ARTICLE_LIMIT");
37c03d3a 266 @$cat_view = db_escape_string($_REQUEST["cat"]) == "true";
c3fddd05
AD
267 @$next_unread_feed = db_escape_string($_REQUEST["nuf"]);
268 @$offset = db_escape_string($_REQUEST["skip"]);
269 @$vgroup_last_feed = db_escape_string($_REQUEST["vgrlf"]);
b4e75b2a 270 $order_by = db_escape_string($_REQUEST["order_by"]);
203de776 271
147f5632
CM
272 if (is_numeric($feed)) $feed = (int) $feed;
273
fe1087fb
AD
274 /* Feed -5 is a special case: it is used to display auxiliary information
275 * when there's nothing to load - e.g. no stuff in fresh feed */
276
277 if ($feed == -5) {
bd202c3f
AD
278 print json_encode(generate_dashboard_feed($link));
279 return;
280 }
281
282 $result = false;
283
284 if ($feed < -10) {
81f6deea 285 $label_feed = -11-$feed;
bd202c3f
AD
286 $result = db_query($link, "SELECT id FROM ttrss_labels2 WHERE
287 id = '$label_feed' AND owner_uid = " . $_SESSION['uid']);
147f5632 288 } else if (!$cat_view && is_numeric($feed) && $feed > 0) {
bd202c3f
AD
289 $result = db_query($link, "SELECT id FROM ttrss_feeds WHERE
290 id = '$feed' AND owner_uid = " . $_SESSION['uid']);
147f5632 291 } else if ($cat_view && is_numeric($feed) && $feed > 0) {
bd202c3f
AD
292 $result = db_query($link, "SELECT id FROM ttrss_feed_categories WHERE
293 id = '$feed' AND owner_uid = " . $_SESSION['uid']);
294 }
295
296 if ($result && db_num_rows($result) == 0) {
297 print json_encode(generate_error_feed($link, __("Feed not found.")));
fe1087fb
AD
298 return;
299 }
300
51e196de
AD
301 /* Updating a label ccache means recalculating all of the caches
302 * so for performance reasons we don't do that here */
303
5225d420
AD
304 if ($feed >= 0) {
305 ccache_update($link, $feed, $_SESSION["uid"], $cat_view);
306 }
0737b95a 307
45004d43
AD
308 set_pref($link, "_DEFAULT_VIEW_MODE", $view_mode);
309 set_pref($link, "_DEFAULT_VIEW_LIMIT", $limit);
7b4d02a8 310 set_pref($link, "_DEFAULT_VIEW_ORDER_BY", $order_by);
40496720 311
45004d43
AD
312 if (!$cat_view && preg_match("/^[0-9][0-9]*$/", $feed)) {
313 db_query($link, "UPDATE ttrss_feeds SET last_viewed = NOW()
314 WHERE id = '$feed' AND owner_uid = ".$_SESSION["uid"]);
315 }
7466dc6a 316
bd202c3f
AD
317 $reply['headlines'] = array();
318
319 if (!$next_unread_feed)
320 $reply['headlines']['id'] = $feed;
321 else
322 $reply['headlines']['id'] = $next_unread_feed;
323
324 $reply['headlines']['is_cat'] = (bool) $cat_view;
009646d2 325
a5c815d3
AD
326 $override_order = false;
327
b3990c92
AD
328 if (get_pref($link, "SORT_HEADLINES_BY_FEED_DATE", $owner_uid)) {
329 $date_sort_field = "updated";
330 } else {
331 $date_sort_field = "date_entered";
332 }
333
a5c815d3
AD
334 switch ($order_by) {
335 case "date":
336 if (get_pref($link, 'REVERSE_HEADLINES', $owner_uid)) {
b3990c92 337 $override_order = "$date_sort_field";
009646d2 338 } else {
b3990c92 339 $override_order = "$date_sort_field DESC";
a5c815d3
AD
340 }
341 break;
c50e2b30 342
a5c815d3 343 case "title":
7b20c977 344 if (get_pref($link, 'REVERSE_HEADLINES', $owner_uid)) {
b3990c92 345 $override_order = "title DESC, $date_sort_field";
7b20c977 346 } else {
b3990c92 347 $override_order = "title, $date_sort_field DESC";
7b20c977 348 }
a5c815d3 349 break;
d76a3b03 350
a5c815d3 351 case "score":
7b20c977 352 if (get_pref($link, 'REVERSE_HEADLINES', $owner_uid)) {
b3990c92 353 $override_order = "score, $date_sort_field";
7b20c977 354 } else {
b3990c92 355 $override_order = "score DESC, $date_sort_field DESC";
7b20c977 356 }
a5c815d3
AD
357 break;
358 }
e19c1824 359
905ff52a
AD
360 if ($_REQUEST["debug"]) $timing_info = print_checkpoint("04", $timing_info);
361
bd202c3f 362 $ret = format_headlines_list($link, $feed, $subop,
009646d2 363 $view_mode, $limit, $cat_view, $next_unread_feed, $offset,
a5c815d3 364 $vgroup_last_feed, $override_order);
7b4d02a8 365
a5c815d3
AD
366 $topmost_article_ids = $ret[0];
367 $headlines_count = $ret[1];
368 $returned_feed = $ret[2];
369 $disable_cache = $ret[3];
370 $vgroup_last_feed = $ret[4];
7b4d02a8 371
7d96bfcd
AD
372// if ($_REQUEST["debug"]) print_r($ret);
373
374 $reply['headlines']['content'] =& $ret[5]['content'];
375 $reply['headlines']['toolbar'] =& $ret[5]['toolbar'];
46921916 376
905ff52a
AD
377 if ($_REQUEST["debug"]) $timing_info = print_checkpoint("05", $timing_info);
378
bd202c3f 379 $reply['headlines-info'] = array("count" => (int) $headlines_count,
ffbe082d 380 "vgroup_last_feed" => $vgroup_last_feed,
ffbe082d
AD
381 "disable_cache" => (bool) $disable_cache);
382
bd202c3f 383 if ($_REQUEST["debug"]) $timing_info = print_checkpoint("20", $timing_info);
009646d2 384
bd202c3f 385 if (is_array($topmost_article_ids) && !get_pref($link, 'COMBINED_DISPLAY_MODE') && !$_SESSION["bw_limit"]) {
009646d2
AD
386 $articles = array();
387
a5c815d3 388 foreach ($topmost_article_ids as $id) {
9949bd15 389 array_push($articles, format_article($link, $id, false));
45004d43 390 }
009646d2 391
bd202c3f
AD
392 $reply['articles'] = $articles;
393 }
46921916 394
81787bbf
AD
395// if ($subop) {
396// $reply['counters'] = getAllCounters($link, $omode, $feed);
397// }
98bea1b1 398
b4e75b2a 399 if ($_REQUEST["debug"]) $timing_info = print_checkpoint("30", $timing_info);
46921916 400
bd202c3f
AD
401 $reply['runtime-info'] = make_runtime_info($link);
402
403 print json_encode($reply);
03c88bdd 404
45004d43 405 break; // viewfeed
1cd17194 406
45004d43
AD
407 case "pref-feeds":
408 module_pref_feeds($link);
409 break; // pref-feeds
a7f22b70 410
45004d43
AD
411 case "pref-filters":
412 module_pref_filters($link);
413 break; // pref-filters
a0476535 414
45004d43
AD
415 case "pref-labels":
416 module_pref_labels($link);
417 break; // pref-labels
d0000401 418
45004d43
AD
419 case "pref-prefs":
420 module_pref_prefs($link);
421 break; // pref-prefs
f27d955a 422
45004d43
AD
423 case "pref-users":
424 module_pref_users($link);
425 break; // prefs-users
a0476535 426
45004d43
AD
427 case "help":
428 module_help($link);
429 break; // help
a0476535 430
45004d43
AD
431 case "dlg":
432 module_popup_dialog($link);
433 break; // dlg
a7f22b70 434
45004d43
AD
435 case "pref-pub-items":
436 module_pref_pub_items($link);
437 break; // pref-pub-items
e4f4b46f 438
45004d43
AD
439 case "pref-feed-browser":
440 module_pref_feed_browser($link);
441 break; // pref-feed-browser
c6232e43 442
373266eb
AD
443 case "pref-instances":
444 module_pref_instances($link);
445 break; // pref-instances
446
45004d43 447 case "digestTest":
45004d43 448 print_r(prepare_headlines_digest($link, $_SESSION["uid"]));
45004d43 449 break; // digestTest
448b0abd 450
45004d43 451 case "digestSend":
45004d43 452 send_headlines_digests($link);
45004d43 453 break; // digestSend
7e3634d9 454
ba7e88e5 455 case "loading":
347b467b 456 header("Content-type: text/html");
009646d2 457 print __("Loading, please wait...") . " " .
ba7e88e5 458 "<img src='images/indicator_tiny.gif'>";
373266eb 459 break; // loading
ba7e88e5 460
1ca77e0a
AD
461 default:
462 header("Content-Type: text/plain");
463 print json_encode(array("error" => array("code" => 7)));
373266eb 464 break; // fallback
5ab9791f 465 } // Select action according to $op value.
ba7e88e5 466
45004d43 467 // We close the connection to database.
4b3dff6e 468 db_close($link);
1cd17194 469?>