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