Extension:AccountManager/Version0.1

From MediaWiki.org
Jump to: navigation, search

[edit] Known Issues

Directly using PHP's header function is certanly not the best way and may, e.g., interfere with other extensions. If you have a better idea, please tell me, thanks!

[edit] Code

<?php
/**
 * NAME
 *
 *      AccountManager
 *
 * SYNOPSIS
 *
 *      implements "HTTP Extensions for Account Management and Session
 *      Identification",
 *      see https://wiki.mozilla.org/Labs/Weave/Identity/Account_Manager
 *
 * INSTALL
 *
 *      Put this file in your MediaWiki extensions directory:
 *
 *          "$IP/extensions/AccountManager.php"
 *
 *      where $IP is the Install Path of your MediaWiki. Then add this line
 *      to LocalSettings.php:
 *
 *          require_once("$IP/extensions/AccountManager.php");
 *
 * DESCRIPTION
 *
 *     This extension allows your browser to log you in and out of MediaWiki
 *     with a simple and uniform interface. For more details, see
 *     https://wiki.mozilla.org/Labs/Weave/Identity/Account_Manager
 *
 * AUTHOR
 *
 *      Klaas Ole Kuertz <klaasole@kuertz.net>
 *
 * @package extensions
 * @version 0.1
 * @copyright Copyright 2010 @author Klaas Ole Kuertz
 * @license GPLv3
 *
 */
 
// block direct access to this script
if (!defined('MEDIAWIKI')) {
echo <<<EOT
To install AccountManager extension, put the following line in
LocalSettings.php: require_once("\$IP/extensions/AccountManager.php");
EOT;
exit(1);
} 
 
// register extension credits
$wgExtensionCredits['other'][] = array(
        'name' => 'AccountManager',
        'version' => 0.1,
        'author' =>'Klaas Ole Kuertz', 
        'url' => 'http://www.mediawiki.org/wiki/Extension:AccountManager', 
        'description' => 'Implements "HTTP Extensions for Account Management
                and Session Identification"'
);
 
// =============================================================================
// add hook to add X-Account-Management headers
// =============================================================================
 
// register hook function
$wgHooks['BeforePageDisplay'][] = 'addAccountManagerHeaders';
 
// add headers before the page is sent out
function addAccountManagerHeaders( &$out, &$sk ) {
        global $wgServer, $wgScriptPath, $wgUser;
 
        // path to AccountManagerControlDocument generated by API, see below
        $amcd = "$wgServer$wgScriptPath/api.php?" . 
                'action=AccountManagerControlDocument&format=json';
        header( "X-Account-Management: $amcd" );
 
        if ( $wgUser->isLoggedIn() ) {
                // get username, filter out special chars
                $name = preg_replace( '/\\W/', '', $wgUser->getName() );
                header( "X-Account-Management-Status: active; name=\"$name\"" );
        } else {
                header( 'X-Account-Management-Status: none' );
        }
 
        return true;
}
 
// =============================================================================
// add API module to generate AccountManagerControlDocument
// =============================================================================
 
// register API module
$wgAPIModules['AccountManagerControlDocument'] =
        'ApiAccountManagerControlDocument';
 
// class to generate AccountManagerControlDocument
class ApiAccountManagerControlDocument extends ApiBase {
 
        public function __construct( $main, $action ) {
                parent :: __construct( $main, $action );
        }
 
        public function execute() {
                global $wgScriptPath;
 
                // JSON paths
                $connect = array( 'methods', 'username-password-form', 'connect' );
                $connectParams = array( 'methods', 'username-password-form',
                        'connect', 'params' );
                $disconnect = array( 'methods', 'username-password-form',
                        'disconnect' );
 
                // connect
                $this->getResult()->addValue( $connect, 'method', 'POST' );
                $this->getResult()->addValue( $connect, 'path',
                        "$wgScriptPath/index.php?title=Special:UserLogin" . 
                        '&action=submitlogin&type=login' );
                $this->getResult()->addValue( $connect, 'onsuccess', 'reload' );
                $this->getResult()->addValue( $connectParams, 'username', 'wpName' );
                $this->getResult()->addValue( $connectParams, 'password', 'wpPassword' );
 
                // disconnect
                $this->getResult()->addValue( $disconnect, 'method', 'POST' );
                $this->getResult()->addValue( $disconnect, 'path',
                        $wgScriptPath.'/index.php?title=Special:UserLogout' );
 
        }
 
        public function getDescription() {
                return array (
                        'Get the AccountManagerControlDocument'
                );
        }
 
        protected function getExamples() {
                return array(
                        'api.php?action=AccountManagerControlDocument&format=json'
                );
        }
        public function getVersion() {
                return '0.1';
        }
}
Personal tools
Namespaces

Variants
Actions
Navigation
Support
Download
Development
Communication
Print/export
Toolbox