]> git.wh0rd.org - tt-rss.git/blob - backend.php
4b9d3a7f3e1bfbf32a38e000ccca6bcab062c829
[tt-rss.git] / backend.php
1 <?
2 session_start();
3
4 $op = $_REQUEST["op"];
5
6 if (($op == "rpc" || $op == "updateAllFeeds" ||
7 $op == "forceUpdateAllFeeds") && !$_REQUEST["noxml"]) {
8 header("Content-Type: application/xml");
9 }
10
11 if (!$_SESSION["uid"]) {
12
13 if (($op == "rpc" || $op == "updateAllFeeds" ||
14 $op == "forceUpdateAllFeeds")) {
15 print "<error error-code=\"6\"/>";
16 }
17 exit;
18 }
19
20 define(SCHEMA_VERSION, 2);
21
22 require_once "config.php";
23 require_once "db.php";
24 require_once "db-prefs.php";
25 require_once "functions.php";
26 require_once "magpierss/rss_fetch.inc";
27
28 $script_started = getmicrotime();
29
30 $link = db_connect(DB_HOST, DB_USER, DB_PASS, DB_NAME);
31
32 if (!$link) {
33 if (DB_TYPE == "mysql") {
34 print mysql_error();
35 }
36 // PG seems to display its own errors just fine by default.
37 return;
38 }
39
40 if (DB_TYPE == "pgsql") {
41 pg_query("set client_encoding = 'utf-8'");
42 }
43
44 /*
45 $result = db_query($link, "SELECT schema_version FROM ttrss_version");
46
47 $schema_version = db_fetch_result($result, 0, "schema_version");
48
49 if ($schema_version != SCHEMA_VERSION) {
50 print "Error: database schema is invalid
51 (got version $schema_version; expected ".SCHEMA_VERSION.")";
52 return;
53 }
54 */
55
56 $fetch = $_GET["fetch"];
57
58 /* FIXME this needs reworking */
59
60 function getGlobalCounters($link) {
61 $result = db_query($link, "SELECT count(id) as c_id FROM ttrss_entries,ttrss_user_entries
62 WHERE unread = true AND
63 ttrss_user_entries.ref_id = ttrss_entries.id AND
64 owner_uid = " . $_SESSION["uid"]);
65 $c_id = db_fetch_result($result, 0, "c_id");
66 print "<counter id='global-unread' counter='$c_id'/>";
67 }
68
69 function getTagCounters($link) {
70
71 $result = db_query($link, "SELECT tag_name,count(ttrss_entries.id) AS count
72 FROM ttrss_tags,ttrss_entries,ttrss_user_entries WHERE
73 ttrss_user_entries.ref_id = ttrss_entries.id AND
74 ttrss_tags.owner_uid = ".$_SESSION["uid"]." AND
75 post_int_id = ttrss_user_entries.int_id AND unread = true GROUP BY tag_name
76 UNION
77 select tag_name,0 as count FROM ttrss_tags
78 WHERE ttrss_tags.owner_uid = ".$_SESSION["uid"]);
79
80 $tags = array();
81
82 while ($line = db_fetch_assoc($result)) {
83 $tags[$line["tag_name"]] += $line["count"];
84 }
85
86 foreach (array_keys($tags) as $tag) {
87 $unread = $tags[$tag];
88
89 $tag = htmlspecialchars($tag);
90 print "<tag id=\"$tag\" counter=\"$unread\"/>";
91 }
92 }
93
94 function getLabelCounters($link) {
95
96 $result = db_query($link, "SELECT count(id) as count FROM ttrss_entries,ttrss_user_entries
97 WHERE marked = true AND ttrss_user_entries.ref_id = ttrss_entries.id AND
98 unread = true AND owner_uid = ".$_SESSION["uid"]);
99
100 $count = db_fetch_result($result, 0, "count");
101
102 print "<label id=\"-1\" counter=\"$count\"/>";
103
104 $result = db_query($link, "SELECT owner_uid,id,sql_exp,description FROM
105 ttrss_labels WHERE owner_uid = ".$_SESSION["uid"]." ORDER by description");
106
107 while ($line = db_fetch_assoc($result)) {
108
109 $id = -$line["id"] - 11;
110
111 error_reporting (0);
112
113 $tmp_result = db_query($link, "SELECT count(id) as count FROM ttrss_user_entries,ttrss_entries
114 WHERE (" . $line["sql_exp"] . ") AND unread = true AND
115 ttrss_user_entries.ref_id = ttrss_entries.id AND
116 owner_uid = ".$_SESSION["uid"]);
117
118 $count = db_fetch_result($tmp_result, 0, "count");
119
120 print "<label id=\"$id\" counter=\"$count\"/>";
121
122 error_reporting (E_ERROR | E_WARNING | E_PARSE);
123
124 }
125 }
126
127 function getFeedCounter($link, $id) {
128
129 $result = db_query($link, "SELECT
130 count(id) as count FROM ttrss_entries,ttrss_user_entries
131 WHERE feed_id = '$id' AND unread = true
132 AND ttrss_user_entries.ref_id = ttrss_entries.id");
133
134 $count = db_fetch_result($result, 0, "count");
135
136 print "<feed id=\"$id\" counter=\"$count\"/>";
137 }
138
139 function getFeedCounters($link) {
140
141 $result = db_query($link, "SELECT id,
142 (SELECT count(id)
143 FROM ttrss_entries,ttrss_user_entries
144 WHERE feed_id = ttrss_feeds.id AND ttrss_user_entries.ref_id = ttrss_entries.id
145 AND unread = true AND owner_uid = ".$_SESSION["uid"].") as count
146 FROM ttrss_feeds WHERE owner_uid = ".$_SESSION["uid"]);
147
148 while ($line = db_fetch_assoc($result)) {
149
150 $id = $line["id"];
151 $count = $line["count"];
152
153 print "<feed id=\"$id\" counter=\"$count\"/>";
154 }
155 }
156
157 function outputFeedList($link, $tags = false) {
158
159 print "<html><head>
160 <title>Tiny Tiny RSS : Feedlist</title>
161 <link rel=\"stylesheet\" href=\"tt-rss.css\" type=\"text/css\">";
162
163 if (get_pref($link, 'USE_COMPACT_STYLESHEET')) {
164 print "<link rel=\"stylesheet\" type=\"text/css\"
165 href=\"tt-rss_compact.css\"/>";
166 } else {
167 print "<link title=\"Compact Stylesheet\" rel=\"alternate stylesheet\"
168 type=\"text/css\" href=\"tt-rss_compact.css\"/>";
169 }
170
171 print "<script type=\"text/javascript\" src=\"functions.js\"></script>
172 <script type=\"text/javascript\" src=\"feedlist.js\"></script>
173 <meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">
174 </head><body onload=\"init()\">";
175
176 print "<ul class=\"feedList\" id=\"feedList\">";
177
178 $owner_uid = $_SESSION["uid"];
179
180 if (!$tags) {
181
182 /* virtual feeds */
183
184 $result = db_query($link, "SELECT count(id) as num_starred
185 FROM ttrss_entries,ttrss_user_entries
186 WHERE marked = true AND
187 ttrss_user_entries.ref_id = ttrss_entries.id AND
188 unread = true AND owner_uid = '$owner_uid'");
189 $num_starred = db_fetch_result($result, 0, "num_starred");
190
191 $class = "virt";
192
193 if ($num_starred > 0) $class .= "Unread";
194
195 printFeedEntry(-1, $class, "Starred articles", $num_starred,
196 "images/mark_set.png", $link);
197
198 if (get_pref($link, 'ENABLE_LABELS')) {
199
200 $result = db_query($link, "SELECT id,sql_exp,description FROM
201 ttrss_labels WHERE owner_uid = '$owner_uid' ORDER by description");
202
203 if (db_num_rows($result) > 0) {
204 print "<li><hr></li>";
205 }
206
207 while ($line = db_fetch_assoc($result)) {
208
209 error_reporting (0);
210
211 $tmp_result = db_query($link, "SELECT count(id) as count FROM ttrss_entries,ttrss_user_entries
212 WHERE (" . $line["sql_exp"] . ") AND unread = true AND
213 ttrss_user_entries.ref_id = ttrss_entries.id
214 AND owner_uid = '$owner_uid'");
215
216 $count = db_fetch_result($tmp_result, 0, "count");
217
218 $class = "label";
219
220 if ($count > 0) {
221 $class .= "Unread";
222 }
223
224 error_reporting (E_ERROR | E_WARNING | E_PARSE);
225
226 printFeedEntry(-$line["id"]-11,
227 $class, $line["description"], $count, "images/label.png", $link);
228
229 }
230 }
231
232 print "<li><hr></li>";
233
234 $result = db_query($link, "SELECT *,
235 (SELECT count(id) FROM ttrss_entries,ttrss_user_entries
236 WHERE feed_id = ttrss_feeds.id AND
237 ttrss_user_entries.ref_id = ttrss_entries.id AND
238 owner_uid = '$owner_uid') AS total,
239 (SELECT count(id) FROM ttrss_entries,ttrss_user_entries
240 WHERE feed_id = ttrss_feeds.id AND unread = true
241 AND ttrss_user_entries.ref_id = ttrss_entries.id
242 AND owner_uid = '$owner_uid') as unread
243 FROM ttrss_feeds WHERE owner_uid = '$owner_uid' ORDER BY title");
244
245 $actid = $_GET["actid"];
246
247 /* real feeds */
248
249 $lnum = 0;
250
251 $total_unread = 0;
252
253 while ($line = db_fetch_assoc($result)) {
254
255 $feed = $line["title"];
256 $feed_id = $line["id"];
257
258 $subop = $_GET["subop"];
259
260 $total = $line["total"];
261 $unread = $line["unread"];
262
263 // $class = ($lnum % 2) ? "even" : "odd";
264
265 $class = "feed";
266
267 if ($unread > 0) $class .= "Unread";
268
269 if ($actid == $feed_id) {
270 $class .= "Selected";
271 }
272
273 $total_unread += $unread;
274
275 printFeedEntry($feed_id, $class, $feed, $unread, "icons/$feed_id.ico", $link);
276
277 ++$lnum;
278 }
279 } else {
280
281 // tags
282
283 $result = db_query($link, "SELECT tag_name,count(ttrss_entries.id) AS count
284 FROM ttrss_tags,ttrss_entries,ttrss_user_entries WHERE
285 post_int_id = ttrss_user_entries.int_id AND
286 unread = true AND ref_id = ttrss_entries.id
287 AND ttrss_tags.owner_uid = '$owner_uid' GROUP BY tag_name
288 UNION
289 select tag_name,0 as count FROM ttrss_tags WHERE owner_uid = '$owner_uid'
290 ORDER BY tag_name");
291
292 $tags = array();
293
294 while ($line = db_fetch_assoc($result)) {
295 $tags[$line["tag_name"]] += $line["count"];
296 }
297
298 foreach (array_keys($tags) as $tag) {
299
300 $unread = $tags[$tag];
301
302 $class = "odd";
303
304 if ($unread > 0) {
305 $class .= "Unread";
306 }
307
308 printFeedEntry($tag, $class, $tag, $unread, "images/tag.png", $link);
309
310 }
311
312 }
313
314 if (db_num_rows($result) == 0) {
315 print "<li>No tags/feeds to display.</li>";
316 }
317
318 print "</ul>";
319
320 print "<div class=\"invisible\" id=\"FEEDTU\">$total_unread</div>";
321
322 }
323
324
325 if ($op == "rpc") {
326
327 $subop = $_GET["subop"];
328
329 if ($subop == "getLabelCounters") {
330 $aid = $_GET["aid"];
331 print "<rpc-reply>";
332 getLabelCounters($link);
333 if ($aid) {
334 getFeedCounter($link, $aid);
335 }
336 print "</rpc-reply>";
337 }
338
339 if ($subop == "getFeedCounters") {
340 print "<rpc-reply>";
341 getFeedCounters($link);
342 print "</rpc-reply>";
343 }
344
345 if ($subop == "getAllCounters") {
346 print "<rpc-reply>";
347 getLabelCounters($link);
348 getFeedCounters($link);
349 getTagCounters($link);
350 getGlobalCounters($link);
351 print "</rpc-reply>";
352 }
353
354 if ($subop == "mark") {
355 $mark = $_GET["mark"];
356 $id = db_escape_string($_GET["id"]);
357
358 if ($mark == "1") {
359 $mark = "true";
360 } else {
361 $mark = "false";
362 }
363
364 // FIXME this needs collision testing
365
366 $result = db_query($link, "UPDATE ttrss_user_entries SET marked = $mark
367 WHERE ref_id = '$id' AND owner_uid = " . $_SESSION["uid"]);
368 }
369
370 if ($subop == "updateFeed") {
371 $feed_id = db_escape_string($_GET["feed"]);
372
373 $result = db_query($link,
374 "SELECT feed_url FROM ttrss_feeds WHERE id = '$feed_id'");
375
376 if (db_num_rows($result) > 0) {
377 $feed_url = db_fetch_result($result, 0, "feed_url");
378 // update_rss_feed($link, $feed_url, $feed_id);
379 }
380
381 print "DONE-$feed_id";
382
383 return;
384 }
385
386 if ($subop == "forceUpdateAllFeeds" || $subop == "updateAllFeeds") {
387
388 update_all_feeds($link, $subop == "forceUpdateAllFeeds");
389
390 $omode = $_GET["omode"];
391
392 if (!$omode) $omode = "tfl";
393
394 print "<rpc-reply>";
395 if (strchr($omode, "l")) getLabelCounters($link);
396 if (strchr($omode, "f")) getFeedCounters($link);
397 if (strchr($omode, "t")) getTagCounters($link);
398 getGlobalCounters($link);
399 print "</rpc-reply>";
400 }
401
402 if ($subop == "catchupPage") {
403
404 $ids = split(",", $_GET["ids"]);
405
406 foreach ($ids as $id) {
407
408 db_query($link, "UPDATE ttrss_entries SET unread=false,last_read = NOW()
409 WHERE id = '$id'");
410
411 }
412
413 print "Marked active page as read.";
414 }
415
416 if ($subop == "sanityCheck") {
417
418 $error_code = 0;
419
420 $result = db_query($link, "SELECT schema_version FROM ttrss_version");
421
422 $schema_version = db_fetch_result($result, 0, "schema_version");
423
424 if ($schema_version != SCHEMA_VERSION) {
425 $error_code = 5;
426 }
427
428 print "<error error-code='$error_code'/>";
429 }
430
431 if ($subop == "globalPurge") {
432
433 print "<rpc-reply>";
434 global_purge_old_posts($link, true);
435 print "</rpc-reply>";
436
437 }
438
439 }
440
441 if ($op == "feeds") {
442
443 $tags = $_GET["tags"];
444
445 $subop = $_GET["subop"];
446
447 if ($subop == "catchupAll") {
448 db_query($link, "UPDATE ttrss_entries SET
449 last_read = NOW(),unread = false WHERE owner_uid = " . $_SESSION["uid"]);
450 }
451
452 outputFeedList($link, $tags);
453
454 }
455
456 if ($op == "view") {
457
458 $id = $_GET["id"];
459 $feed_id = $_GET["feed"];
460
461 $result = db_query($link, "UPDATE ttrss_user_entries
462 SET unread = false,last_read = NOW()
463 WHERE ref_id = '$id' AND feed_id = '$feed_id' AND owner_uid = " . $_SESSION["uid"]);
464
465 $addheader = $_GET["addheader"];
466
467 $result = db_query($link, "SELECT title,link,content,feed_id,comments,
468 (SELECT icon_url FROM ttrss_feeds WHERE id = feed_id) as icon_url
469 FROM ttrss_entries,ttrss_user_entries
470 WHERE id = '$id' AND ref_id = id");
471
472 if ($addheader) {
473 print "<html><head>
474 <title>Tiny Tiny RSS : Article $id</title>
475 <link rel=\"stylesheet\" href=\"tt-rss.css\" type=\"text/css\">
476 <script type=\"text/javascript\" src=\"functions.js\"></script>
477 <meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">
478 </head><body>";
479 }
480
481 if ($result) {
482
483 $line = db_fetch_assoc($result);
484
485 if ($line["icon_url"]) {
486 $feed_icon = "<img class=\"feedIcon\" src=\"" . $line["icon_url"] . "\">";
487 } else {
488 $feed_icon = "&nbsp;";
489 }
490
491 if ($line["comments"] && $line["link"] != $line["comments"]) {
492 $entry_comments = "(<a href=\"".$line["comments"]."\">Comments</a>)";
493 } else {
494 $entry_comments = "";
495 }
496
497 print "<div class=\"postReply\">";
498
499 print "<div class=\"postHeader\"><table>";
500
501 print "<tr><td><b>Title:</b></td>
502 <td width='100%'>" . $line["title"] . "</td></tr>";
503
504 print "<tr><td><b>Link:</b></td>
505 <td width='100%'>
506 <a href=\"" . $line["link"] . "\">".$line["link"]."</a>
507 $entry_comments</td></tr>";
508
509 print "</table></div>";
510
511 print "<div class=\"postIcon\">" . $feed_icon . "</div>";
512 print "<div class=\"postContent\">" . $line["content"] . "</div>";
513
514 print "</div>";
515
516 print "<script type=\"text/javascript\">
517 update_label_counters('$feed_id');
518 </script>";
519 }
520
521 if ($addheader) {
522 print "</body></html>";
523 }
524 }
525
526 if ($op == "viewfeed") {
527
528 $feed = $_GET["feed"];
529 $skip = $_GET["skip"];
530 $subop = $_GET["subop"];
531 $view_mode = $_GET["view"];
532 $addheader = $_GET["addheader"];
533 $limit = $_GET["limit"];
534
535 if (!$feed) {
536 print "Error: no feed to display.";
537 return;
538 }
539
540 if (!$skip) $skip = 0;
541
542 if ($subop == "undefined") $subop = "";
543
544 if ($addheader) {
545 print "<html><head>
546 <title>Tiny Tiny RSS : Feed $feed</title>
547 <link rel=\"stylesheet\" href=\"tt-rss.css\" type=\"text/css\">";
548
549 if (get_pref($link, 'USE_COMPACT_STYLESHEET')) {
550 print "<link rel=\"stylesheet\"
551 type=\"text/css\" href=\"tt-rss_compact.css\"/>";
552
553 } else {
554 print "<link title=\"Compact Stylesheet\" rel=\"alternate stylesheet\"
555 type=\"text/css\" href=\"tt-rss_compact.css\"/>";
556 }
557 print "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">
558 <script type=\"text/javascript\" src=\"functions.js\"></script>
559 <script type=\"text/javascript\" src=\"viewfeed.js\"></script>
560 </head><body onload='init()'>";
561 }
562
563 if ($subop == "ForceUpdate" && sprintf("%d", $feed) > 0) {
564
565 $tmp_result = db_query($link, "SELECT feed_url FROM ttrss_feeds
566 WHERE id = '$feed'");
567
568 $feed_url = db_fetch_result($tmp_result, 0, "feed_url");
569
570 update_rss_feed($link, $feed_url, $feed);
571
572 }
573
574 if ($subop == "MarkAllRead") {
575
576 if (sprintf("%d", $feed) != 0) {
577
578 if ($feed > 0) {
579 db_query($link, "UPDATE ttrss_user_entries
580 SET unread = false,last_read = NOW()
581 WHERE feed_id = '$feed' AND owner_uid = " . $_SESSION["uid"]);
582
583 } else if ($feed < 0 && $feed > -10) { // special, like starred
584
585 if ($feed == -1) {
586 db_query($link, "UPDATE ttrss_user_entries
587 SET unread = false,last_read = NOW()
588 WHERE marked = true AND owner_uid = ".$_SESSION["uid"]);
589 }
590
591 } else if ($feed < -10) { // label
592
593 // TODO make this more efficient
594
595 $label_id = -$feed - 11;
596
597 $tmp_result = db_query($link, "SELECT sql_exp FROM ttrss_labels
598 WHERE id = '$label_id'");
599
600 if ($tmp_result) {
601 $sql_exp = db_fetch_result($tmp_result, 0, "sql_exp");
602
603 db_query($link, "BEGIN");
604
605 $tmp2_result = db_query($link,
606 "SELECT
607 int_id
608 FROM
609 ttrss_user_entries,ttrss_entries
610 WHERE
611 ref_id = id AND
612 $sql_exp AND
613 owner_uid = " . $_SESSION["uid"]);
614
615 while ($tmp_line = db_fetch_assoc($tmp2_result)) {
616 db_query($link, "UPDATE
617 ttrss_user_entries
618 SET
619 unread = false, last_read = NOW()
620 WHERE
621 int_id = " . $tmp_line["int_id"]);
622 }
623
624 db_query($link, "COMMIT");
625
626 /* db_query($link, "UPDATE ttrss_user_entries,ttrss_entries
627 SET unread = false,last_read = NOW()
628 WHERE $sql_exp
629 AND ref_id = id
630 AND owner_uid = ".$_SESSION["uid"]); */
631 }
632 }
633 } else { // tag
634 // FIXME, implement catchup for tags
635 }
636
637 }
638
639 print "<table class=\"headlinesList\" id=\"headlinesList\" width=\"100%\">";
640
641 $search = $_GET["search"];
642
643 $search_mode = $_GET["smode"];
644
645 if ($search) {
646 $search_query_part = "(upper(title) LIKE upper('%$search%')
647 OR content LIKE '%$search%') AND";
648 } else {
649 $search_query_part = "";
650 }
651
652 $view_query_part = "";
653
654 if ($view_mode == "Starred") {
655 $view_query_part = " marked = true AND ";
656 }
657
658 if ($view_mode == "Unread") {
659 $view_query_part = " unread = true AND ";
660 }
661
662 if ($view_mode == "Unread or Starred") {
663 $view_query_part = " (unread = true OR marked = true) AND ";
664 }
665
666 if ($view_mode == "Unread or Updated") {
667 $view_query_part = " (unread = true OR last_read is NULL) AND ";
668 }
669
670 /* $result = db_query($link, "SELECT count(id) AS total_entries
671 FROM ttrss_entries WHERE
672 $search_query_part
673 feed_id = '$feed'");
674
675 $total_entries = db_fetch_result($result, 0, "total_entries"); */
676
677 /* $result = db_query("SELECT count(id) AS unread_entries
678 FROM ttrss_entries WHERE
679 $search_query_part
680 unread = true AND
681 feed_id = '$feed'");
682
683 $unread_entries = db_fetch_result($result, 0, "unread_entries"); */
684
685 if ($limit && $limit != "All") {
686 $limit_query_part = "LIMIT " . $limit;
687 }
688
689 $vfeed_query_part = "";
690
691 // override query strategy and enable feed display when searching globally
692 if ($search && $search_mode == "All feeds") {
693 $query_strategy_part = "id > 0";
694 $vfeed_query_part = "(SELECT title FROM ttrss_feeds WHERE
695 id = feed_id) as feed_title,";
696 } else if (sprintf("%d", $feed) == 0) {
697 $query_strategy_part = "ttrss_entries.id > 0";
698 $vfeed_query_part = "(SELECT title FROM ttrss_feeds WHERE
699 id = feed_id) as feed_title,";
700 } else if ($feed >= 0) {
701 $query_strategy_part = "feed_id = '$feed'";
702 } else if ($feed == -1) { // starred virtual feed
703 $query_strategy_part = "marked = true";
704 $vfeed_query_part = "(SELECT title FROM ttrss_feeds WHERE
705 id = feed_id) as feed_title,";
706 } else if ($feed <= -10) { // labels
707 $label_id = -$feed - 11;
708
709 $tmp_result = db_query($link, "SELECT sql_exp FROM ttrss_labels
710 WHERE id = '$label_id'");
711
712 $query_strategy_part = db_fetch_result($tmp_result, 0, "sql_exp");
713
714 $vfeed_query_part = "(SELECT title FROM ttrss_feeds WHERE
715 id = feed_id) as feed_title,";
716 } else {
717 $query_strategy_part = "id > 0"; // dumb
718 }
719
720
721 $order_by = "updated DESC";
722
723 // if ($feed < -10) {
724 // $order_by = "feed_id,updated DESC";
725 // }
726
727 if ($feed < -10) error_reporting (0);
728
729 if (sprintf("%d", $feed) != 0) {
730
731 $result = db_query($link, "SELECT
732 id,title,updated,unread,feed_id,marked,link,last_read,
733 SUBSTRING(last_read,1,19) as last_read_noms,
734 $vfeed_query_part
735 SUBSTRING(updated,1,19) as updated_noms
736 FROM
737 ttrss_entries,ttrss_user_entries
738 WHERE
739 ttrss_user_entries.ref_id = ttrss_entries.id AND
740 owner_uid = '".$_SESSION["uid"]."' AND
741 $search_query_part
742 $view_query_part
743 $query_strategy_part ORDER BY $order_by
744 $limit_query_part");
745
746 } else {
747 // browsing by tag
748
749 $result = db_query($link, "SELECT
750 ttrss_entries.id as id,title,updated,unread,feed_id,
751 marked,link,last_read,
752 SUBSTRING(last_read,1,19) as last_read_noms,
753 $vfeed_query_part
754 SUBSTRING(updated,1,19) as updated_noms
755 FROM
756 ttrss_entries,ttrss_user_entries,ttrss_tags
757 WHERE
758 ref_id = ttrss_entries.id AND
759 ttrss_user_entries.owner_uid = '".$_SESSION["uid"]."' AND
760 post_int_id = int_id AND tag_name = '$feed' AND
761 $view_query_part
762 $search_query_part
763 $query_strategy_part ORDER BY $order_by
764 $limit_query_part");
765 }
766
767 if (!$result) {
768 print "<tr><td colspan='4' align='center'>
769 Could not display feed (query failed). Please check match syntax or local configuration.</td></tr>";
770 return;
771 }
772
773 $lnum = 0;
774
775 error_reporting (E_ERROR | E_WARNING | E_PARSE);
776
777 $num_unread = 0;
778
779 while ($line = db_fetch_assoc($result)) {
780
781 $class = ($lnum % 2) ? "even" : "odd";
782
783 $id = $line["id"];
784 $feed_id = $line["feed_id"];
785
786 // printf("L %d (%s) &gt; U %d (%s) = %d<br>",
787 // strtotime($line["last_read_noms"]), $line["last_read_noms"],
788 // strtotime($line["updated"]), $line["updated"],
789 // strtotime($line["last_read"]) >= strtotime($line["updated"]));
790
791 /* if ($line["last_read"] != "" && $line["updated"] != "" &&
792 strtotime($line["last_read_noms"]) < strtotime($line["updated_noms"])) {
793
794 $update_pic = "<img id='FUPDPIC-$id' src=\"images/updated.png\"
795 alt=\"Updated\">";
796
797 } else {
798
799 $update_pic = "<img id='FUPDPIC-$id' src=\"images/blank_icon.gif\"
800 alt=\"Updated\">";
801
802 } */
803
804 if ($line["last_read"] == "" &&
805 ($line["unread"] != "t" && $line["unread"] != "1")) {
806
807 $update_pic = "<img id='FUPDPIC-$id' src=\"images/updated.png\"
808 alt=\"Updated\">";
809 } else {
810 $update_pic = "<img id='FUPDPIC-$id' src=\"images/blank_icon.gif\"
811 alt=\"Updated\">";
812 }
813
814 if ($line["unread"] == "t" || $line["unread"] == "1") {
815 $class .= "Unread";
816 ++$num_unread;
817 }
818
819 if ($line["marked"] == "t" || $line["marked"] == "1") {
820 $marked_pic = "<img id=\"FMARKPIC-$id\" src=\"images/mark_set.png\"
821 alt=\"Reset mark\" onclick='javascript:toggleMark($id, false)'>";
822 } else {
823 $marked_pic = "<img id=\"FMARKPIC-$id\" src=\"images/mark_unset.png\"
824 alt=\"Set mark\" onclick='javascript:toggleMark($id, true)'>";
825 }
826
827 $content_link = "<a id=\"FTITLE-$id\" href=\"javascript:view($id,$feed_id);\">" .
828 $line["title"] . "</a>";
829
830 print "<tr class='$class' id='RROW-$id'>";
831 // onclick=\"javascript:view($id,$feed_id)\">
832
833 print "<td valign='center' align='center'>$update_pic</td>";
834 print "<td valign='center' align='center'>$marked_pic</td>";
835
836 print "<td width='25%'>
837 <a href=\"javascript:view($id,$feed_id);\">".$line["updated"]."</a></td>";
838
839 if ($line["feed_title"]) {
840 print "<td width='50%'>$content_link</td>";
841 print "<td width='20%'>
842 <a href='javascript:viewfeed($feed_id)'>".$line["feed_title"]."</a></td>";
843 } else {
844 print "<td width='70%'>$content_link</td>";
845 }
846
847 print "</tr>";
848
849 ++$lnum;
850 }
851
852 if ($lnum == 0) {
853 print "<tr><td align='center'>No articles found.</td></tr>";
854 }
855
856 print "</table>";
857
858 print "<script type=\"text/javascript\">
859 document.onkeydown = hotkey_handler;
860 update_label_counters('$feed');
861 </script>";
862
863 if ($addheader) {
864 print "</body></html>";
865 }
866
867 }
868
869 if ($op == "pref-rpc") {
870
871 $subop = $_GET["subop"];
872
873 if ($subop == "unread") {
874 $ids = split(",", $_GET["ids"]);
875 foreach ($ids as $id) {
876 db_query($link, "UPDATE ttrss_entries SET unread = true WHERE feed_id = '$id'");
877 }
878
879 print "Marked selected feeds as read.";
880 }
881
882 if ($subop == "read") {
883 $ids = split(",", $_GET["ids"]);
884 foreach ($ids as $id) {
885 db_query($link, "UPDATE ttrss_entries
886 SET unread = false,last_read = NOW() WHERE feed_id = '$id'");
887 }
888
889 print "Marked selected feeds as unread.";
890
891 }
892
893 }
894
895 if ($op == "pref-feeds") {
896
897 $subop = $_GET["subop"];
898
899 if ($subop == "editSave") {
900 $feed_title = db_escape_string($_GET["t"]);
901 $feed_link = db_escape_string($_GET["l"]);
902 $upd_intl = db_escape_string($_GET["ui"]);
903 $purge_intl = db_escape_string($_GET["pi"]);
904 $feed_id = $_GET["id"];
905
906 if (strtoupper($upd_intl) == "DEFAULT")
907 $upd_intl = 0;
908
909 if (strtoupper($purge_intl) == "DEFAULT")
910 $purge_intl = 0;
911
912 if (strtoupper($purge_intl) == "DISABLED")
913 $purge_intl = -1;
914
915 $result = db_query($link, "UPDATE ttrss_feeds SET
916 title = '$feed_title', feed_url = '$feed_link',
917 update_interval = '$upd_intl',
918 purge_interval = '$purge_intl'
919 WHERE id = '$feed_id'");
920
921 }
922
923 if ($subop == "remove") {
924
925 if (!WEB_DEMO_MODE) {
926
927 $ids = split(",", $_GET["ids"]);
928
929 foreach ($ids as $id) {
930 db_query($link, "DELETE FROM ttrss_feeds WHERE id = '$id'");
931
932 $icons_dir = ICONS_DIR;
933
934 if (file_exists($icons_dir . "/$id.ico")) {
935 unlink($icons_dir . "/$id.ico");
936 }
937 }
938 }
939 }
940
941 if ($subop == "add") {
942
943 if (!WEB_DEMO_MODE) {
944
945 $feed_link = db_escape_string($_GET["link"]);
946
947 $result = db_query($link,
948 "INSERT INTO ttrss_feeds (owner_uid,feed_url,title) VALUES ('".$_SESSION["uid"]."', '$feed_link', '')");
949
950 $result = db_query($link,
951 "SELECT id FROM ttrss_feeds WHERE feed_url = '$feed_link'");
952
953 $feed_id = db_fetch_result($result, 0, "id");
954
955 if ($feed_id) {
956 update_rss_feed($link, $feed_link, $feed_id);
957 }
958 }
959 }
960
961 $result = db_query($link, "SELECT id,title,feed_url,last_error
962 FROM ttrss_feeds WHERE last_error != '' AND owner_uid = ".$_SESSION["uid"]);
963
964 if (db_num_rows($result) > 0) {
965
966 print "<div class=\"warning\">";
967
968 print "<b>Feeds with update errors:</b>";
969
970 print "<ul class=\"nomarks\">";
971
972 while ($line = db_fetch_assoc($result)) {
973 print "<li>" . $line["title"] . " (" . $line["feed_url"] . "): " .
974 $line["last_error"];
975 }
976
977 print "</ul>";
978 print "</div>";
979
980 }
981
982 print "<table class=\"prefAddFeed\"><tr>
983 <td><input id=\"fadd_link\"></td>
984 <td colspan=\"4\" align=\"right\">
985 <a class=\"button\" href=\"javascript:addFeed()\">Add feed</a></td></tr>
986 </table>";
987
988 $result = db_query($link, "SELECT
989 id,title,feed_url,substring(last_updated,1,16) as last_updated,
990 update_interval,purge_interval
991 FROM
992 ttrss_feeds WHERE owner_uid = '".$_SESSION["uid"]."' ORDER by title");
993
994 print "<p><table width=\"100%\" class=\"prefFeedList\" id=\"prefFeedList\">";
995 print "<tr class=\"title\">
996 <td>&nbsp;</td><td>Select</td><td width=\"30%\">Title</td>
997 <td width=\"30%\">Link</td>
998 <td width=\"10%\">Update Interval</td>
999 <td width=\"10%\">Purge Days</td>
1000 <td>Last updated</td></tr>";
1001
1002 $lnum = 0;
1003
1004 while ($line = db_fetch_assoc($result)) {
1005
1006 $class = ($lnum % 2) ? "even" : "odd";
1007
1008 $feed_id = $line["id"];
1009
1010 $edit_feed_id = $_GET["id"];
1011
1012 if ($subop == "edit" && $feed_id != $edit_feed_id) {
1013 $class .= "Grayed";
1014 }
1015
1016 print "<tr class=\"$class\" id=\"FEEDR-$feed_id\">";
1017
1018 $icon_file = ICONS_DIR . "/$feed_id.ico";
1019
1020 if (file_exists($icon_file) && filesize($icon_file) > 0) {
1021 $feed_icon = "<img width=\"16\" height=\"16\"
1022 src=\"" . ICONS_URL . "/$feed_id.ico\">";
1023 } else {
1024 $feed_icon = "&nbsp;";
1025 }
1026 print "<td align='center'>$feed_icon</td>";
1027
1028 $edit_title = htmlspecialchars(db_unescape_string($line["title"]));
1029 $edit_link = htmlspecialchars(db_unescape_string($line["feed_url"]));
1030
1031 if (!$edit_feed_id || $subop != "edit") {
1032
1033 print "<td><input onclick='toggleSelectRow(this);'
1034 type=\"checkbox\" id=\"FRCHK-".$line["id"]."\"></td>";
1035
1036 print "<td><a href=\"javascript:editFeed($feed_id);\">" .
1037 $edit_title . "</a></td>";
1038 print "<td><a href=\"javascript:editFeed($feed_id);\">" .
1039 $edit_link . "</a></td>";
1040
1041 if ($line["update_interval"] == "0")
1042 $line["update_interval"] = "Default";
1043
1044 print "<td><a href=\"javascript:editFeed($feed_id);\">" .
1045 $line["update_interval"] . "</a></td>";
1046
1047 if ($line["purge_interval"] == "0")
1048 $line["purge_interval"] = "Default";
1049
1050 if ($line["purge_interval"] < 0)
1051 $line["purge_interval"] = "Disabled";
1052
1053 print "<td><a href=\"javascript:editFeed($feed_id);\">" .
1054 $line["purge_interval"] . "</a></td>";
1055
1056 } else if ($feed_id != $edit_feed_id) {
1057
1058 print "<td><input disabled=\"true\" type=\"checkbox\"
1059 id=\"FRCHK-".$line["id"]."\"></td>";
1060
1061 print "<td>$edit_title</td>";
1062 print "<td>$edit_link</td>";
1063
1064 if ($line["update_interval"] == "0")
1065 $line["update_interval"] = "Default";
1066
1067 print "<td>" . $line["update_interval"] . "</td>";
1068
1069 if ($line["purge_interval"] == "0")
1070 $line["purge_interval"] = "Default";
1071
1072 if ($line["purge_interval"] < 0)
1073 $line["purge_interval"] = "Disabled";
1074
1075 print "<td>" . $line["purge_interval"] . "</td>";
1076
1077 } else {
1078
1079 print "<td><input disabled=\"true\" type=\"checkbox\" checked></td>";
1080
1081 print "<td><input id=\"iedit_title\" value=\"$edit_title\"></td>";
1082 print "<td><input id=\"iedit_link\" value=\"$edit_link\"></td>";
1083 print "<td><input id=\"iedit_updintl\" value=\"".$line["update_interval"]."\"></td>";
1084 print "<td><input id=\"iedit_purgintl\" value=\"".$line["purge_interval"]."\"></td>";
1085
1086 }
1087
1088 if (!$line["last_updated"]) $line["last_updated"] = "Never";
1089
1090 print "<td>" . $line["last_updated"] . "</td>";
1091
1092 print "</tr>";
1093
1094 ++$lnum;
1095 }
1096
1097 if ($lnum == 0) {
1098 print "<tr><td colspan=\"5\" align=\"center\">No feeds defined.</td></tr>";
1099 }
1100
1101 print "</table>";
1102
1103 print "<p>";
1104
1105 if ($subop == "edit") {
1106 print "Edit feed:&nbsp;
1107 <input type=\"submit\" class=\"button\"
1108 onclick=\"javascript:feedEditCancel()\" value=\"Cancel\">
1109 <input type=\"submit\" class=\"button\"
1110 onclick=\"javascript:feedEditSave()\" value=\"Save\">";
1111 } else {
1112
1113 print "
1114 Selection:&nbsp;
1115 <input type=\"submit\" class=\"button\"
1116 onclick=\"javascript:editSelectedFeed()\" value=\"Edit\">
1117 <input type=\"submit\" class=\"button\"
1118 onclick=\"javascript:removeSelectedFeeds()\" value=\"Remove\">";
1119
1120 if (get_pref($link, 'ENABLE_PREFS_CATCHUP_UNCATCHUP')) {
1121 print "
1122 <input type=\"submit\" class=\"button\"
1123 onclick=\"javascript:readSelectedFeeds()\" value=\"Mark as read\">
1124 <input type=\"submit\" class=\"button\"
1125 onclick=\"javascript:unreadSelectedFeeds()\" value=\"Mark as unread\">&nbsp;";
1126 }
1127 print "
1128 All feeds:
1129 <input type=\"submit\"
1130 class=\"button\" onclick=\"gotoExportOpml()\" value=\"Export OPML\">";
1131
1132 }
1133
1134 print "<h3>OPML Import</h3>
1135 <form enctype=\"multipart/form-data\" method=\"POST\" action=\"opml.php\">
1136 File: <input id=\"opml_file\" name=\"opml_file\" type=\"file\">&nbsp;
1137 <input class=\"button\" name=\"op\" onclick=\"return validateOpmlImport();\"
1138 type=\"submit\" value=\"Import\">
1139 </form>";
1140
1141 }
1142
1143 if ($op == "pref-filters") {
1144
1145 $subop = $_GET["subop"];
1146
1147 if ($subop == "editSave") {
1148
1149 $regexp = db_escape_string($_GET["r"]);
1150 $descr = db_escape_string($_GET["d"]);
1151 $match = db_escape_string($_GET["m"]);
1152 $filter_id = db_escape_string($_GET["id"]);
1153
1154 $result = db_query($link, "UPDATE ttrss_filters SET
1155 reg_exp = '$regexp',
1156 description = '$descr',
1157 filter_type = (SELECT id FROM ttrss_filter_types WHERE
1158 description = '$match')
1159 WHERE id = '$filter_id'");
1160 }
1161
1162 if ($subop == "remove") {
1163
1164 if (!WEB_DEMO_MODE) {
1165
1166 $ids = split(",", $_GET["ids"]);
1167
1168 foreach ($ids as $id) {
1169 db_query($link, "DELETE FROM ttrss_filters WHERE id = '$id'");
1170
1171 }
1172 }
1173 }
1174
1175 if ($subop == "add") {
1176
1177 if (!WEB_DEMO_MODE) {
1178
1179 $regexp = db_escape_string($_GET["regexp"]);
1180 $match = db_escape_string($_GET["match"]);
1181
1182 $result = db_query($link,
1183 "INSERT INTO ttrss_filters (reg_exp,filter_type,owner_uid) VALUES
1184 ('$regexp', (SELECT id FROM ttrss_filter_types WHERE
1185 description = '$match'),'".$_SESSION["uid"]."')");
1186 }
1187 }
1188
1189 $result = db_query($link, "SELECT description
1190 FROM ttrss_filter_types ORDER BY description");
1191
1192 $filter_types = array();
1193
1194 while ($line = db_fetch_assoc($result)) {
1195 array_push($filter_types, $line["description"]);
1196 }
1197
1198 print "<table class=\"prefAddFeed\"><tr>
1199 <td><input id=\"fadd_regexp\"></td>
1200 <td>";
1201 print_select("fadd_match", "Title", $filter_types);
1202
1203 print"</td><td colspan=\"4\" align=\"right\">
1204 <a class=\"button\" href=\"javascript:addFilter()\">Add filter</a></td></tr>
1205 </table>";
1206
1207 $result = db_query($link, "SELECT
1208 id,reg_exp,description,
1209 (SELECT name FROM ttrss_filter_types WHERE
1210 id = filter_type) as filter_type_name,
1211 (SELECT description FROM ttrss_filter_types
1212 WHERE id = filter_type) as filter_type_descr
1213 FROM
1214 ttrss_filters
1215 WHERE
1216 owner_uid = ".$_SESSION["uid"]."
1217 ORDER by reg_exp");
1218
1219 print "<p><table width=\"100%\" class=\"prefFilterList\" id=\"prefFilterList\">";
1220
1221 print "<tr class=\"title\">
1222 <td width=\"5%\">Select</td><td width=\"40%\">Filter expression</td>
1223 <td width=\"40%\">Description</td><td width=\"10%\">Match</td></tr>";
1224
1225 $lnum = 0;
1226
1227 while ($line = db_fetch_assoc($result)) {
1228
1229 $class = ($lnum % 2) ? "even" : "odd";
1230
1231 $filter_id = $line["id"];
1232 $edit_filter_id = $_GET["id"];
1233
1234 if ($subop == "edit" && $filter_id != $edit_filter_id) {
1235 $class .= "Grayed";
1236 }
1237
1238 print "<tr class=\"$class\" id=\"FILRR-$filter_id\">";
1239
1240 $line["regexp"] = htmlspecialchars($line["reg_exp"]);
1241 $line["description"] = htmlspecialchars($line["description"]);
1242
1243 if (!$edit_filter_id || $subop != "edit") {
1244
1245 if (!$line["description"]) $line["description"] = "[No description]";
1246
1247 print "<td><input onclick='toggleSelectRow(this);'
1248 type=\"checkbox\" id=\"FICHK-".$line["id"]."\"></td>";
1249
1250 print "<td><a href=\"javascript:editFilter($filter_id);\">" .
1251 $line["reg_exp"] . "</td>";
1252
1253 print "<td><a href=\"javascript:editFilter($filter_id);\">" .
1254 $line["description"] . "</td>";
1255
1256 print "<td>".$line["filter_type_descr"]."</td>";
1257
1258 } else if ($filter_id != $edit_filter_id) {
1259
1260 if (!$line["description"]) $line["description"] = "[No description]";
1261
1262 print "<td><input disabled=\"true\" type=\"checkbox\"
1263 id=\"FICHK-".$line["id"]."\"></td>";
1264
1265 print "<td>".$line["reg_exp"]."</td>";
1266 print "<td>".$line["description"]."</td>";
1267 print "<td>".$line["filter_type_descr"]."</td>";
1268
1269 } else {
1270
1271 print "<td><input disabled=\"true\" type=\"checkbox\" checked></td>";
1272
1273 print "<td><input id=\"iedit_regexp\" value=\"".$line["reg_exp"].
1274 "\"></td>";
1275
1276 print "<td><input id=\"iedit_descr\" value=\"".$line["description"].
1277 "\"></td>";
1278
1279 print "<td>";
1280 print_select("iedit_match", $line["filter_type_descr"], $filter_types);
1281 print "</td>";
1282
1283 }
1284
1285
1286 print "</tr>";
1287
1288 ++$lnum;
1289 }
1290
1291 if ($lnum == 0) {
1292 print "<tr><td colspan=\"4\" align=\"center\">No filters defined.</td></tr>";
1293 }
1294
1295 print "</table>";
1296
1297 print "<p>";
1298
1299 if ($subop == "edit") {
1300 print "Edit feed:
1301 <input type=\"submit\" class=\"button\"
1302 onclick=\"javascript:filterEditCancel()\" value=\"Cancel\">
1303 <input type=\"submit\" class=\"button\"
1304 onclick=\"javascript:filterEditSave()\" value=\"Save\">";
1305
1306 } else {
1307
1308 print "
1309 Selection:
1310 <input type=\"submit\" class=\"button\"
1311 onclick=\"javascript:editSelectedFilter()\" value=\"Edit\">
1312 <input type=\"submit\" class=\"button\"
1313 onclick=\"javascript:removeSelectedFilters()\" value=\"Remove\">";
1314 }
1315 }
1316
1317 if ($op == "pref-labels") {
1318
1319 $subop = $_GET["subop"];
1320
1321 if ($subop == "editSave") {
1322
1323 $sql_exp = $_GET["s"];
1324 $descr = $_GET["d"];
1325 $label_id = db_escape_string($_GET["id"]);
1326
1327 // print "$sql_exp : $descr : $label_id";
1328
1329 $result = db_query($link, "UPDATE ttrss_labels SET
1330 sql_exp = '$sql_exp',
1331 description = '$descr'
1332 WHERE id = '$label_id'");
1333 }
1334
1335 if ($subop == "remove") {
1336
1337 if (!WEB_DEMO_MODE) {
1338
1339 $ids = split(",", $_GET["ids"]);
1340
1341 foreach ($ids as $id) {
1342 db_query($link, "DELETE FROM ttrss_labels WHERE id = '$id'");
1343
1344 }
1345 }
1346 }
1347
1348 if ($subop == "add") {
1349
1350 if (!WEB_DEMO_MODE) {
1351
1352 $exp = $_GET["exp"];
1353
1354 $result = db_query($link,
1355 "INSERT INTO ttrss_labels (sql_exp,description,owner_uid)
1356 VALUES ('$exp', '$exp', '".$_SESSION["uid"]."')");
1357 }
1358 }
1359
1360 print "<table class=\"prefAddFeed\"><tr>
1361 <td><input id=\"ladd_expr\"></td>";
1362
1363 print"<td colspan=\"4\" align=\"right\">
1364 <a class=\"button\" href=\"javascript:addLabel()\">Add label</a></td></tr>
1365 </table>";
1366
1367 $result = db_query($link, "SELECT
1368 id,sql_exp,description
1369 FROM
1370 ttrss_labels
1371 WHERE
1372 owner_uid = ".$_SESSION["uid"]."
1373 ORDER by description");
1374
1375 print "<p><table width=\"100%\" class=\"prefLabelList\" id=\"prefLabelList\">";
1376
1377 print "<tr class=\"title\">
1378 <td width=\"5%\">Select</td><td width=\"40%\">SQL expression
1379 <a class=\"helpLink\" href=\"javascript:popupHelp(1)\">(?)</a>
1380 </td>
1381 <td width=\"40%\">Caption</td></tr>";
1382
1383 $lnum = 0;
1384
1385 while ($line = db_fetch_assoc($result)) {
1386
1387 $class = ($lnum % 2) ? "even" : "odd";
1388
1389 $label_id = $line["id"];
1390 $edit_label_id = $_GET["id"];
1391
1392 if ($subop == "edit" && $label_id != $edit_label_id) {
1393 $class .= "Grayed";
1394 }
1395
1396 print "<tr class=\"$class\" id=\"LILRR-$label_id\">";
1397
1398 $line["sql_exp"] = htmlspecialchars($line["sql_exp"]);
1399 $line["description"] = htmlspecialchars($line["description"]);
1400
1401 if (!$edit_label_id || $subop != "edit") {
1402
1403 if (!$line["description"]) $line["description"] = "[No caption]";
1404
1405 print "<td><input onclick='toggleSelectRow(this);'
1406 type=\"checkbox\" id=\"LICHK-".$line["id"]."\"></td>";
1407
1408 print "<td><a href=\"javascript:editLabel($label_id);\">" .
1409 $line["sql_exp"] . "</td>";
1410
1411 print "<td><a href=\"javascript:editLabel($label_id);\">" .
1412 $line["description"] . "</td>";
1413
1414 } else if ($label_id != $edit_label_id) {
1415
1416 if (!$line["description"]) $line["description"] = "[No description]";
1417
1418 print "<td><input disabled=\"true\" type=\"checkbox\"
1419 id=\"LICHK-".$line["id"]."\"></td>";
1420
1421 print "<td>".$line["sql_exp"]."</td>";
1422 print "<td>".$line["description"]."</td>";
1423
1424 } else {
1425
1426 print "<td><input disabled=\"true\" type=\"checkbox\" checked></td>";
1427
1428 print "<td><input id=\"iedit_expr\" value=\"".$line["sql_exp"].
1429 "\"></td>";
1430
1431 print "<td><input id=\"iedit_descr\" value=\"".$line["description"].
1432 "\"></td>";
1433
1434 }
1435
1436
1437 print "</tr>";
1438
1439 ++$lnum;
1440 }
1441
1442 if ($lnum == 0) {
1443 print "<tr><td colspan=\"4\" align=\"center\">No labels defined.</td></tr>";
1444 }
1445
1446 print "</table>";
1447
1448 print "<p>";
1449
1450 if ($subop == "edit") {
1451 print "Edit label:
1452 <input type=\"submit\" class=\"button\"
1453 onclick=\"javascript:labelEditCancel()\" value=\"Cancel\">
1454 <input type=\"submit\" class=\"button\"
1455 onclick=\"javascript:labelEditSave()\" value=\"Save\">";
1456
1457 } else {
1458
1459 print "
1460 Selection:
1461 <input type=\"submit\" class=\"button\"
1462 onclick=\"javascript:editSelectedLabel()\" value=\"Edit\">
1463 <input type=\"submit\" class=\"button\"
1464 onclick=\"javascript:removeSelectedLabels()\" value=\"Remove\">";
1465 }
1466 }
1467
1468 if ($op == "error") {
1469 print "<div width=\"100%\" align='center'>";
1470 $msg = $_GET["msg"];
1471 print $msg;
1472 print "</div>";
1473 }
1474
1475 if ($op == "help") {
1476 print "<html><head>
1477 <title>Tiny Tiny RSS : Help</title>
1478 <link rel=\"stylesheet\" href=\"tt-rss.css\" type=\"text/css\">
1479 <script type=\"text/javascript\" src=\"functions.js\"></script>
1480 <meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">
1481 </head><body>";
1482
1483 $tid = sprintf("%d", $_GET["tid"]);
1484
1485 /* FIXME this badly needs real implementation */
1486
1487 print "<div class='helpResponse'>";
1488
1489 ?>
1490
1491 <h1>Help for SQL expressions</h1>
1492
1493 <h2>Description</h2>
1494
1495 <p>The &laquo;SQL expression&raquo; is added to WHERE clause of
1496 view feed query. You can match on ttrss_entries table fields
1497 and even use subselect to query additional information. This
1498 functionality is considered to be advanced and requires basic
1499 understanding of SQL.</p>
1500
1501 <h2>Examples</h2>
1502
1503 <pre>unread = true</pre>
1504
1505 Matches all unread articles
1506
1507 <pre>title like '%Linux%'</pre>
1508
1509 Matches all articles which mention Linux in the title. You get the idea.
1510
1511 <p>See the database schema included in the distribution package for gruesome
1512 details.</p>
1513
1514 <?
1515
1516 print "<div align='center'>
1517 <a class=\"helpLink\"
1518 href=\"javascript:window.close()\">(Close this window)</a></div>";
1519
1520 print "</div>";
1521
1522 print "</body></html>";
1523
1524 }
1525
1526 if ($op == "dlg") {
1527 $id = $_GET["id"];
1528 $param = $_GET["param"];
1529
1530 if ($id == "quickAddFeed") {
1531 print "Feed URL: <input
1532 onblur=\"javascript:enableHotkeys()\" onfocus=\"javascript:disableHotkeys()\"
1533 id=\"qafInput\">
1534 <input class=\"button\"
1535 type=\"submit\" onclick=\"javascript:qafAdd()\" value=\"Add feed\">
1536 <input class=\"button\"
1537 type=\"submit\" onclick=\"javascript:closeDlg()\"
1538 value=\"Cancel\">";
1539 }
1540
1541 if ($id == "quickDelFeed") {
1542
1543 $param = db_escape_string($param);
1544
1545 $result = db_query($link, "SELECT title FROM ttrss_feeds WHERE id = '$param'");
1546
1547 if ($result) {
1548
1549 $f_title = db_fetch_result($result, 0, "title");
1550
1551 print "Remove current feed ($f_title)?&nbsp;
1552 <input class=\"button\"
1553 type=\"submit\" onclick=\"javascript:qfdDelete($param)\" value=\"Remove\">
1554 <input class=\"button\"
1555 type=\"submit\" onclick=\"javascript:closeDlg()\"
1556 value=\"Cancel\">";
1557 } else {
1558 print "Error: Feed $param not found.&nbsp;
1559 <input class=\"button\"
1560 type=\"submit\" onclick=\"javascript:closeDlg()\"
1561 value=\"Cancel\">";
1562 }
1563 }
1564
1565 if ($id == "search") {
1566
1567 print "<input id=\"searchbox\" class=\"extSearch\"
1568 onblur=\"javascript:enableHotkeys()\" onfocus=\"javascript:disableHotkeys()\"
1569 onchange=\"javascript:search()\">
1570 <select id=\"searchmodebox\">
1571 <option selected>All feeds</option>
1572 <option>This feed</option>
1573 </select>
1574 <input type=\"submit\"
1575 class=\"button\" onclick=\"javascript:search()\" value=\"Search\">
1576 <input class=\"button\"
1577 type=\"submit\" onclick=\"javascript:closeDlg()\"
1578 value=\"Close\">";
1579
1580 }
1581
1582 }
1583
1584 if ($op == "updateAllFeeds") {
1585 update_all_feeds($link, true);
1586
1587 print "<rpc-reply>";
1588 getLabelCounters($link);
1589 getFeedCounters($link);
1590 getTagCounters($link);
1591 getGlobalCounters($link);
1592 print "</rpc-reply>";
1593
1594 }
1595
1596 if ($op == "pref-prefs") {
1597
1598 $subop = $_REQUEST["subop"];
1599
1600 if ($subop == "Save configuration") {
1601
1602 if (WEB_DEMO_MODE) return;
1603
1604 foreach (array_keys($_POST) as $pref_name) {
1605
1606 $pref_name = db_escape_string($pref_name);
1607 $value = db_escape_string($_POST[$pref_name]);
1608
1609 $result = db_query($link, "SELECT type_name
1610 FROM ttrss_prefs,ttrss_prefs_types
1611 WHERE pref_name = '$pref_name' AND type_id = ttrss_prefs_types.id");
1612
1613 if (db_num_rows($result) > 0) {
1614
1615 $type_name = db_fetch_result($result, 0, "type_name");
1616
1617 // print "$pref_name : $type_name : $value<br>";
1618
1619 if ($type_name == "bool") {
1620 if ($value == "1") {
1621 $value = "true";
1622 } else {
1623 $value = "false";
1624 }
1625 } else if ($type_name == "integer") {
1626 $value = sprintf("%d", $value);
1627 }
1628
1629 // print "$pref_name : $type_name : $value<br>";
1630
1631 db_query($link, "UPDATE ttrss_user_prefs SET value = '$value'
1632 WHERE pref_name = '$pref_name' AND owner_uid = ".$_SESSION["uid"]);
1633
1634 }
1635
1636 header("Location: prefs.php");
1637
1638 }
1639
1640 } else if ($subop == "getHelp") {
1641
1642 $pref_name = db_escape_string($_GET["pn"]);
1643
1644 $result = db_query($link, "SELECT help_text FROM ttrss_prefs
1645 WHERE pref_name = '$pref_name'");
1646
1647 if (db_num_rows($result) > 0) {
1648 $help_text = db_fetch_result($result, 0, "help_text");
1649 print $help_text;
1650 } else {
1651 print "Unknown option: $pref_name";
1652 }
1653
1654 } else if ($subop == "Change password") {
1655
1656 if (WEB_DEMO_MODE) return;
1657
1658 $old_pw = $_POST["OLD_PASSWORD"];
1659 $new_pw = $_POST["OLD_PASSWORD"];
1660
1661 $old_pw_hash = 'SHA1:' . sha1($_POST["OLD_PASSWORD"]);
1662 $new_pw_hash = 'SHA1:' . sha1($_POST["NEW_PASSWORD"]);
1663
1664 $active_uid = $_SESSION["uid"];
1665
1666 if ($old_pw && $new_pw) {
1667
1668 $login = db_escape_string($_SERVER['PHP_AUTH_USER']);
1669
1670 $result = db_query($link, "SELECT id FROM ttrss_users WHERE
1671 id = '$active_uid' AND (pwd_hash = '$old_pw' OR
1672 pwd_hash = '$old_pw_hash')");
1673
1674 if (db_num_rows($result) == 1) {
1675 db_query($link, "UPDATE ttrss_users SET pwd_hash = '$new_pw_hash'
1676 WHERE id = '$active_uid'");
1677 }
1678 }
1679
1680 header("Location: prefs.php");
1681
1682 } else if ($subop == "Reset to defaults") {
1683
1684 if (WEB_DEMO_MODE) return;
1685
1686 if (DB_TYPE == "pgsql") {
1687 db_query($link,"UPDATE ttrss_user_prefs
1688 SET value = ttrss_prefs.def_value
1689 WHERE owner_uid = '".$_SESSION["uid"]."' AND
1690 ttrss_prefs.pref_name = ttrss_user_prefs.pref_name");
1691 } else {
1692 db_query($link, "DELETE FROM ttrss_user_prefs
1693 WHERE owner_uid = ".$_SESSION["uid"]);
1694 initialize_user_prefs($link, $_SESSION["uid"]);
1695 }
1696
1697 header("Location: prefs.php");
1698
1699 } else {
1700
1701 if (!SINGLE_USER_MODE) {
1702
1703 print "<form action=\"backend.php\" method=\"POST\">";
1704
1705 print "<table width=\"100%\" class=\"prefPrefsList\">";
1706 print "<tr><td colspan='3'><h3>Authentication</h3></tr></td>";
1707
1708 print "<tr><td width=\"40%\">Old password</td>";
1709 print "<td><input class=\"editbox\" type=\"password\"
1710 name=\"OLD_PASSWORD\"></td></tr>";
1711
1712 print "<tr><td width=\"40%\">New password</td>";
1713
1714 print "<td><input class=\"editbox\" type=\"password\"
1715 name=\"NEW_PASSWORD\"></td></tr>";
1716
1717 print "</table>";
1718
1719 print "<input type=\"hidden\" name=\"op\" value=\"pref-prefs\">";
1720
1721 print "<p><input class=\"button\" type=\"submit\"
1722 value=\"Change password\" name=\"subop\">";
1723
1724 print "</form>";
1725
1726 }
1727
1728 $result = db_query($link, "SELECT
1729 ttrss_user_prefs.pref_name,short_desc,help_text,value,type_name,
1730 section_name,def_value
1731 FROM ttrss_prefs,ttrss_prefs_types,ttrss_prefs_sections,ttrss_user_prefs
1732 WHERE type_id = ttrss_prefs_types.id AND
1733 section_id = ttrss_prefs_sections.id AND
1734 ttrss_user_prefs.pref_name = ttrss_prefs.pref_name AND
1735 owner_uid = ".$_SESSION["uid"]."
1736 ORDER BY section_id,short_desc");
1737
1738 print "<form action=\"backend.php\" method=\"POST\">";
1739
1740 $lnum = 0;
1741
1742 $active_section = "";
1743
1744 while ($line = db_fetch_assoc($result)) {
1745
1746 if ($active_section != $line["section_name"]) {
1747
1748 if ($active_section != "") {
1749 print "</table>";
1750 }
1751
1752 print "<p><table width=\"100%\" class=\"prefPrefsList\">";
1753
1754 $active_section = $line["section_name"];
1755
1756 print "<tr><td colspan=\"3\"><h3>$active_section</h3></td></tr>";
1757 // print "<tr class=\"title\">
1758 // <td width=\"25%\">Option</td><td>Value</td></tr>";
1759
1760 $lnum = 0;
1761 }
1762
1763 // $class = ($lnum % 2) ? "even" : "odd";
1764
1765 print "<tr>";
1766
1767 $type_name = $line["type_name"];
1768 $pref_name = $line["pref_name"];
1769 $value = $line["value"];
1770 $def_value = $line["def_value"];
1771 $help_text = $line["help_text"];
1772
1773 print "<td width=\"40%\" id=\"$pref_name\">" . $line["short_desc"];
1774
1775 if ($help_text) print "<div class=\"prefHelp\">$help_text</div>";
1776
1777 print "</td>";
1778
1779 print "<td>";
1780
1781 if ($type_name == "bool") {
1782 // print_select($pref_name, $value, array("true", "false"));
1783
1784 if ($value == "true") {
1785 $value = "Yes";
1786 } else {
1787 $value = "No";
1788 }
1789
1790 print_radio($pref_name, $value, array("Yes", "No"));
1791
1792 } else {
1793 print "<input class=\"editbox\" name=\"$pref_name\" value=\"$value\">";
1794 }
1795
1796 print "</td>";
1797
1798 print "</tr>";
1799
1800 $lnum++;
1801 }
1802
1803 print "</table>";
1804
1805 print "<input type=\"hidden\" name=\"op\" value=\"pref-prefs\">";
1806
1807 print "<p><input class=\"button\" type=\"submit\"
1808 name=\"subop\" value=\"Save configuration\">";
1809
1810 print "&nbsp;<input class=\"button\" type=\"submit\"
1811 name=\"subop\" value=\"Reset to defaults\"></p>";
1812
1813 print "</form>";
1814
1815 }
1816
1817 }
1818
1819 if ($op == "pref-users") {
1820
1821 $subop = $_GET["subop"];
1822
1823 if ($subop == "editSave") {
1824
1825 if (!WEB_DEMO_MODE) {
1826
1827 $login = db_escape_string($_GET["l"]);
1828 $uid = db_escape_string($_GET["id"]);
1829 $access_level = sprintf("%d", $_GET["al"]);
1830
1831 db_query($link, "UPDATE ttrss_users SET login = '$login', access_level = '$access_level' WHERE id = '$uid'");
1832
1833 }
1834 } else if ($subop == "remove") {
1835
1836 if (!WEB_DEMO_MODE && $_SESSION["access_level"] >= 10) {
1837
1838 $ids = split(",", $_GET["ids"]);
1839
1840 foreach ($ids as $id) {
1841 db_query($link, "DELETE FROM ttrss_users WHERE id = '$id' AND id != " . $_SESSION["uid"]);
1842
1843 }
1844 }
1845 } else if ($subop == "add") {
1846
1847 if (!WEB_DEMO_MODE && $_SESSION["access_level"] >= 10) {
1848
1849 $login = db_escape_string($_GET["login"]);
1850 $tmp_user_pwd = make_password(8);
1851 $pwd_hash = 'SHA1:' . sha1($tmp_user_pwd);
1852
1853 db_query($link, "INSERT INTO ttrss_users (login,pwd_hash,access_level)
1854 VALUES ('$login', '$pwd_hash', 0)");
1855
1856
1857 $result = db_query($link, "SELECT id FROM ttrss_users WHERE
1858 login = '$login' AND pwd_hash = '$pwd_hash'");
1859
1860 if (db_num_rows($result) == 1) {
1861
1862 $new_uid = db_fetch_result($result, 0, "id");
1863
1864 print "<div class=\"notice\">Added user <b>".$_GET["login"].
1865 "</b> with password <b>$tmp_user_pwd</b>.</div>";
1866
1867 initialize_user($link, $new_uid);
1868
1869 } else {
1870
1871 print "<div class=\"warning\">Error while adding user <b>".
1872 $_GET["login"].".</b></div>";
1873
1874 }
1875 }
1876 } else if ($subop == "resetPass") {
1877
1878 if (!WEB_DEMO_MODE && $_SESSION["access_level"] >= 10) {
1879
1880 $uid = db_escape_string($_GET["id"]);
1881
1882 $result = db_query($link, "SELECT login FROM ttrss_users WHERE id = '$uid'");
1883
1884 $login = db_fetch_result($result, 0, "login");
1885 $tmp_user_pwd = make_password(8);
1886 $pwd_hash = 'SHA1:' . sha1($tmp_user_pwd);
1887
1888 db_query($link, "UPDATE ttrss_users SET pwd_hash = '$pwd_hash'
1889 WHERE id = '$uid'");
1890
1891 print "<div class=\"notice\">Changed password of
1892 user <b>$login</b> to <b>$tmp_user_pwd</b>.</div>";
1893
1894 }
1895 }
1896
1897 print "<table class=\"prefAddFeed\"><tr>
1898 <td><input id=\"uadd_box\"></td>";
1899
1900 print"<td colspan=\"4\" align=\"right\">
1901 <a class=\"button\" href=\"javascript:addUser()\">Add user</a></td></tr>
1902 </table>";
1903
1904 $result = db_query($link, "SELECT
1905 id,login,access_level,last_login
1906 FROM
1907 ttrss_users
1908 ORDER by login");
1909
1910 print "<div id=\"prefUserDetails\">PLACEHOLDER</div>";
1911
1912 print "<p><table width=\"100%\" class=\"prefUserList\" id=\"prefUserList\">";
1913
1914 print "<tr class=\"title\">
1915 <td width=\"5%\">Select</td>
1916 <td width='30%'>Username</td>
1917 <td width='30%'>Access Level</td>
1918 <td width='30%'>Last login</td></tr>";
1919
1920 $lnum = 0;
1921
1922 while ($line = db_fetch_assoc($result)) {
1923
1924 $class = ($lnum % 2) ? "even" : "odd";
1925
1926 $uid = $line["id"];
1927 $edit_uid = $_GET["id"];
1928
1929 if ($uid == $_SESSION["uid"] || ($subop == "edit" && $uid != $edit_uid)) {
1930 $class .= "Grayed";
1931 }
1932
1933 print "<tr class=\"$class\" id=\"UMRR-$uid\">";
1934
1935 $line["login"] = htmlspecialchars($line["login"]);
1936
1937 if ($uid == $_SESSION["uid"]) {
1938
1939 print "<td><input disabled=\"true\" type=\"checkbox\"
1940 id=\"UMCHK-".$line["id"]."\"></td>";
1941
1942 print "<td>".$line["login"]."</td>";
1943 print "<td>".$line["access_level"]."</td>";
1944
1945 } else if (!$edit_uid || $subop != "edit") {
1946
1947 print "<td><input onclick='toggleSelectRow(this);'
1948 type=\"checkbox\" id=\"UMCHK-$uid\"></td>";
1949
1950 print "<td><a href=\"javascript:editUser($uid);\">" .
1951 $line["login"] . "</td>";
1952
1953 print "<td><a href=\"javascript:editUser($uid);\">" .
1954 $line["access_level"] . "</td>";
1955
1956 } else if ($uid != $edit_uid) {
1957
1958 print "<td><input disabled=\"true\" type=\"checkbox\"
1959 id=\"UMCHK-".$line["id"]."\"></td>";
1960
1961 print "<td>".$line["login"]."</td>";
1962 print "<td>".$line["access_level"]."</td>";
1963
1964 } else {
1965
1966 print "<td><input disabled=\"true\" type=\"checkbox\" checked></td>";
1967
1968 print "<td><input id=\"iedit_ulogin\" value=\"".$line["login"].
1969 "\"></td>";
1970
1971 print "<td><input id=\"iedit_ulevel\" value=\"".$line["access_level"].
1972 "\"></td>";
1973
1974 }
1975
1976 print "<td>".$line["last_login"]."</td>";
1977
1978 print "</tr>";
1979
1980 ++$lnum;
1981 }
1982
1983 print "</table>";
1984
1985 print "<p>";
1986
1987 if ($subop == "edit") {
1988 print "Edit label:
1989 <input type=\"submit\" class=\"button\"
1990 onclick=\"javascript:userEditCancel()\" value=\"Cancel\">
1991 <input type=\"submit\" class=\"button\"
1992 onclick=\"javascript:userEditSave()\" value=\"Save\">";
1993
1994 } else {
1995
1996 print "
1997 Selection:
1998 <input type=\"submit\" class=\"button\"
1999 onclick=\"javascript:selectedUserDetails()\" value=\"User details\">
2000 <input type=\"submit\" class=\"button\"
2001 onclick=\"javascript:editSelectedUser()\" value=\"Edit\">
2002 <input type=\"submit\" class=\"button\"
2003 onclick=\"javascript:removeSelectedUsers()\" value=\"Remove\">
2004 <input type=\"submit\" class=\"button\"
2005 onclick=\"javascript:resetSelectedUserPass()\" value=\"Reset password\">";
2006
2007 }
2008 }
2009
2010 if ($op == "user-details") {
2011
2012 if (WEB_DEMO_MODE || $_SESSION["access_level"] < 10) {
2013 return;
2014 }
2015
2016 /* print "<html><head>
2017 <title>Tiny Tiny RSS : User Details</title>
2018 <link rel=\"stylesheet\" href=\"tt-rss.css\" type=\"text/css\">
2019 <meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">
2020 </head><body>"; */
2021
2022 $uid = sprintf("%d", $_GET["id"]);
2023
2024 print "<div class='userDetails'>";
2025
2026 $result = db_query($link, "SELECT login,last_login,access_level
2027 FROM ttrss_users
2028 WHERE id = '$uid'");
2029
2030 if (db_num_rows($result) == 0) {
2031 print "<h1>User not found</h1>";
2032 return;
2033 }
2034
2035 print "<h1>User Details</h1>";
2036
2037 print "<table width='100%'>";
2038
2039 $login = db_fetch_result($result, 0, "login");
2040 $last_login = db_fetch_result($result, 0, "last_login");
2041 $access_level = db_fetch_result($result, 0, "access_level");
2042
2043 print "<tr><td>Username</td><td>$login</td></tr>";
2044 print "<tr><td>Access level</td><td>$access_level</td></tr>";
2045 print "<tr><td>Last logged in</td><td>$last_login</td></tr>";
2046
2047 $result = db_query($link, "SELECT COUNT(id) as num_feeds FROM ttrss_feeds
2048 WHERE owner_uid = '$uid'");
2049
2050 $num_feeds = db_fetch_result($result, 0, "num_feeds");
2051
2052 print "<tr><td>Subscribed feeds count</td><td>$num_feeds</td></tr>";
2053
2054 /* $result = db_query($link, "SELECT
2055 SUM(LENGTH(content)+LENGTH(title)+LENGTH(link)+LENGTH(guid)) AS db_size
2056 FROM ttrss_entries WHERE owner_uid = '$uid'");
2057
2058 $db_size = round(db_fetch_result($result, 0, "db_size") / 1024);
2059
2060 print "<tr><td>Approx. DB size</td><td>$db_size KBytes</td></tr>"; */
2061
2062 print "</table>";
2063
2064 print "<h1>Subscribed feeds</h1>";
2065
2066 $result = db_query($link, "SELECT id,title,feed_url FROM ttrss_feeds
2067 WHERE owner_uid = '$uid' ORDER BY title");
2068
2069 print "<ul class=\"nomarks\">";
2070
2071 while ($line = db_fetch_assoc($result)) {
2072
2073 $icon_file = ICONS_URL."/".$line["id"].".ico";
2074
2075 if (file_exists($icon_file) && filesize($icon_file) > 0) {
2076 $feed_icon = "<img class=\"tinyFeedIcon\" src=\"$icon_file\">";
2077 } else {
2078 $feed_icon = "<img class=\"feedIcon\" src=\"images/blank_icon.gif\">";
2079 }
2080
2081 print "<li>$feed_icon&nbsp;<a href=\"".$line["feed_url"]."\">".$line["title"]."</a></li>";
2082 }
2083
2084 print "</ul>";
2085
2086 print "</div>";
2087
2088 print "<div align='center'>
2089 <input type='submit' class='button'
2090 onclick=\"closeUserDetails()\" value=\"Close this window\"></div>";
2091
2092 // print "</body></html>";
2093
2094 }
2095
2096 db_close($link);
2097 ?>
2098
2099 <!-- <?= sprintf("Backend execution time: %.4f seconds", getmicrotime() - $script_started) ?> -->
2100