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