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