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