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