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