From f33479dab87b7e34692201db1c1016e4ffa7c990 Mon Sep 17 00:00:00 2001
From: Christian Weiske <cweiske@cweiske.de>
Date: Sun, 7 Nov 2010 14:43:15 +0100
Subject: [PATCH] part of #276: determine if the url contents are html

---
 functions.js  |  1 +
 functions.php | 21 +++++++++++++++++++++
 2 files changed, 22 insertions(+)

diff --git a/functions.js b/functions.js
index 626a23a89..af18f8383 100644
--- a/functions.js
+++ b/functions.js
@@ -1319,6 +1319,7 @@ function subscribeToFeed() {
 				}
 				break;
 			case 2:
+			case 3:
 				alert(__("Can't subscribe to the specified URL."));
 				break;
 			case 0:
diff --git a/functions.php b/functions.php
index 7f24b4d06..a31e52e0b 100644
--- a/functions.php
+++ b/functions.php
@@ -2953,6 +2953,7 @@
 	 *                 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 = '') {
@@ -2971,6 +2972,9 @@
 			WHERE feed_url = '$url' AND owner_uid = ".$_SESSION["uid"]);
 	
 		if (db_num_rows($result) == 0) {
+			if (url_is_html($url)) {
+				return 3;
+			}
 			
 			$result = db_query($link,
 				"INSERT INTO ttrss_feeds 
@@ -7026,4 +7030,21 @@
 		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;
+	}
 ?>