filters.php 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089
  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. $this->dbh->query("UPDATE ttrss_filters2
  10. SET order_id = 0 WHERE owner_uid = " . $_SESSION["uid"]);
  11. return;
  12. }
  13. function savefilterorder() {
  14. $data = json_decode($_POST['payload'], true);
  15. #file_put_contents("/tmp/saveorder.json", $_POST['payload']);
  16. #$data = json_decode(file_get_contents("/tmp/saveorder.json"), true);
  17. if (!is_array($data['items']))
  18. $data['items'] = json_decode($data['items'], true);
  19. $index = 0;
  20. if (is_array($data) && is_array($data['items'])) {
  21. foreach ($data['items'][0]['items'] as $item) {
  22. $filter_id = (int) str_replace("FILTER:", "", $item['_reference']);
  23. if ($filter_id > 0) {
  24. $this->dbh->query("UPDATE ttrss_filters2 SET
  25. order_id = $index WHERE id = '$filter_id' AND
  26. owner_uid = " .$_SESSION["uid"]);
  27. ++$index;
  28. }
  29. }
  30. }
  31. return;
  32. }
  33. function testFilter() {
  34. $filter = array();
  35. $filter["enabled"] = true;
  36. $filter["match_any_rule"] = sql_bool_to_bool(
  37. checkbox_to_sql_bool($this->dbh->escape_string($_REQUEST["match_any_rule"])));
  38. $filter["inverse"] = sql_bool_to_bool(
  39. checkbox_to_sql_bool($this->dbh->escape_string($_REQUEST["inverse"])));
  40. $filter["rules"] = array();
  41. $result = $this->dbh->query("SELECT id,name FROM ttrss_filter_types");
  42. $filter_types = array();
  43. while ($line = $this->dbh->fetch_assoc($result)) {
  44. $filter_types[$line["id"]] = $line["name"];
  45. }
  46. $rctr = 0;
  47. foreach ($_REQUEST["rule"] AS $r) {
  48. $rule = json_decode($r, true);
  49. if ($rule && $rctr < 5) {
  50. $rule["type"] = $filter_types[$rule["filter_type"]];
  51. unset($rule["filter_type"]);
  52. if (strpos($rule["feed_id"], "CAT:") === 0) {
  53. $rule["cat_id"] = (int) substr($rule["feed_id"], 4);
  54. unset($rule["feed_id"]);
  55. }
  56. array_push($filter["rules"], $rule);
  57. ++$rctr;
  58. } else {
  59. break;
  60. }
  61. }
  62. $qfh_ret = queryFeedHeadlines(-4, 30, "", false, false, false,
  63. "date_entered DESC", 0, $_SESSION["uid"], $filter);
  64. $result = $qfh_ret[0];
  65. $found = 0;
  66. print __("Articles matching this filter:");
  67. print "<div class=\"filterTestHolder\">";
  68. print "<table width=\"100%\" cellspacing=\"0\" id=\"prefErrorFeedList\">";
  69. while ($line = $this->dbh->fetch_assoc($result)) {
  70. $line["content_preview"] = truncate_string(strip_tags($line["content"]), 100, '...');
  71. foreach (PluginHost::getInstance()->get_hooks(PluginHost::HOOK_QUERY_HEADLINES) as $p) {
  72. $line = $p->hook_query_headlines($line, 100);
  73. }
  74. $content_preview = $line["content_preview"];
  75. if ($line["feed_title"])
  76. $feed_title = $line["feed_title"];
  77. print "<tr>";
  78. print "<td width='5%' align='center'><input
  79. dojoType=\"dijit.form.CheckBox\" checked=\"1\"
  80. disabled=\"1\" type=\"checkbox\"></td>";
  81. print "<td>";
  82. print $line["title"];
  83. print "&nbsp;(";
  84. print "<b>" . $feed_title . "</b>";
  85. print "):&nbsp;";
  86. print "<span class=\"insensitive\">" . $content_preview . "</span>";
  87. print " " . mb_substr($line["date_entered"], 0, 16);
  88. print "</td></tr>";
  89. $found++;
  90. }
  91. if ($found == 0) {
  92. print "<tr><td align='center'>" .
  93. __("No recent articles matching this filter have been found.");
  94. print "</td></tr><tr><td class='insensitive' align='center'>";
  95. print __("Complex expressions might not give results while testing due to issues with database server regexp implementation.");
  96. print "</td></tr>";
  97. }
  98. print "</table></div>";
  99. print "<div style='text-align : center'>";
  100. print "<button dojoType=\"dijit.form.Button\" onclick=\"dijit.byId('filterTestDlg').hide()\">".
  101. __('Close this window')."</button>";
  102. print "</div>";
  103. }
  104. private function getfilterrules_concise($filter_id) {
  105. $result = $this->dbh->query("SELECT reg_exp,
  106. inverse,
  107. feed_id,
  108. cat_id,
  109. cat_filter,
  110. ttrss_filter_types.description AS field
  111. FROM
  112. ttrss_filters2_rules, ttrss_filter_types
  113. WHERE
  114. filter_id = '$filter_id' AND filter_type = ttrss_filter_types.id");
  115. $rv = "";
  116. while ($line = $this->dbh->fetch_assoc($result)) {
  117. $where = sql_bool_to_bool($line["cat_filter"]) ?
  118. getCategoryTitle($line["cat_id"]) : getFeedTitle($line["feed_id"]);
  119. # $where = $line["cat_id"] . "/" . $line["feed_id"];
  120. $inverse = sql_bool_to_bool($line["inverse"]) ? "inverse" : "";
  121. $rv .= "<span class='$inverse'>" . T_sprintf("%s on %s in %s %s",
  122. strip_tags($line["reg_exp"]),
  123. $line["field"],
  124. $where,
  125. sql_bool_to_bool($line["inverse"]) ? __("(inverse)") : "") . "</span>";
  126. }
  127. return $rv;
  128. }
  129. function getfiltertree() {
  130. $root = array();
  131. $root['id'] = 'root';
  132. $root['name'] = __('Filters');
  133. $root['items'] = array();
  134. $filter_search = $_SESSION["prefs_filter_search"];
  135. $result = $this->dbh->query("SELECT *,
  136. (SELECT action_param FROM ttrss_filters2_actions
  137. WHERE filter_id = ttrss_filters2.id ORDER BY id LIMIT 1) AS action_param,
  138. (SELECT action_id FROM ttrss_filters2_actions
  139. WHERE filter_id = ttrss_filters2.id ORDER BY id LIMIT 1) AS action_id,
  140. (SELECT description FROM ttrss_filter_actions
  141. WHERE id = (SELECT action_id FROM ttrss_filters2_actions
  142. WHERE filter_id = ttrss_filters2.id ORDER BY id LIMIT 1)) AS action_name,
  143. (SELECT reg_exp FROM ttrss_filters2_rules
  144. WHERE filter_id = ttrss_filters2.id ORDER BY id LIMIT 1) AS reg_exp
  145. FROM ttrss_filters2 WHERE
  146. owner_uid = ".$_SESSION["uid"]." ORDER BY order_id, title");
  147. $folder = array();
  148. $folder['items'] = array();
  149. while ($line = $this->dbh->fetch_assoc($result)) {
  150. $name = $this->getFilterName($line["id"]);
  151. $match_ok = false;
  152. if ($filter_search) {
  153. $rules_result = $this->dbh->query(
  154. "SELECT reg_exp FROM ttrss_filters2_rules WHERE filter_id = ".$line["id"]);
  155. while ($rule_line = $this->dbh->fetch_assoc($rules_result)) {
  156. if (mb_strpos($rule_line['reg_exp'], $filter_search) !== false) {
  157. $match_ok = true;
  158. break;
  159. }
  160. }
  161. }
  162. if ($line['action_id'] == 7) {
  163. $label_result = $this->dbh->query("SELECT fg_color, bg_color
  164. FROM ttrss_labels2 WHERE caption = '".$this->dbh->escape_string($line['action_param'])."' AND
  165. owner_uid = " . $_SESSION["uid"]);
  166. if ($this->dbh->num_rows($label_result) > 0) {
  167. $fg_color = $this->dbh->fetch_result($label_result, 0, "fg_color");
  168. $bg_color = $this->dbh->fetch_result($label_result, 0, "bg_color");
  169. $name[1] = "<span class=\"labelColorIndicator\" id=\"label-editor-indicator\" style='color : $fg_color; background-color : $bg_color; margin-right : 4px'>&alpha;</span>" . $name[1];
  170. }
  171. }
  172. $filter = array();
  173. $filter['id'] = 'FILTER:' . $line['id'];
  174. $filter['bare_id'] = $line['id'];
  175. $filter['name'] = $name[0];
  176. $filter['param'] = $name[1];
  177. $filter['checkbox'] = false;
  178. $filter['enabled'] = sql_bool_to_bool($line["enabled"]);
  179. $filter['rules'] = $this->getfilterrules_concise($line['id']);
  180. if (!$filter_search || $match_ok) {
  181. array_push($folder['items'], $filter);
  182. }
  183. }
  184. /* if (count($folder['items']) > 0) {
  185. array_push($root['items'], $folder);
  186. } */
  187. $root['items'] = $folder['items'];
  188. $fl = array();
  189. $fl['identifier'] = 'id';
  190. $fl['label'] = 'name';
  191. $fl['items'] = array($root);
  192. print json_encode($fl);
  193. return;
  194. }
  195. function edit() {
  196. $filter_id = $this->dbh->escape_string($_REQUEST["id"]);
  197. $result = $this->dbh->query(
  198. "SELECT * FROM ttrss_filters2 WHERE id = '$filter_id' AND owner_uid = " . $_SESSION["uid"]);
  199. $enabled = sql_bool_to_bool($this->dbh->fetch_result($result, 0, "enabled"));
  200. $match_any_rule = sql_bool_to_bool($this->dbh->fetch_result($result, 0, "match_any_rule"));
  201. $inverse = sql_bool_to_bool($this->dbh->fetch_result($result, 0, "inverse"));
  202. $title = htmlspecialchars($this->dbh->fetch_result($result, 0, "title"));
  203. print "<form id=\"filter_edit_form\" onsubmit='return false'>";
  204. print "<input dojoType=\"dijit.form.TextBox\" style=\"display : none\" name=\"op\" value=\"pref-filters\">";
  205. print "<input dojoType=\"dijit.form.TextBox\" style=\"display : none\" name=\"id\" value=\"$filter_id\">";
  206. print "<input dojoType=\"dijit.form.TextBox\" style=\"display : none\" name=\"method\" value=\"editSave\">";
  207. print "<input dojoType=\"dijit.form.TextBox\" style=\"display : none\" name=\"csrf_token\" value=\"".$_SESSION['csrf_token']."\">";
  208. print "<div class=\"dlgSec\">".__("Caption")."</div>";
  209. print "<input required=\"true\" dojoType=\"dijit.form.ValidationTextBox\" style=\"width : 20em;\" name=\"title\" value=\"$title\">";
  210. print "</div>";
  211. print "<div class=\"dlgSec\">".__("Match")."</div>";
  212. print "<div dojoType=\"dijit.Toolbar\">";
  213. print "<div dojoType=\"dijit.form.DropDownButton\">".
  214. "<span>" . __('Select')."</span>";
  215. print "<div dojoType=\"dijit.Menu\" style=\"display: none;\">";
  216. print "<div onclick=\"dijit.byId('filterEditDlg').selectRules(true)\"
  217. dojoType=\"dijit.MenuItem\">".__('All')."</div>";
  218. print "<div onclick=\"dijit.byId('filterEditDlg').selectRules(false)\"
  219. dojoType=\"dijit.MenuItem\">".__('None')."</div>";
  220. print "</div></div>";
  221. print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').addRule()\">".
  222. __('Add')."</button> ";
  223. print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').deleteRule()\">".
  224. __('Delete')."</button> ";
  225. print "</div>";
  226. print "<ul id='filterDlg_Matches'>";
  227. $rules_result = $this->dbh->query("SELECT * FROM ttrss_filters2_rules
  228. WHERE filter_id = '$filter_id' ORDER BY reg_exp, id");
  229. while ($line = $this->dbh->fetch_assoc($rules_result)) {
  230. if (sql_bool_to_bool($line["cat_filter"])) {
  231. $line["feed_id"] = "CAT:" . (int)$line["cat_id"];
  232. }
  233. unset($line["cat_filter"]);
  234. unset($line["cat_id"]);
  235. unset($line["filter_id"]);
  236. unset($line["id"]);
  237. if (!sql_bool_to_bool($line["inverse"])) unset($line["inverse"]);
  238. $data = htmlspecialchars(json_encode($line));
  239. print "<li><input dojoType='dijit.form.CheckBox' type='checkbox' onclick='toggleSelectListRow2(this)'>".
  240. "<span onclick=\"dijit.byId('filterEditDlg').editRule(this)\">".$this->getRuleName($line)."</span>".
  241. "<input type='hidden' name='rule[]' value=\"$data\"/></li>";
  242. }
  243. print "</ul>";
  244. print "</div>";
  245. print "<div class=\"dlgSec\">".__("Apply actions")."</div>";
  246. print "<div dojoType=\"dijit.Toolbar\">";
  247. print "<div dojoType=\"dijit.form.DropDownButton\">".
  248. "<span>" . __('Select')."</span>";
  249. print "<div dojoType=\"dijit.Menu\" style=\"display: none;\">";
  250. print "<div onclick=\"dijit.byId('filterEditDlg').selectActions(true)\"
  251. dojoType=\"dijit.MenuItem\">".__('All')."</div>";
  252. print "<div onclick=\"dijit.byId('filterEditDlg').selectActions(false)\"
  253. dojoType=\"dijit.MenuItem\">".__('None')."</div>";
  254. print "</div></div>";
  255. print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').addAction()\">".
  256. __('Add')."</button> ";
  257. print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').deleteAction()\">".
  258. __('Delete')."</button> ";
  259. print "</div>";
  260. print "<ul id='filterDlg_Actions'>";
  261. $actions_result = $this->dbh->query("SELECT * FROM ttrss_filters2_actions
  262. WHERE filter_id = '$filter_id' ORDER BY id");
  263. while ($line = $this->dbh->fetch_assoc($actions_result)) {
  264. $line["action_param_label"] = $line["action_param"];
  265. unset($line["filter_id"]);
  266. unset($line["id"]);
  267. $data = htmlspecialchars(json_encode($line));
  268. print "<li><input dojoType='dijit.form.CheckBox' type='checkbox' onclick='toggleSelectListRow2(this)'>".
  269. "<span onclick=\"dijit.byId('filterEditDlg').editAction(this)\">".$this->getActionName($line)."</span>".
  270. "<input type='hidden' name='action[]' value=\"$data\"/></li>";
  271. }
  272. print "</ul>";
  273. print "</div>";
  274. if ($enabled) {
  275. $checked = "checked=\"1\"";
  276. } else {
  277. $checked = "";
  278. }
  279. print "<input dojoType=\"dijit.form.CheckBox\" type=\"checkbox\" name=\"enabled\" id=\"enabled\" $checked>
  280. <label for=\"enabled\">".__('Enabled')."</label>";
  281. if ($match_any_rule) {
  282. $checked = "checked=\"1\"";
  283. } else {
  284. $checked = "";
  285. }
  286. print "<br/><input dojoType=\"dijit.form.CheckBox\" type=\"checkbox\" name=\"match_any_rule\" id=\"match_any_rule\" $checked>
  287. <label for=\"match_any_rule\">".__('Match any rule')."</label>";
  288. if ($inverse) {
  289. $checked = "checked=\"1\"";
  290. } else {
  291. $checked = "";
  292. }
  293. print "<br/><input dojoType=\"dijit.form.CheckBox\" type=\"checkbox\" name=\"inverse\" id=\"inverse\" $checked>
  294. <label for=\"inverse\">".__('Inverse matching')."</label>";
  295. print "<p/>";
  296. print "<div class=\"dlgButtons\">";
  297. print "<div style=\"float : left\">";
  298. print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').removeFilter()\">".
  299. __('Remove')."</button>";
  300. print "</div>";
  301. print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').test()\">".
  302. __('Test')."</button> ";
  303. print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').execute()\">".
  304. __('Save')."</button> ";
  305. print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').hide()\">".
  306. __('Cancel')."</button>";
  307. print "</div>";
  308. }
  309. private function getRuleName($rule) {
  310. if (!$rule) $rule = json_decode($_REQUEST["rule"], true);
  311. $feed_id = $rule["feed_id"];
  312. if (strpos($feed_id, "CAT:") === 0) {
  313. $feed_id = (int) substr($feed_id, 4);
  314. $feed = getCategoryTitle($feed_id);
  315. } else {
  316. $feed_id = (int) $feed_id;
  317. if ($rule["feed_id"])
  318. $feed = getFeedTitle((int)$rule["feed_id"]);
  319. else
  320. $feed = __("All feeds");
  321. }
  322. $result = $this->dbh->query("SELECT description FROM ttrss_filter_types
  323. WHERE id = ".(int)$rule["filter_type"]);
  324. $filter_type = $this->dbh->fetch_result($result, 0, "description");
  325. $inverse = isset($rule["inverse"]) ? "inverse" : "";
  326. return "<span class='filterRule $inverse'>" .
  327. T_sprintf("%s on %s in %s %s", strip_tags($rule["reg_exp"]),
  328. $filter_type, $feed, isset($rule["inverse"]) ? __("(inverse)") : "") . "</span>";
  329. }
  330. function printRuleName() {
  331. print $this->getRuleName(json_decode($_REQUEST["rule"], true));
  332. }
  333. private function getActionName($action) {
  334. $result = $this->dbh->query("SELECT description FROM
  335. ttrss_filter_actions WHERE id = " .(int)$action["action_id"]);
  336. $title = __($this->dbh->fetch_result($result, 0, "description"));
  337. if ($action["action_id"] == 4 || $action["action_id"] == 6 ||
  338. $action["action_id"] == 7)
  339. $title .= ": " . $action["action_param"];
  340. return $title;
  341. }
  342. function printActionName() {
  343. print $this->getActionName(json_decode($_REQUEST["action"], true));
  344. }
  345. function editSave() {
  346. if ($_REQUEST["savemode"] && $_REQUEST["savemode"] == "test") {
  347. return $this->testFilter();
  348. }
  349. # print_r($_REQUEST);
  350. $filter_id = $this->dbh->escape_string($_REQUEST["id"]);
  351. $enabled = checkbox_to_sql_bool($this->dbh->escape_string($_REQUEST["enabled"]));
  352. $match_any_rule = checkbox_to_sql_bool($this->dbh->escape_string($_REQUEST["match_any_rule"]));
  353. $inverse = checkbox_to_sql_bool($this->dbh->escape_string($_REQUEST["inverse"]));
  354. $title = $this->dbh->escape_string($_REQUEST["title"]);
  355. $this->dbh->query("UPDATE ttrss_filters2 SET enabled = $enabled,
  356. match_any_rule = $match_any_rule,
  357. inverse = $inverse,
  358. title = '$title'
  359. WHERE id = '$filter_id'
  360. AND owner_uid = ". $_SESSION["uid"]);
  361. $this->saveRulesAndActions($filter_id);
  362. }
  363. function remove() {
  364. $ids = explode(",", $this->dbh->escape_string($_REQUEST["ids"]));
  365. foreach ($ids as $id) {
  366. $this->dbh->query("DELETE FROM ttrss_filters2 WHERE id = '$id' AND owner_uid = ". $_SESSION["uid"]);
  367. }
  368. }
  369. private function saveRulesAndActions($filter_id) {
  370. $this->dbh->query("DELETE FROM ttrss_filters2_rules WHERE filter_id = '$filter_id'");
  371. $this->dbh->query("DELETE FROM ttrss_filters2_actions WHERE filter_id = '$filter_id'");
  372. if ($filter_id) {
  373. /* create rules */
  374. $rules = array();
  375. $actions = array();
  376. foreach ($_REQUEST["rule"] as $rule) {
  377. $rule = json_decode($rule, true);
  378. unset($rule["id"]);
  379. if (array_search($rule, $rules) === false) {
  380. array_push($rules, $rule);
  381. }
  382. }
  383. foreach ($_REQUEST["action"] as $action) {
  384. $action = json_decode($action, true);
  385. unset($action["id"]);
  386. if (array_search($action, $actions) === false) {
  387. array_push($actions, $action);
  388. }
  389. }
  390. foreach ($rules as $rule) {
  391. if ($rule) {
  392. $reg_exp = strip_tags($this->dbh->escape_string(trim($rule["reg_exp"])));
  393. $inverse = isset($rule["inverse"]) ? "true" : "false";
  394. $filter_type = (int) $this->dbh->escape_string(trim($rule["filter_type"]));
  395. $feed_id = $this->dbh->escape_string(trim($rule["feed_id"]));
  396. if (strpos($feed_id, "CAT:") === 0) {
  397. $cat_filter = bool_to_sql_bool(true);
  398. $cat_id = (int) substr($feed_id, 4);
  399. $feed_id = "NULL";
  400. if (!$cat_id) $cat_id = "NULL"; // Uncategorized
  401. } else {
  402. $cat_filter = bool_to_sql_bool(false);
  403. $feed_id = (int) $feed_id;
  404. $cat_id = "NULL";
  405. if (!$feed_id) $feed_id = "NULL"; // Uncategorized
  406. }
  407. $query = "INSERT INTO ttrss_filters2_rules
  408. (filter_id, reg_exp,filter_type,feed_id,cat_id,cat_filter,inverse) VALUES
  409. ('$filter_id', '$reg_exp', '$filter_type', $feed_id, $cat_id, $cat_filter, $inverse)";
  410. $this->dbh->query($query);
  411. }
  412. }
  413. foreach ($actions as $action) {
  414. if ($action) {
  415. $action_id = (int) $this->dbh->escape_string($action["action_id"]);
  416. $action_param = $this->dbh->escape_string($action["action_param"]);
  417. $action_param_label = $this->dbh->escape_string($action["action_param_label"]);
  418. if ($action_id == 7) {
  419. $action_param = $action_param_label;
  420. }
  421. if ($action_id == 6) {
  422. $action_param = (int) str_replace("+", "", $action_param);
  423. }
  424. $query = "INSERT INTO ttrss_filters2_actions
  425. (filter_id, action_id, action_param) VALUES
  426. ('$filter_id', '$action_id', '$action_param')";
  427. $this->dbh->query($query);
  428. }
  429. }
  430. }
  431. }
  432. function add() {
  433. if ($_REQUEST["savemode"] && $_REQUEST["savemode"] == "test") {
  434. return $this->testFilter();
  435. }
  436. # print_r($_REQUEST);
  437. $enabled = checkbox_to_sql_bool($_REQUEST["enabled"]);
  438. $match_any_rule = checkbox_to_sql_bool($_REQUEST["match_any_rule"]);
  439. $title = $this->dbh->escape_string($_REQUEST["title"]);
  440. $inverse = checkbox_to_sql_bool($_REQUEST["inverse"]);
  441. $this->dbh->query("BEGIN");
  442. /* create base filter */
  443. $result = $this->dbh->query("INSERT INTO ttrss_filters2
  444. (owner_uid, match_any_rule, enabled, title, inverse) VALUES
  445. (".$_SESSION["uid"].",$match_any_rule,$enabled, '$title', $inverse)");
  446. $result = $this->dbh->query("SELECT MAX(id) AS id FROM ttrss_filters2
  447. WHERE owner_uid = ".$_SESSION["uid"]);
  448. $filter_id = $this->dbh->fetch_result($result, 0, "id");
  449. $this->saveRulesAndActions($filter_id);
  450. $this->dbh->query("COMMIT");
  451. }
  452. function index() {
  453. $sort = $this->dbh->escape_string($_REQUEST["sort"]);
  454. if (!$sort || $sort == "undefined") {
  455. $sort = "reg_exp";
  456. }
  457. $filter_search = $this->dbh->escape_string($_REQUEST["search"]);
  458. if (array_key_exists("search", $_REQUEST)) {
  459. $_SESSION["prefs_filter_search"] = $filter_search;
  460. } else {
  461. $filter_search = $_SESSION["prefs_filter_search"];
  462. }
  463. print "<div id=\"pref-filter-wrap\" dojoType=\"dijit.layout.BorderContainer\" gutters=\"false\">";
  464. print "<div id=\"pref-filter-header\" dojoType=\"dijit.layout.ContentPane\" region=\"top\">";
  465. print "<div id=\"pref-filter-toolbar\" dojoType=\"dijit.Toolbar\">";
  466. $filter_search = $this->dbh->escape_string($_REQUEST["search"]);
  467. if (array_key_exists("search", $_REQUEST)) {
  468. $_SESSION["prefs_filter_search"] = $filter_search;
  469. } else {
  470. $filter_search = $_SESSION["prefs_filter_search"];
  471. }
  472. print "<div style='float : right; padding-right : 4px;'>
  473. <input dojoType=\"dijit.form.TextBox\" id=\"filter_search\" size=\"20\" type=\"search\"
  474. value=\"$filter_search\">
  475. <button dojoType=\"dijit.form.Button\" onclick=\"updateFilterList()\">".
  476. __('Search')."</button>
  477. </div>";
  478. print "<div dojoType=\"dijit.form.DropDownButton\">".
  479. "<span>" . __('Select')."</span>";
  480. print "<div dojoType=\"dijit.Menu\" style=\"display: none;\">";
  481. print "<div onclick=\"dijit.byId('filterTree').model.setAllChecked(true)\"
  482. dojoType=\"dijit.MenuItem\">".__('All')."</div>";
  483. print "<div onclick=\"dijit.byId('filterTree').model.setAllChecked(false)\"
  484. dojoType=\"dijit.MenuItem\">".__('None')."</div>";
  485. print "</div></div>";
  486. print "<button dojoType=\"dijit.form.Button\" onclick=\"return quickAddFilter()\">".
  487. __('Create filter')."</button> ";
  488. print "<button dojoType=\"dijit.form.Button\" onclick=\"return joinSelectedFilters()\">".
  489. __('Combine')."</button> ";
  490. print "<button dojoType=\"dijit.form.Button\" onclick=\"return editSelectedFilter()\">".
  491. __('Edit')."</button> ";
  492. print "<button dojoType=\"dijit.form.Button\" onclick=\"return resetFilterOrder()\">".
  493. __('Reset sort order')."</button> ";
  494. print "<button dojoType=\"dijit.form.Button\" onclick=\"return removeSelectedFilters()\">".
  495. __('Remove')."</button> ";
  496. if (defined('_ENABLE_FEED_DEBUGGING')) {
  497. print "<button dojoType=\"dijit.form.Button\" onclick=\"rescore_all_feeds()\">".
  498. __('Rescore articles')."</button> ";
  499. }
  500. print "</div>"; # toolbar
  501. print "</div>"; # toolbar-frame
  502. print "<div id=\"pref-filter-content\" dojoType=\"dijit.layout.ContentPane\" region=\"center\">";
  503. print "<div id=\"filterlistLoading\">
  504. <img src='images/indicator_tiny.gif'>".
  505. __("Loading, please wait...")."</div>";
  506. print "<div dojoType=\"fox.PrefFilterStore\" jsId=\"filterStore\"
  507. url=\"backend.php?op=pref-filters&method=getfiltertree\">
  508. </div>
  509. <div dojoType=\"lib.CheckBoxStoreModel\" jsId=\"filterModel\" store=\"filterStore\"
  510. query=\"{id:'root'}\" rootId=\"root\" rootLabel=\"Filters\"
  511. childrenAttrs=\"items\" checkboxStrict=\"false\" checkboxAll=\"false\">
  512. </div>
  513. <div dojoType=\"fox.PrefFilterTree\" id=\"filterTree\"
  514. dndController=\"dijit.tree.dndSource\"
  515. betweenThreshold=\"5\"
  516. model=\"filterModel\" openOnClick=\"true\">
  517. <script type=\"dojo/method\" event=\"onLoad\" args=\"item\">
  518. Element.hide(\"filterlistLoading\");
  519. </script>
  520. <script type=\"dojo/method\" event=\"onClick\" args=\"item\">
  521. var id = String(item.id);
  522. var bare_id = id.substr(id.indexOf(':')+1);
  523. if (id.match('FILTER:')) {
  524. editFilter(bare_id);
  525. }
  526. </script>
  527. </div>";
  528. print "</div>"; #pane
  529. PluginHost::getInstance()->run_hooks(PluginHost::HOOK_PREFS_TAB,
  530. "hook_prefs_tab", "prefFilters");
  531. print "</div>"; #container
  532. }
  533. function newfilter() {
  534. print "<form name='filter_new_form' id='filter_new_form'>";
  535. print "<input dojoType=\"dijit.form.TextBox\" style=\"display : none\" name=\"op\" value=\"pref-filters\">";
  536. print "<input dojoType=\"dijit.form.TextBox\" style=\"display : none\" name=\"method\" value=\"add\">";
  537. print "<input dojoType=\"dijit.form.TextBox\" style=\"display : none\" name=\"csrf_token\" value=\"".$_SESSION['csrf_token']."\">";
  538. print "<div class=\"dlgSec\">".__("Caption")."</div>";
  539. print "<input required=\"true\" dojoType=\"dijit.form.ValidationTextBox\" style=\"width : 20em;\" name=\"title\" value=\"\">";
  540. print "<div class=\"dlgSec\">".__("Match")."</div>";
  541. print "<div dojoType=\"dijit.Toolbar\">";
  542. print "<div dojoType=\"dijit.form.DropDownButton\">".
  543. "<span>" . __('Select')."</span>";
  544. print "<div dojoType=\"dijit.Menu\" style=\"display: none;\">";
  545. print "<div onclick=\"dijit.byId('filterEditDlg').selectRules(true)\"
  546. dojoType=\"dijit.MenuItem\">".__('All')."</div>";
  547. print "<div onclick=\"dijit.byId('filterEditDlg').selectRules(false)\"
  548. dojoType=\"dijit.MenuItem\">".__('None')."</div>";
  549. print "</div></div>";
  550. print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').addRule()\">".
  551. __('Add')."</button> ";
  552. print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').deleteRule()\">".
  553. __('Delete')."</button> ";
  554. print "</div>";
  555. print "<ul id='filterDlg_Matches'>";
  556. # print "<li>No rules</li>";
  557. print "</ul>";
  558. print "</div>";
  559. print "<div class=\"dlgSec\">".__("Apply actions")."</div>";
  560. print "<div dojoType=\"dijit.Toolbar\">";
  561. print "<div dojoType=\"dijit.form.DropDownButton\">".
  562. "<span>" . __('Select')."</span>";
  563. print "<div dojoType=\"dijit.Menu\" style=\"display: none;\">";
  564. print "<div onclick=\"dijit.byId('filterEditDlg').selectActions(true)\"
  565. dojoType=\"dijit.MenuItem\">".__('All')."</div>";
  566. print "<div onclick=\"dijit.byId('filterEditDlg').selectActions(false)\"
  567. dojoType=\"dijit.MenuItem\">".__('None')."</div>";
  568. print "</div></div>";
  569. print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').addAction()\">".
  570. __('Add')."</button> ";
  571. print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').deleteAction()\">".
  572. __('Delete')."</button> ";
  573. print "</div>";
  574. print "<ul id='filterDlg_Actions'>";
  575. # print "<li>No actions</li>";
  576. print "</ul>";
  577. /* print "<div class=\"dlgSec\">".__("Options")."</div>";
  578. print "<div class=\"dlgSecCont\">"; */
  579. print "<input dojoType=\"dijit.form.CheckBox\" type=\"checkbox\" name=\"enabled\" id=\"enabled\" checked=\"1\">
  580. <label for=\"enabled\">".__('Enabled')."</label>";
  581. print "<br/><input dojoType=\"dijit.form.CheckBox\" type=\"checkbox\" name=\"match_any_rule\" id=\"match_any_rule\">
  582. <label for=\"match_any_rule\">".__('Match any rule')."</label>";
  583. print "<br/><input dojoType=\"dijit.form.CheckBox\" type=\"checkbox\" name=\"inverse\" id=\"inverse\">
  584. <label for=\"inverse\">".__('Inverse matching')."</label>";
  585. // print "</div>";
  586. print "<div class=\"dlgButtons\">";
  587. print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').test()\">".
  588. __('Test')."</button> ";
  589. print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').execute()\">".
  590. __('Create')."</button> ";
  591. print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').hide()\">".
  592. __('Cancel')."</button>";
  593. print "</div>";
  594. }
  595. function newrule() {
  596. $rule = json_decode($_REQUEST["rule"], true);
  597. if ($rule) {
  598. $reg_exp = htmlspecialchars($rule["reg_exp"]);
  599. $filter_type = $rule["filter_type"];
  600. $feed_id = $rule["feed_id"];
  601. $inverse_checked = isset($rule["inverse"]) ? "checked" : "";
  602. } else {
  603. $reg_exp = "";
  604. $filter_type = 1;
  605. $feed_id = 0;
  606. $inverse_checked = "";
  607. }
  608. if (strpos($feed_id, "CAT:") === 0) {
  609. $feed_id = substr($feed_id, 4);
  610. $cat_filter = true;
  611. } else {
  612. $cat_filter = false;
  613. }
  614. print "<form name='filter_new_rule_form' id='filter_new_rule_form'>";
  615. $result = $this->dbh->query("SELECT id,description
  616. FROM ttrss_filter_types WHERE id != 5 ORDER BY description");
  617. $filter_types = array();
  618. while ($line = $this->dbh->fetch_assoc($result)) {
  619. $filter_types[$line["id"]] = __($line["description"]);
  620. }
  621. print "<div class=\"dlgSec\">".__("Match")."</div>";
  622. print "<div class=\"dlgSecCont\">";
  623. print "<input dojoType=\"dijit.form.ValidationTextBox\"
  624. required=\"true\" id=\"filterDlg_regExp\"
  625. style=\"font-size : 16px; width : 20em;\"
  626. name=\"reg_exp\" value=\"$reg_exp\"/>";
  627. print "<hr/>";
  628. print "<input id=\"filterDlg_inverse\" dojoType=\"dijit.form.CheckBox\"
  629. name=\"inverse\" $inverse_checked/>";
  630. print "<label for=\"filterDlg_inverse\">".__("Inverse regular expression matching")."</label>";
  631. print "<hr/>" . __("on field") . " ";
  632. print_select_hash("filter_type", $filter_type, $filter_types,
  633. 'dojoType="dijit.form.Select"');
  634. print "<hr/>";
  635. print __("in") . " ";
  636. print "<span id='filterDlg_feeds'>";
  637. print_feed_select("feed_id",
  638. $cat_filter ? "CAT:$feed_id" : $feed_id,
  639. 'dojoType="dijit.form.FilteringSelect"');
  640. print "</span>";
  641. print "</div>";
  642. print "<div class=\"dlgButtons\">";
  643. print "<div style=\"float : left\">
  644. <a class=\"visibleLink\" target=\"_blank\" href=\"http://tt-rss.org/wiki/ContentFilters\">".__("Wiki: Filters")."</a>
  645. </div>";
  646. print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterNewRuleDlg').execute()\">".
  647. ($rule ? __("Save rule") : __('Add rule'))."</button> ";
  648. print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterNewRuleDlg').hide()\">".
  649. __('Cancel')."</button>";
  650. print "</div>";
  651. print "</form>";
  652. }
  653. function newaction() {
  654. $action = json_decode($_REQUEST["action"], true);
  655. if ($action) {
  656. $action_param = $this->dbh->escape_string($action["action_param"]);
  657. $action_id = (int)$action["action_id"];
  658. } else {
  659. $action_param = "";
  660. $action_id = 0;
  661. }
  662. print "<form name='filter_new_action_form' id='filter_new_action_form'>";
  663. print "<div class=\"dlgSec\">".__("Perform Action")."</div>";
  664. print "<div class=\"dlgSecCont\">";
  665. print "<select name=\"action_id\" dojoType=\"dijit.form.Select\"
  666. onchange=\"filterDlgCheckAction(this)\">";
  667. $result = $this->dbh->query("SELECT id,description FROM ttrss_filter_actions
  668. ORDER BY name");
  669. while ($line = $this->dbh->fetch_assoc($result)) {
  670. $is_selected = ($line["id"] == $action_id) ? "selected='1'" : "";
  671. printf("<option $is_selected value='%d'>%s</option>", $line["id"], __($line["description"]));
  672. }
  673. print "</select>";
  674. $param_box_hidden = ($action_id == 7 || $action_id == 4 || $action_id == 6) ?
  675. "" : "display : none";
  676. $param_hidden = ($action_id == 4 || $action_id == 6) ?
  677. "" : "display : none";
  678. $label_param_hidden = ($action_id == 7) ? "" : "display : none";
  679. print "<span id=\"filterDlg_paramBox\" style=\"$param_box_hidden\">";
  680. print " " . __("with parameters:") . " ";
  681. print "<input dojoType=\"dijit.form.TextBox\"
  682. id=\"filterDlg_actionParam\" style=\"$param_hidden\"
  683. name=\"action_param\" value=\"$action_param\">";
  684. print_label_select("action_param_label", $action_param,
  685. "id=\"filterDlg_actionParamLabel\" style=\"$label_param_hidden\"
  686. dojoType=\"dijit.form.Select\"");
  687. print "</span>";
  688. print "&nbsp;"; // tiny layout hack
  689. print "</div>";
  690. print "<div class=\"dlgButtons\">";
  691. print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterNewActionDlg').execute()\">".
  692. ($action ? __("Save action") : __('Add action'))."</button> ";
  693. print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterNewActionDlg').hide()\">".
  694. __('Cancel')."</button>";
  695. print "</div>";
  696. print "</form>";
  697. }
  698. private function getFilterName($id) {
  699. $result = $this->dbh->query(
  700. "SELECT title,COUNT(DISTINCT r.id) AS num_rules,COUNT(DISTINCT a.id) AS num_actions
  701. FROM ttrss_filters2 AS f LEFT JOIN ttrss_filters2_rules AS r
  702. ON (r.filter_id = f.id)
  703. LEFT JOIN ttrss_filters2_actions AS a
  704. ON (a.filter_id = f.id) WHERE f.id = '$id' GROUP BY f.title");
  705. $title = $this->dbh->fetch_result($result, 0, "title");
  706. $num_rules = $this->dbh->fetch_result($result, 0, "num_rules");
  707. $num_actions = $this->dbh->fetch_result($result, 0, "num_actions");
  708. if (!$title) $title = __("[No caption]");
  709. $title = sprintf(_ngettext("%s (%d rule)", "%s (%d rules)", $num_rules), $title, $num_rules);
  710. $result = $this->dbh->query(
  711. "SELECT * FROM ttrss_filters2_actions WHERE filter_id = '$id' ORDER BY id LIMIT 1");
  712. $actions = "";
  713. if ($this->dbh->num_rows($result) > 0) {
  714. $line = $this->dbh->fetch_assoc($result);
  715. $actions = $this->getActionName($line);
  716. $num_actions -= 1;
  717. }
  718. if ($num_actions > 0)
  719. $actions = sprintf(_ngettext("%s (+%d action)", "%s (+%d actions)", $num_actions), $actions, $num_actions);
  720. return array($title, $actions);
  721. }
  722. function join() {
  723. $ids = explode(",", $this->dbh->escape_string($_REQUEST["ids"]));
  724. if (count($ids) > 1) {
  725. $base_id = array_shift($ids);
  726. $ids_str = join(",", $ids);
  727. $this->dbh->query("BEGIN");
  728. $this->dbh->query("UPDATE ttrss_filters2_rules
  729. SET filter_id = '$base_id' WHERE filter_id IN ($ids_str)");
  730. $this->dbh->query("UPDATE ttrss_filters2_actions
  731. SET filter_id = '$base_id' WHERE filter_id IN ($ids_str)");
  732. $this->dbh->query("DELETE FROM ttrss_filters2 WHERE id IN ($ids_str)");
  733. $this->dbh->query("UPDATE ttrss_filters2 SET match_any_rule = true WHERE id = '$base_id'");
  734. $this->dbh->query("COMMIT");
  735. $this->optimizeFilter($base_id);
  736. }
  737. }
  738. private function optimizeFilter($id) {
  739. $this->dbh->query("BEGIN");
  740. $result = $this->dbh->query("SELECT * FROM ttrss_filters2_actions
  741. WHERE filter_id = '$id'");
  742. $tmp = array();
  743. $dupe_ids = array();
  744. while ($line = $this->dbh->fetch_assoc($result)) {
  745. $id = $line["id"];
  746. unset($line["id"]);
  747. if (array_search($line, $tmp) === false) {
  748. array_push($tmp, $line);
  749. } else {
  750. array_push($dupe_ids, $id);
  751. }
  752. }
  753. if (count($dupe_ids) > 0) {
  754. $ids_str = join(",", $dupe_ids);
  755. $this->dbh->query("DELETE FROM ttrss_filters2_actions
  756. WHERE id IN ($ids_str)");
  757. }
  758. $result = $this->dbh->query("SELECT * FROM ttrss_filters2_rules
  759. WHERE filter_id = '$id'");
  760. $tmp = array();
  761. $dupe_ids = array();
  762. while ($line = $this->dbh->fetch_assoc($result)) {
  763. $id = $line["id"];
  764. unset($line["id"]);
  765. if (array_search($line, $tmp) === false) {
  766. array_push($tmp, $line);
  767. } else {
  768. array_push($dupe_ids, $id);
  769. }
  770. }
  771. if (count($dupe_ids) > 0) {
  772. $ids_str = join(",", $dupe_ids);
  773. $this->dbh->query("DELETE FROM ttrss_filters2_rules
  774. WHERE id IN ($ids_str)");
  775. }
  776. $this->dbh->query("COMMIT");
  777. }
  778. }
  779. ?>