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