]> git.wh0rd.org Git - tt-rss.git/blob - classes/pref/filters.php
simplify filter overview
[tt-rss.git] / classes / pref / filters.php
1 <?php
2 class Pref_Filters extends Handler_Protected {
3
4         function csrf_ignore($method) {
5                 $csrf_ignored = array("index", "getfiltertree", "edit", "newfilter", "newrule",
6                         "newaction");
7
8                 return array_search($method, $csrf_ignored) !== false;
9         }
10
11         function testFilter() {
12                 $filter = array();
13
14                 $filter["enabled"] = true;
15                 $filter["match_any_rule"] = sql_bool_to_bool(
16                         checkbox_to_sql_bool(db_escape_string($this->link, $_REQUEST["match_any_rule"])));
17                 $filter["inverse"] = sql_bool_to_bool(
18                         checkbox_to_sql_bool(db_escape_string($this->link, $_REQUEST["inverse"])));
19
20                 $filter["rules"] = array();
21
22                 $result = db_query($this->link, "SELECT id,name FROM ttrss_filter_types");
23
24                 $filter_types = array();
25                 while ($line = db_fetch_assoc($result)) {
26                         $filter_types[$line["id"]] = $line["name"];
27                 }
28
29                 $rctr = 0;
30                 foreach ($_REQUEST["rule"] AS $r) {
31                         $rule = json_decode($r, true);
32
33                         if ($rule && $rctr < 5) {
34                                 $rule["type"] = $filter_types[$rule["filter_type"]];
35                                 unset($rule["filter_type"]);
36
37                                 if (strpos($rule["feed_id"], "CAT:") === 0) {
38                                         $rule["cat_id"] = (int) substr($rule["feed_id"], 4);
39                                         unset($rule["feed_id"]);
40                                 }
41
42                                 array_push($filter["rules"], $rule);
43
44                                 ++$rctr;
45                         } else {
46                                 break;
47                         }
48                 }
49
50                 $feed_title = getFeedTitle($this->link, $feed);
51
52                 $qfh_ret = queryFeedHeadlines($this->link, -4, 30, "", false, false, false,
53                         "date_entered DESC", 0, $_SESSION["uid"], $filter);
54
55                 $result = $qfh_ret[0];
56
57                 $articles = array();
58                 $found = 0;
59
60                 print __("Articles matching this filter:");
61
62                 print "<div class=\"filterTestHolder\">";
63                 print "<table width=\"100%\" cellspacing=\"0\" id=\"prefErrorFeedList\">";
64
65                 while ($line = db_fetch_assoc($result)) {
66
67                         $entry_timestamp = strtotime($line["updated"]);
68                         $entry_tags = get_article_tags($this->link, $line["id"], $_SESSION["uid"]);
69
70                         $content_preview = truncate_string(
71                                 strip_tags($line["content_preview"]), 100, '...');
72
73                         if ($line["feed_title"])
74                                 $feed_title = $line["feed_title"];
75
76                         print "<tr>";
77
78                         print "<td width='5%' align='center'><input
79                                 dojoType=\"dijit.form.CheckBox\" checked=\"1\"
80                                 disabled=\"1\" type=\"checkbox\"></td>";
81                         print "<td>";
82
83                         print $line["title"];
84                         print "&nbsp;(";
85                         print "<b>" . $feed_title . "</b>";
86                         print "):&nbsp;";
87                         print "<span class=\"insensitive\">" . $content_preview . "</span>";
88                         print " " . mb_substr($line["date_entered"], 0, 16);
89
90                         print "</td></tr>";
91
92                         $found++;
93                 }
94
95                 if ($found == 0) {
96                         print "<tr><td align='center'>" .
97                                 __("No recent articles matching this filter have been found.");
98
99                         print "</td></tr><tr><td class='insensitive' align='center'>";
100
101                         print __("Complex expressions might not give results while testing due to issues with database server regexp implementation.");
102
103                         print "</td></tr>";
104
105                 }
106
107                 print "</table></div>";
108
109                 print "<div style='text-align : center'>";
110                 print "<button dojoType=\"dijit.form.Button\" onclick=\"dijit.byId('filterTestDlg').hide()\">".
111                         __('Close this window')."</button>";
112                 print "</div>";
113
114         }
115
116
117         function getfiltertree() {
118                 $root = array();
119                 $root['id'] = 'root';
120                 $root['name'] = __('Filters');
121                 $root['items'] = array();
122
123                 $filter_search = $_SESSION["prefs_filter_search"];
124
125                 $result = db_query($this->link, "SELECT *,
126                         (SELECT action_param FROM ttrss_filters2_actions
127                                 WHERE filter_id = ttrss_filters2.id ORDER BY id LIMIT 1) AS action_param,
128                         (SELECT action_id FROM ttrss_filters2_actions
129                                 WHERE filter_id = ttrss_filters2.id ORDER BY id LIMIT 1) AS action_id,
130                         (SELECT description FROM ttrss_filter_actions
131                                 WHERE id = (SELECT action_id FROM ttrss_filters2_actions
132                                         WHERE filter_id = ttrss_filters2.id ORDER BY id LIMIT 1)) AS action_name,
133                         (SELECT reg_exp FROM ttrss_filters2_rules
134                                 WHERE filter_id = ttrss_filters2.id ORDER BY id LIMIT 1) AS reg_exp
135                         FROM ttrss_filters2 WHERE
136                         owner_uid = ".$_SESSION["uid"]." ORDER BY order_id");
137
138
139                 $action_id = -1;
140                 $folder = array();
141                 $folder['items'] = array();
142
143                 while ($line = db_fetch_assoc($result)) {
144
145                         /* if ($action_id != $line["action_id"]) {
146                                 if (count($folder['items']) > 0) {
147                                         array_push($root['items'], $folder);
148                                 }
149
150                                 $folder = array();
151                                 $folder['id'] = $line["action_id"];
152                                 $folder['name'] = __($line["action_name"]);
153                                 $folder['items'] = array();
154                                 $action_id = $line["action_id"];
155                         } */
156
157                         $name = $this->getFilterName($line["id"]);
158
159                         $match_ok = false;
160                         if ($filter_search) {
161                                 $rules_result = db_query($this->link,
162                                         "SELECT reg_exp FROM ttrss_filters2_rules WHERE filter_id = ".$line["id"]);
163
164                                 while ($rule_line = db_fetch_assoc($rules_result)) {
165                                         if (mb_strpos($rule_line['reg_exp'], $filter_search) !== false) {
166                                                 $match_ok = true;
167                                                 break;
168                                         }
169                                 }
170                         }
171
172                         if ($line['action_id'] == 7) {
173                                 $label_result = db_query($this->link, "SELECT fg_color, bg_color
174                                         FROM ttrss_labels2 WHERE caption = '".db_escape_string($this->link, $line['action_param'])."' AND
175                                                 owner_uid = " . $_SESSION["uid"]);
176
177                                 if (db_num_rows($label_result) > 0) {
178                                         $fg_color = db_fetch_result($label_result, 0, "fg_color");
179                                         $bg_color = db_fetch_result($label_result, 0, "bg_color");
180
181                                         $name[1] = "<span class=\"labelColorIndicator\" id=\"label-editor-indicator\" style='color : $fg_color; background-color : $bg_color; margin-right : 4px'>&alpha;</span>" . $name[1];
182                                 }
183                         }
184
185                         $filter = array();
186                         $filter['id'] = 'FILTER:' . $line['id'];
187                         $filter['bare_id'] = $line['id'];
188                         $filter['name'] = $name[0];
189                         $filter['param'] = $name[1];
190                         $filter['checkbox'] = false;
191                         $filter['enabled'] = sql_bool_to_bool($line["enabled"]);
192
193                         if (!$filter_search || $match_ok) {
194                                 array_push($folder['items'], $filter);
195                         }
196                 }
197
198                 /* if (count($folder['items']) > 0) {
199                         array_push($root['items'], $folder);
200                 } */
201
202                 $root['items'] = $folder['items'];
203
204                 $fl = array();
205                 $fl['identifier'] = 'id';
206                 $fl['label'] = 'name';
207                 $fl['items'] = array($root);
208
209                 print json_encode($fl);
210                 return;
211         }
212
213         function edit() {
214
215                 $filter_id = db_escape_string($this->link, $_REQUEST["id"]);
216
217                 $result = db_query($this->link,
218                         "SELECT * FROM ttrss_filters2 WHERE id = '$filter_id' AND owner_uid = " . $_SESSION["uid"]);
219
220                 $enabled = sql_bool_to_bool(db_fetch_result($result, 0, "enabled"));
221                 $match_any_rule = sql_bool_to_bool(db_fetch_result($result, 0, "match_any_rule"));
222                 $inverse = sql_bool_to_bool(db_fetch_result($result, 0, "inverse"));
223                 $title = htmlspecialchars(db_fetch_result($result, 0, "title"));
224
225                 print "<form id=\"filter_edit_form\" onsubmit='return false'>";
226
227                 print "<input dojoType=\"dijit.form.TextBox\" style=\"display : none\" name=\"op\" value=\"pref-filters\">";
228                 print "<input dojoType=\"dijit.form.TextBox\" style=\"display : none\" name=\"id\" value=\"$filter_id\">";
229                 print "<input dojoType=\"dijit.form.TextBox\" style=\"display : none\" name=\"method\" value=\"editSave\">";
230                 print "<input dojoType=\"dijit.form.TextBox\" style=\"display : none\" name=\"csrf_token\" value=\"".$_SESSION['csrf_token']."\">";
231
232                 print "<div class=\"dlgSec\">".__("Caption")."</div>";
233
234                 print "<input dojoType=\"dijit.form.TextBox\" style=\"width : 20em;\" name=\"title\" value=\"$title\">";
235
236                 print "</div>";
237
238                 print "<div class=\"dlgSec\">".__("Match")."</div>";
239
240                 print "<div dojoType=\"dijit.Toolbar\">";
241
242                 print "<div dojoType=\"dijit.form.DropDownButton\">".
243                                 "<span>" . __('Select')."</span>";
244                 print "<div dojoType=\"dijit.Menu\" style=\"display: none;\">";
245                 print "<div onclick=\"dijit.byId('filterEditDlg').selectRules(true)\"
246                         dojoType=\"dijit.MenuItem\">".__('All')."</div>";
247                 print "<div onclick=\"dijit.byId('filterEditDlg').selectRules(false)\"
248                         dojoType=\"dijit.MenuItem\">".__('None')."</div>";
249                 print "</div></div>";
250
251                 print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').addRule()\">".
252                         __('Add')."</button> ";
253
254                 print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').deleteRule()\">".
255                         __('Delete')."</button> ";
256
257                 print "</div>";
258
259                 print "<ul id='filterDlg_Matches'>";
260
261                 $rules_result = db_query($this->link, "SELECT * FROM ttrss_filters2_rules
262                         WHERE filter_id = '$filter_id' ORDER BY reg_exp, id");
263
264                 while ($line = db_fetch_assoc($rules_result)) {
265                         if (sql_bool_to_bool($line["cat_filter"])) {
266                                 $line["feed_id"] = "CAT:" . (int)$line["cat_id"];
267                         }
268
269                         unset($line["cat_filter"]);
270                         unset($line["cat_id"]);
271                         unset($line["filter_id"]);
272                         unset($line["id"]);
273                         if (!sql_bool_to_bool($line["inverse"])) unset($line["inverse"]);
274
275                         $data = htmlspecialchars(json_encode($line));
276
277                         print "<li><input dojoType='dijit.form.CheckBox' type='checkbox' onclick='toggleSelectListRow2(this)'>".
278                                 "<span onclick=\"dijit.byId('filterEditDlg').editRule(this)\">".$this->getRuleName($line)."</span>".
279                                 "<input type='hidden' name='rule[]' value=\"$data\"/></li>";
280                 }
281
282                 print "</ul>";
283
284                 print "</div>";
285
286                 print "<div class=\"dlgSec\">".__("Apply actions")."</div>";
287
288                 print "<div dojoType=\"dijit.Toolbar\">";
289
290                 print "<div dojoType=\"dijit.form.DropDownButton\">".
291                                 "<span>" . __('Select')."</span>";
292                 print "<div dojoType=\"dijit.Menu\" style=\"display: none;\">";
293                 print "<div onclick=\"dijit.byId('filterEditDlg').selectActions(true)\"
294                         dojoType=\"dijit.MenuItem\">".__('All')."</div>";
295                 print "<div onclick=\"dijit.byId('filterEditDlg').selectActions(false)\"
296                         dojoType=\"dijit.MenuItem\">".__('None')."</div>";
297                 print "</div></div>";
298
299                 print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').addAction()\">".
300                         __('Add')."</button> ";
301
302                 print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').deleteAction()\">".
303                         __('Delete')."</button> ";
304
305                 print "</div>";
306
307                 print "<ul id='filterDlg_Actions'>";
308
309                 $actions_result = db_query($this->link, "SELECT * FROM ttrss_filters2_actions
310                         WHERE filter_id = '$filter_id' ORDER BY id");
311
312                 while ($line = db_fetch_assoc($actions_result)) {
313                         $line["action_param_label"] = $line["action_param"];
314
315                         unset($line["filter_id"]);
316                         unset($line["id"]);
317
318                         $data = htmlspecialchars(json_encode($line));
319
320                         print "<li><input dojoType='dijit.form.CheckBox' type='checkbox' onclick='toggleSelectListRow2(this)'>".
321                                 "<span onclick=\"dijit.byId('filterEditDlg').editAction(this)\">".$this->getActionName($line)."</span>".
322                                 "<input type='hidden' name='action[]' value=\"$data\"/></li>";
323                 }
324
325                 print "</ul>";
326
327                 print "</div>";
328
329                 if ($enabled) {
330                         $checked = "checked=\"1\"";
331                 } else {
332                         $checked = "";
333                 }
334
335                 print "<input dojoType=\"dijit.form.CheckBox\" type=\"checkbox\" name=\"enabled\" id=\"enabled\" $checked>
336                                 <label for=\"enabled\">".__('Enabled')."</label>";
337
338                 if ($match_any_rule) {
339                         $checked = "checked=\"1\"";
340                 } else {
341                         $checked = "";
342                 }
343
344                 print "<br/><input dojoType=\"dijit.form.CheckBox\" type=\"checkbox\" name=\"match_any_rule\" id=\"match_any_rule\" $checked>
345                                 <label for=\"match_any_rule\">".__('Match any rule')."</label>";
346
347                 if ($inverse) {
348                         $checked = "checked=\"1\"";
349                 } else {
350                         $checked = "";
351                 }
352
353                 print "<br/><input dojoType=\"dijit.form.CheckBox\" type=\"checkbox\" name=\"inverse\" id=\"inverse\" $checked>
354                                 <label for=\"inverse\">".__('Inverse matching')."</label>";
355
356                 print "<p/>";
357
358                 print "<div class=\"dlgButtons\">";
359
360                 print "<div style=\"float : left\">";
361                 print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').removeFilter()\">".
362                         __('Remove')."</button>";
363                 print "</div>";
364
365                 print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').test()\">".
366                         __('Test')."</button> ";
367
368                 print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').execute()\">".
369                         __('Save')."</button> ";
370
371                 print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').hide()\">".
372                         __('Cancel')."</button>";
373
374                 print "</div>";
375         }
376
377         private function getRuleName($rule) {
378                 if (!$rule) $rule = json_decode($_REQUEST["rule"], true);
379
380                 $feed_id = $rule["feed_id"];
381
382                 if (strpos($feed_id, "CAT:") === 0) {
383                         $feed_id = (int) substr($feed_id, 4);
384                         $feed = getCategoryTitle($this->link, $feed_id);
385                 } else {
386                         $feed_id = (int) $feed_id;
387
388                         if ($rule["feed_id"])
389                                 $feed = getFeedTitle($this->link, (int)$rule["feed_id"]);
390                         else
391                                 $feed = __("All feeds");
392                 }
393
394                 $result = db_query($this->link, "SELECT description FROM ttrss_filter_types
395                         WHERE id = ".(int)$rule["filter_type"]);
396                 $filter_type = db_fetch_result($result, 0, "description");
397
398                 return T_sprintf("%s on %s in %s %s", strip_tags($rule["reg_exp"]),
399                         $filter_type, $feed, isset($rule["inverse"]) ? __("(inverse)") : "");
400         }
401
402         function printRuleName() {
403                 print $this->getRuleName(json_decode($_REQUEST["rule"], true));
404         }
405
406         private function getActionName($action) {
407                 $result = db_query($this->link, "SELECT description FROM
408                         ttrss_filter_actions WHERE id = " .(int)$action["action_id"]);
409
410                 $title = __(db_fetch_result($result, 0, "description"));
411
412                 if ($action["action_id"] == 4 || $action["action_id"] == 6 ||
413                         $action["action_id"] == 7)
414                                 $title .= ": " . $action["action_param"];
415
416                 return $title;
417         }
418
419         function printActionName() {
420                 print $this->getActionName(json_decode($_REQUEST["action"], true));
421         }
422
423         function editSave() {
424                 if ($_REQUEST["savemode"] && $_REQUEST["savemode"] == "test") {
425                         return $this->testFilter();
426                 }
427
428 #               print_r($_REQUEST);
429
430                 $filter_id = db_escape_string($this->link, $_REQUEST["id"]);
431                 $enabled = checkbox_to_sql_bool(db_escape_string($this->link, $_REQUEST["enabled"]));
432                 $match_any_rule = checkbox_to_sql_bool(db_escape_string($this->link, $_REQUEST["match_any_rule"]));
433                 $inverse = checkbox_to_sql_bool(db_escape_string($this->link, $_REQUEST["inverse"]));
434                 $title = db_escape_string($this->link, $_REQUEST["title"]);
435
436                 $result = db_query($this->link, "UPDATE ttrss_filters2 SET enabled = $enabled,
437                         match_any_rule = $match_any_rule,
438                         inverse = $inverse,
439                         title = '$title'
440                         WHERE id = '$filter_id'
441                         AND owner_uid = ". $_SESSION["uid"]);
442
443                 $this->saveRulesAndActions($filter_id);
444
445         }
446
447         function remove() {
448
449                 $ids = split(",", db_escape_string($this->link, $_REQUEST["ids"]));
450
451                 foreach ($ids as $id) {
452                         db_query($this->link, "DELETE FROM ttrss_filters2 WHERE id = '$id' AND owner_uid = ". $_SESSION["uid"]);
453                 }
454         }
455
456         private function saveRulesAndActions($filter_id) {
457
458                 db_query($this->link, "DELETE FROM ttrss_filters2_rules WHERE filter_id = '$filter_id'");
459                 db_query($this->link, "DELETE FROM ttrss_filters2_actions WHERE filter_id = '$filter_id'");
460
461                 if ($filter_id) {
462                         /* create rules */
463
464                         $rules = array();
465                         $actions = array();
466
467                         foreach ($_REQUEST["rule"] as $rule) {
468                                 $rule = json_decode($rule, true);
469                                 unset($rule["id"]);
470
471                                 if (array_search($rule, $rules) === false) {
472                                         array_push($rules, $rule);
473                                 }
474                         }
475
476                         foreach ($_REQUEST["action"] as $action) {
477                                 $action = json_decode($action, true);
478                                 unset($action["id"]);
479
480                                 if (array_search($action, $actions) === false) {
481                                         array_push($actions, $action);
482                                 }
483                         }
484
485                         foreach ($rules as $rule) {
486                                 if ($rule) {
487
488                                         $reg_exp = strip_tags(db_escape_string($this->link, trim($rule["reg_exp"])));
489                                         $inverse = isset($rule["inverse"]) ? "true" : "false";
490
491                                         $filter_type = (int) db_escape_string($this->link, trim($rule["filter_type"]));
492                                         $feed_id = db_escape_string($this->link, trim($rule["feed_id"]));
493
494                                         if (strpos($feed_id, "CAT:") === 0) {
495
496                                                 $cat_filter = bool_to_sql_bool(true);
497                                                 $cat_id = (int) substr($feed_id, 4);
498                                                 $feed_id = "NULL";
499
500                                                 if (!$cat_id) $cat_id = "NULL"; // Uncategorized
501                                         } else {
502                                                 $cat_filter = bool_to_sql_bool(false);
503                                                 $feed_id = (int) $feed_id;
504                                                 $cat_id = "NULL";
505
506                                                 if (!$feed_id) $feed_id = "NULL"; // Uncategorized
507                                         }
508
509                                         $query = "INSERT INTO ttrss_filters2_rules
510                                                 (filter_id, reg_exp,filter_type,feed_id,cat_id,cat_filter,inverse) VALUES
511                                                 ('$filter_id', '$reg_exp', '$filter_type', $feed_id, $cat_id, $cat_filter, $inverse)";
512
513                                         db_query($this->link, $query);
514                                 }
515                         }
516
517                         foreach ($actions as $action) {
518                                 if ($action) {
519
520                                         $action_id = (int) db_escape_string($this->link, $action["action_id"]);
521                                         $action_param = db_escape_string($this->link, $action["action_param"]);
522                                         $action_param_label = db_escape_string($this->link, $action["action_param_label"]);
523
524                                         if ($action_id == 7) {
525                                                 $action_param = $action_param_label;
526                                         }
527
528                                         if ($action_id == 6) {
529                                                 $action_param = (int) str_replace("+", "", $action_param);
530                                         }
531
532                                         $query = "INSERT INTO ttrss_filters2_actions
533                                                 (filter_id, action_id, action_param) VALUES
534                                                 ('$filter_id', '$action_id', '$action_param')";
535
536                                         db_query($this->link, $query);
537                                 }
538                         }
539                 }
540
541
542         }
543
544         function add() {
545                 if ($_REQUEST["savemode"] && $_REQUEST["savemode"] == "test") {
546                         return $this->testFilter();
547                 }
548
549 #               print_r($_REQUEST);
550
551                 $enabled = checkbox_to_sql_bool($_REQUEST["enabled"]);
552                 $match_any_rule = checkbox_to_sql_bool($_REQUEST["match_any_rule"]);
553                 $title = db_escape_string($this->link, $_REQUEST["title"]);
554
555                 db_query($this->link, "BEGIN");
556
557                 /* create base filter */
558
559                 $result = db_query($this->link, "INSERT INTO ttrss_filters2
560                         (owner_uid, match_any_rule, enabled, title) VALUES
561                         (".$_SESSION["uid"].",$match_any_rule,$enabled, '$title')");
562
563                 $result = db_query($this->link, "SELECT MAX(id) AS id FROM ttrss_filters2
564                         WHERE owner_uid = ".$_SESSION["uid"]);
565
566                 $filter_id = db_fetch_result($result, 0, "id");
567
568                 $this->saveRulesAndActions($filter_id);
569
570                 db_query($this->link, "COMMIT");
571         }
572
573         function index() {
574
575                 $sort = db_escape_string($this->link, $_REQUEST["sort"]);
576
577                 if (!$sort || $sort == "undefined") {
578                         $sort = "reg_exp";
579                 }
580
581                 $filter_search = db_escape_string($this->link, $_REQUEST["search"]);
582
583                 if (array_key_exists("search", $_REQUEST)) {
584                         $_SESSION["prefs_filter_search"] = $filter_search;
585                 } else {
586                         $filter_search = $_SESSION["prefs_filter_search"];
587                 }
588
589                 print "<div id=\"pref-filter-wrap\" dojoType=\"dijit.layout.BorderContainer\" gutters=\"false\">";
590                 print "<div id=\"pref-filter-header\" dojoType=\"dijit.layout.ContentPane\" region=\"top\">";
591                 print "<div id=\"pref-filter-toolbar\" dojoType=\"dijit.Toolbar\">";
592
593                 $filter_search = db_escape_string($this->link, $_REQUEST["search"]);
594
595                 if (array_key_exists("search", $_REQUEST)) {
596                         $_SESSION["prefs_filter_search"] = $filter_search;
597                 } else {
598                         $filter_search = $_SESSION["prefs_filter_search"];
599                 }
600
601                 print "<div style='float : right; padding-right : 4px;'>
602                         <input dojoType=\"dijit.form.TextBox\" id=\"filter_search\" size=\"20\" type=\"search\"
603                                 value=\"$filter_search\">
604                         <button dojoType=\"dijit.form.Button\" onclick=\"updateFilterList()\">".
605                                 __('Search')."</button>
606                         </div>";
607
608                 print "<div dojoType=\"dijit.form.DropDownButton\">".
609                                 "<span>" . __('Select')."</span>";
610                 print "<div dojoType=\"dijit.Menu\" style=\"display: none;\">";
611                 print "<div onclick=\"dijit.byId('filterTree').model.setAllChecked(true)\"
612                         dojoType=\"dijit.MenuItem\">".__('All')."</div>";
613                 print "<div onclick=\"dijit.byId('filterTree').model.setAllChecked(false)\"
614                         dojoType=\"dijit.MenuItem\">".__('None')."</div>";
615                 print "</div></div>";
616
617                 print "<button dojoType=\"dijit.form.Button\" onclick=\"return quickAddFilter()\">".
618                         __('Create filter')."</button> ";
619
620                 print "<button dojoType=\"dijit.form.Button\" onclick=\"return joinSelectedFilters()\">".
621                         __('Combine')."</button> ";
622
623                 print "<button dojoType=\"dijit.form.Button\" onclick=\"return editSelectedFilter()\">".
624                         __('Edit')."</button> ";
625
626                 print "<button dojoType=\"dijit.form.Button\" onclick=\"return removeSelectedFilters()\">".
627                         __('Remove')."</button> ";
628
629                 if (defined('_ENABLE_FEED_DEBUGGING')) {
630                         print "<button dojoType=\"dijit.form.Button\" onclick=\"rescore_all_feeds()\">".
631                                 __('Rescore articles')."</button> ";
632                 }
633
634                 print "</div>"; # toolbar
635                 print "</div>"; # toolbar-frame
636                 print "<div id=\"pref-filter-content\" dojoType=\"dijit.layout.ContentPane\" region=\"center\">";
637
638                 print "<div id=\"filterlistLoading\">
639                 <img src='images/indicator_tiny.gif'>".
640                  __("Loading, please wait...")."</div>";
641
642                 print "<div dojoType=\"dojo.data.ItemFileWriteStore\" jsId=\"filterStore\"
643                         url=\"backend.php?op=pref-filters&method=getfiltertree\">
644                 </div>
645                 <div dojoType=\"lib.CheckBoxStoreModel\" jsId=\"filterModel\" store=\"filterStore\"
646                 query=\"{id:'root'}\" rootId=\"root\" rootLabel=\"Feeds\"
647                         childrenAttrs=\"items\" checkboxStrict=\"false\" checkboxAll=\"false\">
648                 </div>
649                 <div dojoType=\"fox.PrefFilterTree\" id=\"filterTree\"
650                         model=\"filterModel\" openOnClick=\"true\">
651                 <script type=\"dojo/method\" event=\"onLoad\" args=\"item\">
652                         Element.hide(\"filterlistLoading\");
653                 </script>
654                 <script type=\"dojo/method\" event=\"onClick\" args=\"item\">
655                         var id = String(item.id);
656                         var bare_id = id.substr(id.indexOf(':')+1);
657
658                         if (id.match('FILTER:')) {
659                                 editFilter(bare_id);
660                         }
661                 </script>
662
663                 </div>";
664
665                 print "</div>"; #pane
666
667                 global $pluginhost;
668                 $pluginhost->run_hooks($pluginhost::HOOK_PREFS_TAB,
669                         "hook_prefs_tab", "prefFilters");
670
671                 print "</div>"; #container
672
673         }
674
675         function newfilter() {
676
677                 print "<form name='filter_new_form' id='filter_new_form'>";
678
679                 print "<input dojoType=\"dijit.form.TextBox\" style=\"display : none\" name=\"op\" value=\"pref-filters\">";
680                 print "<input dojoType=\"dijit.form.TextBox\" style=\"display : none\" name=\"method\" value=\"add\">";
681                 print "<input dojoType=\"dijit.form.TextBox\" style=\"display : none\" name=\"csrf_token\" value=\"".$_SESSION['csrf_token']."\">";
682
683                 print "<div class=\"dlgSec\">".__("Caption")."</div>";
684
685                 print "<input dojoType=\"dijit.form.TextBox\" style=\"width : 20em;\" name=\"title\" value=\"\">";
686
687                 print "<div class=\"dlgSec\">".__("Match")."</div>";
688
689                 print "<div dojoType=\"dijit.Toolbar\">";
690
691                 print "<div dojoType=\"dijit.form.DropDownButton\">".
692                                 "<span>" . __('Select')."</span>";
693                 print "<div dojoType=\"dijit.Menu\" style=\"display: none;\">";
694                 print "<div onclick=\"dijit.byId('filterEditDlg').selectRules(true)\"
695                         dojoType=\"dijit.MenuItem\">".__('All')."</div>";
696                 print "<div onclick=\"dijit.byId('filterEditDlg').selectRules(false)\"
697                         dojoType=\"dijit.MenuItem\">".__('None')."</div>";
698                 print "</div></div>";
699
700                 print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').addRule()\">".
701                         __('Add')."</button> ";
702
703                 print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').deleteRule()\">".
704                         __('Delete')."</button> ";
705
706                 print "</div>";
707
708                 print "<ul id='filterDlg_Matches'>";
709 #               print "<li>No rules</li>";
710                 print "</ul>";
711
712                 print "</div>";
713
714                 print "<div class=\"dlgSec\">".__("Apply actions")."</div>";
715
716                 print "<div dojoType=\"dijit.Toolbar\">";
717
718                 print "<div dojoType=\"dijit.form.DropDownButton\">".
719                                 "<span>" . __('Select')."</span>";
720                 print "<div dojoType=\"dijit.Menu\" style=\"display: none;\">";
721                 print "<div onclick=\"dijit.byId('filterEditDlg').selectActions(true)\"
722                         dojoType=\"dijit.MenuItem\">".__('All')."</div>";
723                 print "<div onclick=\"dijit.byId('filterEditDlg').selectActions(false)\"
724                         dojoType=\"dijit.MenuItem\">".__('None')."</div>";
725                 print "</div></div>";
726
727                 print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').addAction()\">".
728                         __('Add')."</button> ";
729
730                 print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').deleteAction()\">".
731                         __('Delete')."</button> ";
732
733                 print "</div>";
734
735                 print "<ul id='filterDlg_Actions'>";
736 #               print "<li>No actions</li>";
737                 print "</ul>";
738
739 /*              print "<div class=\"dlgSec\">".__("Options")."</div>";
740                 print "<div class=\"dlgSecCont\">"; */
741
742                 print "<input dojoType=\"dijit.form.CheckBox\" type=\"checkbox\" name=\"enabled\" id=\"enabled\" checked=\"1\">
743                                 <label for=\"enabled\">".__('Enabled')."</label>";
744
745                 print "<br/><input dojoType=\"dijit.form.CheckBox\" type=\"checkbox\" name=\"match_any_rule\" id=\"match_any_rule\">
746                                 <label for=\"match_any_rule\">".__('Match any rule')."</label>";
747
748                 print "<br/><input dojoType=\"dijit.form.CheckBox\" type=\"checkbox\" name=\"inverse\" id=\"inverse\">
749                                 <label for=\"inverse\">".__('Inverse matching')."</label>";
750
751 //              print "</div>";
752
753                 print "<div class=\"dlgButtons\">";
754
755                 print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').test()\">".
756                         __('Test')."</button> ";
757
758                 print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').execute()\">".
759                         __('Create')."</button> ";
760
761                 print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').hide()\">".
762                         __('Cancel')."</button>";
763
764                 print "</div>";
765
766         }
767
768         function newrule() {
769                 $rule = json_decode($_REQUEST["rule"], true);
770
771                 if ($rule) {
772                         $reg_exp = htmlspecialchars($rule["reg_exp"]);
773                         $filter_type = $rule["filter_type"];
774                         $feed_id = $rule["feed_id"];
775                         $inverse_checked = isset($rule["inverse"]) ? "checked" : "";
776                 } else {
777                         $reg_exp = "";
778                         $filter_type = 1;
779                         $feed_id = 0;
780                         $inverse_checked = "";
781                 }
782
783                 if (strpos($feed_id, "CAT:") === 0) {
784                         $feed_id = substr($feed_id, 4);
785                         $cat_filter = true;
786                 } else {
787                         $cat_filter = false;
788                 }
789
790
791                 print "<form name='filter_new_rule_form' id='filter_new_rule_form'>";
792
793                 $result = db_query($this->link, "SELECT id,description
794                         FROM ttrss_filter_types WHERE id != 5 ORDER BY description");
795
796                 $filter_types = array();
797
798                 while ($line = db_fetch_assoc($result)) {
799                         $filter_types[$line["id"]] = __($line["description"]);
800                 }
801
802                 print "<div class=\"dlgSec\">".__("Match")."</div>";
803
804                 print "<div class=\"dlgSecCont\">";
805
806                 print "<input dojoType=\"dijit.form.ValidationTextBox\"
807                          required=\"true\" id=\"filterDlg_regExp\"
808                          style=\"font-size : 16px; width : 20em;\"
809                          name=\"reg_exp\" value=\"$reg_exp\"/>";
810
811                 print "<hr/>";
812                 print "<input id=\"filterDlg_inverse\" dojoType=\"dijit.form.CheckBox\"
813                          name=\"inverse\" $inverse_checked/>";
814                 print "<label for=\"filterDlg_inverse\">".__("Inverse regular expression matching")."</label>";
815
816                 print "<hr/>" .  __("on field") . " ";
817                 print_select_hash("filter_type", $filter_type, $filter_types,
818                         'dojoType="dijit.form.Select"');
819
820                 print "<hr/>";
821
822                 print __("in") . " ";
823
824                 print "<span id='filterDlg_feeds'>";
825                 print_feed_select($this->link, "feed_id",
826                         $cat_filter ? "CAT:$feed_id" : $feed_id,
827                         'dojoType="dijit.form.FilteringSelect"');
828                 print "</span>";
829
830                 print "</div>";
831
832                 print "<div class=\"dlgButtons\">";
833
834                 print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterNewRuleDlg').execute()\">".
835                         ($rule ? __("Save rule") : __('Add rule'))."</button> ";
836
837                 print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterNewRuleDlg').hide()\">".
838                         __('Cancel')."</button>";
839
840                 print "</div>";
841
842                 print "</form>";
843         }
844
845         function newaction() {
846                 $action = json_decode($_REQUEST["action"], true);
847
848                 if ($action) {
849                         $action_param = db_escape_string($this->link, $action["action_param"]);
850                         $action_id = (int)$action["action_id"];
851                 } else {
852                         $action_param = "";
853                         $action_id = 0;
854                 }
855
856                 print "<form name='filter_new_action_form' id='filter_new_action_form'>";
857
858                 print "<div class=\"dlgSec\">".__("Perform Action")."</div>";
859
860                 print "<div class=\"dlgSecCont\">";
861
862                 print "<select name=\"action_id\" dojoType=\"dijit.form.Select\"
863                         onchange=\"filterDlgCheckAction(this)\">";
864
865                 $result = db_query($this->link, "SELECT id,description FROM ttrss_filter_actions
866                         ORDER BY name");
867
868                 while ($line = db_fetch_assoc($result)) {
869                         $is_selected = ($line["id"] == $action_id) ? "selected='1'" : "";
870                         printf("<option $is_selected value='%d'>%s</option>", $line["id"], __($line["description"]));
871                 }
872
873                 print "</select>";
874
875                 $param_box_hidden = ($action_id == 7 || $action_id == 4 || $action_id == 6) ?
876                         "" : "display : none";
877
878                 $param_hidden = ($action_id == 4 || $action_id == 6) ?
879                         "" : "display : none";
880
881                 $label_param_hidden = ($action_id == 7) ?       "" : "display : none";
882
883                 print "<span id=\"filterDlg_paramBox\" style=\"$param_box_hidden\">";
884                 print " " . __("with parameters:") . " ";
885                 print "<input dojoType=\"dijit.form.TextBox\"
886                         id=\"filterDlg_actionParam\" style=\"$param_hidden\"
887                         name=\"action_param\" value=\"$action_param\">";
888
889                 print_label_select($this->link, "action_param_label", $action_param,
890                         "id=\"filterDlg_actionParamLabel\" style=\"$label_param_hidden\"
891                         dojoType=\"dijit.form.Select\"");
892
893                 print "</span>";
894
895                 print "&nbsp;"; // tiny layout hack
896
897                 print "</div>";
898
899                 print "<div class=\"dlgButtons\">";
900
901                 print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterNewActionDlg').execute()\">".
902                         ($action ? __("Save action") : __('Add action'))."</button> ";
903
904                 print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterNewActionDlg').hide()\">".
905                         __('Cancel')."</button>";
906
907                 print "</div>";
908
909                 print "</form>";
910         }
911
912         private function getFilterName($id) {
913
914                 $result = db_query($this->link,
915                         "SELECT title FROM ttrss_filters2 WHERE id = '$id'");
916
917                 $title = db_fetch_result($result, 0, "title");
918
919                 if (!$title) {
920
921                         $result = db_query($this->link,
922                                 "SELECT * FROM ttrss_filters2_rules WHERE filter_id = '$id' ORDER BY id
923                                 LIMIT 3");
924
925                         $titles = array();
926                         $count = 0;
927
928                         while ($line = db_fetch_assoc($result)) {
929
930                                 if (sql_bool_to_bool($line["cat_filter"])) {
931                                         unset($line["cat_filter"]);
932                                         $line["feed_id"] = "CAT:" . (int)$line["cat_id"];
933                                         unset($line["cat_id"]);
934                                 }
935
936                                 if (!sql_bool_to_bool($line["inverse"])) unset($line["inverse"]);
937
938                                 if ($count < 2) {
939                                         array_push($titles, $this->getRuleName($line));
940                                 } else {
941                                         array_push($titles, "...");
942                                         break;
943                                 }
944                                 ++$count;
945                         }
946
947                         $title = truncate_string(join(", ", $titles), 64, "...");
948
949                 }
950
951                 $result = db_query($this->link,
952                         "SELECT * FROM ttrss_filters2_actions WHERE filter_id = '$id' ORDER BY id LIMIT 3");
953
954                 $actions = array();
955                 $count = 0;
956
957                 while ($line = db_fetch_assoc($result)) {
958                         if ($count < 2) {
959                                 array_push($actions, $this->getActionName($line));
960                         } else {
961                                 array_push($actions, "...");
962                                 break;
963                         }
964                         ++$count;
965                 }
966
967                 return array($title, join(", ", $actions));
968         }
969
970         function join() {
971                 $ids = explode(",", db_escape_string($this->link, $_REQUEST["ids"]));
972
973                 if (count($ids) > 1) {
974                         $base_id = array_shift($ids);
975                         $ids_str = join(",", $ids);
976
977                         db_query($this->link, "BEGIN");
978                         db_query($this->link, "UPDATE ttrss_filters2_rules
979                                 SET filter_id = '$base_id' WHERE filter_id IN ($ids_str)");
980                         db_query($this->link, "UPDATE ttrss_filters2_actions
981                                 SET filter_id = '$base_id' WHERE filter_id IN ($ids_str)");
982
983                         db_query($this->link, "DELETE FROM ttrss_filters2 WHERE id IN ($ids_str)");
984                         db_query($this->link, "UPDATE ttrss_filters2 SET match_any_rule = true WHERE id = '$base_id'");
985
986                         db_query($this->link, "COMMIT");
987
988                         $this->optimizeFilter($base_id);
989
990                 }
991         }
992
993         private function optimizeFilter($id) {
994                 db_query($this->link, "BEGIN");
995                 $result = db_query($this->link, "SELECT * FROM ttrss_filters2_actions
996                         WHERE filter_id = '$id'");
997
998                 $tmp = array();
999                 $dupe_ids = array();
1000
1001                 while ($line = db_fetch_assoc($result)) {
1002                         $id = $line["id"];
1003                         unset($line["id"]);
1004
1005                         if (array_search($line, $tmp) === false) {
1006                                 array_push($tmp, $line);
1007                         } else {
1008                                 array_push($dupe_ids, $id);
1009                         }
1010                 }
1011
1012                 if (count($dupe_ids) > 0) {
1013                         $ids_str = join(",", $dupe_ids);
1014                         db_query($this->link, "DELETE FROM ttrss_filters2_actions
1015                                 WHERE id IN ($ids_str)");
1016                 }
1017
1018                 $result = db_query($this->link, "SELECT * FROM ttrss_filters2_rules
1019                         WHERE filter_id = '$id'");
1020
1021                 $tmp = array();
1022                 $dupe_ids = array();
1023
1024                 while ($line = db_fetch_assoc($result)) {
1025                         $id = $line["id"];
1026                         unset($line["id"]);
1027
1028                         if (array_search($line, $tmp) === false) {
1029                                 array_push($tmp, $line);
1030                         } else {
1031                                 array_push($dupe_ids, $id);
1032                         }
1033                 }
1034
1035                 if (count($dupe_ids) > 0) {
1036                         $ids_str = join(",", $dupe_ids);
1037                         db_query($this->link, "DELETE FROM ttrss_filters2_rules
1038                                 WHERE id IN ($ids_str)");
1039                 }
1040
1041                 db_query($this->link, "COMMIT");
1042         }
1043 }
1044 ?>