]> git.wh0rd.org - tt-rss.git/blob - classes/pref/filters.php
let us rejoice on being able to create double negative filters (refs #631)
[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 action_id,reg_exp");
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 $fl = array();
203 $fl['identifier'] = 'id';
204 $fl['label'] = 'name';
205 $fl['items'] = array($root);
206
207 print json_encode($fl);
208 return;
209 }
210
211 function edit() {
212
213 $filter_id = db_escape_string($this->link, $_REQUEST["id"]);
214
215 $result = db_query($this->link,
216 "SELECT * FROM ttrss_filters2 WHERE id = '$filter_id' AND owner_uid = " . $_SESSION["uid"]);
217
218 $enabled = sql_bool_to_bool(db_fetch_result($result, 0, "enabled"));
219 $match_any_rule = sql_bool_to_bool(db_fetch_result($result, 0, "match_any_rule"));
220 $inverse = sql_bool_to_bool(db_fetch_result($result, 0, "inverse"));
221
222 print "<form id=\"filter_edit_form\" onsubmit='return false'>";
223
224 print "<input dojoType=\"dijit.form.TextBox\" style=\"display : none\" name=\"op\" value=\"pref-filters\">";
225 print "<input dojoType=\"dijit.form.TextBox\" style=\"display : none\" name=\"id\" value=\"$filter_id\">";
226 print "<input dojoType=\"dijit.form.TextBox\" style=\"display : none\" name=\"method\" value=\"editSave\">";
227 print "<input dojoType=\"dijit.form.TextBox\" style=\"display : none\" name=\"csrf_token\" value=\"".$_SESSION['csrf_token']."\">";
228
229 print "<div class=\"dlgSec\">".__("Match")."</div>";
230
231 print "<div dojoType=\"dijit.Toolbar\">";
232
233 print "<div dojoType=\"dijit.form.DropDownButton\">".
234 "<span>" . __('Select')."</span>";
235 print "<div dojoType=\"dijit.Menu\" style=\"display: none;\">";
236 print "<div onclick=\"dijit.byId('filterEditDlg').selectRules(true)\"
237 dojoType=\"dijit.MenuItem\">".__('All')."</div>";
238 print "<div onclick=\"dijit.byId('filterEditDlg').selectRules(false)\"
239 dojoType=\"dijit.MenuItem\">".__('None')."</div>";
240 print "</div></div>";
241
242 print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').addRule()\">".
243 __('Add')."</button> ";
244
245 print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').deleteRule()\">".
246 __('Delete')."</button> ";
247
248 print "</div>";
249
250 print "<ul id='filterDlg_Matches'>";
251
252 $rules_result = db_query($this->link, "SELECT * FROM ttrss_filters2_rules
253 WHERE filter_id = '$filter_id' ORDER BY reg_exp, id");
254
255 while ($line = db_fetch_assoc($rules_result)) {
256 if (sql_bool_to_bool($line["cat_filter"])) {
257 $line["feed_id"] = "CAT:" . (int)$line["cat_id"];
258 }
259
260 unset($line["cat_filter"]);
261 unset($line["cat_id"]);
262 unset($line["filter_id"]);
263 unset($line["id"]);
264 if (!sql_bool_to_bool($line["inverse"])) unset($line["inverse"]);
265
266 $data = htmlspecialchars(json_encode($line));
267
268 print "<li><input dojoType='dijit.form.CheckBox' type='checkbox' onclick='toggleSelectListRow2(this)'>".
269 "<span onclick=\"dijit.byId('filterEditDlg').editRule(this)\">".$this->getRuleName($line)."</span>".
270 "<input type='hidden' name='rule[]' value=\"$data\"/></li>";
271 }
272
273 print "</ul>";
274
275 print "</div>";
276
277 print "<div class=\"dlgSec\">".__("Apply actions")."</div>";
278
279 print "<div dojoType=\"dijit.Toolbar\">";
280
281 print "<div dojoType=\"dijit.form.DropDownButton\">".
282 "<span>" . __('Select')."</span>";
283 print "<div dojoType=\"dijit.Menu\" style=\"display: none;\">";
284 print "<div onclick=\"dijit.byId('filterEditDlg').selectActions(true)\"
285 dojoType=\"dijit.MenuItem\">".__('All')."</div>";
286 print "<div onclick=\"dijit.byId('filterEditDlg').selectActions(false)\"
287 dojoType=\"dijit.MenuItem\">".__('None')."</div>";
288 print "</div></div>";
289
290 print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').addAction()\">".
291 __('Add')."</button> ";
292
293 print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').deleteAction()\">".
294 __('Delete')."</button> ";
295
296 print "</div>";
297
298 print "<ul id='filterDlg_Actions'>";
299
300 $actions_result = db_query($this->link, "SELECT * FROM ttrss_filters2_actions
301 WHERE filter_id = '$filter_id' ORDER BY id");
302
303 while ($line = db_fetch_assoc($actions_result)) {
304 $line["action_param_label"] = $line["action_param"];
305
306 unset($line["filter_id"]);
307 unset($line["id"]);
308
309 $data = htmlspecialchars(json_encode($line));
310
311 print "<li><input dojoType='dijit.form.CheckBox' type='checkbox' onclick='toggleSelectListRow2(this)'>".
312 "<span onclick=\"dijit.byId('filterEditDlg').editAction(this)\">".$this->getActionName($line)."</span>".
313 "<input type='hidden' name='action[]' value=\"$data\"/></li>";
314 }
315
316 print "</ul>";
317
318 print "</div>";
319
320 if ($enabled) {
321 $checked = "checked=\"1\"";
322 } else {
323 $checked = "";
324 }
325
326 print "<input dojoType=\"dijit.form.CheckBox\" type=\"checkbox\" name=\"enabled\" id=\"enabled\" $checked>
327 <label for=\"enabled\">".__('Enabled')."</label>";
328
329 if ($match_any_rule) {
330 $checked = "checked=\"1\"";
331 } else {
332 $checked = "";
333 }
334
335 print "<br/><input dojoType=\"dijit.form.CheckBox\" type=\"checkbox\" name=\"match_any_rule\" id=\"match_any_rule\" $checked>
336 <label for=\"match_any_rule\">".__('Match any rule')."</label>";
337
338 if ($inverse) {
339 $checked = "checked=\"1\"";
340 } else {
341 $checked = "";
342 }
343
344 print "<br/><input dojoType=\"dijit.form.CheckBox\" type=\"checkbox\" name=\"inverse\" id=\"inverse\" $checked>
345 <label for=\"inverse\">".__('Inverse matching')."</label>";
346
347 print "<p/>";
348
349 print "<div class=\"dlgButtons\">";
350
351 print "<div style=\"float : left\">";
352 print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').removeFilter()\">".
353 __('Remove')."</button>";
354 print "</div>";
355
356 print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').test()\">".
357 __('Test')."</button> ";
358
359 print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').execute()\">".
360 __('Save')."</button> ";
361
362 print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').hide()\">".
363 __('Cancel')."</button>";
364
365 print "</div>";
366 }
367
368 private function getRuleName($rule) {
369 if (!$rule) $rule = json_decode($_REQUEST["rule"], true);
370
371 $feed_id = $rule["feed_id"];
372
373 if (strpos($feed_id, "CAT:") === 0) {
374 $feed_id = (int) substr($feed_id, 4);
375 $feed = getCategoryTitle($this->link, $feed_id);
376 } else {
377 $feed_id = (int) $feed_id;
378
379 if ($rule["feed_id"])
380 $feed = getFeedTitle($this->link, (int)$rule["feed_id"]);
381 else
382 $feed = __("All feeds");
383 }
384
385 $result = db_query($this->link, "SELECT description FROM ttrss_filter_types
386 WHERE id = ".(int)$rule["filter_type"]);
387 $filter_type = db_fetch_result($result, 0, "description");
388
389 return T_sprintf("%s on %s in %s %s", strip_tags($rule["reg_exp"]),
390 $filter_type, $feed, isset($rule["inverse"]) ? __("(inverse)") : "");
391 }
392
393 function printRuleName() {
394 print $this->getRuleName(json_decode($_REQUEST["rule"], true));
395 }
396
397 private function getActionName($action) {
398 $result = db_query($this->link, "SELECT description FROM
399 ttrss_filter_actions WHERE id = " .(int)$action["action_id"]);
400
401 $title = __(db_fetch_result($result, 0, "description"));
402
403 if ($action["action_id"] == 4 || $action["action_id"] == 6 ||
404 $action["action_id"] == 7)
405 $title .= ": " . $action["action_param"];
406
407 return $title;
408 }
409
410 function printActionName() {
411 print $this->getActionName(json_decode($_REQUEST["action"], true));
412 }
413
414 function editSave() {
415 if ($_REQUEST["savemode"] && $_REQUEST["savemode"] == "test") {
416 return $this->testFilter();
417 }
418
419 # print_r($_REQUEST);
420
421 $filter_id = db_escape_string($this->link, $_REQUEST["id"]);
422 $enabled = checkbox_to_sql_bool(db_escape_string($this->link, $_REQUEST["enabled"]));
423 $match_any_rule = checkbox_to_sql_bool(db_escape_string($this->link, $_REQUEST["match_any_rule"]));
424 $inverse = checkbox_to_sql_bool(db_escape_string($this->link, $_REQUEST["inverse"]));
425
426 $result = db_query($this->link, "UPDATE ttrss_filters2 SET enabled = $enabled,
427 match_any_rule = $match_any_rule,
428 inverse = $inverse
429 WHERE id = '$filter_id'
430 AND owner_uid = ". $_SESSION["uid"]);
431
432 $this->saveRulesAndActions($filter_id);
433
434 }
435
436 function remove() {
437
438 $ids = split(",", db_escape_string($this->link, $_REQUEST["ids"]));
439
440 foreach ($ids as $id) {
441 db_query($this->link, "DELETE FROM ttrss_filters2 WHERE id = '$id' AND owner_uid = ". $_SESSION["uid"]);
442 }
443 }
444
445 private function saveRulesAndActions($filter_id) {
446
447 db_query($this->link, "DELETE FROM ttrss_filters2_rules WHERE filter_id = '$filter_id'");
448 db_query($this->link, "DELETE FROM ttrss_filters2_actions WHERE filter_id = '$filter_id'");
449
450 if ($filter_id) {
451 /* create rules */
452
453 $rules = array();
454 $actions = array();
455
456 foreach ($_REQUEST["rule"] as $rule) {
457 $rule = json_decode($rule, true);
458 unset($rule["id"]);
459
460 if (array_search($rule, $rules) === false) {
461 array_push($rules, $rule);
462 }
463 }
464
465 foreach ($_REQUEST["action"] as $action) {
466 $action = json_decode($action, true);
467 unset($action["id"]);
468
469 if (array_search($action, $actions) === false) {
470 array_push($actions, $action);
471 }
472 }
473
474 foreach ($rules as $rule) {
475 if ($rule) {
476
477 $reg_exp = strip_tags(db_escape_string($this->link, trim($rule["reg_exp"])));
478 $inverse = isset($rule["inverse"]) ? "true" : "false";
479
480 $filter_type = (int) db_escape_string($this->link, trim($rule["filter_type"]));
481 $feed_id = db_escape_string($this->link, trim($rule["feed_id"]));
482
483 if (strpos($feed_id, "CAT:") === 0) {
484
485 $cat_filter = bool_to_sql_bool(true);
486 $cat_id = (int) substr($feed_id, 4);
487 $feed_id = "NULL";
488
489 if (!$cat_id) $cat_id = "NULL"; // Uncategorized
490 } else {
491 $cat_filter = bool_to_sql_bool(false);
492 $feed_id = (int) $feed_id;
493 $cat_id = "NULL";
494
495 if (!$feed_id) $feed_id = "NULL"; // Uncategorized
496 }
497
498 $query = "INSERT INTO ttrss_filters2_rules
499 (filter_id, reg_exp,filter_type,feed_id,cat_id,cat_filter,inverse) VALUES
500 ('$filter_id', '$reg_exp', '$filter_type', $feed_id, $cat_id, $cat_filter, $inverse)";
501
502 db_query($this->link, $query);
503 }
504 }
505
506 foreach ($actions as $action) {
507 if ($action) {
508
509 $action_id = (int) db_escape_string($this->link, $action["action_id"]);
510 $action_param = db_escape_string($this->link, $action["action_param"]);
511 $action_param_label = db_escape_string($this->link, $action["action_param_label"]);
512
513 if ($action_id == 7) {
514 $action_param = $action_param_label;
515 }
516
517 if ($action_id == 6) {
518 $action_param = (int) str_replace("+", "", $action_param);
519 }
520
521 $query = "INSERT INTO ttrss_filters2_actions
522 (filter_id, action_id, action_param) VALUES
523 ('$filter_id', '$action_id', '$action_param')";
524
525 db_query($this->link, $query);
526 }
527 }
528 }
529
530
531 }
532
533 function add() {
534 if ($_REQUEST["savemode"] && $_REQUEST["savemode"] == "test") {
535 return $this->testFilter();
536 }
537
538 # print_r($_REQUEST);
539
540 $enabled = checkbox_to_sql_bool($_REQUEST["enabled"]);
541 $match_any_rule = checkbox_to_sql_bool($_REQUEST["match_any_rule"]);
542
543 db_query($this->link, "BEGIN");
544
545 /* create base filter */
546
547 $result = db_query($this->link, "INSERT INTO ttrss_filters2
548 (owner_uid, match_any_rule, enabled) VALUES
549 (".$_SESSION["uid"].",$match_any_rule,$enabled)");
550
551 $result = db_query($this->link, "SELECT MAX(id) AS id FROM ttrss_filters2
552 WHERE owner_uid = ".$_SESSION["uid"]);
553
554 $filter_id = db_fetch_result($result, 0, "id");
555
556 $this->saveRulesAndActions($filter_id);
557
558 db_query($this->link, "COMMIT");
559 }
560
561 function index() {
562
563 $sort = db_escape_string($this->link, $_REQUEST["sort"]);
564
565 if (!$sort || $sort == "undefined") {
566 $sort = "reg_exp";
567 }
568
569 $filter_search = db_escape_string($this->link, $_REQUEST["search"]);
570
571 if (array_key_exists("search", $_REQUEST)) {
572 $_SESSION["prefs_filter_search"] = $filter_search;
573 } else {
574 $filter_search = $_SESSION["prefs_filter_search"];
575 }
576
577 print "<div id=\"pref-filter-wrap\" dojoType=\"dijit.layout.BorderContainer\" gutters=\"false\">";
578 print "<div id=\"pref-filter-header\" dojoType=\"dijit.layout.ContentPane\" region=\"top\">";
579 print "<div id=\"pref-filter-toolbar\" dojoType=\"dijit.Toolbar\">";
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 style='float : right; padding-right : 4px;'>
590 <input dojoType=\"dijit.form.TextBox\" id=\"filter_search\" size=\"20\" type=\"search\"
591 value=\"$filter_search\">
592 <button dojoType=\"dijit.form.Button\" onclick=\"updateFilterList()\">".
593 __('Search')."</button>
594 </div>";
595
596 print "<div dojoType=\"dijit.form.DropDownButton\">".
597 "<span>" . __('Select')."</span>";
598 print "<div dojoType=\"dijit.Menu\" style=\"display: none;\">";
599 print "<div onclick=\"dijit.byId('filterTree').model.setAllChecked(true)\"
600 dojoType=\"dijit.MenuItem\">".__('All')."</div>";
601 print "<div onclick=\"dijit.byId('filterTree').model.setAllChecked(false)\"
602 dojoType=\"dijit.MenuItem\">".__('None')."</div>";
603 print "</div></div>";
604
605 print "<button dojoType=\"dijit.form.Button\" onclick=\"return quickAddFilter()\">".
606 __('Create filter')."</button> ";
607
608 print "<button dojoType=\"dijit.form.Button\" onclick=\"return joinSelectedFilters()\">".
609 __('Combine')."</button> ";
610
611 print "<button dojoType=\"dijit.form.Button\" onclick=\"return editSelectedFilter()\">".
612 __('Edit')."</button> ";
613
614 print "<button dojoType=\"dijit.form.Button\" onclick=\"return removeSelectedFilters()\">".
615 __('Remove')."</button> ";
616
617 if (defined('_ENABLE_FEED_DEBUGGING')) {
618 print "<button dojoType=\"dijit.form.Button\" onclick=\"rescore_all_feeds()\">".
619 __('Rescore articles')."</button> ";
620 }
621
622 print "</div>"; # toolbar
623 print "</div>"; # toolbar-frame
624 print "<div id=\"pref-filter-content\" dojoType=\"dijit.layout.ContentPane\" region=\"center\">";
625
626 print "<div id=\"filterlistLoading\">
627 <img src='images/indicator_tiny.gif'>".
628 __("Loading, please wait...")."</div>";
629
630 print "<div dojoType=\"dojo.data.ItemFileWriteStore\" jsId=\"filterStore\"
631 url=\"backend.php?op=pref-filters&method=getfiltertree\">
632 </div>
633 <div dojoType=\"lib.CheckBoxStoreModel\" jsId=\"filterModel\" store=\"filterStore\"
634 query=\"{id:'root'}\" rootId=\"root\" rootLabel=\"Feeds\"
635 childrenAttrs=\"items\" checkboxStrict=\"false\" checkboxAll=\"false\">
636 </div>
637 <div dojoType=\"fox.PrefFilterTree\" id=\"filterTree\"
638 model=\"filterModel\" openOnClick=\"true\">
639 <script type=\"dojo/method\" event=\"onLoad\" args=\"item\">
640 Element.hide(\"filterlistLoading\");
641 </script>
642 <script type=\"dojo/method\" event=\"onClick\" args=\"item\">
643 var id = String(item.id);
644 var bare_id = id.substr(id.indexOf(':')+1);
645
646 if (id.match('FILTER:')) {
647 editFilter(bare_id);
648 }
649 </script>
650
651 </div>";
652
653 print "</div>"; #pane
654
655 global $pluginhost;
656 $pluginhost->run_hooks($pluginhost::HOOK_PREFS_TAB,
657 "hook_prefs_tab", "prefFilters");
658
659 print "</div>"; #container
660
661 }
662
663 function newfilter() {
664
665 print "<form name='filter_new_form' id='filter_new_form'>";
666
667 print "<input dojoType=\"dijit.form.TextBox\" style=\"display : none\" name=\"op\" value=\"pref-filters\">";
668 print "<input dojoType=\"dijit.form.TextBox\" style=\"display : none\" name=\"method\" value=\"add\">";
669 print "<input dojoType=\"dijit.form.TextBox\" style=\"display : none\" name=\"csrf_token\" value=\"".$_SESSION['csrf_token']."\">";
670
671 print "<div class=\"dlgSec\">".__("Match")."</div>";
672
673 print "<div dojoType=\"dijit.Toolbar\">";
674
675 print "<div dojoType=\"dijit.form.DropDownButton\">".
676 "<span>" . __('Select')."</span>";
677 print "<div dojoType=\"dijit.Menu\" style=\"display: none;\">";
678 print "<div onclick=\"dijit.byId('filterEditDlg').selectRules(true)\"
679 dojoType=\"dijit.MenuItem\">".__('All')."</div>";
680 print "<div onclick=\"dijit.byId('filterEditDlg').selectRules(false)\"
681 dojoType=\"dijit.MenuItem\">".__('None')."</div>";
682 print "</div></div>";
683
684 print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').addRule()\">".
685 __('Add')."</button> ";
686
687 print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').deleteRule()\">".
688 __('Delete')."</button> ";
689
690 print "</div>";
691
692 print "<ul id='filterDlg_Matches'>";
693 # print "<li>No rules</li>";
694 print "</ul>";
695
696 print "</div>";
697
698 print "<div class=\"dlgSec\">".__("Apply actions")."</div>";
699
700 print "<div dojoType=\"dijit.Toolbar\">";
701
702 print "<div dojoType=\"dijit.form.DropDownButton\">".
703 "<span>" . __('Select')."</span>";
704 print "<div dojoType=\"dijit.Menu\" style=\"display: none;\">";
705 print "<div onclick=\"dijit.byId('filterEditDlg').selectActions(true)\"
706 dojoType=\"dijit.MenuItem\">".__('All')."</div>";
707 print "<div onclick=\"dijit.byId('filterEditDlg').selectActions(false)\"
708 dojoType=\"dijit.MenuItem\">".__('None')."</div>";
709 print "</div></div>";
710
711 print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').addAction()\">".
712 __('Add')."</button> ";
713
714 print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').deleteAction()\">".
715 __('Delete')."</button> ";
716
717 print "</div>";
718
719 print "<ul id='filterDlg_Actions'>";
720 # print "<li>No actions</li>";
721 print "</ul>";
722
723 /* print "<div class=\"dlgSec\">".__("Options")."</div>";
724 print "<div class=\"dlgSecCont\">"; */
725
726 print "<input dojoType=\"dijit.form.CheckBox\" type=\"checkbox\" name=\"enabled\" id=\"enabled\" checked=\"1\">
727 <label for=\"enabled\">".__('Enabled')."</label>";
728
729 print "<br/><input dojoType=\"dijit.form.CheckBox\" type=\"checkbox\" name=\"match_any_rule\" id=\"match_any_rule\">
730 <label for=\"match_any_rule\">".__('Match any rule')."</label>";
731
732 print "<br/><input dojoType=\"dijit.form.CheckBox\" type=\"checkbox\" name=\"inverse\" id=\"inverse\">
733 <label for=\"inverse\">".__('Inverse matching')."</label>";
734
735 // print "</div>";
736
737 print "<div class=\"dlgButtons\">";
738
739 print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').test()\">".
740 __('Test')."</button> ";
741
742 print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').execute()\">".
743 __('Create')."</button> ";
744
745 print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').hide()\">".
746 __('Cancel')."</button>";
747
748 print "</div>";
749
750 }
751
752 function newrule() {
753 $rule = json_decode($_REQUEST["rule"], true);
754
755 if ($rule) {
756 $reg_exp = htmlspecialchars($rule["reg_exp"]);
757 $filter_type = $rule["filter_type"];
758 $feed_id = $rule["feed_id"];
759 $inverse_checked = isset($rule["inverse"]) ? "checked" : "";
760 } else {
761 $reg_exp = "";
762 $filter_type = 1;
763 $feed_id = 0;
764 $inverse_checked = "";
765 }
766
767 if (strpos($feed_id, "CAT:") === 0) {
768 $feed_id = substr($feed_id, 4);
769 $cat_filter = true;
770 } else {
771 $cat_filter = false;
772 }
773
774
775 print "<form name='filter_new_rule_form' id='filter_new_rule_form'>";
776
777 $result = db_query($this->link, "SELECT id,description
778 FROM ttrss_filter_types WHERE id != 5 ORDER BY description");
779
780 $filter_types = array();
781
782 while ($line = db_fetch_assoc($result)) {
783 $filter_types[$line["id"]] = __($line["description"]);
784 }
785
786 print "<div class=\"dlgSec\">".__("Match")."</div>";
787
788 print "<div class=\"dlgSecCont\">";
789
790 print "<input dojoType=\"dijit.form.ValidationTextBox\"
791 required=\"true\" id=\"filterDlg_regExp\"
792 style=\"font-size : 16px; width : 20em;\"
793 name=\"reg_exp\" value=\"$reg_exp\"/>";
794
795 print "<hr/>" . __("on field") . " ";
796 print_select_hash("filter_type", $filter_type, $filter_types,
797 'dojoType="dijit.form.Select"');
798
799 print "<hr/>";
800
801 print __("in") . " ";
802
803 print "<span id='filterDlg_feeds'>";
804 print_feed_select($this->link, "feed_id",
805 $cat_filter ? "CAT:$feed_id" : $feed_id,
806 'dojoType="dijit.form.FilteringSelect"');
807 print "</span>";
808
809 print "<p>";
810
811 print "<input id=\"filterDlg_inverse\" dojoType=\"dijit.form.CheckBox\"
812 name=\"inverse\" $inverse_checked/>";
813
814 print "<label for=\"filterDlg_inverse\">".__("Inverse matching")."</label>";
815
816 print "</div>";
817
818 print "<div class=\"dlgButtons\">";
819
820 print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterNewRuleDlg').execute()\">".
821 ($rule ? __("Save rule") : __('Add rule'))."</button> ";
822
823 print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterNewRuleDlg').hide()\">".
824 __('Cancel')."</button>";
825
826 print "</div>";
827
828 print "</form>";
829 }
830
831 function newaction() {
832 $action = json_decode($_REQUEST["action"], true);
833
834 if ($action) {
835 $action_param = db_escape_string($this->link, $action["action_param"]);
836 $action_id = (int)$action["action_id"];
837 } else {
838 $action_param = "";
839 $action_id = 0;
840 }
841
842 print "<form name='filter_new_action_form' id='filter_new_action_form'>";
843
844 print "<div class=\"dlgSec\">".__("Perform Action")."</div>";
845
846 print "<div class=\"dlgSecCont\">";
847
848 print "<select name=\"action_id\" dojoType=\"dijit.form.Select\"
849 onchange=\"filterDlgCheckAction(this)\">";
850
851 $result = db_query($this->link, "SELECT id,description FROM ttrss_filter_actions
852 ORDER BY name");
853
854 while ($line = db_fetch_assoc($result)) {
855 $is_selected = ($line["id"] == $action_id) ? "selected='1'" : "";
856 printf("<option $is_selected value='%d'>%s</option>", $line["id"], __($line["description"]));
857 }
858
859 print "</select>";
860
861 $param_box_hidden = ($action_id == 7 || $action_id == 4 || $action_id == 6) ?
862 "" : "display : none";
863
864 $param_hidden = ($action_id == 4 || $action_id == 6) ?
865 "" : "display : none";
866
867 $label_param_hidden = ($action_id == 7) ? "" : "display : none";
868
869 print "<span id=\"filterDlg_paramBox\" style=\"$param_box_hidden\">";
870 print " " . __("with parameters:") . " ";
871 print "<input dojoType=\"dijit.form.TextBox\"
872 id=\"filterDlg_actionParam\" style=\"$param_hidden\"
873 name=\"action_param\" value=\"$action_param\">";
874
875 print_label_select($this->link, "action_param_label", $action_param,
876 "id=\"filterDlg_actionParamLabel\" style=\"$label_param_hidden\"
877 dojoType=\"dijit.form.Select\"");
878
879 print "</span>";
880
881 print "&nbsp;"; // tiny layout hack
882
883 print "</div>";
884
885 print "<div class=\"dlgButtons\">";
886
887 print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterNewActionDlg').execute()\">".
888 ($action ? __("Save action") : __('Add action'))."</button> ";
889
890 print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterNewActionDlg').hide()\">".
891 __('Cancel')."</button>";
892
893 print "</div>";
894
895 print "</form>";
896 }
897
898 private function getFilterName($id) {
899 $result = db_query($this->link,
900 "SELECT * FROM ttrss_filters2_rules WHERE filter_id = '$id' ORDER BY id
901 LIMIT 3");
902
903 $titles = array();
904 $count = 0;
905
906 while ($line = db_fetch_assoc($result)) {
907
908 if (sql_bool_to_bool($line["cat_filter"])) {
909 unset($line["cat_filter"]);
910 $line["feed_id"] = "CAT:" . (int)$line["cat_id"];
911 unset($line["cat_id"]);
912 }
913
914 if ($count < 2) {
915 array_push($titles, $this->getRuleName($line));
916 } else {
917 array_push($titles, "...");
918 break;
919 }
920 ++$count;
921 }
922
923 $result = db_query($this->link,
924 "SELECT * FROM ttrss_filters2_actions WHERE filter_id = '$id' ORDER BY id LIMIT 3");
925
926 $actions = array();
927 $count = 0;
928
929 while ($line = db_fetch_assoc($result)) {
930 if ($count < 2) {
931 array_push($actions, $this->getActionName($line));
932 } else {
933 array_push($actions, "...");
934 break;
935 }
936 ++$count;
937 }
938
939 return array(join(", ", $titles), join(", ", $actions));
940 }
941
942 function join() {
943 $ids = explode(",", db_escape_string($this->link, $_REQUEST["ids"]));
944
945 if (count($ids) > 1) {
946 $base_id = array_shift($ids);
947 $ids_str = join(",", $ids);
948
949 db_query($this->link, "BEGIN");
950 db_query($this->link, "UPDATE ttrss_filters2_rules
951 SET filter_id = '$base_id' WHERE filter_id IN ($ids_str)");
952 db_query($this->link, "UPDATE ttrss_filters2_actions
953 SET filter_id = '$base_id' WHERE filter_id IN ($ids_str)");
954
955 db_query($this->link, "DELETE FROM ttrss_filters2 WHERE id IN ($ids_str)");
956 db_query($this->link, "UPDATE ttrss_filters2 SET match_any_rule = true WHERE id = '$base_id'");
957
958 db_query($this->link, "COMMIT");
959
960 $this->optimizeFilter($base_id);
961
962 }
963 }
964
965 private function optimizeFilter($id) {
966 db_query($this->link, "BEGIN");
967 $result = db_query($this->link, "SELECT * FROM ttrss_filters2_actions
968 WHERE filter_id = '$id'");
969
970 $tmp = array();
971 $dupe_ids = array();
972
973 while ($line = db_fetch_assoc($result)) {
974 $id = $line["id"];
975 unset($line["id"]);
976
977 if (array_search($line, $tmp) === false) {
978 array_push($tmp, $line);
979 } else {
980 array_push($dupe_ids, $id);
981 }
982 }
983
984 if (count($dupe_ids) > 0) {
985 $ids_str = join(",", $dupe_ids);
986 db_query($this->link, "DELETE FROM ttrss_filters2_actions
987 WHERE id IN ($ids_str)");
988 }
989
990 $result = db_query($this->link, "SELECT * FROM ttrss_filters2_rules
991 WHERE filter_id = '$id'");
992
993 $tmp = array();
994 $dupe_ids = array();
995
996 while ($line = db_fetch_assoc($result)) {
997 $id = $line["id"];
998 unset($line["id"]);
999
1000 if (array_search($line, $tmp) === false) {
1001 array_push($tmp, $line);
1002 } else {
1003 array_push($dupe_ids, $id);
1004 }
1005 }
1006
1007 if (count($dupe_ids) > 0) {
1008 $ids_str = join(",", $dupe_ids);
1009 db_query($this->link, "DELETE FROM ttrss_filters2_rules
1010 WHERE id IN ($ids_str)");
1011 }
1012
1013 db_query($this->link, "COMMIT");
1014 }
1015 }
1016 ?>