From b296c5f19e3439650528361cc91c4b3ae7a333ed Mon Sep 17 00:00:00 2001
From: westi <westi@1a063a9b-81f0-0310-95a4-ce76da25c4cd>
Date: Sat, 17 Nov 2007 11:21:34 +0000
Subject: [PATCH] Ensure that we offer https access to atom if it is available.
 Fixes #5298 props rubys.

git-svn-id: http://svn.automattic.com/wordpress/trunk@6339 1a063a9b-81f0-0310-95a4-ce76da25c4cd
---
 wp-app.php                      | 19 ++++++++++--------
 wp-includes/default-filters.php |  3 +++
 wp-includes/functions.php       | 34 ++++++++++++++++++++++++++++++++-
 xmlrpc.php                      |  3 +--
 4 files changed, 48 insertions(+), 11 deletions(-)

diff --git a/wp-app.php b/wp-app.php
index dee5cb04d0..9a34fffcc0 100644
--- a/wp-app.php
+++ b/wp-app.php
@@ -68,7 +68,6 @@ class AtomServer {
 	var $MEDIA_SINGLE_PATH = "attachment";
 
 	var $params = array();
-	var $script_name = "wp-app.php";
 	var $media_content_types = array('image/*','audio/*','video/*');
 	var $atom_content_types = array('application/atom+xml');
 
@@ -80,6 +79,10 @@ class AtomServer {
 	function AtomServer() {
 
 		$this->script_name = array_pop(explode('/',$_SERVER['SCRIPT_NAME']));
+		$this->app_base = get_bloginfo('url') . '/' . $this->script_name . '/';
+		if ( isset($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS']) == 'on' ) {
+			$this->app_base = preg_replace( '/^http:\/\//', 'https://', $this->app_base );
+		}
 
 		$this->selectors = array(
 			'@/service$@' =>
@@ -594,7 +597,7 @@ EOD;
 		} else {
 			$path = $this->ENTRIES_PATH;
 		}
-		$url = get_bloginfo('url') . '/' . $this->script_name . '/' . $path;
+		$url = $this->app_base . $path;
 		if(isset($page) && is_int($page)) {
 			$url .= "/$page";
 		}
@@ -607,7 +610,7 @@ EOD;
 	}
 
 	function get_categories_url($page = NULL) {
-		return get_bloginfo('url') . '/' . $this->script_name . '/' . $this->CATEGORIES_PATH;
+		return $this->app_base . $this->CATEGORIES_PATH;
 	}
 
 	function the_categories_url() {
@@ -616,7 +619,7 @@ EOD;
 	}
 
 	function get_attachments_url($page = NULL) {
-		$url = get_bloginfo('url') . '/' . $this->script_name . '/' . $this->MEDIA_PATH;
+		$url = $this->app_base . $this->MEDIA_PATH;
 		if(isset($page) && is_int($page)) {
 			$url .= "/$page";
 		}
@@ -629,7 +632,7 @@ EOD;
 	}
 
 	function get_service_url() {
-		return get_bloginfo('url') . '/' . $this->script_name . '/' . $this->SERVICE_PATH;
+		return $this->app_base . $this->SERVICE_PATH;
 	}
 
 	function get_entry_url($postID = NULL) {
@@ -638,7 +641,7 @@ EOD;
 			$postID = (int) $GLOBALS['post']->ID;
 		}
 
-		$url = get_bloginfo('url') . '/' . $this->script_name . '/' . $this->ENTRY_PATH . "/$postID";
+		$url = $this->app_base . $this->ENTRY_PATH . "/$postID";
 
 		log_app('function',"get_entry_url() = $url");
 		return $url;
@@ -655,7 +658,7 @@ EOD;
 			$postID = (int) $GLOBALS['post']->ID;
 		}
 
-		$url = get_bloginfo('url') . '/' . $this->script_name . '/' . $this->MEDIA_SINGLE_PATH ."/file/$postID";
+		$url = $this->app_base . $this->MEDIA_SINGLE_PATH ."/file/$postID";
 
 		log_app('function',"get_media_url() = $url");
 		return $url;
@@ -919,7 +922,7 @@ EOD;
 				$ctloc = $this->get_entry_url($post_ID);
 				break;
 			case 'attachment':
-				$edit = get_bloginfo('url') . '/' . $this->script_name . "/attachments/$post_ID";
+				$edit = $this->app_base . "attachments/$post_ID";
 				break;
 		}
 		header("Content-Type: $this->ATOM_CONTENT_TYPE");
diff --git a/wp-includes/default-filters.php b/wp-includes/default-filters.php
index 15fab157cf..18b6dde463 100644
--- a/wp-includes/default-filters.php
+++ b/wp-includes/default-filters.php
@@ -134,6 +134,9 @@ add_filter('comment_flood_filter', 'wp_throttle_comment_flood', 10, 3);
 add_filter('pre_comment_content', 'wp_rel_nofollow', 15);
 add_filter('comment_email', 'antispambot');
 
+//Atom SSL support
+add_filter('atom_service_url','atom_service_url_filter');
+
 // Actions
 add_action('wp_head', 'rsd_link');
 add_action('wp_head', 'wlwmanifest_link');
diff --git a/wp-includes/functions.php b/wp-includes/functions.php
index 12ec9198b4..ff531ff793 100644
--- a/wp-includes/functions.php
+++ b/wp-includes/functions.php
@@ -1484,4 +1484,36 @@ function absint( $maybeint ) {
 	return abs( intval( $maybeint ) );
 }
 
-?>
\ No newline at end of file
+/**
+ * Determines if the blog can be accessed over SSL
+ * @return bool whether of not SSL access is available
+ */
+function url_is_accessable_via_ssl($url)
+{
+	if (in_array('curl', get_loaded_extensions())) {
+		 $ssl = preg_replace( '/^http:\/\//', 'https://',  $url );
+
+		 $ch = curl_init();
+		 curl_setopt($ch, CURLOPT_URL, $ssl);
+		 curl_setopt($ch, CURLOPT_FAILONERROR, true);
+		 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
+		 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
+
+		 $data = curl_exec ($ch);
+
+		 $status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
+		 curl_close ($ch);
+
+		 if ($status == 200 || $status == 401) {
+			 return true;
+		 }
+	}
+	return false;
+}
+
+function atom_service_url_filter($url)
+{
+	if ( url_is_accessable_via_ssl($url) )
+		return  preg_replace( '/^http:\/\//', 'https://',  $url );
+}
+?>
diff --git a/xmlrpc.php b/xmlrpc.php
index 26ef29ce14..0deb015b81 100644
--- a/xmlrpc.php
+++ b/xmlrpc.php
@@ -19,7 +19,6 @@ include('./wp-config.php');
 
 if ( isset( $_GET['rsd'] ) ) { // http://archipelago.phrasewise.com/rsd
 header('Content-Type: text/xml; charset=' . get_option('blog_charset'), true);
-
 ?>
 <?php echo '<?xml version="1.0" encoding="'.get_option('blog_charset').'"?'.'>'; ?>
 <rsd version="1.0" xmlns="http://archipelago.phrasewise.com/rsd">
@@ -32,7 +31,7 @@ header('Content-Type: text/xml; charset=' . get_option('blog_charset'), true);
       <api name="Movable Type" blogID="1" preferred="false" apiLink="<?php bloginfo_rss('wpurl') ?>/xmlrpc.php" />
       <api name="MetaWeblog" blogID="1" preferred="false" apiLink="<?php bloginfo_rss('wpurl') ?>/xmlrpc.php" />
       <api name="Blogger" blogID="1" preferred="false" apiLink="<?php bloginfo_rss('wpurl') ?>/xmlrpc.php" />
-      <api name="Atom" blogID="1" preferred="false" apiLink="<?php bloginfo_rss('wpurl') ?>/wp-app.php/service" /> 
+      <api name="Atom" blogID="" preferred="false" apiLink="<?php echo apply_filters('atom_service_url', (get_bloginfo('url')."/wp-app.php/service"))?>" /> 
     </apis>
   </service>
 </rsd>