]> git.mjollnir.org Git - s9y.git/commitdiff
Use PEAR HTTP::Request to send trackbacks. Allows header redirection.
authorgarvinhicking <garvinhicking>
Thu, 15 Dec 2005 11:22:37 +0000 (11:22 +0000)
committergarvinhicking <garvinhicking>
Thu, 15 Dec 2005 11:22:37 +0000 (11:22 +0000)
docs/NEWS
include/functions_trackbacks.inc.php

index cb8bc922c67fe2e93827be88f71df337ad8ae04f..3ea3faaab245ae8e2dbf6119afcd58237e20ae9f 100644 (file)
--- a/docs/NEWS
+++ b/docs/NEWS
@@ -3,6 +3,10 @@
 Version 1.0 ()
 ------------------------------------------------------------------------
 
+   * Trackback now uses PEAR HTTP::Request class to send trackbacks.
+     This allows to use HTTP redirects of trackbacks, which seem to
+     get more common nowadays (garvinhicking)
+
    * Fix Bug #1377095 - Not counting correct number of entries on the
      /archives page. The page was counting entries belonging to more
      then one categories multiple times. (garvinhicking)
index d0b96ea50e7a504a294a72dcced7b4b3da53246b..82c012310458704a4fd4f735f93d10800a80e888 100644 (file)
@@ -82,29 +82,18 @@ function _serendipity_send($loc, $data) {
        $target['port'] = 80;
     }
 
-    $sock = @fsockopen($target['host'], $target['port']);
-    if (!is_resource($sock)) {
-        return "Couldn't connect to $loc";
+    $uri = $target['scheme'] . '://' . $target['host'] . ':' . $target['port'] . $target['path'] . $target['query'];
+    require_once S9Y_PEAR_PATH . 'HTTP/Request.php';
+    $req = &new HTTP_Request($uri, array('allowRedirects' => true, 'maxRedirects' => 5, 'method' => 'POST'));
+    $req->addRawPostData($data, true);
+    $res = $req->sendRequest();
+
+    if (PEAR::isError($res)) {
+        return false;
     }
 
-    $headers = "POST {$target['path']}{$target['query']} HTTP/1.1\r\n";
-    $headers .= "Host: {$target['host']}\r\n";
-    $headers .= "User-Agent: Serendipity/{$serendipity['version']}\r\n";
-    $headers .= "Content-Type: application/x-www-form-urlencoded; charset=" . LANG_CHARSET . "\r\n";
-    $headers .= "Content-Length: " . strlen($data) . "\r\n";
-    $headers .= "Connection: close\r\n";
-    $headers .= "\r\n";
-    $headers .= $data;
-
-    fwrite($sock, $headers);
-
-    $res = '';
-    while (!feof($sock)) {
-        $res .= fgets($sock, 1024);
-    }
-
-    fclose($sock);
-    return $res;
+    $fContent = $req->getResponseBody();
+    return $fContent;
 }
 
 /**
@@ -118,14 +107,14 @@ function _serendipity_send($loc, $data) {
  * @param   string  The title of our entry
  * @param   string  The text of our entry
  * @param   string  A comparsion URL
+
  * @return string   Response
  */
 function serendipity_trackback_autodiscover($res, $loc, $url, $author, $title, $text, $loc2 = '') {
     if (!preg_match('@trackback:ping(\s*rdf:resource)?\s*=\s*["\'](https?:[^"\']+)["\']@i', $res, $matches)) {
         $matches = array();
         serendipity_plugin_api::hook_event('backend_trackback_check', $matches, $loc);
-        
+
         // Plugins may say that a URI is valid, in situations where a blog has no valid RDF metadata
         if (empty($matches[2])) {
             echo '<div>&#8226; ' . sprintf(TRACKBACK_FAILED, TRACKBACK_NOT_FOUND) . '</div>';
@@ -200,32 +189,20 @@ global $serendipity;
 
     $parsed_loc = $u['scheme'] . '://' . $u['host'] . $port . $u['path'];
 
-    $res = '';
+    require_once S9Y_PEAR_PATH . 'HTTP/Request.php';
+    $req = &new HTTP_Request($parsed_loc, array('allowRedirects' => true, 'maxRedirects' => 5, 'method' => 'GET'));
+    $res = $req->sendRequest();
 
-    $fp  = @fsockopen($u['host'], $u['port'], $err, $timeout);
-    if (!$fp) {
+    if (PEAR::isError($res)) {
         echo '<div>&#8226; ' . sprintf(TRACKBACK_COULD_NOT_CONNECT, $u['host'], $u['port']) .'</div>';
         return;
-    } else {
-        fputs($fp, "GET {$u['path']} HTTP/1.0\r\n");
-        fputs($fp, "Host: {$u['host']}\r\n");
-        fputs($fp, "User-Agent: Serendipity/{$serendipity['version']}\r\n");
-        fputs($fp, "Connection: close\r\n\r\n");
-
-        while ($fp && !feof($fp) && strlen($res) < $serendipity['trackback_filelimit']) {
-            $res .= fgets($fp, 4096);
-        }
-        fclose($fp);
-
-        if (strlen($res) >= $serendipity['trackback_filelimit']) {
-            echo '<div>&#8226; ' . sprintf(TRACKBACK_SIZE, $serendipity['trackback_filelimit']) .'</div>';
-            return;
-        }
     }
 
-    if (strlen($res) != 0) {
-        serendipity_trackback_autodiscover($res, $parsed_loc, $url, $author, $title, $text, $loc);
-        serendipity_pingback_autodiscover($loc, $res);
+    $fContent = $req->getResponseBody();
+
+    if (strlen($fContent) != 0) {
+        serendipity_trackback_autodiscover($fContent, $parsed_loc, $url, $author, $title, $text, $loc);
+        serendipity_pingback_autodiscover($loc, $fContent);
     } else {
         echo '<div>&#8226; ' . TRACKBACK_NO_DATA . '</div>';
     }