diff --git a/plugins/Favorite/FavoritePlugin.php b/plugins/Favorite/FavoritePlugin.php index c7a66fe751..438ea803ae 100644 --- a/plugins/Favorite/FavoritePlugin.php +++ b/plugins/Favorite/FavoritePlugin.php @@ -194,7 +194,7 @@ class FavoritePlugin extends ActivityVerbHandlerPlugin $actobj = $act->objects[0]; $object = Fave::saveActivityObject($actobj, $stored); - $stored->object_type = ActivityUtils::resolveUri($object->getObjectType(), true); + $stored->object_type = $object->getObjectType(); return $object; } diff --git a/plugins/Linkback/LinkbackPlugin.php b/plugins/Linkback/LinkbackPlugin.php index 0bbd55d43a..3c051e88a4 100644 --- a/plugins/Linkback/LinkbackPlugin.php +++ b/plugins/Linkback/LinkbackPlugin.php @@ -65,9 +65,27 @@ class LinkbackPlugin extends Plugin // notice content $c = $notice->content; $this->notice = $notice; - // Ignoring results - common_replace_urls_callback($c, - array($this, 'linkbackUrl')); + + if(!$notice->getProfile()-> + getPref("linkbackplugin", "disable_linkbacks") + ) { + // Ignoring results + common_replace_urls_callback($c, + array($this, 'linkbackUrl')); + } + + if($notice->isRepeat()) { + $repeat = Notice::getByID($notice->repeat_of); + $this->linkbackUrl($repeat->getUrl()); + } else if(!empty($notice->reply_to)) { + $parent = $notice->getParent(); + $this->linkbackUrl($parent->getUrl()); + } + + $replyProfiles = Profile::multiGet('id', $notice->getReplies()); + foreach($replyProfiles->fetchAll('profileurl') as $profileurl) { + $this->linkbackUrl($profileurl); + } } return true; } @@ -95,32 +113,89 @@ class LinkbackPlugin extends Plugin return $orig; } - $pb = null; - $tb = null; + // XXX: Should handle relative-URI resolution in these detections - if (array_key_exists('X-Pingback', $result->headers)) { - $pb = $result->headers['X-Pingback']; - } else if (preg_match('//', - $result->body, - $match)) { - $pb = $match[1]; - } - - if (!empty($pb)) { - $this->pingback($result->final_url, $pb); + $wm = $this->getWebmention($result); + if(!empty($wm)) { + // It is the webmention receiver's job to resolve source + // Ref: https://github.com/converspace/webmention/issues/43 + $this->webmention($url, $wm); } else { - $tb = $this->getTrackback($result->body, $result->final_url); - if (!empty($tb)) { - $this->trackback($result->final_url, $tb); + $pb = $this->getPingback($result); + if (!empty($pb)) { + // Pingback still looks for exact URL in our source, so we + // must send what we have + $this->pingback($url, $pb); + } else { + $tb = $this->getTrackback($result); + if (!empty($tb)) { + $this->trackback($result->final_url, $tb); + } } } return $orig; } + // Based on https://github.com/indieweb/mention-client-php + // which is licensed Apache 2.0 + function getWebmention($result) { + // XXX: the fetcher only gives back one of each header, so this may fail on multiple Link headers + if(preg_match('~<((?:https?://)?[^>]+)>; rel="webmention"~', $result->headers['Link'], $match)) { + return $match[1]; + } elseif(preg_match('~<((?:https?://)?[^>]+)>; rel="http://webmention.org/?"~', $result->headers['Link'], $match)) { + return $match[1]; + } + + if(preg_match('/<(?:link|a)[ ]+href="([^"]+)"[ ]+rel="[^" ]* ?webmention ?[^" ]*"[ ]*\/?>/i', $result->body, $match) + || preg_match('/<(?:link|a)[ ]+rel="[^" ]* ?webmention ?[^" ]*"[ ]+href="([^"]+)"[ ]*\/?>/i', $result->body, $match)) { + return $match[1]; + } elseif(preg_match('/<(?:link|a)[ ]+href="([^"]+)"[ ]+rel="http:\/\/webmention\.org\/?"[ ]*\/?>/i', $result->body, $match) + || preg_match('/<(?:link|a)[ ]+rel="http:\/\/webmention\.org\/?"[ ]+href="([^"]+)"[ ]*\/?>/i', $result->body, $match)) { + return $match[1]; + } + } + + function webmention($url, $endpoint) { + $source = $this->notice->getUrl(); + + $payload = array( + 'source' => $source, + 'target' => $url + ); + + $request = HTTPClient::start(); + try { + $response = $request->post($endpoint, + array( + 'Content-type: application/x-www-form-urlencoded', + 'Accept: application/json' + ), + $payload + ); + + if(!in_array($response->getStatus(), array(200,202))) { + common_log(LOG_WARNING, + "Webmention request failed for '$url' ($endpoint)"); + } + } catch (HTTP_Request2_Exception $e) { + common_log(LOG_WARNING, + "Webmention request failed for '$url' ($endpoint)"); + } + } + + function getPingback($result) { + if (array_key_exists('X-Pingback', $result->headers)) { + return $result->headers['X-Pingback']; + } else if(preg_match('/<(?:link|a)[ ]+href="([^"]+)"[ ]+rel="[^" ]* ?pingback ?[^" ]*"[ ]*\/?>/i', $result->body, $match) + || preg_match('/<(?:link|a)[ ]+rel="[^" ]* ?pingback ?[^" ]*"[ ]+href="([^"]+)"[ ]*\/?>/i', $result->body, $match)) { + return $match[1]; + } + } + function pingback($url, $endpoint) { - $args = array($this->notice->uri, $url); + $args = array($this->notice->getUrl(), $url); if (!extension_loaded('xmlrpc')) { if (!dl('xmlrpc.so')) { @@ -131,9 +206,10 @@ class LinkbackPlugin extends Plugin $request = HTTPClient::start(); try { + $request->setBody(xmlrpc_encode_request('pingback.ping', $args)); $response = $request->post($endpoint, array('Content-Type: text/xml'), - xmlrpc_encode_request('pingback.ping', $args)); + false); $response = xmlrpc_decode($response->getBody()); if (xmlrpc_is_fault($response)) { common_log(LOG_WARNING, @@ -153,8 +229,11 @@ class LinkbackPlugin extends Plugin // Largely cadged from trackback_cls.php by // Ran Aroussi , GPL2 or any later version // http://phptrackback.sourceforge.net/ - function getTrackback($text, $url) + function getTrackback($result) { + $text = $result->body; + $url = $result->final_url; + if (preg_match_all('/()/sm', $text, $match, PREG_SET_ORDER)) { for ($i = 0; $i < count($match); $i++) { if (preg_match('|dc:identifier="' . preg_quote($url) . '"|ms', $match[$i][1])) { @@ -246,4 +325,23 @@ class LinkbackPlugin extends Plugin 'or Trackback protocols.')); return true; } + + public function onStartInitializeRouter(URLMapper $m) + { + $m->connect('settings/linkback', array('action' => 'linkbacksettings')); + return true; + } + + function onEndAccountSettingsNav($action) + { + $action_name = $action->trimmed('action'); + + $action->menuItem(common_local_url('linkbacksettings'), + // TRANS: OpenID plugin menu item on user settings page. + _m('MENU', 'Send Linkbacks'), + // TRANS: OpenID plugin tooltip for user settings menu item. + _m('Opt-out of sending linkbacks.'), + $action_name === 'linkbacksettings'); + return true; + } } diff --git a/plugins/Linkback/actions/linkbacksettings.php b/plugins/Linkback/actions/linkbacksettings.php new file mode 100644 index 0000000000..261e2979dc --- /dev/null +++ b/plugins/Linkback/actions/linkbacksettings.php @@ -0,0 +1,91 @@ +. + * + * @category Settings + * @package StatusNet + * @author Stephen Paul Weber + * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0 + */ + +if (!defined('GNUSOCIAL')) { exit(1); } + +/** + * Settings for Linkback + * + * Lets users opt out of sending linkbacks + * + * @category Settings + * @author Stephen Paul Weber + * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0 + */ +class LinkbacksettingsAction extends SettingsAction +{ + /** + * Title of the page + * + * @return string Page title + */ + function title() + { + // TRANS: Title of Linkback settings page for a user. + return _m('TITLE','Linkback settings'); + } + + /** + * Instructions for use + * + * @return string Instructions for use + */ + function getInstructions() + { + // TRANS: Form instructions for Linkback settings. + return _m('Linkbacks inform post authors when you link to them. ' . + 'You can disable this feature here.'); + } + + function showContent() + { + $this->elementStart('form', array('method' => 'post', + 'class' => 'form_settings', + 'action' => + common_local_url('linkbacksettings'))); + $this->hidden('token', common_session_token()); + + $this->elementStart('fieldset'); + $this->element('legend', null, _m('LEGEND','Preferences')); + $this->checkbox('disable_linkbacks', "Opt out of sending linkbacks for URLs you post", $this->scoped->getPref("linkbackplugin", "disable_linkbacks")); + // TRANS: Button text to save OpenID prefs + $this->submit('settings_linkback_prefs_save', _m('BUTTON','Save'), 'submit', 'save_prefs'); + $this->elementEnd('fieldset'); + + $this->elementEnd('form'); + } + + /** + * Handle a POST request + * + * @return void + */ + protected function doPost() + { + $x = $this->scoped->setPref("linkbackplugin", "disable_linkbacks", $this->boolean('disable_linkbacks')); + + return _m('Linkback preferences saved.'); + } +} diff --git a/plugins/OStatus/OStatusPlugin.php b/plugins/OStatus/OStatusPlugin.php index 5a871f322b..e8a3c2d666 100644 --- a/plugins/OStatus/OStatusPlugin.php +++ b/plugins/OStatus/OStatusPlugin.php @@ -118,6 +118,9 @@ class OStatusPlugin extends Plugin // Incoming from a foreign PuSH hub $qm->connect('pushin', 'PushInQueueHandler'); + + // Re-subscribe feeds that need renewal + $qm->connect('pushrenew', 'PushRenewQueueHandler'); return true; } @@ -1396,4 +1399,20 @@ class OStatusPlugin extends Plugin // Since we completed the salmon slap, we discontinue the event return false; } + + public function onCronDaily() + { + try { + $sub = FeedSub::renewalCheck(); + } catch (NoResultException $e) { + common_log(LOG_INFO, "There were no expiring feeds."); + return; + } + + $qm = QueueManager::get(); + while ($sub->fetch()) { + $item = array('feedsub_id' => $sub->id); + $qm->enqueue($item, 'pushrenew'); + } + } } diff --git a/plugins/OStatus/classes/FeedSub.php b/plugins/OStatus/classes/FeedSub.php index 6585e9ffb7..0c7129e885 100644 --- a/plugins/OStatus/classes/FeedSub.php +++ b/plugins/OStatus/classes/FeedSub.php @@ -295,7 +295,7 @@ class FeedSub extends Managed_DataObject { $fs = new FeedSub(); // the "" empty string check is because we historically haven't saved unsubscribed feeds as NULL - $fs->whereAdd('sub_end IS NOT NULL AND sub_end!="" AND sub_end < NOW() - INTERVAL 1 day'); + $fs->whereAdd('sub_end IS NOT NULL AND sub_end!="" AND sub_end < NOW() + INTERVAL 1 day'); if (!$fs->find()) { // find can be both false and 0, depending on why nothing was found throw new NoResultException($fs); } diff --git a/plugins/OStatus/lib/pushrenewqueuehandler.php b/plugins/OStatus/lib/pushrenewqueuehandler.php new file mode 100644 index 0000000000..d79cbe503f --- /dev/null +++ b/plugins/OStatus/lib/pushrenewqueuehandler.php @@ -0,0 +1,49 @@ +. + */ + +if (!defined('STATUSNET')) { + exit(1); +} + +/** + * Renew an expiring feedsub + * @package FeedSub + * @author Stephen Paul Weber + */ +class PushRenewQueueHandler extends QueueHandler +{ + function transport() + { + return 'pushrenew'; + } + + function handle($data) + { + $feedsub_id = $data['feedsub_id']; + $feedsub = FeedSub::getKV('id', $feedsub_id); + if ($feedsub instanceof FeedSub) { + try { + common_log(LOG_INFO, "Renewing feed subscription\n\tExp.: {$feedsub->sub_end}\n\tFeed: {$feedsub->uri}\n\tHub: {$feedsub->huburi}"); + $feedsub->renew(); + } catch(Exception $e) { + common_log(LOG_ERR, "Exception during PuSH renew processing for $feedsub->uri: " . $e->getMessage()); + } + } else { + common_log(LOG_ERR, "Discarding renew for unknown feed subscription id $feedsub_id"); + } + return true; + } +} diff --git a/plugins/WebFinger/WebFingerPlugin.php b/plugins/WebFinger/WebFingerPlugin.php index c2a9c69d0c..ce8c847aa7 100644 --- a/plugins/WebFinger/WebFingerPlugin.php +++ b/plugins/WebFinger/WebFingerPlugin.php @@ -148,7 +148,7 @@ class WebFingerPlugin extends Plugin $url = common_local_url('webfinger') . '?resource='.$acct; foreach (array(Discovery::JRD_MIMETYPE, Discovery::XRD_MIMETYPE) as $type) { - header('Link: <'.$url.'>; rel="'. Discovery::LRDD_REL.'"; type="'.$type.'"'); + header('Link: <'.$url.'>; rel="'. Discovery::LRDD_REL.'"; type="'.$type.'"', false); } } }