filters.php 38 KB

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