]> git.wh0rd.org - tt-rss.git/blobdiff - functions.php
Extended Actions to include Select by tag (add local modifications, fix
[tt-rss.git] / functions.php
index 0707960da42df0d4dcaecef624e0cd3dd4dc68b0..d6f116a8d1edec1d6e0b1c83bd10e324ab50e082 100644 (file)
 
        $purifier = new HTMLPurifier($config);
 
+       $tz_offset = -1;
+       $utc_tz = new DateTimeZone('UTC');
+       $schema_version = false;
+
        /**
         * Print a timestamped debug message.
         *
                                $site_url = $rss->channel["link"];
                        }
 
+                       $site_url = rewrite_relative_url($fetch_url, $site_url);
+
                        if ($debug_enabled) {
                                _debug("update_rss_feed: checking favicon...");
                        }
                                        if (!$entry_link) $entry_link = $item["link"];
                                }
 
+                               $entry_link = rewrite_relative_url($site_url, $entry_link);
+
                                if ($debug_enabled) {
                                        _debug("update_rss_feed: title $entry_title");
+                                       _debug("update_rss_feed: link $entry_link");
                                }
 
                                if (!$entry_title) $entry_title = date("Y-m-d H:i:s", $entry_timestamp);;
 
                                        // First login ?
                                        if (db_num_rows($result) == 0) {
-                                               $query = "INSERT INTO ttrss_users
+                                               $query2 = "INSERT INTO ttrss_users
                                                                (login,access_level,last_login,created)
                                                                VALUES ('$login', 0, null, NOW())";
-                                               db_query($link, $query);
+                                               db_query($link, $query2);
                                        }
                                }
 
                                // LemonLDAP can send user informations via HTTP HEADER
                                if (defined('AUTO_CREATE_USER') && AUTO_CREATE_USER){
                                        // update user name
-                                       if ($_SERVER['HTTP_USER_NAME']){
-                                               $fullname = db_escape_string($_SERVER['HTTP_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
-                                       if ($_SERVER['HTTP_USER_MAIL']){
-                                               $email = db_escape_string($_SERVER['HTTP_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"]);
                                        }
                                        setcookie("ttrss_lang", $_SESSION["language"],
                                                time() + SESSION_COOKIE_LIFETIME);
                                }
+
+                               // try to remove possible duplicates from feed counter cache
+//                             ccache_cleanup($link, $_SESSION["uid"]);
                        }
 
                } else {
                if (!$owner_uid) $owner_uid = $_SESSION['uid'];
                if (!$timestamp) $timestamp = '1970-01-01 0:00';
 
-               $user_tz_string = get_pref($link, 'USER_TIMEZONE', $owner_uid);
+               global $utc_tz;
+               global $tz_offset;
 
-               try {
-                       $user_tz = new DateTimeZone($user_tz_string);
-               } catch (Exception $e) {
-                       $user_tz = new DateTimeZone('UTC');
+               # We store date in UTC internally
+               $dt = new DateTime($timestamp, $utc_tz);
+
+               if ($tz_offset == -1) {
+
+                       $user_tz_string = get_pref($link, 'USER_TIMEZONE', $owner_uid);
+
+                       try {
+                               $user_tz = new DateTimeZone($user_tz_string);
+                       } catch (Exception $e) {
+                               $user_tz = $utc_tz;
+                       }
+
+                       $tz_offset = $user_tz->getOffset($dt);
                }
 
-               # We store date in UTC internally
-               $dt = new DateTime($timestamp, new DateTimeZone('UTC'));
-               $user_timestamp = $dt->format('U') + $user_tz->getOffset($dt);
+               $user_timestamp = $dt->format('U') + $tz_offset;
 
                if (!$no_smart_dt) {
                        return smart_date_time($link, $user_timestamp,
-                               $user_tz->getOffset($dt), $owner_uid);
+                               $tz_offset, $owner_uid);
                } else {
                        if ($long)
                                $format = get_pref($link, 'LONG_DATE_FORMAT', $owner_uid);
        // script when get_schema_version() is called on an obsolete session
        // created on a previous schema version.
        function get_schema_version($link, $nocache = false) {
-//             if (!$_SESSION["schema_version"] || $nocache) {
+               global $schema_version;
+
+               if (!$schema_version) {
                        $result = db_query($link, "SELECT schema_version FROM ttrss_version");
                        $version = db_fetch_result($result, 0, "schema_version");
-                       $_SESSION["schema_version"] = $version;
+                       $schema_version = $version;
                        return $version;
-//             } else {
-//                     return $_SESSION["schema_version"];
-//             }
+               } else {
+                       return $schema_version;
+               }
        }
 
        function sanity_check($link) {
                global $ERRORS;
 
                $error_code = 0;
-               $schema_version = get_schema_version($link);
+               $schema_version = get_schema_version($link, true);
 
                if ($schema_version != SCHEMA_VERSION) {
                        $error_code = 5;
                                return "Unknown label ($label_id)";
                        }
 
-               } else if ($id > 0) {
+               } else if (is_numeric($id) && $id > 0) {
                        $result = db_query($link, "SELECT title FROM ttrss_feeds WHERE id = '$id'");
                        if (db_num_rows($result) == 1) {
                                return db_fetch_result($result, 0, "title");
                return $search_query_part;
        }
 
+
        function queryFeedHeadlines($link, $feed, $limit, $view_mode, $cat_view, $search, $search_mode, $match_on, $override_order = false, $offset = 0, $owner_uid = 0, $filter = false) {
 
                if (!$owner_uid) $owner_uid = $_SESSION["uid"];
                                if ($cat_view) {
                                        $feed_title = getCategoryTitle($link, $feed);
                                } else {
-                                       if ((int)$feed == $feed && $feed > 0) {
+                                       if (is_numeric($feed) && $feed > 0) {
                                                $result = db_query($link, "SELECT title,site_url,last_error
                                                        FROM ttrss_feeds WHERE id = '$feed' AND owner_uid = $owner_uid");
 
                        } else {
                                // browsing by tag
 
+                               $select_qpart = "SELECT DISTINCT " .
+                                                               "date_entered," .
+                                                               "guid," .
+                                                               "note," .
+                                                               "ttrss_entries.id as id," .
+                                                               "title," .
+                                                               "updated," .
+                                                               "unread," .
+                                                               "feed_id," .
+                                                               "orig_feed_id," .
+                                                               "marked," .
+                                                               "link," .
+                                                               "last_read," .
+                                                               SUBSTRING_FOR_DATE . "(last_read,1,19) as last_read_noms," .
+                                                               $vfeed_query_part .
+                                                               $content_query_part .
+                                                               SUBSTRING_FOR_DATE . "(updated,1,19) as updated_noms," .
+                                                               "score ";
+
                                $feed_kind = "Tags";
+                               $all_tags = explode(",", $feed);
+                               if ($search_mode == 'any') {
+                                       $tag_sql = "tag_name in (" . implode(", ", array_map("db_quote", $all_tags)) . ")";
+                                       $from_qpart = " FROM ttrss_entries,ttrss_user_entries,ttrss_tags ";
+                                       $where_qpart = " WHERE " .
+                                                                  "ref_id = ttrss_entries.id AND " .
+                                                                  "ttrss_user_entries.owner_uid = $owner_uid AND " .
+                                                                  "post_int_id = int_id AND $tag_sql AND " .
+                                                                  $view_query_part .
+                                                                  $search_query_part .
+                                                                  $query_strategy_part . " ORDER BY $order_by " .
+                                                                  $limit_query_part;
 
-                               $result = db_query($link, "SELECT DISTINCT
-                                       date_entered,
-                                       guid,
-                                       note,
-                                       ttrss_entries.id as id,title,
-                                       updated,
-                                       unread,feed_id,orig_feed_id,
-                                       marked,link,last_read,
-                                       ".SUBSTRING_FOR_DATE."(last_read,1,19) as last_read_noms,
-                                       $vfeed_query_part
-                                       $content_query_part
-                                       ".SUBSTRING_FOR_DATE."(updated,1,19) as updated_noms,
-                                       score
-                                       FROM
-                                               ttrss_entries,ttrss_user_entries,ttrss_tags
-                                       WHERE
-                                               ref_id = ttrss_entries.id AND
-                                               ttrss_user_entries.owner_uid = '$owner_uid' AND
-                                               post_int_id = int_id AND tag_name = '$feed' AND
-                                               $view_query_part
-                                               $search_query_part
-                                               $query_strategy_part ORDER BY $order_by
-                                       $limit_query_part");
+                               } else {
+                                       $i = 1;
+                                       $sub_selects = array();
+                                       $sub_ands = array();
+                                       foreach ($all_tags as $term) {
+                                               array_push($sub_selects, "(SELECT post_int_id from ttrss_tags WHERE tag_name = " . db_quote($term) . " AND owner_uid = $owner_uid) as A$i");
+                                               $i++;
+                                       }
+                                       if ($i > 2) {
+                                               $x = 1;
+                                               $y = 2;
+                                               do {
+                                                       array_push($sub_ands, "A$x.post_int_id = A$y.post_int_id");
+                                                       $x++;
+                                                       $y++;
+                                               } while ($y < $i);
+                                       }
+                                       array_push($sub_ands, "A1.post_int_id = ttrss_user_entries.int_id and ttrss_user_entries.owner_uid = $owner_uid");
+                                       array_push($sub_ands, "ttrss_user_entries.ref_id = ttrss_entries.id");
+                                       $from_qpart = " FROM " . implode(", ", $sub_selects) . ", ttrss_user_entries, ttrss_entries";
+                                       $where_qpart = " WHERE " . implode(" AND ", $sub_ands);
+                               }
+                               //                              error_log("TAG SQL: " . $tag_sql);
+                               // $tag_sql = "tag_name = '$feed'";   DEFAULT way
+
+                               //                              error_log("[". $select_qpart . "][" . $from_qpart . "][" .$where_qpart . "]");
+                               $result = db_query($link, $select_qpart . $from_qpart . $where_qpart);
                        }
 
                        return array($result, $feed_title, $feed_site_url, $last_error);
 
                $topmost_article_ids = array();
 
-               if (!$offset) {
-                       $offset = 0;
-               }
-
+               if (!$offset) $offset = 0;
                if ($subop == "undefined") $subop = "";
 
                $subop_split = explode(":", $subop);
 
-               if ($subop == "CatchupSelected") {
+/*             if ($subop == "CatchupSelected") {
                        $ids = explode(",", db_escape_string($_REQUEST["ids"]));
                        $cmode = sprintf("%d", $_REQUEST["cmode"]);
 
                        catchupArticlesById($link, $ids, $cmode);
-               }
+               } */
 
-               if ($subop == "ForceUpdate" && sprintf("%d", $feed) > 0) {
+               if ($subop == "ForceUpdate" && $feed && is_numeric($feed) > 0) {
                        update_rss_feed($link, $feed, true);
                }
 
 
                // FIXME: might break tag display?
 
-               if ($feed > 0 && !$cat_view) {
+               if (is_numeric($feed) && $feed > 0 && !$cat_view) {
                        $result = db_query($link,
                                "SELECT id FROM ttrss_feeds WHERE id = '$feed' LIMIT 1");
 
                        $rtl_content = false;
                }
 
-               /// START /////////////////////////////////////////////////////////////////////////////////
-
                @$search = db_escape_string($_REQUEST["query"]);
 
                if ($search) {
                        $match_on = "both";
                }
 
-               $real_offset = $offset * $limit;
-
                if ($_REQUEST["debug"]) $timing_info = print_checkpoint("H0", $timing_info);
 
+//             error_log("format_headlines_list: [" . $feed . "] subop [" . $subop . "]");
+               if( $search_mode == '' && $subop != '' ){
+                   $search_mode = $subop;
+               }
+//             error_log("search_mode: " . $search_mode);
                $qfh_ret = queryFeedHeadlines($link, $feed, $limit, $view_mode, $cat_view,
-                       $search, $search_mode, $match_on, $override_order, $real_offset);
+                       $search, $search_mode, $match_on, $override_order, $offset);
 
                if ($_REQUEST["debug"]) $timing_info = print_checkpoint("H1", $timing_info);
 
 
                $vgroup_last_feed = $vgr_last_feed;
 
-               /// STOP //////////////////////////////////////////////////////////////////////////////////
-
                if (!$offset) {
 
                        if (db_num_rows($result) > 0) {
-                               $reply['toolbar'] = format_headline_subtoolbar($link, $feed_site_url, $feed_title,
+                               $reply['toolbar'] = format_headline_subtoolbar($link, $feed_site_url,
+                                       $feed_title,
                                        $feed, $cat_view, $search, $match_on, $search_mode, $view_mode,
                                        $last_error);
                        }
 
                if (db_num_rows($result) > 0) {
 
-                       $lnum = $limit*$offset;
+                       $lnum = $offset;
 
                        $num_unread = 0;
                        $cur_feed_title = '';
 
                        $fresh_intl = get_pref($link, "FRESH_ARTICLE_MAX_AGE") * 60 * 60;
 
+                       if ($_REQUEST["debug"]) $timing_info = print_checkpoint("PS", $timing_info);
+
                        while ($line = db_fetch_assoc($result)) {
 
                                $class = ($lnum % 2) ? "even" : "odd";
                                ++$lnum;
                        }
 
+                       if ($_REQUEST["debug"]) $timing_info = print_checkpoint("PE", $timing_info);
+
                } else {
                        $message = "";
 
                        }
                }
 
-#              if (!$offset) {
-#                      if ($headlines_count > 0) print "</div>";
-#                      print "</div>";
-#              }
-
-               #print "]]></content>";
+               if ($_REQUEST["debug"]) $timing_info = print_checkpoint("H2", $timing_info);
 
                return array($topmost_article_ids, $headlines_count, $feed, $disable_cache,
-                       $vgroup_last_feed, $reply['content'], $reply['toolbar']);
+                       $vgroup_last_feed, $reply);
        }
 
 // from here: http://www.roscripts.com/Create_tag_cloud-71.html
        }
 
        function get_self_url_prefix() {
-
-               /* $url_path = "";
-
-               if ($_SERVER['HTTPS'] != "on") {
-                       $url_path = "http://";
-               } else {
-                       $url_path = "https://";
-               }
-
-               $url_path .= $_SERVER['HTTP_HOST'].dirname($_SERVER['PHP_SELF']);
-
-               return $url_path; */
-
                return SELF_URL_PATH;
-
        }
