diff --git a/docs/NEWS b/docs/NEWS
index 6e30d23e..ba8da10c 100644
--- a/docs/NEWS
+++ b/docs/NEWS
@@ -3,6 +3,9 @@
Version 1.3 ()
------------------------------------------------------------------------
+ * Pingback receiving is working now with internal functionality in
+ comment.php (brockhaus)
+
* On the fly update of the media database: Not only images are added
but video and audio, too. (brockhaus)
diff --git a/include/functions_trackbacks.inc.php b/include/functions_trackbacks.inc.php
index 4882d3db..c825cd0e 100644
--- a/include/functions_trackbacks.inc.php
+++ b/include/functions_trackbacks.inc.php
@@ -377,14 +377,29 @@ function add_trackback ($id, $title, $url, $name, $excerpt) {
*/
function add_pingback ($id, $postdata) {
global $serendipity;
+ $sourceURI = getPingbackParam('sourceURI', $postdata);
+ $targetURI = getPingbackParam('targetURI', $postdata);
+ if (isset($sourceURI) && isset($targetURI)) {
+ $path = parse_url($sourceURI);
+ $local = $targetURI;
+ $comment['title'] = 'PingBack';
+ $comment['url'] = $sourceURI;
+ $comment['comment'] = '';
+ $comment['name'] = $path['host'];
+ fetchPingbackData($comment);
- if(preg_match('@\s*\s*pingback.ping\s*\s*\s*\s*\s*([^<]*)\s*\s*\s*\s*\s*([^<]*)\s*\s*\s*\s*@i', $postdata, $matches)) {
+ serendipity_saveComment($id, $comment, 'PINGBACK');
+ return 1;
+ }
+ if(preg_match('@\s*\s*pingback.ping\s*\s*\s*\s*\s*([^<]*)\s*\s*\s*\s*\s*([^<]*)\s*\s*\s*\s*@is', $postdata, $matches)) {
$remote = $matches[1];
$local = $matches[2];
- $comment['title'] = '';
+ $path = parse_url($remote);
+ $comment['title'] = 'PingBack';
$comment['url'] = $remote;
$comment['comment'] = '';
- $comment['name'] = '';
+ $comment['name'] = $path['host'];
+ fetchPingbackData($comment);
serendipity_saveComment($id, $comment, 'PINGBACK');
return 1;
@@ -392,6 +407,78 @@ function add_pingback ($id, $postdata) {
return 0;
}
+/**
+ * Gets a XML-RPC pingback.ping value by given parameter name
+ * @access private
+ * @param string Name of the paramameter
+ * @param string Buffer containing the pingback XML
+ */
+function getPingbackParam($paramName, $data) {
+ $pattern = ".*?\s*pingback.ping\s*.*?.*?\s*((\s*$paramName\s*\s*\s*([^<]*)\s*)|(\s*([^<]*)\s*\s*\s*$paramName\s*))\s*.*?.*?";
+ if (preg_match('@' . $pattern .'@is',$data, $matches)) {
+ return $matches[3];
+ } else {
+ return null;
+ }
+}
+
+/**
+ * Fetches additional comment data from the page that sent the pingback
+ * @access private
+ * @param array comment array to be filled
+ */
+function fetchPingbackData( &$comment) {
+ if (!isset($comment) || !is_array($comment) || !isset($comment['url'])) {
+ return;
+ }
+ require_once S9Y_PEAR_PATH . 'HTTP/Request.php';
+ $url = $comment['url'];
+
+ // Allow redirection
+ $request_pars['allowRedirects'] = true;
+ $request_pars['timeout'] = 20;
+
+ serendipity_request_start();
+
+ // Request the page
+ $req = &new HTTP_Request($url, $request_pars);
+
+ // code 200: OK, code 30x: REDIRECTION
+ $responses = "/(200 OK)|(30[0-9] Found)/"; // |(30[0-9] Moved)
+ if ((PEAR::isError($req->sendRequest()) || preg_match($responses, $req->getResponseCode()))) {
+ // nothing to do,
+ }
+ else {
+ $fContent = $req->getResponseBody();
+
+ // Get a title
+ if (preg_match('@
.*?(.*?).*?@is',$fContent,$matches)) {
+ $comment['title'] = strip_tags($matches[1]);
+ }
+
+ // Get a part of the article
+ if (preg_match('@]*>(.*?)@is',$fContent,$matches)) {
+
+ $body = strip_tags($matches[1]);
+
+ //TODO: Serendipity comes into trouble wit html_entity_decode and "Umlaute"
+ $body = str_replace(array(" "),array(' '),$body);
+
+ // replace whitespace with single space
+ $body = preg_replace('@\s+@s', ' ', $body);
+
+ // truncate the text to 200 chars
+ $arr = str_split($body, 200);
+ $body = $arr[0];
+
+ $comment['comment'] = $body . '[..]';
+ }
+ }
+
+ serendipity_request_end();
+
+}
+
/**
* Create an excerpt for a trackback to send
*