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