]> git.wh0rd.org - tt-rss.git/blob - backend.php
fix DAEMON_REFRESH_ONLY handling
[tt-rss.git] / backend.php
1 <?
2 $op = $_GET["op"];
3
4 if ($op == "rpc") {
5 header("Content-Type: application/xml");
6 }
7
8 require_once "config.php";
9 require_once "db.php";
10 require_once "functions.php";
11 require_once "magpierss/rss_fetch.inc";
12
13 $script_started = getmicrotime();
14
15 $link = db_connect(DB_HOST, DB_USER, DB_PASS, DB_NAME);
16
17 if (!$link) {
18 if (DB_TYPE == "mysql") {
19 print mysql_error();
20 }
21 // PG seems to display its own errors just fine by default.
22 return;
23 }
24
25 if (DB_TYPE == "pgsql") {
26 pg_query("set client_encoding = 'utf-8'");
27 }
28
29 $fetch = $_GET["fetch"];
30
31 /* FIXME this needs reworking */
32
33 function getGlobalCounters($link) {
34 $result = db_query($link, "SELECT count(id) as c_id FROM ttrss_entries
35 WHERE unread = true");
36 $c_id = db_fetch_result($result, 0, "c_id");
37 print "<counter id='global-unread' counter='$c_id'/>";
38 }
39
40 function getTagCounters($link) {
41 $result = db_query($link, "SELECT tag_name,count(ttrss_entries.id) AS count
42 FROM ttrss_tags,ttrss_entries WHERE
43 post_id = ttrss_entries.id AND unread = true GROUP BY tag_name
44 UNION
45 select tag_name,0 as count FROM ttrss_tags");
46
47 $tags = array();
48
49 while ($line = db_fetch_assoc($result)) {
50 $tags[$line["tag_name"]] += $line["count"];
51 }
52
53 foreach (array_keys($tags) as $tag) {
54 $unread = $tags[$tag];
55
56 $tag = htmlspecialchars($tag);
57 print "<tag id=\"$tag\" counter=\"$unread\"/>";
58 }
59 }
60
61 function getLabelCounters($link) {
62
63 $result = db_query($link, "SELECT count(id) as count FROM ttrss_entries
64 WHERE marked = true AND unread = true");
65
66 $count = db_fetch_result($result, 0, "count");
67
68 print "<label id=\"-1\" counter=\"$count\"/>";
69
70 $result = db_query($link, "SELECT id,sql_exp,description FROM
71 ttrss_labels ORDER by description");
72
73 while ($line = db_fetch_assoc($result)) {
74
75 $id = -$line["id"] - 11;
76
77 error_reporting (0);
78
79 $tmp_result = db_query($link, "SELECT count(id) as count FROM ttrss_entries
80 WHERE (" . $line["sql_exp"] . ") AND unread = true");
81
82 $count = db_fetch_result($tmp_result, 0, "count");
83
84 print "<label id=\"$id\" counter=\"$count\"/>";
85
86 error_reporting (E_ERROR | E_WARNING | E_PARSE);
87
88 }
89 }
90
91 function getFeedCounter($link, $id) {
92
93 $result = db_query($link, "SELECT
94 count(id) as count FROM ttrss_entries
95 WHERE feed_id = '$id' AND unread = true");
96
97 $count = db_fetch_result($result, 0, "count");
98
99 print "<feed id=\"$id\" counter=\"$count\"/>";
100 }
101
102 function getFeedCounters($link) {
103
104 $result = db_query($link, "SELECT id,
105 (SELECT count(id) FROM ttrss_entries WHERE feed_id = ttrss_feeds.id
106 AND unread = true) as count
107 FROM ttrss_feeds");
108
109 while ($line = db_fetch_assoc($result)) {
110
111 $id = $line["id"];
112 $count = $line["count"];
113
114 print "<feed id=\"$id\" counter=\"$count\"/>";
115 }
116 }
117
118 function outputFeedList($link, $tags = false) {
119
120 print "<html><head>
121 <title>Tiny Tiny RSS : Feedlist</title>
122 <link rel=\"stylesheet\" href=\"tt-rss.css\" type=\"text/css\">
123 <script type=\"text/javascript\" src=\"functions.js\"></script>
124 <script type=\"text/javascript\" src=\"feedlist.js\"></script>
125 <meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">
126 </head><body>";
127
128 print "<ul class=\"feedList\" id=\"feedList\">";
129
130 if (!$tags) {
131
132 /* virtual feeds */
133
134 $result = db_query($link, "SELECT count(id) as num_starred
135 FROM ttrss_entries WHERE marked = true AND unread = true");
136 $num_starred = db_fetch_result($result, 0, "num_starred");
137
138 $class = "odd";
139
140 if ($num_starred > 0) $class .= "Unread";
141
142 printFeedEntry(-1, $class, "Starred articles", $num_starred,
143 "images/mark_set.png");
144
145 if (ENABLE_LABELS) {
146
147 $result = db_query($link, "SELECT id,sql_exp,description FROM
148 ttrss_labels ORDER by description");
149
150 if (db_num_rows($result) > 0) {
151 print "<li><hr></li>";
152 }
153
154 while ($line = db_fetch_assoc($result)) {
155
156 error_reporting (0);
157
158 $tmp_result = db_query($link, "SELECT count(id) as count FROM ttrss_entries
159 WHERE (" . $line["sql_exp"] . ") AND unread = true");
160
161 $count = db_fetch_result($tmp_result, 0, "count");
162
163 $class = "odd";
164
165 if ($count > 0) {
166 $class .= "Unread";
167 }
168
169 error_reporting (E_ERROR | E_WARNING | E_PARSE);
170
171 printFeedEntry(-$line["id"]-11,
172 $class, $line["description"], $count, "images/label.png");
173
174 }
175 }
176
177 print "<li><hr></li>";
178
179 $result = db_query($link, "SELECT *,
180 (SELECT count(id) FROM ttrss_entries
181 WHERE feed_id = ttrss_feeds.id) AS total,
182 (SELECT count(id) FROM ttrss_entries
183 WHERE feed_id = ttrss_feeds.id AND unread = true) as unread
184 FROM ttrss_feeds ORDER BY title");
185
186 $actid = $_GET["actid"];
187
188 /* real feeds */
189
190 $lnum = 0;
191
192 $total_unread = 0;
193
194 while ($line = db_fetch_assoc($result)) {
195
196 $feed = $line["title"];
197 $feed_id = $line["id"];
198
199 $subop = $_GET["subop"];
200
201 $total = $line["total"];
202 $unread = $line["unread"];
203
204 // $class = ($lnum % 2) ? "even" : "odd";
205
206 $class = "odd";
207
208 if ($unread > 0) $class .= "Unread";
209
210 if ($actid == $feed_id) {
211 $class .= "Selected";
212 }
213
214 $total_unread += $unread;
215
216 printFeedEntry($feed_id, $class, $feed, $unread, "icons/$feed_id.ico");
217
218 ++$lnum;
219 }
220 } else {
221
222 // tags
223
224 $result = db_query($link, "SELECT tag_name,count(ttrss_entries.id) AS count
225 FROM ttrss_tags,ttrss_entries WHERE
226 post_id = ttrss_entries.id AND unread = true GROUP BY tag_name
227 UNION
228 select tag_name,0 as count FROM ttrss_tags");
229
230 $tags = array();
231
232 while ($line = db_fetch_assoc($result)) {
233 $tags[$line["tag_name"]] += $line["count"];
234 }
235
236 foreach (array_keys($tags) as $tag) {
237
238 $unread = $tags[$tag];
239
240 $class = "odd";
241
242 if ($unread > 0) {
243 $class .= "Unread";
244 }
245
246 printFeedEntry($tag, $class, $tag, $unread, "images/tag.png");
247
248 }
249
250 }
251
252 if (db_num_rows($result) == 0) {
253 print "<li>No tags to display.</li>";
254 }
255
256 print "</ul>";
257
258 print "<div class=\"invisible\" id=\"FEEDTU\">$total_unread</div>";
259
260 }
261
262
263 if ($op == "rpc") {
264
265 $subop = $_GET["subop"];
266
267 if ($subop == "getLabelCounters") {
268 $aid = $_GET["aid"];
269 print "<rpc-reply>";
270 getLabelCounters($link);
271 if ($aid) {
272 getFeedCounter($link, $aid);
273 }
274 print "</rpc-reply>";
275 }
276
277 if ($subop == "getFeedCounters") {
278 print "<rpc-reply>";
279 getFeedCounters($link);
280 print "</rpc-reply>";
281 }
282
283 if ($subop == "getAllCounters") {
284 print "<rpc-reply>";
285 getLabelCounters($link);
286 getFeedCounters($link);
287 getTagCounters($link);
288 getGlobalCounters($link);
289 print "</rpc-reply>";
290 }
291
292 if ($subop == "mark") {
293 $mark = $_GET["mark"];
294 $id = db_escape_string($_GET["id"]);
295
296 if ($mark == "1") {
297 $mark = "true";
298 } else {
299 $mark = "false";
300 }
301
302 $result = db_query($link, "UPDATE ttrss_entries SET marked = $mark
303 WHERE id = '$id'");
304 }
305
306 if ($subop == "updateFeed") {
307 $feed_id = db_escape_string($_GET["feed"]);
308
309 $result = db_query($link,
310 "SELECT feed_url FROM ttrss_feeds WHERE id = '$feed_id'");
311
312 if (db_num_rows($result) > 0) {
313 $feed_url = db_fetch_result($result, 0, "feed_url");
314 // update_rss_feed($link, $feed_url, $feed_id);
315 }
316
317 print "DONE-$feed_id";
318
319 return;
320 }
321
322 if ($subop == "forceUpdateAllFeeds" || $subop == "updateAllFeeds") {
323
324 update_all_feeds($link, true);
325
326 $omode = $_GET["omode"];
327
328 if (!$omode) $omode = "tfl";
329
330 print "<rpc-reply>";
331 if (strchr($omode, "l")) getLabelCounters($link);
332 if (strchr($omode, "f")) getFeedCounters($link);
333 if (strchr($omode, "t")) getTagCounters($link);
334 getGlobalCounters($link);
335 print "</rpc-reply>";
336 }
337
338 if ($subop == "catchupPage") {
339
340 $ids = split(",", $_GET["ids"]);
341
342 foreach ($ids as $id) {
343
344 db_query($link, "UPDATE ttrss_entries SET unread=false,last_read = NOW()
345 WHERE id = '$id'");
346
347 }
348
349 print "Marked active page as read.";
350 }
351 }
352
353 if ($op == "feeds") {
354
355 $tags = $_GET["tags"];
356
357 $subop = $_GET["subop"];
358
359 if ($subop == "catchupAll") {
360 db_query($link, "UPDATE ttrss_entries SET last_read = NOW(),unread = false");
361 }
362
363 outputFeedList($link, $tags);
364
365 }
366
367 if ($op == "view") {
368
369 $id = $_GET["id"];
370 $feed_id = $_GET["feed"];
371
372 $result = db_query($link, "UPDATE ttrss_entries SET unread = false,last_read = NOW() WHERE id = '$id'");
373
374 $addheader = $_GET["addheader"];
375
376 $result = db_query($link, "SELECT title,link,content,feed_id,comments,
377 (SELECT icon_url FROM ttrss_feeds WHERE id = feed_id) as icon_url
378 FROM ttrss_entries
379 WHERE id = '$id'");
380
381 if ($addheader) {
382 print "<html><head>
383 <title>Tiny Tiny RSS : Article $id</title>
384 <link rel=\"stylesheet\" href=\"tt-rss.css\" type=\"text/css\">
385 <script type=\"text/javascript\" src=\"functions.js\"></script>
386 <meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">
387 </head><body>";
388 }
389
390 if ($result) {
391
392 $line = db_fetch_assoc($result);
393
394 if ($line["icon_url"]) {
395 $feed_icon = "<img class=\"feedIcon\" src=\"" . $line["icon_url"] . "\">";
396 } else {
397 $feed_icon = "&nbsp;";
398 }
399
400 if ($line["comments"] && $line["link"] != $line["comments"]) {
401 $entry_comments = "(<a href=\"".$line["comments"]."\">Comments</a>)";
402 } else {
403 $entry_comments = "";
404 }
405
406 print "<div class=\"postReply\">";
407
408 print "<div class=\"postHeader\"><table>";
409
410 print "<tr><td><b>Title:</b></td>
411 <td width='100%'>" . $line["title"] . "</td></tr>";
412
413 print "<tr><td><b>Link:</b></td>
414 <td width='100%'>
415 <a href=\"" . $line["link"] . "\">".$line["link"]."</a>
416 $entry_comments</td></tr>";
417
418 print "</table></div>";
419
420 print "<div class=\"postIcon\">" . $feed_icon . "</div>";
421 print "<div class=\"postContent\">" . $line["content"] . "</div>";
422
423 print "</div>";
424
425 print "<script type=\"text/javascript\">
426 update_label_counters('$feed_id');
427 </script>";
428 }
429
430 if ($addheader) {
431 print "</body></html>";
432 }
433 }
434
435 if ($op == "viewfeed") {
436
437 $feed = $_GET["feed"];
438 $skip = $_GET["skip"];
439 $subop = $_GET["subop"];
440 $view_mode = $_GET["view"];
441 $addheader = $_GET["addheader"];
442 $limit = $_GET["limit"];
443
444 if (!$feed) {
445 print "Error: no feed to display.";
446 return;
447 }
448
449 if (!$skip) $skip = 0;
450
451 if ($subop == "undefined") $subop = "";
452
453 if ($addheader) {
454 print "<html><head>
455 <title>Tiny Tiny RSS : Feed $feed</title>
456 <link rel=\"stylesheet\" href=\"tt-rss.css\" type=\"text/css\">
457 <meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">
458 <script type=\"text/javascript\" src=\"functions.js\"></script>
459 <script type=\"text/javascript\" src=\"viewfeed.js\"></script>
460 </head><body>";
461 }
462
463 if ($subop == "ForceUpdate" && sprintf("%d", $feed) > 0) {
464
465 $tmp_result = db_query($link, "SELECT feed_url FROM ttrss_feeds
466 WHERE id = '$feed'");
467
468 $feed_url = db_fetch_result($tmp_result, 0, "feed_url");
469
470 update_rss_feed($link, $feed_url, $feed);
471
472 }
473
474 if ($subop == "MarkAllRead") {
475
476 if (sprintf("%d", $feed) != 0) {
477
478 if ($feed > 0) {
479 db_query($link, "UPDATE ttrss_entries
480 SET unread = false,last_read = NOW()
481 WHERE feed_id = '$feed'");
482
483 } else if ($feed < 0 && $feed > -10) { // special, like starred
484
485 if ($feed == -1) {
486 db_query($link, "UPDATE ttrss_entries
487 SET unread = false,last_read = NOW()
488 WHERE marked = true");
489 }
490
491 } else if ($feed < -10) { // label
492
493 $label_id = -$feed - 11;
494
495 $tmp_result = db_query($link, "SELECT sql_exp FROM ttrss_labels
496 WHERE id = '$label_id'");
497
498 if ($tmp_result) {
499 $sql_exp = db_fetch_result($tmp_result, 0, "sql_exp");
500
501 db_query($link, "UPDATE ttrss_entries
502 SET unread = false,last_read = NOW()
503 WHERE $sql_exp");
504 }
505 }
506 } else { // tag
507 // FIXME, implement catchup for tags
508 }
509
510 }
511
512 print "<table class=\"headlinesList\" id=\"headlinesList\" width=\"100%\">";
513
514 $search = $_GET["search"];
515
516 $search_mode = $_GET["smode"];
517
518 if ($search) {
519 $search_query_part = "(upper(title) LIKE upper('%$search%')
520 OR content LIKE '%$search%') AND";
521 } else {
522 $search_query_part = "";
523 }
524
525 $view_query_part = "";
526
527 if ($view_mode == "Starred") {
528 $view_query_part = " marked = true AND ";
529 }
530
531 if ($view_mode == "Unread") {
532 $view_query_part = " unread = true AND ";
533 }
534
535 if ($view_mode == "Unread or Starred") {
536 $view_query_part = " (unread = true OR marked = true) AND ";
537 }
538
539 if ($view_mode == "Unread or Updated") {
540 $view_query_part = " (unread = true OR last_read is NULL) AND ";
541 }
542
543 /* $result = db_query($link, "SELECT count(id) AS total_entries
544 FROM ttrss_entries WHERE
545 $search_query_part
546 feed_id = '$feed'");
547
548 $total_entries = db_fetch_result($result, 0, "total_entries"); */
549
550 /* $result = db_query("SELECT count(id) AS unread_entries
551 FROM ttrss_entries WHERE
552 $search_query_part
553 unread = true AND
554 feed_id = '$feed'");
555
556 $unread_entries = db_fetch_result($result, 0, "unread_entries"); */
557
558 if ($limit && $limit != "All") {
559 $limit_query_part = "LIMIT " . $limit;
560 }
561
562 $vfeed_query_part = "";
563
564 // override query strategy and enable feed display when searching globally
565 if ($search_mode == "All feeds") {
566 $query_strategy_part = "id > 0";
567 $vfeed_query_part = "(SELECT title FROM ttrss_feeds WHERE
568 id = feed_id) as feed_title,";
569 } else if (sprintf("%d", $feed) == 0) {
570 $query_strategy_part = "ttrss_entries.id > 0";
571 $vfeed_query_part = "(SELECT title FROM ttrss_feeds WHERE
572 id = feed_id) as feed_title,";
573 } else if ($feed >= 0) {
574 $query_strategy_part = "feed_id = '$feed'";
575 } else if ($feed == -1) { // starred virtual feed
576 $query_strategy_part = "marked = true";
577 $vfeed_query_part = "(SELECT title FROM ttrss_feeds WHERE
578 id = feed_id) as feed_title,";
579 } else if ($feed <= -10) { // labels
580 $label_id = -$feed - 11;
581
582 $tmp_result = db_query($link, "SELECT sql_exp FROM ttrss_labels
583 WHERE id = '$label_id'");
584
585 $query_strategy_part = db_fetch_result($tmp_result, 0, "sql_exp");
586
587 $vfeed_query_part = "(SELECT title FROM ttrss_feeds WHERE
588 id = feed_id) as feed_title,";
589 } else {
590 $query_strategy_part = "id > 0"; // dumb
591 }
592
593
594 $order_by = "updated DESC";
595
596 // if ($feed < -10) {
597 // $order_by = "feed_id,updated DESC";
598 // }
599
600 if ($feed < -10) error_reporting (0);
601
602 if (sprintf("%d", $feed) != 0) {
603
604 $result = db_query($link, "SELECT
605 id,title,updated,unread,feed_id,marked,link,last_read,
606 SUBSTRING(last_read,1,19) as last_read_noms,
607 $vfeed_query_part
608 SUBSTRING(updated,1,19) as updated_noms
609 FROM
610 ttrss_entries
611 WHERE
612 $search_query_part
613 $view_query_part
614 $query_strategy_part ORDER BY $order_by
615 $limit_query_part");
616
617 } else {
618 // browsing by tag
619
620 $result = db_query($link, "SELECT
621 ttrss_entries.id as id,title,updated,unread,feed_id,
622 marked,link,last_read,
623 SUBSTRING(last_read,1,19) as last_read_noms,
624 $vfeed_query_part
625 SUBSTRING(updated,1,19) as updated_noms
626 FROM
627 ttrss_entries,ttrss_tags
628 WHERE
629 post_id = ttrss_entries.id AND tag_name = '$feed' AND
630 $view_query_part
631 $search_query_part
632 $query_strategy_part ORDER BY $order_by
633 $limit_query_part");
634 }
635
636 if (!$result) {
637 print "<tr><td colspan='4' align='center'>
638 Could not display feed (query failed). Please check match syntax or local configuration.</td></tr>";
639 return;
640 }
641
642 $lnum = 0;
643
644 error_reporting (E_ERROR | E_WARNING | E_PARSE);
645
646 $num_unread = 0;
647
648 while ($line = db_fetch_assoc($result)) {
649
650 $class = ($lnum % 2) ? "even" : "odd";
651
652 $id = $line["id"];
653 $feed_id = $line["feed_id"];
654
655 // printf("L %d (%s) &gt; U %d (%s) = %d<br>",
656 // strtotime($line["last_read_noms"]), $line["last_read_noms"],
657 // strtotime($line["updated"]), $line["updated"],
658 // strtotime($line["last_read"]) >= strtotime($line["updated"]));
659
660 /* if ($line["last_read"] != "" && $line["updated"] != "" &&
661 strtotime($line["last_read_noms"]) < strtotime($line["updated_noms"])) {
662
663 $update_pic = "<img id='FUPDPIC-$id' src=\"images/updated.png\"
664 alt=\"Updated\">";
665
666 } else {
667
668 $update_pic = "<img id='FUPDPIC-$id' src=\"images/blank_icon.gif\"
669 alt=\"Updated\">";
670
671 } */
672
673 if ($line["last_read"] == "" &&
674 ($line["unread"] != "t" && $line["unread"] != "1")) {
675
676 $update_pic = "<img id='FUPDPIC-$id' src=\"images/updated.png\"
677 alt=\"Updated\">";
678 } else {
679 $update_pic = "<img id='FUPDPIC-$id' src=\"images/blank_icon.gif\"
680 alt=\"Updated\">";
681 }
682
683 if ($line["unread"] == "t" || $line["unread"] == "1") {
684 $class .= "Unread";
685 ++$num_unread;
686 }
687
688 if ($line["marked"] == "t" || $line["marked"] == "1") {
689 $marked_pic = "<img id=\"FMARKPIC-$id\" src=\"images/mark_set.png\"
690 alt=\"Reset mark\" onclick='javascript:toggleMark($id, false)'>";
691 } else {
692 $marked_pic = "<img id=\"FMARKPIC-$id\" src=\"images/mark_unset.png\"
693 alt=\"Set mark\" onclick='javascript:toggleMark($id, true)'>";
694 }
695
696 $content_link = "<a id=\"FTITLE-$id\" href=\"javascript:view($id,$feed_id);\">" .
697 $line["title"] . "</a>";
698
699 print "<tr class='$class' id='RROW-$id'>";
700 // onclick=\"javascript:view($id,$feed_id)\">
701
702 print "<td valign='center' align='center'>$update_pic</td>";
703 print "<td valign='center' align='center'>$marked_pic</td>";
704
705 print "<td width='25%'>
706 <a href=\"javascript:view($id,$feed_id);\">".$line["updated"]."</a></td>";
707
708 if ($line["feed_title"]) {
709 print "<td width='50%'>$content_link</td>";
710 print "<td width='20%'>".$line["feed_title"]."</td>";
711 } else {
712 print "<td width='70%'>$content_link</td>";
713 }
714
715 print "</tr>";
716
717 ++$lnum;
718 }
719
720 if ($lnum == 0) {
721 print "<tr><td align='center'>No articles found.</td></tr>";
722 }
723
724 print "</table>";
725
726 print "<script type=\"text/javascript\">
727 document.onkeydown = hotkey_handler;
728 update_label_counters('$feed');
729 </script>";
730
731 if ($addheader) {
732 print "</body></html>";
733 }
734
735 }
736
737 if ($op == "pref-rpc") {
738
739 $subop = $_GET["subop"];
740
741 if ($subop == "unread") {
742 $ids = split(",", $_GET["ids"]);
743 foreach ($ids as $id) {
744 db_query($link, "UPDATE ttrss_entries SET unread = true WHERE feed_id = '$id'");
745 }
746
747 print "Marked selected feeds as read.";
748 }
749
750 if ($subop == "read") {
751 $ids = split(",", $_GET["ids"]);
752 foreach ($ids as $id) {
753 db_query($link, "UPDATE ttrss_entries
754 SET unread = false,last_read = NOW() WHERE feed_id = '$id'");
755 }
756
757 print "Marked selected feeds as unread.";
758
759 }
760
761 }
762
763 if ($op == "pref-feeds") {
764
765 $subop = $_GET["subop"];
766
767 if ($subop == "editSave") {
768 $feed_title = db_escape_string($_GET["t"]);
769 $feed_link = db_escape_string($_GET["l"]);
770 $upd_intl = db_escape_string($_GET["ui"]);
771 $feed_id = $_GET["id"];
772
773 if (strtoupper($upd_intl) == "DEFAULT")
774 $upd_intl = 0;
775
776 $result = db_query($link, "UPDATE ttrss_feeds SET
777 title = '$feed_title', feed_url = '$feed_link',
778 update_interval = '$upd_intl' WHERE id = '$feed_id'");
779
780 }
781
782 if ($subop == "remove") {
783
784 if (!WEB_DEMO_MODE) {
785
786 $ids = split(",", $_GET["ids"]);
787
788 foreach ($ids as $id) {
789 db_query($link, "DELETE FROM ttrss_feeds WHERE id = '$id'");
790
791 if (file_exists(ICONS_DIR . "/$id.ico")) {
792 unlink(ICONS_DIR . "/$id.ico");
793 }
794 }
795 }
796 }
797
798 if ($subop == "add") {
799
800 if (!WEB_DEMO_MODE) {
801
802 $feed_link = db_escape_string($_GET["link"]);
803
804 $result = db_query($link,
805 "INSERT INTO ttrss_feeds (feed_url,title) VALUES ('$feed_link', '')");
806
807 $result = db_query($link,
808 "SELECT id FROM ttrss_feeds WHERE feed_url = '$feed_link'");
809
810 $feed_id = db_fetch_result($result, 0, "id");
811
812 if ($feed_id) {
813 update_rss_feed($link, $feed_link, $feed_id);
814 }
815 }
816 }
817
818 print "<table class=\"prefAddFeed\"><tr>
819 <td><input id=\"fadd_link\"></td>
820 <td colspan=\"4\" align=\"right\">
821 <a class=\"button\" href=\"javascript:addFeed()\">Add feed</a></td></tr>
822 </table>";
823
824 $result = db_query($link, "SELECT
825 id,title,feed_url,substring(last_updated,1,16) as last_updated,
826 update_interval
827 FROM
828 ttrss_feeds ORDER by title");
829
830 print "<p><table width=\"100%\" class=\"prefFeedList\" id=\"prefFeedList\">";
831 print "<tr class=\"title\">
832 <td>&nbsp;</td><td>Select</td><td width=\"40%\">Title</td>
833 <td width=\"30%\">Link</td><td width=\"10%\">Update Interval</td>
834 <td>Last updated</td></tr>";
835
836 $lnum = 0;
837
838 while ($line = db_fetch_assoc($result)) {
839
840 $class = ($lnum % 2) ? "even" : "odd";
841
842 $feed_id = $line["id"];
843
844 $edit_feed_id = $_GET["id"];
845
846 if ($subop == "edit" && $feed_id != $edit_feed_id) {
847 $class .= "Grayed";
848 }
849
850 print "<tr class=\"$class\" id=\"FEEDR-$feed_id\">";
851
852 $icon_file = ICONS_DIR . "/$feed_id.ico";
853
854 if (file_exists($icon_file) && filesize($icon_file) > 0) {
855 $feed_icon = "<img width=\"16\" height=\"16\"
856 src=\"" . ICONS_URL . "/$feed_id.ico\">";
857 } else {
858 $feed_icon = "&nbsp;";
859 }
860 print "<td align='center'>$feed_icon</td>";
861
862 $edit_title = htmlspecialchars(db_unescape_string($line["title"]));
863 $edit_link = htmlspecialchars(db_unescape_string($line["feed_url"]));
864
865 if (!$edit_feed_id || $subop != "edit") {
866
867 print "<td><input onclick='toggleSelectRow(this);'
868 type=\"checkbox\" id=\"FRCHK-".$line["id"]."\"></td>";
869
870 print "<td><a href=\"javascript:editFeed($feed_id);\">" .
871 $edit_title . "</td>";
872 print "<td><a href=\"javascript:editFeed($feed_id);\">" .
873 $edit_link . "</td>";
874
875 if ($line["update_interval"] == "0")
876 $line["update_interval"] = "Default";
877
878 print "<td>" . $line["update_interval"] . "</td>";
879
880
881 } else if ($feed_id != $edit_feed_id) {
882
883 print "<td><input disabled=\"true\" type=\"checkbox\"
884 id=\"FRCHK-".$line["id"]."\"></td>";
885
886 print "<td>$edit_title</td>";
887 print "<td>$edit_link</td>";
888
889 if ($line["update_interval"] == "0")
890 $line["update_interval"] = "Default";
891
892 print "<td>" . $line["update_interval"] . "</td>";
893
894 } else {
895
896 print "<td><input disabled=\"true\" type=\"checkbox\"></td>";
897
898 print "<td><input id=\"iedit_title\" value=\"$edit_title\"></td>";
899 print "<td><input id=\"iedit_link\" value=\"$edit_link\"></td>";
900 print "<td><input id=\"iedit_updintl\" value=\"".$line["update_interval"]."\"></td>";
901
902 }
903
904 if (!$line["last_updated"]) $line["last_updated"] = "Never";
905
906 print "<td>" . $line["last_updated"] . "</td>";
907
908 print "</tr>";
909
910 ++$lnum;
911 }
912
913 if ($lnum == 0) {
914 print "<tr><td colspan=\"5\" align=\"center\">No feeds defined.</td></tr>";
915 }
916
917 print "</table>";
918
919 print "<p>";
920
921 if ($subop == "edit") {
922 print "Edit feed:&nbsp;
923 <input type=\"submit\" class=\"button\"
924 onclick=\"javascript:feedEditCancel()\" value=\"Cancel\">
925 <input type=\"submit\" class=\"button\"
926 onclick=\"javascript:feedEditSave()\" value=\"Save\">";
927 } else {
928
929 print "
930 Selection:&nbsp;
931 <input type=\"submit\" class=\"button\"
932 onclick=\"javascript:editSelectedFeed()\" value=\"Edit\">
933 <input type=\"submit\" class=\"button\"
934 onclick=\"javascript:removeSelectedFeeds()\" value=\"Remove\">";
935
936 if (ENABLE_PREFS_CATCHUP_UNCATCHUP) {
937 print "
938 <input type=\"submit\" class=\"button\"
939 onclick=\"javascript:readSelectedFeeds()\" value=\"Mark as read\">
940 <input type=\"submit\" class=\"button\"
941 onclick=\"javascript:unreadSelectedFeeds()\" value=\"Mark as unread\">&nbsp;";
942 }
943 print "
944 All feeds:
945 <input type=\"submit\"
946 class=\"button\" onclick=\"gotoExportOpml()\" value=\"Export OPML\">";
947
948 }
949
950 }
951
952 if ($op == "pref-filters") {
953
954 $subop = $_GET["subop"];
955
956 if ($subop == "editSave") {
957
958 $regexp = db_escape_string($_GET["r"]);
959 $descr = db_escape_string($_GET["d"]);
960 $match = db_escape_string($_GET["m"]);
961 $filter_id = db_escape_string($_GET["id"]);
962
963 $result = db_query($link, "UPDATE ttrss_filters SET
964 reg_exp = '$regexp',
965 description = '$descr',
966 filter_type = (SELECT id FROM ttrss_filter_types WHERE
967 description = '$match')
968 WHERE id = '$filter_id'");
969 }
970
971 if ($subop == "remove") {
972
973 if (!WEB_DEMO_MODE) {
974
975 $ids = split(",", $_GET["ids"]);
976
977 foreach ($ids as $id) {
978 db_query($link, "DELETE FROM ttrss_filters WHERE id = '$id'");
979
980 }
981 }
982 }
983
984 if ($subop == "add") {
985
986 if (!WEB_DEMO_MODE) {
987
988 $regexp = db_escape_string($_GET["regexp"]);
989 $match = db_escape_string($_GET["match"]);
990
991 $result = db_query($link,
992 "INSERT INTO ttrss_filters (reg_exp,filter_type) VALUES
993 ('$regexp', (SELECT id FROM ttrss_filter_types WHERE
994 description = '$match'))");
995 }
996 }
997
998 $result = db_query($link, "SELECT description
999 FROM ttrss_filter_types ORDER BY description");
1000
1001 $filter_types = array();
1002
1003 while ($line = db_fetch_assoc($result)) {
1004 array_push($filter_types, $line["description"]);
1005 }
1006
1007 print "<table class=\"prefAddFeed\"><tr>
1008 <td><input id=\"fadd_regexp\"></td>
1009 <td>";
1010 print_select("fadd_match", "Title", $filter_types);
1011
1012 print"</td><td colspan=\"4\" align=\"right\">
1013 <a class=\"button\" href=\"javascript:addFilter()\">Add filter</a></td></tr>
1014 </table>";
1015
1016 $result = db_query($link, "SELECT
1017 id,reg_exp,description,
1018 (SELECT name FROM ttrss_filter_types WHERE
1019 id = filter_type) as filter_type_name,
1020 (SELECT description FROM ttrss_filter_types
1021 WHERE id = filter_type) as filter_type_descr
1022 FROM
1023 ttrss_filters ORDER by reg_exp");
1024
1025 print "<p><table width=\"100%\" class=\"prefFilterList\" id=\"prefFilterList\">";
1026
1027 print "<tr class=\"title\">
1028 <td width=\"5%\">Select</td><td width=\"40%\">Filter expression</td>
1029 <td width=\"40%\">Description</td><td width=\"10%\">Match</td></tr>";
1030
1031 $lnum = 0;
1032
1033 while ($line = db_fetch_assoc($result)) {
1034
1035 $class = ($lnum % 2) ? "even" : "odd";
1036
1037 $filter_id = $line["id"];
1038 $edit_filter_id = $_GET["id"];
1039
1040 if ($subop == "edit" && $filter_id != $edit_filter_id) {
1041 $class .= "Grayed";
1042 }
1043
1044 print "<tr class=\"$class\" id=\"FILRR-$filter_id\">";
1045
1046 $line["regexp"] = htmlspecialchars($line["reg_exp"]);
1047 $line["description"] = htmlspecialchars($line["description"]);
1048
1049 if (!$edit_filter_id || $subop != "edit") {
1050
1051 if (!$line["description"]) $line["description"] = "[No description]";
1052
1053 print "<td><input onclick='toggleSelectRow(this);'
1054 type=\"checkbox\" id=\"FICHK-".$line["id"]."\"></td>";
1055
1056 print "<td><a href=\"javascript:editFilter($filter_id);\">" .
1057 $line["reg_exp"] . "</td>";
1058
1059 print "<td><a href=\"javascript:editFilter($filter_id);\">" .
1060 $line["description"] . "</td>";
1061
1062 print "<td>".$line["filter_type_descr"]."</td>";
1063
1064 } else if ($filter_id != $edit_filter_id) {
1065
1066 if (!$line["description"]) $line["description"] = "[No description]";
1067
1068 print "<td><input disabled=\"true\" type=\"checkbox\"
1069 id=\"FICHK-".$line["id"]."\"></td>";
1070
1071 print "<td>".$line["reg_exp"]."</td>";
1072 print "<td>".$line["description"]."</td>";
1073 print "<td>".$line["filter_type_descr"]."</td>";
1074
1075 } else {
1076
1077 print "<td><input disabled=\"true\" type=\"checkbox\"></td>";
1078
1079 print "<td><input id=\"iedit_regexp\" value=\"".$line["reg_exp"].
1080 "\"></td>";
1081
1082 print "<td><input id=\"iedit_descr\" value=\"".$line["description"].
1083 "\"></td>";
1084
1085 print "<td>";
1086 print_select("iedit_match", $line["filter_type_descr"], $filter_types);
1087 print "</td>";
1088
1089 }
1090
1091
1092 print "</tr>";
1093
1094 ++$lnum;
1095 }
1096
1097 if ($lnum == 0) {
1098 print "<tr><td colspan=\"4\" align=\"center\">No filters defined.</td></tr>";
1099 }
1100
1101 print "</table>";
1102
1103 print "<p>";
1104
1105 if ($subop == "edit") {
1106 print "Edit feed:
1107 <input type=\"submit\" class=\"button\"
1108 onclick=\"javascript:filterEditCancel()\" value=\"Cancel\">
1109 <input type=\"submit\" class=\"button\"
1110 onclick=\"javascript:filterEditSave()\" value=\"Save\">";
1111
1112 } else {
1113
1114 print "
1115 Selection:
1116 <input type=\"submit\" class=\"button\"
1117 onclick=\"javascript:editSelectedFilter()\" value=\"Edit\">
1118 <input type=\"submit\" class=\"button\"
1119 onclick=\"javascript:removeSelectedFilters()\" value=\"Remove\">";
1120 }
1121 }
1122
1123 if ($op == "pref-labels") {
1124
1125 $subop = $_GET["subop"];
1126
1127 if ($subop == "editSave") {
1128
1129 $sql_exp = $_GET["s"];
1130 $descr = $_GET["d"];
1131 $label_id = db_escape_string($_GET["id"]);
1132
1133 // print "$sql_exp : $descr : $label_id";
1134
1135 $result = db_query($link, "UPDATE ttrss_labels SET
1136 sql_exp = '$sql_exp',
1137 description = '$descr'
1138 WHERE id = '$label_id'");
1139 }
1140
1141 if ($subop == "remove") {
1142
1143 if (!WEB_DEMO_MODE) {
1144
1145 $ids = split(",", $_GET["ids"]);
1146
1147 foreach ($ids as $id) {
1148 db_query($link, "DELETE FROM ttrss_labels WHERE id = '$id'");
1149
1150 }
1151 }
1152 }
1153
1154 if ($subop == "add") {
1155
1156 if (!WEB_DEMO_MODE) {
1157
1158 $exp = $_GET["exp"];
1159
1160 $result = db_query($link,
1161 "INSERT INTO ttrss_labels (sql_exp,description)
1162 VALUES ('$exp', '$exp')");
1163 }
1164 }
1165
1166 print "<table class=\"prefAddFeed\"><tr>
1167 <td><input id=\"ladd_expr\"></td>";
1168
1169 print"<td colspan=\"4\" align=\"right\">
1170 <a class=\"button\" href=\"javascript:addLabel()\">Add label</a></td></tr>
1171 </table>";
1172
1173 $result = db_query($link, "SELECT
1174 id,sql_exp,description
1175 FROM
1176 ttrss_labels ORDER by description");
1177
1178 print "<p><table width=\"100%\" class=\"prefLabelList\" id=\"prefLabelList\">";
1179
1180 print "<tr class=\"title\">
1181 <td width=\"5%\">Select</td><td width=\"40%\">SQL expression
1182 <a class=\"helpLink\" href=\"javascript:popupHelp(1)\">(?)</a>
1183 </td>
1184 <td width=\"40%\">Caption</td></tr>";
1185
1186 $lnum = 0;
1187
1188 while ($line = db_fetch_assoc($result)) {
1189
1190 $class = ($lnum % 2) ? "even" : "odd";
1191
1192 $label_id = $line["id"];
1193 $edit_label_id = $_GET["id"];
1194
1195 if ($subop == "edit" && $label_id != $edit_label_id) {
1196 $class .= "Grayed";
1197 }
1198
1199 print "<tr class=\"$class\" id=\"LILRR-$label_id\">";
1200
1201 $line["sql_exp"] = htmlspecialchars($line["sql_exp"]);
1202 $line["description"] = htmlspecialchars($line["description"]);
1203
1204 if (!$edit_label_id || $subop != "edit") {
1205
1206 if (!$line["description"]) $line["description"] = "[No caption]";
1207
1208 print "<td><input onclick='toggleSelectRow(this);'
1209 type=\"checkbox\" id=\"LICHK-".$line["id"]."\"></td>";
1210
1211 print "<td><a href=\"javascript:editLabel($label_id);\">" .
1212 $line["sql_exp"] . "</td>";
1213
1214 print "<td><a href=\"javascript:editLabel($label_id);\">" .
1215 $line["description"] . "</td>";
1216
1217 } else if ($label_id != $edit_label_id) {
1218
1219 if (!$line["description"]) $line["description"] = "[No description]";
1220
1221 print "<td><input disabled=\"true\" type=\"checkbox\"
1222 id=\"LICHK-".$line["id"]."\"></td>";
1223
1224 print "<td>".$line["sql_exp"]."</td>";
1225 print "<td>".$line["description"]."</td>";
1226
1227 } else {
1228
1229 print "<td><input disabled=\"true\" type=\"checkbox\"></td>";
1230
1231 print "<td><input id=\"iedit_expr\" value=\"".$line["sql_exp"].
1232 "\"></td>";
1233
1234 print "<td><input id=\"iedit_descr\" value=\"".$line["description"].
1235 "\"></td>";
1236
1237 }
1238
1239
1240 print "</tr>";
1241
1242 ++$lnum;
1243 }
1244
1245 if ($lnum == 0) {
1246 print "<tr><td colspan=\"4\" align=\"center\">No labels defined.</td></tr>";
1247 }
1248
1249 print "</table>";
1250
1251 print "<p>";
1252
1253 if ($subop == "edit") {
1254 print "Edit label:
1255 <input type=\"submit\" class=\"button\"
1256 onclick=\"javascript:labelEditCancel()\" value=\"Cancel\">
1257 <input type=\"submit\" class=\"button\"
1258 onclick=\"javascript:labelEditSave()\" value=\"Save\">";
1259
1260 } else {
1261
1262 print "
1263 Selection:
1264 <input type=\"submit\" class=\"button\"
1265 onclick=\"javascript:editSelectedLabel()\" value=\"Edit\">
1266 <input type=\"submit\" class=\"button\"
1267 onclick=\"javascript:removeSelectedLabels()\" value=\"Remove\">";
1268 }
1269 }
1270
1271 if ($op == "error") {
1272 print "<div width=\"100%\" align='center'>";
1273 $msg = $_GET["msg"];
1274 print $msg;
1275 print "</div>";
1276 }
1277
1278 if ($op == "help") {
1279 print "<html><head>
1280 <title>Tiny Tiny RSS : Help</title>
1281 <link rel=\"stylesheet\" href=\"tt-rss.css\" type=\"text/css\">
1282 <script type=\"text/javascript\" src=\"functions.js\"></script>
1283 <script type=\"text/javascript\" src=\"feedlist.js\"></script>
1284 <meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">
1285 </head><body>";
1286
1287 $tid = sprintf("%d", $_GET["tid"]);
1288
1289 /* FIXME this badly needs real implementation */
1290
1291 print "<div class='helpResponse'>";
1292
1293 ?>
1294
1295 <h1>Help for SQL expressions</h1>
1296
1297 <h2>Description</h2>
1298
1299 <p>The &laquo;SQL expression&raquo; is added to WHERE clause of
1300 view feed query. You can match on ttrss_entries table fields
1301 and even use subselect to query additional information. This
1302 functionality is considered to be advanced and requires basic
1303 understanding of SQL.</p>
1304
1305 <h2>Examples</h2>
1306
1307 <pre>unread = true</pre>
1308
1309 Matches all unread articles
1310
1311 <pre>title like '%Linux%'</pre>
1312
1313 Matches all articles which mention Linux in the title. You get the idea.
1314
1315 <p>See the database schema included in the distribution package for gruesome
1316 details.</p>
1317
1318 <?
1319
1320 print "<div align='center'>
1321 <a class=\"helpLink\"
1322 href=\"javascript:window.close()\">(Close this window)</a></div>";
1323
1324 print "</div>";
1325
1326 print "</body></html>";
1327
1328 }
1329
1330 if ($op == "dlg") {
1331 $id = $_GET["id"];
1332 $param = $_GET["param"];
1333
1334 if ($id == "quickAddFeed") {
1335 print "Feed URL: <input
1336 onblur=\"javascript:enableHotkeys()\" onfocus=\"javascript:disableHotkeys()\"
1337 id=\"qafInput\">
1338 <input class=\"button\"
1339 type=\"submit\" onclick=\"javascript:qafAdd()\" value=\"Add feed\">
1340 <input class=\"button\"
1341 type=\"submit\" onclick=\"javascript:closeDlg()\"
1342 value=\"Cancel\">";
1343 }
1344
1345 if ($id == "quickDelFeed") {
1346
1347 $param = db_escape_string($param);
1348
1349 $result = db_query($link, "SELECT title FROM ttrss_feeds WHERE id = '$param'");
1350
1351 if ($result) {
1352
1353 $f_title = db_fetch_result($result, 0, "title");
1354
1355 print "Remove current feed ($f_title)?&nbsp;
1356 <input class=\"button\"
1357 type=\"submit\" onclick=\"javascript:qfdDelete($param)\" value=\"Remove\">
1358 <input class=\"button\"
1359 type=\"submit\" onclick=\"javascript:closeDlg()\"
1360 value=\"Cancel\">";
1361 } else {
1362 print "Error: Feed $param not found.&nbsp;
1363 <input class=\"button\"
1364 type=\"submit\" onclick=\"javascript:closeDlg()\"
1365 value=\"Cancel\">";
1366 }
1367 }
1368
1369 if ($id == "search") {
1370
1371 print "<input id=\"searchbox\" class=\"extSearch\"
1372 onblur=\"javascript:enableHotkeys()\" onfocus=\"javascript:disableHotkeys()\"
1373 onchange=\"javascript:search()\">
1374 <select id=\"searchmodebox\">
1375 <option selected>All feeds</option>
1376 <option>This feed</option>
1377 </select>
1378 <input type=\"submit\"
1379 class=\"button\" onclick=\"javascript:search()\" value=\"Search\">
1380 <input class=\"button\"
1381 type=\"submit\" onclick=\"javascript:closeDlg()\"
1382 value=\"Close\">";
1383
1384 }
1385
1386 }
1387
1388 db_close($link);
1389 ?>
1390
1391 <!-- <?= sprintf("Backend execution time: %.4f seconds", getmicrotime() - $script_started) ?> -->
1392