3 * StatusNet, the distributed open-source microblogging tool
5 * Plugin to do linkbacks for notices containing links
9 * LICENCE: This program is free software: you can redistribute it and/or modify
10 * it under the terms of the GNU Affero General Public License as published by
11 * the Free Software Foundation, either version 3 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU Affero General Public License for more details.
19 * You should have received a copy of the GNU Affero General Public License
20 * along with this program. If not, see <http://www.gnu.org/licenses/>.
24 * @author Evan Prodromou <evan@status.net>
25 * @copyright 2009 StatusNet, Inc.
26 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
27 * @link http://status.net/
30 if (!defined('STATUSNET')) {
34 require_once('Auth/Yadis/Yadis.php');
36 define('LINKBACKPLUGIN_VERSION', '0.1');
39 * Plugin to do linkbacks for notices containing URLs
41 * After new notices are saved, we check their text for URLs. If there
42 * are URLs, we test each URL to see if it supports any
46 * @author Evan Prodromou <evan@status.net>
47 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
48 * @link http://status.net/
53 class LinkbackPlugin extends Plugin
57 function __construct()
59 parent::__construct();
62 function onEndNoticeSave($notice)
64 if ($notice->is_local == 1) {
65 // Try to avoid actually mucking with the
67 $c = $notice->content;
68 $this->notice = $notice;
70 common_replace_urls_callback($c,
71 array($this, 'linkbackUrl'));
76 function linkbackUrl($url)
78 common_log(LOG_DEBUG,"Attempting linkback for " . $url);
81 $url = htmlspecialchars_decode($orig);
82 $scheme = parse_url($url, PHP_URL_SCHEME);
83 if (!in_array($scheme, array('http', 'https'))) {
87 // XXX: Do a HEAD first to save some time/bandwidth
89 $fetcher = Auth_Yadis_Yadis::getHTTPFetcher();
91 $result = $fetcher->get($url,
92 array('User-Agent: ' . $this->userAgent(),
93 'Accept: application/html+xml,text/html'));
95 if (!in_array($result->status, array('200', '206'))) {
102 if (array_key_exists('X-Pingback', $result->headers)) {
103 $pb = $result->headers['X-Pingback'];
104 } else if (preg_match('/<link rel="pingback" href="([^"]+)" ?\/?>/',
111 $this->pingback($result->final_url, $pb);
113 $tb = $this->getTrackback($result->body, $result->final_url);
115 $this->trackback($result->final_url, $tb);
122 function pingback($url, $endpoint)
124 $args = array($this->notice->uri, $url);
126 if (!extension_loaded('xmlrpc')) {
127 if (!dl('xmlrpc.so')) {
128 common_log(LOG_ERR, "Can't pingback; xmlrpc extension not available.");
132 $request = xmlrpc_encode_request('pingback.ping', $args);
133 $context = stream_context_create(array('http' => array('method' => "POST",
135 "Content-Type: text/xml\r\n".
136 "User-Agent: " . $this->userAgent(),
137 'content' => $request)));
138 $file = file_get_contents($endpoint, false, $context);
140 common_log(LOG_WARNING,
141 "Pingback request failed for '$url' ($endpoint)");
143 $response = xmlrpc_decode($file);
144 if (xmlrpc_is_fault($response)) {
145 common_log(LOG_WARNING,
146 "Pingback error for '$url' ($endpoint): ".
147 "$response[faultString] ($response[faultCode])");
150 "Pingback success for '$url' ($endpoint): ".
156 // Largely cadged from trackback_cls.php by
157 // Ran Aroussi <ran@blogish.org>, GPL2 or any later version
158 // http://phptrackback.sourceforge.net/
160 function getTrackback($text, $url)
162 if (preg_match_all('/(<rdf:RDF.*?<\/rdf:RDF>)/sm', $text, $match, PREG_SET_ORDER)) {
163 for ($i = 0; $i < count($match); $i++) {
164 if (preg_match('|dc:identifier="' . preg_quote($url) . '"|ms', $match[$i][1])) {
165 $rdf_array[] = trim($match[$i][1]);
169 // Loop through the RDFs array and extract trackback URIs
171 $tb_array = array(); // <- holds list of trackback URIs
173 if (!empty($rdf_array)) {
175 for ($i = 0; $i < count($rdf_array); $i++) {
176 if (preg_match('/trackback:ping="([^"]+)"/', $rdf_array[$i], $array)) {
177 $tb_array[] = trim($array[1]);
185 if (empty($tb_array)) {
192 if (preg_match_all('/(<a[^>]*?rel=[\'"]trackback[\'"][^>]*?>)/', $text, $match)) {
193 foreach ($match[1] as $atag) {
194 if (preg_match('/href=[\'"]([^\'"]*?)[\'"]/', $atag, $url)) {
204 function trackback($url, $endpoint)
206 $profile = $this->notice->getProfile();
208 $args = array('title' => sprintf(_('%1$s\'s status on %2$s'),
210 common_exact_date($this->notice->created)),
211 'excerpt' => $this->notice->content,
212 'url' => $this->notice->uri,
213 'blog_name' => $profile->nickname);
215 $fetcher = Auth_Yadis_Yadis::getHTTPFetcher();
217 $result = $fetcher->post($endpoint,
218 http_build_query($args),
219 array('User-Agent: ' . $this->userAgent()));
221 if ($result->status != '200') {
222 common_log(LOG_WARNING,
223 "Trackback error for '$url' ($endpoint): ".
227 "Trackback success for '$url' ($endpoint): ".
234 return 'LinkbackPlugin/'.LINKBACKPLUGIN_VERSION .
235 ' StatusNet/' . STATUSNET_VERSION;