]> git.wh0rd.org - tt-rss.git/blobdiff - functions.php
add schema update scripts for 1.2.2
[tt-rss.git] / functions.php
index 8175a43261a2d50dfc891abff8bde8757e7e6b72..885505c922545e8023d4d07fcb7e6eb751772a1c 100644 (file)
@@ -9,8 +9,14 @@
        require_once 'config.php';
        require_once 'db-prefs.php';
        require_once 'compat.php';
+       require_once 'errors.php';
+       require_once 'version.php';
 
-       require_once 'magpierss/rss_utils.inc';
+       if (RSS_BACKEND_TYPE == "magpie") {     
+               require_once 'magpierss/rss_utils.inc';
+       } else if (RSS_BACKEND_TYPE == "simplepie") {
+               require_once 'simplepie/simplepie.inc';
+       }
 
        define('MAGPIE_OUTPUT_ENCODING', 'UTF-8');
 
@@ -24,7 +30,9 @@
                                (SELECT date_entered FROM ttrss_entries WHERE
                                        id = ref_id) < NOW() - INTERVAL '$purge_interval days'"); */
 
-                       if (PG_VERSION == "7.4" || PG_VERSION == "8.0") {
+                       $pg_version = get_pgsql_version($link);
+
+                       if (preg_match("/^7\./", $pg_version) || preg_match("/^8\.0/", $pg_version)) {
 
                                $result = db_query($link, "DELETE FROM ttrss_user_entries WHERE 
                                        ttrss_entries.id = ref_id AND 
@@ -39,7 +47,7 @@
                                        WHERE ttrss_entries.id = ref_id AND 
                                        marked = false AND 
                                        feed_id = '$feed_id' AND 
-                                       ttrss_entries.date_entered < NOW() - INTERVAL '$purge_interval days')");
+                                       ttrss_entries.date_entered < NOW() - INTERVAL '$purge_interval days'");
                        }
 
                        $rows = pg_affected_rows($result);
                        }
 
                }
-               error_reporting(0);
-               $rss = fetch_rss($fetch_url);
 
-               error_reporting (DEFAULT_ERROR_LEVEL);
+               if (RSS_BACKEND_TYPE == "magpie") {             
+                       error_reporting(0);
+                       $rss = fetch_rss($fetch_url);
+                       error_reporting (DEFAULT_ERROR_LEVEL);
+               } else if (RSS_BACKEND_TYPE == "simplepie") {
+                       
+                       if (!file_exists(SIMPLEPIE_CACHE_DIR)) {
+                                       mkdir(SIMPLEPIE_CACHE_DIR);
+                       }
 
+                       $rss = new SimplePie();
+                       $rss->feed_url($fetch_url);
+                       $rss->cache_location(SIMPLEPIE_CACHE_DIR);
+                       $rss->init();
+               }
+               
                $feed = db_escape_string($feed);
 
