]> git.wh0rd.org - tt-rss.git/blame - modules/pref-filters.php
filters: cast score expression as integer on save to prevent misscoring
[tt-rss.git] / modules / pref-filters.php
CommitLineData
ef8be8ea
AD
1<?php
2 function module_pref_filters($link) {
b4e75b2a
AD
3 $subop = $_REQUEST["subop"];
4 $quiet = $_REQUEST["quiet"];
ef8be8ea 5
a740f4b5
AD
6 if ($subop == "getfiltertree") {
7 $root = array();
8 $root['id'] = 'root';
9 $root['name'] = __('Filters');
10 $root['items'] = array();
11
b6c1201c 12 $result = db_query($link, "SELECT
a740f4b5
AD
13 ttrss_filters.id AS id,reg_exp,
14 ttrss_filter_types.name AS filter_type_name,
15 ttrss_filter_types.description AS filter_type_descr,
16 enabled,
17 inverse,
18 feed_id,
19 action_id,
20 filter_param,
21 filter_type,
22 ttrss_filter_actions.description AS action_description,
23 ttrss_feeds.title AS feed_title,
24 ttrss_filter_actions.name AS action_name,
25 ttrss_filters.action_param AS action_param
b6c1201c 26 FROM
a740f4b5
AD
27 ttrss_filter_types,ttrss_filter_actions,ttrss_filters LEFT JOIN
28 ttrss_feeds ON (ttrss_filters.feed_id = ttrss_feeds.id)
29 WHERE
30 filter_type = ttrss_filter_types.id AND
31 ttrss_filter_actions.id = action_id AND
32 ttrss_filters.owner_uid = ".$_SESSION["uid"]."
33 ORDER by action_description, reg_exp");
34
35 $cat = false;
36 $cur_action_description = "";
37
e43399b7
AD
38 if (db_num_rows($result) > 0) {
39
40 while ($line = db_fetch_assoc($result)) {
41 if ($cur_action_description != $line['action_description']) {
b6c1201c 42
e43399b7
AD
43 if ($cat)
44 array_push($root['items'], $cat);
b6c1201c 45
e43399b7
AD
46 $cat = array();
47 $cat['id'] = 'ACTION:' . $line['action_id'];
48 $cat['name'] = $line['action_description'];
49 $cat['items'] = array();
b6c1201c 50
e43399b7
AD
51 $cur_action_description = $line['action_description'];
52 }
b6c1201c
AD
53
54 if (array_search($line["action_name"],
e43399b7 55 array("score", "tag", "label")) === false) {
b6c1201c 56
e43399b7
AD
57 $line["action_param"] = '';
58 } else {
59 if ($line['action_name'] == 'label') {
b6c1201c 60
e43399b7
AD
61 $tmp_result = db_query($link, "SELECT fg_color, bg_color
62 FROM ttrss_labels2 WHERE caption = '".
63 db_escape_string($line["action_param"])."' AND
64 owner_uid = " . $_SESSION["uid"]);
b6c1201c 65
e43399b7
AD
66 if (db_num_rows($tmp_result) != 0) {
67 $fg_color = db_fetch_result($tmp_result, 0, "fg_color");
68 $bg_color = db_fetch_result($tmp_result, 0, "bg_color");
b6c1201c 69
e43399b7 70 $tmp = "<span class=\"labelColorIndicator\" style='color : $fg_color; background-color : $bg_color'>&alpha;</span> " . $line['action_param'];
b6c1201c 71
e43399b7
AD
72 $line['action_param'] = $tmp;
73 }
42623d7f 74 }
a740f4b5 75 }
b6c1201c 76
e43399b7
AD
77 $filter = array();
78 $filter['id'] = 'FILTER:' . $line['id'];
79 $filter['bare_id'] = $line['id'];
80 $filter['name'] = $line['reg_exp'];
81 $filter['type'] = $line['filter_type'];
82 $filter['enabled'] = sql_bool_to_bool($line['enabled']);
83 $filter['param'] = $line['action_param'];
84 $filter['inverse'] = sql_bool_to_bool($line['inverse']);
85 $filter['checkbox'] = false;
b6c1201c 86
e43399b7 87 if ($line['feed_id'])
b6c1201c
AD
88 $filter['feed'] = $line['feed_title'];
89
e43399b7 90 array_push($cat['items'], $filter);
a740f4b5 91 }
b6c1201c 92
e43399b7 93 array_push($root['items'], $cat);
a740f4b5
AD
94 }
95
a740f4b5
AD
96 $fl = array();
97 $fl['identifier'] = 'id';
98 $fl['label'] = 'name';
99 $fl['items'] = array($root);
100
101 print json_encode($fl);
102 return;
103 }
104
ef8be8ea
AD
105 if ($subop == "edit") {
106
b4e75b2a 107 $filter_id = db_escape_string($_REQUEST["id"]);
ef8be8ea 108
b6c1201c 109 $result = db_query($link,
ef8be8ea
AD
110 "SELECT * FROM ttrss_filters WHERE id = '$filter_id' AND owner_uid = " . $_SESSION["uid"]);
111
47439031 112 $reg_exp = htmlspecialchars(db_fetch_result($result, 0, "reg_exp"));
ef8be8ea
AD
113 $filter_type = db_fetch_result($result, 0, "filter_type");
114 $feed_id = db_fetch_result($result, 0, "feed_id");
115 $action_id = db_fetch_result($result, 0, "action_id");
073ca0e6 116 $action_param = db_fetch_result($result, 0, "action_param");
44d0e774 117 $filter_param = db_fetch_result($result, 0, "filter_param");
ef8be8ea
AD
118
119 $enabled = sql_bool_to_bool(db_fetch_result($result, 0, "enabled"));
3f2ff803 120 $inverse = sql_bool_to_bool(db_fetch_result($result, 0, "inverse"));
ef8be8ea 121
e6312f6c 122 print "<form id=\"filter_edit_form\" onsubmit='return false'>";
ef8be8ea 123
d90868d7
AD
124 print "<input dojoType=\"dijit.form.TextBox\" style=\"display : none\" name=\"op\" value=\"pref-filters\">";
125 print "<input dojoType=\"dijit.form.TextBox\" style=\"display : none\" name=\"id\" value=\"$filter_id\">";
b6c1201c
AD
126 print "<input dojoType=\"dijit.form.TextBox\" style=\"display : none\" name=\"subop\" value=\"editSave\">";
127
128 $result = db_query($link, "SELECT id,description
ef8be8ea 129 FROM ttrss_filter_types ORDER BY description");
b6c1201c 130
ef8be8ea 131 $filter_types = array();
b6c1201c 132
ef8be8ea
AD
133 while ($line = db_fetch_assoc($result)) {
134 //array_push($filter_types, $line["description"]);
89cb787e 135 $filter_types[$line["id"]] = __($line["description"]);
ef8be8ea
AD
136 }
137
10fa6615
AD
138 print "<div class=\"dlgSec\">".__("Match")."</div>";
139
140 print "<div class=\"dlgSecCont\">";
141
6e278744 142 if ($filter_type != 5) {
df00fb80 143 $date_ops_invisible = 'style="display : none"';
6e278744
AD
144 }
145
d90868d7 146 print "<span id=\"filterDlg_dateModBox\" $date_ops_invisible>";
6e278744 147 print __("Date") . " ";
44d0e774
AD
148
149 $filter_params = array(
150 "before" => __("before"),
151 "after" => __("after"));
152
153 print_select_hash("filter_date_modifier", $filter_param,
d90868d7 154 $filter_params, 'dojoType="dijit.form.Select"');
44d0e774
AD
155
156 print "&nbsp;</span>";
6e278744 157
d90868d7
AD
158 print "<input dojoType=\"dijit.form.ValidationTextBox\"
159 required=\"1\"
160 name=\"reg_exp\" style=\"font-size : 16px;\" value=\"$reg_exp\">";
10fa6615 161
b6c1201c 162 print "<span id=\"filterDlg_dateChkBox\" $date_ops_invisible>";
59216e08 163 print "&nbsp;<button dojoType=\"dijit.form.Button\" onclick=\"return filterDlgCheckDate()\">".
a918f5f9 164 __('Check it')."</button>";
6e278744
AD
165 print "</span>";
166
bcf5ed46 167 print "<hr/> " . __("on field") . " ";
6e278744 168 print_select_hash("filter_type", $filter_type, $filter_types,
d90868d7 169 'onchange="filterDlgCheckType(this)" dojoType="dijit.form.Select"');
10fa6615 170
bcf5ed46 171 print "<hr/>";
10fa6615
AD
172
173 print __("in") . " ";
d90868d7
AD
174 print_feed_select($link, "feed_id", $feed_id,
175 'dojoType="dijit.form.FilteringSelect"');
10fa6615
AD
176
177 print "</div>";
178
ecace165 179 print "<div class=\"dlgSec\">".__("Perform Action")."</div>";
10fa6615
AD
180
181 print "<div class=\"dlgSecCont\">";
182
d90868d7 183 print "<select name=\"action_id\" dojoType=\"dijit.form.Select\"
10fa6615 184 onchange=\"filterDlgCheckAction(this)\">";
b6c1201c
AD
185
186 $result = db_query($link, "SELECT id,description FROM ttrss_filter_actions
10fa6615
AD
187 ORDER BY name");
188
189 while ($line = db_fetch_assoc($result)) {
b6c1201c 190 $is_sel = ($line["id"] == $action_id) ? "selected=\"1\"" : "";
10fa6615
AD
191 printf("<option value='%d' $is_sel>%s</option>", $line["id"], __($line["description"]));
192 }
b6c1201c 193
10fa6615
AD
194 print "</select>";
195
ceb30ba4 196 $param_hidden = ($action_id == 4 || $action_id == 6 || $action_id == 7) ? "" : "display : none";
10fa6615 197
d90868d7 198 print "<span id=\"filterDlg_paramBox\" style=\"$param_hidden\">";
143a4973 199 print " " . __("with parameters:") . " ";
ceb30ba4
AD
200
201 $param_int_hidden = ($action_id != 7) ? "" : "display : none";
202
d90868d7
AD
203 print "<input style=\"$param_int_hidden\"
204 dojoType=\"dijit.form.TextBox\" id=\"filterDlg_actionParam\"
143a4973 205 name=\"action_param\" value=\"$action_param\">";
ceb30ba4
AD
206
207 $param_int_hidden = ($action_id == 7) ? "" : "display : none";
208
b6c1201c
AD
209 print_label_select($link, "action_param_label", $action_param,
210 "style=\"$param_int_hidden\"" .
d90868d7 211 'id="filterDlg_actionParamLabel" dojoType="dijit.form.Select"');
ceb30ba4 212
143a4973 213 print "</span>";
10fa6615 214
143a4973 215 print "&nbsp;"; // tiny layout hack
10fa6615
AD
216
217 print "</div>";
218
219 print "<div class=\"dlgSec\">".__("Options")."</div>";
220 print "<div class=\"dlgSecCont\">";
221
ecace165
AD
222 print "<div style=\"line-height : 100%\">";
223
10fa6615 224 if ($enabled) {
d90868d7 225 $checked = "checked=\"1\"";
10fa6615
AD
226 } else {
227 $checked = "";
228 }
229
d90868d7 230 print "<input dojoType=\"dijit.form.CheckBox\" type=\"checkbox\" name=\"enabled\" id=\"enabled\" $checked>
bcf5ed46 231 <label for=\"enabled\">".__('Enabled')."</label><hr/>";
10fa6615
AD
232
233 if ($inverse) {
d90868d7 234 $checked = "checked=\"1\"";
10fa6615
AD
235 } else {
236 $checked = "";
237 }
238
d90868d7 239 print "<input dojoType=\"dijit.form.CheckBox\" type=\"checkbox\" name=\"inverse\" id=\"inverse\" $checked>
10fa6615
AD
240 <label for=\"inverse\">".__('Inverse match')."</label>";
241
242 print "</div>";
ecace165 243 print "</div>";
10fa6615
AD
244
245 print "<div class=\"dlgButtons\">";
ef8be8ea 246
143a4973 247 print "<div style=\"float : left\">";
90e4e726 248 print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').removeFilter()\">".
577399e8 249 __('Remove')."</button>";
143a4973
AD
250 print "</div>";
251
59216e08 252 print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').execute()\">".
577399e8 253 __('Save')."</button> ";
ef8be8ea 254
59216e08 255 print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').hide()\">".
577399e8 256 __('Cancel')."</button>";
ef8be8ea 257
d90868d7 258 print "</div>";
ef8be8ea
AD
259
260 return;
261 }
262
263
264 if ($subop == "editSave") {
265
b8ffa322
AD
266 global $memcache;
267
268 if ($memcache) $memcache->flush();
269
b4e75b2a
AD
270 $reg_exp = db_escape_string(trim($_REQUEST["reg_exp"]));
271 $filter_type = db_escape_string(trim($_REQUEST["filter_type"]));
272 $filter_id = db_escape_string($_REQUEST["id"]);
273 $feed_id = db_escape_string($_REQUEST["feed_id"]);
b6c1201c
AD
274 $action_id = db_escape_string($_REQUEST["action_id"]);
275 $action_param = db_escape_string($_REQUEST["action_param"]);
276 $action_param_label = db_escape_string($_REQUEST["action_param_label"]);
b4e75b2a
AD
277 $enabled = checkbox_to_sql_bool(db_escape_string($_REQUEST["enabled"]));
278 $inverse = checkbox_to_sql_bool(db_escape_string($_REQUEST["inverse"]));
ef8be8ea 279
44d0e774 280 # for the time being, no other filters use params anyway...
b4e75b2a 281 $filter_param = db_escape_string($_REQUEST["filter_date_modifier"]);
44d0e774 282
ef8be8ea
AD
283 if (!$feed_id) {
284 $feed_id = 'NULL';
285 } else {
286 $feed_id = sprintf("'%s'", db_escape_string($feed_id));
287 }
ceb30ba4
AD
288
289 /* When processing 'assign label' filters, action_param_label dropbox
290 * overrides action_param */
291
292 if ($action_id == 7) {
293 $action_param = $action_param_label;
294 }
295
b6c1201c
AD
296 if ($action_id == 6) {
297 $action_param = (int) str_replace("+", "", $action_param);
298 }
299
300 $result = db_query($link, "UPDATE ttrss_filters SET
301 reg_exp = '$reg_exp',
ef8be8ea
AD
302 feed_id = $feed_id,
303 action_id = '$action_id',
304 filter_type = '$filter_type',
073ca0e6 305 enabled = $enabled,
3f2ff803 306 inverse = $inverse,
44d0e774
AD
307 action_param = '$action_param',
308 filter_param = '$filter_param'
4dccf1ed 309 WHERE id = '$filter_id' AND owner_uid = " . $_SESSION["uid"]);
ef8be8ea
AD
310 }
311
312 if ($subop == "remove") {
b6c1201c 313
a522a767 314 if ($memcache) $memcache->flush();
ef8be8ea 315
b4e75b2a 316 $ids = split(",", db_escape_string($_REQUEST["ids"]));
ef8be8ea 317
4dccf1ed
AD
318 foreach ($ids as $id) {
319 db_query($link, "DELETE FROM ttrss_filters WHERE id = '$id' AND owner_uid = ". $_SESSION["uid"]);
ef8be8ea 320 }
90e4e726 321 return;
ef8be8ea
AD
322 }
323
324 if ($subop == "add") {
a522a767
AD
325
326 if ($memcache) $memcache->flush();
327
b4e75b2a
AD
328 $regexp = db_escape_string(trim($_REQUEST["reg_exp"]));
329 $filter_type = db_escape_string(trim($_REQUEST["filter_type"]));
330 $feed_id = db_escape_string($_REQUEST["feed_id"]);
b6c1201c
AD
331 $action_id = db_escape_string($_REQUEST["action_id"]);
332 $action_param = db_escape_string($_REQUEST["action_param"]);
333 $action_param_label = db_escape_string($_REQUEST["action_param_label"]);
b4e75b2a 334 $inverse = checkbox_to_sql_bool(db_escape_string($_REQUEST["inverse"]));
4dccf1ed 335
44d0e774 336 # for the time being, no other filters use params anyway...
b4e75b2a 337 $filter_param = db_escape_string($_REQUEST["filter_date_modifier"]);
44d0e774 338
4dccf1ed
AD
339 if (!$regexp) return;
340
341 if (!$feed_id) {
342 $feed_id = 'NULL';
343 } else {
344 $feed_id = sprintf("'%s'", db_escape_string($feed_id));
345 }
346
db34a63e
AD
347 /* When processing 'assign label' filters, action_param_label dropbox
348 * overrides action_param */
349
350 if ($action_id == 7) {
351 $action_param = $action_param_label;
352 }
353
b6c1201c
AD
354 if ($action_id == 6) {
355 $action_param = (int) str_replace("+", "", $action_param);
356 }
357
4dccf1ed
AD
358 $result = db_query($link,
359 "INSERT INTO ttrss_filters (reg_exp,filter_type,owner_uid,feed_id,
b6c1201c
AD
360 action_id, action_param, inverse, filter_param)
361 VALUES
362 ('$regexp', '$filter_type','".$_SESSION["uid"]."',
a8732d0c 363 $feed_id, '$action_id', '$action_param', $inverse, '$filter_param')");
4dccf1ed 364
5e6f933a
AD
365 if (db_affected_rows($link, $result) != 0) {
366 print T_sprintf("Created filter <b>%s</b>", htmlspecialchars($regexp));
367 }
368
369 return;
ef8be8ea
AD
370 }
371
372 if ($quiet) return;
373
b4e75b2a 374 $sort = db_escape_string($_REQUEST["sort"]);
ef8be8ea
AD
375
376 if (!$sort || $sort == "undefined") {
377 $sort = "reg_exp";
378 }
379
b6c1201c 380 $result = db_query($link, "SELECT id,description
ef8be8ea
AD
381 FROM ttrss_filter_types ORDER BY description");
382
383 $filter_types = array();
384
385 while ($line = db_fetch_assoc($result)) {
386 //array_push($filter_types, $line["description"]);
387 $filter_types[$line["id"]] = $line["description"];
388 }
389
9c87d75c 390
b4e75b2a 391 $filter_search = db_escape_string($_REQUEST["search"]);
9c87d75c 392
b4e75b2a 393 if (array_key_exists("search", $_REQUEST)) {
9c87d75c
AD
394 $_SESSION["prefs_filter_search"] = $filter_search;
395 } else {
396 $filter_search = $_SESSION["prefs_filter_search"];
397 }
b6c1201c 398
8df7184c
AD
399 print "<div id=\"pref-filter-wrap\" dojoType=\"dijit.layout.BorderContainer\" gutters=\"false\">";
400 print "<div id=\"pref-filter-header\" dojoType=\"dijit.layout.ContentPane\" region=\"top\">";
401 print "<div id=\"pref-filter-toolbar\" dojoType=\"dijit.Toolbar\">";
9c87d75c 402
d69fa6d6
AD
403 print "<div dojoType=\"dijit.form.DropDownButton\">".
404 "<span>" . __('Select')."</span>";
405 print "<div dojoType=\"dijit.Menu\" style=\"display: none;\">";
b6c1201c 406 print "<div onclick=\"dijit.byId('filterTree').model.setAllChecked(true)\"
d69fa6d6 407 dojoType=\"dijit.MenuItem\">".__('All')."</div>";
b6c1201c 408 print "<div onclick=\"dijit.byId('filterTree').model.setAllChecked(false)\"
d69fa6d6
AD
409 dojoType=\"dijit.MenuItem\">".__('None')."</div>";
410 print "</div></div>";
b6c1201c 411
1985a5e0 412 print "<button dojoType=\"dijit.form.Button\" onclick=\"return quickAddFilter()\">".
b6c1201c 413 __('Create filter')."</button> ";
0d32b41e 414
1985a5e0 415 print "<button dojoType=\"dijit.form.Button\" onclick=\"return editSelectedFilter()\">".
c33f56f3 416 __('Edit')."</button> ";
ef8be8ea 417
1985a5e0 418 print "<button dojoType=\"dijit.form.Button\" onclick=\"return removeSelectedFilters()\">".
c33f56f3 419 __('Remove')."</button> ";
1e5548db 420
fe1c99af 421 if (defined('_ENABLE_FEED_DEBUGGING')) {
1985a5e0 422 print "<button dojoType=\"dijit.form.Button\" onclick=\"rescore_all_feeds()\">".
b6c1201c 423 __('Rescore articles')."</button> ";
fe1c99af 424 }
95a948a5 425
8df7184c
AD
426 print "</div>"; # toolbar
427 print "</div>"; # toolbar-frame
428 print "<div id=\"pref-filter-content\" dojoType=\"dijit.layout.ContentPane\" region=\"center\">";
429
a740f4b5
AD
430 print "<div id=\"filterlistLoading\">
431 <img src='images/indicator_tiny.gif'>".
432 __("Loading, please wait...")."</div>";
433
b6c1201c 434 print "<div dojoType=\"dojo.data.ItemFileWriteStore\" jsId=\"filterStore\"
a740f4b5
AD
435 url=\"backend.php?op=pref-filters&subop=getfiltertree\">
436 </div>
437 <div dojoType=\"lib.CheckBoxStoreModel\" jsId=\"filterModel\" store=\"filterStore\"
438 query=\"{id:'root'}\" rootId=\"root\" rootLabel=\"Feeds\"
439 childrenAttrs=\"items\" checkboxStrict=\"false\" checkboxAll=\"false\">
440 </div>
b6c1201c 441 <div dojoType=\"fox.PrefFilterTree\" id=\"filterTree\"
a740f4b5
AD
442 model=\"filterModel\" openOnClick=\"true\">
443 <script type=\"dojo/method\" event=\"onLoad\" args=\"item\">
444 Element.hide(\"filterlistLoading\");
445 </script>
8a097fa3
AD
446 <script type=\"dojo/method\" event=\"onClick\" args=\"item\">
447 var id = String(item.id);
448 var bare_id = id.substr(id.indexOf(':')+1);
449
450 if (id.match('FILTER:')) {
451 editFilter(bare_id);
b6c1201c 452 }
8a097fa3
AD
453 </script>
454
a740f4b5 455 </div>";
8df7184c
AD
456
457 print "</div>"; #pane
458 print "</div>"; #container
ef8be8ea 459 }
ceb30ba4 460
ef8be8ea 461?>