Index: trunk/extensions/Sudo/Sudo.php |
— | — | @@ -1,62 +1,76 @@ |
2 | 2 | <?php |
3 | 3 | /** |
4 | 4 | * Sudo |
5 | | - * @package Sudo |
| 5 | + * |
| 6 | + * @file |
| 7 | + * @ingroup Extensions |
| 8 | + * @version 0.2 |
6 | 9 | * @author Daniel Friesen (http://mediawiki.org/wiki/User:Dantman) <mediawiki@danielfriesen.name> |
7 | 10 | * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License 2.0 or later |
8 | | - * |
| 11 | + * @link http://www.mediawiki.org/wiki/Extension:Sudo Documentation |
| 12 | + * |
9 | 13 | * This program is free software; you can redistribute it and/or |
10 | 14 | * modify it under the terms of the GNU General Public License |
11 | 15 | * as published by the Free Software Foundation; either version 2 |
12 | 16 | * of the License, or (at your option) any later version. |
13 | | - * |
| 17 | + * |
14 | 18 | * This program is distributed in the hope that it will be useful, |
15 | 19 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | 20 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
17 | 21 | * GNU General Public License for more details. |
18 | | - * |
| 22 | + * |
19 | 23 | * You should have received a copy of the GNU General Public License |
20 | 24 | * along with this program; if not, write to the Free Software |
21 | 25 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
22 | 26 | */ |
23 | 27 | |
24 | | -if( !defined( 'MEDIAWIKI' ) ) die( "This is an extension to the MediaWiki package and cannot be run standalone." ); |
| 28 | +if( !defined( 'MEDIAWIKI' ) ) { |
| 29 | + die( 'This is an extension to the MediaWiki package and cannot be run standalone.' ); |
| 30 | +} |
25 | 31 | |
26 | | -$wgExtensionCredits['other'][] = array ( |
| 32 | +// Extension credits that will show up on Special:Version |
| 33 | +$wgExtensionCredits['other'][] = array( |
27 | 34 | 'name' => 'Sudo', |
28 | | - 'url' => 'http://mediawiki.org/wiki/Extension:Sudo', |
29 | 35 | 'version' => '0.2', |
30 | | - 'author' => "[http://mediawiki.org/wiki/User:Dantman Daniel Friesen] [mailto:Daniel%20Friesen%20%3Cmediawiki@danielfriesen.name%3E <mediawiki@danielfriesen.name>]", |
31 | | - 'description' => "Allows sudoers to login as other users." |
| 36 | + 'author' => '[http://www.mediawiki.org/wiki/User:Dantman Daniel Friesen] [mailto:Daniel%20Friesen%20%3Cmediawiki@danielfriesen.name%3E <mediawiki@danielfriesen.name>]', |
| 37 | + 'description' => 'Allows sudoers to login as other users.', |
| 38 | + 'url' => 'http://www.mediawiki.org/wiki/Extension:Sudo', |
32 | 39 | ); |
33 | 40 | |
34 | | -$wgAvailableRights[] = 'sudo'; |
35 | | -$wgExtensionMessagesFiles['Sudo'] = dirname(__FILE__).'/Sudo.i18n.php'; |
36 | | -$wgAutoloadClasses['SpecialSudo'] = dirname(__FILE__).'/SpecialSudo.php'; |
| 41 | +// Set up i18n and the new special page |
| 42 | +$dir = dirname( __FILE__ ) . '/'; |
| 43 | +$wgExtensionMessagesFiles['Sudo'] = $dir . 'Sudo.i18n.php'; |
| 44 | +$wgAutoloadClasses['SpecialSudo'] = $dir . 'SpecialSudo.php'; |
37 | 45 | $wgSpecialPages['Sudo'] = 'SpecialSudo'; |
38 | 46 | $wgSpecialPageGroups['Sudo'] = 'users'; |
39 | 47 | |
| 48 | +// New user right, required to use Special:Sudo |
| 49 | +$wgAvailableRights[] = 'sudo'; |
| 50 | + |
| 51 | +// New log type, all sudo actions are logged to this log (Special:Log/sudo) |
40 | 52 | $wgLogTypes[] = 'sudo'; |
41 | 53 | $wgLogNames['sudo'] = 'sudo-logpagename'; |
42 | 54 | $wgLogHeaders['sudo'] = 'sudo-logpagetext'; |
43 | 55 | $wgLogActions['sudo/sudo'] = 'sudo-logentry'; |
44 | 56 | |
45 | | -$wgHooks['UserLogoutComplete'][] = 'efSudoLogout'; |
46 | | -$wgHooks['PersonalUrls'][] = 'efSudoPersonalUrls'; |
| 57 | +// Hooked functions |
| 58 | +$wgHooks['UserLogoutComplete'][] = 'wfSudoLogout'; |
| 59 | +$wgHooks['PersonalUrls'][] = 'wfSudoPersonalUrls'; |
47 | 60 | |
48 | | -function efSudoLogout( &$user, &$inject_html ) { |
49 | | - ## Unset wsSudoId when we logout. We don't want to be in a sudo login while logged out. |
50 | | - unset($_SESSION['wsSudoId']); |
| 61 | +function wfSudoLogout( &$user, &$inject_html ) { |
| 62 | + // Unset wsSudoId when we logout. |
| 63 | + // We don't want to be in a sudo login while logged out. |
| 64 | + unset( $_SESSION['wsSudoId'] ); |
51 | 65 | return true; |
52 | 66 | } |
53 | 67 | |
54 | | -function efSudoPersonalUrls( &$personal_urls, &$wgTitle ) { |
55 | | - ## Replace logout link with a unsudo link while in a sudo login. |
56 | | - if( isset($_SESSION['wsSudoId']) && $_SESSION['wsSudoId'] > 0 ) { |
| 68 | +function wfSudoPersonalUrls( &$personal_urls, &$wgTitle ) { |
| 69 | + // Replace logout link with a unsudo link while in a sudo login. |
| 70 | + if( isset( $_SESSION['wsSudoId'] ) && $_SESSION['wsSudoId'] > 0 ) { |
57 | 71 | $personal_urls['logout'] = array( |
58 | | - 'text' => wfMsg( 'sudo-personal-unsudo' ), |
59 | | - 'href' => Skin::makeSpecialUrl( 'Sudo', 'mode=unsudo' ), |
60 | | - 'active' => false |
| 72 | + 'text' => wfMsg( 'sudo-personal-unsudo' ), |
| 73 | + 'href' => Skin::makeSpecialUrl( 'Sudo', 'mode=unsudo' ), |
| 74 | + 'active' => false |
61 | 75 | ); |
62 | 76 | } |
63 | 77 | return true; |
Index: trunk/extensions/Sudo/SpecialSudo.php |
— | — | @@ -1,115 +1,140 @@ |
2 | 2 | <?php |
3 | 3 | /** |
4 | 4 | * Sudo |
5 | | - * @package Sudo |
| 5 | + * |
| 6 | + * @file |
| 7 | + * @ingroup Extensions |
6 | 8 | * @author Daniel Friesen (http://mediawiki.org/wiki/User:Dantman) <mediawiki@danielfriesen.name> |
7 | 9 | * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License 2.0 or later |
8 | | - * |
| 10 | + * |
9 | 11 | * This program is free software; you can redistribute it and/or |
10 | 12 | * modify it under the terms of the GNU General Public License |
11 | 13 | * as published by the Free Software Foundation; either version 2 |
12 | 14 | * of the License, or (at your option) any later version. |
13 | | - * |
| 15 | + * |
14 | 16 | * This program is distributed in the hope that it will be useful, |
15 | 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
17 | 19 | * GNU General Public License for more details. |
18 | | - * |
| 20 | + * |
19 | 21 | * You should have received a copy of the GNU General Public License |
20 | 22 | * along with this program; if not, write to the Free Software |
21 | 23 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
22 | 24 | */ |
23 | 25 | |
24 | | -if( !defined( 'MEDIAWIKI' ) ) die( "This is an extension to the MediaWiki package and cannot be run standalone." ); |
| 26 | +if( !defined( 'MEDIAWIKI' ) ) { |
| 27 | + die( 'This is an extension to the MediaWiki package and cannot be run standalone.' ); |
| 28 | +} |
25 | 29 | |
26 | 30 | class SpecialSudo extends SpecialPage { |
27 | | - protected $mMode, $mSkin, $mTarget, $mReason, $mErrors; |
28 | | - |
29 | | - function __construct() { |
| 31 | + protected $mode, $skin, $target, $reason, $errors; |
| 32 | + |
| 33 | + /** |
| 34 | + * Constructor -- set up the new special page |
| 35 | + */ |
| 36 | + public function __construct() { |
30 | 37 | parent::__construct( 'Sudo', 'sudo' ); |
31 | | - wfLoadExtensionMessages('Sudo'); |
| 38 | + wfLoadExtensionMessages( 'Sudo' ); |
32 | 39 | } |
33 | | - |
| 40 | + |
34 | 41 | /** |
35 | | - * Execute |
| 42 | + * Show the special page |
| 43 | + * |
| 44 | + * @param $par String: name of the user to sudo into |
36 | 45 | */ |
37 | | - function execute( $par = '' ) { |
| 46 | + public function execute( $par ) { |
38 | 47 | global $wgOut, $wgRequest, $wgUser; |
39 | | - |
40 | | - $this->mMode = $wgRequest->getText('mode'); |
41 | | - if( $this->mMode == 'success' ) return $this->showSuccessPage(); |
42 | | - if( $this->mMode == 'unsudo' ) return $this->showUnsudoPage(); |
43 | | - |
| 48 | + |
| 49 | + $this->mode = $wgRequest->getText( 'mode' ); |
| 50 | + if( $this->mode == 'success' ) { |
| 51 | + return $this->showSuccessPage(); |
| 52 | + } |
| 53 | + if( $this->mode == 'unsudo' ) { |
| 54 | + return $this->showUnsudoPage(); |
| 55 | + } |
| 56 | + |
| 57 | + // Check that the user is allowed to access this special page... |
44 | 58 | if( !$wgUser->isAllowed( 'sudo' ) ) { |
45 | 59 | $wgOut->permissionRequired( 'sudo' ); |
46 | 60 | return; |
47 | 61 | } |
| 62 | + |
| 63 | + // ...and that the user isn't blocked |
48 | 64 | if( $wgUser->isBlocked() ) { |
49 | 65 | $wgOut->blockedPage(); |
50 | 66 | return; |
51 | 67 | } |
| 68 | + |
| 69 | + // ...and that the database is not in read-only mode. |
52 | 70 | if( wfReadOnly() ) { |
53 | 71 | $wgOut->readOnlyPage(); |
54 | 72 | return; |
55 | 73 | } |
56 | | - |
| 74 | + |
| 75 | + // Set page title and other stuff |
57 | 76 | $this->setHeaders(); |
58 | | - |
59 | | - $this->mSkin = $wgUser->getSkin(); |
60 | | - $this->mTarget = $wgRequest->getText('target', $par); |
61 | | - $this->mReason = $wgRequest->getText('reason', ''); |
62 | | - $this->mErrors = array(); |
63 | | - |
| 77 | + |
| 78 | + $this->skin = $wgUser->getSkin(); |
| 79 | + $this->target = $wgRequest->getText( 'target', $par ); |
| 80 | + $this->reason = $wgRequest->getText( 'reason', '' ); |
| 81 | + $this->errors = array(); |
| 82 | + |
64 | 83 | $this->showSudoForm(); |
65 | | - if( $this->mTarget != '' && $wgRequest->wasPosted() ) $this->doSudo(); |
| 84 | + if( $this->target != '' && $wgRequest->wasPosted() ) { |
| 85 | + $this->doSudo(); |
| 86 | + } |
66 | 87 | $this->showErrors(); |
67 | 88 | } |
68 | | - |
| 89 | + |
69 | 90 | function showSuccessPage() { |
70 | 91 | global $wgOut, $wgUser; |
71 | | - if( !isset($_SESSION['wsSudoId']) || $_SESSION['wsSudoId'] < 0 ) { |
72 | | - $this->showError('sudo-error-nosudo'); |
| 92 | + if( !isset( $_SESSION['wsSudoId'] ) || $_SESSION['wsSudoId'] < 0 ) { |
| 93 | + $this->showError( 'sudo-error-nosudo' ); |
73 | 94 | } else { |
74 | 95 | $this->setHeaders(); |
75 | 96 | $s = $wgUser->getSkin(); |
76 | | - $suUser = User::newFromId($_SESSION['wsSudoId']); |
| 97 | + $suUser = User::newFromId( $_SESSION['wsSudoId'] ); |
77 | 98 | $wgOut->addWikiMsgArray( 'sudo-success', array( |
78 | 99 | $s->makeLinkObj( $suUser->getUserPage(), $suUser->getName() ), |
79 | 100 | $s->makeLinkObj( $wgUser->getUserPage(), $wgUser->getName() ) ), |
80 | | - array('replaceafter') ); |
| 101 | + array( 'replaceafter' ) |
| 102 | + ); |
81 | 103 | } |
82 | 104 | } |
83 | | - |
| 105 | + |
84 | 106 | function showUnsudoPage() { |
85 | 107 | global $wgOut, $wgUser, $wgRequest; |
86 | | - if( !isset($_SESSION['wsSudoId']) || $_SESSION['wsSudoId'] < 0 ) { |
87 | | - $this->showError('sudo-error-nosudo'); |
| 108 | + if( !isset( $_SESSION['wsSudoId'] ) || $_SESSION['wsSudoId'] < 0 ) { |
| 109 | + $this->showError( 'sudo-error-nosudo' ); |
88 | 110 | } else { |
89 | | - $suUser = User::newFromId($_SESSION['wsSudoId']); |
| 111 | + $suUser = User::newFromId( $_SESSION['wsSudoId'] ); |
90 | 112 | if( $wgRequest->wasPosted() ) { |
91 | | - unset($_SESSION['wsSudoId']); |
| 113 | + unset( $_SESSION['wsSudoId'] ); |
92 | 114 | $suUser->setCookies(); |
93 | | - $wgOut->redirect($this->getTitle()->getFullURL()); |
| 115 | + $wgOut->redirect( $this->getTitle()->getFullURL() ); |
94 | 116 | return; |
95 | 117 | } |
96 | 118 | $this->setHeaders(); |
97 | | - $wgOut->setPageTitle(wfMsg('unsudo')); |
| 119 | + $wgOut->setPageTitle( wfMsg( 'unsudo' ) ); |
98 | 120 | $s = $wgUser->getSkin(); |
99 | | - |
| 121 | + |
100 | 122 | $wgOut->addHTML( |
101 | | - Xml::openElement('form', array( 'method' => 'post', |
102 | | - 'action' => $this->getTitle()->getFullURL('mode=unsudo') )). |
103 | | - Xml::hidden('title', $this->getTitle()->getPrefixedText()) ); |
| 123 | + Xml::openElement( 'form', array( 'method' => 'post', |
| 124 | + 'action' => $this->getTitle()->getFullURL( 'mode=unsudo' ) ) ) . |
| 125 | + Xml::hidden( 'title', $this->getTitle()->getPrefixedText() ) |
| 126 | + ); |
104 | 127 | $wgOut->addWikiMsgArray( 'sudo-unsudo', array( |
105 | 128 | $s->makeLinkObj( $suUser->getUserPage(), $suUser->getName() ), |
106 | 129 | $s->makeLinkObj( $wgUser->getUserPage(), $wgUser->getName() ) ), |
107 | | - array('replaceafter') ); |
| 130 | + array( 'replaceafter' ) |
| 131 | + ); |
108 | 132 | $wgOut->addHTML( |
109 | | - Xml::submitButton(wfMsg('sudo-unsudo-submit')). |
110 | | - Xml::closeElement('form')); |
| 133 | + Xml::submitButton( wfMsg( 'sudo-unsudo-submit' ) ) . |
| 134 | + Xml::closeElement( 'form' ) |
| 135 | + ); |
111 | 136 | } |
112 | 137 | } |
113 | | - |
| 138 | + |
114 | 139 | function showSudoForm() { |
115 | 140 | global $wgOut; |
116 | 141 | $wgOut->addHTML( |
— | — | @@ -118,49 +143,59 @@ |
119 | 144 | Xml::hidden( 'title', $this->getTitle()->getPrefixedText() ) . |
120 | 145 | Xml::openElement( 'fieldset' ) . |
121 | 146 | Xml::element( 'legend', array(), wfMsg( 'sudo-form' ) ) . |
122 | | - Xml::inputLabel( wfMsg( 'sudo-user' ), 'target', 'sudo-user', 20, $this->mTarget ) . ' ' . |
123 | | - Xml::inputLabel( wfMsg( 'sudo-reason' ), 'reason', 'sudo-reason', 45, $this->mReason ) . ' ' . |
| 147 | + Xml::inputLabel( wfMsg( 'sudo-user' ), 'target', 'sudo-user', 20, $this->target ) . ' ' . |
| 148 | + Xml::inputLabel( wfMsg( 'sudo-reason' ), 'reason', 'sudo-reason', 45, $this->reason ) . ' ' . |
124 | 149 | Xml::submitButton( wfMsg( 'sudo-submit' ) ) . |
125 | 150 | Xml::closeElement( 'fieldset' ) . |
126 | 151 | Xml::closeElement( 'form' ) . "\n" |
127 | 152 | ); |
128 | 153 | } |
129 | | - |
| 154 | + |
130 | 155 | function addError( $error = '' ) { |
131 | | - $this->mErrors[] = $error; |
| 156 | + $this->errors[] = $error; |
132 | 157 | return; |
133 | 158 | } |
134 | | - |
| 159 | + |
135 | 160 | function showError( $error ) { |
136 | 161 | global $wgOut; |
137 | | - $wgOut->addHTML( Xml::openElement('div', array( 'class' => 'sudo-error' )) ); |
138 | | - $wgOut->addWikiText( wfMsg('sudo-error',wfMsg($error)) ); |
139 | | - $wgOut->addHTML( Xml::closeElement('div') ); |
| 162 | + $wgOut->addHTML( Xml::openElement( 'div', array( 'class' => 'sudo-error' ) ) ); |
| 163 | + $wgOut->addWikiMsg( 'sudo-error', wfMsg( $error ) ); |
| 164 | + $wgOut->addHTML( Xml::closeElement( 'div' ) ); |
140 | 165 | } |
141 | | - |
| 166 | + |
142 | 167 | function showErrors() { |
143 | | - global $wgOut; |
144 | | - foreach( $this->mErrors as $error ) { |
| 168 | + foreach( $this->errors as $error ) { |
145 | 169 | $this->showError( $error ); |
146 | 170 | } |
147 | 171 | } |
148 | | - |
| 172 | + |
149 | 173 | function doSudo() { |
150 | 174 | global $wgOut, $wgUser; |
151 | | - $u = User::newFromName( $this->mTarget ); |
152 | | - if( is_null($u) ) return $this->addError('sudo-error-sudo-invaliduser'); |
153 | | - if( User::isIP($u->getName()) ) return $this->addError('sudo-error-ip'); |
154 | | - if( $u->isAnon() ) return $this->addError('sudo-error-sudo-nonexistant'); |
155 | | - if( $u->getName() === $wgUser->getName() ) return $this->addError('sudo-error-sudo-self'); |
156 | | - |
| 175 | + |
| 176 | + $u = User::newFromName( $this->target ); |
| 177 | + if( is_null( $u ) ) { |
| 178 | + return $this->addError( 'sudo-error-sudo-invaliduser' ); |
| 179 | + } |
| 180 | + if( User::isIP( $u->getName() ) ) { |
| 181 | + return $this->addError( 'sudo-error-ip' ); |
| 182 | + } |
| 183 | + if( $u->isAnon() ) { |
| 184 | + return $this->addError( 'sudo-error-sudo-nonexistent' ); |
| 185 | + } |
| 186 | + if( $u->getName() === $wgUser->getName() ) { |
| 187 | + return $this->addError( 'sudo-error-sudo-self' ); |
| 188 | + } |
| 189 | + |
157 | 190 | $s = $wgUser->getSkin(); |
158 | 191 | $log = new LogPage( 'sudo' ); |
159 | | - $log->addEntry( 'sudo', $wgUser->getUserPage(), $this->mReason, |
| 192 | + $log->addEntry( 'sudo', $wgUser->getUserPage(), $this->reason, |
160 | 193 | array( $s->makeLinkObj( $u->getUserPage(), $u->getName() ) ) ); |
161 | | - |
162 | | - if( !isset($_SESSION['wsSudoId']) || $_SESSION['wsSudoId'] < 0 ) $_SESSION['wsSudoId'] = $wgUser->getId(); |
| 194 | + |
| 195 | + if( !isset( $_SESSION['wsSudoId'] ) || $_SESSION['wsSudoId'] < 0 ) { |
| 196 | + $_SESSION['wsSudoId'] = $wgUser->getId(); |
| 197 | + } |
163 | 198 | $u->setCookies(); |
164 | | - |
| 199 | + |
165 | 200 | $wgOut->redirect( $this->getTitle()->getFullURL( 'mode=success' ) ); |
166 | 201 | } |
167 | 202 | } |
Index: trunk/extensions/Sudo/Sudo.i18n.php |
— | — | @@ -1,27 +1,27 @@ |
2 | 2 | <?php |
3 | 3 | /** |
4 | 4 | * Sudo |
5 | | - * @package Sudo |
| 5 | + * |
| 6 | + * @file |
| 7 | + * @ingroup Extensions |
6 | 8 | * @author Daniel Friesen (http://mediawiki.org/wiki/User:Dantman) <mediawiki@danielfriesen.name> |
7 | 9 | * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License 2.0 or later |
8 | | - * |
| 10 | + * |
9 | 11 | * This program is free software; you can redistribute it and/or |
10 | 12 | * modify it under the terms of the GNU General Public License |
11 | 13 | * as published by the Free Software Foundation; either version 2 |
12 | 14 | * of the License, or (at your option) any later version. |
13 | | - * |
| 15 | + * |
14 | 16 | * This program is distributed in the hope that it will be useful, |
15 | 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
17 | 19 | * GNU General Public License for more details. |
18 | | - * |
| 20 | + * |
19 | 21 | * You should have received a copy of the GNU General Public License |
20 | 22 | * along with this program; if not, write to the Free Software |
21 | 23 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
22 | 24 | */ |
23 | 25 | |
24 | | -if( !defined( 'MEDIAWIKI' ) ) die( "This is an extension to the MediaWiki package and cannot be run standalone." ); |
25 | | - |
26 | 26 | $messages = array(); |
27 | 27 | |
28 | 28 | /** English |
— | — | @@ -38,10 +38,10 @@ |
39 | 39 | 'sudo-unsudo' => 'Welcome $1, you are currently logged into the wiki as $2, just submit the form to return to your normal login.', |
40 | 40 | 'sudo-unsudo-submit' => 'Return', |
41 | 41 | 'sudo-success' => 'Welcome $1, you are now logged into the wiki as $2.', |
42 | | - 'sudo-error' => 'Sudo Error: $1', |
43 | | - 'sudo-error-sudo-invaliduser' => 'Invalid Username', |
| 42 | + 'sudo-error' => 'Sudo error: $1', |
| 43 | + 'sudo-error-sudo-invaliduser' => 'Invalid username', |
44 | 44 | 'sudo-error-sudo-ip' => 'Cannot login to an IP', |
45 | | - 'sudo-error-sudo-nonexistant' => 'That user does not exist', |
| 45 | + 'sudo-error-sudo-nonexistent' => 'That user does not exist', |
46 | 46 | 'sudo-error-sudo-self' => 'Cannot sudo into yourself', |
47 | 47 | 'sudo-error-nosudo' => 'You do not appear to be inside of a sudo login', |
48 | 48 | 'sudo-logpagename' => 'Sudo log', |