Skip to content

Commit

Permalink
Use MW >= 1.25 token handling
Browse files Browse the repository at this point in the history
Closes #120
  • Loading branch information
reedy committed Oct 12, 2021
1 parent 2cd3f93 commit 70ebdbe
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 28 deletions.
6 changes: 3 additions & 3 deletions Includes/Image.php
Original file line number Diff line number Diff line change
Expand Up @@ -412,7 +412,7 @@ public function revert( $comment = '', $revertto = null ) {
if( !$pgNotag ) $comment .= $pgTag;
$apiArray = array(
'action' => 'filerevert',
'token' => $tokens['edit'],
'token' => $tokens['csrf'],
'comment' => $comment,
'filename' => $this->rawtitle
);
Expand Down Expand Up @@ -460,7 +460,7 @@ public function rotate( $degree = 90 ) {

$apiArray = array(
'action' => 'imagerotate',
'token' => $tokens['edit'],
'token' => $tokens['csrf'],
'titles' => $this->title
);
pecho( "Rotating image(s) $degree degrees...\n\n", PECHO_NOTICE );
Expand Down Expand Up @@ -571,7 +571,7 @@ public function api_upload( $file, $text = '', $comment = '', $watch = null, $ig
'filename' => $this->rawtitle,
'comment' => $comment,
'text' => $text,
'token' => $tokens['edit'],
'token' => $tokens['csrf'],
'ignorewarnings' => intval( $ignorewarnings )
);

Expand Down
10 changes: 5 additions & 5 deletions Includes/Page.php
Original file line number Diff line number Diff line change
Expand Up @@ -1159,7 +1159,7 @@ public function edit(
$summary = substr( $summary . $pgTag, 0, 255 );
}

if( $tokens['edit'] == '' ) {
if( $tokens['csrf'] == '' ) {
pecho( "User is not allowed to edit {$this->title}\n\n", PECHO_FATAL );
return false;
}
Expand All @@ -1172,7 +1172,7 @@ public function edit(
$editarray = array(
'title' => $this->title,
'action' => 'edit',
'token' => $tokens['edit'],
'token' => $tokens['csrf'],
'basetimestamp' => $this->lastedit,
'md5' => md5( $text ),
'text' => $text
Expand Down Expand Up @@ -1354,18 +1354,18 @@ public function undo($summary = null, $revisions = 1, $force = false, $watch = n

$tokens = $this->wiki->get_tokens();

if( $tokens['edit'] == '+\\' ) {
if( $tokens['csrf'] == '+\\' ) {
pecho( "User has logged out.\n\n", PECHO_FATAL );
return false;
} elseif( $tokens['edit'] == '' ) {
} elseif( $tokens['csrf'] == '' ) {
pecho( "User is not allowed to edit {$this->title}\n\n", PECHO_FATAL );
return false;
}

$params = array(
'title' => $this->title,
'action' => 'edit',
'token' => $tokens['edit'],
'token' => $tokens['csrf'],
'basetimestamp' => $this->lastedit,
'undo' => $oldrev,
'undoafter' => $newrev
Expand Down
4 changes: 2 additions & 2 deletions Includes/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -689,8 +689,8 @@ public function email( $text = null, $subject = "Wikipedia Email", $ccme = false
public function userrights( $add = array(), $remove = array(), $reason = '' ) {
global $pgNotag, $pgTag;

$tokens = $this->wiki->get_tokens();
$tokens = $this->wiki->get_tokens();

if( !$pgNotag ) $reason .= $pgTag;
$apiArr = array(
'action' => 'userrights',
Expand Down
18 changes: 4 additions & 14 deletions Includes/Wiki.php
Original file line number Diff line number Diff line change
Expand Up @@ -2314,8 +2314,9 @@ public function get_tokens( $force = false ) {

$tokens = $this->apiQuery(
array(
'action' => 'tokens',
'type' => 'block|delete|deleteglobalaccount|edit|email|import|move|options|patrol|protect|setglobalaccountstatus|unblock|watch'
'action' => 'query',
'meta' => 'tokens',
'type' => 'createaccount|csrf|deleteglobalaccount|login|patrol|rollback|setglobalaccountstatus|userrights|watch'
)
);

Expand All @@ -2325,18 +2326,7 @@ public function get_tokens( $force = false ) {
}
}

$token = $this->apiQuery(
array(
'action' => 'query',
'list' => 'users',
'ususers' => $this->username,
'ustoken' => 'userrights'
)
);

if( isset( $token['query']['users'][0]['userrightstoken'] ) ) {
$this->tokens['userrights'] = $token['query']['users'][0]['userrightstoken'];
} else {
if( !isset( $tokens['userrights'] ) ) {
pecho( "Error retrieving userrights token...\n\n", PECHO_FATAL );
return array();
}
Expand Down
8 changes: 4 additions & 4 deletions Plugins/FlaggedRevs.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public function review( $revid, $reason = null, $status = 1 ) {

$tokens = $this->wiki->get_tokens();

if( $tokens['edit'] == '+\\' ) {
if( $tokens['csrf'] == '+\\' ) {
pecho( "User has logged out.\n\n", PECHO_FATAL );
return false;
}
Expand All @@ -69,7 +69,7 @@ public function review( $revid, $reason = null, $status = 1 ) {
$editarray = array(
'flag_accuracy' => $status,
'action' => 'review',
'token' => $tokens['edit'],
'token' => $tokens['csrf'],
'revid' => $revid
);

Expand Down Expand Up @@ -118,7 +118,7 @@ public function stabilize( $title, $level = 'none', $reason = null, $autoreview

$tokens = $this->wiki->get_tokens();

if( $tokens['edit'] == '+\\' ) {
if( $tokens['csrf'] == '+\\' ) {
pecho( "User has logged out.\n\n", PECHO_FATAL );
return false;
}
Expand All @@ -133,7 +133,7 @@ public function stabilize( $title, $level = 'none', $reason = null, $autoreview
$editarray = array(
'action' => 'review',
'title' => $title,
'token' => $tokens['edit'],
'token' => $tokens['csrf'],
'protectlevel' => $level
);

Expand Down

0 comments on commit 70ebdbe

Please sign in to comment.