-               if ($rss) {
+               $rss_check = $rss;
 
+               if (RSS_BACKEND_TYPE == "simplepie") {
+                       $rss_check = $rss->data;
+               }
+               
+               if ($rss_check) {
+                       
 //                     db_query($link, "BEGIN");
 
                        $result = db_query($link, "SELECT title,icon_url,site_url,owner_uid
                        }
 
                        if (!$registered_title || $registered_title == "[Unknown]") {
-                               $feed_title = db_escape_string($rss->channel["title"]);
+                       
+                               if (RSS_BACKEND_TYPE == "magpie") {                             
+                                       $feed_title = db_escape_string($rss->channel["title"]);
+                               } else {
+                                       $feed_title = $rss->get_feed_title();
+                               }
+                               
                                db_query($link, "UPDATE ttrss_feeds SET 
                                        title = '$feed_title' WHERE id = '$feed'");
                        }
 
-                       $site_url = $rss->channel["link"];
-                       // weird, weird Magpie
-                       if (!$site_url) $site_url = db_escape_string($rss->channel["link_"]);
+                       if (RSS_BACKEND_TYPE == "magpie") {                             
+                               $site_url = $rss->channel["link"];
+                               // weird, weird Magpie
+                               if (!$site_url) $site_url = db_escape_string($rss->channel["link_"]);
+                       } else {
+                               $site_url = $rss->get_feed_link();
+                       }
 
                        if ($site_url && $orig_site_url != db_escape_string($site_url)) {
                                db_query($link, "UPDATE ttrss_feeds SET 
 
 //                     print "I: " . $rss->channel["image"]["url"];
 
-                       $icon_url = $rss->image["url"];
+                       if (RSS_BACKEND_TYPE == "magpie") {
+                               $icon_url = $rss->image["url"];
+                       } else {
+                               $icon_url = $rss->get_image_url(); # FIXME
+                       }
 
                        if ($icon_url && !$orig_icon_url != db_escape_string($icon_url)) {
                                $icon_url = db_escape_string($icon_url);
                                array_push($filters[$line["name"]], $filter);
                        }
 
-                       $iterator = $rss->items;
+                       if (RSS_BACKEND_TYPE == "magpie") {                     
+                               $iterator = $rss->items;
 
-                       if (!$iterator || !is_array($iterator)) $iterator = $rss->entries;
-                       if (!$iterator || !is_array($iterator)) $iterator = $rss;
+                               if (!$iterator || !is_array($iterator)) $iterator = $rss->entries;
+                               if (!$iterator || !is_array($iterator)) $iterator = $rss;
+
+                       } else {
+                               $iterator = $rss->get_items();
+                       }
 
                        if (!is_array($iterator)) {
-                               db_query($link, "UPDATE ttrss_feeds 
+                               /* db_query($link, "UPDATE ttrss_feeds 
                                        SET last_error = 'Parse error: can\'t find any articles.'
-                                               WHERE id = '$feed'");
+                                               WHERE id = '$feed'"); */
                                return; // WTF?
                        }
 
                        foreach ($iterator as $item) {
+
+                               if (RSS_BACKEND_TYPE == "magpie") {
+                               
+                                       $entry_guid = $item["id"];
+               
+                                       if (!$entry_guid) $entry_guid = $item["guid"];
+                                       if (!$entry_guid) $entry_guid = $item["link"];
        
-                               $entry_guid = $item["id"];
+                                       if (!$entry_guid) continue;
        
-                               if (!$entry_guid) $entry_guid = $item["guid"];
-                               if (!$entry_guid) $entry_guid = $item["link"];
-
-                               if (!$entry_guid) continue;
-
-                               $entry_timestamp = "";
-
-                               $rss_2_date = $item['pubdate'];
-                               $rss_1_date = $item['dc']['date'];
-                               $atom_date = $item['issued'];
-                               if (!$atom_date) $atom_date = $item['updated'];
-                       
-                               if ($atom_date != "") $entry_timestamp = parse_w3cdtf($atom_date);
-                               if ($rss_1_date != "") $entry_timestamp = parse_w3cdtf($rss_1_date);
-                               if ($rss_2_date != "") $entry_timestamp = strtotime($rss_2_date);
+                                       $entry_timestamp = "";
+       
+                                       $rss_2_date = $item['pubdate'];
+                                       $rss_1_date = $item['dc']['date'];
+                                       $atom_date = $item['issued'];
+                                       if (!$atom_date) $atom_date = $item['updated'];
                                
-                               if ($entry_timestamp == "") {
-                                       $entry_timestamp = time();
-                                       $no_orig_date = 'true';
-                               } else {
-                                       $no_orig_date = 'false';
-                               }
-
-                               $entry_timestamp_fmt = strftime("%Y/%m/%d %H:%M:%S", $entry_timestamp);
+                                       if ($atom_date != "") $entry_timestamp = parse_w3cdtf($atom_date);
+                                       if ($rss_1_date != "") $entry_timestamp = parse_w3cdtf($rss_1_date);
+                                       if ($rss_2_date != "") $entry_timestamp = strtotime($rss_2_date);
+                                       
+                                       if ($entry_timestamp == "") {
+                                               $entry_timestamp = time();
+                                               $no_orig_date = 'true';
+                                       } else {
+                                               $no_orig_date = 'false';
+                                       }
+       
+                                       $entry_timestamp_fmt = strftime("%Y/%m/%d %H:%M:%S", $entry_timestamp);
+       
+                                       $entry_title = $item["title"];
+       
+                                       // strange Magpie workaround
+                                       $entry_link = $item["link_"];
+                                       if (!$entry_link) $entry_link = $item["link"];
+       
+                                       if (!$entry_title) continue;
+                                       if (!$entry_link) continue;
+       
+                                       $entry_content = $item["content:escaped"];
+       
+                                       if (!$entry_content) $entry_content = $item["content:encoded"];
+                                       if (!$entry_content) $entry_content = $item["content"];
+                                       if (!$entry_content) $entry_content = $item["atom_content"];
+                                       if (!$entry_content) $entry_content = $item["summary"];
+                                       if (!$entry_content) $entry_content = $item["description"];
+       
+       //                              if (!$entry_content) continue;
+       
+                                       // WTF
+                                       if (is_array($entry_content)) {
+                                               $entry_content = $entry_content["encoded"];
+                                               if (!$entry_content) $entry_content = $entry_content["escaped"];
+                                       }
+       
+       //                              print_r($item);
+       //                              print_r(htmlspecialchars($entry_content));
+       //                              print "<br>";
+       
+                                       $entry_content_unescaped = $entry_content;
+                                       $content_hash = "SHA1:" . sha1(strip_tags($entry_content));
+       
+                                       $entry_comments = $item["comments"];
+       
+                                       $entry_author = db_escape_string($item['dc']['creator']);
+       
+                                       $entry_guid = db_escape_string($entry_guid);
+       
+                                       $result = db_query($link, "SELECT id FROM       ttrss_entries 
+                                               WHERE guid = '$entry_guid'");
+       
+                                       $entry_content = db_escape_string($entry_content);
+                                       $entry_title = db_escape_string($entry_title);
+                                       $entry_link = db_escape_string($entry_link);
+                                       $entry_comments = db_escape_string($entry_comments);
+       
+                                       $num_comments = db_escape_string($item["slash"]["comments"]);
+       
+                                       if (!$num_comments) $num_comments = 0;
 
-                               $entry_title = $item["title"];
+                               } else if (RSS_BACKEND_TYPE == "simplepie") {
 
-                               // strange Magpie workaround
-                               $entry_link = $item["link_"];
-                               if (!$entry_link) $entry_link = $item["link"];
+                                       $entry_guid = $item->get_id();
 
-                               if (!$entry_title) continue;
-                               if (!$entry_link) continue;
+                                       if (!$entry_guid) {
+                                                       $entry_guid = $item->get_permalink();
+                                       }
+       
+                                       if (!$entry_guid) continue;
+                                       
+                                       $entry_timestamp = $item->get_date("U");
+                                       
+                                       if ($entry_timestamp == "") {
+                                               $entry_timestamp = time();
+                                               $no_orig_date = 'true';
+                                       } else {
+                                               $no_orig_date = 'false';
+                                       }
+       
+                                       $entry_timestamp_fmt = strftime("%Y/%m/%d %H:%M:%S", $entry_timestamp);
+       
+                                       $entry_title = $item->get_title();
+                                       $entry_link = $item->get_permalink();
+       
+                                       if (!$entry_title) continue;
+                                       if (!$entry_link) continue;
 
-                               $entry_content = $item["content:escaped"];
+                                       $entry_content = $item->get_description();
+                                               
+//                                     print_r(htmlspecialchars($entry_content));
+//                                     print "<br>";
+       
+                                       $entry_content_unescaped = $entry_content;
+                                       $content_hash = "SHA1:" . sha1(strip_tags($entry_content));
+       
+                                       $entry_comments = ""; # FIXME
+                                       
+                                       $entry_author = $item->get_author(0);
 
-                               if (!$entry_content) $entry_content = $item["content:encoded"];
-                               if (!$entry_content) $entry_content = $item["content"];
-                               if (!$entry_content) $entry_content = $item["summary"];
-                               if (!$entry_content) $entry_content = $item["description"];
+                                       $entry_author = db_escape_string($entry_author->name);
+                                       
+                                       $entry_guid = db_escape_string($entry_guid);
 
-//                             if (!$entry_content) continue;
+                                       $result = db_query($link, "SELECT id FROM       ttrss_entries 
+                                               WHERE guid = '$entry_guid'");
+       
+                                       $entry_content = db_escape_string($entry_content);
+                                       $entry_title = db_escape_string($entry_title);
+                                       $entry_link = db_escape_string($entry_link);
+                                       $entry_comments = db_escape_string($entry_comments);
+       
+                                       $num_comments = 0; # FIXME
+       
+                                       if (!$num_comments) $num_comments = 0;
 
-                               // WTF
-                               if (is_array($entry_content)) {
-                                       $entry_content = $entry_content["encoded"];
-                                       if (!$entry_content) $entry_content = $entry_content["escaped"];
                                }
 
-//                             print_r($item);
-//                             print_r(htmlspecialchars($entry_content));
-//                             print "<br>";
-
-                               $entry_content_unescaped = $entry_content;
-                               $content_hash = "SHA1:" . sha1(strip_tags($entry_content));
-
-                               $entry_comments = $item["comments"];
-
-                               $entry_author = db_escape_string($item['dc']['creator']);
-
-                               $entry_guid = db_escape_string($entry_guid);
-
-                               $result = db_query($link, "SELECT id FROM       ttrss_entries 
-                                       WHERE guid = '$entry_guid'");
-
-                               $entry_content = db_escape_string($entry_content);
-                               $entry_title = db_escape_string($entry_title);
-                               $entry_link = db_escape_string($entry_link);
-                               $entry_comments = db_escape_string($entry_comments);
-
-                               $num_comments = db_escape_string($item["slash"]["comments"]);
-
-                               if (!$num_comments) $num_comments = 0;
-
                                db_query($link, "BEGIN");
 
                                if (db_num_rows($result) == 0) {
                                                        $unread = 'false';
                                                        $last_read_qpart = 'NOW()';
                                                }                                               
+
+                                               if ($filter_name == 'mark') {
+                                                       $marked = 'true';
+                                               } else {
+                                                       $marked = 'false';
+                                               }
                                                
                                                $result = db_query($link,
                                                        "INSERT INTO ttrss_user_entries 
-                                                               (ref_id, owner_uid, feed_id, unread, last_read) 
+                                                               (ref_id, owner_uid, feed_id, unread, last_read, marked
                                                        VALUES ('$ref_id', '$owner_uid', '$feed', $unread,
-                                                               $last_read_qpart)");
+                                                               $last_read_qpart, $marked)");
                                        }
                                        
                                        $post_needs_update = false;
                                                                num_comments = '$num_comments'
                                                        WHERE id = '$ref_id'");
 
-                                               db_query($link, "UPDATE ttrss_user_entries 
-                                                       SET last_read = null WHERE ref_id = '$ref_id' AND unread = false");
+                                               if (get_pref($link, "MARK_UNREAD_ON_UPDATE")) {
+                                                       db_query($link, "UPDATE ttrss_user_entries 
+                                                               SET last_read = null, unread = true WHERE ref_id = '$ref_id'");
+                                               } else {
+                                                       db_query($link, "UPDATE ttrss_user_entries 
+                                                               SET last_read = null WHERE ref_id = '$ref_id' AND unread = false");
+                                               }
 
                                        }
                                }
        }
 
        function print_select($id, $default, $values, $attributes = "") {
-               print "<select id=\"$id\" $attributes>";
+               print "<select name=\"$id\" id=\"$id\" $attributes>";
                foreach ($values as $v) {
                        if ($v == $default)
                                $sel = " selected";
                print "</select>";
        }
 
+       function print_select_hash($id, $default, $values, $attributes = "") {
+               print "<select name=\"$id\" id='$id' $attributes>";
+               foreach (array_keys($values) as $v) {
+                       if ($v == $default)
+                               $sel = "selected";
+                        else
+                               $sel = "";
+                       
+                       print "<option $sel value=\"$v\">".$values[$v]."</option>";
+               }
+
+               print "</select>";
+       }
+
        function get_filter_name($title, $content, $link, $filters) {
 
                if ($filters["title"]) {
                        $rtl_tag = "dir=\"ltr\"";
                }
 
+               $error_notify_msg = "";
+               
                if ($last_error) {
                        $link_title = "Error: $last_error ($last_updated)";
+                       $error_notify_msg = "(Error)";
                } else if ($last_updated) {
                        $link_title = "Updated: $last_updated";
                }
 
                print " <span $rtl_tag $fctr_class id=\"FEEDCTR-$feed_id\">
                         (<span id=\"FEEDU-$feed_id\">$unread</span>)</span>";
-               
+
+               if (get_pref($link, "EXTENDED_FEEDLIST")) {                      
+                       print "<div class=\"feedExtInfo\">
+                               <span id=\"FLUPD-$feed_id\">$last_updated $error_notify_msg</span></div>";
+               }
+                        
                print "</li>";
 
        }
                db_query($link, "COMMIT");
 
        }
-       
-       function authenticate_user($link, $login, $password) {
 
-               $pwd_hash = 'SHA1:' . sha1($password);
+       function lookup_user_id($link, $user) {
 
-               $result = db_query($link, "SELECT id,login,access_level FROM ttrss_users WHERE 
-                       login = '$login' AND pwd_hash = '$pwd_hash'");
+               $result = db_query($link, "SELECT id FROM ttrss_users WHERE 
+                       login = '$login'");
 
                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");
+                       return db_fetch_result($result, 0, "id");
+               } else {
+                       return false;
+               }
+       }
 
-                       db_query($link, "UPDATE ttrss_users SET last_login = NOW() WHERE id = " . 
-                               $_SESSION["uid"]);
+       function authenticate_user($link, $login, $password) {
 
-                       $user_theme = get_user_theme_path($link);
+               if (!SINGLE_USER_MODE) {
 
+                       $pwd_hash = 'SHA1:' . sha1($password);
+       
+                       $result = db_query($link, "SELECT id,login,access_level FROM ttrss_users WHERE 
+                               login = '$login' AND pwd_hash = '$pwd_hash'");
+       
+                       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");
+       
+                               db_query($link, "UPDATE ttrss_users SET last_login = NOW() WHERE id = " . 
+                                       $_SESSION["uid"]);
+       
+                               $user_theme = get_user_theme_path($link);
+       
+                               $_SESSION["theme"] = $user_theme;
+                               $_SESSION["ip_address"] = $_SERVER["REMOTE_ADDR"];
+       
+                               initialize_user_prefs($link, $_SESSION["uid"]);
+       
+                               return true;
+                       }
+       
+                       return false;
+
+               } else {
+
+                       $_SESSION["uid"] = 1;
+                       $_SESSION["name"] = "admin";
+
+                       $user_theme = get_user_theme_path($link);
+       
                        $_SESSION["theme"] = $user_theme;
                        $_SESSION["ip_address"] = $_SERVER["REMOTE_ADDR"];
-
+       
                        initialize_user_prefs($link, $_SESSION["uid"]);
-
+       
                        return true;
                }
-
-               return false;
-
        }
 
        function make_password($length = 8) {
 
        function basic_nosid_redirect_check() {
                if (!SINGLE_USER_MODE) {
-                       if (!$_COOKIE["ttrss_sid"]) {
+                       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");
                                }                               
                        }
                } else {
-                       $_SESSION["uid"] = 1;
-                       $_SESSION["name"] = "admin";
-                       initialize_user_prefs($link, 1); 
+                       return authenticate_user($link, "admin", null);
                }
        }
 
                }
 
                if ($error_code != 0) {
-                       print "<error error-code='$error_code'/>";
+                       print_error_xml(5);
                        return false;
                } else {
                        return true;
                }
        }
 
-       function getFeedUnread($link, $feed) {
+       function getCategoryUnread($link, $cat) {
+
+               $result = db_query($link, "SELECT id FROM ttrss_feeds WHERE cat_id = '$cat' 
+                               AND owner_uid = " . $_SESSION["uid"]);
+
+               $cat_feeds = array();
+               while ($line = db_fetch_assoc($result)) {
+                       array_push($cat_feeds, "feed_id = " . $line["id"]);
+               }
+
+               $match_part = implode(" OR ", $cat_feeds);
+
+               $result = db_query($link, "SELECT COUNT(int_id) AS unread 
+                       FROM ttrss_user_entries 
+                       WHERE   unread = true AND ($match_part) AND owner_uid = " . $_SESSION["uid"]);
+
+               $unread = 0;
+
+               # this needs to be rewritten
+               while ($line = db_fetch_assoc($result)) {
+                       $unread += $line["unread"];
+               }
+
+               return $unread;
+
+       }
+
+       function getFeedUnread($link, $feed, $is_cat = false) {
                $n_feed = sprintf("%d", $feed);
-       
-               if ($n_feed == -1) {
+
+               if ($is_cat) {
+                       return getCategoryUnread($link, $n_feed);               
+               } else if ($n_feed == -1) {
                        $match_part = "marked = true";
-               } else if ($feed > 0) {
-                       $match_part = "feed_id = '$n_feed'";
+               } else if ($n_feed > 0) {
+
+                       $result = db_query($link, "SELECT id FROM ttrss_feeds WHERE parent_feed = '$n_feed'
+                                       AND owner_uid = " . $_SESSION["uid"]);
+
+                       if (db_num_rows($result) > 0) {
+
+                               $linked_feeds = array();
+                               while ($line = db_fetch_assoc($result)) {
+                                       array_push($linked_feeds, "feed_id = " . $line["id"]);
+                               }
+                               
+                               $match_part = implode(" OR ", $linked_feeds);
+
+                               $result = db_query($link, "SELECT COUNT(int_id) AS unread 
+                                       FROM ttrss_user_entries 
+                                       WHERE   unread = true AND ($match_part) AND owner_uid = " . $_SESSION["uid"]);
+
+                               $unread = 0;
+
+                               # this needs to be rewritten
+                               while ($line = db_fetch_assoc($result)) {
+                                       $unread += $line["unread"];
+                               }
+
+                               return $unread;
+
+                       } else {
+                               $match_part = "feed_id = '$n_feed'";
+                       }
                } else if ($feed < -10) {
                        $label_id = -$feed - 11;
 
 
        }
 
-       function getLabelCounters($link, $smart_mode = SMART_RPC_COUNTERS) {
+       function getLabelCounters($link, $smart_mode = SMART_RPC_COUNTERS, $ret_mode = false) {
 
                if ($smart_mode) {
                        if (!$_SESSION["lctr_last_value"]) {
                        }
                }
 
+               $ret_arr = array();
+               
                $old_counters = $_SESSION["lctr_last_value"];
                $lctrs_modified = false;
 
 
                $count = db_fetch_result($result, 0, "count");
 
-               print "<counter type=\"label\" id=\"-1\" counter=\"$count\"/>";
+               if (!$ret_mode) {
+                       print "<counter type=\"label\" id=\"-1\" counter=\"$count\"/>";
+               } else {
+                       $ret_arr["-1"]["counter"] = $count;
+                       $ret_arr["-1"]["description"] = "Starred";
+               }
 
                $result = db_query($link, "SELECT owner_uid,id,sql_exp,description FROM
                        ttrss_labels WHERE owner_uid = ".$_SESSION["uid"]." ORDER by description");
 
                        $id = -$line["id"] - 11;
 
+                       $label_name = $line["description"];
+
                        error_reporting (0);
 
                        $tmp_result = db_query($link, "SELECT count(id) as count FROM ttrss_user_entries,ttrss_entries
                        if (!$smart_mode || $old_counters[$id] != $count) {     
                                $old_counters[$id] = $count;
                                $lctrs_modified = true;
-                               print "<counter type=\"label\" id=\"$id\" counter=\"$count\"/>";
+                               if (!$ret_mode) {
+                                       print "<counter type=\"label\" id=\"$id\" counter=\"$count\"/>";
+                               } else {
+                                       $ret_arr[$id]["counter"] = $count;
+                                       $ret_arr[$id]["description"] = $label_name;
+                               }
                        }
 
                        error_reporting (DEFAULT_ERROR_LEVEL);
                if ($smart_mode && $lctrs_modified) {
                        $_SESSION["lctr_last_value"] = $old_counters;
                }
+
+               return $ret_arr;
        }
 
 /*     function getFeedCounter($link, $id) {
                }
        }
 
+       function get_script_dt_add() {
+               if (strpos(VERSION, "99") === false) {
+                       return VERSION;
+               } else {
+                       return time();
+               }
+       }
+
+       function get_pgsql_version($link) {
+               $result = db_query($link, "SELECT version() AS version");
+               $version = split(" ", db_fetch_result($result, 0, "version"));
+               return $version[1];
+       }
+
+       function print_error_xml($code, $add_msg = "") {
+               global $ERRORS;
+
+               $error_msg = $ERRORS[$code];
+               
+               if ($add_msg) {
+                       $error_msg = "$error_msg; $add_msg";
+               }
+               
+               print "<rpc-reply>";
+               print "<error error-code=\"$code\" error-msg=\"$error_msg\"/>";
+               print "</rpc-reply>";
+       }
+
+       function subscribe_to_feed($link, $feed_link, $cat_id = 0) {
+       
+               if ($cat_id == "0" || !$cat_id) {
+                       $cat_qpart = "NULL";
+               } else {
+                       $cat_qpart = "'$cat_id'";
+               }
+       
+               $result = db_query($link,
+                       "SELECT id FROM ttrss_feeds 
+                       WHERE feed_url = '$feed_link' AND owner_uid = ".$_SESSION["uid"]);
+       
+               if (db_num_rows($result) == 0) {
+                       
+                       $result = db_query($link,
+                               "INSERT INTO ttrss_feeds (owner_uid,feed_url,title,cat_id) 
+                               VALUES ('".$_SESSION["uid"]."', '$feed_link', 
+                               '[Unknown]', $cat_qpart)");
+       
+                       $result = db_query($link,
+                               "SELECT id FROM ttrss_feeds WHERE feed_url = '$feed_link' 
+                               AND owner_uid = " . $_SESSION["uid"]);
+       
+                       $feed_id = db_fetch_result($result, 0, "id");
+       
+                       if ($feed_id) {
+                               update_rss_feed($link, $feed_link, $feed_id, true);
+                       }
+
+                       return true;
+               } else {
+                       return false;
+               }
+       }
+
+       function print_feed_select($link, $id, $default_id = "", 
+               $attributes = "", $include_all_feeds = true) {
+
+               print "<select id=\"$id\" name=\"$id\" $attributes>";
+               if ($include_all_feeds) { 
+                       print "<option value=\"0\">All feeds</option>";
+               }
+       
+               $result = db_query($link, "SELECT id,title FROM ttrss_feeds
+                       WHERE owner_uid = ".$_SESSION["uid"]." ORDER BY title");
+
+               if (db_num_rows($result) > 0 && $include_all_feeds) {
+                       print "<option disabled>--------</option>";
+               }
+
+               while ($line = db_fetch_assoc($result)) {
+                       if ($line["id"] == $default_id) {
+                               $is_selected = "selected";
+                       } else {
+                               $is_selected = "";
+                       }
+                       printf("<option $is_selected value='%d'>%s</option>", 
+                               $line["id"], htmlspecialchars(db_unescape_string($line["title"])));
+               }
+       
+               print "</select>";
+       }
+
+       function print_feed_cat_select($link, $id, $default_id = "", 
+               $attributes = "", $include_all_cats = true) {
+               
+               print "<select id=\"$id\" name=\"$id\" $attributes>";
+
+               if ($include_all_cats) {
+                       print "<option value=\"0\">Uncategorized</option>";
+               }
+
+               $result = db_query($link, "SELECT id,title FROM ttrss_feed_categories
+                       WHERE owner_uid = ".$_SESSION["uid"]." ORDER BY title");
+
+               if (db_num_rows($result) > 0 && $include_all_cats) {
+                       print "<option disabled>--------</option>";
+               }
+
+               while ($line = db_fetch_assoc($result)) {
+                       if ($line["id"] == $default_id) {
+                               $is_selected = "selected";
+                       } else {
+                               $is_selected = "";
+                       }
+                       printf("<option $is_selected value='%d'>%s</option>", 
+                               $line["id"], htmlspecialchars(db_unescape_string($line["title"])));
+               }
+
+               print "</select>";
+       }
+       
+       function checkbox_to_sql_bool($val) {
+               return ($val == "on") ? "true" : "false";
+       }
+
+       function getFeedCatTitle($link, $id) {
+               if ($id == -1) {
+                       return "Special";
+               } else if ($id < -10) {
+                       return "Labels";
+               } else if ($id > 0) {
+                       $result = db_query($link, "SELECT ttrss_feed_categories.title 
+                               FROM ttrss_feeds, ttrss_feed_categories WHERE ttrss_feeds.id = '$id' AND
+                                       cat_id = ttrss_feed_categories.id");
+                       if (db_num_rows($result) == 1) {
+                               return db_fetch_result($result, 0, "title");
+                       } else {
+                               return "Uncategorized";
+                       }
+               } else {
+                       return "getFeedCatTitle($id) failed";
+               }
+
+       }
+
+       function getFeedTitle($link, $id) {
+               if ($id == -1) {
+                       return "Starred articles";
+               } else if ($id < -10) {
+                       $label_id = -10 - $id;
+                       $result = db_query($link, "SELECT description FROM ttrss_labels WHERE id = '$label_id'");
+                       if (db_num_rows($result) == 1) {
+                               return db_fetch_result($result, 0, "description");
+                       } else {
+                               return "Unknown label ($label_id)";
+                       }
+
+               } else if ($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");
+                       } else {
+                               return "Unknown feed ($id)";
+                       }
+               } else {
+                       return "getFeedTitle($id) failed";
+               }
+
+       }
+
+       function get_session_cookie_name() {
+               return ((!defined('TTRSS_SESSION_NAME')) ? "ttrss_sid" : TTRSS_SESSION_NAME);
+       }
+
+       function print_init_params($link) {
+               print "<init-params>";
+               if ($_SESSION["stored-params"]) {
+                       foreach (array_keys($_SESSION["stored-params"]) as $key) {
+                               $value = htmlspecialchars($_SESSION["stored-params"][$key]);
+                               print "<param key=\"$key\" value=\"$value\"/>";
+                       }
+               }
+
+               print "<param key=\"daemon_enabled\" value=\"" . ENABLE_UPDATE_DAEMON . "\"/>";
+               print "<param key=\"feeds_frame_refresh\" value=\"" . FEEDS_FRAME_REFRESH . "\"/>";
+               print "<param key=\"daemon_refresh_only\" value=\"" . DAEMON_REFRESH_ONLY . "\"/>";
+
+               print "<param key=\"on_catchup_show_next_feed\" value=\"" . 
+                       get_pref($link, "ON_CATCHUP_SHOW_NEXT_FEED") . "\"/>";
+
+               print "<param key=\"hide_read_feeds\" value=\"" . 
+                       sprintf("%d", get_pref($link, "HIDE_READ_FEEDS")) . "\"/>";
+
+               print "<param key=\"feeds_sort_by_unread\" value=\"" . 
+                       sprintf("%d", get_pref($link, "FEEDS_SORT_BY_UNREAD")) . "\"/>";
+
+               print "</init-params>";
+       }
+
+       function print_runtime_info($link) {
+               print "<runtime-info>";
+               if (ENABLE_UPDATE_DAEMON) {
+                       print "<param key=\"daemon_is_running\" value=\"".
+                               sprintf("%d", file_is_locked("update_daemon.lock")) . "\"/>";
+               }
+               print "</runtime-info>";
+       }
+
+       function queryFeedHeadlines($link, $feed, $limit, $view_mode, $cat_view, $search, $search_mode, $match_on) {
+
+                       if ($search) {
+                               if ($match_on == "both") {
+                                       $search_query_part = "(upper(ttrss_entries.title) LIKE upper('%$search%') 
+                                               OR upper(ttrss_entries.content) LIKE '%$search%') AND";
+                               } else if ($match_on == "title") {
+                                       $search_query_part = "upper(ttrss_entries.title) LIKE upper('%$search%') 
+                                               AND";
+                               } else if ($match_on == "content") {
+                                       $search_query_part = "upper(ttrss_entries.content) LIKE upper('%$search%') AND";
+                               }
+                       } else {
+                               $search_query_part = "";
+                       }
+
+                       $view_query_part = "";
+       
+                       if ($view_mode == "adaptive") {
+                               if ($search) {
+                                       $view_query_part = " ";
+                               } else if ($feed != -1) {
+                                       $unread = getFeedUnread($link, $feed, $cat_view);
+                                       if ($unread > 0) {
+                                               $view_query_part = " unread = true AND ";
+                                       }
+                               }
+                       }
+       
+                       if ($view_mode == "marked") {
+                               $view_query_part = " marked = true AND ";
+                       }
+       
+                       if ($view_mode == "unread") {
+                               $view_query_part = " unread = true AND ";
+                       }
+       
+                       if ($limit > 0) {
+                               $limit_query_part = "LIMIT " . $limit;
+                       } 
+
+                       $vfeed_query_part = "";
+       
+                       // override query strategy and enable feed display when searching globally
+                       if ($search && $search_mode == "all_feeds") {
+                               $query_strategy_part = "ttrss_entries.id > 0";
+                               $vfeed_query_part = "ttrss_feeds.title AS feed_title,";         
+                       } else if (preg_match("/^-?[0-9][0-9]*$/", $feed) == false) {
+                               $query_strategy_part = "ttrss_entries.id > 0";
+                               $vfeed_query_part = "(SELECT title FROM ttrss_feeds WHERE
+                                       id = feed_id) as feed_title,";
+                       } else if ($feed >= 0 && $search && $search_mode == "this_cat") {
+       
+                               $vfeed_query_part = "ttrss_feeds.title AS feed_title,";         
+       
+                               $tmp_result = db_query($link, "SELECT id 
+                                       FROM ttrss_feeds WHERE cat_id = 
+                                               (SELECT cat_id FROM ttrss_feeds WHERE id = '$feed') AND id != '$feed'");
+       
+                               $cat_siblings = array();
+       
+                               if (db_num_rows($tmp_result) > 0) {
+                                       while ($p = db_fetch_assoc($tmp_result)) {
+                                               array_push($cat_siblings, "feed_id = " . $p["id"]);
+                                       }
+       
+                                       $query_strategy_part = sprintf("(feed_id = %d OR %s)", 
+                                               $feed, implode(" OR ", $cat_siblings));
+       
+                               } else {
+                                       $query_strategy_part = "ttrss_entries.id > 0";
+                               }
+                               
+                       } else if ($feed >= 0) {
+       
+                               if ($cat_view) {
+       
+                                       if ($feed > 0) {
+                                               $query_strategy_part = "cat_id = '$feed'";
+                                       } else {
+                                               $query_strategy_part = "cat_id IS NULL";
+                                       }
+       
+                                       $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
+       
+                               } else {                
+                                       $tmp_result = db_query($link, "SELECT id 
+                                               FROM ttrss_feeds WHERE parent_feed = '$feed'
+                                               ORDER BY cat_id,title");
+               
+                                       $parent_ids = array();
+               
+                                       if (db_num_rows($tmp_result) > 0) {
+                                               while ($p = db_fetch_assoc($tmp_result)) {
+                                                       array_push($parent_ids, "feed_id = " . $p["id"]);
+                                               }
+               
+                                               $query_strategy_part = sprintf("(feed_id = %d OR %s)", 
+                                                       $feed, implode(" OR ", $parent_ids));
+               
+                                               $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
+                                       } else {
+                                               $query_strategy_part = "feed_id = '$feed'";
+                                       }
+                               }
+                       } else if ($feed == -1) { // starred virtual feed
+                               $query_strategy_part = "marked = true";
+                               $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
+                       } else if ($feed <= -10) { // labels
+                               $label_id = -$feed - 11;
+       
+                               $tmp_result = db_query($link, "SELECT sql_exp FROM ttrss_labels
+                                       WHERE id = '$label_id'");
+                       
+                               $query_strategy_part = db_fetch_result($tmp_result, 0, "sql_exp");
+               
+                               $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
+                       } else {
+                               $query_strategy_part = "id > 0"; // dumb
+                       }
+       
+                       $order_by = "updated DESC";
+       
+                       $feed_title = "";
+
+                       if ($search && $search_mode == "all_feeds") {
+                               $feed_title = "Global search results ($search)";
+                       } else if ($search && preg_match('/^-?[0-9][0-9]*$/', $feed) == false) {
+                               $feed_title = "Feed search results ($search, $feed)";
+                       } else if (preg_match('/^-?[0-9][0-9]*$/', $feed) == false) {
+                               $feed_title = $feed;
+                       } else if (preg_match('/^-?[0-9][0-9]*$/', $feed) != false && $feed >= 0) {
+       
+                               if ($cat_view) {
+       
+                                       if ($feed != 0) {                       
+                                               $result = db_query($link, "SELECT title FROM ttrss_feed_categories
+                                                       WHERE id = '$feed' AND owner_uid = " . $_SESSION["uid"]);
+                                               $feed_title = db_fetch_result($result, 0, "title");
+                                       } else {
+                                               $feed_title = "Uncategorized";
+                                       }
+                               } else {
+                                       
+                                       $result = db_query($link, "SELECT title,site_url,last_error FROM ttrss_feeds 
+                                               WHERE id = '$feed' AND owner_uid = " . $_SESSION["uid"]);
+               
+                                       $feed_title = db_fetch_result($result, 0, "title");
+                                       $feed_site_url = db_fetch_result($result, 0, "site_url");
+                                       $last_error = db_fetch_result($result, 0, "last_error");
+       
+                               }
+       
+                       } else if ($feed == -1) {
+                               $feed_title = "Starred articles";
+                       } else if ($feed < -10) {
+                               $label_id = -$feed - 11;
+                               $result = db_query($link, "SELECT description FROM ttrss_labels
+                                       WHERE id = '$label_id'");
+                               $feed_title = db_fetch_result($result, 0, "description");
+                       } else {
+                               $feed_title = "?";
+                       }
+
+                       $feed_title = db_unescape_string($feed_title);
+
+                       if ($feed < -10) error_reporting (0);
+
+                       if (preg_match("/^-?[0-9][0-9]*$/", $feed) != false) {
+       
+                               if ($feed >= 0) {
+                                       $feed_kind = "Feeds";
+                               } else {
+                                       $feed_kind = "Labels";
+                               }
+       
+                               $content_query_part = "content as content_preview,";
+                               
+                               $query = "SELECT 
+                                               ttrss_entries.id,ttrss_entries.title,
+                                               SUBSTRING(updated,1,16) as updated,
+                                               unread,feed_id,marked,link,last_read,
+                                               SUBSTRING(last_read,1,19) as last_read_noms,
+                                               $vfeed_query_part
+                                               $content_query_part
+                                               SUBSTRING(updated,1,19) as updated_noms
+                                       FROM
+                                               ttrss_entries,ttrss_user_entries,ttrss_feeds
+                                       WHERE
+                                       ttrss_user_entries.feed_id = ttrss_feeds.id AND
+                                       ttrss_user_entries.ref_id = ttrss_entries.id AND
+                                       ttrss_user_entries.owner_uid = '".$_SESSION["uid"]."' AND
+                                       $search_query_part
+                                       $view_query_part
+                                       $query_strategy_part ORDER BY $order_by
+                                       $limit_query_part";
+                                       
+                               $result = db_query($link, $query);
+       
+                               if ($_GET["debug"]) print $query;
+       
+                       } else {
+                               // browsing by tag
+       
+                               $feed_kind = "Tags";
+       
+                               $result = db_query($link, "SELECT
+                                       ttrss_entries.id as id,title,
+                                       SUBSTRING(updated,1,16) as updated,
+                                       unread,feed_id,
+                                       marked,link,last_read,                          
+                                       SUBSTRING(last_read,1,19) as last_read_noms,
+                                       $vfeed_query_part
+                                       $content_query_part
+                                       SUBSTRING(updated,1,19) as updated_noms
+                                       FROM
+                                               ttrss_entries,ttrss_user_entries,ttrss_tags
+                                       WHERE
+                                               ref_id = ttrss_entries.id AND
+                                               ttrss_user_entries.owner_uid = '".$_SESSION["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");    
+                       }
+
+                       return array($result, $feed_title, $feed_site_url, $last_error);
+                       
+       }
+
 ?>