3 function wfSpecialBoardvote( $par = "" ) {
4 global $wgRequest, $wgBoardVoteDB;
6 $form = new BoardVoteForm( $wgRequest, $wgBoardVoteDB );
15 var $mPosted, $mContributing, $mVolunteer, $mDBname, $mUserDays, $mUserEdits;
16 var $mHasVoted, $mAction, $mUserKey;
18 function BoardVoteForm( &$request, $dbName ) {
19 global $wgUser, $wgDBname, $wgInputEncoding;
21 $this->mUserKey
= iconv( $wgInputEncoding, "UTF-8", $wgUser->getName() ) . "@$wgDBname";
22 $this->mPosted
= $request->wasPosted();
23 $this->mContributing
= $request->getInt( "contributing" );
24 $this->mVolunteer
= $request->getInt( "volunteer" );
25 $this->mDBname
= $dbName;
26 $this->mHasVoted
= $this->hasVoted( $wgUser );
27 $this->mAction
= $request->getText( "action" );
32 if ( $this->mAction
== "list" ) {
34 } elseif ( $this->mAction
== "dump" ) {
36 } elseif( $this->mAction
== "vote" ) {
37 if ( !$wgUser->getID() ) {
40 $this->getQualifications( $wgUser );
41 if ( $this->mUserDays
< 90 ) {
42 $this->notQualified();
43 } elseif ( $this->mPosted
) {
50 $this->displayEntry();
54 function displayEntry() {
56 $wgOut->addWikiText( wfMsg( "boardvote_entry" ) );
59 function hasVoted( &$user ) {
61 $row = wfGetArray( $this->mDBname
. ".vote", array( "1" ),
62 array( "vote_key" => $this->mUserKey
), "BoardVoteForm::getUserVote" );
63 if ( $row === false ) {
71 global $wgUser, $wgDBname, $wgIP, $wgOut;
72 $now = wfTimestampNow();
73 $record = $this->encrypt( $this->mContributing
, $this->mVolunteer
);
77 wfInsertArray( "$db.log", array(
78 "log_user" => $wgUser->getID(),
79 "log_user_text" => $wgUser->getName(),
80 "log_user_key" => $this->mUserKey
,
81 "log_wiki" => $wgDBname,
82 "log_edits" => $this->mUserEdits
,
83 "log_days" => $this->mUserDays
,
84 "log_record" => $record,
86 "log_xff" => @$_SERVER['HTTP_X_FORWARDED_FOR'],
87 "log_ua" => $_SERVER['HTTP_USER_AGENT'],
88 "log_timestamp" => $now
91 # Record vote in non-duplicating vote table
92 $sql = "REPLACE INTO $db.vote (vote_key,vote_record) " .
93 "VALUES ('". wfStrencode( $this->mUserKey
) . "','" . wfStrencode( $record ) . "')";
94 wfQuery( $sql, DB_WRITE
);
96 $wgOut->addWikiText( wfMsg( "boardvote_entered", $record ) );
99 function displayVote() {
100 global $wgContributingCandidates, $wgVolunteerCandidates, $wgOut;
102 $thisTitle = Title
::makeTitle( NS_SPECIAL
, "Boardvote" );
103 $action = $thisTitle->getLocalURL( "action=vote" );
104 if ( $this->mHasVoted
) {
105 $intro = wfMsg( "boardvote_intro_change" );
107 $intro = wfMsg( "boardvote_intro" );
109 $contributing = wfMsg( "boardvote_contributing" );
110 $volunteer = wfMsg( "boardvote_volunteer" );
113 $candidatesV = $candidatesC = array();
114 foreach( $wgContributingCandidates as $i => $candidate ) {
115 $candidatesC[] = array( $i, $candidate );
117 foreach ( $wgVolunteerCandidates as $i => $candidate ) {
118 $candidatesV[] = array( $i, $candidate );
121 srand ((float)microtime()*1000000);
122 shuffle( $candidatesC );
123 shuffle( $candidatesV );
127 <form name=\"boardvote\" id=\"boardvote\" method=\"post\" action=\"$action\">
128 <table border='0'><tr><td colspan=2>
129 <h2>$contributing</h2>
131 $text .= $this->voteEntry( -1, wfMsg( "boardvote_abstain" ), "contributing" );
132 foreach ( $candidatesC as $candidate ) {
133 $text .= $this->voteEntry( $candidate[0], $candidate[1], "contributing" );
137 <h2>$volunteer</h2></td></tr>";
138 $text .= $this->voteEntry( -1, wfMsg( "boardvote_abstain" ), "volunteer" );
139 foreach ( $candidatesV as $candidate ) {
140 $text .= $this->voteEntry( $candidate[0], $candidate[1], "volunteer" );
143 $text .= "<tr><td> </td><td>
144 <input name=\"submit\" type=\"submit\" value=\"$ok\">
145 </td></tr></table></form>";
146 $text .= wfMsg( "boardvote_footer" );
147 $wgOut->addHTML( $text );
150 function voteEntry( $index, $candidate, $name ) {
151 if ( $index == -1 ) {
152 $checked = " CHECKED";
158 <tr><td align=\"right\">
159 <input type=\"radio\" name=\"$name\" value=\"$index\"$checked>
160 </td><td align=\"left\">
165 function notLoggedIn() {
167 $wgOut->addWikiText( wfMsg( "boardvote_notloggedin" ) );
170 function notQualified() {
172 $wgOut->addWikiText( wfMsg( "boardvote_notqualified", $this->mUserDays
) );
175 function encrypt( $contributing, $volunteer ) {
176 global $wgVolunteerCandidates, $wgContributingCandidates;
177 global $wgGPGCommand, $wgGPGRecipient, $wgGPGHomedir;
178 $file = @fopen
( "/dev/urandom", "r" );
180 $salt = implode( "", unpack( "H*", fread( $file, 64 ) ));
183 $salt = Parser
::getRandomString() . Parser
::getRandomString();
186 "Contributing: $contributing (" .$wgContributingCandidates[$contributing] . ")\n" .
187 "Volunteer: $volunteer (" . $wgVolunteerCandidates[$volunteer] . ")\n" .
190 $input = tempnam( "/tmp", "gpg_" );
191 $output = tempnam( "/tmp", "gpg_" );
193 # Write unencrypted record
194 $file = fopen( $input, "w" );
195 fwrite( $file, $record );
199 $command = wfEscapeShellArg( $wgGPGCommand ) . " --batch --yes -ear " .
200 wfEscapeShellArg( $wgGPGRecipient ) . " -o " . wfEscapeShellArg( $output );
201 if ( $wgGPGHomedir ) {
202 $command .= " --homedir " . wfEscapeShellArg( $wgGPGHomedir );
204 $command .= " " . wfEscapeShellArg( $input );
206 shell_exec( $command );
209 $result = file_get_contents( $output );
211 # Delete temporary files
218 function getQualifications( &$user ) {
219 $id = $user->getID();
221 $this->mUserDays
= 0;
222 $this->mUserEdits
= 0;
226 # Count contributions and find earliest edit
228 $sql = "SELECT COUNT(*) as n, MIN(cur_timestamp) as t FROM cur WHERE cur_user=$id";
229 $res = wfQuery( $sql, DB_READ
, "BoardVoteForm::getQualifications" );
230 $cur = wfFetchObject( $res );
231 wfFreeResult( $res );
233 # If the user has stacks of contributions, don't check old as well
235 if ( is_null( $cur->t
) ) {
238 $signup = wfTimestamp2Unix( $cur->t
);
241 $days = ($now - $signup) / 86400;
242 if ( $cur->n
> 400 && $days > 180 ) {
243 $this->mUserDays
= 0x7fffffff;
244 $this->mUserEdits
= 0x7fffffff;
249 $sql = "SELECT COUNT(*) as n, MIN(old_timestamp) as t FROM old WHERE old_user=$id";
250 $res = wfQuery( $sql, DB_READ
, "BoardVoteForm::getQualifications" );
251 $old = wfFetchObject( $res );
252 wfFreeResult( $res );
254 if ( !is_null( $old->t
) ) {
255 $signup = min( wfTimestamp2Unix( $old->t
), $signup );
257 $this->mUserDays
= (int)(($now - $signup) / 86400);
258 $this->mUserEdits
= $cur->n +
$old->n
;
261 function displayList() {
262 global $wgOut, $wgOutputEncoding, $wgLang, $wgUser;
263 $sql = "SELECT log_timestamp,log_user_key FROM {$this->mDBname}.log";
264 $res = wfQuery( $sql, DB_READ
, "BoardVoteForm::list" );
265 if ( wfNumRows( $res ) == 0 ) {
266 $wgOut->addWikiText( wfMsg( "boardvote_novotes" ) );
269 $thisTitle = Title
::makeTitle( NS_SPECIAL
, "Boardvote" );
270 $sk = $wgUser->getSkin();
271 $dumpLink = $sk->makeKnownLinkObj( $thisTitle, wfMsg( "boardvote_dumplink" ), "action=dump" );
273 $intro = wfMsg( "boardvote_listintro", $dumpLink );
274 $hTime = wfMsg( "boardvote_time" );
275 $hUser = wfMsg( "boardvote_user" );
276 $hContributing = wfMsg( "boardvote_contributing" );
277 $hVolunteer = wfMsg( "boardvote_volunteer" );
279 $s = "$intro <table border=0><tr><th>
285 while ( $row = wfFetchObject( $res ) ) {
286 if ( $wgOutputEncoding != "utf-8" ) {
287 $user = wfUtf8ToHTML( $row->log_user_key
);
289 $user = $row->log_user_key
;
291 $time = $wgLang->timeanddate( $row->log_timestamp
);
299 $wgOut->addHTML( $s );
303 global $wgOut, $wgOutputEncoding, $wgLang, $wgUser;
305 $userRights = $wgUser->getRights();
306 if ( in_array( "boardvote", $userRights ) ) {
312 $sql = "SELECT * FROM {$this->mDBname}.log";
313 $res = wfQuery( $sql, DB_READ
, "BoardVoteForm::list" );
314 if ( wfNumRows( $res ) == 0 ) {
315 $wgOut->addWikiText( wfMsg( "boardvote_novotes" ) );
318 $s = "<table border=1>";
320 $s .= wfMsg( "boardvote_dumpheader" );
323 while ( $row = wfFetchObject( $res ) ) {
324 if ( $wgOutputEncoding != "utf-8" ) {
325 $user = wfUtf8ToHTML( $row->log_user_key
);
327 $user = $row->log_user_key
;
329 $time = $wgLang->timeanddate( $row->log_timestamp
);
330 $record = nl2br( $row->log_record
);
332 $edits = $row->log_edits
== 0x7fffffff ?
"many" : $row->log_edits
;
333 $days = $row->log_days
== 0x7fffffff ?
"many" : $row->log_days
;
350 $s .= "<tr><td>$time</td><td>$user</td><td>$record</td></tr>";
354 $wgOut->addHTML( $s );