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