]> git.mxchange.org Git - quix0rs-gnu-social.git/blobdiff - plugins/RSSCloud/RSSCloudNotifier.php
Plugin now checks notify handlers before registering subscriptions
[quix0rs-gnu-social.git] / plugins / RSSCloud / RSSCloudNotifier.php
index c2130b7b83d45ddab0a058896864913ebb1945a6..eb3198b5a43e489ae2a2f538a4afe33c68f740e7 100644 (file)
@@ -33,59 +33,77 @@ if (!defined('STATUSNET')) {
 
 class RSSCloudNotifier {
 
-    function postUpdate($endpoint, $feed) {
-        common_debug("CloudNotifier->notify: $feed");
+    function challenge($endpoint, $feed)
+    {
+        $code   = common_confirmation_code(128);
+        $params = array('url' => $feed, 'challenge' => $code);
+        $url    = $endpoint . '?' . http_build_query($params);
+
+        try {
+            $client = new HTTPClient();
+            $response = $client->get($url);
+        } catch (HTTP_Request2_Exception $e) {
+            common_log(LOG_INFO, 'RSSCloud plugin - failure testing notify handler ' .
+                       $endpoint . ' - '  . $e->getMessage());
+            return false;
+        }
 
-        $params = 'url=' . urlencode($feed);
+        // Check response is betweet 200 and 299 and body contains challenge data
 
-        $result = $this->httpPost($endpoint, $params);
+        $status = $response->getStatus();
+        $body   = $response->getBody();
 
-        // XXX: Make all this use CurlClient (lib/curlclient.php)
+        if ($status >= 200 && $status < 300) {
 
-        if ($result) {
-            common_debug('RSSCloud plugin - success notifying cloud endpoint!');
+            if (strpos($body, $code) !== false) {
+                common_log(LOG_INFO, 'RSSCloud plugin - ' .
+                           "success testing notify handler:  $endpoint");
+                return true;
+            } else {
+                common_log(LOG_INFO, 'RSSCloud plugin - ' .
+                          'challenge/repsonse failed for notify handler ' .
+                           $endpoint);
+                common_debug('body = ' . var_export($body, true));
+                return false;
+            }
         } else {
-            common_debug('RSSClous plugin - failure notifying cloud endpoint!');
+            common_log(LOG_INFO, 'RSSCloud plugin - ' .
+                       "failure testing notify handler:  $endpoint " .
+                       ' - got HTTP ' . $status);
+            common_debug('body = ' . var_export($body, true));
+            return false;
         }
-
-        return $result;
     }
 
-    function userAgent()
-    {
-        return 'rssCloudPlugin/' . RSSCLOUDPLUGIN_VERSION .
-          ' StatusNet/' . STATUSNET_VERSION;
-    }
-
-    private function httpPost($url, $params) {
-
-        $options = array(CURLOPT_URL            => $url,
-                         CURLOPT_POST           => true,
-                         CURLOPT_POSTFIELDS     => $params,
-                         CURLOPT_USERAGENT      => $this->userAgent(),
-                         CURLOPT_RETURNTRANSFER => true,
-                         CURLOPT_FAILONERROR    => true,
-                         CURLOPT_HEADER         => false,
-                         CURLOPT_FOLLOWLOCATION => true,
-                         CURLOPT_CONNECTTIMEOUT => 5,
-                         CURLOPT_TIMEOUT        => 5);
-
-        $ch = curl_init();
-        curl_setopt_array($ch, $options);
+    function postUpdate($endpoint, $feed) {
 
-        $response = curl_exec($ch);
+        $headers  = array();
+        $postdata = array('url' => $feed);
 
-        $info = curl_getinfo($ch);
+        try {
+            $client = new HTTPClient();
+            $response = $client->post($endpoint, $headers, $postdata);
+        } catch (HTTP_Request2_Exception $e) {
+            common_log(LOG_INFO, 'RSSCloud plugin - failure notifying ' .
+                       $endpoint . ' that feed ' . $feed .
+                       ' has changed: ' . $e->getMessage());
+            return false;
+        }
 
-        curl_close($ch);
+        $status = $response->getStatus();
 
-        if ($info['http_code'] == 200) {
+        if ($status >= 200 && $status < 300) {
+            common_log(LOG_INFO, 'RSSCloud plugin - success notifying ' .
+                       $endpoint . ' that feed ' . $feed . ' has changed.');
             return true;
         } else {
+            common_log(LOG_INFO, 'RSSCloud plugin - failure notifying ' .
+                       $endpoint . ' that feed ' . $feed .
+                       ' has changed: got HTTP ' . $status);
+            common_debug('body = ' . var_export($response->getBody(), true));
             return false;
         }
     }
 
 }
 
-