filters.php 37 KB

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