-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathSpecialW4GRB_body.php
More file actions
277 lines (252 loc) · 9.58 KB
/
SpecialW4GRB_body.php
File metadata and controls
277 lines (252 loc) · 9.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
<?php
/*********************************************************************
**
** This file is part of the W4G Rating Bar extension for MediaWiki
** Copyright (C)2011
** - David Dernoncourt <www.patheticcockroach.com>
** - Franck Dernoncourt <www.francky.me>
**
** Home Page: http://www.wiki4games.com/Wiki4Games:W4G Rating Bar
**
** This program is licensed under the Creative Commons
** Attribution-ShareAlike 4.0 license
** <https://creativecommons.org/licenses/by-sa/4.0/>
**
** 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.
**
*********************************************************************/
class W4GRB extends UnlistedSpecialPage
{
const ANONYMOUS_UID = 0;
private $bar_id,$page_idnum,$uid;
function __construct()
{
parent::__construct( 'W4GRB');
#$this->includable( false );
}
/**
* That's what is executed when the W4GRB special page is loaded
* (basically, it deals with a vote cast by a user)
* @param $par parser: parser object passed by MediaWiki
* @see SpecialPage::execute()
*/
function execute( $par )
{
global $wgRequest, $wgOut, $wgUser;
global $wgW4GRB_Settings;
$this->setHeaders(); # not sure what's that for
$wgOut->disable(); # for raw output
// header( 'Pragma: nocache' ); # no caching
# Get minimum parameters, then see if a vote was cast or if we just want to read
$this->bar_id = $wgRequest->getInt('bid') or die('No bar specified');
$this->page_idnum = $wgRequest->getInt('pid') or die('No page specified');
$this->uid = $wgUser->getID(); # cache user ID
$vote_sent = $wgRequest->getInt('vote') or die($this->justShowVotes());
# Fights XSS
if(!preg_match("/^https?:\/\/".$_SERVER["HTTP_HOST"]."\//",$_SERVER["HTTP_REFERER"]))
die('XSS attempt detected. Please make sure your browser isn't configured to hide referer information.');
if(wfReadOnly()) die('Wiki in read only mode: voting disabled.'); # if we want to vote (previous line passed) but we're read-only, die now
$ip = $_SERVER['REMOTE_ADDR']; # cache IP
# Restrict vote to legit values
if ($vote_sent > 100) $vote_sent=100;
else if ($vote_sent < 0) $vote_sent=0;
$dbslave = wfGetDB( DB_REPLICA );
# Check if the page ID is valid
$result = $dbslave->select('page', 'page_id,page_title',
array('page_id' => $this->page_idnum),
__METHOD__ ,
array('LIMIT' => 1));
if(!($row = $dbslave->fetchObject($result))) die('No page with this ID');
$dbslave->freeResult($result);
# If not logged in (and anonymous voting not enabled)
# Or if not allowed to vote (NB: anonymous voting overrides this!)
if( ($this->uid==self::ANONYMOUS_UID || !$wgUser->isAllowed('w4g_rb-canvote'))
&& !$wgW4GRB_Settings['anonymous-voting-enabled'])
die($this->justShowVotes());
$dbmaster = wfGetDB( DB_MASTER );
// $dbmaster->ignoreErrors('off'); # we need this so that failed queries return false
# Checks in the master if the user has already voted
# (But don't do the search if casting an anonymous vote)
$user_already_voted=false;
if($this->uid!=self::ANONYMOUS_UID)
{
$result = $dbmaster->select('w4grb_votes', 'vote',
array( 'uid' => $this->uid,
'pid' => $this->page_idnum),
__METHOD__ ,
array('LIMIT' => 1));
if($row = $dbmaster->fetchObject($result)) $user_already_voted=true;
$dbmaster->freeResult($result);
}
# Checks in the master if the IP has already voted and:
# - grab all matching uids
# - get most recent time this IP voted
$result = $dbmaster->select('w4grb_votes',
array( 'uid','time'),
array( 'ip' => $ip,
'pid' => $this->page_idnum),
__METHOD__ ,
array('ORDER BY' => 'time DESC'));
if($row = $dbmaster->fetchObject($result))
{
$ip_already_voted=true;
$most_recent_vote_from_ip=$row->time;
$uids_who_voted=array();
do
{
$uids_who_voted[]=$row->uid;
} while($row = $dbmaster->fetchObject($result));
$dbmaster->freeResult($result);
}
else $ip_already_voted=false;
# If the same user already voted, we update their vote and it's over
if($user_already_voted)
{
$dbmaster->update('w4grb_votes',
array( 'vote' => $vote_sent,
'ip' => $ip,
'time' => time()),
array( 'uid' => $this->uid,
'pid' => $this->page_idnum),
__METHOD__ ) or die('Query vote failed!');
$vote_cast=true;
}
# Else if the same IP already voted anonymously, we update that vote
else if($ip_already_voted && in_array(self::ANONYMOUS_UID,$uids_who_voted))
{
# If current vote is still anonymous, just update it
# Otherwise, logged-in user will steal the anonymous vote
# (design: anonymous vote is impossible if an identified user
# already used the IP to vote)
$dbmaster->update('w4grb_votes',
array( 'vote' => $vote_sent,
'uid' => $this->uid,
'ip' => $ip,
'time' => time()),
array( 'uid' => self::ANONYMOUS_UID,
'pid' => $this->page_idnum),
__METHOD__ ) or die('Query vote failed!');
}
# Now this case is if the same IP voted as ANOTHER logged-in user:
else if($ip_already_voted)
{
# 1. Maybe this guy's trying to multivote! Let's check that with timestamps
if(time()-$most_recent_vote_from_ip < $wgW4GRB_Settings['multivote-cooldown'])
die (wfMessage('w4g_rb-error_ip_overused'));
# 2. Maybe an anonymous is trying to vote from the same IP as a registered
# user => this is not allowed
else if($this->uid==self::ANONYMOUS_UID)
die (wfMessage('w4g_rb-error_ip_used_by_registered'));
}
# If we arrive there, the vote is new and clean (no vote from the
# same user at all, and not from same IP recently)
# so we can insert a new line
else
{
$dbmaster->insert('w4grb_votes',
array( 'uid' => $this->uid,
'pid' => $this->page_idnum,
'vote' => $vote_sent,
'ip' => $ip,
'time' => time()),
__METHOD__ ) or die('Query vote failed!');
}
# Calculate the average rating and number of votes
$result=$dbmaster->select('w4grb_votes', 'AVG(vote) AS avg, COUNT(vote) AS n',
array('pid' => $this->page_idnum),
__METHOD__);
if($row = $dbslave->fetchObject($result))
{
$average_rating = intval($row->avg);
$num_votes = intval($row->n);
}
$dbmaster->freeResult($result);
# Place those in the average rating table (same procedure as votecasting: first check in slave, then try in master)
$result = $dbslave->select('w4grb_avg', 'avg',
array('pid' => $this->page_idnum),
__METHOD__ ,
array('LIMIT' => 1));
if($row = $dbslave->fetchObject($result)) $already_averaged=true;
else $already_averaged=false;
$dbslave->freeResult($result);
if ($already_averaged || !$dbmaster->insert('w4grb_avg',
array( 'pid' => $this->page_idnum,
'avg' => $average_rating,
'n' => $num_votes),
__METHOD__ ))
{
if (!$dbmaster->update('w4grb_avg',
array( 'avg' => $average_rating,
'n' => $num_votes),
array( 'pid' => $this->page_idnum),
__METHOD__ ))
echo 'Query avg failed!';
}
# Display what we did
echo wfMessage('w4g_rb-current_user_rating','<b>'.$average_rating.'</b>',$num_votes).'<br/>';
echo wfMessage('w4g_rb-you_voted').' <b><span id="w4g_rb_rating_value-'.$this->bar_id.'">'.$vote_sent.'</span>%</b>';
}
/**
* Displays current vote (by the user and on average)
*/
private function justShowVotes()
{
global $wgUser;
global $wgW4GRB_Settings;
$out = '';
$dbslave = wfGetDB( DB_REPLICA );
# Gets average rating
$result = $dbslave->select('w4grb_avg', 'avg,n',
array('pid' => $this->page_idnum),
__METHOD__ ,
array('LIMIT' => 1));
if($row = $dbslave->fetchObject($result))
{
$average_rating = intval($row->avg);
$num_votes = intval($row->n);
$out .= wfMessage('w4g_rb-current_user_rating','<b>'.$average_rating.'</b>',$num_votes).'<br/>';
}
else $out .= wfMessage('w4g_rb-nobody_voted').'.<br/>';
$dbslave->freeResult($result);
# If not logged in and anonymous voting not enabled
if($this->uid==self::ANONYMOUS_UID && !$wgW4GRB_Settings['anonymous-voting-enabled'])
{
$loginLink = Linker::link( SpecialPage::getTitleFor( 'Userlogin' ), wfMessage('w4g_rb-log_in'), array(), array());
$signupLink = Linker::link( SpecialPage::getTitleFor( 'Userlogin' ), wfMessage('w4g_rb-register'), array(), array('type'=>'signup'));
$out .= wfMessage('w4g_rb-error_must_login',$loginLink,$signupLink)->plain();
}
# Else if not allowed to vote (NB: anonymous voting overrides this!)
else if (!$wgUser->isAllowed('w4g_rb-canvote') && !$wgW4GRB_Settings['anonymous-voting-enabled'])
{
$rightsLink = Linker::link( SpecialPage::getTitleFor( 'Listgrouprights' ), wfMessage('w4g_rb-voting_rights'), array(), array());
$out .= wfMessage('w4g_rb-error_no_canvote',$rightsLink);
}
# If the user can vote, get their vote (if anonymous, get the vote from their IP)
else
{
if($this->uid==self::ANONYMOUS_UID)
$result = $dbslave->select('w4grb_votes', 'vote',
array( 'pid' => $this->page_idnum,
'uid' => self::ANONYMOUS_UID,
'ip' => $_SERVER['REMOTE_ADDR']),
__METHOD__ ,
array('LIMIT' => 1));
else $result = $dbslave->select('w4grb_votes', 'vote',
array( 'pid' => $this->page_idnum,
'uid' => $this->uid),
__METHOD__ ,
array('LIMIT' => 1));
if($row = $dbslave->fetchObject($result))
{
$existing_vote = intval($row->vote);
$out .= wfMessage('w4g_rb-you_voted').' <b><span id="w4g_rb_rating_value-'.$this->bar_id.'">'.$existing_vote.'</span>%</b>';
}
else $out .= wfMessage('w4g_rb-you_didnt_vote').'.<br/>';
$dbslave->freeResult($result);
}
# Display what we did
return $out;
}
}