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