]> git.wh0rd.org - tt-rss.git/blame - backend.php
add twitter register/callback server
[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');
d4dcf8e9 212 $article_url = str_replace("\n", "", $article_url);
b509d64e
AD
213
214 header("Location: $article_url");
215 return;
216
217 } else {
218 print_error(__("Article not found."));
219 }
220 break;
221
45004d43 222 case "view":
e097e8be 223
b4e75b2a
AD
224 $id = db_escape_string($_REQUEST["id"]);
225 $cids = split(",", db_escape_string($_REQUEST["cids"]));
226 $mode = db_escape_string($_REQUEST["mode"]);
227 $omode = db_escape_string($_REQUEST["omode"]);
e097e8be 228
8cab2eb9 229 if ($mode != "zoom") print "<reply>";
e097e8be 230
45004d43
AD
231 // in prefetch mode we only output requested cids, main article
232 // just gets marked as read (it already exists in client cache)
e097e8be 233
45004d43 234 if ($mode == "") {
e04c18a2 235 outputArticleXML($link, $id, false);
eedfb635 236 } else if ($mode == "zoom") {
e04c18a2 237 outputArticleXML($link, $id, false, true, true);
45004d43
AD
238 } else {
239 catchupArticleById($link, $id, 0);
240 }
e097e8be 241
a598370d
AD
242 if (!$_SESSION["bw_limit"]) {
243 foreach ($cids as $cid) {
244 if ($cid) {
e04c18a2 245 outputArticleXML($link, $cid, false, false);
a598370d 246 }
45004d43 247 }
e097e8be 248 }
e097e8be 249
5225d420 250 /* if ($mode == "prefetch") {
6a7817c1
AD
251 print "<counters><![CDATA[";
252 print json_encode(getAllCounters($link, $omode));
253 print "]]></counters>";
5225d420 254 } */
5a94a953 255
8cab2eb9 256 if ($mode != "zoom") print "</reply>";
45004d43 257 break; // view
1cd17194 258
45004d43 259 case "viewfeed":
1cd17194 260
45004d43
AD
261 $print_exec_time = true;
262 $timing_info = getmicrotime();
46921916 263
45004d43 264 print "<reply>";
3de0261a 265
b4e75b2a 266 if ($_REQUEST["debug"]) $timing_info = print_checkpoint("0", $timing_info);
46921916 267
b4e75b2a 268 $omode = db_escape_string($_REQUEST["omode"]);
3de0261a 269
b4e75b2a
AD
270 $feed = db_escape_string($_REQUEST["feed"]);
271 $subop = db_escape_string($_REQUEST["subop"]);
272 $view_mode = db_escape_string($_REQUEST["view_mode"]);
6f068202 273 $limit = (int) get_pref($link, "DEFAULT_ARTICLE_LIMIT");
c3fddd05
AD
274 @$cat_view = db_escape_string($_REQUEST["cat"]);
275 @$next_unread_feed = db_escape_string($_REQUEST["nuf"]);
276 @$offset = db_escape_string($_REQUEST["skip"]);
277 @$vgroup_last_feed = db_escape_string($_REQUEST["vgrlf"]);
b4e75b2a 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
5225d420
AD
292 if ($feed >= 0) {
293 ccache_update($link, $feed, $_SESSION["uid"], $cat_view);
294 }
0737b95a 295
45004d43
AD
296 set_pref($link, "_DEFAULT_VIEW_MODE", $view_mode);
297 set_pref($link, "_DEFAULT_VIEW_LIMIT", $limit);
7b4d02a8 298 set_pref($link, "_DEFAULT_VIEW_ORDER_BY", $order_by);
40496720 299
45004d43
AD
300 if (!$cat_view && preg_match("/^[0-9][0-9]*$/", $feed)) {
301 db_query($link, "UPDATE ttrss_feeds SET last_viewed = NOW()
302 WHERE id = '$feed' AND owner_uid = ".$_SESSION["uid"]);
303 }
7466dc6a 304
1098687a 305 if (!$next_unread_feed) {
6e4f4ce1 306 print "<headlines id=\"$feed\" is_cat=\"$cat_view\">";
1098687a 307 } else {
6e4f4ce1 308 print "<headlines id=\"$next_unread_feed\" is_cat=\"$cat_view\">";
1098687a 309 }
a5c815d3
AD
310
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";
a5c815d3 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
a5c815d3
AD
347 $ret = outputHeadlinesList($link, $feed, $subop,
348 $view_mode, $limit, $cat_view, $next_unread_feed, $offset,
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
6e4f4ce1 357 print "</headlines>";
46921916 358
905ff52a
AD
359 if ($_REQUEST["debug"]) $timing_info = print_checkpoint("05", $timing_info);
360
ffbe082d
AD
361 //print "<headlines-count value=\"$headlines_count\"/>";
362 //print "<vgroup-last-feed value=\"$vgroup_last_feed\"/>";
081e527d 363
cfcb7d42 364 $headlines_unread = ccache_find($link, $returned_feed, $_SESSION["uid"],
696a6850
AD
365 $cat_view, true);
366
367 if ($headlines_unread == -1) {
368 $headlines_unread = getFeedUnread($link, $returned_feed, $cat_view);
369
370 }
a5c815d3 371
ffbe082d
AD
372 //print "<headlines-unread value=\"$headlines_unread\"/>";
373 //printf("<disable-cache value=\"%d\"/>", $disable_cache);
374
375 print "<headlines-info><![CDATA[";
376
377 $info = array("count" => (int) $headlines_count,
378 "vgroup_last_feed" => $vgroup_last_feed,
379 "unread" => (int) $headlines_unread,
380 "disable_cache" => (bool) $disable_cache);
381
382 print json_encode($info);
383
384 print "]]></headlines-info>";
a5c815d3 385
b4e75b2a 386 if ($_REQUEST["debug"]) $timing_info = print_checkpoint("10", $timing_info);
a5c815d3
AD
387
388 if (is_array($topmost_article_ids) && !get_pref($link, 'COMBINED_DISPLAY_MODE') && !$_SESSION["bw_limit"]) {
389 print "<articles>";
390 foreach ($topmost_article_ids as $id) {
391 outputArticleXML($link, $id, $feed, false);
45004d43 392 }
a5c815d3 393 print "</articles>";
961f4c73 394 }
961f4c73 395
b4e75b2a 396 if ($_REQUEST["debug"]) $timing_info = print_checkpoint("20", $timing_info);
46921916 397
dd25b0c6
AD
398 //if (get_pref($link, 'COMBINED_DISPLAY_MODE') || $subop) {
399 if ($subop) {
5225d420
AD
400 print "<counters><![CDATA[";
401 print json_encode(getAllCounters($link, $omode, $feed));
402 print "]]></counters>";
d36f5607 403 }
98bea1b1 404
b4e75b2a 405 if ($_REQUEST["debug"]) $timing_info = print_checkpoint("30", $timing_info);
46921916 406
45004d43 407 print_runtime_info($link);
03c88bdd 408
45004d43
AD
409 print "</reply>";
410 break; // viewfeed
1cd17194 411
45004d43
AD
412 case "pref-feeds":
413 module_pref_feeds($link);
414 break; // pref-feeds
a7f22b70 415
45004d43
AD
416 case "pref-filters":
417 module_pref_filters($link);
418 break; // pref-filters
a0476535 419
45004d43
AD
420 case "pref-labels":
421 module_pref_labels($link);
422 break; // pref-labels
d0000401 423
45004d43
AD
424 case "pref-prefs":
425 module_pref_prefs($link);
426 break; // pref-prefs
f27d955a 427
45004d43
AD
428 case "pref-users":
429 module_pref_users($link);
430 break; // prefs-users
a0476535 431
45004d43
AD
432 case "help":
433 module_help($link);
434 break; // help
a0476535 435
45004d43
AD
436 case "dlg":
437 module_popup_dialog($link);
438 break; // dlg
a7f22b70 439
45004d43
AD
440 case "pref-pub-items":
441 module_pref_pub_items($link);
442 break; // pref-pub-items
e4f4b46f 443
45004d43
AD
444 case "globalUpdateFeeds":
445 // update feeds of all users, may be used anonymously
e4f4b46f 446
5cbaf873 447 print "<!--";
45004d43 448 // Update all feeds needing a update.
5cbaf873
AD
449 update_daemon_common($link, 0, true, true);
450 print " -->";
a7f22b70 451
45004d43
AD
452 print "<rpc-reply>
453 <message msg=\"All feeds updated\"/>
454 </rpc-reply>";
455 break; // globalUpdateFeeds
e5d758e3 456
45004d43
AD
457 case "pref-feed-browser":
458 module_pref_feed_browser($link);
459 break; // pref-feed-browser
c6232e43 460
45004d43 461 case "rss":
b4e75b2a 462 $feed = db_escape_string($_REQUEST["id"]);
8801fb01 463 $key = db_escape_string($_REQUEST["key"]);
b4e75b2a
AD
464 $is_cat = $_REQUEST["is_cat"] != false;
465 $limit = (int)db_escape_string($_REQUEST["limit"]);
e1eb2147 466
b4e75b2a
AD
467 $search = db_escape_string($_REQUEST["q"]);
468 $match_on = db_escape_string($_REQUEST["m"]);
469 $search_mode = db_escape_string($_REQUEST["smode"]);
2f2bd1b3 470 $view_mode = db_escape_string($_REQUEST["view-mode"]);
18664970 471
b4798282
AD
472 if (SINGLE_USER_MODE) {
473 authenticate_user($link, "admin", null);
474 }
475
8801fb01
AD
476 if ($key && !$_SESSION["uid"]) {
477 $result = db_query($link, "SELECT owner_uid FROM
478 ttrss_access_keys WHERE access_key = '$key' AND feed_id = '$feed'");
479
480 if (db_num_rows($result) == 1)
481 $_SESSION["uid"] = db_fetch_result($result, 0, "owner_uid");
482
45004d43 483 }
18664970 484
135d4251 485 if ($_SESSION["uid"]) {
23d72f39
AD
486 generate_syndicated_feed($link, 0, $feed, $is_cat, $limit,
487 $search, $search_mode, $match_on, $view_mode);
45004d43
AD
488 }
489 break; // rss
18664970 490
45004d43 491 case "getUnread":
b4e75b2a 492 $login = db_escape_string($_REQUEST["login"]);
1f7b77d1 493 $fresh = $_REQUEST["fresh"] == "1";
f3acc32e 494
45004d43 495 header("Content-Type: text/plain; charset=utf-8");
f3acc32e 496
45004d43 497 $result = db_query($link, "SELECT id FROM ttrss_users WHERE login = '$login'");
7e3634d9 498
45004d43
AD
499 if (db_num_rows($result) == 1) {
500 $uid = db_fetch_result($result, 0, "id");
1f7b77d1 501
45004d43 502 print getGlobalUnread($link, $uid);
1f7b77d1
AD
503
504 if ($fresh) {
505 print ";";
506 print getFeedArticles($link, -3, false, true, $uid);
507 }
508
45004d43
AD
509 } else {
510 print "-1;User not found";
511 }
7e3634d9 512
45004d43
AD
513 $print_exec_time = false;
514 break; // getUnread
9cd7c995 515
45004d43
AD
516 case "digestTest":
517 header("Content-Type: text/plain");
518 print_r(prepare_headlines_digest($link, $_SESSION["uid"]));
519 $print_exec_time = false;
520 break; // digestTest
448b0abd 521
45004d43
AD
522 case "digestSend":
523 header("Content-Type: text/plain");
524 send_headlines_digests($link);
525 $print_exec_time = false;
526 break; // digestSend
7e3634d9 527
d9084cf2
AD
528 case "getProfiles":
529 $login = db_escape_string($_REQUEST["login"]);
530 $password = db_escape_string($_REQUEST["password"]);
531
532 if (authenticate_user($link, $login, $password)) {
533 $result = db_query($link, "SELECT * FROM ttrss_settings_profiles
534 WHERE owner_uid = " . $_SESSION["uid"] . " ORDER BY title");
535
536 print "<select style='width: 100%' name='profile'>";
537
538 print "<option value='0'>" . __("Default profile") . "</option>";
539
540 while ($line = db_fetch_assoc($result)) {
541 $id = $line["id"];
542 $title = $line["title"];
543
544 print "<option value='$id'>$title</option>";
545 }
546
547 print "</select>";
548
549 $_SESSION = array();
0456176a 550 }
d9084cf2 551 break;
d9084cf2 552
45004d43 553 } // Select action according to $op value.
f3acc32e 554
45004d43 555 // We close the connection to database.
4b3dff6e 556 db_close($link);
1cd17194 557?>
406d9489 558
7e3634d9 559<?php if ($print_exec_time) { ?>
1d3a17c7 560<!-- <?php echo sprintf("Backend execution time: %.4f seconds", getmicrotime() - $script_started) ?> -->
f3acc32e 561<?php } ?>