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