]> git.wh0rd.org - tt-rss.git/blob - backend.php
Merge branch 'master' of /home/fox/public_html/testbox/tt-rss
[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 $_GET = array_map('stripslashes', $_GET);
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 ($_GET["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 = $_GET["tags"];
163
164 $subop = $_GET["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($_GET["cid"]);
176
177 db_query($link, "UPDATE ttrss_feed_categories SET
178 collapsed = NOT collapsed WHERE id = '$cat_id' AND owner_uid = " .
179 $_SESSION["uid"]);
180 return;
181 break;
182
183 case "catsortreset":
184 db_query($link, "UPDATE ttrss_feed_categories
185 SET order_id = 0 WHERE owner_uid = " . $_SESSION["uid"]);
186 return;
187 break;
188
189 case "catsort":
190 $corder = db_escape_string($_GET["corder"]);
191
192 $cats = split(",", $corder);
193
194 for ($i = 0; $i < count($cats); $i++) {
195 $cat_id = $cats[$i];
196
197 if ($cat_id > 0) {
198 db_query($link, "UPDATE ttrss_feed_categories
199 SET order_id = '$i' WHERE id = '$cat_id' AND
200 owner_uid = " . $_SESSION["uid"]);
201 }
202 }
203
204 return;
205 break;
206
207 }
208
209 $_SESSION["viewfeed:counters_stamp"] = time();
210
211 outputFeedList($link, $tags);
212 break; // feeds
213
214 case "view":
215
216 $id = db_escape_string($_GET["id"]);
217 $feed_id = db_escape_string($_GET["feed"]);
218 $cids = split(",", db_escape_string($_GET["cids"]));
219 $mode = db_escape_string($_GET["mode"]);
220 $omode = db_escape_string($_GET["omode"]);
221
222 $csync = $_GET["csync"];
223
224 print "<reply>";
225
226 // in prefetch mode we only output requested cids, main article
227 // just gets marked as read (it already exists in client cache)
228
229 if ($mode == "") {
230 outputArticleXML($link, $id, $feed_id);
231 } else if ($mode == "zoom") {
232 outputArticleXML($link, $id, $feed_id, true, true);
233 } else {
234 catchupArticleById($link, $id, 0);
235 ccache_update($link, $feed_id, $_SESSION["uid"]);
236 }
237
238 if (!$_SESSION["bw_limit"]) {
239 foreach ($cids as $cid) {
240 if ($cid) {
241 outputArticleXML($link, $cid, $feed_id, false);
242 }
243 }
244 }
245
246 // if (get_pref($link, "SYNC_COUNTERS") || ($mode == "prefetch" && $csync)) {
247
248 if (time() - $_SESSION["view:counters_stamp"] > 3 && $mode == "prefetch") {
249 print "<counters>";
250 getAllCounters($link, $omode);
251 print "</counters>";
252 $_SESSION["view:counters_stamp"] = time();
253 }
254
255 print "</reply>";
256 break; // view
257
258 case "viewfeed":
259
260 $print_exec_time = true;
261 $timing_info = getmicrotime();
262
263 print "<reply>";
264
265 if ($_GET["debug"]) $timing_info = print_checkpoint("0", $timing_info);
266
267 $omode = db_escape_string($_GET["omode"]);
268
269 $feed = db_escape_string($_GET["feed"]);
270 $subop = db_escape_string($_GET["subop"]);
271 $view_mode = db_escape_string($_GET["view_mode"]);
272 $limit = db_escape_string($_GET["limit"]);
273 $cat_view = db_escape_string($_GET["cat"]);
274 $next_unread_feed = db_escape_string($_GET["nuf"]);
275 $offset = db_escape_string($_GET["skip"]);
276 $vgroup_last_feed = db_escape_string($_GET["vgrlf"]);
277 $csync = $_GET["csync"];
278 $order_by = db_escape_string($_GET["order_by"]);
279
280 /* Updating a label ccache means recalculating all of the caches
281 * so for performance reasons we don't do that here */
282
283 // if (time() - $_SESSION["viewfeed:ccache_update_stamp"] > 120) {
284 if ($feed >= 0) {
285 ccache_update($link, $feed, $_SESSION["uid"], $cat_view);
286 }
287 $_SESSION["viewfeed:ccache_update_stamp"] = time();
288 // }
289
290 set_pref($link, "_DEFAULT_VIEW_MODE", $view_mode);
291 set_pref($link, "_DEFAULT_VIEW_LIMIT", $limit);
292 set_pref($link, "_DEFAULT_VIEW_ORDER_BY", $order_by);
293
294 if (!$cat_view && preg_match("/^[0-9][0-9]*$/", $feed)) {
295 db_query($link, "UPDATE ttrss_feeds SET last_viewed = NOW()
296 WHERE id = '$feed' AND owner_uid = ".$_SESSION["uid"]);
297 }
298
299 if (!$next_unread_feed) {
300 print "<headlines id=\"$feed\" is_cat=\"$cat_view\"><![CDATA[";
301 } else {
302 print "<headlines id=\"$next_unread_feed\" is_cat=\"$cat_view\"><![CDATA[";
303 }
304
305 $override_order = false;
306
307 switch ($order_by) {
308 case "date":
309 if (get_pref($link, 'REVERSE_HEADLINES', $owner_uid)) {
310 $override_order = "updated";
311 } else {
312 $override_order = "updated DESC";
313 }
314 break;
315
316 case "title":
317 $override_order = "updated DESC";
318 break;
319
320 case "score":
321 $override_order = "score DESC";
322 break;
323 }
324
325 $ret = outputHeadlinesList($link, $feed, $subop,
326 $view_mode, $limit, $cat_view, $next_unread_feed, $offset,
327 $vgroup_last_feed, $override_order);
328
329 $topmost_article_ids = $ret[0];
330 $headlines_count = $ret[1];
331 $returned_feed = $ret[2];
332 $disable_cache = $ret[3];
333 $vgroup_last_feed = $ret[4];
334
335 print "]]></headlines>";
336
337 print "<headlines-count value=\"$headlines_count\"/>";
338 print "<vgroup-last-feed value=\"$vgroup_last_feed\"/>";
339
340 $headlines_unread = ccache_find($link, $returned_feed, $_SESSION["uid"],
341 $cat_view, true);
342
343 if ($headlines_unread == -1) {
344 $headlines_unread = getFeedUnread($link, $returned_feed, $cat_view);
345
346 }
347
348 print "<headlines-unread value=\"$headlines_unread\"/>";
349 printf("<disable-cache value=\"%d\"/>", $disable_cache);
350
351 if ($_GET["debug"]) $timing_info = print_checkpoint("10", $timing_info);
352
353 if (is_array($topmost_article_ids) && !get_pref($link, 'COMBINED_DISPLAY_MODE') && !$_SESSION["bw_limit"]) {
354 print "<articles>";
355 foreach ($topmost_article_ids as $id) {
356 outputArticleXML($link, $id, $feed, false);
357 }
358 print "</articles>";
359 }
360
361 if ($_GET["debug"]) $timing_info = print_checkpoint("20", $timing_info);
362
363
364 // if (get_pref($link, "SYNC_COUNTERS") ||
365 // time() - $_SESSION["get_all_counters_stamp"] > $viewfeed_ctr_interval) {
366 // print "<counters>";
367 // getAllCounters($link, $omode, $feed);
368 // print "</counters>";
369 // }
370
371 if (get_pref($link, 'COMBINED_DISPLAY_MODE') || $subop ||
372 time() - $_SESSION["viewfeed:counters_stamp"] > 60) {
373 if (!$offset) {
374 print "<counters>";
375 getAllCounters($link, $omode, $feed);
376 print "</counters>";
377 $_SESSION["viewfeed:counters_stamp"] = time();
378 }
379 }
380
381 if ($_GET["debug"]) $timing_info = print_checkpoint("30", $timing_info);
382
383 print_runtime_info($link);
384
385 print "</reply>";
386 break; // viewfeed
387
388 case "pref-feeds":
389 module_pref_feeds($link);
390 break; // pref-feeds
391
392 case "pref-filters":
393 module_pref_filters($link);
394 break; // pref-filters
395
396 case "pref-labels":
397 module_pref_labels($link);
398 break; // pref-labels
399
400 case "pref-prefs":
401 module_pref_prefs($link);
402 break; // pref-prefs
403
404 case "pref-users":
405 module_pref_users($link);
406 break; // prefs-users
407
408 case "help":
409 module_help($link);
410 break; // help
411
412 case "dlg":
413 module_popup_dialog($link);
414 break; // dlg
415
416 case "pref-pub-items":
417 module_pref_pub_items($link);
418 break; // pref-pub-items
419
420 case "globalUpdateFeeds":
421 // update feeds of all users, may be used anonymously
422
423 print "<!--";
424 // Update all feeds needing a update.
425 update_daemon_common($link, 0, true, true);
426 print " -->";
427
428 print "<rpc-reply>
429 <message msg=\"All feeds updated\"/>
430 </rpc-reply>";
431 break; // globalUpdateFeeds
432
433 case "pref-feed-browser":
434 module_pref_feed_browser($link);
435 break; // pref-feed-browser
436
437 case "publish":
438 $key = db_escape_string($_GET["key"]);
439 $limit = (int)db_escape_string($_GET["limit"]);
440
441 $result = db_query($link, "SELECT login, owner_uid
442 FROM ttrss_user_prefs, ttrss_users WHERE
443 pref_name = '_PREFS_PUBLISH_KEY' AND
444 value = '$key' AND
445 ttrss_users.id = owner_uid");
446
447 if (db_num_rows($result) == 1) {
448 $owner = db_fetch_result($result, 0, "owner_uid");
449 $login = db_fetch_result($result, 0, "login");
450
451 generate_syndicated_feed($link, $owner, -2, false, $limit);
452
453 } else {
454 print "<error>User not found</error>";
455 }
456 break; // publish
457
458 case "rss":
459 $feed = db_escape_string($_GET["id"]);
460 $user = db_escape_string($_GET["user"]);
461 $pass = db_escape_string($_GET["pass"]);
462 $is_cat = $_GET["is_cat"] != false;
463 $limit = (int)db_escape_string($_GET["limit"]);
464
465 $search = db_escape_string($_GET["q"]);
466 $match_on = db_escape_string($_GET["m"]);
467 $search_mode = db_escape_string($_GET["smode"]);
468
469 if (SINGLE_USER_MODE) {
470 authenticate_user($link, "admin", null);
471 }
472
473 if (!$_SESSION["uid"] && $user && $pass) {
474 authenticate_user($link, $user, $pass);
475 }
476
477 if ($_SESSION["uid"] ||
478 http_authenticate_user($link)) {
479
480 generate_syndicated_feed($link, 0, $feed, $is_cat, $limit,
481 $search, $search_mode, $match_on);
482 }
483 break; // rss
484
485 case "getUnread":
486 $login = db_escape_string($_GET["login"]);
487
488 header("Content-Type: text/plain; charset=utf-8");
489
490 $result = db_query($link, "SELECT id FROM ttrss_users WHERE login = '$login'");
491
492 if (db_num_rows($result) == 1) {
493 $uid = db_fetch_result($result, 0, "id");
494 print getGlobalUnread($link, $uid);
495 } else {
496 print "-1;User not found";
497 }
498
499 $print_exec_time = false;
500 break; // getUnread
501
502 case "digestTest":
503 header("Content-Type: text/plain");
504 print_r(prepare_headlines_digest($link, $_SESSION["uid"]));
505 $print_exec_time = false;
506 break; // digestTest
507
508 case "digestSend":
509 header("Content-Type: text/plain");
510 send_headlines_digests($link);
511 $print_exec_time = false;
512 break; // digestSend
513
514 } // Select action according to $op value.
515
516 // We close the connection to database.
517 db_close($link);
518 ?>
519
520 <?php if ($print_exec_time) { ?>
521 <!-- <?php echo sprintf("Backend execution time: %.4f seconds", getmicrotime() - $script_started) ?> -->
522 <?php } ?>