]> git.wh0rd.org - tt-rss.git/blob - backend.php
viewfeed: return counters when subop is present or when in CDM
[tt-rss.git] / backend.php
1 <?php
2 /* remove ill effects of magic quotes */
3
4 if (get_magic_quotes_gpc()) {
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);
15 }
16
17 require_once "functions.php";
18 require_once "sessions.php";
19 require_once "modules/backend-rpc.php";
20 require_once "sanity_check.php";
21 require_once "config.php";
22 require_once "db.php";
23 require_once "db-prefs.php";
24
25 no_cache_incantation();
26
27 if (ENABLE_TRANSLATIONS == true) {
28 startup_gettext();
29 }
30
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
43 init_connection($link);
44
45 $op = $_REQUEST["op"];
46 $subop = $_REQUEST["subop"];
47 $mode = $_REQUEST["mode"];
48
49 $print_exec_time = false;
50
51 if ((!$op || $op == "rpc" || $op == "rss" ||
52 ($op == "view" && $mode != "zoom") ||
53 $op == "digestSend" || $op == "viewfeed" || $op == "publish" ||
54 $op == "globalUpdateFeeds") && !$_REQUEST["noxml"]) {
55 header("Content-Type: application/xml; charset=utf-8");
56
57 if (ENABLE_GZIP_OUTPUT) {
58 ob_start("ob_gzhandler");
59 }
60
61 } else {
62 if (!$_REQUEST["noxml"]) {
63 header("Content-Type: text/html; charset=utf-8");
64 } else {
65 header("Content-Type: text/plain; charset=utf-8");
66 }
67 }
68
69 if (!$op) {
70 header("Content-Type: application/xml");
71 print_error_xml(7); exit;
72 }
73
74 if (SINGLE_USER_MODE) {
75 authenticate_user($link, "admin", null);
76 }
77
78 if (!($_SESSION["uid"] && validate_session($link)) && $op != "globalUpdateFeeds"
79 && $op != "rss" && $op != "getUnread" && $op != "publish" && $op != "getProfiles") {
80
81 if ($op == "rpc" || $op == "viewfeed" || $op == "view") {
82 print_error_xml(6); die;
83 } else {
84 print "
85 <html><body>
86 <p>Error: Not logged in.</p>
87 <script type=\"text/javascript\">
88 if (parent.window != 'undefined') {
89 parent.window.location = \"tt-rss.php\";
90 } else {
91 window.location = \"tt-rss.php\";
92 }
93 </script>
94 </body></html>
95 ";
96 }
97 exit;
98 }
99
100 $purge_intervals = array(
101 0 => __("Use default"),
102 -1 => __("Never purge"),
103 5 => __("1 week old"),
104 14 => __("2 weeks old"),
105 31 => __("1 month old"),
106 60 => __("2 months old"),
107 90 => __("3 months old"));
108
109 $update_intervals = array(
110 0 => __("Default interval"),
111 -1 => __("Disable updates"),
112 15 => __("Each 15 minutes"),
113 30 => __("Each 30 minutes"),
114 60 => __("Hourly"),
115 240 => __("Each 4 hours"),
116 720 => __("Each 12 hours"),
117 1440 => __("Daily"),
118 10080 => __("Weekly"));
119
120 $update_intervals_nodefault = array(
121 -1 => __("Disable updates"),
122 15 => __("Each 15 minutes"),
123 30 => __("Each 30 minutes"),
124 60 => __("Hourly"),
125 240 => __("Each 4 hours"),
126 720 => __("Each 12 hours"),
127 1440 => __("Daily"),
128 10080 => __("Weekly"));
129
130 $update_methods = array(
131 0 => __("Default"),
132 1 => __("Magpie"),
133 2 => __("SimplePie"));
134
135 if (DEFAULT_UPDATE_METHOD == "1") {
136 $update_methods[0] .= ' (SimplePie)';
137 } else {
138 $update_methods[0] .= ' (Magpie)';
139 }
140
141 $access_level_names = array(
142 0 => __("User"),
143 5 => __("Power User"),
144 10 => __("Administrator"));
145
146 require_once "modules/pref-prefs.php";
147 require_once "modules/popup-dialog.php";
148 require_once "modules/help.php";
149 require_once "modules/pref-feeds.php";
150 require_once "modules/pref-filters.php";
151 require_once "modules/pref-labels.php";
152 require_once "modules/pref-users.php";
153
154 if (!sanity_check($link)) { return; }
155
156 switch($op) { // Select action according to $op value.
157 case "rpc":
158 // Handle remote procedure calls.
159 handle_rpc_request($link);
160 break; // rpc
161
162 case "feeds":
163 if (ENABLE_GZIP_OUTPUT) {
164 ob_start("ob_gzhandler");
165 }
166
167 $tags = $_REQUEST["tags"];
168
169 $subop = $_REQUEST["subop"];
170
171 switch($subop) {
172 case "catchupAll":
173 db_query($link, "UPDATE ttrss_user_entries SET
174 last_read = NOW(),unread = false WHERE owner_uid = " . $_SESSION["uid"]);
175 ccache_zero_all($link, $_SESSION["uid"]);
176
177 break;
178
179 case "collapse":
180 $cat_id = db_escape_string($_REQUEST["cid"]);
181 toggle_collapse_cat($link, $cat_id);
182 return;
183 break;
184
185 case "catsortreset":
186 db_query($link, "UPDATE ttrss_feed_categories
187 SET order_id = 0 WHERE owner_uid = " . $_SESSION["uid"]);
188 return;
189 break;
190
191 case "catsort":
192 $corder = db_escape_string($_REQUEST["corder"]);
193
194 $cats = split(",", $corder);
195
196 for ($i = 0; $i < count($cats); $i++) {
197 $cat_id = $cats[$i];
198
199 if ($cat_id > 0) {
200 db_query($link, "UPDATE ttrss_feed_categories
201 SET order_id = '$i' WHERE id = '$cat_id' AND
202 owner_uid = " . $_SESSION["uid"]);
203 }
204 }
205
206 return;
207 break;
208
209 }
210
211 outputFeedList($link, $tags);
212 break; // feeds
213
214 case "view":
215
216 $id = db_escape_string($_REQUEST["id"]);
217 $cids = split(",", db_escape_string($_REQUEST["cids"]));
218 $mode = db_escape_string($_REQUEST["mode"]);
219 $omode = db_escape_string($_REQUEST["omode"]);
220
221 if ($mode != "zoom") print "<reply>";
222
223 // in prefetch mode we only output requested cids, main article
224 // just gets marked as read (it already exists in client cache)
225
226 if ($mode == "") {
227 outputArticleXML($link, $id, false);
228 } else if ($mode == "zoom") {
229 outputArticleXML($link, $id, false, true, true);
230 } else {
231 catchupArticleById($link, $id, 0);
232 }
233
234 if (!$_SESSION["bw_limit"]) {
235 foreach ($cids as $cid) {
236 if ($cid) {
237 outputArticleXML($link, $cid, false, false);
238 }
239 }
240 }
241
242 /* if ($mode == "prefetch") {
243 print "<counters><![CDATA[";
244 print json_encode(getAllCounters($link, $omode));
245 print "]]></counters>";
246 } */
247
248 if ($mode != "zoom") print "</reply>";
249 break; // view
250
251 case "viewfeed":
252
253 $print_exec_time = true;
254 $timing_info = getmicrotime();
255
256 print "<reply>";
257
258 if ($_REQUEST["debug"]) $timing_info = print_checkpoint("0", $timing_info);
259
260 $omode = db_escape_string($_REQUEST["omode"]);
261
262 $feed = db_escape_string($_REQUEST["feed"]);
263 $subop = db_escape_string($_REQUEST["subop"]);
264 $view_mode = db_escape_string($_REQUEST["view_mode"]);
265 $limit = (int) get_pref($link, "DEFAULT_ARTICLE_LIMIT");
266 @$cat_view = db_escape_string($_REQUEST["cat"]);
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"]);
270 $order_by = db_escape_string($_REQUEST["order_by"]);
271
272 /* Feed -5 is a special case: it is used to display auxiliary information
273 * when there's nothing to load - e.g. no stuff in fresh feed */
274
275 if ($feed == -5) {
276 generate_dashboard_feed($link);
277 print "</reply>";
278 return;
279 }
280
281 /* Updating a label ccache means recalculating all of the caches
282 * so for performance reasons we don't do that here */
283
284 if ($feed >= 0) {
285 ccache_update($link, $feed, $_SESSION["uid"], $cat_view);
286 }
287
288 set_pref($link, "_DEFAULT_VIEW_MODE", $view_mode);
289 set_pref($link, "_DEFAULT_VIEW_LIMIT", $limit);
290 set_pref($link, "_DEFAULT_VIEW_ORDER_BY", $order_by);
291
292 if (!$cat_view && preg_match("/^[0-9][0-9]*$/", $feed)) {
293 db_query($link, "UPDATE ttrss_feeds SET last_viewed = NOW()
294 WHERE id = '$feed' AND owner_uid = ".$_SESSION["uid"]);
295 }
296
297 if (!$next_unread_feed) {
298 print "<headlines id=\"$feed\" is_cat=\"$cat_view\"><![CDATA[";
299 } else {
300 print "<headlines id=\"$next_unread_feed\" is_cat=\"$cat_view\"><![CDATA[";
301 }
302
303 $override_order = false;
304
305 if (get_pref($link, "SORT_HEADLINES_BY_FEED_DATE", $owner_uid)) {
306 $date_sort_field = "updated";
307 } else {
308 $date_sort_field = "date_entered";
309 }
310
311 switch ($order_by) {
312 case "date":
313 if (get_pref($link, 'REVERSE_HEADLINES', $owner_uid)) {
314 $override_order = "$date_sort_field";
315 } else {
316 $override_order = "$date_sort_field DESC";
317 }
318 break;
319
320 case "title":
321 if (get_pref($link, 'REVERSE_HEADLINES', $owner_uid)) {
322 $override_order = "title DESC, $date_sort_field";
323 } else {
324 $override_order = "title, $date_sort_field DESC";
325 }
326 break;
327
328 case "score":
329 if (get_pref($link, 'REVERSE_HEADLINES', $owner_uid)) {
330 $override_order = "score, $date_sort_field";
331 } else {
332 $override_order = "score DESC, $date_sort_field DESC";
333 }
334 break;
335 }
336
337 if ($_REQUEST["debug"]) $timing_info = print_checkpoint("04", $timing_info);
338
339 $ret = outputHeadlinesList($link, $feed, $subop,
340 $view_mode, $limit, $cat_view, $next_unread_feed, $offset,
341 $vgroup_last_feed, $override_order);
342
343 $topmost_article_ids = $ret[0];
344 $headlines_count = $ret[1];
345 $returned_feed = $ret[2];
346 $disable_cache = $ret[3];
347 $vgroup_last_feed = $ret[4];
348
349 print "]]></headlines>";
350
351 if ($_REQUEST["debug"]) $timing_info = print_checkpoint("05", $timing_info);
352
353 //print "<headlines-count value=\"$headlines_count\"/>";
354 //print "<vgroup-last-feed value=\"$vgroup_last_feed\"/>";
355
356 $headlines_unread = ccache_find($link, $returned_feed, $_SESSION["uid"],
357 $cat_view, true);
358
359 if ($headlines_unread == -1) {
360 $headlines_unread = getFeedUnread($link, $returned_feed, $cat_view);
361
362 }
363
364 //print "<headlines-unread value=\"$headlines_unread\"/>";
365 //printf("<disable-cache value=\"%d\"/>", $disable_cache);
366
367 print "<headlines-info><![CDATA[";
368
369 $info = array("count" => (int) $headlines_count,
370 "vgroup_last_feed" => $vgroup_last_feed,
371 "unread" => (int) $headlines_unread,
372 "disable_cache" => (bool) $disable_cache);
373
374 print json_encode($info);
375
376 print "]]></headlines-info>";
377
378 if ($_REQUEST["debug"]) $timing_info = print_checkpoint("10", $timing_info);
379
380 if (is_array($topmost_article_ids) && !get_pref($link, 'COMBINED_DISPLAY_MODE') && !$_SESSION["bw_limit"]) {
381 print "<articles>";
382 foreach ($topmost_article_ids as $id) {
383 outputArticleXML($link, $id, $feed, false);
384 }
385 print "</articles>";
386 }
387
388 if ($_REQUEST["debug"]) $timing_info = print_checkpoint("20", $timing_info);
389
390 if (get_pref($link, 'COMBINED_DISPLAY_MODE') || $subop) {
391 print "<counters><![CDATA[";
392 print json_encode(getAllCounters($link, $omode, $feed));
393 print "]]></counters>";
394 }
395
396 if ($_REQUEST["debug"]) $timing_info = print_checkpoint("30", $timing_info);
397
398 print_runtime_info($link);
399
400 print "</reply>";
401 break; // viewfeed
402
403 case "pref-feeds":
404 module_pref_feeds($link);
405 break; // pref-feeds
406
407 case "pref-filters":
408 module_pref_filters($link);
409 break; // pref-filters
410
411 case "pref-labels":
412 module_pref_labels($link);
413 break; // pref-labels
414
415 case "pref-prefs":
416 module_pref_prefs($link);
417 break; // pref-prefs
418
419 case "pref-users":
420 module_pref_users($link);
421 break; // prefs-users
422
423 case "help":
424 module_help($link);
425 break; // help
426
427 case "dlg":
428 module_popup_dialog($link);
429 break; // dlg
430
431 case "pref-pub-items":
432 module_pref_pub_items($link);
433 break; // pref-pub-items
434
435 case "globalUpdateFeeds":
436 // update feeds of all users, may be used anonymously
437
438 print "<!--";
439 // Update all feeds needing a update.
440 update_daemon_common($link, 0, true, true);
441 print " -->";
442
443 print "<rpc-reply>
444 <message msg=\"All feeds updated\"/>
445 </rpc-reply>";
446 break; // globalUpdateFeeds
447
448 case "pref-feed-browser":
449 module_pref_feed_browser($link);
450 break; // pref-feed-browser
451
452 case "rss":
453 $feed = db_escape_string($_REQUEST["id"]);
454 $key = db_escape_string($_REQUEST["key"]);
455 $is_cat = $_REQUEST["is_cat"] != false;
456 $limit = (int)db_escape_string($_REQUEST["limit"]);
457
458 $search = db_escape_string($_REQUEST["q"]);
459 $match_on = db_escape_string($_REQUEST["m"]);
460 $search_mode = db_escape_string($_REQUEST["smode"]);
461 $view_mode = db_escape_string($_REQUEST["view-mode"]);
462
463 if (SINGLE_USER_MODE) {
464 authenticate_user($link, "admin", null);
465 }
466
467 if ($key && !$_SESSION["uid"]) {
468 $result = db_query($link, "SELECT owner_uid FROM
469 ttrss_access_keys WHERE access_key = '$key' AND feed_id = '$feed'");
470
471 if (db_num_rows($result) == 1)
472 $_SESSION["uid"] = db_fetch_result($result, 0, "owner_uid");
473
474 }
475
476 if ($_SESSION["uid"]) {
477 generate_syndicated_feed($link, 0, $feed, $is_cat, $limit,
478 $search, $search_mode, $match_on, $view_mode);
479 }
480 break; // rss
481
482 case "getUnread":
483 $login = db_escape_string($_REQUEST["login"]);
484 $fresh = $_REQUEST["fresh"] == "1";
485
486 header("Content-Type: text/plain; charset=utf-8");
487
488 $result = db_query($link, "SELECT id FROM ttrss_users WHERE login = '$login'");
489
490 if (db_num_rows($result) == 1) {
491 $uid = db_fetch_result($result, 0, "id");
492
493 print getGlobalUnread($link, $uid);
494
495 if ($fresh) {
496 print ";";
497 print getFeedArticles($link, -3, false, true, $uid);
498 }
499
500 } else {
501 print "-1;User not found";
502 }
503
504 $print_exec_time = false;
505 break; // getUnread
506
507 case "digestTest":
508 header("Content-Type: text/plain");
509 print_r(prepare_headlines_digest($link, $_SESSION["uid"]));
510 $print_exec_time = false;
511 break; // digestTest
512
513 case "digestSend":
514 header("Content-Type: text/plain");
515 send_headlines_digests($link);
516 $print_exec_time = false;
517 break; // digestSend
518
519 case "getProfiles":
520 $login = db_escape_string($_REQUEST["login"]);
521 $password = db_escape_string($_REQUEST["password"]);
522
523 if (authenticate_user($link, $login, $password)) {
524 $result = db_query($link, "SELECT * FROM ttrss_settings_profiles
525 WHERE owner_uid = " . $_SESSION["uid"] . " ORDER BY title");
526
527 print "<select style='width: 100%' name='profile'>";
528
529 print "<option value='0'>" . __("Default profile") . "</option>";
530
531 while ($line = db_fetch_assoc($result)) {
532 $id = $line["id"];
533 $title = $line["title"];
534
535 print "<option value='$id'>$title</option>";
536 }
537
538 print "</select>";
539
540 $_SESSION = array();
541 }
542 break;
543
544 } // Select action according to $op value.
545
546 // We close the connection to database.
547 db_close($link);
548 ?>
549
550 <?php if ($print_exec_time) { ?>
551 <!-- <?php echo sprintf("Backend execution time: %.4f seconds", getmicrotime() - $script_started) ?> -->
552 <?php } ?>