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