]> git.wh0rd.org Git - tt-rss.git/blob - modules/pref-feeds.php
allow onclick editing of categories and drag-reordering of feeds and categories
[tt-rss.git] / modules / pref-feeds.php
1 <?php
2
3         function batch_edit_cbox($elem, $label = false) {
4                 print "<input type=\"checkbox\" title=\"".__("Check to enable field")."\"
5                         onchange=\"batchFeedsToggleField(this, '$elem', '$label')\">";
6         }
7
8         function module_pref_feeds($link) {
9
10                 global $update_intervals;
11                 global $purge_intervals;
12                 global $update_methods;
13
14                 $subop = $_REQUEST["subop"];
15                 $quiet = $_REQUEST["quiet"];
16                 $mode = $_REQUEST["mode"];
17
18                 if ($subop == "renamecat") {
19                         $title = db_escape_string($_REQUEST['title']);
20                         $id = db_escape_string($_REQUEST['id']);
21
22                         if ($title) {
23                                 db_query($link, "UPDATE ttrss_feed_categories SET
24                                         title = '$title' WHERE id = '$id' AND owner_uid = " . $_SESSION["uid"]);
25                         }
26                         return;
27                 }
28
29                 if ($subop == "getfeedtree") {
30
31                         $root = array();
32                         $root['id'] = 'root';
33                         $root['name'] = __('Feeds');
34                         $root['items'] = array();
35
36                         if (get_pref($link, 'ENABLE_FEED_CATS')) {
37
38                                 $result = db_query($link, "SELECT id, title FROM ttrss_feed_categories
39                                         WHERE owner_uid = " . $_SESSION["uid"] . " ORDER BY order_id, title");
40         
41                                 while ($line = db_fetch_assoc($result)) {
42                                         $cat = array();
43                                         $cat['id'] = 'CAT:' . $line['id'];
44                                         $cat['bare_id'] = $feed_id;
45                                         $cat['name'] = $line['title'];
46                                         $cat['items'] = array();
47         
48                                         $feed_result = db_query($link, "SELECT id, title FROM ttrss_feeds
49                                                 WHERE cat_id = '".$line['id']."' AND owner_uid = ".$_SESSION["uid"].
50                                                 " ORDER BY order_id, title");
51         
52                                         while ($feed_line = db_fetch_assoc($feed_result)) {
53                                                 $feed = array();
54                                                 $feed['id'] = 'FEED:' . $feed_line['id'];
55                                                 $feed['bare_id'] = $feed_line['id'];
56                                                 $feed['name'] = $feed_line['title'];
57                                                 $feed['checkbox'] = false;
58                                                 array_push($cat['items'], $feed);
59                                         }
60         
61                                         array_push($root['items'], $cat);
62                                 }
63         
64                                 /* Uncategorized is a special case */
65         
66                                 $cat = array();
67                                 $cat['id'] = 'CAT:0';
68                                 $cat['bare_id'] = 0;
69                                 $cat['name'] = __("Uncategorized");
70                                 $cat['items'] = array();
71         
72                                 $feed_result = db_query($link, "SELECT id, title FROM ttrss_feeds
73                                         WHERE cat_id IS NULL AND owner_uid = ".$_SESSION["uid"].
74                                         " ORDER BY order_id, title");
75         
76                                 while ($feed_line = db_fetch_assoc($feed_result)) {
77                                         $feed = array();
78                                         $feed['id'] = 'FEED:' . $feed_line['id'];
79                                         $feed['bare_id'] = $feed_line['id'];
80                                         $feed['name'] = $feed_line['title'];
81                                         $feed['checkbox'] = false;
82                                         array_push($cat['items'], $feed);
83                                 }
84         
85                                 array_push($root['items'], $cat);
86                         } else {
87                                 $feed_result = db_query($link, "SELECT id, title FROM ttrss_feeds
88                                         WHERE owner_uid = ".$_SESSION["uid"].
89                                         " ORDER BY order_id, title");
90         
91                                 while ($feed_line = db_fetch_assoc($feed_result)) {
92                                         $feed = array();
93                                         $feed['id'] = 'FEED:' . $feed_line['id'];
94                                         $feed['bare_id'] = $feed_line['id'];
95                                         $feed['name'] = $feed_line['title'];
96                                         $feed['checkbox'] = false;
97                                         array_push($root['items'], $feed);
98                                 }
99                         }
100
101                         $fl = array();
102                         $fl['identifier'] = 'id';
103                         $fl['label'] = 'name';
104                         $fl['items'] = array($root);
105
106                         print json_encode($fl);
107                         return;
108                 }
109
110                 if ($subop == "catsortreset") {
111                         db_query($link, "UPDATE ttrss_feed_categories 
112                                         SET order_id = 0 WHERE owner_uid = " . $_SESSION["uid"]);
113                         return;
114                 }       
115
116                 if ($subop == "feedsortreset") {
117                         db_query($link, "UPDATE ttrss_feeds 
118                                         SET order_id = 0 WHERE owner_uid = " . $_SESSION["uid"]);
119                         return;
120                 }       
121
122                 if ($subop == "savefeedorder") {
123                         if ($_POST['payload']) {
124                                 file_put_contents("/tmp/blahblah.txt", $_POST['payload']);
125                         }
126
127                         $data = file_get_contents("/tmp/blahblah.txt");
128                         $data = json_decode($data, true);       
129
130                         if (is_array($data) && is_array($data['items'])) {
131                                 $cat_order_id = 0;
132
133                                 $data_map = array();
134
135                                 foreach ($data['items'] as $item) {
136                                         $data_map[$item['id']] =& $item['items'];
137                                 }
138
139                                 foreach ($data['items'][0]['items'] as $item) {
140                                         $id = $item['_reference'];
141                                         $bare_id = substr($id, strpos($id, ':')+1);
142
143                                         ++$cat_order_id;
144
145                                         if ($bare_id > 0) {
146                                                 db_query($link, "UPDATE ttrss_feed_categories 
147                                                         SET order_id = '$cat_order_id' WHERE id = '$bare_id' AND
148                                                         owner_uid = " . $_SESSION["uid"]);
149                                         }
150
151                                         $feed_order_id = 0;
152
153                                         if (is_array($data_map[$id])) {
154                                                 foreach ($data_map[$id] as $feed) {
155                                                         $id = $feed['_reference'];
156                                                         $bare_id = substr($id, strpos($id, ':')+1);
157         
158                                                         db_query($link, "UPDATE ttrss_feeds
159                                                                 SET order_id = '$feed_order_id' WHERE id = '$bare_id' AND
160                                                                 owner_uid = " . $_SESSION["uid"]);
161         
162                                                         ++$feed_order_id;
163                                                 } 
164                                         }
165                                 }
166                         }
167
168                         return;
169                 }
170
171                 if ($subop == "removeicon") {                   
172                         $feed_id = db_escape_string($_REQUEST["feed_id"]);
173
174                         $result = db_query($link, "SELECT id FROM ttrss_feeds
175                                 WHERE id = '$feed_id' AND owner_uid = ". $_SESSION["uid"]);
176
177                         if (db_num_rows($result) != 0) {
178                                 unlink(ICONS_DIR . "/$feed_id.ico");
179                         }
180
181                         return;
182                 }
183
184                 if ($subop == "uploadicon") {
185                         $icon_file = $_FILES['icon_file']['tmp_name'];
186                         $feed_id = db_escape_string($_REQUEST["feed_id"]);
187
188                         if (is_file($icon_file) && $feed_id) {
189                                 if (filesize($icon_file) < 20000) {
190                                         
191                                         $result = db_query($link, "SELECT id FROM ttrss_feeds
192                                                 WHERE id = '$feed_id' AND owner_uid = ". $_SESSION["uid"]);
193
194                                         if (db_num_rows($result) != 0) {
195                                                 unlink(ICONS_DIR . "/$feed_id.ico");
196                                                 move_uploaded_file($icon_file, ICONS_DIR . "/$feed_id.ico");
197                                                 $rc = 0;
198                                         } else {
199                                                 $rc = 2;
200                                         }
201                                 } else {
202                                         $rc = 1;
203                                 }
204                         } else {
205                                 $rc = 2;
206                         }
207
208                         print "<script type=\"text/javascript\">";
209                         print "parent.uploadIconHandler($rc);";
210                         print "</script>";
211                         return;
212                 }
213
214                 if ($subop == "editfeed") {
215                         header("Content-Type: text/xml");
216
217                         print "<dlg id=\"$subop\">";
218                         print "<title>".__('Feed Editor')."</title>";
219                         print "<content><![CDATA[";
220
221                         $feed_id = db_escape_string($_REQUEST["id"]);
222
223                         $result = db_query($link, 
224                                 "SELECT * FROM ttrss_feeds WHERE id = '$feed_id' AND
225                                         owner_uid = " . $_SESSION["uid"]);
226
227                         $title = htmlspecialchars(db_fetch_result($result,
228                                 0, "title"));
229
230                         $icon_file = ICONS_DIR . "/$feed_id.ico";
231         
232                         if (file_exists($icon_file) && filesize($icon_file) > 0) {
233                                         $feed_icon = "<img width=\"16\" height=\"16\"
234                                                 src=\"" . ICONS_URL . "/$feed_id.ico\">";
235                         } else {
236                                 $feed_icon = "";
237                         }
238
239                         print "<form id=\"edit_feed_form\" onsubmit=\"return false\">"; 
240
241                         print "<input type=\"hidden\" name=\"id\" value=\"$feed_id\">";
242                         print "<input type=\"hidden\" name=\"op\" value=\"pref-feeds\">";
243                         print "<input type=\"hidden\" name=\"subop\" value=\"editSave\">";
244
245                         print "<div class=\"dlgSec\">".__("Feed")."</div>";
246                         print "<div class=\"dlgSecCont\">";
247
248                         /* Title */
249
250                         print "<input style=\"font-size : 16px\" size=\"40\" onkeypress=\"return filterCR(event, feedEditSave)\"
251                                             name=\"title\" value=\"$title\">";
252
253                         /* Feed URL */
254
255                         $feed_url = db_fetch_result($result, 0, "feed_url");
256                         $feed_url = htmlspecialchars(db_fetch_result($result,
257                                 0, "feed_url"));
258
259                         print "<br/>";
260
261                         print __('URL:') . " ";
262                         print "<input size=\"40\" onkeypress=\"return filterCR(event, feedEditSave)\"
263                                 name=\"feed_url\" value=\"$feed_url\">";
264
265                         /* Category */
266
267                         if (get_pref($link, 'ENABLE_FEED_CATS')) {
268
269                                 $cat_id = db_fetch_result($result, 0, "cat_id");
270
271                                 print "<br/>";
272
273                                 print __('Place in category:') . " ";
274
275                                 print_feed_cat_select($link, "cat_id", $cat_id, $disabled);
276                         }
277
278                         print "</div>";
279
280                         print "<div class=\"dlgSec\">".__("Update")."</div>";
281                         print "<div class=\"dlgSecCont\">";
282
283                         /* Update Interval */
284
285                         $update_interval = db_fetch_result($result, 0, "update_interval");
286
287                         print_select_hash("update_interval", $update_interval, $update_intervals);
288
289                         /* Update method */
290
291                         $update_method = db_fetch_result($result, 0, "update_method");
292
293                         print " " . __('using') . " ";
294                         print_select_hash("update_method", $update_method, $update_methods);                    
295
296                         $purge_interval = db_fetch_result($result, 0, "purge_interval");
297
298                         if (FORCE_ARTICLE_PURGE == 0) {
299
300                                 /* Purge intl */
301
302                                 print "<br/>";
303
304                                 print __('Article purging:') . " ";
305
306                                 print_select_hash("purge_interval", $purge_interval, $purge_intervals);
307
308                         } else {
309                                 print "<input type='hidden' name='purge_interval' value='$purge_interval'>";
310
311                         }
312
313                         print "</div>";
314                         print "<div class=\"dlgSec\">".__("Authentication")."</div>";
315                         print "<div class=\"dlgSecCont\">";
316
317                         $auth_login = htmlspecialchars(db_fetch_result($result, 0, "auth_login"));
318
319                         print "<table>";
320
321                         print "<tr><td>" . __('Login:') . "</td><td>";
322
323                         print "<input size=\"20\" onkeypress=\"return filterCR(event, feedEditSave)\"
324                                 name=\"auth_login\" value=\"$auth_login\">";
325
326                         print "</tr><tr><td>" . __("Password:") . "</td><td>";
327
328                         $auth_pass = htmlspecialchars(db_fetch_result($result, 0, "auth_pass"));
329
330                         print "<input size=\"20\" type=\"password\" name=\"auth_pass\" 
331                                 onkeypress=\"return filterCR(event, feedEditSave)\"
332                                 value=\"$auth_pass\">";
333
334                         print "</td></tr></table>";
335
336                         print "</div>";
337                         print "<div class=\"dlgSec\">".__("Options")."</div>";
338                         print "<div class=\"dlgSecCont\">";
339
340                         print "<div style=\"line-height : 100%\">";
341
342                         $private = sql_bool_to_bool(db_fetch_result($result, 0, "private"));
343
344                         if ($private) {
345                                 $checked = "checked";
346                         } else {
347                                 $checked = "";
348                         }
349
350                         print "<input type=\"checkbox\" name=\"private\" id=\"private\" 
351                                 $checked>&nbsp;<label for=\"private\">".__('Hide from Popular feeds')."</label>";
352
353                         $rtl_content = sql_bool_to_bool(db_fetch_result($result, 0, "rtl_content"));
354
355                         if ($rtl_content) {
356                                 $checked = "checked";
357                         } else {
358                                 $checked = "";
359                         }
360
361                         print "<br/><input type=\"checkbox\" id=\"rtl_content\" name=\"rtl_content\"
362                                 $checked>&nbsp;<label for=\"rtl_content\">".__('Right-to-left content')."</label>";
363
364                         $include_in_digest = sql_bool_to_bool(db_fetch_result($result, 0, "include_in_digest"));
365
366                         if ($include_in_digest) {
367                                 $checked = "checked";
368                         } else {
369                                 $checked = "";
370                         }
371
372                         print "<br/><input type=\"checkbox\" id=\"include_in_digest\" 
373                                 name=\"include_in_digest\"
374                                 $checked>&nbsp;<label for=\"include_in_digest\">".__('Include in e-mail digest')."</label>";
375
376
377                         $always_display_enclosures = sql_bool_to_bool(db_fetch_result($result, 0, "always_display_enclosures"));
378
379                         if ($always_display_enclosures) {
380                                 $checked = "checked";
381                         } else {
382                                 $checked = "";
383                         }
384
385                         print "<br/><input type=\"checkbox\" id=\"always_display_enclosures\" 
386                                 name=\"always_display_enclosures\"
387                                 $checked>&nbsp;<label for=\"always_display_enclosures\">".__('Always display image attachments')."</label>";
388
389
390                         $cache_images = sql_bool_to_bool(db_fetch_result($result, 0, "cache_images"));
391
392                         if ($cache_images) {
393                                 $checked = "checked";
394                         } else {
395                                 $checked = "";
396                         }
397
398                         if (SIMPLEPIE_CACHE_IMAGES) {
399                                 $disabled = "";
400                                 $label_class = "";
401                         } else {
402                                 $disabled = "disabled";
403                                 $label_class = "class='insensitive'";
404                         }
405
406                         print "<br/><input type=\"checkbox\" id=\"cache_images\" 
407                                 name=\"cache_images\" $disabled
408                                 $checked>&nbsp;<label $label_class for=\"cache_images\">".
409                                 __('Cache images locally')."</label>";
410
411
412                         print "</div>";
413                         print "</div>";
414
415                         print "</form>";
416
417                         /* Icon */
418
419                         print "<br/>";
420
421                         print "<div class=\"dlgSec\">".__("Icon")."</div>";
422                         print "<div class=\"dlgSecCont\">";
423
424                         print "<iframe name=\"icon_upload_iframe\"
425                                 style=\"width: 400px; height: 100px; display: none;\"></iframe>";
426
427                         print "<form style='display : block' target=\"icon_upload_iframe\"
428                                 enctype=\"multipart/form-data\" method=\"POST\" 
429                                 action=\"backend.php\">
430                                 <input id=\"icon_file\" size=\"10\" name=\"icon_file\" type=\"file\">
431                                 <input type=\"hidden\" name=\"op\" value=\"pref-feeds\">
432                                 <input type=\"hidden\" name=\"feed_id\" value=\"$feed_id\">
433                                 <input type=\"hidden\" name=\"subop\" value=\"uploadicon\">
434                                 <button onclick=\"return uploadFeedIcon();\"
435                                         type=\"submit\">".__('Replace')."</button>
436                                 <button onclick=\"return removeFeedIcon($feed_id);\"
437                                         type=\"submit\">".__('Remove')."</button>
438                                 </form>";
439
440                         print "</div>";
441
442                         $title = htmlspecialchars($title, ENT_QUOTES);
443
444                         print "<div class='dlgButtons'>
445                                 <div style=\"float : left\">
446                                 <button onclick='return unsubscribeFeed($feed_id, \"$title\")'>".
447                                         __('Unsubscribe')."</button>
448                                 </div>
449                                 <button onclick=\"return feedEditSave()\">".__('Save')."</button>
450                                 <button onclick=\"return feedEditCancel()\">".__('Cancel')."</button>
451                                 </div>";
452
453                         print "]]></content></dlg>";
454
455                         return;
456                 }
457
458                 if ($subop == "editfeeds") {
459
460                         $feed_ids = db_escape_string($_REQUEST["ids"]);
461                         
462                         header("Content-Type: text/xml");
463                         print "<dlg id=\"$subop\">";
464                         print "<title>".__('Multiple Feed Editor')."</title>";
465                         print "<content><![CDATA[";
466
467                         print "<form id=\"batch_edit_feed_form\" onsubmit=\"return false\">";   
468
469                         print "<input type=\"hidden\" name=\"ids\" value=\"$feed_ids\">";
470                         print "<input type=\"hidden\" name=\"op\" value=\"pref-feeds\">";
471                         print "<input type=\"hidden\" name=\"subop\" value=\"batchEditSave\">";
472
473                         print "<div class=\"dlgSec\">".__("Feed")."</div>";
474                         print "<div class=\"dlgSecCont\">";
475
476                         /* Title */
477
478                         print "<input disabled style=\"font-size : 16px\" size=\"35\" onkeypress=\"return filterCR(event, feedEditSave)\"
479                                             name=\"title\" value=\"$title\">";
480
481                         batch_edit_cbox("title");
482
483                         /* Feed URL */
484
485                         print "<br/>";
486
487                         print __('URL:') . " ";
488                         print "<input disabled size=\"40\" onkeypress=\"return filterCR(event, feedEditSave)\"
489                                 name=\"feed_url\" value=\"$feed_url\">";
490
491                         batch_edit_cbox("feed_url");
492
493                         /* Category */
494
495                         if (get_pref($link, 'ENABLE_FEED_CATS')) {
496
497                                 print "<br/>";
498
499                                 print __('Place in category:') . " ";
500
501                                 print_feed_cat_select($link, "cat_id", $cat_id, "disabled");
502
503                                 batch_edit_cbox("cat_id");
504
505                         }
506
507                         print "</div>";
508
509                         print "<div class=\"dlgSec\">".__("Update")."</div>";
510                         print "<div class=\"dlgSecCont\">";
511
512                         /* Update Interval */
513
514                         print_select_hash("update_interval", $update_interval, $update_intervals, 
515                                 "disabled");
516
517                         batch_edit_cbox("update_interval");
518
519                         /* Update method */
520
521                         print " " . __('using') . " ";
522                         print_select_hash("update_method", $update_method, $update_methods, 
523                                 "disabled");                    
524                         batch_edit_cbox("update_method");
525
526                         /* Purge intl */
527
528                         if (FORCE_ARTICLE_PURGE != 0) {
529
530                                 print "<br/>";
531
532                                 print __('Article purging:') . " ";
533
534                                 print_select_hash("purge_interval", $purge_interval, $purge_intervals,
535                                         "disabled");
536
537                                 batch_edit_cbox("purge_interval");
538                         }
539
540                         print "</div>";
541                         print "<div class=\"dlgSec\">".__("Authentication")."</div>";
542                         print "<div class=\"dlgSecCont\">";
543
544                         print __('Login:') . " ";
545                         print "<input disabled size=\"15\" onkeypress=\"return filterCR(event, feedEditSave)\"
546                                 name=\"auth_login\" value=\"$auth_login\">";
547
548                         batch_edit_cbox("auth_login");
549
550                         print " " . __("Password:") . " ";
551
552                         print "<input disabled size=\"15\" type=\"password\" name=\"auth_pass\" 
553                                 onkeypress=\"return filterCR(event, feedEditSave)\"
554                                 value=\"$auth_pass\">";
555
556                         batch_edit_cbox("auth_pass");
557
558                         print "</div>";
559                         print "<div class=\"dlgSec\">".__("Options")."</div>";
560                         print "<div class=\"dlgSecCont\">";
561
562                         print "<div style=\"line-height : 100%\">";
563
564                         print "<input disabled type=\"checkbox\" name=\"private\" id=\"private\" 
565                                 $checked>&nbsp;<label id=\"private_l\" class='insensitive' for=\"private\">".__('Hide from Popular feeds')."</label>";
566
567                         print "&nbsp;"; batch_edit_cbox("private", "private_l");
568
569                         print "<br/><input disabled type=\"checkbox\" id=\"rtl_content\" name=\"rtl_content\"
570                                 $checked>&nbsp;<label class='insensitive' id=\"rtl_content_l\" for=\"rtl_content\">".__('Right-to-left content')."</label>";
571
572                         print "&nbsp;"; batch_edit_cbox("rtl_content", "rtl_content_l");
573
574                         print "<br/><input disabled type=\"checkbox\" id=\"include_in_digest\" 
575                                 name=\"include_in_digest\" 
576                                 $checked>&nbsp;<label id=\"include_in_digest_l\" class='insensitive' for=\"include_in_digest\">".__('Include in e-mail digest')."</label>";
577
578                         print "&nbsp;"; batch_edit_cbox("include_in_digest", "include_in_digest_l");
579
580                         print "<br/><input disabled type=\"checkbox\" id=\"always_display_enclosures\" 
581                                 name=\"always_display_enclosures\" 
582                                 $checked>&nbsp;<label id=\"always_display_enclosures_l\" class='insensitive' for=\"always_display_enclosures\">".__('Always display image attachments')."</label>";
583
584                         print "&nbsp;"; batch_edit_cbox("always_display_enclosures", "always_display_enclosures_l");
585
586                         print "<br/><input disabled type=\"checkbox\" id=\"cache_images\" 
587                                 name=\"cache_images\" 
588                                 $checked>&nbsp;<label class='insensitive' id=\"cache_images_l\" 
589                                         for=\"cache_images\">".
590                                 __('Cache images locally')."</label>";
591
592
593                         if (SIMPLEPIE_CACHE_IMAGES) {
594                                 print "&nbsp;"; batch_edit_cbox("cache_images", "cache_images_l");
595                         }
596
597                         print "</div>";
598                         print "</div>";
599
600                         print "</form>";
601
602                         print "<div class='dlgButtons'>
603                                 <input type=\"submit\" class=\"button\" 
604                                 onclick=\"return feedsEditSave()\" value=\"".__('Save')."\">
605                                 <input type='submit' class='button'                     
606                                 onclick=\"return feedEditCancel()\" value=\"".__('Cancel')."\">
607                                 </div>";
608
609                         print "]]></content></dlg>";
610
611                         return;
612                 }
613
614                 if ($subop == "editSave" || $subop == "batchEditSave") {
615
616                         $feed_title = db_escape_string(trim($_POST["title"]));
617                         $feed_link = db_escape_string(trim($_POST["feed_url"]));
618                         $upd_intl = db_escape_string($_POST["update_interval"]);
619                         $purge_intl = db_escape_string($_POST["purge_interval"]);
620                         $feed_id = db_escape_string($_POST["id"]); /* editSave */
621                         $feed_ids = db_escape_string($_POST["ids"]); /* batchEditSave */
622                         $cat_id = db_escape_string($_POST["cat_id"]);
623                         $auth_login = db_escape_string(trim($_POST["auth_login"]));
624                         $auth_pass = db_escape_string(trim($_POST["auth_pass"]));
625                         $private = checkbox_to_sql_bool(db_escape_string($_POST["private"]));
626                         $rtl_content = checkbox_to_sql_bool(db_escape_string($_POST["rtl_content"]));
627                         $include_in_digest = checkbox_to_sql_bool(
628                                 db_escape_string($_POST["include_in_digest"]));
629                         $cache_images = checkbox_to_sql_bool(
630                                 db_escape_string($_POST["cache_images"]));
631                         $update_method = (int) db_escape_string($_POST["update_method"]);
632
633                         $always_display_enclosures = checkbox_to_sql_bool(
634                                 db_escape_string($_POST["always_display_enclosures"]));
635
636                         if (get_pref($link, 'ENABLE_FEED_CATS')) {                      
637                                 if ($cat_id && $cat_id != 0) {
638                                         $category_qpart = "cat_id = '$cat_id',";
639                                         $category_qpart_nocomma = "cat_id = '$cat_id'";
640                                 } else {
641                                         $category_qpart = 'cat_id = NULL,';
642                                         $category_qpart_nocomma = 'cat_id = NULL';
643                                 }
644                         } else {
645                                 $category_qpart = "";
646                                 $category_qpart_nocomma = "";
647                         }
648
649                         if (SIMPLEPIE_CACHE_IMAGES) {
650                                 $cache_images_qpart = "cache_images = $cache_images,";
651                         } else {
652                                 $cache_images_qpart = "";
653                         }
654
655                         if ($subop == "editSave") {
656
657                                 $result = db_query($link, "UPDATE ttrss_feeds SET 
658                                         $category_qpart
659                                         title = '$feed_title', feed_url = '$feed_link',
660                                         update_interval = '$upd_intl',
661                                         purge_interval = '$purge_intl',
662                                         auth_login = '$auth_login',
663                                         auth_pass = '$auth_pass',
664                                         private = $private,
665                                         rtl_content = $rtl_content,
666                                         $cache_images_qpart
667                                         include_in_digest = $include_in_digest,
668                                         always_display_enclosures = $always_display_enclosures,
669                                         update_method = '$update_method'
670                                         WHERE id = '$feed_id' AND owner_uid = " . $_SESSION["uid"]);
671
672                         } else if ($subop == "batchEditSave") {
673                                 $feed_data = array();
674
675                                 foreach (array_keys($_POST) as $k) {
676                                         if ($k != "op" && $k != "subop" && $k != "ids") {
677                                                 $feed_data[$k] = $_POST[$k];
678                                         }
679                                 }
680
681                                 db_query($link, "BEGIN");
682
683                                 foreach (array_keys($feed_data) as $k) {
684
685                                         $qpart = "";
686
687                                         switch ($k) {
688                                                 case "title":                                                   
689                                                         $qpart = "title = '$feed_title'";
690                                                         break;
691
692                                                 case "feed_url":
693                                                         $qpart = "feed_url = '$feed_link'";
694                                                         break;
695
696                                                 case "update_interval":
697                                                         $qpart = "update_interval = '$upd_intl'";
698                                                         break;
699
700                                                 case "purge_interval":
701                                                         $qpart = "purge_interval = '$purge_intl'";
702                                                         break;
703
704                                                 case "auth_login":
705                                                         $qpart = "auth_login = '$auth_login'";
706                                                         break;
707
708                                                 case "auth_pass":
709                                                         $qpart = "auth_pass = '$auth_pass'";
710                                                         break;
711
712                                                 case "private":
713                                                         $qpart = "private = '$private'";
714                                                         break;
715
716                                                 case "include_in_digest":
717                                                         $qpart = "include_in_digest = '$include_in_digest'";
718                                                         break;
719
720                                                 case "always_display_enclosures":
721                                                         $qpart = "always_display_enclosures = '$always_display_enclosures'";
722                                                         break;
723
724                                                 case "cache_images":
725                                                         $qpart = "cache_images = '$cache_images'";
726                                                         break;
727
728                                                 case "rtl_content":
729                                                         $qpart = "rtl_content = '$rtl_content'";
730                                                         break;
731
732                                                 case "update_method":
733                                                         $qpart = "update_method = '$update_method'";
734                                                         break;
735
736                                                 case "cat_id":
737                                                         $qpart = $category_qpart_nocomma;
738                                                         break;
739
740                                         }
741
742                                         if ($qpart) {
743                                                 db_query($link,
744                                                         "UPDATE ttrss_feeds SET $qpart WHERE id IN ($feed_ids)
745                                                         AND owner_uid = " . $_SESSION["uid"]);
746                                                 print "<br/>";
747                                         }
748                                 }
749
750                                 db_query($link, "COMMIT");
751                         }
752
753                 }
754
755                 if ($subop == "remove") {
756
757                         $ids = split(",", db_escape_string($_REQUEST["ids"]));
758
759                         foreach ($ids as $id) {
760                                 remove_feed($link, $id, $_SESSION["uid"]);
761                         }
762
763                         return;
764                 }
765
766                 if ($subop == "clear") {
767                         $id = db_escape_string($_REQUEST["id"]);
768                         clear_feed_articles($link, $id);
769                 }
770
771                 if ($subop == "rescore") {
772                         $ids = split(",", db_escape_string($_REQUEST["ids"]));
773
774                         foreach ($ids as $id) {
775
776                                 $filters = load_filters($link, $id, $_SESSION["uid"], 6);
777
778                                 $result = db_query($link, "SELECT 
779                                         title, content, link, ref_id, author,".
780                                         SUBSTRING_FOR_DATE."(updated, 1, 19) AS updated
781                                         FROM
782                                                 ttrss_user_entries, ttrss_entries 
783                                                 WHERE ref_id = id AND feed_id = '$id' AND 
784                                                         owner_uid = " .$_SESSION['uid']."
785                                                 ");
786
787                                 $scores = array();
788
789                                 while ($line = db_fetch_assoc($result)) {
790
791                                         $tags = get_article_tags($link, $line["ref_id"]);
792
793                                         $article_filters = get_article_filters($filters, $line['title'], 
794                                                 $line['content'], $line['link'], strtotime($line['updated']), 
795                                                 $line['author'], $tags);
796                                         
797                                         $new_score = calculate_article_score($article_filters);
798
799                                         if (!$scores[$new_score]) $scores[$new_score] = array();
800
801                                         array_push($scores[$new_score], $line['ref_id']);
802                                 }
803
804                                 foreach (array_keys($scores) as $s) {
805                                         if ($s > 1000) {
806                                                 db_query($link, "UPDATE ttrss_user_entries SET score = '$s', 
807                                                         marked = true WHERE
808                                                         ref_id IN (" . join(',', $scores[$s]) . ")");
809                                         } else if ($s < -500) {
810                                                 db_query($link, "UPDATE ttrss_user_entries SET score = '$s', 
811                                                         unread = false WHERE
812                                                         ref_id IN (" . join(',', $scores[$s]) . ")");
813                                         } else {
814                                                 db_query($link, "UPDATE ttrss_user_entries SET score = '$s' WHERE
815                                                         ref_id IN (" . join(',', $scores[$s]) . ")");
816                                         }
817                                 }
818                         }
819
820                         print __("All done.");
821
822                 }
823
824                 if ($subop == "rescoreAll") {
825
826                         $result = db_query($link, 
827                                 "SELECT id FROM ttrss_feeds WHERE owner_uid = " . $_SESSION['uid']);
828
829                         while ($feed_line = db_fetch_assoc($result)) {
830
831                                 $id = $feed_line["id"];
832
833                                 $filters = load_filters($link, $id, $_SESSION["uid"], 6);
834
835                                 $tmp_result = db_query($link, "SELECT 
836                                         title, content, link, ref_id, author,".
837                                                 SUBSTRING_FOR_DATE."(updated, 1, 19) AS updated
838                                                 FROM
839                                                 ttrss_user_entries, ttrss_entries 
840                                                 WHERE ref_id = id AND feed_id = '$id' AND 
841                                                         owner_uid = " .$_SESSION['uid']."
842                                                 ");
843
844                                 $scores = array();
845
846                                 while ($line = db_fetch_assoc($tmp_result)) {
847
848                                         $tags = get_article_tags($link, $line["ref_id"]);
849
850                                         $article_filters = get_article_filters($filters, $line['title'], 
851                                                 $line['content'], $line['link'], strtotime($line['updated']), 
852                                                 $line['author'], $tags);
853
854                                         $new_score = calculate_article_score($article_filters);
855
856                                         if (!$scores[$new_score]) $scores[$new_score] = array();
857
858                                         array_push($scores[$new_score], $line['ref_id']);
859                                 }
860
861                                 foreach (array_keys($scores) as $s) {
862                                         if ($s > 1000) {
863                                                 db_query($link, "UPDATE ttrss_user_entries SET score = '$s', 
864                                                         marked = true WHERE
865                                                         ref_id IN (" . join(',', $scores[$s]) . ")");
866                                         } else {
867                                                 db_query($link, "UPDATE ttrss_user_entries SET score = '$s' WHERE
868                                                         ref_id IN (" . join(',', $scores[$s]) . ")");
869                                         }
870                                 }
871                         }
872
873                         print __("All done.");
874
875                 }
876
877                 if ($subop == "add") {
878
879                         $feed_url = db_escape_string(trim($_REQUEST["feed_url"]));
880                         $cat_id = db_escape_string($_REQUEST["cat_id"]);
881                         $p_from = db_escape_string($_REQUEST["from"]);
882
883                         /* only read authentication information from POST */
884
885                         $auth_login = db_escape_string(trim($_POST["auth_login"]));
886                         $auth_pass = db_escape_string(trim($_POST["auth_pass"]));
887
888                         if ($p_from != 'tt-rss') {
889                                 print "<html>
890                                         <head>
891                                                 <title>Tiny Tiny RSS</title>
892                                                 <link rel=\"stylesheet\" type=\"text/css\" href=\"utility.css\">
893                                         </head>
894                                         <body>
895                                         <img class=\"floatingLogo\" src=\"images/ttrss_logo.png\"
896                                                 alt=\"Tiny Tiny RSS\"/> 
897                                         <h1>Subscribe to feed...</h1>";
898                         }
899
900                         $rc = subscribe_to_feed($link, $feed_url, $cat_id, $auth_login, $auth_pass);
901
902                         switch ($rc) {
903                         case 1: 
904                                 print_notice(T_sprintf("Subscribed to <b>%s</b>.", $feed_url));
905                                 break;
906                         case 2:
907                                 print_error(T_sprintf("Could not subscribe to <b>%s</b>.", $feed_url));
908                                 break;
909                         case 0:
910                                 print_warning(T_sprintf("Already subscribed to <b>%s</b>.", $feed_url));
911                                 break;
912                         }
913
914                         if ($p_from != 'tt-rss') {
915                                 $tt_uri = ($_SERVER['HTTPS'] != "on" ? 'http://' : 'https://') . $_SERVER['HTTP_HOST'] . preg_replace('/backend\.php.*$/', 'tt-rss.php', $_SERVER["REQUEST_URI"]);
916
917
918                                 $tp_uri = ($_SERVER['HTTPS'] != "on" ? 'http://' : 'https://') . $_SERVER['HTTP_HOST'] . preg_replace('/backend\.php.*$/', 'prefs.php', $_SERVER["REQUEST_URI"]);
919
920                                 $result = db_query($link, "SELECT id FROM ttrss_feeds WHERE
921                                         feed_url = '$feed_url' AND owner_uid = " . $_SESSION["uid"]);
922
923                                 $feed_id = db_fetch_result($result, 0, "id");
924
925                                 print "<p>";
926
927                                 if ($feed_id) {
928                                         print "<form method=\"GET\" style='display: inline' 
929                                                 action=\"$tp_uri\">
930                                                 <input type=\"hidden\" name=\"tab\" value=\"feedConfig\">
931                                                 <input type=\"hidden\" name=\"subop\" value=\"editFeed\">
932                                                 <input type=\"hidden\" name=\"subopparam\" value=\"$feed_id\">
933                                                 <input type=\"submit\" value=\"".__("Edit subscription options")."\">
934                                                 </form>";
935                                 }
936
937                                 print "<form style='display: inline' method=\"GET\" action=\"$tt_uri\">
938                                         <input type=\"submit\" value=\"".__("Return to Tiny Tiny RSS")."\">
939                                         </form></p>";
940
941                                 print "</body></html>";
942                                 return;
943                         }
944                 }
945
946                 if ($subop == "categorize") {
947
948
949                         $ids = split(",", db_escape_string($_REQUEST["ids"]));
950
951                         $cat_id = db_escape_string($_REQUEST["cat_id"]);
952
953                         if ($cat_id == 0) {
954                                 $cat_id_qpart = 'NULL';
955                         } else {
956                                 $cat_id_qpart = "'$cat_id'";
957                         }
958
959                         db_query($link, "BEGIN");
960
961                         foreach ($ids as $id) {
962                         
963                                 db_query($link, "UPDATE ttrss_feeds SET cat_id = $cat_id_qpart
964                                         WHERE id = '$id'
965                                         AND owner_uid = " . $_SESSION["uid"]);
966
967                         }
968
969                         db_query($link, "COMMIT");
970
971                 }
972
973                 if ($subop == "editCats") {
974
975                         $action = $_REQUEST["action"];
976
977                         if ($action == "save") {
978
979                                 $cat_title = db_escape_string(trim($_REQUEST["value"]));
980                                 $cat_id = db_escape_string($_REQUEST["cid"]);
981
982                                 db_query($link, "BEGIN");
983
984                                 $result = db_query($link, "SELECT title FROM ttrss_feed_categories
985                                         WHERE id = '$cat_id' AND owner_uid = ".$_SESSION["uid"]);
986
987                                 if (db_num_rows($result) == 1) {
988
989                                         $old_title = db_fetch_result($result, 0, "title");
990                                         
991                                         if ($cat_title != "") {
992                                                 $result = db_query($link, "UPDATE ttrss_feed_categories SET
993                                                         title = '$cat_title' WHERE id = '$cat_id' AND 
994                                                         owner_uid = ".$_SESSION["uid"]);
995
996                                                 print $cat_title;
997                                         } else {
998                                                 print $old_title;
999                                         }
1000                                 } else {
1001                                         print $_REQUEST["value"];
1002                                 }
1003
1004                                 db_query($link, "COMMIT");
1005
1006                                 return;
1007
1008                         }
1009
1010                         header("Content-Type: text/xml");
1011                         print "<dlg id=\"$subop\">";
1012                         print "<title>".__('Category editor')."</title>";
1013                         print "<content><![CDATA[";
1014
1015                         if ($action == "add") {
1016
1017                                 $feed_cat = db_escape_string(trim($_REQUEST["cat"]));
1018
1019                                 if (!add_feed_category($link, $feed_cat))
1020                                         print_warning(T_sprintf("Category <b>$%s</b> already exists in the database.", $feed_cat));
1021
1022                         }
1023
1024                         if ($action == "remove") {
1025         
1026                                 $ids = split(",", db_escape_string($_REQUEST["ids"]));
1027         
1028                                 foreach ($ids as $id) {
1029                                         remove_feed_category($link, $id, $_SESSION["uid"]);
1030                                 }
1031                         }
1032
1033                         print "<div>
1034                                 <input id=\"fadd_cat\" 
1035                                         onkeypress=\"return filterCR(event, addFeedCat)\"
1036                                         size=\"40\">
1037                                         <button onclick=\"addFeedCat()\">".
1038                                         __('Create category')."</button></div>";
1039         
1040                         $result = db_query($link, "SELECT title,id FROM ttrss_feed_categories
1041                                 WHERE owner_uid = ".$_SESSION["uid"]."
1042                                 ORDER BY title");
1043
1044                         print "<p>";
1045
1046                         if (db_num_rows($result) != 0) {
1047
1048                                 print   __('Select:')." 
1049                                         <a href=\"#\" onclick=\"selectTableRows('prefFeedCatList', 'all')\">".__('All')."</a>,
1050                                         <a href=\"#\" onclick=\"selectTableRows('prefFeedCatList', 'none')\">".__('None')."</a>";
1051
1052                                 print "<div class=\"prefFeedCatHolder\">";
1053
1054                                 print "<form id=\"feed_cat_edit_form\" onsubmit=\"return false\">";
1055
1056                                 print "<table width=\"100%\" class=\"prefFeedCatList\" 
1057                                         cellspacing=\"0\" id=\"prefFeedCatList\">";
1058                                                 
1059                                 $lnum = 0;
1060                                 
1061                                 while ($line = db_fetch_assoc($result)) {
1062                 
1063                                         $class = ($lnum % 2) ? "even" : "odd";
1064                 
1065                                         $cat_id = $line["id"];
1066                                         $this_row_id = "id=\"FCATR-$cat_id\"";
1067                 
1068                                         print "<tr class=\"$class\" $this_row_id>";
1069                 
1070                                         $edit_title = htmlspecialchars($line["title"]);
1071                 
1072                                         print "<td width='5%' align='center'><input 
1073                                                 onclick='toggleSelectRow(this);' 
1074                                                 type=\"checkbox\" id=\"FCCHK-$cat_id\"></td>";
1075         
1076                                         print "<td><span id=\"FCATT-$cat_id\">" . 
1077                                                 $edit_title . "</span></td>";           
1078                                         
1079                                         print "</tr>";
1080                 
1081                                         ++$lnum;
1082                                 }
1083         
1084                                 print "</table>";
1085
1086                                 print "</form>";
1087
1088                                 print "</div>";
1089
1090                         } else {
1091                                 print "<p>".__('No feed categories defined.')."</p>";
1092                         }
1093
1094                         print "<div class='dlgButtons'>
1095                                 <div style='float : left'>
1096                                 <button onclick=\"return removeSelectedFeedCats()\">".
1097                                 __('Remove')."</button>
1098                                 </div>";
1099
1100                         print "<button onclick=\"selectTab('feedConfig')\">".
1101                                 __('Close this window')."</button></div>";
1102
1103                         print "]]></content></dlg>";
1104
1105                         return;
1106
1107                 }
1108
1109                 if ($quiet) return;
1110
1111                 print "<div dojoType=\"dijit.layout.AccordionContainer\" region=\"center\">";
1112                 print "<div dojoType=\"dijit.layout.AccordionPane\" title=\"".__('Feeds')."\">";
1113
1114                 set_pref($link, "_PREFS_ACTIVE_TAB", "feedConfig");
1115
1116                 $result = db_query($link, "SELECT COUNT(id) AS num_errors
1117                         FROM ttrss_feeds WHERE last_error != '' AND owner_uid = ".$_SESSION["uid"]);
1118
1119                 $num_errors = db_fetch_result($result, 0, "num_errors");
1120
1121                 if ($num_errors > 0) {
1122
1123                         $error_button = "<button dojoType=\"dijit.form.Button\"
1124                                         onclick=\"showFeedsWithErrors\" id=\"errorButton\">" .
1125                                 __("Feeds with errors") . "</button>";
1126
1127 //                      print format_notice("<a href=\"javascript:showFeedsWithErrors()\">".
1128 //                              __('Some feeds have update errors (click for details)')."</a>");
1129                 }
1130
1131                 $feed_search = db_escape_string($_REQUEST["search"]);
1132
1133                 if (array_key_exists("search", $_REQUEST)) {
1134                         $_SESSION["prefs_feed_search"] = $feed_search;
1135                 } else {
1136                         $feed_search = $_SESSION["prefs_feed_search"];
1137                 }
1138
1139                 print "<div dojoType=\"dijit.Toolbar\">";
1140
1141                 /* 
1142                 print "<div style='float : right'> 
1143                         <input id=\"feed_search\" size=\"20\" type=\"search\"
1144                                 onfocus=\"disableHotkeys();\" 
1145                                 onblur=\"enableHotkeys();\"
1146                                 onchange=\"updateFeedList()\" value=\"$feed_search\">
1147                         <button onclick=\"updateFeedList()\">".
1148                                 __('Search')."</button>
1149                                 </div>"; */
1150
1151                 print "<div dojoType=\"dijit.form.DropDownButton\">".
1152                                 "<span>" . __('Select')."</span>";
1153                 print "<div dojoType=\"dijit.Menu\" style=\"display: none;\">";
1154                 print "<div onclick=\"dijit.byId('feedTree').model.setAllChecked(true)\" 
1155                         dojoType=\"dijit.MenuItem\">".__('All')."</div>";
1156                 print "<div onclick=\"dijit.byId('feedTree').model.setAllChecked(false)\" 
1157                         dojoType=\"dijit.MenuItem\">".__('None')."</div>";
1158                 print "</div></div>";
1159
1160                 print "<div dojoType=\"dijit.form.DropDownButton\">".
1161                                 "<span>" . __('Feeds')."</span>";
1162                 print "<div dojoType=\"dijit.Menu\" style=\"display: none;\">";
1163                 print "<div onclick=\"quickAddFeed()\" 
1164                         dojoType=\"dijit.MenuItem\">".__('Subscribe to feed')."</div>";
1165                 print "<div onclick=\"editSelectedFeed()\" 
1166                         dojoType=\"dijit.MenuItem\">".__('Edit feeds')."</div>";
1167                 print "<div onclick=\"resetFeedOrder()\" 
1168                         dojoType=\"dijit.MenuItem\">".__('Reset sort order')."</div>";
1169                 print "</div></div>";
1170
1171                 if (get_pref($link, 'ENABLE_FEED_CATS')) {
1172                         print "<div dojoType=\"dijit.form.DropDownButton\">".
1173                                         "<span>" . __('Categories')."</span>";
1174                         print "<div dojoType=\"dijit.Menu\" style=\"display: none;\">";
1175                         print "<div onclick=\"editFeedCats()\" 
1176                                 dojoType=\"dijit.MenuItem\">".__('Edit categories')."</div>";
1177                         print "<div onclick=\"resetCatOrder()\" 
1178                                 dojoType=\"dijit.MenuItem\">".__('Reset sort order')."</div>";
1179                         print "</div></div>";
1180
1181                 }
1182
1183                 print $error_button;
1184
1185                 print "<button dojoType=\"dijit.form.Button\" onclick=\"removeSelectedFeeds()\">"
1186                         .__('Unsubscribe')."</button dojoType=\"dijit.form.Button\"> ";
1187
1188                 if (defined('_ENABLE_FEED_DEBUGGING')) {
1189
1190                         print "<select id=\"feedActionChooser\" onchange=\"feedActionChange()\">
1191                                 <option value=\"facDefault\" selected>".__('More actions...')."</option>";
1192         
1193                         if (FORCE_ARTICLE_PURGE == 0) {
1194                                 print 
1195                                         "<option value=\"facPurge\">".__('Manual purge')."</option>";
1196                         }
1197         
1198                         print "
1199                                 <option value=\"facClear\">".__('Clear feed data')."</option>
1200                                 <option value=\"facRescore\">".__('Rescore articles')."</option>";
1201         
1202                         print "</select>";
1203
1204                 }
1205
1206                 print "</div>"; # toolbar
1207
1208                 print "<div id=\"feedlistLoading\">
1209                 <img src='images/indicator_tiny.gif'>".
1210                  __("Loading, please wait...")."</div>";
1211
1212                 print "<div dojoType=\"fox.PrefFeedStore\" jsId=\"feedStore\" 
1213                         url=\"backend.php?op=pref-feeds&subop=getfeedtree\">
1214                 </div>
1215                 <div dojoType=\"lib.CheckBoxStoreModel\" jsId=\"feedModel\" store=\"feedStore\"
1216                 query=\"{id:'root'}\" rootId=\"root\" rootLabel=\"Feeds\"
1217                         childrenAttrs=\"items\" checkboxStrict=\"false\" checkboxAll=\"false\">
1218                 </div>
1219                 <div dojoType=\"fox.PrefFeedTree\" id=\"feedTree\" 
1220                         dndController=\"dijit.tree.dndSource\" 
1221                         betweenThreshold=\"5\"
1222                         model=\"feedModel\" openOnClick=\"false\">
1223                 <script type=\"dojo/method\" event=\"onClick\" args=\"item\">
1224                         var id = String(item.id);
1225                         var bare_id = id.substr(id.indexOf(':')+1);
1226
1227                         if (id.match('FEED:')) {
1228                                 editFeed(bare_id, event);
1229                         } else if (id.match('CAT:')) {
1230                                 editCat(bare_id, item, event);
1231                         }                       
1232                 </script>
1233                 <script type=\"dojo/method\" event=\"onLoad\" args=\"item\">
1234                         Element.hide(\"feedlistLoading\");
1235                 </script>
1236                 </div>";
1237
1238                 /*
1239                 $feeds_sort = db_escape_string($_REQUEST["sort"]);
1240
1241                 if (!$feeds_sort || $feeds_sort == "undefined") {
1242                         $feeds_sort = $_SESSION["pref_sort_feeds"];                     
1243                         if (!$feeds_sort) $feeds_sort = "title";
1244                 }
1245
1246                 $_SESSION["pref_sort_feeds"] = $feeds_sort;
1247
1248                 if ($feed_search) {
1249
1250                         $feed_search = split(" ", $feed_search);
1251                         $tokens = array();
1252
1253                         foreach ($feed_search as $token) {
1254
1255                                 $token = trim($token);
1256
1257                                 array_push($tokens, "(UPPER(F1.title) LIKE UPPER('%$token%') OR
1258                                         UPPER(C1.title) LIKE UPPER('%$token%') OR
1259                                         UPPER(F1.feed_url) LIKE UPPER('%$token%'))");
1260                         }
1261
1262                         $search_qpart = "(" . join($tokens, " AND ") . ") AND ";
1263
1264                 } else {
1265                         $search_qpart = "";
1266                 }
1267
1268                 $show_last_article_info = false;
1269                 $show_last_article_checked = "";
1270                 $show_last_article_qpart = "";
1271
1272                 if ($_REQUEST["slat"] == "true") {
1273                         $show_last_article_info = true;
1274                         $show_last_article_checked = "checked";
1275                         $show_last_article_qpart = ", (SELECT ".SUBSTRING_FOR_DATE."(MAX(updated),1,16) FROM ttrss_user_entries,
1276                                 ttrss_entries WHERE ref_id = ttrss_entries.id
1277                                 AND feed_id = F1.id) AS last_article";
1278                 } else if ($feeds_sort == "last_article") {
1279                         $feeds_sort = "title";
1280                 }
1281
1282                 if (get_pref($link, 'ENABLE_FEED_CATS')) {
1283                         $order_by_qpart = "category,$feeds_sort,title";
1284                 } else {
1285                         $order_by_qpart = "$feeds_sort,title";
1286                 }
1287
1288                 $result = db_query($link, "SELECT 
1289                                 F1.id,
1290                                 F1.title,
1291                                 F1.feed_url,
1292                                 ".SUBSTRING_FOR_DATE."(F1.last_updated,1,16) AS last_updated,
1293                                 F1.update_interval,
1294                                 F1.last_error,
1295                                 F1.purge_interval,
1296                                 F1.cat_id,
1297                                 C1.title AS category,
1298                                 F1.include_in_digest
1299                                 $show_last_article_qpart
1300                         FROM 
1301                                 ttrss_feeds AS F1 
1302                                 LEFT JOIN ttrss_feed_categories AS C1
1303                                         ON (F1.cat_id = C1.id)
1304                         WHERE 
1305                                 $search_qpart F1.owner_uid = '".$_SESSION["uid"]."'                     
1306                         ORDER by $order_by_qpart");
1307
1308                 if (db_num_rows($result) != 0) {
1309
1310                         print "<p><table width=\"100%\" cellspacing=\"0\" 
1311                                 class=\"prefFeedList\" id=\"prefFeedList\">";
1312                         print "<tr><td class=\"selectPrompt\" colspan=\"8\">".
1313                                 "<div style='float : right'>".
1314                                 "<input id='show_last_article_times' type='checkbox' onchange='feedlistToggleSLAT()'
1315                                 $show_last_article_checked><label 
1316                                         for='show_last_article_times'>".__('Show last article times')."</label></div>".
1317                                 __('Select:')."
1318                                         <a href=\"#\" onclick=\"selectTableRows('prefFeedList', 'all')\">".__('All')."</a>,
1319                                         <a href=\"#\" onclick=\"selectTableRows('prefFeedList', 'none')\">".__('None')."</a>
1320                                 </td</tr>";
1321
1322                         if (!get_pref($link, 'ENABLE_FEED_CATS')) {
1323                                 print "<tr class=\"title\">
1324                                         <td width='5%' align='center'>&nbsp;</td>";
1325
1326                                 print "<td width='3%'>&nbsp;</td>";
1327
1328                                 print "<td width='60%'><a href=\"#\" onclick=\"updateFeedList('title')\">".__('Title')."</a></td>";
1329
1330                                 if ($show_last_article_info) {
1331                                         print "<td width='20%' align='right'><a href=\"#\" onclick=\"updateFeedList('last_article')\">".__('Last&nbsp;Article')."</a></td>";
1332                                 }
1333
1334                                 print "<td width='20%' align='right'><a href=\"#\" onclick=\"updateFeedList('last_updated')\">".__('Updated')."</a></td>";
1335                         }
1336                         
1337                         $lnum = 0;
1338
1339                         $cur_cat_id = -1;
1340                         
1341                         while ($line = db_fetch_assoc($result)) {
1342         
1343                                 $feed_id = $line["id"];
1344                                 $cat_id = $line["cat_id"];
1345
1346                                 $edit_title = htmlspecialchars($line["title"]);
1347                                 $edit_cat = htmlspecialchars($line["category"]);
1348
1349                                 $last_error = $line["last_error"];
1350
1351                                 if (!$edit_cat) $edit_cat = __("Uncategorized");
1352
1353                                 $last_updated = $line["last_updated"];
1354
1355                                 if (!$last_updated) {
1356                                         $last_updated = "&mdash;";
1357                                 } else {
1358                                         $last_updated = make_local_datetime($link, $last_updated, false);
1359                                 }
1360
1361                                 $last_article = $line["last_article"];
1362
1363                                 if (!$last_article) {
1364                                         $last_article = "&mdash;";      
1365                                 } else {
1366                                         $last_article = make_local_datetime($link, $last_article, false);
1367                                 }
1368
1369                                 if (get_pref($link, 'ENABLE_FEED_CATS') && $cur_cat_id != $cat_id) {
1370                                         $lnum = 0;
1371                                 
1372                                         print "<tr><td colspan=\"6\" class=\"feedEditCat\">$edit_cat</td></tr>";
1373
1374                                         print "<tr class=\"title\">
1375                                                 <td width='5%'>&nbsp;</td>";
1376
1377                                         print "<td width='3%'>&nbsp;</td>";
1378
1379                                         print "<td width='60%'><a href=\"#\" onclick=\"updateFeedList('title')\">".__('Title')."</a></td>";
1380
1381                                         if ($show_last_article_info) {
1382                                                 print "<td width='20%' align='right'>
1383                                                         <a href=\"#\" onclick=\"updateFeedList('last_article')\">".__('Last&nbsp;Article')."</a></td>";
1384                                         }
1385
1386                                         print "<td width='20%' align='right'>
1387                                                 <a href=\"#\" onclick=\"updateFeedList('last_updated')\">".__('Updated')."</a></td>";
1388
1389                                         $cur_cat_id = $cat_id;
1390                                 }
1391
1392                                 $class = ($lnum % 2) ? "even" : "odd";
1393                                 $this_row_id = "id=\"FEEDR-$feed_id\"";
1394
1395                                 print "<tr class=\"$class\" $this_row_id>";
1396         
1397                                 $icon_file = ICONS_DIR . "/$feed_id.ico";
1398         
1399                                 if (file_exists($icon_file) && filesize($icon_file) > 0) {
1400                                                 $feed_icon = "<img class=\"tinyFeedIcon\"       src=\"" . ICONS_URL . "/$feed_id.ico\">";
1401                                 } else {
1402                                         $feed_icon = "<img class=\"tinyFeedIcon\" src=\"images/blank_icon.gif\">";
1403                                 }
1404                                 
1405                                 print "<td class='feedSelect'><input 
1406                                 onclick='toggleSelectRowById(this, \"FEEDR-".$line['id']."\");' 
1407                                 type=\"checkbox\" id=\"FRCHK-".$line["id"]."\"></td>";
1408
1409                                 $onclick = "onclick='editFeed($feed_id, event)' title='".__('Click to edit')."'";
1410
1411                                 print "<td $onclick class='feedIcon'>$feed_icon</td>";          
1412
1413                                 if ($last_error) {
1414                                         $edit_title = "<span class=\"feed_error\">$edit_title</span>";
1415                                         $last_updated = "<span class=\"feed_error\">$last_updated</span>";
1416                                         $last_article = "<span class=\"feed_error\">$last_article</span>";
1417                                 }
1418
1419                                 print "<td $onclick>" . $edit_title . "</td>";
1420
1421                                 if ($show_last_article_info) {
1422                                         print "<td align='right' $onclick>" . 
1423                                                 "$last_article</td>";
1424                                 }
1425
1426                                 print "<td $onclick align='right'>$last_updated</td>";
1427
1428                                 print "</tr>";
1429         
1430                                 ++$lnum;
1431                         }
1432         
1433                         print "</table>";
1434
1435                         print "<p>";
1436
1437                 } else {
1438
1439                         print "<p>";
1440
1441                         if (!$feed_search) { 
1442                                 print_warning(__("You don't have any subscribed feeds."));
1443                         } else {
1444                                 print_warning(__('No matching feeds found.'));
1445                         }
1446                         print "</p>";
1447
1448                 } */
1449
1450                 print "</div>"; # feeds pane
1451
1452                 print "<div dojoType=\"dijit.layout.AccordionPane\" title=\"".__('OPML')."\">";
1453
1454                 print "<p>" . __("Using OPML you can export and import your feeds and Tiny Tiny RSS settings.");
1455
1456                 print "<div class=\"insensitive\">" . __("Note: Only main settings profile can be migrated using OPML.") . "</div>";
1457
1458                 print "</p>";
1459
1460                 print "<iframe name=\"upload_iframe\"
1461                         style=\"width: 400px; height: 100px; display: none;\"></iframe>";
1462
1463                 print "<div style='float : left'>";
1464                 print "<form style='display : block' target=\"upload_iframe\"
1465                         enctype=\"multipart/form-data\" method=\"POST\" 
1466                                 action=\"backend.php\">
1467                         <input id=\"opml_file\" name=\"opml_file\" type=\"file\">&nbsp;
1468                         <input type=\"hidden\" name=\"op\" value=\"dlg\">
1469                         <input type=\"hidden\" name=\"id\" value=\"importOpml\">
1470                         <button onclick=\"return opmlImport();\"
1471                                 type=\"submit\">".__('Import')."</button>
1472                         </form>";
1473                 print "</div>&nbsp;";
1474
1475                 print "<button onclick=\"gotoExportOpml()\">".__('Export OPML')."</button>";
1476
1477                 print "<p>".__('Your OPML can be published publicly and can be subscribed by anyone who knows the URL below.');
1478
1479                 print "<div class=\"insensitive\">" . __("Note: Published OPML does not include your Tiny Tiny RSS settings, feeds that require authentication or feeds hidden from Popular feeds.") .                  "</div>" . "</p>";
1480
1481                 print "<button onclick=\"return displayDlg('pubOPMLUrl')\">".
1482                         __('Display URL')."</button> ";
1483
1484
1485                 print "</div>"; # pane
1486
1487                 if (strpos($_SERVER['HTTP_USER_AGENT'], "Firefox") !== false) {
1488
1489                         print "<div dojoType=\"dijit.layout.AccordionPane\" title=\"".__('Firefox integration')."\">";
1490                 
1491                         print "<p>" . __('This Tiny Tiny RSS site can be used as a Firefox Feed Reader by clicking the link below.') . "</p>";
1492
1493                         print "<p>";
1494
1495                         print "<button onclick='window.navigator.registerContentHandler(" .
1496                       "\"application/vnd.mozilla.maybe.feed\", " .
1497                       "\"" . add_feed_url() . "\", " . " \"Tiny Tiny RSS\")'>" .
1498                                                          __('Click here to register this site as a feed reader.') . 
1499                                 "</button>";
1500
1501                         print "</p>";
1502
1503                         print "</div>"; # pane
1504                 }
1505
1506                 print "<div dojoType=\"dijit.layout.AccordionPane\" title=\"".__('Subscribing using bookmarklet')."\">";
1507                 
1508                 print "<p>" . __("Drag the link below to your browser toolbar, open the feed you're interested in in your browser and click on the link to subscribe to it.") . "</p>";
1509
1510                 $bm_subscribe_url = str_replace('%s', '', add_feed_url());
1511
1512                 $confirm_str = __('Subscribe to %s in Tiny Tiny RSS?');
1513
1514                 $bm_url = htmlspecialchars("javascript:{if(confirm('$confirm_str'.replace('%s',window.location.href)))window.location.href='$bm_subscribe_url'+window.location.href}");
1515
1516                 print "<a href=\"$bm_url\" class='visibleLink'>" . __('Subscribe in Tiny Tiny RSS'). "</a>";
1517
1518                 print "</div>"; #pane
1519
1520                 print "<div dojoType=\"dijit.layout.AccordionPane\" title=\"".__('Published articles and generated feeds')."\">";
1521
1522                 print "<p>".__('Published articles are exported as a public RSS feed and can be subscribed by anyone who knows the URL specified below.')."</p>";
1523
1524                 $rss_url = '-2::' . htmlspecialchars(get_self_url_prefix() . 
1525                                 "/backend.php?op=rss&id=-2&view-mode=all_articles");;
1526
1527                 print "<button onclick=\"return displayDlg('generatedFeed', '$rss_url')\">".
1528                         __('Display URL')."</button> ";
1529                
1530                 print "<button onclick=\"return clearFeedAccessKeys()\">".
1531                         __('Clear all generated URLs')."</button> ";
1532
1533                 print "</div>"; #pane
1534                 print "</div>"; #container
1535
1536         }
1537
1538         function print_feed_browser($link, $search, $limit, $mode = 1) {
1539
1540                         $owner_uid = $_SESSION["uid"];
1541
1542                         if ($search) {
1543                                 $search_qpart = "AND (UPPER(feed_url) LIKE UPPER('%$search%') OR 
1544                                         UPPER(title) LIKE UPPER('%$search%'))";
1545                         } else {
1546                                 $search_qpart = "";
1547                         }
1548
1549                         if ($mode == 1) {
1550                                 $result = db_query($link, "SELECT feed_url, subscribers FROM
1551                                         ttrss_feedbrowser_cache WHERE (SELECT COUNT(id) = 0 FROM ttrss_feeds AS tf
1552                                         WHERE tf.feed_url = ttrss_feedbrowser_cache.feed_url
1553                                         AND owner_uid = '$owner_uid') $search_qpart 
1554                                         ORDER BY subscribers DESC LIMIT $limit");
1555                         } else if ($mode == 2) {
1556                                 $result = db_query($link, "SELECT *,
1557                                         (SELECT COUNT(*) FROM ttrss_user_entries WHERE
1558                                                 orig_feed_id = ttrss_archived_feeds.id) AS articles_archived
1559                                         FROM
1560                                                 ttrss_archived_feeds
1561                                         WHERE 
1562                                         (SELECT COUNT(*) FROM ttrss_feeds 
1563                                                 WHERE ttrss_feeds.feed_url = ttrss_archived_feeds.feed_url AND
1564                                                         owner_uid = '$owner_uid') = 0   AND                                     
1565                                         owner_uid = '$owner_uid' $search_qpart 
1566                                         ORDER BY id DESC LIMIT $limit");
1567                         }
1568
1569                         $feedctr = 0;
1570                         
1571                         while ($line = db_fetch_assoc($result)) {
1572
1573                                 if ($mode == 1) {
1574
1575                                         $feed_url = $line["feed_url"];
1576                                         $subscribers = $line["subscribers"];
1577
1578                                         $det_result = db_query($link, "SELECT site_url,title,id 
1579                                                 FROM ttrss_feeds WHERE feed_url = '$feed_url' LIMIT 1");
1580         
1581                                         $details = db_fetch_assoc($det_result);
1582                                 
1583                                         $icon_file = ICONS_DIR . "/" . $details["id"] . ".ico";
1584         
1585                                         if (file_exists($icon_file) && filesize($icon_file) > 0) {
1586                                                         $feed_icon = "<img class=\"tinyFeedIcon\"       src=\"" . ICONS_URL . 
1587                                                                 "/".$details["id"].".ico\">";
1588                                         } else {
1589                                                 $feed_icon = "<img class=\"tinyFeedIcon\" src=\"images/blank_icon.gif\">";
1590                                         }
1591         
1592                                         $check_box = "<input onclick='toggleSelectListRow(this)' 
1593                                                 class='feedBrowseCB' 
1594                                                 type=\"checkbox\" id=\"FBCHK-" . $details["id"] . "\">";
1595         
1596                                         $class = ($feedctr % 2) ? "even" : "odd";
1597         
1598                                         $feed_url = htmlspecialchars($line["feed_url"]);
1599
1600                                         if ($details["site_url"]) {
1601                                                 $site_url = "<a target=\"_blank\" href=\"".
1602                                                         htmlspecialchars($details["site_url"])."\">
1603                                                         <img style='border-width : 0px' src='images/www.png' alt='www'></a>";
1604                                         } else {
1605                                                 $site_url = "";
1606                                         }
1607
1608                                         $feed_url = "<a target=\"_blank\" href=\"$feed_url\"><img 
1609                                                 style='border-width : 0px; vertical-align : middle' 
1610                                                 src='images/feed-icon-12x12.png'></a>";
1611
1612                                         print "<li title=\"".htmlspecialchars($details["site_url"])."\" 
1613                                                 class='$class' id=\"FBROW-".$details["id"]."\">$check_box".
1614                                                 "$feed_icon $feed_url " . htmlspecialchars($details["title"]) . 
1615                                                 "&nbsp;<span class='subscribers'>($subscribers)</span>
1616                                                 $site_url</li>";
1617         
1618                                 } else if ($mode == 2) {
1619                                         $feed_url = htmlspecialchars($line["feed_url"]);
1620                                         $site_url = htmlspecialchars($line["site_url"]); 
1621                                         $title = htmlspecialchars($line["title"]);
1622
1623                                         $icon_file = ICONS_DIR . "/" . $line["id"] . ".ico";
1624         
1625                                         if (file_exists($icon_file) && filesize($icon_file) > 0) {
1626                                                         $feed_icon = "<img class=\"tinyFeedIcon\"       src=\"" . ICONS_URL . 
1627                                                                 "/".$line["id"].".ico\">";
1628                                         } else {
1629                                                 $feed_icon = "<img class=\"tinyFeedIcon\" src=\"images/blank_icon.gif\">";
1630                                         }
1631
1632                                         $check_box = "<input onclick='toggleSelectListRow(this)' class='feedBrowseCB' 
1633                                                 type=\"checkbox\" id=\"FBCHK-" . $line["id"] . "\">";
1634         
1635                                         $class = ($feedctr % 2) ? "even" : "odd";
1636
1637                                         if ($line['articles_archived'] > 0) {
1638                                                 $archived = sprintf(__("%d archived articles"), $line['articles_archived']);
1639                                                 $archived = "&nbsp;<span class='subscribers'>($archived)</span>";
1640                                         } else {
1641                                                 $archived = '';
1642                                         }
1643
1644                                         if ($line["site_url"]) {
1645                                                 $site_url = "<a target=\"_blank\" href=\"$site_url\">
1646                                                         <img style='border-width : 0px' src='images/www.png' alt='www'></a>";
1647                                         } else {
1648                                                 $site_url = "";
1649                                         }
1650
1651                                         $feed_url = "<a target=\"_blank\" href=\"$feed_url\"><img 
1652                                                 style='border-width : 0px; vertical-align : middle' 
1653                                                 src='images/feed-icon-12x12.png'></a>";
1654
1655                                         print "<li title='".$line['site_url']."' class='$class' 
1656                                                 id=\"FBROW-".$line["id"]."\">".
1657                                                 $check_box . "$feed_icon $feed_url " . $title . 
1658                                                 $archived . $site_url . "</li>";
1659
1660
1661                                 }
1662
1663                                 ++$feedctr;
1664                         }
1665
1666                         if ($feedctr == 0) {
1667                                 print "<li style=\"text-align : center\"><p>".__('No feeds found.')."</p></li>";
1668                         }
1669
1670                 return $feedctr;
1671
1672         }
1673
1674 ?>