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