]> git.wh0rd.org - tt-rss.git/blobdiff - functions.php
rework login process, drop http auth
[tt-rss.git] / functions.php
index 0b3f7ba9fe4c10a0c3d0b4f26c1fb928e2ece6f9..17069635279130b29d6d318ecde1d012fcbbf0c2 100644 (file)
                        $result = db_query($link, "SELECT reg_exp,
                                ttrss_filter_types.name AS name,
                                ttrss_filter_actions.name AS action,
+                               inverse,
                                action_param
                                FROM ttrss_filters,ttrss_filter_types,ttrss_filter_actions WHERE                                        
                                        enabled = true AND
                                        owner_uid = $owner_uid AND
                                        ttrss_filter_types.id = filter_type AND
                                        ttrss_filter_actions.id = action_id AND
-                               (feed_id IS NULL OR feed_id = '$feed')");
+                               (feed_id IS NULL OR feed_id = '$feed') ORDER BY reg_exp");
 
                        while ($line = db_fetch_assoc($result)) {
                                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["inverse"] = sql_bool_to_bool($line["inverse"]);
                        
                                array_push($filters[$line["name"]], $filter);
                        }
                                if ($rss_1_date != "") $entry_timestamp = parse_w3cdtf($rss_1_date);
                                if ($rss_2_date != "") $entry_timestamp = strtotime($rss_2_date);
                                
-                               if ($entry_timestamp == "") {
+                               if ($entry_timestamp == "" || $entry_timestamp == -1 || !$entry_timestamp) {
                                        $entry_timestamp = time();
                                        $no_orig_date = 'true';
                                } else {
                                $entry_author = db_escape_string(strip_tags($item['dc']['creator']));
 
                                if ($item['author']) {
-                                       if (!$entry_author) {
-                                               $entry_author = db_escape_string(strip_tags($item['author']['name']));
-                                       }
 
-                                       if (!$entry_author) {
-                                               $entry_author = db_escape_string(strip_tags($item['author']['email']));
+                                       if (is_array($item['author'])) {
+
+                                               if (!$entry_author) {
+                                                       $entry_author = db_escape_string(strip_tags($item['author']['name']));
+                                               }
+
+                                               if (!$entry_author) {
+                                                       $entry_author = db_escape_string(strip_tags($item['author']['email']));
+                                               }
                                        }
 
                                        if (!$entry_author) {
 
                                if (!$num_comments) $num_comments = 0;
 
-                               $dc_subject = $item['dc']['subject'];
+/*                             $dc_subject = $item['dc']['subject'];
 
                                $subject_tags = false;
 
                                        $subject_tags = $dc_subject;
                                } else if ($dc_subject) {
                                        $subject_tags = array($dc_subject);
-                               }
+                               } */
 
                                # sanitize content
                                
 
 //                                     error_reporting(0);
 
-                                       $tuple = get_filter_name($entry_title, $entry_content, 
-                                               $entry_link, $filters);
-
-                                       $filter_name = $tuple[0];
-                                       $filter_param = $tuple[1];
+                                       $article_filters = get_article_filters($filters, $entry_title, 
+                                                       $entry_content, $entry_link);
 
-                                       if ($filter_name == "filter") {
+                                       if (find_article_filter($article_filters, "filter")) {
                                                continue;
                                        }
 
                                        // okay it doesn't exist - create user entry
                                        if (db_num_rows($result) == 0) {
 
-                                               if ($filter_name != 'catchup') {
+                                               if (!find_article_filter($article_filters, 'catchup')) {
                                                        $unread = 'true';
                                                        $last_read_qpart = 'NULL';
                                                } else {
                                                        $last_read_qpart = 'NOW()';
                                                }                                               
 
-                                               if ($filter_name == 'mark') {
+                                               if (find_article_filter($article_filters, 'mark')) {
                                                        $marked = 'true';
                                                } else {
                                                        $marked = 'false';
 
                                # check for manual tags
 
-                               if ($filter_name == "tag") {
+                               $tag_filter = find_article_filter($article_filters, "tag"); 
 
-                                       $manual_tags = trim_array(split(",", $filter_param));
+                               if ($tag_filter) {
 
-                                       foreach ($manual_tags as $tag) {                                        
+                                       $manual_tags = trim_array(split(",", $tag_filter[1]));
+
+                                       foreach ($manual_tags as $tag) {
                                                if (tag_is_valid($tag)) {
                                                        array_push($entry_tags, $tag);
                                                }
                                        }
                                }
 
-                               if ($subject_tags) {
+/*                             if ($subject_tags) {
                                        foreach ($subject_tags as $tag) {
                                                if (tag_is_valid($tag)) {
                                                        array_push($entry_tags, $tag);
                                                }
                                        }
-                               }
+                               } */
 
                                if (count($entry_tags) > 0) {
                                
                                                $entry_int_id = db_fetch_result($result, 0, "int_id");
                                                
                                                foreach ($entry_tags as $tag) {
-                                                       $tag = db_escape_string(strtolower($tag));
+                                                       $tag = db_escape_string(mb_strtolower(strip_tags($tag)));
 
                                                        $tag = str_replace("+", " ", $tag);     
                                                        $tag = str_replace("technorati tag: ", "", $tag);
-       
+
+                                                       if (!tag_is_valid($tag)) continue;
+                                                       
                                                        $result = db_query($link, "SELECT id FROM ttrss_tags            
                                                                WHERE tag_name = '$tag' AND post_int_id = '$entry_int_id' AND 
                                                                owner_uid = '$owner_uid' LIMIT 1");
                print "</select>";
        }
 
-       function get_filter_name($title, $content, $link, $filters) {
+       function get_article_filters($filters, $title, $content, $link) {
+               $matches = array();
 
                if ($filters["title"]) {
                        foreach ($filters["title"] as $filter) {
-                               $reg_exp = $filter["reg_exp"];                  
-                               if (preg_match("/$reg_exp/i", $title)) {
-                                       return array($filter["action"], $filter["action_param"]);
-                               }
-                       }
-               }
-
-               if ($filters["content"]) {
-                       foreach ($filters["content"] as $filter) {
-                               $reg_exp = $filter["reg_exp"];                  
-                               if (preg_match("/$reg_exp/i", $content)) {
-                                       return array($filter["action"], $filter["action_param"]);
-                               }               
-                       }
-               }
-
-               if ($filters["both"]) {
-                       foreach ($filters["both"] as $filter) {                 
                                $reg_exp = $filter["reg_exp"];          
-                               if (preg_match("/$reg_exp/i", $title) || 
-                                       preg_match("/$reg_exp/i", $content)) {
-                                               return array($filter["action"], $filter["action_param"]);
-                               }
-                       }
-               }
-
-               if ($filters["link"]) {
-                       $reg_exp = $filter["reg_exp"];
-                       foreach ($filters["link"] as $filter) {
-                               $reg_exp = $filter["reg_exp"];
-                               if (preg_match("/$reg_exp/i", $link)) {
-                                       return array($filter["action"], $filter["action_param"]);
-                               }
-                       }
-               }
-
-               return false;
-       }
-
-       function get_filter_matches($title, $content, $link, $filters) {
+                               $inverse = $filter["inverse"];  
+                               if ((!$inverse && preg_match("/$reg_exp/i", $title)) || 
+                                               ($inverse && !preg_match("/$reg_exp/i", $title))) {
 
-               $matches = array();
-
-               if ($filters["title"]) {
-                       foreach ($filters["title"] as $filter) {
-                               $reg_exp = $filter["reg_exp"];                  
-                               if (preg_match("/$reg_exp/i", $title)) {
                                        array_push($matches, array($filter["action"], $filter["action_param"]));
                                }
                        }
 
                if ($filters["content"]) {
                        foreach ($filters["content"] as $filter) {
-                               $reg_exp = $filter["reg_exp"];                  
-                               if (preg_match("/$reg_exp/i", $content)) {
+                               $reg_exp = $filter["reg_exp"];
+                               $inverse = $filter["inverse"];
+
+                               if ((!$inverse && preg_match("/$reg_exp/i", $content)) || 
+                                               ($inverse && !preg_match("/$reg_exp/i", $content))) {
+
                                        array_push($matches, array($filter["action"], $filter["action_param"]));
                                }               
                        }
                if ($filters["both"]) {
                        foreach ($filters["both"] as $filter) {                 
                                $reg_exp = $filter["reg_exp"];          
-                               if (preg_match("/$reg_exp/i", $title) || 
-                                       preg_match("/$reg_exp/i", $content)) {
+                               $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"]));
+                                       }
+                               } else {
+                                       if (preg_match("/$reg_exp/i", $title) || preg_match("/$reg_exp/i", $content)) {
                                                array_push($matches, array($filter["action"], $filter["action_param"]));
+                                       }
                                }
                        }
                }
                        $reg_exp = $filter["reg_exp"];
                        foreach ($filters["link"] as $filter) {
                                $reg_exp = $filter["reg_exp"];
-                               if (preg_match("/$reg_exp/i", $link)) {
+                               $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"]));
                                }
                        }
                return $matches;
        }
 
+       function find_article_filter($filters, $filter_name) {
+               foreach ($filters as $f) {
+                       if ($f[0] == $filter_name) {
+                               return $f;
+                       };
+               }
+               return false;
+       }
+
        function printFeedEntry($feed_id, $class, $feed_title, $unread, $icon_file, $link,
                $rtl_content = false, $last_updated = false, $last_error = false) {
 
                }
 
                $feed = "<a title=\"$link_title\" id=\"FEEDL-$feed_id\" 
-                       href=\"javascript:viewfeed('$feed_id', '', false);\">$feed_title</a>";
+                       href=\"javascript:viewfeed('$feed_id', '', false, '', false, 0);\">$feed_title</a>";
 
                print "<li id=\"FEEDR-$feed_id\" class=\"$class\">";
                if (get_pref($link, 'ENABLE_FEED_ICONS')) {
                return preg_replace('/\/[^\/]*$/', "", $_SERVER["REQUEST_URI"]);
        }
 
-       function get_login_redirect() {
-               $server = $_SERVER["SERVER_NAME"];
-
-               if (ENABLE_LOGIN_SSL) {
-                       $protocol = "https";
-               } else {
-                       $protocol = "http";
-               }               
-
-               $url_path = get_script_urlpath();
-
-               $redirect_uri = "$protocol://$server$url_path/login.php";
-
-               return $redirect_uri;
-       }
-
        function validate_session($link) {
                if (SESSION_CHECK_ADDRESS && $_SESSION["uid"]) {
                        if ($_SESSION["ip_address"]) {
                return true;
        }
 
-       function basic_nosid_redirect_check() {
-               if (!SINGLE_USER_MODE) {
-                       if (!$_COOKIE[get_session_cookie_name()]) {
-                               $redirect_uri = get_login_redirect();
-                               $return_to = preg_replace('/.*?\//', '', $_SERVER["REQUEST_URI"]);
-                               header("Location: $redirect_uri?rt=$return_to");
-                               exit;
-                       }
-               }
-       }
-
        function login_sequence($link) {
                if (!SINGLE_USER_MODE) {
 
 
                        if (!validate_session($link)) {
                                logout_user();
-                               $redirect_uri = get_login_redirect();
-                               $return_to = preg_replace('/.*?\//', '', $_SERVER["REQUEST_URI"]);
-                               header("Location: $redirect_uri?rt=$return_to");
+                               render_login_form($link);
                                exit;
                        }
 
-                       if (!USE_HTTP_AUTH) {
-                               if (!$_SESSION["uid"]) {
-                                       $redirect_uri = get_login_redirect();
-                                       $return_to = preg_replace('/.*?\//', '', $_SERVER["REQUEST_URI"]);
-                                       header("Location: $redirect_uri?rt=$return_to");
-                                       exit;
-                               }
-                       } else {
-                               if (!$_SESSION["uid"]) {
-                                       if (!$_SERVER["PHP_AUTH_USER"]) {
+                       $login_action = $_POST["login_action"];
 
-                                               header('WWW-Authenticate: Basic realm="Tiny Tiny RSS"');
-                                               header('HTTP/1.0 401 Unauthorized');
-                                               exit;
-                                               
-                                       } else {
-                                               $auth_result = authenticate_user($link, 
-                                                       $_SERVER["PHP_AUTH_USER"], $_SERVER["PHP_AUTH_PW"]);
+                       # try to authenticate user if called from login form                    
+                       if ($login_action == "do_login") {
+                               $login = $_POST["login"];
+                               $password = $_POST["password"];
 
-                                               if (!$auth_result) {
-                                                       header('WWW-Authenticate: Basic realm="Tiny Tiny RSS"');
-                                                       header('HTTP/1.0 401 Unauthorized');
-                                                       exit;
-                                               }
-                                       }
-                               }                               
+                               if (authenticate_user($link, $login, $password)) {
+                                       $_POST["password"] = "";
+                                       return;
+                               }
+                       }
+
+                       if (!$_SESSION["uid"]) {
+                               render_login_form($link);
+                               exit;
                        }
                } else {
                        return authenticate_user($link, "admin", null);
                        return false;
                } else {
                        return true;
-               } 
+               }
        }
 
        function file_is_locked($filename) {
 
        function subscribe_to_feed($link, $feed_link, $cat_id = 0) {
 
+               # check for feed:http://url
                $feed_link = trim(preg_replace("/^feed:/", "", $feed_link));
 
+               # check for feed://URL
+               if (strpos($feed_link, "//") === 0) {
+                       $feed_link = "http:$feed_link";
+               }
+
                if ($feed_link == "") return;
 
                if ($cat_id == "0" || !$cat_id) {
                        print "<param key=\"daemon_is_running\" value=\"".
                                sprintf("%d", file_is_locked("update_daemon.lock")) . "\"/>";
                }
+               if (CHECK_FOR_NEW_VERSION && $_SESSION["access_level"] >= 10) {
+                       
+                       if ($_SESSION["last_version_check"] + 600 < time()) {
+                               $new_version_details = check_for_update($link);
+
+                               print "<param key=\"new_version_available\" value=\"".
+                                       sprintf("%d", $new_version_details != ""). "\"/>";
+
+                               $_SESSION["last_version_check"] = time();
+                       }
+               }
+
                print "</runtime-info>";
        }
 
        
                                $content_query_part = "content as content_preview,";
 
+                               if ($limit_query_part) {
+                                       $offset_query_part = "OFFSET $offset";
+                               }
+
                                $query = "SELECT 
                                                guid,
                                                ttrss_entries.id,ttrss_entries.title,
                                        $search_query_part
                                        $view_query_part
                                        $query_strategy_part ORDER BY $order_by
-                                       $limit_query_part OFFSET $offset";
+                                       $limit_query_part $offset_query_part";
                                        
                                $result = db_query($link, $query);
        
                return array($tmp, $headlines_count);
        }
 
-       function check_for_update($link) {
+       function check_for_update($link, $brief_fmt = true) {
                $releases_feed = "http://tt-rss.spb.ru/releases.rss";
 
                if (!CHECK_FOR_NEW_VERSION || $_SESSION["access_level"] < 10) {
                        $content = sanitize_rss($latest_item["description"]);
 
                        if (version_compare(VERSION, $latest_version) == -1) {
-                               return "<div class=\"notice\"><a href=\"javascript:showBlockElement('milestoneDetails')\">      
-                                       New version of Tiny-Tiny RSS ($latest_version) is available (click for details)</a>
-                                       <div id=\"milestoneDetails\">$content</div></div>";
+                               if ($brief_fmt) {
+                                       return "<div class=\"notice\"><a href=\"javascript:showBlockElement('milestoneDetails')\">      
+                                               New version of Tiny-Tiny RSS ($latest_version) is available (click for details)</a>
+                                               <div id=\"milestoneDetails\">$content</div></div>";
+                               } else {
+                                       return "New version of Tiny-Tiny RSS ($latest_version) is available:
+                                               <div class='milestoneDetails'>$content</div>
+                                               Visit <a target=\"_new\" href=\"http://tt-rss.spb.ru/\">official site</a> for
+                                               download and update information.";      
+                               }
+
                        }                       
                }
        }
        function print_headline_subtoolbar($link, $feed_site_url, $feed_title, 
                        $bottom = false, $rtl_content = false, $feed_id = 0,
                        $is_cat = false, $search = false, $match_on = false,
-                       $search_mode = false, $offset = 0) {
+                       $search_mode = false, $offset = 0, $limit = 0) {
 
                        $user_page_offset = $offset + 1;
 
 
                        }
 
-                       print "<td class=\"headlineActions$rtl_cpart\">
-                               <ul class=\"headlineDropdownMenu\">
-                               <li class=\"top2\">
-                               Select:
-                                       <a href=\"$sel_all_link\">All</a>,
-                                       <a href=\"$sel_unread_link\">Unread</a>,
-                                       <a href=\"$sel_none_link\">None</a></li>
-                               <li class=\"vsep\">&nbsp;</li>
-                               <li class=\"top\">Selection<ul>
-                                       <li onclick=\"$tog_unread_link\">Toggle unread</li>
-                                       <li onclick=\"$tog_marked_link\">Toggle starred</li></ul></li>
-                               <li class=\"vsep\">&nbsp;</li>
-                               <li class=\"top\"><a href=\"$catchup_page_link\">Mark as read</a><ul>
-                                       <li onclick=\"$catchup_page_link\">This page</li>
-                                       <li onclick=\"$catchup_feed_link\">Entire feed</li></ul></li>
-                               <li class=\"vsep\">&nbsp;</li>
-                               <!-- <li class=\"top2\">
-                               Page:
-                                       <a href=\"$page_prev_link\">Previous</a>,
-                                       <a href=\"$page_next_link\">Next</a></li> -->
-
-                               <li class=\"top\"><a href=\"$page_next_link\">Next page</a><ul>
-                                       <li onclick=\"$page_prev_link\">Previous page</li>
-                                       <li onclick=\"$page_first_link\">First page</li></ul></li>
-                               </ul>  
-
-                               </td>"; 
+                       if (!strstr($_SESSION["client.userAgent"], "MSIE")) {
 
+                               print "<td class=\"headlineActions$rtl_cpart\">
+                                       <ul class=\"headlineDropdownMenu\">
+                                       <li class=\"top2\">
+                                       Select:
+                                               <a href=\"$sel_all_link\">All</a>,
+                                               <a href=\"$sel_unread_link\">Unread</a>,
+                                               <a href=\"$sel_none_link\">None</a></li>
+                                       <li class=\"vsep\">&nbsp;</li>
+                                       <li class=\"top\">Selection<ul>
+                                               <li onclick=\"$tog_unread_link\">Toggle unread</li>
+                                               <li onclick=\"$tog_marked_link\">Toggle starred</li></ul></li>
+                                       <li class=\"vsep\">&nbsp;</li>
+                                       <li class=\"top\"><a href=\"$catchup_page_link\">Mark as read</a><ul>
+                                               <li onclick=\"$catchup_page_link\">This page</li>
+                                               <li onclick=\"$catchup_feed_link\">Entire feed</li></ul></li>
+                                       <li class=\"vsep\">&nbsp;</li>
+                                       <!-- <li class=\"top2\">
+                                       Page:
+                                               <a href=\"$page_prev_link\">Previous</a>,
+                                               <a href=\"$page_next_link\">Next</a></li> -->";
+
+                                       if ($limit != 0) {
+                                               print "
+                                               <li class=\"top\"><a href=\"$page_next_link\">Next page</a><ul>
+                                                       <li onclick=\"$page_prev_link\">Previous page</li>
+                                                       <li onclick=\"$page_first_link\">First page</li></ul></li>
+                                                       </ul>";
+                                       }
+
+                                       print " 
+                                       </td>"; 
+
+                       } else {
                        // old style subtoolbar:
 
-/*                     print "<td class=\"headlineActions$rtl_cpart\">".
-                               _('Select:')."
-                                                       <a href=\"$sel_all_link\">All</a>,
-                                                       <a href=\"$sel_unread_link\">Unread</a>,
-                                                       <a href=\"$sel_none_link\">None</a>
-                                       &nbsp;&nbsp;".
-                                       _('Toggle:')." <a href=\"$tog_unread_link\">Unread</a>,
-                                               <a href=\"$tog_marked_link\">Starred</a>
-                                       &nbsp;&nbsp;".
-                                       _('Mark as read:')."
-                                               <a href=\"#\" onclick=\"$catchup_page_link\">Page</a>,
-                                               <a href=\"#\" onclick=\"$catchup_feed_link\">Feed</a>";
-                       print "</td>";  */
+                               print "<td class=\"headlineActions$rtl_cpart\">".
+                                       _('Select:')."
+                                                               <a href=\"$sel_all_link\">All</a>,
+                                                               <a href=\"$sel_unread_link\">Unread</a>,
+                                                               <a href=\"$sel_none_link\">None</a>
+                                               &nbsp;&nbsp;".
+                                               _('Toggle:')." <a href=\"$tog_unread_link\">Unread</a>,
+                                                       <a href=\"$tog_marked_link\">Starred</a>
+                                               &nbsp;&nbsp;".
+                                               _('Mark as read:')."
+                                                       <a href=\"#\" onclick=\"$catchup_page_link\">Page</a>,
+                                                       <a href=\"#\" onclick=\"$catchup_feed_link\">Feed</a>";
+                               print "</td>";  
+
+                       }
 
                        if ($search && $feed_id >= 0 && get_pref($link, 'ENABLE_LABELS') && GLOBAL_ENABLE_LABELS) {
                                print "<td class=\"headlineActions$rtl_cpart\">
 
                $tmp_result = db_query($link, "SELECT DISTINCT tag_name FROM
                        ttrss_tags WHERE post_int_id = (SELECT int_id FROM ttrss_user_entries WHERE
-                               ref_id = '$a_id' AND owner_uid = '".$_SESSION["uid"]."') ORDER BY tag_name");
+                               ref_id = '$a_id' AND owner_uid = '".$_SESSION["uid"]."' LIMIT 1) ORDER BY tag_name");
 
                $tags = array();        
        
        }
 
        function tag_is_valid($tag) {
-               return ($tag !='' && !preg_match("/^[0-9]*$/", $tag));
+               if ($tag == '') return false;
+               if (preg_match("/^[0-9]*$/", $tag)) return false;
+
+               $tag = iconv("utf-8", "utf-8", $tag);
+               if (!$tag) return false;
+
+               return true;
+       }
+
+       function render_login_form($link) {
+               require_once "login_form.php";
        }
 
 ?>