filters.php 33 KB

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