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