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