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