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