]> git.wh0rd.org - tt-rss.git/blobdiff - include/functions.php
implement multiple rule/action filters
[tt-rss.git] / include / functions.php
index ac9c5372ce6e567c55d1bd11b9d2c5906b6066b2..ac0276fc7607b180ece39b616d8d9d1af1d675ba 100644 (file)
@@ -1,6 +1,18 @@
 <?php
-       define('EXPECTED_CONFIG_VERSION', 25);
-       define('SCHEMA_VERSION', 93);
+       define('EXPECTED_CONFIG_VERSION', 26);
+       define('SCHEMA_VERSION', 96);
+
+       $fetch_last_error = false;
+
+       function __autoload($class) {
+               $class_file = str_replace("_", "/", strtolower(basename($class)));
+
+               $file = dirname(__FILE__)."/../classes/$class_file.php";
+
+               if (file_exists($file)) {
+                       require $file;
+               }
+       }
 
        mb_internal_encoding("UTF-8");
        date_default_timezone_set('UTC');
                $login = urlencode($login);
                $pass = urlencode($pass);
 
+               global $fetch_last_error;
+
                if (function_exists('curl_init') && !ini_get("open_basedir")) {
                        $ch = curl_init($url);
 
                        $contents = @curl_exec($ch);
 
                        if ($contents === false) {
+                               $fetch_last_error = curl_error($ch);
                                curl_close($ch);
                                return false;
                        }
                                }
                        }
 
