aboutsummaryrefslogtreecommitdiffstats
path: root/extensions/BoardVote.php
blob: 4659ddb2d9c3f2d09c8bcb588d7552eb1a317c04 (plain) (blame)
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
<?php

# Wikimedia Foundation Board of Trustees Election

# Register extension
$wgExtensionFunctions[] = "wfBoardvoteSetup";


function wfBoardvoteSetup()
{
# Look out, freaky indenting
# All this happens after SpecialPage.php has been included in Setup.php

class BoardVotePage extends SpecialPage {
	var $mPosted, $mContributing, $mVolunteer, $mDBname, $mUserDays, $mUserEdits;
	var $mHasVoted, $mAction, $mUserKey;

	function BoardVotePage() {
		SpecialPage::SpecialPage( "Boardvote" );
	}

	function execute( $par ) {
		global $wgUser, $wgDBname, $wgInputEncoding, $wgRequest, $wgBoardVoteDB;

		$this->mUserKey = iconv( $wgInputEncoding, "UTF-8", $wgUser->getName() ) . "@$wgDBname";
		$this->mPosted = $wgRequest->wasPosted();
		$this->mContributing = $wgRequest->getInt( "contributing" );
		$this->mVolunteer = $wgRequest->getInt( "volunteer" );
		$this->mDBname = $wgBoardVoteDB;
		$this->mHasVoted = $this->hasVoted( $wgUser );
		
		if ( $par ) {
			$this->mAction = $par;
		} else {
			$this->mAction = $wgRequest->getText( "action" );
		}

		$this->setHeaders();

		if ( $this->mAction == "list" ) {
			$this->displayList();
		} elseif ( $this->mAction == "dump" ) {
			$this->dump();
		} elseif( $this->mAction == "vote" ) {
			if ( !$wgUser->getID() ) {
				$this->notLoggedIn();
			} else {
				$this->getQualifications( $wgUser );
				if ( $this->mUserDays < 90 ) {
					$this->notQualified();
				} elseif ( $this->mPosted ) {
					$this->logVote();
				} else {
					$this->displayVote();
				}
			}
		} else {
			$this->displayEntry();
		}
	}
	
	function displayEntry() {
		global $wgOut;
		$wgOut->addWikiText( wfMsg( "boardvote_entry" ) );
	}

	function hasVoted( &$user ) {
		global $wgDBname;
		$row = wfGetArray( $this->mDBname . ".log", array( "1" ), 
		  array( "log_user_key" => $this->mUserKey ), "BoardVotePage::getUserVote" );
		if ( $row === false ) {
			return false;
		} else {
			return true;
		}
	}

	function logVote() {
		global $wgUser, $wgDBname, $wgIP, $wgOut;
		$fname = "BoardVotePage::logVote";
		
		$now = wfTimestampNow();
		$record = $this->encrypt( $this->mContributing, $this->mVolunteer );
		$db = $this->mDBname;
		
		# Mark previous votes as old
		$encKey = wfStrencode( $this->mUserKey );
		$sql = "UPDATE $db.log SET log_current=0 WHERE log_user_key='$encKey'";
		wfQuery( $sql, DB_WRITE, $fname );
		
		# Add vote to log
		wfInsertArray( "$db.log", array(
			"log_user" => $wgUser->getID(),
			"log_user_text" => $wgUser->getName(),
			"log_user_key" => $this->mUserKey,
			"log_wiki" => $wgDBname,
			"log_edits" => $this->mUserEdits,
			"log_days" => $this->mUserDays,
			"log_record" => $record,
			"log_ip" => $wgIP,
			"log_xff" => @$_SERVER['HTTP_X_FORWARDED_FOR'],
			"log_ua" => $_SERVER['HTTP_USER_AGENT'],
			"log_timestamp" => $now,
			"log_current" => 1
		), $fname );

		$wgOut->addWikiText( wfMsg( "boardvote_entered", $record ) );
	}
	
	function displayVote() {
		global $wgContributingCandidates, $wgVolunteerCandidates, $wgOut;
		
		$thisTitle = Title::makeTitle( NS_SPECIAL, "Boardvote" );
		$action = $thisTitle->getLocalURL( "action=vote" );
		if ( $this->mHasVoted ) {
			$intro = wfMsg( "boardvote_intro_change" );
		} else {
			$intro = wfMsg( "boardvote_intro" );
		}
		$contributing = wfMsg( "boardvote_contributing" );
		$volunteer = wfMsg( "boardvote_volunteer" );
		$ok = wfMsg( "ok" );
		
		$candidatesV = $candidatesC = array();
		foreach( $wgContributingCandidates as $i => $candidate ) {
			$candidatesC[] = array( $i, $candidate );
		}
		foreach ( $wgVolunteerCandidates as $i => $candidate ) {
			$candidatesV[] = array( $i, $candidate );
		}

		srand ((float)microtime()*1000000);
		shuffle( $candidatesC );
		shuffle( $candidatesV );

		$text = "
		  $intro
		  <form name=\"boardvote\" id=\"boardvote\" method=\"post\" action=\"$action\">
		  <table border='0'><tr><td colspan=2>
		  <h2>$contributing</h2>
		  </td></tr>";
		$text .= $this->voteEntry( -1, wfMsg( "boardvote_abstain" ), "contributing" );
		foreach ( $candidatesC as $candidate ) {
			$text .= $this->voteEntry( $candidate[0], $candidate[1], "contributing" );
		}
		$text .= "
		  <tr><td colspan=2>
		  <h2>$volunteer</h2></td></tr>";
		$text .= $this->voteEntry( -1, wfMsg( "boardvote_abstain" ), "volunteer" );
		foreach ( $candidatesV as $candidate ) {
			$text .= $this->voteEntry( $candidate[0], $candidate[1], "volunteer" );
		}
		
		$text .= "<tr><td>&nbsp;</td><td>
		  <input name=\"submit\" type=\"submit\" value=\"$ok\">
		  </td></tr></table></form>";
		$text .= wfMsg( "boardvote_footer" );
		$wgOut->addHTML( $text );
	}

	function voteEntry( $index, $candidate, $name ) {
		if ( $index == -1 ) {
			$checked = " CHECKED";
		} else {
			$checked = "";
		}

		return "
		<tr><td align=\"right\">
		  <input type=\"radio\" name=\"$name\" value=\"$index\"$checked>
		</td><td align=\"left\">
		  $candidate
		</td></tr>";
	}

	function notLoggedIn() {
		global $wgOut;
		$wgOut->addWikiText( wfMsg( "boardvote_notloggedin" ) );
	}
	
	function notQualified() {
		global $wgOut;
		$wgOut->addWikiText( wfMsg( "boardvote_notqualified", $this->mUserDays ) );
	}
	
	function encrypt( $contributing, $volunteer ) {
		global $wgVolunteerCandidates, $wgContributingCandidates;
		global $wgGPGCommand, $wgGPGRecipient, $wgGPGHomedir;
		$file = @fopen( "/dev/urandom", "r" );
		if ( $file ) {
			$salt = implode( "", unpack( "H*", fread( $file, 64 ) ));
			fclose( $file );
		} else {
			$salt = Parser::getRandomString() . Parser::getRandomString();
		}
		$record = 
		  "Contributing: $contributing (" .$wgContributingCandidates[$contributing] . ")\n" .
		  "Volunteer: $volunteer (" . $wgVolunteerCandidates[$volunteer] . ")\n" .
		  "Salt: $salt\n";
		# Get file names
		$input = tempnam( "/tmp", "gpg_" );
		$output = tempnam( "/tmp", "gpg_" );

		# Write unencrypted record
		$file = fopen( $input, "w" );
		fwrite( $file, $record );
		fclose( $file );

		# Call GPG
		$command = wfEscapeShellArg( $wgGPGCommand ) . " --batch --yes -ear " . 
		  wfEscapeShellArg( $wgGPGRecipient ) . " -o " . wfEscapeShellArg( $output );
		if ( $wgGPGHomedir ) {
			$command .= " --homedir " . wfEscapeShellArg( $wgGPGHomedir );
		} 
		$command .= " " . wfEscapeShellArg( $input );

		shell_exec( $command );

		# Read result
		$result = file_get_contents( $output );

		# Delete temporary files
		unlink( $input );
		unlink( $output );
		
		return $result;
	}

	function getQualifications( &$user ) {
		$id = $user->getID();
		if ( !$id ) {
			$this->mUserDays = 0;
			$this->mUserEdits = 0;
			return;
		}

		# Count contributions and find earliest edit
		# First cur
		$sql = "SELECT COUNT(*) as n, MIN(cur_timestamp) as t FROM cur WHERE cur_user=$id";
		$res = wfQuery( $sql, DB_READ, "BoardVotePage::getQualifications" );
		$cur = wfFetchObject( $res );
		wfFreeResult( $res );

		# If the user has stacks of contributions, don't check old as well
		$now = time();
		if ( is_null( $cur->t ) ) {
			$signup = $now;
		} else {
			$signup = wfTimestamp2Unix( $cur->t );
		}
		
		$days = ($now - $signup) / 86400;
		if ( $cur->n > 400 && $days > 180 ) {
			$this->mUserDays = 0x7fffffff;
			$this->mUserEdits = 0x7fffffff;
			return;
		}

		# Now check old
		$sql = "SELECT COUNT(*) as n, MIN(old_timestamp) as t FROM old WHERE old_user=$id";
		$res = wfQuery( $sql, DB_READ, "BoardVotePage::getQualifications" );
		$old = wfFetchObject( $res );
		wfFreeResult( $res );
		
		if ( !is_null( $old->t ) ) {
			$signup = min( wfTimestamp2Unix( $old->t ), $signup );
		}
		$this->mUserDays = (int)(($now - $signup) / 86400);
		$this->mUserEdits = $cur->n + $old->n;
	}
	
	function displayList() {
		global $wgOut, $wgOutputEncoding, $wgLang, $wgUser;
		$sql = "SELECT log_timestamp,log_user_key FROM {$this->mDBname}.log ORDER BY log_user_key";
		$res = wfQuery( $sql, DB_READ, "BoardVotePage::list" );
		if ( wfNumRows( $res ) == 0 ) {
			$wgOut->addWikiText( wfMsg( "boardvote_novotes" ) );
			return;
		}
		$thisTitle = Title::makeTitle( NS_SPECIAL, "Boardvote" );
		$sk = $wgUser->getSkin();
		$dumpLink = $sk->makeKnownLinkObj( $thisTitle, wfMsg( "boardvote_dumplink" ), "action=dump" );
		
		$intro = wfMsg( "boardvote_listintro", $dumpLink );
		$hTime = wfMsg( "boardvote_time" );
		$hUser = wfMsg( "boardvote_user" );
		$hContributing = wfMsg( "boardvote_contributing" );
		$hVolunteer = wfMsg( "boardvote_volunteer" );

		$s = "$intro <table border=1><tr><th>
		    $hUser
		  </th><th>
		    $hTime
		  </th></tr>";

		while ( $row = wfFetchObject( $res ) ) {
			if ( $wgOutputEncoding != "utf-8" ) {
				$user = wfUtf8ToHTML( $row->log_user_key );
			} else {
				$user = $row->log_user_key;
			}
			$time = $wgLang->timeanddate( $row->log_timestamp );
			$s .= "<tr><td>
			    $user
			  </td><td>
			    $time
			  </td></tr>";
		}
		$s .= "</table>";
		$wgOut->addHTML( $s );
	}

	function dump() {
		global $wgOut, $wgOutputEncoding, $wgLang, $wgUser;

		$userRights = $wgUser->getRights();
		if ( in_array( "boardvote", $userRights ) ) {
			$admin = true;
		} else {
			$admin = false;
		}
		
		$sql = "SELECT * FROM {$this->mDBname}.log";
		$res = wfQuery( $sql, DB_READ, "BoardVotePage::list" );
		if ( wfNumRows( $res ) == 0 ) {
			$wgOut->addWikiText( wfMsg( "boardvote_novotes" ) );
			return;
		}
		$s = "<table border=1>";
		if ( $admin ) {
			$s .= wfMsg( "boardvote_dumpheader" );
		}
		
		while ( $row = wfFetchObject( $res ) ) {
			if ( $wgOutputEncoding != "utf-8" ) {
				$user = wfUtf8ToHTML( $row->log_user_key );
			} else {
				$user = $row->log_user_key;
			}
			$time = $wgLang->timeanddate( $row->log_timestamp );
			$record = nl2br( $row->log_record );
			if ( $admin ) {
				$edits = $row->log_edits == 0x7fffffff ? "many" : $row->log_edits;
				$days = $row->log_days == 0x7fffffff ? "many" : $row->log_days;
				$s .= "<tr><td>
				  $time
				</td><td>
				  $user
				</td><td>
				  $edits
				</td><td>
				  $days
				</td><td>
				  {$row->log_ip}
				</td><td>
				  {$row->log_ua}
				</td><td>
				  $record
				</td></tr>";
			} else {
				$s .= "<tr><td>$time</td><td>$user</td><td>$record</td></tr>";
			}
		}
		$s .= "</table>";
		$wgOut->addHTML( $s );
	}
}

SpecialPage::addPage( new BoardVotePage );

global $wgMessageCache;
$wgMessageCache->addMessages( array(
# Board vote
"boardvote"               => "Wikimedia Board of Trustees election",
"boardvote_entry"         => 
"* [[Special:Boardvote/vote|Vote]]
* [[Special:Boardvote/list|List votes to date]]
* [[Special:Boardvote/dump|Dump encrypted election record]]",
"boardvote_intro"         => "<p>Please choose your preferred candidate for both the 
Contributing Representative and the Volunteer Representative.</p>",
"boardvote_intro_change"  => "<p>You have voted before. However you may change 
your vote using the form below.</p>",
"boardvote_abstain"       => "Abstain",
"boardvote_footer"        => "&nbsp;",
"boardvote_entered"       => "Thank you, your vote has been recorded.

Following is your encrypted vote record. It will appear publicly at [[Special:Boardvote/dump]]. 

<pre>$1</pre>

[[Special:Boardvote/entry|Back]]",
"boardvote_notloggedin"   => "You are not logged in. To vote, you must use an account
which has existed for at least 90 days.",
"boardvote_notqualified"  => "Sorry, your first contribution was only $1 days ago. 
You need to have been contributing for at least 90 days to vote in this election.",
"boardvote_novotes"       => "Nobody has voted yet.",
"boardvote_contributing"  => "Contributing candidate",
"boardvote_volunteer"     => "Volunteer candidate",
"boardvote_time"          => "Time",
"boardvote_user"          => "User",
"boardvote_listintro"     => "<p>This is a list of all votes which have been recorded 
to date. $1 for the full encrypted election record.</p>",
"boardvote_dumplink"      => "Click here",
"boardvote_dumpheader"    => "<caption><strong>Election administrator private dump</strong></caption>
<tr><th>Time</th><th>User</th><th>Edits</th><th>Days</th>
<th>IP</th><th>User agent</th><th>Record</th></tr>"

));

} # End of extension function

?>