]> git.wh0rd.org Git - tt-rss.git/blob - backend.php
do not reference gettext modules if ENABLE_TRANSLATIONS is disabled
[tt-rss.git] / backend.php
1 <?php
2         error_reporting(E_ERROR | E_WARNING | E_PARSE);
3
4         require_once "sessions.php";
5         require_once "modules/backend-rpc.php";
6
7 /*      if ($_GET["debug"]) {
8                 define('DEFAULT_ERROR_LEVEL', E_ALL);
9         } else {
10                 define('DEFAULT_ERROR_LEVEL', E_ERROR | E_WARNING | E_PARSE);
11         }
12         
13         error_reporting(DEFAULT_ERROR_LEVEL); */
14
15         require_once "sanity_check.php";
16         require_once "config.php";
17         
18         require_once "db.php";
19         require_once "db-prefs.php";
20         require_once "functions.php";
21
22         no_cache_incantation();
23
24         if (ENABLE_TRANSLATIONS == true) { 
25                 startup_gettext();
26         }
27
28         $script_started = getmicrotime();
29
30         $link = db_connect(DB_HOST, DB_USER, DB_PASS, DB_NAME); 
31
32         if (!$link) {
33                 if (DB_TYPE == "mysql") {
34                         print mysql_error();
35                 }
36                 // PG seems to display its own errors just fine by default.             
37                 return;
38         }
39
40         if (DB_TYPE == "pgsql") {
41                 pg_query("set client_encoding = 'UTF-8'");
42                 pg_set_client_encoding("UNICODE");
43         }
44
45         $op = $_REQUEST["op"];
46
47         $print_exec_time = false;
48
49         if ((!$op || $op == "rpc" || $op == "rss" || $op == "view" || 
50                         $op == "digestSend" || $op == "viewfeed" ||
51                         $op == "globalUpdateFeeds") && !$_REQUEST["noxml"]) {
52                 header("Content-Type: application/xml; charset=utf-8");
53         } else {
54                 header("Content-Type: text/html; charset=utf-8");
55         }
56
57         if (!$op) {
58                 header("Content-Type: application/xml");
59                 print_error_xml(7); exit;
60         }
61         
62         if (!($_SESSION["uid"] && validate_session($link)) && $op != "globalUpdateFeeds" 
63                         && $op != "rss" && $op != "getUnread") {
64
65                 if ($op == "rpc") {
66                         print_error_xml(6); die;
67                 } else {
68                         print "
69                         <html><body>
70                                 <p>Error: Not logged in.</p>
71                                 <script type=\"text/javascript\">
72                                         if (parent.window != 'undefined') {
73                                                 parent.window.location = \"tt-rss.php\";                
74                                         } else {
75                                                 window.location = \"tt-rss.php\";
76                                         }
77                                 </script>
78                         </body></html>
79                         ";
80                 }
81                 exit;
82         }
83
84         $purge_intervals = array(
85                 0  => __("Use default"),
86                 -1 => __("Never purge"),
87                 5  => __("1 week old"),
88                 14 => __("2 weeks old"),
89                 31 => __("1 month old"),
90                 60 => __("2 months old"),
91                 90 => __("3 months old"));
92
93         $update_intervals = array(
94                 0   => __("Use default"),
95                 -1  => __("Disable updates"),
96                 30  => __("Each 30 minutes"),
97                 60  => __("Hourly"),
98                 240 => __("Each 4 hours"),
99                 720 => __("Each 12 hours"),
100                 1440 => __("Daily"),
101                 10080 => __("Weekly"));
102
103
104         $access_level_names = array(
105                 0 => __("User"), 
106                 10 => __("Administrator"));
107
108         require_once "modules/pref-prefs.php";
109         require_once "modules/popup-dialog.php";
110         require_once "modules/help.php";
111         require_once "modules/pref-feeds.php";
112         require_once "modules/pref-filters.php";
113         require_once "modules/pref-labels.php";
114         require_once "modules/pref-users.php";
115         require_once "modules/pref-feed-browser.php"; 
116
117
118         if (!sanity_check($link)) { return; }
119
120         if ($op == "rpc") {
121                 handle_rpc_request($link);
122         }
123         
124         if ($op == "feeds") {
125
126                 $tags = $_GET["tags"];
127
128                 $subop = $_GET["subop"];
129
130                 if ($subop == "catchupAll") {
131                         db_query($link, "UPDATE ttrss_user_entries SET 
132                                 last_read = NOW(),unread = false WHERE owner_uid = " . $_SESSION["uid"]);
133                 }
134
135                 if ($subop == "collapse") {
136                         $cat_id = db_escape_string($_GET["cid"]);
137
138                         db_query($link, "UPDATE ttrss_feed_categories SET
139                                 collapsed = NOT collapsed WHERE id = '$cat_id' AND owner_uid = " . 
140                                 $_SESSION["uid"]);
141                         return;
142                 }
143
144                 outputFeedList($link, $tags);
145
146         }
147
148         if ($op == "view") {
149
150                 $id = db_escape_string($_GET["id"]);
151                 $feed_id = db_escape_string($_GET["feed"]);
152                 $cids = split(",", db_escape_string($_GET["cids"]));
153                 $mode = db_escape_string($_GET["mode"]);
154                 $omode = db_escape_string($_GET["omode"]);
155
156                 print "<reply>";
157
158                 // in prefetch mode we only output requested cids, main article 
159                 // just gets marked as read (it already exists in client cache)
160
161                 if ($mode == "") {
162                         outputArticleXML($link, $id, $feed_id);
163                 } else {
164                         catchupArticleById($link, $id, 0);
165                 }
166
167                 foreach ($cids as $cid) {
168                         if ($cid) {
169                                 outputArticleXML($link, $cid, $feed_id, false);
170                         }
171                 }
172
173                 if ($mode != "prefetch_old") {
174                         print "<counters>";
175                         getAllCounters($link, $omode);
176                         print "</counters>";
177                 }
178
179                 print "</reply>";
180         }
181
182         if ($op == "viewfeed") {
183
184                 $print_exec_time = true;
185                 $timing_info = getmicrotime();
186
187                 print "<reply>";
188
189                 if ($_GET["debug"]) $timing_info = print_checkpoint("0", $timing_info);
190
191                 $omode = db_escape_string($_GET["omode"]);
192
193                 $feed = db_escape_string($_GET["feed"]);
194                 $subop = db_escape_string($_GET["subop"]);
195                 $view_mode = db_escape_string($_GET["view_mode"]);
196                 $limit = db_escape_string($_GET["limit"]);
197                 $cat_view = db_escape_string($_GET["cat"]);
198                 $next_unread_feed = db_escape_string($_GET["nuf"]);
199                 $offset = db_escape_string($_GET["skip"]);
200
201                 set_pref($link, "_DEFAULT_VIEW_MODE", $view_mode);
202                 set_pref($link, "_DEFAULT_VIEW_LIMIT", $limit);
203
204                 print "<headlines id=\"$feed\"><![CDATA[";
205
206                 $topmost_article_ids = outputHeadlinesList($link, $feed, $subop, 
207                         $view_mode, $limit, $cat_view, $next_unread_feed, $offset);
208
209                 print "]]></headlines>";
210
211                 if ($_GET["debug"]) $timing_info = print_checkpoint("10", $timing_info);
212
213                 if (is_array($topmost_article_ids) && !get_pref($link, 'COMBINED_DISPLAY_MODE')) {
214                         print "<articles>";
215                         foreach ($topmost_article_ids as $id) {
216                                 outputArticleXML($link, $id, $feed, false);
217                         }
218                         print "</articles>";
219                 }
220
221                 if ($_GET["debug"]) $timing_info = print_checkpoint("20", $timing_info);
222
223                 print "<counters>";
224                 getAllCounters($link, $omode);
225                 print "</counters>";
226
227                 if ($_GET["debug"]) $timing_info = print_checkpoint("30", $timing_info);
228
229                 print "</reply>";
230         }
231
232         if ($op == "pref-feeds") {
233                 module_pref_feeds($link);
234         }
235
236         if ($op == "pref-filters") {
237                 module_pref_filters($link);
238         }
239
240         if ($op == "pref-labels") {
241                 module_pref_labels($link);
242         }
243
244         if ($op == "pref-prefs") {
245                 module_pref_prefs($link);
246         }
247
248         if ($op == "pref-users") {
249                 module_pref_users($link);
250         }
251
252         if ($op == "help") {
253                 module_help($link);
254         }
255
256         if ($op == "dlg") {
257                 module_popup_dialog($link);
258         }
259
260         // update feeds of all users, may be used anonymously
261         if ($op == "globalUpdateFeeds") {
262
263                 $result = db_query($link, "SELECT id FROM ttrss_users");
264
265                 while ($line = db_fetch_assoc($result)) {
266                         $user_id = $line["id"];
267 //                      print "<!-- updating feeds of uid $user_id -->";
268                         update_all_feeds($link, false, $user_id);
269                 }
270
271                 print "<rpc-reply>
272                         <message msg=\"All feeds updated\"/>
273                 </rpc-reply>";
274
275         }
276
277         if ($op == "user-details") {
278
279                 if (WEB_DEMO_MODE || $_SESSION["access_level"] < 10) {
280                         return;
281                 }
282                           
283 /*              print "<html><head>
284                         <title>Tiny Tiny RSS : User Details</title>
285                         <link rel=\"stylesheet\" href=\"tt-rss.css\" type=\"text/css\">
286                         <meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">
287                         </head><body>"; */
288
289                 $uid = sprintf("%d", $_GET["id"]);
290
291                 print "<div id=\"infoBoxTitle\">User details</div>";
292
293                 print "<div class='infoBoxContents'>";
294
295                 $result = db_query($link, "SELECT login,
296                         SUBSTRING(last_login,1,16) AS last_login,
297                         access_level,
298                         (SELECT COUNT(int_id) FROM ttrss_user_entries 
299                                 WHERE owner_uid = id) AS stored_articles
300                         FROM ttrss_users 
301                         WHERE id = '$uid'");
302                         
303                 if (db_num_rows($result) == 0) {
304                         print "<h1>User not found</h1>";
305                         return;
306                 }
307                 
308 #               print "<h1>User Details</h1>";
309
310                 $login = db_fetch_result($result, 0, "login");
311
312 #               print "<h1>$login</h1>";
313
314                 print "<table width='100%'>";
315
316                 $last_login = date(get_pref($link, 'LONG_DATE_FORMAT'),
317                         strtotime(db_fetch_result($result, 0, "last_login")));
318                 $access_level = db_fetch_result($result, 0, "access_level");
319                 $stored_articles = db_fetch_result($result, 0, "stored_articles");
320
321 #               print "<tr><td>Username</td><td>$login</td></tr>";
322 #               print "<tr><td>Access level</td><td>$access_level</td></tr>";
323                 print "<tr><td>Last logged in</td><td>$last_login</td></tr>";
324                 print "<tr><td>Stored articles</td><td>$stored_articles</td></tr>";
325
326                 $result = db_query($link, "SELECT COUNT(id) as num_feeds FROM ttrss_feeds
327                         WHERE owner_uid = '$uid'");
328
329                 $num_feeds = db_fetch_result($result, 0, "num_feeds");
330
331                 print "<tr><td>Subscribed feeds count</td><td>$num_feeds</td></tr>";
332
333 /*              $result = db_query($link, "SELECT 
334                         SUM(LENGTH(content)+LENGTH(title)+LENGTH(link)+LENGTH(guid)) AS db_size 
335                         FROM ttrss_user_entries,ttrss_entries 
336                                 WHERE owner_uid = '$uid' AND ref_id = id");
337
338                 $db_size = round(db_fetch_result($result, 0, "db_size") / 1024);
339
340                 print "<tr><td>Approx. used DB size</td><td>$db_size KBytes</td></tr>";  */
341
342                 print "</table>";
343
344                 print "<h1>Subscribed feeds</h1>";
345
346                 $result = db_query($link, "SELECT id,title,site_url FROM ttrss_feeds
347                         WHERE owner_uid = '$uid' ORDER BY title");
348
349                 print "<ul class=\"userFeedList\">";
350
351                 $row_class = "odd";
352
353                 while ($line = db_fetch_assoc($result)) {
354
355                         $icon_file = ICONS_URL."/".$line["id"].".ico";
356
357                         if (file_exists($icon_file) && filesize($icon_file) > 0) {
358                                 $feed_icon = "<img class=\"tinyFeedIcon\" src=\"$icon_file\">";
359                         } else {
360                                 $feed_icon = "<img class=\"tinyFeedIcon\" src=\"images/blank_icon.gif\">";
361                         }
362
363                         print "<li class=\"$row_class\">$feed_icon&nbsp;<a href=\"".$line["site_url"]."\">".$line["title"]."</a></li>";
364
365                         $row_class = toggleEvenOdd($row_class);
366
367                 }
368
369                 if (db_num_rows($result) < $num_feeds) {
370                          // FIXME - add link to show ALL subscribed feeds here somewhere
371                         print "<li><img 
372                                 class=\"tinyFeedIcon\" src=\"images/blank_icon.gif\">&nbsp;...</li>";
373                 }
374                 
375                 print "</ul>";
376
377                 print "</div>";
378
379                 print "<div align='center'>
380                         <input type='submit' class='button'                     
381                         onclick=\"closeInfoBox()\" value=\"Close this window\"></div>";
382
383 //              print "</body></html>"; 
384
385         }
386
387         if ($op == "pref-feed-browser") {
388                 module_pref_feed_browser($link);
389         }
390
391         if ($op == "rss") {
392                 $feed = db_escape_string($_GET["id"]);
393                 $user = db_escape_string($_GET["user"]);
394                 $pass = db_escape_string($_GET["pass"]);
395                 $is_cat = $_GET["is_cat"] != false;
396
397                 $search = db_escape_string($_GET["q"]);
398                 $match_on = db_escape_string($_GET["m"]);
399                 $search_mode = db_escape_string($_GET["smode"]);
400
401                 if (!$_SESSION["uid"] && $user && $pass) {
402                         authenticate_user($link, $user, $pass);
403                 }
404
405                 if ($_SESSION["uid"] ||
406                         http_authenticate_user($link)) {
407
408                                 generate_syndicated_feed($link, $feed, $is_cat, 
409                                         $search, $search_mode, $match_on);
410                 }
411         }
412
413         if ($op == "labelFromSearch") {
414                 $search = db_escape_string($_GET["search"]);
415                 $search_mode = db_escape_string($_GET["smode"]);
416                 $match_on = db_escape_string($_GET["match"]);
417                 $is_cat = db_escape_string($_GET["is_cat"]);
418                 $title = db_escape_string($_GET["title"]);
419                 $feed = sprintf("%d", $_GET["feed"]);
420
421                 $label_qparts = array();
422
423                 $search_expr = getSearchSql($search, $match_on);
424
425                 if ($is_cat) {
426                         if ($feed != 0) {
427                                 $search_expr .= " AND ttrss_feeds.cat_id = $feed ";
428                         } else {
429                                 $search_expr .= " AND ttrss_feeds.cat_id IS NULL ";
430                         }
431                 } else {
432                         if ($search_mode == "all_feeds") {
433                                 // NOOP
434                         } else if ($search_mode == "this_cat") {
435
436                                 $tmp_result = db_query($link, "SELECT cat_id
437                                         FROM ttrss_feeds WHERE id = '$feed'");
438
439                                 $cat_id = db_fetch_result($tmp_result, 0, "cat_id");
440
441                                 if ($cat_id > 0) {
442                                         $search_expr .= " AND ttrss_feeds.cat_id = $cat_id ";
443                                 } else {
444                                         $search_expr .= " AND ttrss_feeds.cat_id IS NULL ";
445                                 }
446                         } else {
447                                 $search_expr .= " AND ttrss_feeds.id = $feed ";
448                         }
449
450                 }
451
452                 $search_expr = db_escape_string($search_expr);
453
454                 print $search_expr;
455
456                 if ($title) {
457                         $result = db_query($link,
458                                 "INSERT INTO ttrss_labels (sql_exp,description,owner_uid) 
459                                 VALUES ('$search_expr', '$title', '".$_SESSION["uid"]."')");
460                 }
461         }
462
463         if ($op == "getUnread") {
464                 $login = db_escape_string($_GET["login"]);
465
466                 header("Content-Type: text/plain; charset=utf-8");
467
468                 $result = db_query($link, "SELECT id FROM ttrss_users WHERE login = '$login'");
469
470                 if (db_num_rows($result) == 1) {
471                         $uid = db_fetch_result($result, 0, "id");
472                         print getGlobalUnread($link, $uid);
473                 } else {
474                         print "-1;User not found";
475                 }
476
477                 $print_exec_time = false;
478         }
479
480         if ($op == "digestTest") {
481                 header("Content-Type: text/plain");
482                 print_r(prepare_headlines_digest($link, $_SESSION["uid"]));
483                 $print_exec_time = false;
484
485         }
486
487         if ($op == "digestSend") {
488                 header("Content-Type: text/plain");
489                 send_headlines_digests($link);
490                 $print_exec_time = false;
491
492         }
493
494         db_close($link);
495 ?>
496
497 <?php if ($print_exec_time) { ?>
498 <!-- <?php echo sprintf("Backend execution time: %.4f seconds", getmicrotime() - $script_started) ?> -->
499 <?php } ?>