+
        function opml_publish_url($link){
 
                $url_path = get_self_url_prefix();
                return $unread;
        }
 
+       function ccache_cleanup($link, $owner_uid) {
+
+               if (DB_TYPE == "pgsql") {
+                       db_query($link, "DELETE FROM ttrss_counters_cache AS c1 WHERE
+                               (SELECT count(*) FROM ttrss_counters_cache AS c2
+                                       WHERE c1.feed_id = c2.feed_id AND c2.owner_uid = c1.owner_uid) > 1
+                                       AND owner_uid = '$owner_uid'");
+
+                       db_query($link, "DELETE FROM ttrss_cat_counters_cache AS c1 WHERE
+                               (SELECT count(*) FROM ttrss_cat_counters_cache AS c2
+                                       WHERE c1.feed_id = c2.feed_id AND c2.owner_uid = c1.owner_uid) > 1
+                                       AND owner_uid = '$owner_uid'");
+               } else {
+                       db_query($link, "DELETE c1 FROM
+                                       ttrss_counters_cache AS c1,
+                                       ttrss_counters_cache AS c2
+                               WHERE
+                                       c1.owner_uid = '$owner_uid' AND
+                                       c1.owner_uid = c2.owner_uid AND
+                                       c1.feed_id = c2.feed_id");
+
+                       db_query($link, "DELETE c1 FROM
+                                       ttrss_cat_counters_cache AS c1,
+                                       ttrss_cat_counters_cache AS c2
+                               WHERE
+                                       c1.owner_uid = '$owner_uid' AND
+                                       c1.owner_uid = c2.owner_uid AND
+                                       c1.feed_id = c2.feed_id");
+
+               }
+       }
+
        function label_find_id($link, $label, $owner_uid) {
                $result = db_query($link,
                        "SELECT id FROM ttrss_labels2 WHERE caption = '$label'
 
                $num_tags = 0;
 
-/*             if (get_user_theme($link) == "3pane") {
-                       $tag_limit = 3;
-               } else {
-                       $tag_limit = 6;
-               } */
-
                $tag_limit = 6;
 
                $formatted_tags = array();