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