Merge branch 'pings-interop' into 'master'
Clean up Linkback plugin and add Webmention support Closes #82 Fixes Pingback, adds Webmention, makes linkbacks opt-out-able for users (at request of quitter.se admin) See merge request !36
This commit is contained in:
		@@ -65,9 +65,27 @@ class LinkbackPlugin extends Plugin
 | 
				
			|||||||
            // notice content
 | 
					            // notice content
 | 
				
			||||||
            $c = $notice->content;
 | 
					            $c = $notice->content;
 | 
				
			||||||
            $this->notice = $notice;
 | 
					            $this->notice = $notice;
 | 
				
			||||||
            // Ignoring results
 | 
					
 | 
				
			||||||
            common_replace_urls_callback($c,
 | 
					            if(!$notice->getProfile()->
 | 
				
			||||||
                                         array($this, 'linkbackUrl'));
 | 
					                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;
 | 
					        return true;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
@@ -95,32 +113,89 @@ class LinkbackPlugin extends Plugin
 | 
				
			|||||||
            return $orig;
 | 
					            return $orig;
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        $pb = null;
 | 
					        // XXX: Should handle relative-URI resolution in these detections
 | 
				
			||||||
        $tb = null;
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
        if (array_key_exists('X-Pingback', $result->headers)) {
 | 
					        $wm = $this->getWebmention($result);
 | 
				
			||||||
            $pb = $result->headers['X-Pingback'];
 | 
					        if(!empty($wm)) {
 | 
				
			||||||
        } else if (preg_match('/<link rel="pingback" href="([^"]+)" ?\/?>/',
 | 
					            // It is the webmention receiver's job to resolve source
 | 
				
			||||||
                              $result->body,
 | 
					            // Ref: https://github.com/converspace/webmention/issues/43
 | 
				
			||||||
                              $match)) {
 | 
					            $this->webmention($url, $wm);
 | 
				
			||||||
            $pb = $match[1];
 | 
					 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        if (!empty($pb)) {
 | 
					 | 
				
			||||||
            $this->pingback($result->final_url, $pb);
 | 
					 | 
				
			||||||
        } else {
 | 
					        } else {
 | 
				
			||||||
            $tb = $this->getTrackback($result->body, $result->final_url);
 | 
					            $pb = $this->getPingback($result);
 | 
				
			||||||
            if (!empty($tb)) {
 | 
					            if (!empty($pb)) {
 | 
				
			||||||
                $this->trackback($result->final_url, $tb);
 | 
					                // 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;
 | 
					        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)
 | 
					    function pingback($url, $endpoint)
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
        $args = array($this->notice->uri, $url);
 | 
					        $args = array($this->notice->getUrl(), $url);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        if (!extension_loaded('xmlrpc')) {
 | 
					        if (!extension_loaded('xmlrpc')) {
 | 
				
			||||||
            if (!dl('xmlrpc.so')) {
 | 
					            if (!dl('xmlrpc.so')) {
 | 
				
			||||||
@@ -131,9 +206,10 @@ class LinkbackPlugin extends Plugin
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
        $request = HTTPClient::start();
 | 
					        $request = HTTPClient::start();
 | 
				
			||||||
        try {
 | 
					        try {
 | 
				
			||||||
 | 
					            $request->setBody(xmlrpc_encode_request('pingback.ping', $args));
 | 
				
			||||||
            $response = $request->post($endpoint,
 | 
					            $response = $request->post($endpoint,
 | 
				
			||||||
                array('Content-Type: text/xml'),
 | 
					                array('Content-Type: text/xml'),
 | 
				
			||||||
                xmlrpc_encode_request('pingback.ping', $args));
 | 
					                false);
 | 
				
			||||||
            $response = xmlrpc_decode($response->getBody());
 | 
					            $response = xmlrpc_decode($response->getBody());
 | 
				
			||||||
            if (xmlrpc_is_fault($response)) {
 | 
					            if (xmlrpc_is_fault($response)) {
 | 
				
			||||||
                common_log(LOG_WARNING,
 | 
					                common_log(LOG_WARNING,
 | 
				
			||||||
@@ -153,8 +229,11 @@ class LinkbackPlugin extends Plugin
 | 
				
			|||||||
    // Largely cadged from trackback_cls.php by
 | 
					    // Largely cadged from trackback_cls.php by
 | 
				
			||||||
    // Ran Aroussi <ran@blogish.org>, GPL2 or any later version
 | 
					    // Ran Aroussi <ran@blogish.org>, GPL2 or any later version
 | 
				
			||||||
    // http://phptrackback.sourceforge.net/
 | 
					    // http://phptrackback.sourceforge.net/
 | 
				
			||||||
    function getTrackback($text, $url)
 | 
					    function getTrackback($result)
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
 | 
					        $text = $result->body;
 | 
				
			||||||
 | 
					        $url = $result->final_url;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        if (preg_match_all('/(<rdf:RDF.*?<\/rdf:RDF>)/sm', $text, $match, PREG_SET_ORDER)) {
 | 
					        if (preg_match_all('/(<rdf:RDF.*?<\/rdf:RDF>)/sm', $text, $match, PREG_SET_ORDER)) {
 | 
				
			||||||
            for ($i = 0; $i < count($match); $i++) {
 | 
					            for ($i = 0; $i < count($match); $i++) {
 | 
				
			||||||
                if (preg_match('|dc:identifier="' . preg_quote($url) . '"|ms', $match[$i][1])) {
 | 
					                if (preg_match('|dc:identifier="' . preg_quote($url) . '"|ms', $match[$i][1])) {
 | 
				
			||||||
@@ -246,4 +325,23 @@ class LinkbackPlugin extends Plugin
 | 
				
			|||||||
                               'or <a href="http://www.movabletype.org/docs/mttrackback.html">Trackback</a> protocols.'));
 | 
					                               'or <a href="http://www.movabletype.org/docs/mttrackback.html">Trackback</a> protocols.'));
 | 
				
			||||||
        return true;
 | 
					        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;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										91
									
								
								plugins/Linkback/actions/linkbacksettings.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										91
									
								
								plugins/Linkback/actions/linkbacksettings.php
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,91 @@
 | 
				
			|||||||
 | 
					<?php
 | 
				
			||||||
 | 
					/**
 | 
				
			||||||
 | 
					 * Settings for Linkback
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 * PHP version 5
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 * LICENCE: This program is free software: you can redistribute it and/or modify
 | 
				
			||||||
 | 
					 * it under the terms of the GNU Affero General Public License as published by
 | 
				
			||||||
 | 
					 * the Free Software Foundation, either version 3 of the License, or
 | 
				
			||||||
 | 
					 * (at your option) any later version.
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 * This program is distributed in the hope that it will be useful,
 | 
				
			||||||
 | 
					 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 | 
				
			||||||
 | 
					 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 | 
				
			||||||
 | 
					 * GNU Affero General Public License for more details.
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 * You should have received a copy of the GNU Affero General Public License
 | 
				
			||||||
 | 
					 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 * @category  Settings
 | 
				
			||||||
 | 
					 * @package   StatusNet
 | 
				
			||||||
 | 
					 * @author    Stephen Paul Weber <singpolyma@singpolyma.net>
 | 
				
			||||||
 | 
					 * @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 <singpolyma@singpolyma.net>
 | 
				
			||||||
 | 
					 * @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.');
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
		Reference in New Issue
	
	Block a user