3 Trackback plugin for Elgg
4 Initial author: Marcus Povey <marcus@dushka.co.uk>
7 /** Initialise the trackback code & hook into framework */
8 function trackback_init() {
10 global $function, $CFG, $metatags;
12 // Register javascript
13 $metatags .= "<script type=\"text/javascript\" src=\"{$CFG->wwwroot}mod/trackback/trackback.js\"><!-- trackback js --></script>";
15 $function['weblog:post:links'][] = $CFG->dirroot
. "mod/trackback/lib/weblog_post_links.php"; // Add trackback link.
16 $function['display:content:toolbar'][] = $CFG->dirroot
. "mod/trackback/lib/weblog_post_trackback.php"; // Add trackback options to post
18 listen_for_event("weblog_post","publish","trackback_parsefortrackbacks");
21 function trackback_pagesetup() {
26 * Truncate some text to a given number of words.
27 * This function truncates some text to a given number of words.
29 function trackback_TruncateToWords($text, $maxwords)
34 for ($n = 0; $n < strlen($text); $n++
)
49 * This function sends some data as a post request to a server url.
51 function trackback_PostSend($server, $query, $contenttype = "application/x-www-form-urlencoded") {
53 if (trim($server) == "")
58 $serverdetails = parse_url($server);
60 $host = $serverdetails['host'];
63 if ($serverdetails['scheme'] == "https") {
64 $host = "ssl://" . $host;
68 if (($serverdetails['port']!="") && ($serverdetails['port']!=$port)) $port = $serverdetails['port'];
71 $socket = @fsockopen
($host, $port,$errno, $errstr);
76 // Construct URI to query
77 if (isset($serverdetails['query'])) {
78 $uri = $serverdetails['path'] . "?" . $serverdetails['query'];
80 $uri = $serverdetails['path'];
84 $header_o = "POST " . $uri. " HTTP/1.0\r\n";
85 $header_o .= "Host: ".$serverdetails['host']."\r\n";
86 $header_o .= "Content-Length: " . strlen($data_o) . "\r\n";
87 $header_o .= "Content-type: $contenttype\r\n";
88 $header_o .= "User-Agent: Elgg trackback pinger\r\n";
89 $header_o .= "Connection: close\r\n";
92 $buff = $header_o . "\n" . $data_o;
95 fputs($socket, $buff, strlen($buff));
99 while (!feof($socket)) {
100 $blob .= fgets($socket);
106 // Parse out session variables
107 $blobs = explode("\r\n\r\n", $blob);
108 $headers = $blobs[0];
114 if (strlen(trim($r_data))==0)
121 function trackback_Ok()
130 function trackback_Error($message)
135 <message
><?php
echo $message; ?
></message
>
142 * Send a trackback ping to a url.
143 * @returns false on error
145 function trackback_sendtrackbackping($post, $url)
152 $username = urlencode(user_info("username", $post->weblog
));
153 $blog_name = urlencode(user_info("name",$post->weblog
));
155 $words = trackback_TruncateToWords(
158 stripslashes($post->body
)
164 $title = urlencode(strip_tags(stripslashes($post->title
)));
165 $excerpt = urlencode(
171 $src = urlencode($CFG->wwwroot
. $username . "/weblog/" . $post->ident
. ".html");
173 $query = "title=$title&url=$src&excerpt=$excerpt&blog_name=$blog_name";
175 $result = trackback_PostSend($url, $query);
176 if (($result != FALSE) && (strpos($result, "<error>0")!==FALSE) )
183 * Mine the post for links and see if we can find trackbacks.
185 function trackback_parsefortrackbacks($object_type, $event, $object)
189 if ($event=="publish")
191 // Check options for trackbacks
192 $SendTrackbacks = trim(optional_param('sendtrackbacks',''));
193 $giventrackbacks = trim(optional_param('trackbackurls',''), " \r\n,");
195 $TrackbackUrls = array();
197 // Have we been provided a trackback urls?
198 if ($giventrackbacks!="")
199 $TrackbackUrls = explode(",", $giventrackbacks);
201 // Do we need to search for trackbacks
202 if ($SendTrackbacks!="")
204 // Find trackback pings
207 $rtn = preg_match_all("*href=\"([^\"]+)\"*", $object->body
, $urlarray);
208 //$rtn = preg_match_all("/href=\"(.*)\"/imsU", $object->body, $urlarray);
212 foreach ($urlarray[1] as $url)
217 // attempt to fetch url
218 $page = file_get_contents($url);
220 // find trackback containing the permalink
221 preg_match_all("/<rdf\:RDF(.*)<\/rdf\:RDF>/imsU", $page, $matches);
223 foreach ($matches[1] as $rdf)
225 if (strpos($rdf, $url)!==FALSE)
227 // Found the correct trackback link
228 $a = strpos($rdf, "trackback:ping=");
231 $b = strpos($rdf, "\"", $a);
233 if (($a!==FALSE) && ($b!==FALSE))
235 $TrackbackUrls[] = substr($rdf, $a, $b - $a);
245 // Ok, now send trackbacks to each url we have been given or harvested.
246 foreach (array_unique($TrackbackUrls) as $url)
248 if (!trackback_sendtrackbackping($object, $url))
249 $messages[] = "Could not send trackback to $url";
251 $messages[] = "Trackback sent to $url";