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