]> git.wh0rd.org - tt-rss.git/blobdiff - functions.php
properly handle on-the-fly adding of labels
[tt-rss.git] / functions.php
index 8945ef43702ceec3704081998055256621710a7c..f9de0622fe3a07cc7e38e210c6de6c9a15306519 100644 (file)
                print "</rpc-reply>";
        }
 
+       /**
+        * Subscribes the user to the given feed
+        *
+        * @param resource $link       Database connection
+        * @param string   $url        Feed URL to subscribe to
+        * @param integer  $cat_id     Category ID the feed shall be added to
+        * @param string   $auth_login (optional) Feed username
+        * @param string   $auth_pass  (optional) Feed password
+        *
+        * @return integer Status code:
+        *                 0 - OK, Feed already exists
+        *                 1 - OK, Feed added
+        *                 2 - Invalid URL
+        *                 3 - URL content is HTML, not a feed
+        */
        function subscribe_to_feed($link, $url, $cat_id = 0, 
                        $auth_login = '', $auth_pass = '') {
 
-               $parts = parse_url($url);
-
+               $url = fix_url($url);
                if (!validate_feed_url($url)) return 2;
 
-               if ($parts['scheme'] == 'feed') $parts['scheme'] = 'http';
-
-               $url = make_url_from_parts($parts);
-
                if ($cat_id == "0" || !$cat_id) {
                        $cat_qpart = "NULL";
                } else {
                        WHERE feed_url = '$url' AND owner_uid = ".$_SESSION["uid"]);
        
                if (db_num_rows($result) == 0) {
+                       if (url_is_html($url)) {
+                               $feedUrls = get_feeds_from_html($url);
+                               if (count($feedUrls) != 1) {
+                                       return 3;
+                               }
+                               //use feed url as new URL
+                               $url = key($feedUrls);
+                       }
                        
                        $result = db_query($link,
                                "INSERT INTO ttrss_feeds 
 
                while ($line = db_fetch_assoc($result)) {
                        if ($line["id"] == $default_id) {
-                               $is_selected = "selected";
+                               $is_selected = "selected=\"1\"";
                        } else {
                                $is_selected = "";
                        }
 
                while ($line = db_fetch_assoc($result)) {
                        if ($line["id"] == $default_id) {
-                               $is_selected = "selected";
+                               $is_selected = "selected=\"1\"";
                        } else {
                                $is_selected = "";
                        }
                                                print "Marking affected articles as read...\n";
                                                catchupArticlesById($link, $affected_ids, 0, $line["id"]);
                                        }
-
-                                       db_query($link, "UPDATE ttrss_users SET last_digest_sent = NOW() 
-                                                       WHERE id = " . $line["id"]);
                                } else {
                                        print "No headlines\n";
                                }
+
+                               db_query($link, "UPDATE ttrss_users SET last_digest_sent = NOW() 
+                                       WHERE id = " . $line["id"]);
                        }
                }
 
                                $search_q = "";
                        }
 
+                       // Adaptive doesn't really make any sense for generated feeds
+                       // All Articles is the default, so no need to insert it either
+                       if ($view_mode == "adaptive" || $view_mode == "all_articles") 
+                               $view_mode = "";
+                       else
+                               $view_mode = "&view-mode=$view_mode";
+
                        $rss_link = htmlspecialchars(get_self_url_prefix() . 
-                               "/backend.php?op=rss&id=$feed_id&is_cat=$is_cat&view-mode=$view_mode$search_q");
+                               "/backend.php?op=rss&id=$feed_id&is_cat=$is_cat$view_mode$search_q");
 
                        #print "
                        #       <a target=\"_blank\"
                        print_labels_headlines_dropdown($link, $feed_id);
 
                        print "<li class=\"insensitive\">".__('Feed:')."</li>";
-                       print "<li onclick=\"window.open('$rss_link')\">&nbsp;&nbsp;".__('View as RSS')."</li>";
+                       print "<li onclick=\"displayDlg('generatedFeed', '$feed_id:$is_cat:$rss_link')\">&nbsp;&nbsp;".__('View as RSS')."</li>";
 
                        print "</ul>";
 
 
                $url_path = get_self_url_prefix();
                $url_path .= "/opml.php?op=publish&key=" . 
-                       get_pref($link, "_PREFS_PUBLISH_KEY", $_SESSION["uid"]);
+                       get_feed_access_key($link, 'OPML:Publish', false, $_SESSION["uid"]);
 
                return $url_path;
        }
                return $url;
        }
 
+       /**
+        * Fixes incomplete URLs by prepending "http://".
+        * Also replaces feed:// with http://, and
+        * prepends a trailing slash if the url is a domain name only.
+        *
+        * @param string $url Possibly incomplete URL
+        *
+        * @return string Fixed URL.
+        */
+       function fix_url($url) {
+               if (strpos($url, '://') === false) {
+                       $url = 'http://' . $url;
+               } else if (substr($url, 0, 5) == 'feed:') {
+                       $url = 'http:' . substr($url, 5);
+               }
+
+               //prepend slash if the URL has no slash in it
+               // "http://www.example" -> "http://www.example/"
+               if (strpos($url, '/', 7) === false) {
+                       $url .= '/';
+               }
+               return $url;
+       }
+
        function validate_feed_url($url) {
                $parts = parse_url($url);
 
                }
                return false;
        }
+
+       /**
+        * Extracts RSS/Atom feed URLs from the given HTML URL.
+        *
+        * @param string $url HTML page URL
+        *
+        * @return array Array of feeds. Key is the full URL, value the title
+        */
+       function get_feeds_from_html($url)
+       {
+               $url     = fix_url($url);
+               $baseUrl = substr($url, 0, strrpos($url, '/') + 1);
+
+               $doc = new DOMDocument();
+               $doc->loadHTMLFile($url);
+               $xpath = new DOMXPath($doc);
+               $entries = $xpath->query('/html/head/link[@rel="alternate"]');
+               $feedUrls = array();
+               foreach ($entries as $entry) {
+                       if ($entry->hasAttribute('href')) {
+                               $title = $entry->getAttribute('title');
+                               if ($title == '') {
+                                       $title = $entry->getAttribute('type');
+                               }
+                               $feedUrl = $entry->getAttribute('href');
+                               if (strpos($feedUrl, '://') === false) {
+                                       //no protocol -> relative URL
+                                       $feedUrl = $baseUrl . $feedUrl;
+                               }
+                               $feedUrls[$feedUrl] = $title;
+                       }
+               }
+               return $feedUrls;
+       }
+
+       /**
+        * Checks if the content behind the given URL is a HTML file
+        *
+        * @param string $url URL to check
+        *
+        * @return boolean True if the URL contains HTML content
+        */
+       function url_is_html($url) {
+               $content = substr(fetch_file_contents($url, false), 0, 1000);
+               if (strpos($content, '<html>') === false
+                       && strpos($content, '<html ') === false
+               ) {
+                       return false;
+               }
+
+               return true;
+       }
 ?>