Simple status box for the sidebar.
[elgg_plugins.git] / trackback / lib.php
blob8f37cf4d95766c531c89b9866039de5b5147a961
1 <?php
2 /*
3 Trackback plugin for Elgg
4 Initial author: Marcus Povey <marcus@dushka.co.uk>
5 */
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() {
25 /**
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)
31 $output = "";
32 $cnt = 0;
34 for ($n = 0; $n < strlen($text); $n++)
36 if ($text[$n]==" ")
37 $cnt++;
39 if ($cnt>$maxwords)
40 return $output;
42 $output .= $text[$n];
45 return $output;
48 /**
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) == "")
54 return FALSE;
56 // Construct data
57 $data_o = $query;
58 $serverdetails = parse_url($server);
60 $host = $serverdetails['host'];
61 $port = 80;
63 if ($serverdetails['scheme'] == "https") {
64 $host = "ssl://" . $host;
65 $port = 443;
68 if (($serverdetails['port']!="") && ($serverdetails['port']!=$port)) $port = $serverdetails['port'];
70 // Open socket
71 $socket = @fsockopen($host, $port,$errno, $errstr);
73 if ($socket==FALSE)
74 return FALSE;
76 // Construct URI to query
77 if (isset($serverdetails['query'])) {
78 $uri = $serverdetails['path'] . "?" . $serverdetails['query'];
79 } else {
80 $uri = $serverdetails['path'];
83 // Construct query
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";
91 // Construct buffer
92 $buff = $header_o . "\n" . $data_o;
94 // Write query
95 fputs($socket, $buff, strlen($buff));
97 // Read response
98 $blob = "";
99 while (!feof($socket)) {
100 $blob .= fgets($socket);
103 // Close socket
104 fclose($socket);
106 // Parse out session variables
107 $blobs = explode("\r\n\r\n", $blob);
108 $headers = $blobs[0];
109 $body = $blobs[1];
111 $r_data = $body;
113 // Sanity check data
114 if (strlen(trim($r_data))==0)
115 return FALSE;
117 return $r_data;
121 function trackback_Ok()
124 <response>
125 <error>0</error>
126 </response>
127 <?php
130 function trackback_Error($message)
133 <response>
134 <error>1</error>
135 <message><?php echo $message; ?></message>
136 </response>
137 <?php
138 die();
141 /**
142 * Send a trackback ping to a url.
143 * @returns false on error
145 function trackback_sendtrackbackping($post, $url)
147 global $CFG;
149 $url = trim($url);
151 // Pull user details
152 $username = urlencode(user_info("username", $post->weblog));
153 $blog_name = urlencode(user_info("name",$post->weblog));
155 $words = trackback_TruncateToWords(
156 htmlentities(
157 strip_tags(
158 stripslashes($post->body)
164 $title = urlencode(strip_tags(stripslashes($post->title)));
165 $excerpt = urlencode(
166 trim(
167 $words . "...",
168 "\r\n\t \0."
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) )
177 return true;
179 return false;
182 /**
183 * Mine the post for links and see if we can find trackbacks.
185 function trackback_parsefortrackbacks($object_type, $event, $object)
187 global $messages;
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
206 // Find urls in post
207 $rtn = preg_match_all("*href=\"([^\"]+)\"*", $object->body, $urlarray);
208 //$rtn = preg_match_all("/href=\"(.*)\"/imsU", $object->body, $urlarray);
210 if ($rtn>0) {
212 foreach ($urlarray[1] as $url)
214 // Fetch permalink
215 $url = trim($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=");
229 $a+=16;
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";
250 else
251 $messages[] = "Trackback sent to $url";
256 return $object;