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