filters.php 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250
  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='popupOpenArticle($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] = "<i class=\"material-icons\" style='color : $bg_color; margin-right : 4px'>label</i>" . $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=\"dlgSecHoriz\">".__("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=\"dlgSecHoriz\">".__("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='Lists.onRowChecked(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=\"dlgSecHoriz\">".__("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='Lists.onRowChecked(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=\"alt-danger\" onclick=\"return dijit.byId('filterEditDlg').removeFilter()\">".
  382. __('Remove')."</button>";
  383. print "</div>";
  384. print "<button dojoType=\"dijit.form.Button\" class=\"alt-info\" onclick=\"return dijit.byId('filterEditDlg').test()\">".
  385. __('Test')."</button> ";
  386. print "<button dojoType=\"dijit.form.Button\" type=\"submit\" class=\"alt-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 Filters.quickAddFilter()\">".
  591. __('Create filter')."</button> ";
  592. print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterTree').joinSelectedFilters()\">".
  593. __('Combine')."</button> ";
  594. print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterTree').editSelectedFilter()\">".
  595. __('Edit')."</button> ";
  596. print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterTree').resetFilterOrder()\">".
  597. __('Reset sort order')."</button> ";
  598. print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterTree').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. dijit.byId('filterTree').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' onsubmit='return false'>";
  635. print_hidden("op", "pref-filters");
  636. print_hidden("method", "add");
  637. print_hidden("csrf_token", $_SESSION['csrf_token']);
  638. print "<div class=\"dlgSecHoriz\">".__("Caption")."</div>";
  639. print "<input required=\"true\" dojoType=\"dijit.form.ValidationTextBox\" style=\"width : 20em;\" name=\"title\" value=\"\">";
  640. print "<div class=\"dlgSecHoriz\">".__("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=\"dlgSecHoriz\">".__("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\" class=\"alt-info\" onclick=\"return dijit.byId('filterEditDlg').test()\">".
  688. __('Test')."</button> ";
  689. print "<button dojoType=\"dijit.form.Button\" type=\"submit\" class=\"alt-primary\" 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. style=\"font-size : 16px; width : 20em;\"
  720. name=\"reg_exp\" value=\"$reg_exp\"/>";
  721. print "<div dojoType=\"dijit.Tooltip\" connectId=\"filterDlg_regExp\" position=\"below\">
  722. ".__("Regular expression, without outer delimiters (i.e. slashes)")."
  723. </div>";
  724. print "<hr/>";
  725. print "<input id=\"filterDlg_inverse\" dojoType=\"dijit.form.CheckBox\"
  726. name=\"inverse\" $inverse_checked/>";
  727. print "<label for=\"filterDlg_inverse\">".__("Inverse regular expression matching")."</label>";
  728. print "<hr/>" . __("on field") . " ";
  729. print_select_hash("filter_type", $filter_type, $filter_types,
  730. 'dojoType="dijit.form.Select"');
  731. print "<hr/>";
  732. print __("in") . " ";
  733. print "<span id='filterDlg_feeds'>";
  734. print_feed_multi_select("feed_id",
  735. $feed_id,
  736. 'dojoType="dijit.form.MultiSelect"');
  737. print "</span>";
  738. print "</div>";
  739. print "<div class=\"dlgButtons\">";
  740. print "<div style=\"float : left\">
  741. <a class=\"visibleLink\" target=\"_blank\" href=\"http://tt-rss.org/wiki/ContentFilters\">".__("Wiki: Filters")."</a>
  742. </div>";
  743. print "<button dojoType=\"dijit.form.Button\" class=\"alt-primary \" type=\"submit\" onclick=\"return dijit.byId('filterNewRuleDlg').execute()\">".
  744. ($rule ? __("Save rule") : __('Add rule'))."</button> ";
  745. print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterNewRuleDlg').hide()\">".
  746. __('Cancel')."</button>";
  747. print "</div>";
  748. print "</form>";
  749. }
  750. function newaction() {
  751. $action = json_decode(clean($_REQUEST["action"]), true);
  752. if ($action) {
  753. $action_param = $action["action_param"];
  754. $action_id = (int)$action["action_id"];
  755. } else {
  756. $action_param = "";
  757. $action_id = 0;
  758. }
  759. print "<form name='filter_new_action_form' id='filter_new_action_form' onsubmit='return false;'>";
  760. print "<div class=\"dlgSec\">".__("Perform Action")."</div>";
  761. print "<div class=\"dlgSecCont\">";
  762. print "<select name=\"action_id\" dojoType=\"dijit.form.Select\"
  763. onchange=\"Filters.filterDlgCheckAction(this)\">";
  764. $res = $this->pdo->query("SELECT id,description FROM ttrss_filter_actions
  765. ORDER BY name");
  766. while ($line = $res->fetch()) {
  767. $is_selected = ($line["id"] == $action_id) ? "selected='1'" : "";
  768. printf("<option $is_selected value='%d'>%s</option>", $line["id"], __($line["description"]));
  769. }
  770. print "</select>";
  771. $param_box_hidden = ($action_id == 7 || $action_id == 4 || $action_id == 6 || $action_id == 9) ?
  772. "" : "display : none";
  773. $param_hidden = ($action_id == 4 || $action_id == 6) ?
  774. "" : "display : none";
  775. $label_param_hidden = ($action_id == 7) ? "" : "display : none";
  776. $plugin_param_hidden = ($action_id == 9) ? "" : "display : none";
  777. print "<span id=\"filterDlg_paramBox\" style=\"$param_box_hidden\">";
  778. print " ";
  779. //print " " . __("with parameters:") . " ";
  780. print "<input dojoType=\"dijit.form.TextBox\"
  781. id=\"filterDlg_actionParam\" style=\"$param_hidden\"
  782. name=\"action_param\" value=\"$action_param\">";
  783. print_label_select("action_param_label", $action_param,
  784. "id=\"filterDlg_actionParamLabel\" style=\"$label_param_hidden\"
  785. dojoType=\"dijit.form.Select\"");
  786. $filter_actions = PluginHost::getInstance()->get_filter_actions();
  787. $filter_action_hash = array();
  788. foreach ($filter_actions as $fclass => $factions) {
  789. foreach ($factions as $faction) {
  790. $filter_action_hash[$fclass . ":" . $faction["action"]] =
  791. $fclass . ": " . $faction["description"];
  792. }
  793. }
  794. if (count($filter_action_hash) == 0) {
  795. $filter_plugin_disabled = "disabled";
  796. $filter_action_hash["no-data"] = __("No actions available");
  797. } else {
  798. $filter_plugin_disabled = "";
  799. }
  800. print_select_hash("filterDlg_actionParamPlugin", $action_param, $filter_action_hash,
  801. "style=\"$plugin_param_hidden\" dojoType=\"dijit.form.Select\" $filter_plugin_disabled",
  802. "action_param_plugin");
  803. print "</span>";
  804. print "&nbsp;"; // tiny layout hack
  805. print "</div>";
  806. print "<div class=\"dlgButtons\">";
  807. print "<button dojoType=\"dijit.form.Button\" class=\"alt-primary\" type=\"submit\" onclick=\"return dijit.byId('filterNewActionDlg').execute()\">".
  808. ($action ? __("Save action") : __('Add action'))."</button> ";
  809. print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterNewActionDlg').hide()\">".
  810. __('Cancel')."</button>";
  811. print "</div>";
  812. print "</form>";
  813. }
  814. private function getFilterName($id) {
  815. $sth = $this->pdo->prepare(
  816. "SELECT title,match_any_rule,COUNT(DISTINCT r.id) AS num_rules,COUNT(DISTINCT a.id) AS num_actions
  817. FROM ttrss_filters2 AS f LEFT JOIN ttrss_filters2_rules AS r
  818. ON (r.filter_id = f.id)
  819. LEFT JOIN ttrss_filters2_actions AS a
  820. ON (a.filter_id = f.id) WHERE f.id = ? GROUP BY f.title, f.match_any_rule");
  821. $sth->execute([$id]);
  822. if ($row = $sth->fetch()) {
  823. $title = $row["title"];
  824. $num_rules = $row["num_rules"];
  825. $num_actions = $row["num_actions"];
  826. $match_any_rule = $row["match_any_rule"];
  827. if (!$title) $title = __("[No caption]");
  828. $title = sprintf(_ngettext("%s (%d rule)", "%s (%d rules)", (int) $num_rules), $title, $num_rules);
  829. $sth = $this->pdo->prepare("SELECT * FROM ttrss_filters2_actions
  830. WHERE filter_id = ? ORDER BY id LIMIT 1");
  831. $sth->execute([$id]);
  832. $actions = "";
  833. if ($line = $sth->fetch()) {
  834. $actions = $this->getActionName($line);
  835. $num_actions -= 1;
  836. }
  837. if ($match_any_rule) $title .= " (" . __("matches any rule") . ")";
  838. if ($num_actions > 0)
  839. $actions = sprintf(_ngettext("%s (+%d action)", "%s (+%d actions)", (int) $num_actions), $actions, $num_actions);
  840. return [$title, $actions];
  841. }
  842. return [];
  843. }
  844. function join() {
  845. $ids = explode(",", clean($_REQUEST["ids"]));
  846. if (count($ids) > 1) {
  847. $base_id = array_shift($ids);
  848. $ids_qmarks = arr_qmarks($ids);
  849. $this->pdo->beginTransaction();
  850. $sth = $this->pdo->prepare("UPDATE ttrss_filters2_rules
  851. SET filter_id = ? WHERE filter_id IN ($ids_qmarks)");
  852. $sth->execute(array_merge([$base_id], $ids));
  853. $sth = $this->pdo->prepare("UPDATE ttrss_filters2_actions
  854. SET filter_id = ? WHERE filter_id IN ($ids_qmarks)");
  855. $sth->execute(array_merge([$base_id], $ids));
  856. $sth = $this->pdo->prepare("DELETE FROM ttrss_filters2 WHERE id IN ($ids_qmarks)");
  857. $sth->execute($ids);
  858. $sth = $this->pdo->prepare("UPDATE ttrss_filters2 SET match_any_rule = true WHERE id = ?");
  859. $sth->execute([$base_id]);
  860. $this->pdo->commit();
  861. $this->optimizeFilter($base_id);
  862. }
  863. }
  864. private function optimizeFilter($id) {
  865. $this->pdo->beginTransaction();
  866. $sth = $this->pdo->prepare("SELECT * FROM ttrss_filters2_actions
  867. WHERE filter_id = ?");
  868. $sth->execute([$id]);
  869. $tmp = array();
  870. $dupe_ids = array();
  871. while ($line = $sth->fetch()) {
  872. $id = $line["id"];
  873. unset($line["id"]);
  874. if (array_search($line, $tmp) === false) {
  875. array_push($tmp, $line);
  876. } else {
  877. array_push($dupe_ids, $id);
  878. }
  879. }
  880. if (count($dupe_ids) > 0) {
  881. $ids_str = join(",", $dupe_ids);
  882. $this->pdo->query("DELETE FROM ttrss_filters2_actions WHERE id IN ($ids_str)");
  883. }
  884. $sth = $this->pdo->prepare("SELECT * FROM ttrss_filters2_rules
  885. WHERE filter_id = ?");
  886. $sth->execute([$id]);
  887. $tmp = array();
  888. $dupe_ids = array();
  889. while ($line = $sth->fetch()) {
  890. $id = $line["id"];
  891. unset($line["id"]);
  892. if (array_search($line, $tmp) === false) {
  893. array_push($tmp, $line);
  894. } else {
  895. array_push($dupe_ids, $id);
  896. }
  897. }
  898. if (count($dupe_ids) > 0) {
  899. $ids_str = join(",", $dupe_ids);
  900. $this->pdo->query("DELETE FROM ttrss_filters2_rules WHERE id IN ($ids_str)");
  901. }
  902. $this->pdo->commit();
  903. }
  904. }