Extension:Login At Edit/LoginAtEdit.php
From MediaWiki.org
<?php # Login At Edit Extension for MediaWiki, version 0.1 # Copyright 2006 Evan Miller # This extension integrates a login form into the edit form, # so you can provide your log in credentials at the same time # you submit a page edit. # THIS EXTENSION IS A TOTAL HACK. IT MAY BREAK SOMETHING UNEXPECTED. # DO NOT USE UNLESS YOU ARE WILLING TO CONTRACT DIPHTHERIA. $wgHooks['EditFilter'][] = 'loginAtEdit_CheckCredentials'; $wgHooks['EditPage::showEditForm:initial'][] = 'loginAtEdit_InsertLoginForm'; # This method was ripped off of processLogin of SpecialUserlogin.php, # replacing notices straight to wgOut. $wgExtensionCredits['other'][] = array( 'name' => 'Login At Edit', 'version' => '0.1', 'author' => 'Evan Miller', 'url' => 'http://www.mediawiki.org/wiki/Extension:Login_At_Edit', 'description' => 'Integrates a login form into the edit form', ); function loginAtEdit_CheckCredentials($form, $textbox, $section) { global $wgUser, $wgAuth, $wgReservedUsernames, $wgOut, $wgDisableCookieCheck; if ($wgUser->isLoggedIn()) { return true; } $name = $form->editintro; $pass = $form->textbox2; if ( '' == $name ) { $wgOut->setPageTitle( "Log in problem" ); $wgOut->addWikiText( wfMsg( 'noname' ) ); return false; } $u = User::newFromName( $name ); if( is_null( $u ) || in_array( $u->getName(), $wgReservedUsernames ) ) { $wgOut->setPageTitle( "Log in problem" ); $wgOut->addWikiText( wfMsg( 'noname' ) ); return false; } if ( 0 == $u->getID() ) { /** * If the external authentication plugin allows it, * automatically create a new account for users that * are externally defined but have not yet logged in. */ if ( $wgAuth->autoCreate() && $wgAuth->userExists( $u->getName() ) ) { if ( $wgAuth->authenticate( $u->getName(), $pass ) ) { $u =& $this->initUser( $u ); } else { $wgOut->setPageTitle( "Log in problem" ); $wgOut->addWikiText( wfMsg( 'wrongpassword' ) ); return false; } } else { $wgOut->setPageTitle( "Log in problem" ); $wgOut->addWikiText( wfMsg( 'nosuchuser', $u->getName() ) ); return false; } } else { $u->loadFromDatabase(); } if (!$u->checkPassword( $pass )) { $wgOut->setPageTitle( "Log in problem" ); $wgOut->addWikiText( wfMsg( $pass == '' ? 'wrongpasswordempty' : 'wrongpassword' ) ); return false; } # We've verified now, update the real record # $r = 0; # Maybe we can pack in the "Remember me" too? /* if ( $this->mRemember ) { $r = 1; } else { $r = 0; } */ $u->setOption( 'rememberpassword', $r ); $wgAuth->updateUser( $u ); $wgUser = $u; $wgUser->setCookies(); $wgUser->saveSettings(); if( !$wgDisableCookieCheck && !isset( $_COOKIE[session_name()] ) ) { $wgOut->setPageTitle( "Log in problem" ); $wgOut->addWikiText("'''Please make sure cookies are enabled in your browser'''"); return false; } return true; } function loginAtEdit_InsertLoginForm($form) { global $wgOut, $wgUser; if ($wgUser->isLoggedIn()) { return; } $output = <<<END <script type="$wgJsMimeType"><!-- //<!CDATA[ function insertLoginForm() { var editform = document.getElementById('editform') // We smuggle in user/pass info by calling them // variables that are extracted from the form // but not actually used (as far as I can tell) var username_span = document.createElement('span'); username_span.innerHTML = 'User name:'; var password_span = document.createElement('span'); password_span.innerHTML = ' Password:'; var username = document.createElement('input'); username.setAttribute("name", "editintro"); username.setAttribute("type", "text"); username.setAttribute("tabindex", "2"); var password = document.createElement('input'); password.setAttribute("name", "wpTextbox2"); password.setAttribute("type", "password"); password.setAttribute("tabindex", "3"); br = document.createElement('br'); var bump = [ 'wpSummary', 'wpMinoredit', 'wpSave', 'wpPreview', 'wpDiff' ]; for(var i=0;bump[i];i++) { var elem = document.getElementById(bump[i]); if (elem != null) { elem.setAttribute("tabindex", parseInt(elem.getAttribute("tabindex")) + 2); } } marker = document.getElementById('editpage-copywarn'); editform.insertBefore(br, marker); editform.insertBefore(username_span, marker); editform.insertBefore(username, marker); editform.insertBefore(password_span, marker); editform.insertBefore(password, marker); } function addLoadEvent(func) { var oldonload = window.onload; if (typeof oldonload == 'function') { window.onload = function() { oldonload(); func(); }; } else { window.onload = func; } } addLoadEvent(insertLoginForm); //--> </script> END; $wgOut->addHTML($output); return true; }
