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