-                       return @file_get_contents($url);
+                       $data = @file_get_contents($url);
+
+                       if (!$data && function_exists('error_get_last')) {
+                               $error = error_get_last();
+                               $fetch_last_error = $error["message"];
+                       }
+                       return $data;
                }
 
        }
        function get_article_filters($filters, $title, $content, $link, $timestamp, $author, $tags) {
                $matches = array();
 
-               if ($filters["title"]) {
-                       foreach ($filters["title"] as $filter) {
-                               $reg_exp = $filter["reg_exp"];
-                               $inverse = $filter["inverse"];
-                               if ((!$inverse && @preg_match("/$reg_exp/i", $title)) ||
-                                               ($inverse && !@preg_match("/$reg_exp/i", $title))) {
+               foreach ($filters as $filter) {
+                       $match_any_rule = $filter["match_any_rule"];
+                       $filter_match = false;
 
-                                       array_push($matches, array($filter["action"], $filter["action_param"]));
-                               }
-                       }
-               }
-
-               if ($filters["content"]) {
-                       foreach ($filters["content"] as $filter) {
-                               $reg_exp = $filter["reg_exp"];
-                               $inverse = $filter["inverse"];
+                       foreach ($filter["rules"] as $rule) {
+                               $match = false;
+                               $reg_exp = $rule["reg_exp"];
 
-                               if ((!$inverse && @preg_match("/$reg_exp/i", $content)) ||
-                                               ($inverse && !@preg_match("/$reg_exp/i", $content))) {
+                               if (!$reg_exp)
+                                       continue;
 
-                                       array_push($matches, array($filter["action"], $filter["action_param"]));
+                               switch ($rule["type"]) {
+                               case "title":
+                                       $match = @preg_match("/$reg_exp/i", $title);
+                                       break;
+                               case "content":
+                                       $match = @preg_match("/$reg_exp/i", $content);
+                                       break;
+                               case "both":
+                                       $match = (@preg_match("/$reg_exp/i", $title) || @preg_match("/$reg_exp/i", $title));
+                                       break;
+                               case "link":
+                                       $match = @preg_match("/$reg_exp/i", $link);
+                                       break;
+                               case "author":
+                                       $match = @preg_match("/$reg_exp/i", $author);
+                                       break;
+                               case "tag":
+                                       $tag_string = join(",", $tags);
+                                       $match = @preg_match("/$reg_exp/i", $tag_string);
+                                       break;
                                }
-                       }
-               }
-
-               if ($filters["both"]) {
-                       foreach ($filters["both"] as $filter) {
-                               $reg_exp = $filter["reg_exp"];
-                               $inverse = $filter["inverse"];
 
-                               if ($inverse) {
-                                       if (!@preg_match("/$reg_exp/i", $title) && !preg_match("/$reg_exp/i", $content)) {
-                                               array_push($matches, array($filter["action"], $filter["action_param"]));
+                               if ($match_any_rule) {
+                                       if ($match) {
+                                               $filter_match = true;
+                                               break;
                                        }
                                } else {
-                                       if (@preg_match("/$reg_exp/i", $title) || preg_match("/$reg_exp/i", $content)) {
-                                               array_push($matches, array($filter["action"], $filter["action_param"]));
+                                       $filter_match = $match;
+                                       if (!$match) {
+                                               break;
                                        }
                                }
                        }
-               }
-
-               if ($filters["link"]) {
-                       $reg_exp = $filter["reg_exp"];
-                       foreach ($filters["link"] as $filter) {
-                               $reg_exp = $filter["reg_exp"];
-                               $inverse = $filter["inverse"];
-
-                               if ((!$inverse && @preg_match("/$reg_exp/i", $link)) ||
-                                               ($inverse && !@preg_match("/$reg_exp/i", $link))) {
-
-                                       array_push($matches, array($filter["action"], $filter["action_param"]));
-                               }
-                       }
-               }
-
-               if ($filters["date"]) {
-                       $reg_exp = $filter["reg_exp"];
-                       foreach ($filters["date"] as $filter) {
-                               $date_modifier = $filter["filter_param"];
-                               $inverse = $filter["inverse"];
-                               $check_timestamp = strtotime($filter["reg_exp"]);
-
-                               # no-op when timestamp doesn't parse to prevent misfires
-
-                               if ($check_timestamp) {
-                                       $match_ok = false;
-
-                                       if ($date_modifier == "before" && $timestamp < $check_timestamp ||
-                                               $date_modifier == "after" && $timestamp > $check_timestamp) {
-                                                       $match_ok = true;
-                                       }
-
-                                       if ($inverse) $match_ok = !$match_ok;
-
-                                       if ($match_ok) {
-                                               array_push($matches, array($filter["action"], $filter["action_param"]));
-                                       }
-                               }
-                       }
-               }
-
-               if ($filters["author"]) {
-                       foreach ($filters["author"] as $filter) {
-                               $reg_exp = $filter["reg_exp"];
-                               $inverse = $filter["inverse"];
-                               if ((!$inverse && @preg_match("/$reg_exp/i", $author)) ||
-                                               ($inverse && !@preg_match("/$reg_exp/i", $author))) {
-
-                                       array_push($matches, array($filter["action"], $filter["action_param"]));
-                               }
-                       }
-               }
-
-               if ($filters["tag"]) {
-
-                       $tag_string = join(",", $tags);
 
-                       foreach ($filters["tag"] as $filter) {
-                               $reg_exp = $filter["reg_exp"];
-                               $inverse = $filter["inverse"];
-
-                               if ((!$inverse && @preg_match("/$reg_exp/i", $tag_string)) ||
-                                               ($inverse && !@preg_match("/$reg_exp/i", $tag_string))) {
-
-                                       array_push($matches, array($filter["action"], $filter["action_param"]));
+                       if ($filter_match) {
+                               foreach ($filter["actions"] AS $action) {
+                                       array_push($matches, $action);
                                }
                        }
                }
 
-
                return $matches;
        }
 
        function find_article_filter($filters, $filter_name) {
                foreach ($filters as $f) {
-                       if ($f[0] == $filter_name) {
+                       if ($f["type"] == $filter_name) {
                                return $f;
                        };
                }
                return false;
        }
 
+       function find_article_filters($filters, $filter_name) {
+               $results = array();
+
+               foreach ($filters as $f) {
+                       if ($f["type"] == $filter_name) {
+                               array_push($results, $f);
+                       };
+               }
+               return $results;
+       }
+
        function calculate_article_score($filters) {
                $score = 0;
 
                foreach ($filters as $f) {
-                       if ($f[0] == "score") {
-                               $score += $f[1];
+                       if ($f["type"] == "score") {
+                               $score += $f["param"];
                        };
                }
                return $score;
 
        function assign_article_to_labels($link, $id, $filters, $owner_uid) {
                foreach ($filters as $f) {
-                       if ($f[0] == "label") {
-                               label_add_article($link, $id, $f[1], $owner_uid);
+                       if ($f["type"] == "label") {
+                               label_add_article($link, $id, $f["param"], $owner_uid);
                        };
                }
        }
                return "";
        }
 
-       function get_login_by_ssl_certificate($link) {
-
-               $cert_serial = db_escape_string(get_ssl_certificate_id());
-
-               if ($cert_serial) {
-                       $result = db_query($link, "SELECT login FROM ttrss_user_prefs, ttrss_users
-                               WHERE pref_name = 'SSL_CERT_SERIAL' AND value = '$cert_serial' AND
-                               owner_uid = ttrss_users.id");
-
-                       if (db_num_rows($result) != 0) {
-                               return db_escape_string(db_fetch_result($result, 0, "login"));
-                       }
-               }
-
-               return "";
-       }
-
-       function get_remote_user($link) {
-
-               if (defined('ALLOW_REMOTE_USER_AUTH') && ALLOW_REMOTE_USER_AUTH) {
-                       return db_escape_string($_SERVER["REMOTE_USER"]);
-               }
-
-               return db_escape_string(get_login_by_ssl_certificate($link));
-       }
-
-       function get_remote_fakepass($link) {
-               if (get_remote_user($link))
-                       return "******";
-               else
-                       return "";
-       }
-
-       function authenticate_user($link, $login, $password, $force_auth = false) {
+       function authenticate_user($link, $login, $password, $check_only = false) {
 
                if (!SINGLE_USER_MODE) {
 
-                       $pwd_hash1 = encrypt_password($password);
-                       $pwd_hash2 = encrypt_password($password, $login);
-                       $login = db_escape_string($login);
-
-                       $remote_user = get_remote_user($link);
-
-                       if ($remote_user && $remote_user == $login && $login != "admin") {
-
-                               $login = $remote_user;
-
-                               $query = "SELECT id,login,access_level,pwd_hash
-                   FROM ttrss_users WHERE
-                                       login = '$login'";
+                       $user_id = false;
+                       $modules = explode(",", AUTH_MODULES);
 
-                               if (defined('AUTO_CREATE_USER') && AUTO_CREATE_USER
-                                               && $_SERVER["REMOTE_USER"]) {
-                                       $result = db_query($link, $query);
+                       foreach ($modules as $module) {
+                               $module_class = "auth_$module";
+                               if (class_exists($module_class)) {
+                                       $authenticator = new $module_class($link);
 
-                                       // First login ?
-                                       if (db_num_rows($result) == 0) {
-                                               $salt = substr(bin2hex(get_random_bytes(125)), 0, 250);
-                                               $pwd_hash = encrypt_password($password, $salt, true);
+                                       $user_id = (int) $authenticator->authenticate($login, $password);
 
-                                               $query2 = "INSERT INTO ttrss_users
-                                                               (login,access_level,last_login,created,pwd_hash,salt)
-                                                               VALUES ('$login', 0, null, NOW(), '$pwd_hash','$salt')";
-                                               db_query($link, $query2);
-                                       }
-                               }
-
-                       } else if (get_schema_version($link) > 87) {
-                               $result = db_query($link, "SELECT salt FROM ttrss_users WHERE
-                                       login = '$login'");
-
-                               if (db_num_rows($result) != 1) {
-                                       return false;
-                               }
-
-                               $salt = db_fetch_result($result, 0, "salt");
-
-                               if ($salt == "") {
-
-                                       $query = "SELECT id,login,access_level,pwd_hash
-                           FROM ttrss_users WHERE
-                                               login = '$login' AND (pwd_hash = '$pwd_hash1' OR
-                                               pwd_hash = '$pwd_hash2')";
-
-                                       // verify and upgrade password to new salt base
-
-                                       $result = db_query($link, $query);
-
-                                       if (db_num_rows($result) == 1) {
-                                               // upgrade password to MODE2
-
-                                               $salt = substr(bin2hex(get_random_bytes(125)), 0, 250);
-                                               $pwd_hash = encrypt_password($password, $salt, true);
-
-                                               db_query($link, "UPDATE ttrss_users SET
-                                                       pwd_hash = '$pwd_hash', salt = '$salt' WHERE login = '$login'");
-
-                                               $query = "SELECT id,login,access_level,pwd_hash
-                                   FROM ttrss_users WHERE
-                                                       login = '$login' AND pwd_hash = '$pwd_hash'";
-
-                                       } else {
-                                               return false;
+                                       if ($user_id) {
+                                               $_SESSION["auth_module"] = $module;
+                                               break;
                                        }
 
                                } else {
-
-                                       $pwd_hash = encrypt_password($password, $salt, true);
-
-                                       $query = "SELECT id,login,access_level,pwd_hash
-                                FROM ttrss_users WHERE
-                                               login = '$login' AND pwd_hash = '$pwd_hash'";
-
+                                       print T_sprintf("Fatal: authentication module %s not found.", $module);
+                                       die;
                                }
-                       } else {
-                               $query = "SELECT id,login,access_level,pwd_hash
-                        FROM ttrss_users WHERE
-                                       login = '$login' AND (pwd_hash = '$pwd_hash1' OR
-                                               pwd_hash = '$pwd_hash2')";
                        }
 
-                       $result = db_query($link, $query);
+                       if ($user_id && !$check_only) {
+                               $_SESSION["uid"] = $user_id;
+
+                               $result = db_query($link, "SELECT login,access_level,pwd_hash FROM ttrss_users
+                                       WHERE id = '$user_id'");
 
-                       if (db_num_rows($result) == 1) {
-                               $_SESSION["uid"] = db_fetch_result($result, 0, "id");
                                $_SESSION["name"] = db_fetch_result($result, 0, "login");
                                $_SESSION["access_level"] = db_fetch_result($result, 0, "access_level");
                                $_SESSION["csrf_token"] = sha1(uniqid(rand(), true));
                                db_query($link, "UPDATE ttrss_users SET last_login = NOW() WHERE id = " .
                                        $_SESSION["uid"]);
 
-
-                               // LemonLDAP can send user informations via HTTP HEADER
-                               if (defined('AUTO_CREATE_USER') && AUTO_CREATE_USER){
-                                       // update user name
-                                       $fullname = $_SERVER['HTTP_USER_NAME'] ? $_SERVER['HTTP_USER_NAME'] : $_SERVER['AUTHENTICATE_CN'];
-                                       if ($fullname){
-                                               $fullname = db_escape_string($fullname);
-                                               db_query($link, "UPDATE ttrss_users SET full_name = '$fullname' WHERE id = " .
-                                                       $_SESSION["uid"]);
-                                       }
-                                       // update user mail
-                                       $email = $_SERVER['HTTP_USER_MAIL'] ? $_SERVER['HTTP_USER_MAIL'] : $_SERVER['AUTHENTICATE_MAIL'];
-                                       if ($email){
-                                               $email = db_escape_string($email);
-                                               db_query($link, "UPDATE ttrss_users SET email = '$email' WHERE id = " .
-                                                       $_SESSION["uid"]);
-                                       }
-                               }
-
                                $_SESSION["ip_address"] = $_SERVER["REMOTE_ADDR"];
                                $_SESSION["pwd_hash"] = db_fetch_result($result, 0, "pwd_hash");
 
                        $_SESSION["name"] = "admin";
                        $_SESSION["access_level"] = 10;
 
+                       $_SESSION["hide_hello"] = true;
+                       $_SESSION["hide_logout"] = true;
+
+                       $_SESSION["auth_module"] = false;
+
                        if (!$_SESSION["csrf_token"]) {
                                $_SESSION["csrf_token"] = sha1(uniqid(rand(), true));
                        }
 
                        if (!$_SESSION["uid"] || !validate_session($link)) {
 
-                               if (get_remote_user($link) && AUTO_LOGIN) {
-                                   authenticate_user($link, get_remote_user($link), null);
+                               if (AUTH_AUTO_LOGIN && authenticate_user($link, null, null)) {
                                    $_SESSION["ref_schema_version"] = get_schema_version($link, true);
                                } else {
+                                        authenticate_user($link, null, null, true);
                                    render_login_form($link, $mobile);
-                                   //header("Location: login.php");
                                    exit;
                                }
                        } else {
                                WHERE c2.parent_cat = ttrss_feed_categories.id) AS num_children
                        FROM ttrss_feed_categories, ttrss_cat_counters_cache
                        WHERE ttrss_cat_counters_cache.feed_id = id AND
+                       ttrss_cat_counters_cache.owner_uid = ttrss_feed_categories.owner_uid AND
                        ttrss_feed_categories.owner_uid = " . $_SESSION["uid"]);
 
                while ($line = db_fetch_assoc($result)) {
 
                if ($is_cat) {
                        return getCategoryUnread($link, $n_feed, $owner_uid);
-               } if ($feed != "0" && $n_feed == 0) {
+               } else if ($n_feed == -6) {
+                       return 0;
+               } else if ($feed != "0" && $n_feed == 0) {
 
                        $feed = db_escape_string($feed);
 
                                last_error, value AS count
                        FROM ttrss_feeds, ttrss_counters_cache
                        WHERE ttrss_feeds.owner_uid = ".$_SESSION["uid"]."
+                               AND ttrss_counters_cache.owner_uid = ttrss_feeds.owner_uid
                                AND ttrss_counters_cache.feed_id = id";
 
                $result = db_query($link, $query);
        }
 
        /**
-        * @return integer Status code:
+        * @return array (code => Status code, message => error message if available)
+        *
         *                 0 - OK, Feed already exists
         *                 1 - OK, Feed added
         *                 2 - Invalid URL
        function subscribe_to_feed($link, $url, $cat_id = 0,
                        $auth_login = '', $auth_pass = '', $need_auth = false) {
 
+               global $fetch_last_error;
+
                require_once "include/rssfuncs.php";
 
                $url = fix_url($url);
 
-               if (!$url || !validate_feed_url($url)) return 2;
+               if (!$url || !validate_feed_url($url)) return array("code" => 2);
 
                $update_method = 0;
 
                $has_oauth = db_fetch_result($result, 0, 'twitter_oauth');
 
                if (!$need_auth || !$has_oauth || strpos($url, '://api.twitter.com') === false) {
-                       if (!fetch_file_contents($url, false, $auth_login, $auth_pass)) return 5;
+                       if (!fetch_file_contents($url, false, $auth_login, $auth_pass))
+                               return array("code" => 5, "message" => $fetch_last_error);
 
                        if (url_is_html($url, $auth_login, $auth_pass)) {
                                $feedUrls = get_feeds_from_html($url, $auth_login, $auth_pass);
                                if (count($feedUrls) == 0) {
-                                       return 3;
+                                       return array("code" => 3);
                                } else if (count($feedUrls) > 1) {
-                                       return 4;
+                                       return array("code" => 4);
                                }
                                //use feed url as new URL
                                $url = key($feedUrls);
 
                        } else {
                                if (!fetch_twitter_rss($link, $url, $_SESSION['uid']))
-                                       return 5;
+                                       return array("code" => 5);
 
                                $update_method = 3;
                        }
                                update_rss_feed($link, $feed_id, true);
                        }
 
-                       return 1;
+                       return array("code" => 1);
                } else {
-                       return 0;
+                       return array("code" => 0);
                }
        }
 
        function print_feed_select($link, $id, $default_id = "",
-               $attributes = "", $include_all_feeds = true) {
+               $attributes = "", $include_all_feeds = true,
+               $root_id = false, $nest_level = 0) {
 
-               print "<select id=\"$id\" name=\"$id\" $attributes>";
-               if ($include_all_feeds) {
-                       print "<option value=\"0\">".__('All feeds')."</option>";
+               if (!$root_id) {
+                       print "<select id=\"$id\" name=\"$id\" $attributes>";
+                       if ($include_all_feeds) {
+                               $is_selected = ("0" == $default_id) ? "selected=\"1\"" : "";
+                               print "<option $is_selected value=\"0\">".__('All feeds')."</option>";
+                       }
                }
 
-               $result = db_query($link, "SELECT id,title FROM ttrss_feeds
-                       WHERE owner_uid = ".$_SESSION["uid"]." ORDER BY title");
+               if (get_pref($link, 'ENABLE_FEED_CATS')) {
 
-               if (db_num_rows($result) > 0 && $include_all_feeds) {
-                       print "<option disabled>--------</option>";
-               }
+                       if ($root_id)
+                               $parent_qpart = "parent_cat = '$root_id'";
+                       else
+                               $parent_qpart = "parent_cat IS NULL";
 
-               while ($line = db_fetch_assoc($result)) {
-                       if ($line["id"] == $default_id) {
-                               $is_selected = "selected=\"1\"";
-                       } else {
-                               $is_selected = "";
+                       $result = db_query($link, "SELECT id,title,
+                               (SELECT COUNT(id) FROM ttrss_feed_categories AS c2 WHERE
+                                       c2.parent_cat = ttrss_feed_categories.id) AS num_children
+                               FROM ttrss_feed_categories
+                               WHERE owner_uid = ".$_SESSION["uid"]." AND $parent_qpart ORDER BY title");
+
+                       while ($line = db_fetch_assoc($result)) {
+
+                               for ($i = 0; $i < $nest_level; $i++)
+                                       $line["title"] = " - " . $line["title"];
+
+                               $is_selected = ("CAT:".$line["id"] == $default_id) ? "selected=\"1\"" : "";
+
+                               printf("<option $is_selected value='CAT:%d'>%s</option>",
+                                       $line["id"], htmlspecialchars($line["title"]));
+
+                               if ($line["num_children"] > 0)
+                                       print_feed_select($link, $id, $default_id, $attributes,
+                                               $include_all_feeds, $line["id"], $nest_level+1);
+
+                               $feed_result = db_query($link, "SELECT id,title FROM ttrss_feeds
+                                       WHERE cat_id = '".$line["id"]."' AND owner_uid = ".$_SESSION["uid"] . " ORDER BY title");
+
+                               while ($fline = db_fetch_assoc($feed_result)) {
+                                       $is_selected = ($fline["id"] == $default_id) ? "selected=\"1\"" : "";
+
+                                       $fline["title"] = " + " . $fline["title"];
+
+                                       for ($i = 0; $i < $nest_level; $i++)
+                                               $fline["title"] = " - " . $fline["title"];
+
+                                       printf("<option $is_selected value='%d'>%s</option>",
+                                               $fline["id"], htmlspecialchars($fline["title"]));
+                               }
                        }
 
-                       $title = truncate_string(htmlspecialchars($line["title"]), 40);
+                       if (!$root_id) {
+                               $is_selected = ($default_id == "CAT:0") ? "selected=\"1\"" : "";
+
+                               printf("<option $is_selected value='CAT:0'>%s</option>",
+                                       __("Uncategorized"));
+
+                               $feed_result = db_query($link, "SELECT id,title FROM ttrss_feeds
+                                       WHERE cat_id IS NULL AND owner_uid = ".$_SESSION["uid"] . " ORDER BY title");
+
+                               while ($fline = db_fetch_assoc($feed_result)) {
+                                       $is_selected = ($fline["id"] == $default_id && !$default_is_cat) ? "selected=\"1\"" : "";
+
+                                       $fline["title"] = " + " . $fline["title"];
 
-                       printf("<option $is_selected value='%d'>%s</option>",
-                               $line["id"], $title);
+                                       for ($i = 0; $i < $nest_level; $i++)
+                                               $fline["title"] = " - " . $fline["title"];
+
+                                       printf("<option $is_selected value='%d'>%s</option>",
+                                               $fline["id"], htmlspecialchars($fline["title"]));
+                               }
+                       }
+
+               } else {
+                       $result = db_query($link, "SELECT id,title FROM ttrss_feeds
+                               WHERE owner_uid = ".$_SESSION["uid"]." ORDER BY title");
+
+                       while ($line = db_fetch_assoc($result)) {
+
+                               $is_selected = ($line["id"] == $default_id) ? "selected=\"1\"" : "";
+
+                               printf("<option $is_selected value='%d'>%s</option>",
+                                       $line["id"], htmlspecialchars($line["title"]));
+                       }
                }
 
-               print "</select>";
+               if (!$root_id) {
+                       print "</select>";
+               }
        }
 
        function print_feed_cat_select($link, $id, $default_id,
                                        if (db_num_rows($result) > 0) {
                                                print "<option disabled=\"1\">--------</option>";
                                        }
-                                               print "<option value=\"0\">".__('Uncategorized')."</option>";
+
+                                       if ($default_id == 0) {
+                                               $is_selected = "selected=\"1\"";
+                                       } else {
+                                               $is_selected = "";
+                                       }
+
+                                       print "<option $is_selected value=\"0\">".__('Uncategorized')."</option>";
                                }
                                print "</select>";
                        }
                case -4:
                        return "images/tag.png";
                        break;
+               case -6:
+                       return "images/recently_read.png";
+                       break;
                default:
                        if ($id < -10) {
                                return "images/label.png";
                        return __("All articles");
                } else if ($id === 0 || $id === "0") {
                        return __("Archived articles");
+               } else if ($id == -6) {
+                       return __("Recently read");
                } else if ($id < -10) {
                        $label_id = -$id - 11;
                        $result = db_query($link, "SELECT caption FROM ttrss_labels2 WHERE id = '$label_id'");
 
                $params["icons_url"] = ICONS_URL;
                $params["cookie_lifetime"] = SESSION_COOKIE_LIFETIME;
-               $params["default_include_children"] = $_SESSION["_DEFAULT_INCLUDE_CHILDREN"];
+               $params["default_include_children"] = get_pref($link, "_DEFAULT_INCLUDE_CHILDREN");
                $params["default_view_mode"] = get_pref($link, "_DEFAULT_VIEW_MODE");
                $params["default_view_limit"] = (int) get_pref($link, "_DEFAULT_VIEW_LIMIT");
                $params["default_view_order_by"] = get_pref($link, "_DEFAULT_VIEW_ORDER_BY");
                                $data['new_version_available'] = (int) ($new_version_details != false);
 
                                $_SESSION["last_version_check"] = time();
+                               $_SESSION["version_data"] = $new_version_details;
                }
 
                return $data;
                                                ttrss_user_labels2.article_id = ref_id";
 
                                }
-
+                       } else if ($feed == -6) { // recently read
+                               $query_strategy_part = "unread = false AND last_read IS NOT NULL";
+                               $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
+                               $override_order = "last_read DESC";
                        } else if ($feed == -3) { // fresh virtual feed
                                $query_strategy_part = "unread = true AND score >= 0";
 
        }
 
        function format_article($link, $id, $mark_as_read = true, $zoom_mode = false, $owner_uid = false) {
-
                if (!$owner_uid) $owner_uid = $_SESSION["uid"];
 
                $rv = array();
                                $button_plugins = explode(",", ARTICLE_BUTTON_PLUGINS);
 
                                foreach ($button_plugins as $p) {
-                                       $pclass = trim("${p}_button");
+                                       $pclass = trim("button_${p}");
 
                                        if (class_exists($pclass)) {
                                                $plugin = new $pclass($link);
                return $text;
        }
 
-       function load_filters($link, $feed, $owner_uid, $action_id = false) {
+       function load_filters($link, $feed_id, $owner_uid, $action_id = false) {
                $filters = array();
 
+               $cat_id = getFeedCategory($link, $feed_id);
 
-               if ($action_id) $ftype_query_part = "action_id = '$action_id' AND";
-
-               $result = db_query($link, "SELECT reg_exp,
-                       ttrss_filter_types.name AS name,
-                       ttrss_filter_actions.name AS action,
-                       inverse,
-                       action_param,
-                       filter_param
-                       FROM ttrss_filters
-                               LEFT JOIN ttrss_feeds ON (ttrss_feeds.id = '$feed'),
-                               ttrss_filter_types,ttrss_filter_actions
-                       WHERE
-                               enabled = true AND
-                               $ftype_query_part
-                               ttrss_filters.owner_uid = $owner_uid AND
-                               ttrss_filter_types.id = filter_type AND
-                               ttrss_filter_actions.id = action_id AND
-                               ((cat_filter = true AND ttrss_feeds.cat_id = ttrss_filters.cat_id) OR
-                               (cat_filter = true AND ttrss_feeds.cat_id IS NULL AND
-                                       ttrss_filters.cat_id IS NULL) OR
-                               (cat_filter = false AND (feed_id IS NULL OR feed_id = '$feed')))
-                       ORDER BY reg_exp");
+               $result = db_query($link, "SELECT * FROM ttrss_filters2 WHERE
+                       owner_uid = $owner_uid AND enabled = true");
 
                while ($line = db_fetch_assoc($result)) {
+                       $filter_id = $line["id"];
+
+                       $result2 = db_query($link, "SELECT
+                               r.reg_exp, r.feed_id, r.cat_id, r.cat_filter, t.name AS type_name
+                               FROM ttrss_filters2_rules AS r,
+                               ttrss_filter_types AS t
+                               WHERE
+                                       (cat_id IS NULL OR cat_id = '$cat_id') AND
+                                       (feed_id IS NULL OR feed_id = '$feed_id') AND
+                                       filter_type = t.id AND filter_id = '$filter_id'");
+
+                       $rules = array();
+                       $actions = array();
+
+                       while ($rule_line = db_fetch_assoc($result2)) {
+#                              print_r($rule_line);
 
-                       if (!$filters[$line["name"]]) $filters[$line["name"]] = array();
-                               $filter["reg_exp"] = $line["reg_exp"];
-                               $filter["action"] = $line["action"];
-                               $filter["action_param"] = $line["action_param"];
-                               $filter["filter_param"] = $line["filter_param"];
-                               $filter["inverse"] = sql_bool_to_bool($line["inverse"]);
+                               $rule = array();
+                               $rule["reg_exp"] = $rule_line["reg_exp"];
+                               $rule["type"] = $rule_line["type_name"];
 
-                               array_push($filters[$line["name"]], $filter);
+                               array_push($rules, $rule);
                        }
 
+                       $result2 = db_query($link, "SELECT a.action_param,t.name AS type_name
+                               FROM ttrss_filters2_actions AS a,
+                               ttrss_filter_actions AS t
+                               WHERE
+                                       action_id = t.id AND filter_id = '$filter_id'");
+
+                       while ($action_line = db_fetch_assoc($result2)) {
+#                              print_r($action_line);
+
+                               $action = array();
+                               $action["type"] = $action_line["type_name"];
+                               $action["param"] = $action_line["action_param"];
+
+                               array_push($actions, $action);
+                       }
+
+
+                       $filter = array();
+                       $filter["match_any_rule"] = sql_bool_to_bool($line["match_any_rule"]);
+                       $filter["rules"] = $rules;
+                       $filter["actions"] = $actions;
+
+                       if (count($rules) > 0 && count($actions) > 0) {
+                               array_push($filters, $filter);
+                       }
+               }
 
                return $filters;
        }
                }
        }
 
-       function add_feed_category($link, $feed_cat) {
+       function get_feed_category($link, $feed_cat, $parent_cat_id = false) {
+               if ($parent_cat_id) {
+                       $parent_qpart = "parent_cat = '$parent_cat_id'";
+                       $parent_insert = "'$parent_cat_id'";
+               } else {
+                       $parent_qpart = "parent_cat IS NULL";
+                       $parent_insert = "NULL";
+               }
+
+               $result = db_query($link,
+                       "SELECT id FROM ttrss_feed_categories
+                       WHERE $parent_qpart AND title = '$feed_cat' AND owner_uid = ".$_SESSION["uid"]);
+
+               if (db_num_rows($result) == 0) {
+                       return false;
+               } else {
+                       return db_fetch_result($result, 0, "id");
+               }
+       }
+
+       function add_feed_category($link, $feed_cat, $parent_cat_id = false) {
 
                if (!$feed_cat) return false;
 
                db_query($link, "BEGIN");
 
+               if ($parent_cat_id) {
+                       $parent_qpart = "parent_cat = '$parent_cat_id'";
+                       $parent_insert = "'$parent_cat_id'";
+               } else {
+                       $parent_qpart = "parent_cat IS NULL";
+                       $parent_insert = "NULL";
+               }
+
                $result = db_query($link,
                        "SELECT id FROM ttrss_feed_categories
-                       WHERE title = '$feed_cat' AND owner_uid = ".$_SESSION["uid"]);
+                       WHERE $parent_qpart AND title = '$feed_cat' AND owner_uid = ".$_SESSION["uid"]);
 
                if (db_num_rows($result) == 0) {
 
                        $result = db_query($link,
-                               "INSERT INTO ttrss_feed_categories (owner_uid,title)
-                               VALUES ('".$_SESSION["uid"]."', '$feed_cat')");
+                               "INSERT INTO ttrss_feed_categories (owner_uid,title,parent_cat)
+                               VALUES ('".$_SESSION["uid"]."', '$feed_cat', $parent_insert)");
 
                        db_query($link, "COMMIT");
 
                                }
                        }
 
-                       $rv .= "<div dojoType=\"dijit.form.DropDownButton\">".
+                       $rv .= "<br/><div dojoType=\"dijit.form.DropDownButton\">".
                                "<span>" . __('Attachments')."</span>";
                        $rv .= "<div dojoType=\"dijit.Menu\" style=\"display: none;\">";
 
 
                return null;
        }
+
+       function tmpdirname($path, $prefix) {
+               // Use PHP's tmpfile function to create a temporary
+               // directory name. Delete the file and keep the name.
+               $tempname = tempnam($path,$prefix);
+               if (!$tempname)
+                       return false;
+
+               if (!unlink($tempname))
+                       return false;
+
+       return $tempname;
+       }
+
+       function getFeedCategory($link, $feed) {
+               $result = db_query($link, "SELECT cat_id FROM ttrss_feeds
+                       WHERE id = '$feed'");
+
+               if (db_num_rows($result) > 0) {
+                       return db_fetch_result($result, 0, "cat_id");
+               } else {
+                       return false;
+               }
+
+       }
+
 ?>