]> git.wh0rd.org - tt-rss.git/blame - backend.php
tweak display of tweet button
[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 133 1 => __("Magpie"),
b3009bcd
AD
134 2 => __("SimplePie"),
135 3 => __("Twitter OAuth"));
16211ddb 136
78a5c296 137 if (DEFAULT_UPDATE_METHOD == "1") {
16211ddb
AD
138 $update_methods[0] .= ' (SimplePie)';
139 } else {
140 $update_methods[0] .= ' (Magpie)';
141 }
142
3c5783b7 143 $access_level_names = array(
d1db26aa 144 0 => __("User"),
a88d37e5 145 5 => __("Power User"),
d1db26aa 146 10 => __("Administrator"));
3c5783b7 147
f27d955a 148 require_once "modules/pref-prefs.php";
ef8be8ea
AD
149 require_once "modules/popup-dialog.php";
150 require_once "modules/help.php";
151 require_once "modules/pref-feeds.php";
152 require_once "modules/pref-filters.php";
153 require_once "modules/pref-labels.php";
154 require_once "modules/pref-users.php";
ef8be8ea 155
b2804af7 156 if (!sanity_check($link)) { return; }
023fe037 157
45004d43
AD
158 switch($op) { // Select action according to $op value.
159 case "rpc":
160 // Handle remote procedure calls.
161 handle_rpc_request($link);
162 break; // rpc
163
164 case "feeds":
b4e75b2a 165 $subop = $_REQUEST["subop"];
1985a5e0 166 $root = (bool)$_REQUEST["root"];
45004d43
AD
167
168 switch($subop) {
169 case "catchupAll":
170 db_query($link, "UPDATE ttrss_user_entries SET
171 last_read = NOW(),unread = false WHERE owner_uid = " . $_SESSION["uid"]);
ad0056a8
AD
172 ccache_zero_all($link, $_SESSION["uid"]);
173
45004d43
AD
174 break;
175
176 case "collapse":
b4e75b2a 177 $cat_id = db_escape_string($_REQUEST["cid"]);
fcf70c51
AD
178 $mode = (int) db_escape_string($_REQUEST['mode']);
179 toggle_collapse_cat($link, $cat_id, $mode);
45004d43
AD
180 return;
181 break;
182 }
c3b81db0 183
1985a5e0
AD
184 if (!$root) {
185 print json_encode(outputFeedList($link));
186 } else {
187
188 $feeds = outputFeedList($link, false);
189
190 $root = array();
191 $root['id'] = 'root';
192 $root['name'] = __('Feeds');
193 $root['items'] = $feeds['items'];
194
195 $fl = array();
196 $fl['identifier'] = 'id';
197 $fl['label'] = 'name';
198 $fl['items'] = array($root);
199
200 print json_encode($fl);
201 }
13e785e0 202
45004d43 203 break; // feeds
1cd17194 204
b509d64e
AD
205 case "la":
206 $id = db_escape_string($_REQUEST['id']);
207
208 $result = db_query($link, "SELECT link FROM ttrss_entries, ttrss_user_entries
209 WHERE id = '$id' AND id = ref_id AND owner_uid = '".$_SESSION['uid']."'");
210
211 if (db_num_rows($result) == 1) {
212 $article_url = db_fetch_result($result, 0, 'link');
d4dcf8e9 213 $article_url = str_replace("\n", "", $article_url);
b509d64e
AD
214
215 header("Location: $article_url");
216 return;
217
218 } else {
219 print_error(__("Article not found."));
220 }
221 break;
222
45004d43 223 case "view":
e097e8be 224
b4e75b2a
AD
225 $id = db_escape_string($_REQUEST["id"]);
226 $cids = split(",", db_escape_string($_REQUEST["cids"]));
227 $mode = db_escape_string($_REQUEST["mode"]);
228 $omode = db_escape_string($_REQUEST["omode"]);
e097e8be 229
8cab2eb9 230 if ($mode != "zoom") print "<reply>";
e097e8be 231
45004d43
AD
232 // in prefetch mode we only output requested cids, main article
233 // just gets marked as read (it already exists in client cache)
e097e8be 234
45004d43 235 if ($mode == "") {
e04c18a2 236 outputArticleXML($link, $id, false);
eedfb635 237 } else if ($mode == "zoom") {
e04c18a2 238 outputArticleXML($link, $id, false, true, true);
45004d43
AD
239 } else {
240 catchupArticleById($link, $id, 0);
241 }
e097e8be 242
a598370d
AD
243 if (!$_SESSION["bw_limit"]) {
244 foreach ($cids as $cid) {
245 if ($cid) {
e04c18a2 246 outputArticleXML($link, $cid, false, false);
a598370d 247 }
45004d43 248 }
e097e8be 249 }
e097e8be 250
5225d420 251 /* if ($mode == "prefetch") {
6a7817c1
AD
252 print "<counters><![CDATA[";
253 print json_encode(getAllCounters($link, $omode));
254 print "]]></counters>";
5225d420 255 } */
5a94a953 256
8cab2eb9 257 if ($mode != "zoom") print "</reply>";
45004d43 258 break; // view
1cd17194 259
45004d43 260 case "viewfeed":
1cd17194 261
45004d43
AD
262 $print_exec_time = true;
263 $timing_info = getmicrotime();
46921916 264
45004d43 265 print "<reply>";
3de0261a 266
b4e75b2a 267 if ($_REQUEST["debug"]) $timing_info = print_checkpoint("0", $timing_info);
46921916 268
b4e75b2a 269 $omode = db_escape_string($_REQUEST["omode"]);
3de0261a 270
b4e75b2a
AD
271 $feed = db_escape_string($_REQUEST["feed"]);
272 $subop = db_escape_string($_REQUEST["subop"]);
273 $view_mode = db_escape_string($_REQUEST["view_mode"]);
6f068202 274 $limit = (int) get_pref($link, "DEFAULT_ARTICLE_LIMIT");
c3fddd05
AD
275 @$cat_view = db_escape_string($_REQUEST["cat"]);
276 @$next_unread_feed = db_escape_string($_REQUEST["nuf"]);
277 @$offset = db_escape_string($_REQUEST["skip"]);
278 @$vgroup_last_feed = db_escape_string($_REQUEST["vgrlf"]);
b4e75b2a 279 $order_by = db_escape_string($_REQUEST["order_by"]);
203de776 280
fe1087fb
AD
281 /* Feed -5 is a special case: it is used to display auxiliary information
282 * when there's nothing to load - e.g. no stuff in fresh feed */
283
284 if ($feed == -5) {
285 generate_dashboard_feed($link);
286 print "</reply>";
287 return;
288 }
289
51e196de
AD
290 /* Updating a label ccache means recalculating all of the caches
291 * so for performance reasons we don't do that here */
292
5225d420
AD
293 if ($feed >= 0) {
294 ccache_update($link, $feed, $_SESSION["uid"], $cat_view);
295 }
0737b95a 296
45004d43
AD
297 set_pref($link, "_DEFAULT_VIEW_MODE", $view_mode);
298 set_pref($link, "_DEFAULT_VIEW_LIMIT", $limit);
7b4d02a8 299 set_pref($link, "_DEFAULT_VIEW_ORDER_BY", $order_by);
40496720 300
45004d43
AD
301 if (!$cat_view && preg_match("/^[0-9][0-9]*$/", $feed)) {
302 db_query($link, "UPDATE ttrss_feeds SET last_viewed = NOW()
303 WHERE id = '$feed' AND owner_uid = ".$_SESSION["uid"]);
304 }
7466dc6a 305
1098687a 306 if (!$next_unread_feed) {
6e4f4ce1 307 print "<headlines id=\"$feed\" is_cat=\"$cat_view\">";
1098687a 308 } else {
6e4f4ce1 309 print "<headlines id=\"$next_unread_feed\" is_cat=\"$cat_view\">";
1098687a 310 }
a5c815d3
AD
311
312 $override_order = false;
313
b3990c92
AD
314 if (get_pref($link, "SORT_HEADLINES_BY_FEED_DATE", $owner_uid)) {
315 $date_sort_field = "updated";
316 } else {
317 $date_sort_field = "date_entered";
318 }
319
a5c815d3
AD
320 switch ($order_by) {
321 case "date":
322 if (get_pref($link, 'REVERSE_HEADLINES', $owner_uid)) {
b3990c92 323 $override_order = "$date_sort_field";
a5c815d3 324 } else {
b3990c92 325 $override_order = "$date_sort_field DESC";
a5c815d3
AD
326 }
327 break;
c50e2b30 328
a5c815d3 329 case "title":
7b20c977 330 if (get_pref($link, 'REVERSE_HEADLINES', $owner_uid)) {
b3990c92 331 $override_order = "title DESC, $date_sort_field";
7b20c977 332 } else {
b3990c92 333 $override_order = "title, $date_sort_field DESC";
7b20c977 334 }
a5c815d3 335 break;
d76a3b03 336
a5c815d3 337 case "score":
7b20c977 338 if (get_pref($link, 'REVERSE_HEADLINES', $owner_uid)) {
b3990c92 339 $override_order = "score, $date_sort_field";
7b20c977 340 } else {
b3990c92 341 $override_order = "score DESC, $date_sort_field DESC";
7b20c977 342 }
a5c815d3
AD
343 break;
344 }
e19c1824 345
905ff52a
AD
346 if ($_REQUEST["debug"]) $timing_info = print_checkpoint("04", $timing_info);
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
6e4f4ce1 358 print "</headlines>";
46921916 359
905ff52a
AD
360 if ($_REQUEST["debug"]) $timing_info = print_checkpoint("05", $timing_info);
361
ffbe082d
AD
362 //print "<headlines-count value=\"$headlines_count\"/>";
363 //print "<vgroup-last-feed value=\"$vgroup_last_feed\"/>";
081e527d 364
cfcb7d42 365 $headlines_unread = ccache_find($link, $returned_feed, $_SESSION["uid"],
696a6850
AD
366 $cat_view, true);
367
368 if ($headlines_unread == -1) {
369 $headlines_unread = getFeedUnread($link, $returned_feed, $cat_view);
370
371 }
a5c815d3 372
ffbe082d
AD
373 //print "<headlines-unread value=\"$headlines_unread\"/>";
374 //printf("<disable-cache value=\"%d\"/>", $disable_cache);
375
376 print "<headlines-info><![CDATA[";
377
378 $info = array("count" => (int) $headlines_count,
379 "vgroup_last_feed" => $vgroup_last_feed,
380 "unread" => (int) $headlines_unread,
381 "disable_cache" => (bool) $disable_cache);
382
383 print json_encode($info);
384
385 print "]]></headlines-info>";
a5c815d3 386
b4e75b2a 387 if ($_REQUEST["debug"]) $timing_info = print_checkpoint("10", $timing_info);
a5c815d3
AD
388
389 if (is_array($topmost_article_ids) && !get_pref($link, 'COMBINED_DISPLAY_MODE') && !$_SESSION["bw_limit"]) {
390 print "<articles>";
391 foreach ($topmost_article_ids as $id) {
392 outputArticleXML($link, $id, $feed, false);
45004d43 393 }
a5c815d3 394 print "</articles>";
961f4c73 395 }
961f4c73 396
b4e75b2a 397 if ($_REQUEST["debug"]) $timing_info = print_checkpoint("20", $timing_info);
46921916 398
dd25b0c6
AD
399 //if (get_pref($link, 'COMBINED_DISPLAY_MODE') || $subop) {
400 if ($subop) {
5225d420
AD
401 print "<counters><![CDATA[";
402 print json_encode(getAllCounters($link, $omode, $feed));
403 print "]]></counters>";
d36f5607 404 }
98bea1b1 405
b4e75b2a 406 if ($_REQUEST["debug"]) $timing_info = print_checkpoint("30", $timing_info);
46921916 407
45004d43 408 print_runtime_info($link);
03c88bdd 409
45004d43
AD
410 print "</reply>";
411 break; // viewfeed
1cd17194 412
45004d43
AD
413 case "pref-feeds":
414 module_pref_feeds($link);
415 break; // pref-feeds
a7f22b70 416
45004d43
AD
417 case "pref-filters":
418 module_pref_filters($link);
419 break; // pref-filters
a0476535 420
45004d43
AD
421 case "pref-labels":
422 module_pref_labels($link);
423 break; // pref-labels
d0000401 424
45004d43
AD
425 case "pref-prefs":
426 module_pref_prefs($link);
427 break; // pref-prefs
f27d955a 428
45004d43
AD
429 case "pref-users":
430 module_pref_users($link);
431 break; // prefs-users
a0476535 432
45004d43
AD
433 case "help":
434 module_help($link);
435 break; // help
a0476535 436
45004d43
AD
437 case "dlg":
438 module_popup_dialog($link);
439 break; // dlg
a7f22b70 440
45004d43
AD
441 case "pref-pub-items":
442 module_pref_pub_items($link);
443 break; // pref-pub-items
e4f4b46f 444
45004d43
AD
445 case "globalUpdateFeeds":
446 // update feeds of all users, may be used anonymously
e4f4b46f 447
5cbaf873 448 print "<!--";
45004d43 449 // Update all feeds needing a update.
5cbaf873
AD
450 update_daemon_common($link, 0, true, true);
451 print " -->";
a7f22b70 452
45004d43
AD
453 print "<rpc-reply>
454 <message msg=\"All feeds updated\"/>
455 </rpc-reply>";
456 break; // globalUpdateFeeds
e5d758e3 457
45004d43
AD
458 case "pref-feed-browser":
459 module_pref_feed_browser($link);
460 break; // pref-feed-browser
c6232e43 461
45004d43 462 case "rss":
b4e75b2a 463 $feed = db_escape_string($_REQUEST["id"]);
8801fb01 464 $key = db_escape_string($_REQUEST["key"]);
b4e75b2a
AD
465 $is_cat = $_REQUEST["is_cat"] != false;
466 $limit = (int)db_escape_string($_REQUEST["limit"]);
e1eb2147 467
b4e75b2a
AD
468 $search = db_escape_string($_REQUEST["q"]);
469 $match_on = db_escape_string($_REQUEST["m"]);
470 $search_mode = db_escape_string($_REQUEST["smode"]);
2f2bd1b3 471 $view_mode = db_escape_string($_REQUEST["view-mode"]);
18664970 472
b4798282
AD
473 if (SINGLE_USER_MODE) {
474 authenticate_user($link, "admin", null);
475 }
476
8801fb01
AD
477 if ($key && !$_SESSION["uid"]) {
478 $result = db_query($link, "SELECT owner_uid FROM
479 ttrss_access_keys WHERE access_key = '$key' AND feed_id = '$feed'");
480
481 if (db_num_rows($result) == 1)
482 $_SESSION["uid"] = db_fetch_result($result, 0, "owner_uid");
483
45004d43 484 }
18664970 485
135d4251 486 if ($_SESSION["uid"]) {
23d72f39
AD
487 generate_syndicated_feed($link, 0, $feed, $is_cat, $limit,
488 $search, $search_mode, $match_on, $view_mode);
45004d43
AD
489 }
490 break; // rss
18664970 491
45004d43 492 case "getUnread":
b4e75b2a 493 $login = db_escape_string($_REQUEST["login"]);
1f7b77d1 494 $fresh = $_REQUEST["fresh"] == "1";
f3acc32e 495
45004d43 496 header("Content-Type: text/plain; charset=utf-8");
f3acc32e 497
45004d43 498 $result = db_query($link, "SELECT id FROM ttrss_users WHERE login = '$login'");
7e3634d9 499
45004d43
AD
500 if (db_num_rows($result) == 1) {
501 $uid = db_fetch_result($result, 0, "id");
1f7b77d1 502
45004d43 503 print getGlobalUnread($link, $uid);
1f7b77d1
AD
504
505 if ($fresh) {
506 print ";";
507 print getFeedArticles($link, -3, false, true, $uid);
508 }
509
45004d43
AD
510 } else {
511 print "-1;User not found";
512 }
7e3634d9 513
45004d43
AD
514 $print_exec_time = false;
515 break; // getUnread
9cd7c995 516
45004d43
AD
517 case "digestTest":
518 header("Content-Type: text/plain");
519 print_r(prepare_headlines_digest($link, $_SESSION["uid"]));
520 $print_exec_time = false;
521 break; // digestTest
448b0abd 522
45004d43
AD
523 case "digestSend":
524 header("Content-Type: text/plain");
525 send_headlines_digests($link);
526 $print_exec_time = false;
527 break; // digestSend
7e3634d9 528
d9084cf2
AD
529 case "getProfiles":
530 $login = db_escape_string($_REQUEST["login"]);
531 $password = db_escape_string($_REQUEST["password"]);
532
533 if (authenticate_user($link, $login, $password)) {
534 $result = db_query($link, "SELECT * FROM ttrss_settings_profiles
535 WHERE owner_uid = " . $_SESSION["uid"] . " ORDER BY title");
536
537 print "<select style='width: 100%' name='profile'>";
538
539 print "<option value='0'>" . __("Default profile") . "</option>";
540
541 while ($line = db_fetch_assoc($result)) {
542 $id = $line["id"];
543 $title = $line["title"];
544
545 print "<option value='$id'>$title</option>";
546 }
547
548 print "</select>";
549
550 $_SESSION = array();
0456176a 551 }
d9084cf2 552 break;
d9084cf2 553
45004d43 554 } // Select action according to $op value.
f3acc32e 555
45004d43 556 // We close the connection to database.
4b3dff6e 557 db_close($link);
1cd17194 558?>
406d9489 559
7e3634d9 560<?php if ($print_exec_time) { ?>
1d3a17c7 561<!-- <?php echo sprintf("Backend execution time: %.4f seconds", getmicrotime() - $script_started) ?> -->
f3acc32e 562<?php } ?>