filters.php 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070
  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 "<div style=\"float : left\">
  630. <a class=\"visibleLink\" target=\"_blank\" href=\"http://tt-rss.org/wiki/ContentFilters\">".__("Wiki: Filters")."</a>
  631. </div>";
  632. print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterNewRuleDlg').execute()\">".
  633. ($rule ? __("Save rule") : __('Add rule'))."</button> ";
  634. print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterNewRuleDlg').hide()\">".
  635. __('Cancel')."</button>";
  636. print "</div>";
  637. print "</form>";
  638. }
  639. function newaction() {
  640. $action = json_decode($_REQUEST["action"], true);
  641. if ($action) {
  642. $action_param = $this->dbh->escape_string($action["action_param"]);
  643. $action_id = (int)$action["action_id"];
  644. } else {
  645. $action_param = "";
  646. $action_id = 0;
  647. }
  648. print "<form name='filter_new_action_form' id='filter_new_action_form'>";
  649. print "<div class=\"dlgSec\">".__("Perform Action")."</div>";
  650. print "<div class=\"dlgSecCont\">";
  651. print "<select name=\"action_id\" dojoType=\"dijit.form.Select\"
  652. onchange=\"filterDlgCheckAction(this)\">";
  653. $result = $this->dbh->query("SELECT id,description FROM ttrss_filter_actions
  654. ORDER BY name");
  655. while ($line = $this->dbh->fetch_assoc($result)) {
  656. $is_selected = ($line["id"] == $action_id) ? "selected='1'" : "";
  657. printf("<option $is_selected value='%d'>%s</option>", $line["id"], __($line["description"]));
  658. }
  659. print "</select>";
  660. $param_box_hidden = ($action_id == 7 || $action_id == 4 || $action_id == 6) ?
  661. "" : "display : none";
  662. $param_hidden = ($action_id == 4 || $action_id == 6) ?
  663. "" : "display : none";
  664. $label_param_hidden = ($action_id == 7) ? "" : "display : none";
  665. print "<span id=\"filterDlg_paramBox\" style=\"$param_box_hidden\">";
  666. print " " . __("with parameters:") . " ";
  667. print "<input dojoType=\"dijit.form.TextBox\"
  668. id=\"filterDlg_actionParam\" style=\"$param_hidden\"
  669. name=\"action_param\" value=\"$action_param\">";
  670. print_label_select("action_param_label", $action_param,
  671. "id=\"filterDlg_actionParamLabel\" style=\"$label_param_hidden\"
  672. dojoType=\"dijit.form.Select\"");
  673. print "</span>";
  674. print "&nbsp;"; // tiny layout hack
  675. print "</div>";
  676. print "<div class=\"dlgButtons\">";
  677. print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterNewActionDlg').execute()\">".
  678. ($action ? __("Save action") : __('Add action'))."</button> ";
  679. print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterNewActionDlg').hide()\">".
  680. __('Cancel')."</button>";
  681. print "</div>";
  682. print "</form>";
  683. }
  684. private function getFilterName($id) {
  685. $result = $this->dbh->query(
  686. "SELECT title,COUNT(DISTINCT r.id) AS num_rules,COUNT(DISTINCT a.id) AS num_actions
  687. FROM ttrss_filters2 AS f LEFT JOIN ttrss_filters2_rules AS r
  688. ON (r.filter_id = f.id)
  689. LEFT JOIN ttrss_filters2_actions AS a
  690. ON (a.filter_id = f.id) WHERE f.id = '$id' GROUP BY f.title");
  691. $title = $this->dbh->fetch_result($result, 0, "title");
  692. $num_rules = $this->dbh->fetch_result($result, 0, "num_rules");
  693. $num_actions = $this->dbh->fetch_result($result, 0, "num_actions");
  694. if (!$title) $title = __("[No caption]");
  695. $title = sprintf(_ngettext("%s (%d rule)", "%s (%d rules)", $num_rules), $title, $num_rules);
  696. $result = $this->dbh->query(
  697. "SELECT * FROM ttrss_filters2_actions WHERE filter_id = '$id' ORDER BY id LIMIT 1");
  698. $actions = "";
  699. if ($this->dbh->num_rows($result) > 0) {
  700. $line = $this->dbh->fetch_assoc($result);
  701. $actions = $this->getActionName($line);
  702. $num_actions -= 1;
  703. }
  704. if ($num_actions > 0)
  705. $actions = sprintf(_ngettext("%s (+%d action)", "%s (+%d actions)", $num_actions), $actions, $num_actions);
  706. return array($title, $actions);
  707. }
  708. function join() {
  709. $ids = explode(",", $this->dbh->escape_string($_REQUEST["ids"]));
  710. if (count($ids) > 1) {
  711. $base_id = array_shift($ids);
  712. $ids_str = join(",", $ids);
  713. $this->dbh->query("BEGIN");
  714. $this->dbh->query("UPDATE ttrss_filters2_rules
  715. SET filter_id = '$base_id' WHERE filter_id IN ($ids_str)");
  716. $this->dbh->query("UPDATE ttrss_filters2_actions
  717. SET filter_id = '$base_id' WHERE filter_id IN ($ids_str)");
  718. $this->dbh->query("DELETE FROM ttrss_filters2 WHERE id IN ($ids_str)");
  719. $this->dbh->query("UPDATE ttrss_filters2 SET match_any_rule = true WHERE id = '$base_id'");
  720. $this->dbh->query("COMMIT");
  721. $this->optimizeFilter($base_id);
  722. }
  723. }
  724. private function optimizeFilter($id) {
  725. $this->dbh->query("BEGIN");
  726. $result = $this->dbh->query("SELECT * FROM ttrss_filters2_actions
  727. WHERE filter_id = '$id'");
  728. $tmp = array();
  729. $dupe_ids = array();
  730. while ($line = $this->dbh->fetch_assoc($result)) {
  731. $id = $line["id"];
  732. unset($line["id"]);
  733. if (array_search($line, $tmp) === false) {
  734. array_push($tmp, $line);
  735. } else {
  736. array_push($dupe_ids, $id);
  737. }
  738. }
  739. if (count($dupe_ids) > 0) {
  740. $ids_str = join(",", $dupe_ids);
  741. $this->dbh->query("DELETE FROM ttrss_filters2_actions
  742. WHERE id IN ($ids_str)");
  743. }
  744. $result = $this->dbh->query("SELECT * FROM ttrss_filters2_rules
  745. WHERE filter_id = '$id'");
  746. $tmp = array();
  747. $dupe_ids = array();
  748. while ($line = $this->dbh->fetch_assoc($result)) {
  749. $id = $line["id"];
  750. unset($line["id"]);
  751. if (array_search($line, $tmp) === false) {
  752. array_push($tmp, $line);
  753. } else {
  754. array_push($dupe_ids, $id);
  755. }
  756. }
  757. if (count($dupe_ids) > 0) {
  758. $ids_str = join(",", $dupe_ids);
  759. $this->dbh->query("DELETE FROM ttrss_filters2_rules
  760. WHERE id IN ($ids_str)");
  761. }
  762. $this->dbh->query("COMMIT");
  763. }
  764. }
  765. ?>