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