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