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