pdo->prepare("UPDATE ttrss_filters2
				SET order_id = 0 WHERE owner_uid = ?");
		$sth->execute([$_SESSION['uid']]);
		return;
	}
	function savefilterorder() {
		$data = json_decode($_POST['payload'], true);
		#file_put_contents("/tmp/saveorder.json", clean($_POST['payload']));
		#$data = json_decode(file_get_contents("/tmp/saveorder.json"), true);
		if (!is_array($data['items']))
			$data['items'] = json_decode($data['items'], true);
		$index = 0;
		if (is_array($data) && is_array($data['items'])) {
			$sth = $this->pdo->prepare("UPDATE ttrss_filters2 SET
						order_id = ? WHERE id = ? AND
						owner_uid = ?");
			foreach ($data['items'][0]['items'] as $item) {
				$filter_id = (int) str_replace("FILTER:", "", $item['_reference']);
				if ($filter_id > 0) {
					$sth->execute([$index, $filter_id, $_SESSION['uid']]);
					++$index;
				}
			}
		}
		return;
	}
	function testFilterDo() {
		$offset = (int) clean($_REQUEST["offset"]);
		$limit = (int) clean($_REQUEST["limit"]);
		$filter = array();
		$filter["enabled"] = true;
		$filter["match_any_rule"] = checkbox_to_sql_bool(clean($_REQUEST["match_any_rule"]));
		$filter["inverse"] = checkbox_to_sql_bool(clean($_REQUEST["inverse"]));
		$filter["rules"] = array();
		$filter["actions"] = array("dummy-action");
		$res = $this->pdo->query("SELECT id,name FROM ttrss_filter_types");
		$filter_types = array();
		while ($line = $res->fetch()) {
			$filter_types[$line["id"]] = $line["name"];
		}
		$scope_qparts = array();
		$rctr = 0;
		foreach (clean($_REQUEST["rule"]) AS $r) {
			$rule = json_decode($r, true);
			if ($rule && $rctr < 5) {
				$rule["type"] = $filter_types[$rule["filter_type"]];
				unset($rule["filter_type"]);
				$scope_inner_qparts = [];
				foreach ($rule["feed_id"] as $feed_id) {
                    if (strpos($feed_id, "CAT:") === 0) {
                        $cat_id = (int) substr($feed_id, 4);
                        array_push($scope_inner_qparts, "cat_id = " . $this->pdo->quote($cat_id));
                    } else if ($feed_id > 0) {
                        array_push($scope_inner_qparts, "feed_id = " . $this->pdo->quote($feed_id));
                    }
                }
                if (count($scope_inner_qparts) > 0) {
				    array_push($scope_qparts, "(" . implode(" OR ", $scope_inner_qparts) . ")");
                }
				array_push($filter["rules"], $rule);
				++$rctr;
			} else {
				break;
			}
		}
		if (count($scope_qparts) == 0) $scope_qparts = ["true"];
		$glue = $filter['match_any_rule'] ? " OR " :  " AND ";
		$scope_qpart = join($glue, $scope_qparts);
		if (!$scope_qpart) $scope_qpart = "true";
		$rv = array();
		//while ($found < $limit && $offset < $limit * 1000 && time() - $started < ini_get("max_execution_time") * 0.7) {
		$sth = $this->pdo->prepare("SELECT ttrss_entries.id,
				ttrss_entries.title,
				ttrss_feeds.id AS feed_id,
				ttrss_feeds.title AS feed_title,
				ttrss_feed_categories.id AS cat_id,
				content,
				date_entered,
				link,
				author,
				tag_cache
			FROM
				ttrss_entries, ttrss_user_entries
					LEFT JOIN ttrss_feeds ON (feed_id = ttrss_feeds.id)
					LEFT JOIN ttrss_feed_categories ON (ttrss_feeds.cat_id = ttrss_feed_categories.id)
			WHERE
				ref_id = ttrss_entries.id AND
				($scope_qpart) AND
				ttrss_user_entries.owner_uid = ?
			ORDER BY date_entered DESC LIMIT $limit OFFSET $offset");
		$sth->execute([$_SESSION['uid']]);
		while ($line = $sth->fetch()) {
			$rc = RSSUtils::get_article_filters(array($filter), $line['title'], $line['content'], $line['link'],
				$line['author'], explode(",", $line['tag_cache']));
			if (count($rc) > 0) {
				$line["content_preview"] = truncate_string(strip_tags($line["content"]), 200, '…');
				foreach (PluginHost::getInstance()->get_hooks(PluginHost::HOOK_QUERY_HEADLINES) as $p) {
					$line = $p->hook_query_headlines($line, 100);
				}
				$content_preview = $line["content_preview"];
				$tmp = "
" . $line["title"] . "
" .
					"" . $line['feed_title'] . ", " . mb_substr($line["date_entered"], 0, 16) . "" .
					"" . $content_preview . "
" .
					"";
				array_push($rv, $tmp);
			}
		}
		print json_encode($rv);
	}
	function testFilter() {
		if (isset($_REQUEST["offset"])) return $this->testFilterDo();
		//print __("Articles matching this filter:");
		print " Looking for articles...
 Looking for articles...";
		print "
";
		print "
";
		if (array_key_exists("search", $_REQUEST)) {
			$_SESSION["prefs_filter_search"] = $filter_search;
		} else {
			$filter_search = $_SESSION["prefs_filter_search"];
		}
		print "
			
			
			
";
		print "
".
				"
" . __('Select')."";
		print "
";
		print "
".__('All')."
";
		print "
".__('None')."
";
		print "
";
		print "
 ";
		print "
 ";
		print "
 ";
		print "
 ";
		print "
 ";
		print "
"; # toolbar
		print "
"; # toolbar-frame
		print "
";
		print "
		
".
		 __("Loading, please wait...")."
 ";
		print "
		
		
		
		
		
		
		
";
		print "
"; #pane
		PluginHost::getInstance()->run_hooks(PluginHost::HOOK_PREFS_TAB,
			"hook_prefs_tab", "prefFilters");
		print "