]> git.wh0rd.org - tt-rss.git/blob - backend.php
fix owner_uid checking in OPML export (path2)
[tt-rss.git] / backend.php
1 <?
2 session_start();
3
4 if ($_GET["debug"]) {
5 define('DEFAULT_ERROR_LEVEL', E_ALL);
6 } else {
7 define('DEFAULT_ERROR_LEVEL', E_ERROR | E_WARNING | E_PARSE);
8 }
9
10 error_reporting(DEFAULT_ERROR_LEVEL);
11
12 $op = $_REQUEST["op"];
13
14 if ((!$op || $op == "rpc" || $op == "globalUpdateFeeds") && !$_REQUEST["noxml"]) {
15 header("Content-Type: application/xml");
16 }
17
18 if (!$_SESSION["uid"] && $op != "globalUpdateFeeds") {
19
20 if ($op == "rpc") {
21 print "<error error-code=\"6\"/>";
22 }
23 exit;
24 }
25
26 if (!$op) {
27 print "<error error-code=\"7\"/>";
28 exit;
29 }
30
31 define('SCHEMA_VERSION', 2);
32
33 require_once "sanity_check.php";
34 require_once "config.php";
35 require_once "db.php";
36 require_once "db-prefs.php";
37 require_once "functions.php";
38 require_once "magpierss/rss_fetch.inc";
39
40 $script_started = getmicrotime();
41
42 $link = db_connect(DB_HOST, DB_USER, DB_PASS, DB_NAME);
43
44 if (!$link) {
45 if (DB_TYPE == "mysql") {
46 print mysql_error();
47 }
48 // PG seems to display its own errors just fine by default.
49 return;
50 }
51
52 if (DB_TYPE == "pgsql") {
53 pg_query("set client_encoding = 'utf-8'");
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 (DEFAULT_ERROR_LEVEL);
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 if (get_pref($link, 'ENABLE_FEED_CATS')) {
185 print "<li class=\"feedCat\">Special</li>";
186 print "<li id=\"feedCatHolder\"><ul class=\"feedCatList\">";
187 }
188
189 $result = db_query($link, "SELECT count(id) as num_starred
190 FROM ttrss_entries,ttrss_user_entries
191 WHERE marked = true AND
192 ttrss_user_entries.ref_id = ttrss_entries.id AND
193 unread = true AND owner_uid = '$owner_uid'");
194 $num_starred = db_fetch_result($result, 0, "num_starred");
195
196 $class = "virt";
197
198 if ($num_starred > 0) $class .= "Unread";
199
200 printFeedEntry(-1, $class, "Starred articles", $num_starred,
201 "images/mark_set.png", $link);
202
203 if (get_pref($link, 'ENABLE_FEED_CATS')) {
204 print "</li></ul>";
205 }
206
207 if (get_pref($link, 'ENABLE_LABELS')) {
208
209 $result = db_query($link, "SELECT id,sql_exp,description FROM
210 ttrss_labels WHERE owner_uid = '$owner_uid' ORDER by description");
211
212 if (db_num_rows($result) > 0) {
213 if (get_pref($link, 'ENABLE_FEED_CATS')) {
214 print "<li class=\"feedCat\">Labels</li>";
215 print "<li id=\"feedCatHolder\"><ul class=\"feedCatList\">";
216 } else {
217 print "<li><hr></li>";
218 }
219 }
220
221 while ($line = db_fetch_assoc($result)) {
222
223 error_reporting (0);
224
225 $tmp_result = db_query($link, "SELECT count(id) as count FROM ttrss_entries,ttrss_user_entries
226 WHERE (" . $line["sql_exp"] . ") AND unread = true AND
227 ttrss_user_entries.ref_id = ttrss_entries.id
228 AND owner_uid = '$owner_uid'");
229
230 $count = db_fetch_result($tmp_result, 0, "count");
231
232 $class = "label";
233
234 if ($count > 0) {
235 $class .= "Unread";
236 }
237
238 error_reporting (DEFAULT_ERROR_LEVEL);
239
240 printFeedEntry(-$line["id"]-11,
241 $class, $line["description"], $count, "images/label.png", $link);
242
243 }
244
245 if (db_num_rows($result) > 0) {
246 if (get_pref($link, 'ENABLE_FEED_CATS')) {
247 print "</li></ul>";
248 }
249 }
250
251 }
252
253 // if (!get_pref($link, 'ENABLE_FEED_CATS')) {
254 print "<li><hr></li>";
255 // }
256
257 if (get_pref($link, 'ENABLE_FEED_CATS')) {
258 $order_by_qpart = "category,title";
259 } else {
260 $order_by_qpart = "title";
261 }
262
263 $result = db_query($link, "SELECT *,
264 (SELECT count(id) FROM ttrss_entries,ttrss_user_entries
265 WHERE feed_id = ttrss_feeds.id AND
266 ttrss_user_entries.ref_id = ttrss_entries.id AND
267 owner_uid = '$owner_uid') AS total,
268 (SELECT count(id) FROM ttrss_entries,ttrss_user_entries
269 WHERE feed_id = ttrss_feeds.id AND unread = true
270 AND ttrss_user_entries.ref_id = ttrss_entries.id
271 AND owner_uid = '$owner_uid') as unread,
272 (SELECT title FROM ttrss_feed_categories
273 WHERE id = cat_id) AS category
274 FROM ttrss_feeds WHERE owner_uid = '$owner_uid' ORDER BY $order_by_qpart");
275
276 $actid = $_GET["actid"];
277
278 /* real feeds */
279
280 $lnum = 0;
281
282 $total_unread = 0;
283
284 $category = "";
285
286 while ($line = db_fetch_assoc($result)) {
287
288 $feed = $line["title"];
289 $feed_id = $line["id"];
290
291 $subop = $_GET["subop"];
292
293 $total = $line["total"];
294 $unread = $line["unread"];
295
296 $tmp_category = $line["category"];
297
298 if (!$tmp_category) {
299 $tmp_category = "Uncategorized";
300 }
301
302 // $class = ($lnum % 2) ? "even" : "odd";
303
304 $class = "feed";
305
306 if ($unread > 0) $class .= "Unread";
307
308 if ($actid == $feed_id) {
309 $class .= "Selected";
310 }
311
312 $total_unread += $unread;
313
314 if ($category != $tmp_category && get_pref($link, 'ENABLE_FEED_CATS')) {
315
316 if ($category) {
317 print "</li></ul></li>";
318 }
319
320 $category = $tmp_category;
321
322 print "<li class=\"feedCat\">$category</li>";
323 print "<li id=\"feedCatHolder\"><ul class=\"feedCatList\">";
324 }
325
326 printFeedEntry($feed_id, $class, $feed, $unread,
327 "icons/$feed_id.ico", $link);
328
329 ++$lnum;
330 }
331
332 } else {
333
334 // tags
335
336 $result = db_query($link, "SELECT tag_name,count(ttrss_entries.id) AS count
337 FROM ttrss_tags,ttrss_entries,ttrss_user_entries WHERE
338 post_int_id = ttrss_user_entries.int_id AND
339 unread = true AND ref_id = ttrss_entries.id
340 AND ttrss_tags.owner_uid = '$owner_uid' GROUP BY tag_name
341 UNION
342 select tag_name,0 as count FROM ttrss_tags WHERE owner_uid = '$owner_uid'
343 ORDER BY tag_name");
344
345 $tags = array();
346
347 while ($line = db_fetch_assoc($result)) {
348 $tags[$line["tag_name"]] += $line["count"];
349 }
350
351 foreach (array_keys($tags) as $tag) {
352
353 $unread = $tags[$tag];
354
355 $class = "odd";
356
357 if ($unread > 0) {
358 $class .= "Unread";
359 }
360
361 printFeedEntry($tag, $class, $tag, $unread, "images/tag.png", $link);
362
363 }
364
365 }
366
367 if (db_num_rows($result) == 0) {
368 if ($tags) {
369 $what = "tags";
370 } else {
371 $what = "feeds";
372 }
373 print "<li>No $what to display.</li>";
374 }
375
376 print "</ul>";
377
378 print "<div class=\"invisible\" id=\"FEEDTU\">$total_unread</div>";
379
380 }
381
382
383 if ($op == "rpc") {
384
385 $subop = $_GET["subop"];
386
387 if ($subop == "getLabelCounters") {
388 $aid = $_GET["aid"];
389 print "<rpc-reply>";
390 getLabelCounters($link);
391 if ($aid) {
392 getFeedCounter($link, $aid);
393 }
394 print "</rpc-reply>";
395 }
396
397 if ($subop == "getFeedCounters") {
398 print "<rpc-reply>";
399 getFeedCounters($link);
400 print "</rpc-reply>";
401 }
402
403 if ($subop == "getAllCounters") {
404 print "<rpc-reply>";
405 getLabelCounters($link);
406 getFeedCounters($link);
407 getTagCounters($link);
408 getGlobalCounters($link);
409 print "</rpc-reply>";
410 }
411
412 if ($subop == "mark") {
413 $mark = $_GET["mark"];
414 $id = db_escape_string($_GET["id"]);
415
416 if ($mark == "1") {
417 $mark = "true";
418 } else {
419 $mark = "false";
420 }
421
422 // FIXME this needs collision testing
423
424 $result = db_query($link, "UPDATE ttrss_user_entries SET marked = $mark
425 WHERE ref_id = '$id' AND owner_uid = " . $_SESSION["uid"]);
426 }
427
428 if ($subop == "updateFeed") {
429 $feed_id = db_escape_string($_GET["feed"]);
430
431 $result = db_query($link,
432 "SELECT feed_url FROM ttrss_feeds WHERE id = '$feed_id'
433 AND owner_uid = " . $_SESSION["uid"]);
434
435 if (db_num_rows($result) > 0) {
436 $feed_url = db_fetch_result($result, 0, "feed_url");
437 update_rss_feed($link, $feed_url, $feed_id);
438 }
439
440 print "<rpc-reply>";
441 getFeedCounter($link, $feed_id);
442 print "</rpc-reply>";
443
444 return;
445 }
446
447 if ($subop == "forceUpdateAllFeeds" || $subop == "updateAllFeeds") {
448
449 update_all_feeds($link, $subop == "forceUpdateAllFeeds");
450
451 $omode = $_GET["omode"];
452
453 if (!$omode) $omode = "tfl";
454
455 print "<rpc-reply>";
456 if (strchr($omode, "l")) getLabelCounters($link);
457 if (strchr($omode, "f")) getFeedCounters($link);
458 if (strchr($omode, "t")) getTagCounters($link);
459 getGlobalCounters($link);
460 print "</rpc-reply>";
461 }
462
463 if ($subop == "catchupSelected") {
464
465 $ids = split(",", $_GET["ids"]);
466
467 foreach ($ids as $id) {
468
469 db_query($link, "UPDATE ttrss_user_entries SET unread=false,last_read = NOW()
470 WHERE id = '$id' AND owner_uid = " . $_SESSION["uid"]);
471
472 }
473
474 print "Marked active page as read.";
475 }
476
477 if ($subop == "sanityCheck") {
478
479 $error_code = 0;
480
481 $result = db_query($link, "SELECT schema_version FROM ttrss_version");
482
483 $schema_version = db_fetch_result($result, 0, "schema_version");
484
485 if ($schema_version != SCHEMA_VERSION) {
486 $error_code = 5;
487 }
488
489 print "<error error-code='$error_code'/>";
490 }
491
492 if ($subop == "globalPurge") {
493
494 print "<rpc-reply>";
495 global_purge_old_posts($link, true);
496 print "</rpc-reply>";
497
498 }
499
500 }
501
502 if ($op == "feeds") {
503
504 $tags = $_GET["tags"];
505
506 $subop = $_GET["subop"];
507
508 if ($subop == "catchupAll") {
509 db_query($link, "UPDATE ttrss_user_entries SET
510 last_read = NOW(),unread = false WHERE owner_uid = " . $_SESSION["uid"]);
511 }
512
513 outputFeedList($link, $tags);
514
515 }
516
517 if ($op == "view") {
518
519 $id = $_GET["id"];
520 $feed_id = $_GET["feed"];
521
522 $result = db_query($link, "UPDATE ttrss_user_entries
523 SET unread = false,last_read = NOW()
524 WHERE ref_id = '$id' AND feed_id = '$feed_id' AND owner_uid = " . $_SESSION["uid"]);
525
526 $addheader = $_GET["addheader"];
527
528 $result = db_query($link, "SELECT title,link,content,feed_id,comments,int_id,
529 (SELECT icon_url FROM ttrss_feeds WHERE id = feed_id) as icon_url
530 FROM ttrss_entries,ttrss_user_entries
531 WHERE id = '$id' AND ref_id = id");
532
533 if ($addheader) {
534 print "<html><head>
535 <title>Tiny Tiny RSS : Article $id</title>
536 <link rel=\"stylesheet\" href=\"tt-rss.css\" type=\"text/css\">
537 <script type=\"text/javascript\" src=\"functions.js\"></script>
538 <meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">
539 </head><body>";
540 }
541
542 if ($result) {
543
544 $line = db_fetch_assoc($result);
545
546 if ($line["icon_url"]) {
547 $feed_icon = "<img class=\"feedIcon\" src=\"" . $line["icon_url"] . "\">";
548 } else {
549 $feed_icon = "&nbsp;";
550 }
551
552 if ($line["comments"] && $line["link"] != $line["comments"]) {
553 $entry_comments = "(<a href=\"".$line["comments"]."\">Comments</a>)";
554 } else {
555 $entry_comments = "";
556 }
557
558 print "<div class=\"postReply\">";
559
560 print "<div class=\"postHeader\"><table width=\"100%\">";
561
562 print "<tr><td colspan='2'>" . $line["title"] . "</td></tr>";
563
564 $tmp_result = db_query($link, "SELECT DISTINCT tag_name FROM
565 ttrss_tags WHERE post_int_id = " . $line["int_id"] . "
566 ORDER BY tag_name");
567
568 $tags_str = "";
569
570 while ($tmp_line = db_fetch_assoc($tmp_result)) {
571 $tag = $tmp_line["tag_name"];
572 $tags_str .= "<a href=\"javascript:parent.viewfeed('$tag')\">$tag</a>, ";
573 }
574
575 $tags_str = preg_replace("/, $/", "", $tags_str);
576
577 print "<tr><td width='50%'>
578 <a href=\"" . $line["link"] . "\">".$line["link"]."</a>
579 $entry_comments</td>
580 <td align=\"right\">$tags_str</td></tr>";
581
582 /* if ($tags_str) {
583 print "<tr><td><b>Tags:</b></td>
584 <td width='100%'>$tags_str</td></tr>";
585 } */
586
587 print "</table></div>";
588
589 print "<div class=\"postIcon\">" . $feed_icon . "</div>";
590 print "<div class=\"postContent\">" . $line["content"] . "</div>";
591
592 print "</div>";
593
594 print "<script type=\"text/javascript\">
595 update_label_counters('$feed_id');
596 </script>";
597 }
598
599 if ($addheader) {
600 print "</body></html>";
601 }
602 }
603
604 if ($op == "viewfeed") {
605
606 $feed = $_GET["feed"];
607 $skip = $_GET["skip"];
608 $subop = $_GET["subop"];
609 $view_mode = $_GET["view"];
610 $addheader = $_GET["addheader"];
611 $limit = $_GET["limit"];
612
613 if (!$feed) {
614 return;
615 }
616
617 if (!$skip) $skip = 0;
618
619 if ($subop == "undefined") $subop = "";
620
621 if ($addheader) {
622 print "<html><head>
623 <title>Tiny Tiny RSS : Feed $feed</title>
624 <link rel=\"stylesheet\" href=\"tt-rss.css\" type=\"text/css\">";
625
626 if (get_pref($link, 'USE_COMPACT_STYLESHEET')) {
627 print "<link rel=\"stylesheet\"
628 type=\"text/css\" href=\"tt-rss_compact.css\"/>";
629
630 } else {
631 print "<link title=\"Compact Stylesheet\" rel=\"alternate stylesheet\"
632 type=\"text/css\" href=\"tt-rss_compact.css\"/>";
633 }
634 print "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">
635 <script type=\"text/javascript\" src=\"functions.js\"></script>
636 <script type=\"text/javascript\" src=\"viewfeed.js\"></script>
637 </head><body onload='init()'>";
638 }
639
640 if ($subop == "ForceUpdate" && sprintf("%d", $feed) > 0) {
641
642 $tmp_result = db_query($link, "SELECT feed_url FROM ttrss_feeds
643 WHERE id = '$feed'");
644
645 $feed_url = db_fetch_result($tmp_result, 0, "feed_url");
646
647 update_rss_feed($link, $feed_url, $feed);
648
649 }
650
651 if ($subop == "MarkAllRead") {
652
653 if (sprintf("%d", $feed) != 0) {
654
655 if ($feed > 0) {
656 db_query($link, "UPDATE ttrss_user_entries
657 SET unread = false,last_read = NOW()
658 WHERE feed_id = '$feed' AND owner_uid = " . $_SESSION["uid"]);
659
660 } else if ($feed < 0 && $feed > -10) { // special, like starred
661
662 if ($feed == -1) {
663 db_query($link, "UPDATE ttrss_user_entries
664 SET unread = false,last_read = NOW()
665 WHERE marked = true AND owner_uid = ".$_SESSION["uid"]);
666 }
667
668 } else if ($feed < -10) { // label
669
670 // TODO make this more efficient
671
672 $label_id = -$feed - 11;
673
674 $tmp_result = db_query($link, "SELECT sql_exp FROM ttrss_labels
675 WHERE id = '$label_id'");
676
677 if ($tmp_result) {
678 $sql_exp = db_fetch_result($tmp_result, 0, "sql_exp");
679
680 db_query($link, "BEGIN");
681
682 $tmp2_result = db_query($link,
683 "SELECT
684 int_id
685 FROM
686 ttrss_user_entries,ttrss_entries
687 WHERE
688 ref_id = id AND
689 $sql_exp AND
690 owner_uid = " . $_SESSION["uid"]);
691
692 while ($tmp_line = db_fetch_assoc($tmp2_result)) {
693 db_query($link, "UPDATE
694 ttrss_user_entries
695 SET
696 unread = false, last_read = NOW()
697 WHERE
698 int_id = " . $tmp_line["int_id"]);
699 }
700
701 db_query($link, "COMMIT");
702
703 /* db_query($link, "UPDATE ttrss_user_entries,ttrss_entries
704 SET unread = false,last_read = NOW()
705 WHERE $sql_exp
706 AND ref_id = id
707 AND owner_uid = ".$_SESSION["uid"]); */
708 }
709 }
710 } else { // tag
711 db_query($link, "BEGIN");
712
713 $tag_name = db_escape_string($feed);
714
715 $result = db_query($link, "SELECT post_int_id FROM ttrss_tags
716 WHERE tag_name = '$tag_name' AND owner_uid = " . $_SESSION["uid"]);
717
718 while ($line = db_fetch_assoc($result)) {
719 db_query($link, "UPDATE ttrss_user_entries SET
720 unread = false, last_read = NOW()
721 WHERE int_id = " . $line["post_int_id"]);
722 }
723 db_query($link, "COMMIT");
724 }
725
726 }
727
728 print "<table class=\"headlinesList\" id=\"headlinesList\" width=\"100%\">";
729
730 $search = $_GET["search"];
731
732 $search_mode = $_GET["smode"];
733
734 if ($search) {
735 $search_query_part = "(upper(title) LIKE upper('%$search%')
736 OR content LIKE '%$search%') AND";
737 } else {
738 $search_query_part = "";
739 }
740
741 $view_query_part = "";
742
743 if ($view_mode == "Starred") {
744 $view_query_part = " marked = true AND ";
745 }
746
747 if ($view_mode == "Unread") {
748 $view_query_part = " unread = true AND ";
749 }
750
751 if ($view_mode == "Unread or Starred") {
752 $view_query_part = " (unread = true OR marked = true) AND ";
753 }
754
755 if ($view_mode == "Unread or Updated") {
756 $view_query_part = " (unread = true OR last_read is NULL) AND ";
757 }
758
759 /* $result = db_query($link, "SELECT count(id) AS total_entries
760 FROM ttrss_entries WHERE
761 $search_query_part
762 feed_id = '$feed'");
763
764 $total_entries = db_fetch_result($result, 0, "total_entries"); */
765
766 /* $result = db_query("SELECT count(id) AS unread_entries
767 FROM ttrss_entries WHERE
768 $search_query_part
769 unread = true AND
770 feed_id = '$feed'");
771
772 $unread_entries = db_fetch_result($result, 0, "unread_entries"); */
773
774 if ($limit && $limit != "All") {
775 $limit_query_part = "LIMIT " . $limit;
776 }
777
778 $vfeed_query_part = "";
779
780 // override query strategy and enable feed display when searching globally
781 if ($search && $search_mode == "All feeds") {
782 $query_strategy_part = "id > 0";
783 $vfeed_query_part = "(SELECT title FROM ttrss_feeds WHERE
784 id = feed_id) as feed_title,";
785 } else if (sprintf("%d", $feed) == 0) {
786 $query_strategy_part = "ttrss_entries.id > 0";
787 $vfeed_query_part = "(SELECT title FROM ttrss_feeds WHERE
788 id = feed_id) as feed_title,";
789 } else if ($feed >= 0) {
790 $query_strategy_part = "feed_id = '$feed'";
791 } else if ($feed == -1) { // starred virtual feed
792 $query_strategy_part = "marked = true";
793 $vfeed_query_part = "(SELECT title FROM ttrss_feeds WHERE
794 id = feed_id) as feed_title,";
795 } else if ($feed <= -10) { // labels
796 $label_id = -$feed - 11;
797
798 $tmp_result = db_query($link, "SELECT sql_exp FROM ttrss_labels
799 WHERE id = '$label_id'");
800
801 $query_strategy_part = db_fetch_result($tmp_result, 0, "sql_exp");
802
803 $vfeed_query_part = "(SELECT title FROM ttrss_feeds WHERE
804 id = feed_id) as feed_title,";
805 } else {
806 $query_strategy_part = "id > 0"; // dumb
807 }
808
809
810 $order_by = "updated DESC";
811
812 // if ($feed < -10) {
813 // $order_by = "feed_id,updated DESC";
814 // }
815
816 if ($feed < -10) error_reporting (0);
817
818 if (sprintf("%d", $feed) != 0) {
819
820 if ($feed > 0) {
821 $feed_kind = "Feeds";
822 } else {
823 $feed_kind = "Labels";
824 }
825
826 $result = db_query($link, "SELECT
827 id,title,updated,unread,feed_id,marked,link,last_read,
828 SUBSTRING(last_read,1,19) as last_read_noms,
829 $vfeed_query_part
830 SUBSTRING(updated,1,19) as updated_noms
831 FROM
832 ttrss_entries,ttrss_user_entries
833 WHERE
834 ttrss_user_entries.ref_id = ttrss_entries.id AND
835 owner_uid = '".$_SESSION["uid"]."' AND
836 $search_query_part
837 $view_query_part
838 $query_strategy_part ORDER BY $order_by
839 $limit_query_part");
840
841 } else {
842 // browsing by tag
843
844 $feed_kind = "Tags";
845
846 $result = db_query($link, "SELECT
847 ttrss_entries.id as id,title,updated,unread,feed_id,
848 marked,link,last_read,
849 SUBSTRING(last_read,1,19) as last_read_noms,
850 $vfeed_query_part
851 SUBSTRING(updated,1,19) as updated_noms
852 FROM
853 ttrss_entries,ttrss_user_entries,ttrss_tags
854 WHERE
855 ref_id = ttrss_entries.id AND
856 ttrss_user_entries.owner_uid = '".$_SESSION["uid"]."' AND
857 post_int_id = int_id AND tag_name = '$feed' AND
858 $view_query_part
859 $search_query_part
860 $query_strategy_part ORDER BY $order_by
861 $limit_query_part");
862 }
863
864 if (!$result) {
865 print "<tr><td colspan='4' align='center'>
866 Could not display feed (query failed). Please check match syntax or local configuration.</td></tr>";
867 return;
868 }
869
870 $lnum = 0;
871
872 error_reporting (DEFAULT_ERROR_LEVEL);
873
874 $num_unread = 0;
875
876 while ($line = db_fetch_assoc($result)) {
877
878 $class = ($lnum % 2) ? "even" : "odd";
879
880 $id = $line["id"];
881 $feed_id = $line["feed_id"];
882
883 // printf("L %d (%s) &gt; U %d (%s) = %d<br>",
884 // strtotime($line["last_read_noms"]), $line["last_read_noms"],
885 // strtotime($line["updated"]), $line["updated"],
886 // strtotime($line["last_read"]) >= strtotime($line["updated"]));
887
888 /* if ($line["last_read"] != "" && $line["updated"] != "" &&
889 strtotime($line["last_read_noms"]) < strtotime($line["updated_noms"])) {
890
891 $update_pic = "<img id='FUPDPIC-$id' src=\"images/updated.png\"
892 alt=\"Updated\">";
893
894 } else {
895
896 $update_pic = "<img id='FUPDPIC-$id' src=\"images/blank_icon.gif\"
897 alt=\"Updated\">";
898
899 } */
900
901 if ($line["last_read"] == "" &&
902 ($line["unread"] != "t" && $line["unread"] != "1")) {
903
904 $update_pic = "<img id='FUPDPIC-$id' src=\"images/updated.png\"
905 alt=\"Updated\">";
906 } else {
907 $update_pic = "<img id='FUPDPIC-$id' src=\"images/blank_icon.gif\"
908 alt=\"Updated\">";
909 }
910
911 if ($line["unread"] == "t" || $line["unread"] == "1") {
912 $class .= "Unread";
913 ++$num_unread;
914 }
915
916 if ($line["marked"] == "t" || $line["marked"] == "1") {
917 $marked_pic = "<img id=\"FMARKPIC-$id\" src=\"images/mark_set.png\"
918 alt=\"Reset mark\" onclick='javascript:toggleMark($id, false)'>";
919 } else {
920 $marked_pic = "<img id=\"FMARKPIC-$id\" src=\"images/mark_unset.png\"
921 alt=\"Set mark\" onclick='javascript:toggleMark($id, true)'>";
922 }
923
924 $content_link = "<a id=\"FTITLE-$id\" href=\"javascript:view($id,$feed_id);\">" .
925 $line["title"] . "</a>";
926
927 print "<tr class='$class' id='RROW-$id'>";
928 // onclick=\"javascript:view($id,$feed_id)\">
929
930 print "<td valign='center' align='center'>$update_pic</td>";
931 print "<td valign='center' align='center'>$marked_pic</td>";
932
933 print "<td width='25%'>
934 <a href=\"javascript:view($id,$feed_id);\">".$line["updated"]."</a></td>";
935
936 if ($line["feed_title"]) {
937 print "<td width='50%'>$content_link</td>";
938 print "<td width='20%'>
939 <a href='javascript:viewfeed($feed_id)'>".$line["feed_title"]."</a></td>";
940 } else {
941 print "<td width='70%'>$content_link</td>";
942 }
943
944 print "</tr>";
945
946 ++$lnum;
947 }
948
949 if ($lnum == 0) {
950 print "<tr><td align='center'>No articles found.</td></tr>";
951 }
952
953 print "</table>";
954
955 print "<script type=\"text/javascript\">
956 document.onkeydown = hotkey_handler;
957 update_label_counters('$feed');
958 </script>";
959
960 if ($addheader) {
961 print "</body></html>";
962 }
963
964 }
965
966 if ($op == "pref-rpc") {
967
968 $subop = $_GET["subop"];
969
970 if ($subop == "unread") {
971 $ids = split(",", $_GET["ids"]);
972 foreach ($ids as $id) {
973 db_query($link, "UPDATE ttrss_user_entries SET unread = true
974 WHERE feed_id = '$id' AND owner_uid = ".$_SESSION["uid"]);
975 }
976
977 print "Marked selected feeds as unread.";
978 }
979
980 if ($subop == "read") {
981 $ids = split(",", $_GET["ids"]);
982 foreach ($ids as $id) {
983 db_query($link, "UPDATE ttrss_user_entries
984 SET unread = false,last_read = NOW() WHERE
985 feed_id = '$id' AND owner_uid = ".$_SESSION["uid"]);
986 }
987
988 print "Marked selected feeds as read.";
989
990 }
991
992 }
993
994 if ($op == "pref-feeds") {
995
996 $subop = $_GET["subop"];
997
998 if ($subop == "editSave") {
999 $feed_title = db_escape_string($_GET["t"]);
1000 $feed_link = db_escape_string($_GET["l"]);
1001 $upd_intl = db_escape_string($_GET["ui"]);
1002 $purge_intl = db_escape_string($_GET["pi"]);
1003 $feed_id = db_escape_string($_GET["id"]);
1004 $cat_id = db_escape_string($_GET["catid"]);
1005
1006 if (strtoupper($upd_intl) == "DEFAULT")
1007 $upd_intl = 0;
1008
1009 if (strtoupper($purge_intl) == "DEFAULT")
1010 $purge_intl = 0;
1011
1012 if (strtoupper($purge_intl) == "DISABLED")
1013 $purge_intl = -1;
1014
1015 if ($cat_id != 0) {
1016 $category_qpart = "cat_id = '$cat_id'";
1017 } else {
1018 $category_qpart = 'cat_id = NULL';
1019 }
1020
1021 $result = db_query($link, "UPDATE ttrss_feeds SET
1022 $category_qpart,
1023 title = '$feed_title', feed_url = '$feed_link',
1024 update_interval = '$upd_intl',
1025 purge_interval = '$purge_intl'
1026 WHERE id = '$feed_id' AND owner_uid = " . $_SESSION["uid"]);
1027
1028 }
1029
1030 if ($subop == "remove") {
1031
1032 if (!WEB_DEMO_MODE) {
1033
1034 $ids = split(",", $_GET["ids"]);
1035
1036 foreach ($ids as $id) {
1037 db_query($link, "DELETE FROM ttrss_feeds
1038 WHERE id = '$id' AND owner_uid = " . $_SESSION["uid"]);
1039
1040 $icons_dir = ICONS_DIR;
1041
1042 if (file_exists($icons_dir . "/$id.ico")) {
1043 unlink($icons_dir . "/$id.ico");
1044 }
1045 }
1046 }
1047 }
1048
1049 if ($subop == "add") {
1050
1051 if (!WEB_DEMO_MODE) {
1052
1053 $feed_link = db_escape_string(trim($_GET["link"]));
1054
1055 $result = db_query($link,
1056 "SELECT id FROM ttrss_feeds
1057 WHERE feed_url = '$feed_link' AND owner_uid = ".$_SESSION["uid"]);
1058
1059 if (db_num_rows($result) == 0) {
1060
1061 $result = db_query($link,
1062 "INSERT INTO ttrss_feeds (owner_uid,feed_url,title)
1063 VALUES ('".$_SESSION["uid"]."', '$feed_link', '')");
1064
1065 $result = db_query($link,
1066 "SELECT id FROM ttrss_feeds WHERE feed_url = '$feed_link'
1067 AND owner_uid = " . $_SESSION["uid"]);
1068
1069 $feed_id = db_fetch_result($result, 0, "id");
1070
1071 if ($feed_id) {
1072 update_rss_feed($link, $feed_link, $feed_id);
1073 }
1074 } else {
1075
1076 print "<div class=\"warning\">
1077 Feed <b>$feed_link</b> already exists in the database.
1078 </div>";
1079 }
1080 }
1081 }
1082
1083 if ($subop == "addCat") {
1084
1085 if (!WEB_DEMO_MODE) {
1086
1087 $feed_cat = db_escape_string(trim($_GET["cat"]));
1088
1089 $result = db_query($link,
1090 "SELECT id FROM ttrss_feed_categories
1091 WHERE title = '$feed_cat' AND owner_uid = ".$_SESSION["uid"]);
1092
1093 if (db_num_rows($result) == 0) {
1094
1095 $result = db_query($link,
1096 "INSERT INTO ttrss_feed_categories (owner_uid,title)
1097 VALUES ('".$_SESSION["uid"]."', '$feed_cat')");
1098
1099 } else {
1100
1101 print "<div class=\"warning\">
1102 Category <b>$feed_cat</b> already exists in the database.
1103 </div>";
1104 }
1105
1106
1107 }
1108 }
1109
1110 if ($subop == "removeCats") {
1111
1112 if (!WEB_DEMO_MODE) {
1113
1114 $ids = split(",", $_GET["ids"]);
1115
1116 foreach ($ids as $id) {
1117
1118 db_query($link, "BEGIN");
1119
1120 $result = db_query($link,
1121 "SELECT count(id) as num_feeds FROM ttrss_feeds
1122 WHERE cat_id = '$id'");
1123
1124 $num_feeds = db_fetch_result($result, 0, "num_feeds");
1125
1126 if ($num_feeds == 0) {
1127 db_query($link, "DELETE FROM ttrss_feed_categories
1128 WHERE id = '$id' AND owner_uid = " . $_SESSION["uid"]);
1129 } else {
1130
1131 print "<div class=\"warning\">
1132 Unable to delete non empty feed categories.</div>";
1133
1134 }
1135
1136 db_query($link, "COMMIT");
1137 }
1138 }
1139 }
1140
1141 // print "<h3>Edit Feeds</h3>";
1142
1143 $result = db_query($link, "SELECT id,title,feed_url,last_error
1144 FROM ttrss_feeds WHERE last_error != '' AND owner_uid = ".$_SESSION["uid"]);
1145
1146 if (db_num_rows($result) > 0) {
1147
1148 print "<div class=\"warning\">";
1149
1150 print "<b>Feeds with update errors:</b>";
1151
1152 print "<ul class=\"nomarks\">";
1153
1154 while ($line = db_fetch_assoc($result)) {
1155 print "<li>" . $line["title"] . " (" . $line["feed_url"] . "): " .
1156 $line["last_error"];
1157 }
1158
1159 print "</ul>";
1160 print "</div>";
1161
1162 }
1163
1164 print "<p><div class=\"prefGenericAddBox\">
1165 <input id=\"fadd_link\" size=\"40\">&nbsp;<input
1166 type=\"submit\" class=\"button\"
1167 onclick=\"javascript:addFeed()\" value=\"Add feed\"></div>";
1168
1169 $feeds_sort = db_escape_string($_GET["sort"]);
1170
1171 if (!$feeds_sort || $feeds_sort == "undefined") {
1172 $feeds_sort = $_SESSION["pref_sort_feeds"];
1173 if (!$feeds_sort) $feeds_sort = "title";
1174 }
1175
1176 $_SESSION["pref_sort_feeds"] = $feeds_sort;
1177
1178 $result = db_query($link, "SELECT
1179 id,title,feed_url,substring(last_updated,1,16) as last_updated,
1180 update_interval,purge_interval,
1181 (SELECT title FROM ttrss_feed_categories
1182 WHERE id = cat_id) AS category
1183 FROM
1184 ttrss_feeds WHERE owner_uid = '".$_SESSION["uid"]."'
1185 ORDER by $feeds_sort,title");
1186
1187 if (db_num_rows($result) != 0) {
1188
1189 print "<div id=\"infoBoxShadow\"><div id=\"infoBox\">PLACEHOLDER</div></div>";
1190
1191 print "<p><table width=\"100%\" class=\"prefFeedList\" id=\"prefFeedList\">";
1192 print "<tr class=\"title\">
1193 <td width=\"3%\">&nbsp;</td>
1194 <td width=\"3%\">Select</td>
1195 <td width=\"20%\">
1196 <a href=\"javascript:updateFeedList('title')\">Title</a></td>
1197 <td width=\"20%\">
1198 <a href=\"javascript:updateFeedList('feed_url')\">Link</a>
1199 </td>";
1200
1201 if (get_pref($link, 'ENABLE_FEED_CATS')) {
1202 print "<td width=\"10%\">
1203 <a href=\"javascript:updateFeedList('category')\">Category</a></td>";
1204 }
1205
1206 print "
1207 <td width=\"10%\">
1208 <a href=\"javascript:updateFeedList('update_interval')\">Update Interval</a>
1209 </td>
1210 <td width=\"10%\">
1211 <a href=\"javascript:updateFeedList('purge_interval')\">Purge Days</a>
1212 </td>
1213 </tr>";
1214
1215 $lnum = 0;
1216
1217 while ($line = db_fetch_assoc($result)) {
1218
1219 $class = ($lnum % 2) ? "even" : "odd";
1220
1221 $feed_id = $line["id"];
1222
1223 $edit_feed_id = $_GET["id"];
1224
1225 if ($subop == "edit" && $feed_id != $edit_feed_id) {
1226 $class .= "Grayed";
1227 }
1228
1229 print "<tr class=\"$class\" id=\"FEEDR-$feed_id\">";
1230
1231 $icon_file = ICONS_DIR . "/$feed_id.ico";
1232
1233 if (file_exists($icon_file) && filesize($icon_file) > 0) {
1234 $feed_icon = "<img width=\"16\" height=\"16\"
1235 src=\"" . ICONS_URL . "/$feed_id.ico\">";
1236 } else {
1237 $feed_icon = "&nbsp;";
1238 }
1239 print "<td align='center'>$feed_icon</td>";
1240
1241 $edit_title = htmlspecialchars(db_unescape_string($line["title"]));
1242 $edit_link = htmlspecialchars(db_unescape_string($line["feed_url"]));
1243 $edit_cat = htmlspecialchars(db_unescape_string($line["category"]));
1244
1245 if (!$edit_cat) $edit_cat = "Uncategorized";
1246
1247 if (!$edit_feed_id || $subop != "edit") {
1248
1249 print "<td><input onclick='toggleSelectRow(this);'
1250 type=\"checkbox\" id=\"FRCHK-".$line["id"]."\"></td>";
1251
1252 $edit_title = truncate_string($edit_title, 40);
1253 $edit_link = truncate_string($edit_link, 60);
1254
1255 print "<td><a href=\"javascript:editFeed($feed_id);\">" .
1256 $edit_title . "</a></td>";
1257
1258 print "<td><a href=\"javascript:editFeed($feed_id);\">" .
1259 $edit_link . "</a></td>";
1260
1261 if (get_pref($link, 'ENABLE_FEED_CATS')) {
1262 print "<td><a href=\"javascript:editFeed($feed_id);\">" .
1263 $edit_cat . "</a></td>";
1264 }
1265
1266 if ($line["update_interval"] == "0")
1267 $line["update_interval"] = "Default";
1268
1269 print "<td><a href=\"javascript:editFeed($feed_id);\">" .
1270 $line["update_interval"] . "</a></td>";
1271
1272 if ($line["purge_interval"] == "0")
1273 $line["purge_interval"] = "Default";
1274
1275 if ($line["purge_interval"] < 0)
1276 $line["purge_interval"] = "Disabled";
1277
1278 print "<td><a href=\"javascript:editFeed($feed_id);\">" .
1279 $line["purge_interval"] . "</a></td>";
1280
1281 } else if ($feed_id != $edit_feed_id) {
1282
1283 print "<td><input disabled=\"true\" type=\"checkbox\"
1284 id=\"FRCHK-".$line["id"]."\"></td>";
1285
1286 $edit_title = truncate_string($edit_title, 40);
1287 $edit_link = truncate_string($edit_link, 60);
1288
1289 print "<td>$edit_title</td>";
1290 print "<td>$edit_link</td>";
1291
1292 if (get_pref($link, 'ENABLE_FEED_CATS')) {
1293 print "<td>$edit_cat</td>";
1294 }
1295
1296 if ($line["update_interval"] == "0")
1297 $line["update_interval"] = "Default";
1298
1299 print "<td>" . $line["update_interval"] . "</td>";
1300
1301 if ($line["purge_interval"] == "0")
1302 $line["purge_interval"] = "Default";
1303
1304 if ($line["purge_interval"] < 0)
1305 $line["purge_interval"] = "Disabled";
1306
1307 print "<td>" . $line["purge_interval"] . "</td>";
1308
1309 } else {
1310
1311 print "<td><input disabled=\"true\" type=\"checkbox\" checked></td>";
1312
1313 print "<td><input id=\"iedit_title\" value=\"$edit_title\"></td>";
1314 print "<td><input id=\"iedit_link\" value=\"$edit_link\"></td>";
1315
1316 if (get_pref($link, 'ENABLE_FEED_CATS')) {
1317
1318 print "<td>";
1319 print "<select id=\"iedit_fcat\">";
1320 print "<option id=\"0\">Uncategorized</option>";
1321
1322 $tmp_result = db_query($link, "SELECT id,title FROM ttrss_feed_categories
1323 WHERE owner_uid = ".$_SESSION["uid"]." ORDER BY title");
1324
1325 if (db_num_rows($tmp_result) > 0) {
1326 print "<option disabled>--------</option>";
1327 }
1328
1329 while ($tmp_line = db_fetch_assoc($tmp_result)) {
1330 if ($tmp_line["id"] == $line["cat_id"]) {
1331 $is_selected = "selected";
1332 } else {
1333 $is_selected = "";
1334 }
1335 printf("<option $is_selected id='%d'>%s</option>",
1336 $tmp_line["id"], $tmp_line["title"]);
1337 }
1338
1339 print "</select></td>";
1340 print "</td>";
1341
1342 }
1343
1344 print "<td><input id=\"iedit_updintl\"
1345 value=\"".$line["update_interval"]."\"></td>";
1346 print "<td><input id=\"iedit_purgintl\"
1347 value=\"".$line["purge_interval"]."\"></td>";
1348
1349 }
1350
1351 /* if (!$line["last_updated"]) $line["last_updated"] = "Never";
1352
1353 print "<td>" . $line["last_updated"] . "</td>"; */
1354
1355 print "</tr>";
1356
1357 ++$lnum;
1358 }
1359
1360 print "</table>";
1361
1362 print "<p>";
1363
1364 if ($subop == "edit") {
1365 print "Edit feed:&nbsp;
1366 <input type=\"submit\" class=\"button\"
1367 onclick=\"javascript:feedEditCancel()\" value=\"Cancel\">
1368 <input type=\"submit\" class=\"button\"
1369 onclick=\"javascript:feedEditSave()\" value=\"Save\">";
1370 } else {
1371
1372 print "
1373 Selection:&nbsp;
1374 <input type=\"submit\" class=\"button\"
1375 onclick=\"javascript:selectedFeedDetails()\" value=\"Details\">
1376 <input type=\"submit\" class=\"button\"
1377 onclick=\"javascript:editSelectedFeed()\" value=\"Edit\">
1378 <input type=\"submit\" class=\"button\"
1379 onclick=\"javascript:removeSelectedFeeds()\" value=\"Remove\">";
1380
1381 if (get_pref($link, 'ENABLE_PREFS_CATCHUP_UNCATCHUP')) {
1382 print "
1383 <input type=\"submit\" class=\"button\"
1384 onclick=\"javascript:readSelectedFeeds()\" value=\"Mark as read\">
1385 <input type=\"submit\" class=\"button\"
1386 onclick=\"javascript:unreadSelectedFeeds()\"
1387 value=\"Mark as unread\">&nbsp;";
1388 }
1389
1390 print "
1391 All feeds: <input type=\"submit\"
1392 class=\"button\" onclick=\"gotoExportOpml()\"
1393 value=\"Export OPML\">";
1394 }
1395 } else {
1396
1397 print "<p>No feeds defined.</p>";
1398
1399 }
1400
1401 if (get_pref($link, 'ENABLE_FEED_CATS')) {
1402
1403 print "<h3>Edit Categories</h3>";
1404
1405 // print "<h3>Categories</h3>";
1406
1407 print "<div class=\"prefGenericAddBox\">
1408 <input id=\"fadd_cat\" size=\"40\">&nbsp;<input
1409 type=\"submit\" class=\"button\"
1410 onclick=\"javascript:addFeedCat()\" value=\"Add category\"></div>";
1411
1412 $result = db_query($link, "SELECT title,id FROM ttrss_feed_categories
1413 WHERE owner_uid = ".$_SESSION["uid"]."
1414 ORDER BY title");
1415
1416 if (db_num_rows($result) != 0) {
1417
1418 print "<p><table width=\"100%\" class=\"prefFeedCatList\" id=\"prefFeedCatList\">";
1419 print "<tr class=\"title\">
1420 <td width=\"10%\">Select</td><td width=\"80%\">Title</td>
1421 </tr>";
1422
1423 $lnum = 0;
1424
1425 while ($line = db_fetch_assoc($result)) {
1426
1427 $class = ($lnum % 2) ? "even" : "odd";
1428
1429 $cat_id = $line["id"];
1430
1431 $edit_cat_id = $_GET["id"];
1432
1433 if ($subop == "editCat" && $cat_id != $edit_cat_id) {
1434 $class .= "Grayed";
1435 }
1436
1437 print "<tr class=\"$class\" id=\"FCATR-$cat_id\">";
1438
1439 $edit_title = htmlspecialchars(db_unescape_string($line["title"]));
1440
1441 if (!$edit_cat_id || $subop != "editCat") {
1442
1443 print "<td><input onclick='toggleSelectRow(this);'
1444 type=\"checkbox\" id=\"FCCHK-".$line["id"]."\"></td>";
1445
1446 print "<td><a href=\"javascript:editFeedCat($cat_id);\">" .
1447 $edit_title . "</a></td>";
1448
1449 } else if ($cat_id != $edit_cat_id) {
1450
1451 print "<td><input disabled=\"true\" type=\"checkbox\"
1452 id=\"FRCHK-".$line["id"]."\"></td>";
1453
1454 print "<td>$edit_title</td>";
1455
1456 } else {
1457
1458 print "<td><input disabled=\"true\" type=\"checkbox\" checked></td>";
1459
1460 print "<td><input id=\"iedit_title\" value=\"$edit_title\"></td>";
1461
1462 }
1463
1464 print "</tr>";
1465
1466 ++$lnum;
1467 }
1468
1469 print "</table>";
1470
1471 print "<p>";
1472
1473 if ($subop == "editCat") {
1474 print "Edit category:&nbsp;
1475 <input type=\"submit\" class=\"button\"
1476 onclick=\"javascript:feedCatEditCancel()\" value=\"Cancel\">
1477 <input type=\"submit\" class=\"button\"
1478 onclick=\"javascript:feedCatEditSave()\" value=\"Save\">";
1479 } else {
1480
1481 print "
1482 Selection:&nbsp;
1483 <input type=\"submit\" class=\"button\"
1484 onclick=\"javascript:editSelectedFeedCat()\" value=\"Edit\">
1485 <input type=\"submit\" class=\"button\"
1486 onclick=\"javascript:removeSelectedFeedCats()\" value=\"Remove\">";
1487
1488 }
1489
1490 } else {
1491 print "<p>No feed categories defined.</p>";
1492 }
1493 }
1494
1495 print "<h3>Import OPML</h3>
1496 <form enctype=\"multipart/form-data\" method=\"POST\" action=\"opml.php\">
1497 File: <input id=\"opml_file\" name=\"opml_file\" type=\"file\">&nbsp;
1498 <input class=\"button\" name=\"op\" onclick=\"return validateOpmlImport();\"
1499 type=\"submit\" value=\"Import\">
1500 </form>";
1501
1502 }
1503
1504 if ($op == "pref-filters") {
1505
1506 $subop = $_GET["subop"];
1507
1508 if ($subop == "editSave") {
1509
1510 $regexp = db_escape_string($_GET["r"]);
1511 $descr = db_escape_string($_GET["d"]);
1512 $match = db_escape_string($_GET["m"]);
1513 $filter_id = db_escape_string($_GET["id"]);
1514 $feed_id = db_escape_string($_GET["fid"]);
1515
1516 if (!$feed_id) {
1517 $feed_id = 'NULL';
1518 } else {
1519 $feed_id = sprintf("'%s'", db_escape_string($feed_id));
1520 }
1521
1522 $result = db_query($link, "UPDATE ttrss_filters SET
1523 reg_exp = '$regexp',
1524 description = '$descr',
1525 feed_id = $feed_id,
1526 filter_type = (SELECT id FROM ttrss_filter_types WHERE
1527 description = '$match')
1528 WHERE id = '$filter_id'");
1529 }
1530
1531 if ($subop == "remove") {
1532
1533 if (!WEB_DEMO_MODE) {
1534
1535 $ids = split(",", $_GET["ids"]);
1536
1537 foreach ($ids as $id) {
1538 db_query($link, "DELETE FROM ttrss_filters WHERE id = '$id'");
1539
1540 }
1541 }
1542 }
1543
1544 if ($subop == "add") {
1545
1546 if (!WEB_DEMO_MODE) {
1547
1548 $regexp = db_escape_string(trim($_GET["regexp"]));
1549 $match = db_escape_string(trim($_GET["match"]));
1550 $feed_id = db_escape_string($_GET["fid"]);
1551
1552 if (!$feed_id) {
1553 $feed_id = 'NULL';
1554 } else {
1555 $feed_id = sprintf("'%s'", db_escape_string($feed_id));
1556 }
1557
1558 $result = db_query($link,
1559 "INSERT INTO ttrss_filters (reg_exp,filter_type,owner_uid,feed_id) VALUES
1560 ('$regexp', (SELECT id FROM ttrss_filter_types WHERE
1561 description = '$match'),'".$_SESSION["uid"]."', $feed_id)");
1562 }
1563 }
1564
1565 $result = db_query($link, "SELECT description
1566 FROM ttrss_filter_types ORDER BY description");
1567
1568 $filter_types = array();
1569
1570 while ($line = db_fetch_assoc($result)) {
1571 array_push($filter_types, $line["description"]);
1572 }
1573
1574 print "<div class=\"prefGenericAddBox\">
1575 <input id=\"fadd_regexp\" size=\"40\">&nbsp;";
1576
1577 print_select("fadd_match", "Title", $filter_types);
1578
1579 print "&nbsp;<select id=\"fadd_feed\">";
1580
1581 print "<option selected id=\"0\">All feeds</option>";
1582
1583 $result = db_query($link, "SELECT id,title FROM ttrss_feeds
1584 WHERE owner_uid = ".$_SESSION["uid"]." ORDER BY title");
1585
1586 if (db_num_rows($result) > 0) {
1587 print "<option disabled>--------</option>";
1588 }
1589
1590 while ($line = db_fetch_assoc($result)) {
1591 printf("<option id='%d'>%s</option>", $line["id"], $line["title"]);
1592 }
1593
1594 print "</select>&nbsp;";
1595
1596 print "<input type=\"submit\"
1597 class=\"button\" onclick=\"javascript:addFilter()\"
1598 value=\"Add filter\">";
1599
1600 $result = db_query($link, "SELECT
1601 ttrss_filters.id AS id,reg_exp,
1602 ttrss_filters.description AS description,
1603 ttrss_filter_types.name AS filter_type_name,
1604 ttrss_filter_types.description AS filter_type_descr,
1605 feed_id,
1606 (SELECT title FROM ttrss_feeds WHERE id = feed_id) AS feed_title
1607 FROM
1608 ttrss_filters,ttrss_filter_types
1609 WHERE
1610 filter_type = ttrss_filter_types.id AND
1611 ttrss_filters.owner_uid = ".$_SESSION["uid"]."
1612 ORDER by reg_exp");
1613
1614 if (db_num_rows($result) != 0) {
1615
1616 print "<p><table width=\"100%\" class=\"prefFilterList\" id=\"prefFilterList\">";
1617
1618 print "<tr class=\"title\">
1619 <td width=\"5%\">Select</td><td width=\"30%\">Filter expression</td>
1620 <td width=\"30%\">Feed</td><td width=\"10%\">Match</td>
1621 <td width=\"30%\">Description</td></tr>";
1622
1623 $lnum = 0;
1624
1625 while ($line = db_fetch_assoc($result)) {
1626
1627 $class = ($lnum % 2) ? "even" : "odd";
1628
1629 $filter_id = $line["id"];
1630 $edit_filter_id = $_GET["id"];
1631
1632 if ($subop == "edit" && $filter_id != $edit_filter_id) {
1633 $class .= "Grayed";
1634 }
1635
1636 print "<tr class=\"$class\" id=\"FILRR-$filter_id\">";
1637
1638 $line["regexp"] = htmlspecialchars($line["reg_exp"]);
1639 $line["description"] = htmlspecialchars($line["description"]);
1640
1641 if (!$line["feed_title"]) $line["feed_title"] = "All feeds";
1642
1643 if (!$edit_filter_id || $subop != "edit") {
1644
1645 if (!$line["description"]) $line["description"] = "[No description]";
1646
1647 print "<td><input onclick='toggleSelectRow(this);'
1648 type=\"checkbox\" id=\"FICHK-".$line["id"]."\"></td>";
1649
1650 print "<td><a href=\"javascript:editFilter($filter_id);\">" .
1651 $line["reg_exp"] . "</td>";
1652
1653 print "<td><a href=\"javascript:editFilter($filter_id);\">" .
1654 $line["feed_title"] . "</td>";
1655
1656 print "<td><a href=\"javascript:editFilter($filter_id);\">" .
1657 $line["filter_type_descr"] . "</td>";
1658
1659 print "<td><a href=\"javascript:editFilter($filter_id);\">" .
1660 $line["description"] . "</td>";
1661
1662 } else if ($filter_id != $edit_filter_id) {
1663
1664 if (!$line["description"]) $line["description"] = "[No description]";
1665
1666 print "<td><input disabled=\"true\" type=\"checkbox\"
1667 id=\"FICHK-".$line["id"]."\"></td>";
1668
1669 print "<td>".$line["reg_exp"]."</td>";
1670 print "<td>".$line["feed_title"]."</td>";
1671 print "<td>".$line["filter_type_descr"]."</td>";
1672 print "<td>".$line["description"]."</td>";
1673
1674 } else {
1675
1676 print "<td><input disabled=\"true\" type=\"checkbox\" checked></td>";
1677
1678 print "<td><input id=\"iedit_regexp\" value=\"".$line["reg_exp"].
1679 "\"></td>";
1680
1681 print "<td>";
1682
1683 print "<select id=\"iedit_feed\">";
1684
1685 print "<option id=\"0\">All feeds</option>";
1686
1687 if (db_num_rows($result) > 0) {
1688 print "<option disabled>--------</option>";
1689 }
1690
1691 $tmp_result = db_query($link, "SELECT id,title FROM ttrss_feeds
1692 WHERE owner_uid = ".$_SESSION["uid"]." ORDER BY title");
1693
1694 while ($tmp_line = db_fetch_assoc($tmp_result)) {
1695 if ($tmp_line["id"] == $line["feed_id"]) {
1696 $is_selected = "selected";
1697 } else {
1698 $is_selected = "";
1699 }
1700 printf("<option $is_selected id='%d'>%s</option>",
1701 $tmp_line["id"], $tmp_line["title"]);
1702 }
1703
1704 print "</select></td>";
1705
1706 print "<td>";
1707 print_select("iedit_match", $line["filter_type_descr"], $filter_types);
1708 print "</td>";
1709
1710 print "<td><input id=\"iedit_descr\" value=\"".$line["description"].
1711 "\"></td>";
1712
1713 print "</td>";
1714 }
1715
1716 print "</tr>";
1717
1718 ++$lnum;
1719 }
1720
1721 if ($lnum == 0) {
1722 print "<tr><td colspan=\"4\" align=\"center\">No filters defined.</td></tr>";
1723 }
1724
1725 print "</table>";
1726
1727 print "<p>";
1728
1729 if ($subop == "edit") {
1730 print "Edit feed:
1731 <input type=\"submit\" class=\"button\"
1732 onclick=\"javascript:filterEditCancel()\" value=\"Cancel\">
1733 <input type=\"submit\" class=\"button\"
1734 onclick=\"javascript:filterEditSave()\" value=\"Save\">";
1735
1736 } else {
1737
1738 print "
1739 Selection:
1740 <input type=\"submit\" class=\"button\"
1741 onclick=\"javascript:editSelectedFilter()\" value=\"Edit\">
1742 <input type=\"submit\" class=\"button\"
1743 onclick=\"javascript:removeSelectedFilters()\" value=\"Remove\">";
1744 }
1745
1746 } else {
1747
1748 print "<p>No filters defined.</p>";
1749
1750 }
1751 }
1752
1753 if ($op == "pref-labels") {
1754
1755 $subop = $_GET["subop"];
1756
1757 if ($subop == "test") {
1758
1759 $expr = $_GET["expr"];
1760 $descr = $_GET["descr"];
1761
1762 print "<div class='infoBoxContents'>";
1763
1764 print "<h1>Label &laquo;$descr&raquo;</h1>";
1765
1766 // print "<p><b>Expression</b>: $expr</p>";
1767
1768 $result = db_query($link,
1769 "SELECT count(id) AS num_matches
1770 FROM ttrss_entries,ttrss_user_entries
1771 WHERE ($expr) AND
1772 ttrss_user_entries.ref_id = ttrss_entries.id AND
1773 owner_uid = " . $_SESSION["uid"]);
1774
1775 $num_matches = db_fetch_result($result, 0, "num_matches");;
1776
1777 if ($num_matches > 0) {
1778
1779 print "<p>Query returned <b>$num_matches</b> matches, first 5:</p>";
1780
1781 $result = db_query($link,
1782 "SELECT title,
1783 (SELECT title FROM ttrss_feeds WHERE id = feed_id) AS feed_title
1784 FROM ttrss_entries,ttrss_user_entries
1785 WHERE ($expr) AND
1786 ttrss_user_entries.ref_id = ttrss_entries.id
1787 AND owner_uid = " . $_SESSION["uid"] . "
1788 ORDER BY date_entered DESC LIMIT 5");
1789
1790 print "<ul class=\"nomarks\">";
1791 while ($line = db_fetch_assoc($result)) {
1792 print "<li>".$line["title"].
1793 " <span class=\"insensitive\">(".$line["feed_title"].")</span></li>";
1794 }
1795 print "</ul>";
1796
1797 } else {
1798 print "<p>Query didn't return any matches.</p>";
1799 }
1800
1801 print "</div>";
1802
1803 print "<div align='center'>
1804 <input type='submit' class='button'
1805 onclick=\"closeInfoBox()\" value=\"Close this window\"></div>";
1806 return;
1807 }
1808
1809 if ($subop == "editSave") {
1810
1811 $sql_exp = $_GET["s"];
1812 $descr = $_GET["d"];
1813 $label_id = db_escape_string($_GET["id"]);
1814
1815 // print "$sql_exp : $descr : $label_id";
1816
1817 $result = db_query($link, "UPDATE ttrss_labels SET
1818 sql_exp = '$sql_exp',
1819 description = '$descr'
1820 WHERE id = '$label_id'");
1821 }
1822
1823 if ($subop == "remove") {
1824
1825 if (!WEB_DEMO_MODE) {
1826
1827 $ids = split(",", $_GET["ids"]);
1828
1829 foreach ($ids as $id) {
1830 db_query($link, "DELETE FROM ttrss_labels WHERE id = '$id'");
1831
1832 }
1833 }
1834 }
1835
1836 if ($subop == "add") {
1837
1838 if (!WEB_DEMO_MODE) {
1839
1840 // no escaping is done here on purpose
1841 $exp = trim($_GET["exp"]);
1842
1843 $result = db_query($link,
1844 "INSERT INTO ttrss_labels (sql_exp,description,owner_uid)
1845 VALUES ('$exp', '$exp', '".$_SESSION["uid"]."')");
1846 }
1847 }
1848
1849 print "<div class=\"prefGenericAddBox\">
1850 <input size=\"40\" id=\"ladd_expr\">&nbsp;";
1851
1852 print"<input type=\"submit\" class=\"button\"
1853 onclick=\"javascript:addLabel()\" value=\"Add label\"></div>";
1854
1855 $result = db_query($link, "SELECT
1856 id,sql_exp,description
1857 FROM
1858 ttrss_labels
1859 WHERE
1860 owner_uid = ".$_SESSION["uid"]."
1861 ORDER by description");
1862
1863 print "<div id=\"infoBoxShadow\"><div id=\"infoBox\">PLACEHOLDER</div></div>";
1864
1865 if (db_num_rows($result) != 0) {
1866
1867 print "<p><table width=\"100%\" class=\"prefLabelList\" id=\"prefLabelList\">";
1868
1869 print "<tr class=\"title\">
1870 <td width=\"5%\">Select</td><td width=\"40%\">SQL expression
1871 <a class=\"helpLink\" href=\"javascript:popupHelp(1)\">(?)</a>
1872 </td>
1873 <td width=\"40%\">Caption</td></tr>";
1874
1875 $lnum = 0;
1876
1877 while ($line = db_fetch_assoc($result)) {
1878
1879 $class = ($lnum % 2) ? "even" : "odd";
1880
1881 $label_id = $line["id"];
1882 $edit_label_id = $_GET["id"];
1883
1884 if ($subop == "edit" && $label_id != $edit_label_id) {
1885 $class .= "Grayed";
1886 }
1887
1888 print "<tr class=\"$class\" id=\"LILRR-$label_id\">";
1889
1890 $line["sql_exp"] = htmlspecialchars($line["sql_exp"]);
1891 $line["description"] = htmlspecialchars($line["description"]);
1892
1893 if (!$edit_label_id || $subop != "edit") {
1894
1895 if (!$line["description"]) $line["description"] = "[No caption]";
1896
1897 print "<td><input onclick='toggleSelectRow(this);'
1898 type=\"checkbox\" id=\"LICHK-".$line["id"]."\"></td>";
1899
1900 print "<td><a href=\"javascript:editLabel($label_id);\">" .
1901 $line["sql_exp"] . "</td>";
1902
1903 print "<td><a href=\"javascript:editLabel($label_id);\">" .
1904 $line["description"] . "</td>";
1905
1906 } else if ($label_id != $edit_label_id) {
1907
1908 if (!$line["description"]) $line["description"] = "[No description]";
1909
1910 print "<td><input disabled=\"true\" type=\"checkbox\"
1911 id=\"LICHK-".$line["id"]."\"></td>";
1912
1913 print "<td>".$line["sql_exp"]."</td>";
1914 print "<td>".$line["description"]."</td>";
1915
1916 } else {
1917
1918 print "<td><input disabled=\"true\" type=\"checkbox\" checked></td>";
1919
1920 print "<td><input id=\"iedit_expr\" value=\"".$line["sql_exp"].
1921 "\"></td>";
1922
1923 print "<td><input id=\"iedit_descr\" value=\"".$line["description"].
1924 "\"></td>";
1925
1926 }
1927
1928
1929 print "</tr>";
1930
1931 ++$lnum;
1932 }
1933
1934 if ($lnum == 0) {
1935 print "<tr><td colspan=\"4\" align=\"center\">No labels defined.</td></tr>";
1936 }
1937
1938 print "</table>";
1939
1940 print "<p>";
1941
1942 if ($subop == "edit") {
1943 print "Edit label:
1944 <input type=\"submit\" class=\"button\"
1945 onclick=\"javascript:labelTest()\" value=\"Test\">
1946 <input type=\"submit\" class=\"button\"
1947 onclick=\"javascript:labelEditCancel()\" value=\"Cancel\">
1948 <input type=\"submit\" class=\"button\"
1949 onclick=\"javascript:labelEditSave()\" value=\"Save\">";
1950
1951 } else {
1952
1953 print "
1954 Selection:
1955 <input type=\"submit\" class=\"button\"
1956 onclick=\"javascript:editSelectedLabel()\" value=\"Edit\">
1957 <input type=\"submit\" class=\"button\"
1958 onclick=\"javascript:removeSelectedLabels()\" value=\"Remove\">";
1959 }
1960 } else {
1961 print "<p>No labels defined.</p>";
1962 }
1963 }
1964
1965 if ($op == "error") {
1966 print "<div width=\"100%\" align='center'>";
1967 $msg = $_GET["msg"];
1968 print $msg;
1969 print "</div>";
1970 }
1971
1972 if ($op == "help") {
1973 print "<html><head>
1974 <title>Tiny Tiny RSS : Help</title>
1975 <link rel=\"stylesheet\" href=\"tt-rss.css\" type=\"text/css\">
1976 <script type=\"text/javascript\" src=\"functions.js\"></script>
1977 <meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">
1978 </head><body>";
1979
1980 $tid = sprintf("%d", $_GET["tid"]);
1981
1982 /* FIXME this badly needs real implementation */
1983
1984 print "<div class='helpResponse'>";
1985
1986 ?>
1987
1988 <h1>Help for SQL expressions</h1>
1989
1990 <h2>Description</h2>
1991
1992 <p>The &laquo;SQL expression&raquo; is added to WHERE clause of
1993 view feed query. You can match on ttrss_entries table fields
1994 and even use subselect to query additional information. This
1995 functionality is considered to be advanced and requires basic
1996 understanding of SQL.</p>
1997
1998 <h2>Examples</h2>
1999
2000 <pre>unread = true</pre>
2001
2002 Matches all unread articles
2003
2004 <pre>title like '%Linux%'</pre>
2005
2006 Matches all articles which mention Linux in the title. You get the idea.
2007
2008 <p>See the database schema included in the distribution package for gruesome
2009 details.</p>
2010
2011 <?
2012
2013 print "<div align='center'>
2014 <a class=\"helpLink\"
2015 href=\"javascript:window.close()\">(Close this window)</a></div>";
2016
2017 print "</div>";
2018
2019 print "</body></html>";
2020
2021 }
2022
2023 if ($op == "dlg") {
2024 $id = $_GET["id"];
2025 $param = $_GET["param"];
2026
2027 if ($id == "quickAddFeed") {
2028 print "Feed URL: <input
2029 onblur=\"javascript:enableHotkeys()\" onfocus=\"javascript:disableHotkeys()\"
2030 id=\"qafInput\">
2031 <input class=\"button\"
2032 type=\"submit\" onclick=\"javascript:qafAdd()\" value=\"Add feed\">
2033 <input class=\"button\"
2034 type=\"submit\" onclick=\"javascript:closeDlg()\"
2035 value=\"Cancel\">";
2036 }
2037
2038 if ($id == "quickDelFeed") {
2039
2040 $param = db_escape_string($param);
2041
2042 $result = db_query($link, "SELECT title FROM ttrss_feeds WHERE id = '$param'");
2043
2044 if ($result) {
2045
2046 $f_title = db_fetch_result($result, 0, "title");
2047
2048 print "Remove current feed ($f_title)?&nbsp;
2049 <input class=\"button\"
2050 type=\"submit\" onclick=\"javascript:qfdDelete($param)\" value=\"Remove\">
2051 <input class=\"button\"
2052 type=\"submit\" onclick=\"javascript:closeDlg()\"
2053 value=\"Cancel\">";
2054 } else {
2055 print "Error: Feed $param not found.&nbsp;
2056 <input class=\"button\"
2057 type=\"submit\" onclick=\"javascript:closeDlg()\"
2058 value=\"Cancel\">";
2059 }
2060 }
2061
2062 if ($id == "search") {
2063
2064 print "<input id=\"searchbox\" class=\"extSearch\"
2065 onblur=\"javascript:enableHotkeys()\" onfocus=\"javascript:disableHotkeys()\"
2066 onchange=\"javascript:search()\">
2067 <select id=\"searchmodebox\">
2068 <option selected>All feeds</option>
2069 <option>This feed</option>
2070 </select>
2071 <input type=\"submit\"
2072 class=\"button\" onclick=\"javascript:search()\" value=\"Search\">
2073 <input class=\"button\"
2074 type=\"submit\" onclick=\"javascript:closeDlg()\"
2075 value=\"Close\">";
2076
2077 }
2078
2079 }
2080
2081 // update feeds of all users, may be used anonymously
2082 if ($op == "globalUpdateFeeds") {
2083
2084 $result = db_query($link, "SELECT id FROM ttrss_users");
2085
2086 while ($line = db_fetch_assoc($result)) {
2087 $user_id = $line["id"];
2088 // print "<!-- updating feeds of uid $user_id -->";
2089 update_all_feeds($link, false, $user_id);
2090 }
2091
2092 print "<rpc-reply>
2093 <message msg=\"All feeds updated\"/>
2094 </rpc-reply>";
2095
2096 }
2097
2098 if ($op == "pref-prefs") {
2099
2100 $subop = $_REQUEST["subop"];
2101
2102 if ($subop == "Save configuration") {
2103
2104 if (WEB_DEMO_MODE) {
2105 header("Location: prefs.php");
2106 return;
2107 }
2108
2109 $_SESSION["prefs_op_result"] = "save-config";
2110
2111 foreach (array_keys($_POST) as $pref_name) {
2112
2113 $pref_name = db_escape_string($pref_name);
2114 $value = db_escape_string($_POST[$pref_name]);
2115
2116 $result = db_query($link, "SELECT type_name
2117 FROM ttrss_prefs,ttrss_prefs_types
2118 WHERE pref_name = '$pref_name' AND type_id = ttrss_prefs_types.id");
2119
2120 if (db_num_rows($result) > 0) {
2121
2122 $type_name = db_fetch_result($result, 0, "type_name");
2123
2124 // print "$pref_name : $type_name : $value<br>";
2125
2126 if ($type_name == "bool") {
2127 if ($value == "1") {
2128 $value = "true";
2129 } else {
2130 $value = "false";
2131 }
2132 } else if ($type_name == "integer") {
2133 $value = sprintf("%d", $value);
2134 }
2135
2136 // print "$pref_name : $type_name : $value<br>";
2137
2138 db_query($link, "UPDATE ttrss_user_prefs SET value = '$value'
2139 WHERE pref_name = '$pref_name' AND owner_uid = ".$_SESSION["uid"]);
2140
2141 }
2142
2143 header("Location: prefs.php");
2144
2145 }
2146
2147 } else if ($subop == "getHelp") {
2148
2149 $pref_name = db_escape_string($_GET["pn"]);
2150
2151 $result = db_query($link, "SELECT help_text FROM ttrss_prefs
2152 WHERE pref_name = '$pref_name'");
2153
2154 if (db_num_rows($result) > 0) {
2155 $help_text = db_fetch_result($result, 0, "help_text");
2156 print $help_text;
2157 } else {
2158 print "Unknown option: $pref_name";
2159 }
2160
2161 } else if ($subop == "Change password") {
2162
2163 if (WEB_DEMO_MODE) {
2164 header("Location: prefs.php");
2165 return;
2166 }
2167
2168 $old_pw = $_POST["OLD_PASSWORD"];
2169 $new_pw = $_POST["OLD_PASSWORD"];
2170
2171 $old_pw_hash = 'SHA1:' . sha1($_POST["OLD_PASSWORD"]);
2172 $new_pw_hash = 'SHA1:' . sha1($_POST["NEW_PASSWORD"]);
2173
2174 $active_uid = $_SESSION["uid"];
2175
2176 if ($old_pw && $new_pw) {
2177
2178 $login = db_escape_string($_SERVER['PHP_AUTH_USER']);
2179
2180 $result = db_query($link, "SELECT id FROM ttrss_users WHERE
2181 id = '$active_uid' AND (pwd_hash = '$old_pw' OR
2182 pwd_hash = '$old_pw_hash')");
2183
2184 if (db_num_rows($result) == 1) {
2185 db_query($link, "UPDATE ttrss_users SET pwd_hash = '$new_pw_hash'
2186 WHERE id = '$active_uid'");
2187
2188 $_SESSION["pwd_change_result"] = "ok";
2189 } else {
2190 $_SESSION["pwd_change_result"] = "failed";
2191 }
2192 }
2193
2194 header("Location: prefs.php");
2195
2196 } else if ($subop == "Reset to defaults") {
2197
2198 if (WEB_DEMO_MODE) {
2199 header("Location: prefs.php");
2200 return;
2201 }
2202
2203 $_SESSION["prefs_op_result"] = "reset-to-defaults";
2204
2205 if (DB_TYPE == "pgsql") {
2206 db_query($link,"UPDATE ttrss_user_prefs
2207 SET value = ttrss_prefs.def_value
2208 WHERE owner_uid = '".$_SESSION["uid"]."' AND
2209 ttrss_prefs.pref_name = ttrss_user_prefs.pref_name");
2210 } else {
2211 db_query($link, "DELETE FROM ttrss_user_prefs
2212 WHERE owner_uid = ".$_SESSION["uid"]);
2213 initialize_user_prefs($link, $_SESSION["uid"]);
2214 }
2215
2216 header("Location: prefs.php");
2217
2218 } else {
2219
2220 if (!SINGLE_USER_MODE) {
2221
2222 $result = db_query($link, "SELECT id FROM ttrss_users
2223 WHERE id = ".$_SESSION["uid"]." AND (pwd_hash = 'password' OR
2224 pwd_hash = 'SHA1:".sha1("password")."')");
2225
2226 if (db_num_rows($result) != 0) {
2227 print "<div class=\"warning\">
2228 Your password is at default value, please change it.
2229 </div>";
2230 }
2231
2232 if ($_SESSION["pwd_change_result"] == "failed") {
2233 print "<div class=\"warning\">
2234 There was an error while changing your password.
2235 </div>";
2236 }
2237
2238 if ($_SESSION["pwd_change_result"] == "ok") {
2239 print "<div class=\"notice\">
2240 Password changed successfully.
2241 </div>";
2242 }
2243
2244 $_SESSION["pwd_change_result"] = "";
2245
2246 if ($_SESSION["prefs_op_result"] == "reset-to-defaults") {
2247 print "<div class=\"notice\">
2248 Your configuration was reset to defaults.
2249 </div>";
2250 }
2251
2252 if ($_SESSION["prefs_op_result"] == "save-config") {
2253 print "<div class=\"notice\">
2254 Your configuration was saved successfully.
2255 </div>";
2256 }
2257
2258 $_SESSION["prefs_op_result"] = "";
2259
2260 print "<form action=\"backend.php\" method=\"POST\">";
2261
2262 print "<table width=\"100%\" class=\"prefPrefsList\">";
2263 print "<tr><td colspan='3'><h3>Authentication</h3></tr></td>";
2264
2265 print "<tr><td width=\"40%\">Old password</td>";
2266 print "<td><input class=\"editbox\" type=\"password\"
2267 name=\"OLD_PASSWORD\"></td></tr>";
2268
2269 print "<tr><td width=\"40%\">New password</td>";
2270
2271 print "<td><input class=\"editbox\" type=\"password\"
2272 name=\"NEW_PASSWORD\"></td></tr>";
2273
2274 print "</table>";
2275
2276 print "<input type=\"hidden\" name=\"op\" value=\"pref-prefs\">";
2277
2278 print "<p><input class=\"button\" type=\"submit\"
2279 value=\"Change password\" name=\"subop\">";
2280
2281 print "</form>";
2282
2283 }
2284
2285 $result = db_query($link, "SELECT
2286 ttrss_user_prefs.pref_name,short_desc,help_text,value,type_name,
2287 section_name,def_value
2288 FROM ttrss_prefs,ttrss_prefs_types,ttrss_prefs_sections,ttrss_user_prefs
2289 WHERE type_id = ttrss_prefs_types.id AND
2290 section_id = ttrss_prefs_sections.id AND
2291 ttrss_user_prefs.pref_name = ttrss_prefs.pref_name AND
2292 owner_uid = ".$_SESSION["uid"]."
2293 ORDER BY section_id,short_desc");
2294
2295 print "<form action=\"backend.php\" method=\"POST\">";
2296
2297 $lnum = 0;
2298
2299 $active_section = "";
2300
2301 while ($line = db_fetch_assoc($result)) {
2302
2303 if ($active_section != $line["section_name"]) {
2304
2305 if ($active_section != "") {
2306 print "</table>";
2307 }
2308
2309 print "<p><table width=\"100%\" class=\"prefPrefsList\">";
2310
2311 $active_section = $line["section_name"];
2312
2313 print "<tr><td colspan=\"3\"><h3>$active_section</h3></td></tr>";
2314 // print "<tr class=\"title\">
2315 // <td width=\"25%\">Option</td><td>Value</td></tr>";
2316
2317 $lnum = 0;
2318 }
2319
2320 // $class = ($lnum % 2) ? "even" : "odd";
2321
2322 print "<tr>";
2323
2324 $type_name = $line["type_name"];
2325 $pref_name = $line["pref_name"];
2326 $value = $line["value"];
2327 $def_value = $line["def_value"];
2328 $help_text = $line["help_text"];
2329
2330 print "<td width=\"40%\" id=\"$pref_name\">" . $line["short_desc"];
2331
2332 if ($help_text) print "<div class=\"prefHelp\">$help_text</div>";
2333
2334 print "</td>";
2335
2336 print "<td>";
2337
2338 if ($type_name == "bool") {
2339 // print_select($pref_name, $value, array("true", "false"));
2340
2341 if ($value == "true") {
2342 $value = "Yes";
2343 } else {
2344 $value = "No";
2345 }
2346
2347 print_radio($pref_name, $value, array("Yes", "No"));
2348
2349 } else {
2350 print "<input class=\"editbox\" name=\"$pref_name\" value=\"$value\">";
2351 }
2352
2353 print "</td>";
2354
2355 print "</tr>";
2356
2357 $lnum++;
2358 }
2359
2360 print "</table>";
2361
2362 print "<input type=\"hidden\" name=\"op\" value=\"pref-prefs\">";
2363
2364 print "<p><input class=\"button\" type=\"submit\"
2365 name=\"subop\" value=\"Save configuration\">";
2366
2367 print "&nbsp;<input class=\"button\" type=\"submit\"
2368 name=\"subop\" value=\"Reset to defaults\"></p>";
2369
2370 print "</form>";
2371
2372 }
2373
2374 }
2375
2376 if ($op == "pref-users") {
2377
2378 $subop = $_GET["subop"];
2379
2380 if ($subop == "editSave") {
2381
2382 if (!WEB_DEMO_MODE) {
2383
2384 $login = db_escape_string($_GET["l"]);
2385 $uid = db_escape_string($_GET["id"]);
2386 $access_level = sprintf("%d", $_GET["al"]);
2387
2388 db_query($link, "UPDATE ttrss_users SET login = '$login', access_level = '$access_level' WHERE id = '$uid'");
2389
2390 }
2391 } else if ($subop == "remove") {
2392
2393 if (!WEB_DEMO_MODE && $_SESSION["access_level"] >= 10) {
2394
2395 $ids = split(",", $_GET["ids"]);
2396
2397 foreach ($ids as $id) {
2398 db_query($link, "DELETE FROM ttrss_users WHERE id = '$id' AND id != " . $_SESSION["uid"]);
2399
2400 }
2401 }
2402 } else if ($subop == "add") {
2403
2404 if (!WEB_DEMO_MODE && $_SESSION["access_level"] >= 10) {
2405
2406 $login = db_escape_string(trim($_GET["login"]));
2407 $tmp_user_pwd = make_password(8);
2408 $pwd_hash = 'SHA1:' . sha1($tmp_user_pwd);
2409
2410 db_query($link, "INSERT INTO ttrss_users (login,pwd_hash,access_level)
2411 VALUES ('$login', '$pwd_hash', 0)");
2412
2413
2414 $result = db_query($link, "SELECT id FROM ttrss_users WHERE
2415 login = '$login' AND pwd_hash = '$pwd_hash'");
2416
2417 if (db_num_rows($result) == 1) {
2418
2419 $new_uid = db_fetch_result($result, 0, "id");
2420
2421 print "<div class=\"notice\">Added user <b>".$_GET["login"].
2422 "</b> with password <b>$tmp_user_pwd</b>.</div>";
2423
2424 initialize_user($link, $new_uid);
2425
2426 } else {
2427
2428 print "<div class=\"warning\">Error while adding user <b>".
2429 $_GET["login"].".</b></div>";
2430
2431 }
2432 }
2433 } else if ($subop == "resetPass") {
2434
2435 if (!WEB_DEMO_MODE && $_SESSION["access_level"] >= 10) {
2436
2437 $uid = db_escape_string($_GET["id"]);
2438
2439 $result = db_query($link, "SELECT login FROM ttrss_users WHERE id = '$uid'");
2440
2441 $login = db_fetch_result($result, 0, "login");
2442 $tmp_user_pwd = make_password(8);
2443 $pwd_hash = 'SHA1:' . sha1($tmp_user_pwd);
2444
2445 db_query($link, "UPDATE ttrss_users SET pwd_hash = '$pwd_hash'
2446 WHERE id = '$uid'");
2447
2448 print "<div class=\"notice\">Changed password of
2449 user <b>$login</b> to <b>$tmp_user_pwd</b>.</div>";
2450
2451 }
2452 }
2453
2454 print "<div class=\"prefGenericAddBox\">
2455 <input id=\"uadd_box\" size=\"40\">&nbsp;";
2456
2457 print"<input type=\"submit\" class=\"button\"
2458 onclick=\"javascript:addUser()\" value=\"Add user\"></div>";
2459
2460 $result = db_query($link, "SELECT
2461 id,login,access_level,
2462 SUBSTRING(last_login,1,16) as last_login
2463 FROM
2464 ttrss_users
2465 ORDER by login");
2466
2467 print "<div id=\"infoBoxShadow\"><div id=\"infoBox\">PLACEHOLDER</div></div>";
2468
2469 print "<p><table width=\"100%\" class=\"prefUserList\" id=\"prefUserList\">";
2470
2471 print "<tr class=\"title\">
2472 <td width=\"5%\">Select</td>
2473 <td width='30%'>Username</td>
2474 <td width='30%'>Access Level</td>
2475 <td width='30%'>Last login</td></tr>";
2476
2477 $lnum = 0;
2478
2479 while ($line = db_fetch_assoc($result)) {
2480
2481 $class = ($lnum % 2) ? "even" : "odd";
2482
2483 $uid = $line["id"];
2484 $edit_uid = $_GET["id"];
2485
2486 if ($uid == $_SESSION["uid"] || ($subop == "edit" && $uid != $edit_uid)) {
2487 $class .= "Grayed";
2488 }
2489
2490 print "<tr class=\"$class\" id=\"UMRR-$uid\">";
2491
2492 $line["login"] = htmlspecialchars($line["login"]);
2493
2494 if ($uid == $_SESSION["uid"]) {
2495
2496 print "<td><input disabled=\"true\" type=\"checkbox\"
2497 id=\"UMCHK-".$line["id"]."\"></td>";
2498
2499 print "<td>".$line["login"]."</td>";
2500 print "<td>".$line["access_level"]."</td>";
2501
2502 } else if (!$edit_uid || $subop != "edit") {
2503
2504 print "<td><input onclick='toggleSelectRow(this);'
2505 type=\"checkbox\" id=\"UMCHK-$uid\"></td>";
2506
2507 print "<td><a href=\"javascript:editUser($uid);\">" .
2508 $line["login"] . "</td>";
2509
2510 print "<td><a href=\"javascript:editUser($uid);\">" .
2511 $line["access_level"] . "</td>";
2512
2513 } else if ($uid != $edit_uid) {
2514
2515 print "<td><input disabled=\"true\" type=\"checkbox\"
2516 id=\"UMCHK-".$line["id"]."\"></td>";
2517
2518 print "<td>".$line["login"]."</td>";
2519 print "<td>".$line["access_level"]."</td>";
2520
2521 } else {
2522
2523 print "<td><input disabled=\"true\" type=\"checkbox\" checked></td>";
2524
2525 print "<td><input id=\"iedit_ulogin\" value=\"".$line["login"].
2526 "\"></td>";
2527
2528 print "<td><input id=\"iedit_ulevel\" value=\"".$line["access_level"].
2529 "\"></td>";
2530
2531 }
2532
2533 print "<td>".$line["last_login"]."</td>";
2534
2535 print "</tr>";
2536
2537 ++$lnum;
2538 }
2539
2540 print "</table>";
2541
2542 print "<p>";
2543
2544 if ($subop == "edit") {
2545 print "Edit label:
2546 <input type=\"submit\" class=\"button\"
2547 onclick=\"javascript:userEditCancel()\" value=\"Cancel\">
2548 <input type=\"submit\" class=\"button\"
2549 onclick=\"javascript:userEditSave()\" value=\"Save\">";
2550
2551 } else {
2552
2553 print "
2554 Selection:
2555 <input type=\"submit\" class=\"button\"
2556 onclick=\"javascript:selectedUserDetails()\" value=\"User details\">
2557 <input type=\"submit\" class=\"button\"
2558 onclick=\"javascript:editSelectedUser()\" value=\"Edit\">
2559 <input type=\"submit\" class=\"button\"
2560 onclick=\"javascript:removeSelectedUsers()\" value=\"Remove\">
2561 <input type=\"submit\" class=\"button\"
2562 onclick=\"javascript:resetSelectedUserPass()\" value=\"Reset password\">";
2563
2564 }
2565 }
2566
2567 if ($op == "user-details") {
2568
2569 if (WEB_DEMO_MODE || $_SESSION["access_level"] < 10) {
2570 return;
2571 }
2572
2573 /* print "<html><head>
2574 <title>Tiny Tiny RSS : User Details</title>
2575 <link rel=\"stylesheet\" href=\"tt-rss.css\" type=\"text/css\">
2576 <meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">
2577 </head><body>"; */
2578
2579 $uid = sprintf("%d", $_GET["id"]);
2580
2581 print "<div class='infoBoxContents'>";
2582
2583 $result = db_query($link, "SELECT login,
2584 SUBSTRING(last_login,1,16) AS last_login,
2585 access_level,
2586 (SELECT COUNT(int_id) FROM ttrss_user_entries
2587 WHERE owner_uid = id) AS stored_articles
2588 FROM ttrss_users
2589 WHERE id = '$uid'");
2590
2591 if (db_num_rows($result) == 0) {
2592 print "<h1>User not found</h1>";
2593 return;
2594 }
2595
2596 print "<h1>User Details</h1>";
2597
2598 print "<table width='100%'>";
2599
2600 $login = db_fetch_result($result, 0, "login");
2601 $last_login = db_fetch_result($result, 0, "last_login");
2602 $access_level = db_fetch_result($result, 0, "access_level");
2603 $stored_articles = db_fetch_result($result, 0, "stored_articles");
2604
2605 print "<tr><td>Username</td><td>$login</td></tr>";
2606 print "<tr><td>Access level</td><td>$access_level</td></tr>";
2607 print "<tr><td>Last logged in</td><td>$last_login</td></tr>";
2608 print "<tr><td>Stored articles</td><td>$stored_articles</td></tr>";
2609
2610 $result = db_query($link, "SELECT COUNT(id) as num_feeds FROM ttrss_feeds
2611 WHERE owner_uid = '$uid'");
2612
2613 $num_feeds = db_fetch_result($result, 0, "num_feeds");
2614
2615 print "<tr><td>Subscribed feeds count</td><td>$num_feeds</td></tr>";
2616
2617 /* $result = db_query($link, "SELECT
2618 SUM(LENGTH(content)+LENGTH(title)+LENGTH(link)+LENGTH(guid)) AS db_size
2619 FROM ttrss_user_entries,ttrss_entries
2620 WHERE owner_uid = '$uid' AND ref_id = id");
2621
2622 $db_size = round(db_fetch_result($result, 0, "db_size") / 1024);
2623
2624 print "<tr><td>Approx. used DB size</td><td>$db_size KBytes</td></tr>"; */
2625
2626 print "</table>";
2627
2628 print "<h1>Subscribed feeds</h1>";
2629
2630 $result = db_query($link, "SELECT id,title,feed_url FROM ttrss_feeds
2631 WHERE owner_uid = '$uid' ORDER BY title");
2632
2633 print "<ul class=\"nomarks\">";
2634
2635 while ($line = db_fetch_assoc($result)) {
2636
2637 $icon_file = ICONS_URL."/".$line["id"].".ico";
2638
2639 if (file_exists($icon_file) && filesize($icon_file) > 0) {
2640 $feed_icon = "<img class=\"tinyFeedIcon\" src=\"$icon_file\">";
2641 } else {
2642 $feed_icon = "<img class=\"tinyFeedIcon\" src=\"images/blank_icon.gif\">";
2643 }
2644
2645 print "<li>$feed_icon&nbsp;<a href=\"".$line["feed_url"]."\">".$line["title"]."</a></li>";
2646 }
2647
2648 print "</ul>";
2649
2650 print "</div>";
2651
2652 print "<div align='center'>
2653 <input type='submit' class='button'
2654 onclick=\"closeInfoBox()\" value=\"Close this window\"></div>";
2655
2656 // print "</body></html>";
2657
2658 }
2659
2660 if ($op == "feed-details") {
2661
2662 $feed_id = $_GET["id"];
2663
2664 $result = db_query($link,
2665 "SELECT
2666 title,feed_url,last_updated,icon_url,site_url,
2667 (SELECT COUNT(int_id) FROM ttrss_user_entries
2668 WHERE feed_id = id) AS total,
2669 (SELECT COUNT(int_id) FROM ttrss_user_entries
2670 WHERE feed_id = id AND unread = true) AS unread,
2671 (SELECT COUNT(int_id) FROM ttrss_user_entries
2672 WHERE feed_id = id AND marked = true) AS marked
2673 FROM ttrss_feeds
2674 WHERE id = '$feed_id' AND owner_uid = ".$_SESSION["uid"]);
2675
2676 if (db_num_rows($result) == 0) return;
2677
2678 $title = db_fetch_result($result, 0, "title");
2679 $last_updated = db_fetch_result($result, 0, "last_updated");
2680 $feed_url = db_fetch_result($result, 0, "feed_url");
2681 $icon_url = db_fetch_result($result, 0, "icon_url");
2682 $total = db_fetch_result($result, 0, "total");
2683 $unread = db_fetch_result($result, 0, "unread");
2684 $marked = db_fetch_result($result, 0, "marked");
2685 $site_url = db_fetch_result($result, 0, "site_url");
2686
2687 $result = db_query($link, "SELECT COUNT(id) AS subscribed
2688 FROM ttrss_feeds WHERE feed_url = '$feed_url'");
2689
2690 $subscribed = db_fetch_result($result, 0, "subscribed");
2691
2692 print "<div class=\"infoBoxContents\">";
2693
2694 $icon_file = ICONS_DIR . "/$feed_id.ico";
2695
2696 if (file_exists($icon_file) && filesize($icon_file) > 0) {
2697 $feed_icon = "<img width=\"16\" height=\"16\"
2698 src=\"" . ICONS_URL . "/$feed_id.ico\">";
2699 } else {
2700 $feed_icon = "";
2701 }
2702
2703 print "<h1>$feed_icon $title</h1>";
2704
2705 print "<table width='100%'>";
2706
2707 if ($site_url) {
2708 print "<tr><td width='30%'>Link</td>
2709 <td><a href=\"$site_url\">$site_url</a>
2710 <a href=\"$feed_url\">(feed)</a></td>
2711 </td></tr>";
2712 } else {
2713 print "<tr><td width='30%'>Feed URL</td>
2714 <td><a href=\"$feed_url\">$feed_url</a></td></tr>";
2715 }
2716 print "<tr><td>Last updated</td><td>$last_updated</td></tr>";
2717 print "<tr><td>Total articles</td><td>$total</td></tr>";
2718 print "<tr><td>Unread articles</td><td>$unread</td></tr>";
2719 print "<tr><td>Starred articles</td><td>$marked</td></tr>";
2720 print "<tr><td>Subscribed users</td><td>$subscribed</td></tr>";
2721
2722 print "</table>";
2723
2724 $result = db_query($link, "SELECT title,
2725 SUBSTRING(updated,1,16) AS updated,unread
2726 FROM ttrss_entries,ttrss_user_entries
2727 WHERE ref_id = id AND feed_id = '$feed_id'
2728 ORDER BY date_entered DESC LIMIT 5");
2729
2730 if (db_num_rows($result) > 0) {
2731
2732 print "<h1>Latest headlines</h1>";
2733
2734 print "<ul class=\"nomarks\">";
2735
2736 while ($line = db_fetch_assoc($result)) {
2737 if ($line["unread"] == "t" || $line["unread"] == "1") {
2738 $line["title"] = "<b>" . $line["title"] . "</b>";
2739 }
2740 print "<li>" . $line["title"].
2741 "&nbsp;<span class=\"insensitive\">(" .$line["updated"].")</span></li>";
2742 }
2743
2744 print "</ul>";
2745
2746 print "</div>";
2747
2748 print "<div align='center'>
2749 <input type='submit' class='button'
2750 onclick=\"closeInfoBox()\" value=\"Close this window\"></div>";
2751 }
2752 }
2753
2754 db_close($link);
2755 ?>
2756
2757 <!-- <?= sprintf("Backend execution time: %.4f seconds", getmicrotime() - $script_started) ?> -->
2758