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