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