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