cvs: pearweb /include/election pear-election.php pear-voter.php
| From: | Greg Beaver | Date: | Tue, 21 Nov 2006 05:40:25 +0000 |
| Subject: | cvs: pearweb /include/election pear-election.php pear-voter.php | ||
| Groups: | php.pear.cvs php.pear.core | ||
| Request: | Send a blank email to pear-core+get-5214@lists.php.net to get a copy of this message | ||
cellog Tue Nov 21 05:40:25 2006 UTC
Added files:
/pearweb/include/election pear-election.php pear-voter.php
Log:
as Martin requested,
move election into a subdirectory.
include/election has pear-voter/pear-election
public_html/election has mapped from:
election.php -> index.php
election-info.php -> info.php
election-new.php -> new.php
election-edit.php -> edit.php
http://cvs.php.net/viewvc.cgi/pearweb/include/election/pear-election.php?view=markup&rev=1.1 Index: pearweb/include/election/pear-election.php +++ pearweb/include/election/pear-election.php <?php class PEAR_Election { var $dbh; var $karma; var $user; function PEAR_Election() { $this->dbh = &$GLOBALS['dbh']; $this->user = isset($GLOBALS['auth_user']) ? $GLOBALS['auth_user']->handle : false; $this->karma =& new Damblan_Karma($this->dbh); } function listElections() { if ($this->karma->has($this->user, 'pear.admin')) { return $this->dbh->getAll(' SELECT IF(votestart > NOW(),"no","yes") as active, elections.* FROM elections ORDER BY votestart DESC ', array(), DB_FETCHMODE_ASSOC); } else { // if we aren't admin, we can't touch other people's elections return $this->dbh->getAll(' SELECT IF(votestart > NOW(),"no","yes") as active, elections.* FROM elections WHERE votestart > NOW() AND creator=? ', array($this->user), DB_FETCHMODE_ASSOC); } } function electionExists($id) { return $this->dbh->getOne('SELECT id FROM elections WHERE id=?', array($id)); } function setupChoices($id, $info) { $all = $this->dbh->getAssoc(' SELECT choice,summary,summary_link FROM election_choices WHERE election_id=? ORDER BY choice ', false, array($id), DB_FETCHMODE_ASSOC); for ($i = 1; $i <= $_POST['choices']; $i++) { if (isset($all[$i])) { $info['summary' . $i] = $all[$i]['summary']; $info['summary_link' . $i] = $all[$i]['summary_link']; } else { $info['summary' . $i] = empty($_POST['summary' . $i]) ? '' : $_POST['summary' . $i]; $info['summary_link' . $i] = empty($_POST['summary_link' . $i]) ? '' : $_POST['summary_link' . $i]; } } $info['choices'] = $_POST['choices']; return $info; } function getInfo($id) { $all = $this->dbh->getAll(' SELECT purpose, detail, YEAR(votestart) as year, MONTH(votestart) as month, DAYOFMONTH(votestart) as day, voteend - votestart as length, minimum_choices as minimum, maximum_choices maximum, eligiblevoters, COUNT(c.choice) as choices FROM elections e, election_choices c WHERE id=? AND c.election_id = e.id GROUP BY c.election_id ', array($id), DB_FETCHMODE_ASSOC); if (!count($all)) { return false; } $info = $all[0]; return $info; } function validateStep1($new = true) { $error = array(); if (empty($_POST['purpose'])) { $error[] = 'Election Purpose (summary) is required'; } if (empty($_POST['choices'])) { $error[] = 'Number of Choices is required'; } else { if (!is_numeric($_POST['choices']) || ((int) $_POST['choices'] != $_POST['choices'])) { $error[] = 'Number of Choices must be an integer'; } if ($_POST['choices'] < 2 || $_POST['choices'] > 20) { $error[] = 'Number of Choices must be between 2 and 20'; } } if (empty($_POST['eligiblevoters'])) { $error[] = 'Eligible Voters is required'; } else { if (!is_numeric($_POST['eligiblevoters'])) { $error[] = 'Eligible Voters must be "PEAR Developers" or "General PHP Public"'; } if ($_POST['eligiblevoters'] != 1 && $_POST['eligiblevoters'] != 2) { $error[] = 'Eligible Voters must be "PEAR Developers" or "General PHP Public"'; } } if (empty($_POST['detail'])) { $error[] = 'Election detail is required'; } $nextyear = date('Y') + 1; $thisyear = $nextyear - 1; if (empty($_POST['year'])) { $error[] = 'Year is required'; } else { if (!is_numeric($_POST['year']) || $_POST['year'] != (int) $_POST['year']) { $error[] = 'Month is invalid'; } elseif ($_POST['year'] != $nextyear && $_POST['year'] != $thisyear) { $error[] = 'Year is invalid, must be next year or this year'; } } if (empty($_POST['month'])) { $error[] = 'Month is required'; } else { if (!is_numeric($_POST['month']) || $_POST['month'] != (int) $_POST['month']) { $error[] = 'Month is invalid'; } elseif ($_POST['month'] < 1 || $_POST['month'] > 12) { $error[] = 'Month is invalid, must be 1-12'; } } if (empty($_POST['day'])) { $error[] = 'Day is required'; } else { if (!is_numeric($_POST['day']) || $_POST['day'] != (int) $_POST['day']) { $error[] = 'Day is invalid'; } elseif ($_POST['day'] < 1 || $_POST['day'] > date('d', strtotime( $_POST['year'] . '-' . $_POST['month'] . '-01 +1 month -1 day'))) { $error[] = 'Day is invalid, must be 1-' . date('d', strtotime( $_POST['year'] . '-' . $_POST['month'] . '-01 +1 month -1 day')); } } $date = $_POST['year'] . '-' . $_POST['month'] . '-' . $_POST['day']; if (strtotime($date) != strtotime(date('Y-m-d', strtotime($date)))) { $error[] = 'Full date is invalid'; } if ($new && (strtotime($date) - time()) / 86400 < 29.0) { $error[] = 'Voting must start at least 30 days from today'; } if (empty($_POST['length'])) { $error[] = 'Election length is required'; } else { if (!is_numeric($_POST['length']) || $_POST['length'] != (int) $_POST['length']) { $error[] = 'Voting length is invalid'; } elseif ($_POST['length'] < 1 || $_POST['length'] > 7) { $error[] = 'Voting length must be between 1 and 7 days'; } } if (empty($_POST['minimum'])) { $error[] = 'Election minimum votes needed is required'; } else { if (!is_numeric($_POST['minimum']) || $_POST['minimum'] != (int) $_POST['minimum']) { $error[] = 'Minimum votes needed is invalid'; } elseif ($_POST['minimum'] < 1 || $_POST['minimum'] > 19) { $error[] = 'Minimum votes needed must be between 1 and 19'; } } if (empty($_POST['maximum'])) { $error[] = 'Election maximum votes needed is required'; } else { if (!is_numeric($_POST['maximum']) || $_POST['maximum'] != (int) $_POST['maximum']) { $error[] = 'Maximum votes needed is invalid'; } elseif ($_POST['maximum'] < 1 || $_POST['maximum'] > 19) { $error[] = 'Maximum votes needed must be between 1 and 19'; } } if ($_POST['maximum'] < $_POST['minimum']) { $error[] = 'Maximum votes needed must be greater or the same as minimum votes needed'; } if ($_POST['minimum'] > $_POST['choices']) { $error[] = 'Minimum votes needed must be less than or equal to the number of choices'; } if ($_POST['maximum'] > $_POST['choices']) { $error[] = 'Maximum votes allowed must be less than or equal to the number of choices'; } return $error; } function validateStep2() { $error = array(); if (isset($_POST['add1choice'])) { $error[] = 'Added one choice'; $_POST['choices']++; } elseif (isset($_POST['delete1choice'])) { if ($_POST['choices'] <= $_POST['maximum']) { $error[] = 'Cannot delete, must have at least the as many choices as ' . 'the Maximum votes allowed'; } else { $error[] = 'Deleted last choice'; $_POST['choices']--; } } else { for ($i = 1; $i <= $_POST['choices']; $i++) { if (empty($_POST['summary' . $i])) { $error[] = 'Summary for Choice #' . $i . ' must not be empty'; } if (empty($_POST['summary_link' . $i])) { $error[] = 'Link to more info for Choice #' . $i . ' must not be empty'; } } } return $error; } function saveNewElection() { $startdate = date('Y-m-d', strtotime($_POST['year'] . '-' . $_POST['month'] . '-' . $_POST['day'])); $enddate = date('Y-m-d', strtotime($startdate . ' +' . $_POST['length'] . ' days')); $this->dbh->query(' INSERT INTO elections (purpose, detail, votestart, voteend, creator, createdate, minimum_choices, maximum_choices, eligiblevoters) VALUES(?,?,?,?,?,NOW(),?,?,?) ', array( $_POST['purpose'], $_POST['detail'], $startdate, $enddate, $this->user, $_POST['minimum'], $_POST['maximum'], $_POST['eligiblevoters'] )); $id = $this->dbh->phptype == 'mysql' ? mysql_insert_id() : mysqli_insert_id($this->dbh->connection); for ($i = 1; $i <= $_POST['choices']; $i++) { $this->dbh->query(' INSERT INTO election_choices (election_id, choice, summary, summary_link) VALUES(?,?,?,?) ', array($id, $i, $_POST['summary' . $i], $_POST['summary_link' . $i])); } } function saveEditedElection() { $id = $_POST['election_id']; $info = $this->getInfo($id); $startdate = date('Y-m-d', strtotime($_POST['year'] . '-' . $_POST['month'] . '-' . $_POST['day'])); $enddate = date('Y-m-d', strtotime($startdate . ' +' . $_POST['length'] . ' days')); $this->dbh->query(' UPDATE elections SET purpose=?, detail=?, votestart=?, voteend=?, minimum_choices=?, maximum_choices=?, eligiblevoters=? WHERE id=? ', array( $_POST['purpose'], $_POST['detail'], $startdate, $enddate, $_POST['minimum'], $_POST['maximum'], $_POST['eligiblevoters'], $id )); $this->dbh->query('DELETE FROM election_choices WHERE election_id=?', array($id)); for ($i = 1; $i <= $_POST['choices']; $i++) { $this->dbh->query(' INSERT INTO election_choices (election_id, choice, summary, summary_link) VALUES(?,?,?,?) ', array($id, $i, $_POST['summary' . $i], $_POST['summary_link' . $i])); } } function canEdit($id) { if ('yes' == $this->dbh->getOne(' SELECT IF(votestart > NOW(),"no","yes") FROM elections WHERE id=?', array($id))) { // cannot edit active or old elections return false; } if ($this->karma->has($this->user, 'pear.admin')) { return true; } if (!$this->electionExists($id)) { return false; } if ($this->user == $this->dbh->getOne('SELECT creator FROM elections WHERE id=?', array($id))) { return true; } return false; } } http://cvs.php.net/viewvc.cgi/pearweb/include/election/pear-voter.php?view=markup&rev=1.1 Index: pearweb/include/election/pear-voter.php +++ pearweb/include/election/pear-voter.php <?php require 'Text/Wiki.php'; require_once 'Damblan/Karma.php'; class PEAR_Voter { var $dbh; var $user = false; var $voteSalt; var $damblan; function PEAR_Voter() { $this->dbh = &$GLOBALS['dbh']; $this->user = isset($GLOBALS['auth_user']) ? $GLOBALS['auth_user']->handle : false; $this->damblan = new Damblan_Karma($this->dbh); } function listCurrentElections() { if ($this->user) { $all = $this->dbh->getAll(' SELECT IF(e.votestart > NOW(),"no","yes") as active, e.purpose, e.votestart, e.voteend, e.id FROM elections e WHERE e.votestart >= NOW() OR (e.votestart < NOW() AND e.voteend >= NOW()) ORDER BY e.votestart ', array(), DB_FETCHMODE_ASSOC); foreach ($all as $i => $election) { $vote = $this->dbh->getOne('SELECT COUNT(*) FROM election_handle_votes WHERE election_id=? AND handle=?', array($election['id'], $this->user)); $all[$i]['voted'] = $vote ? 'yes' : 'no'; } } else { $all = $this->dbh->getAll(' SELECT IF(e.votestart > NOW(),"no","yes") as active, "no" as voted, e.purpose, e.votestart, e.voteend, e.id FROM elections e WHERE e.votestart >= NOW() OR (e.votestart < NOW() AND e.voteend >= NOW()) ORDER BY e.votestart ', array(), DB_FETCHMODE_ASSOC); } if (!is_array($all)) { return array(); } return $all; } function listCompletedElections() { if ($this->user) { $all = $this->dbh->getAll(' SELECT e.purpose, e.votestart, e.voteend, e.id FROM elections e, election_results r WHERE e.id = r.election_id AND e.voteend < NOW() GROUP BY e.id ORDER BY e.voteend DESC ', array(), DB_FETCHMODE_ASSOC); foreach ($all as $i => $election) { $results = $this->dbh->getAll(' SELECT * FROM election_results WHERE election_id=? ORDER BY votepercent DESC ', array($election['id']), DB_FETCHMODE_ASSOC); $vote = $this->dbh->getOne('SELECT COUNT(*) FROM election_handle_votes WHERE election_id=? AND handle=?', array($election['id'], $this->user)); $all[$i]['voted'] = $vote ? 'yes' : 'no'; $all[$i]['results'] = $results; } } else { $all = $this->dbh->getAll(' SELECT "no" as voted, e.purpose, e.votestart, e.voteend, e.id, c.summary as winner, c.summary_link as winnerlink, e.id FROM elections e, election_choices c, election_results r WHERE e.id = r.election_id AND c.election_id = e.id AND e.voteend < NOW() ORDER BY e.voteend DESC ', array(), DB_FETCHMODE_ASSOC); foreach ($all as $i => $election) { $results = $this->dbh->getAll(' SELECT * FROM election_results WHERE election_id=? ORDER BY votepercent DESC ', array($election['id']), DB_FETCHMODE_ASSOC); $all[$i]['results'] = $results; } } if (!is_array($all)) { return array(); } return $all; } function listAllElections() { if (!$this->user) { return array(); } $all = $this->dbh->getAll(' SELECT e.purpose, e.id FROM elections e, election_handle_votes v WHERE v.election_id = e.id AND v.handle=? ORDER BY e.voteend DESC ', array($this->user), DB_FETCHMODE_ASSOC); if (!is_array($all)) { return array(); } return $all; } function electionExists($id) { return $this->dbh->getOne('SELECT COUNT(id) FROM elections WHERE id=?', array($id)); } function electionInfo($id) { $info = $this->dbh->getAll(' SELECT * FROM elections WHERE id = ? ', array($id), DB_FETCHMODE_ASSOC); if (!is_array($info)) { return false; } $info = $info[0]; $choices = $this->dbh->getAll(' SELECT * FROM election_choices WHERE election_id = ? ORDER BY choice ', array($id), DB_FETCHMODE_ASSOC); $info['choices'] = $choices; $info['results'] = $this->dbh->getAll(' SELECT e.votepercent, e.votetotal, c.choice, c.summary, c.summary_link FROM election_results e, election_choices c WHERE e.election_id = ? AND c.election_id = e.election_id AND c.choice = e.choice ORDER BY e.votetotal DESC ', array($id), DB_FETCHMODE_ASSOC); // calculate winners $order = array(); foreach ($info['results'] as $result) { $order[$result['votetotal']][] = $result; } krsort($order, SORT_NUMERIC); $winners = array(); foreach ($order as $results) { if (count($winners) >= $info['maximum_choices']) { break; // done } foreach ($results as $result) { $winners[] = $result['choice']; } } $info['winners'] = $winners; $abstain = $this->dbh->getOne(' SELECT COUNT(*) FROM election_votes_abstain WHERE election_id=? ', array($id)); $allvoters = $this->dbh->getOne(" SELECT COUNT(DISTINCT k.user) FROM karma k, users u WHERE k.user = u.handle AND k.level in ('pear.dev', 'pear.voter', 'pear.admin')", array()); $votedthis = $this->dbh->getOne(' SELECT count(*) FROM election_handle_votes where election_id=? ', array($id)); $info['turnout'] = $votedthis / $allvoters; $wiki =& new Text_Wiki(); $wiki->disableRule('wikilink'); $info['detail'] = $wiki->transform($info['detail']); if ($info['maximum_choices'] > 1) { $total = $this->dbh->getOne(' SELECT COUNT(*) FROM election_votes_multiple WHERE election_id=? ', array($id)); } else { $total = $this->dbh->getOne(' SELECT COUNT(*) FROM election_votes_single WHERE election_id=? ', array($id)); } // percentage of abstaining voters if ($total + $abstain > 0) { $info['abstain'] = $abstain / ($total + $abstain); $info['abstaincount'] = $abstain; } else { $info['abstain'] = 0; $info['abstaincount'] = 0; } return $info; } function hasVoted($id) { return $this->dbh->getOne(' SELECT COUNT(*) FROM election_handle_votes WHERE election_id=? AND handle=?', array($id, $this->user)); } function canVote($id) { if ($this->hasVoted($id)) { return false; } $info = $this->electionInfo($id); if (strtotime($info['votestart']) - time() > 0) { // election is not active return false; } if (strtotime($info['voteend']) - time() < 0) { // election is finished return false; } if ($info['eligiblevoters'] == 1) { // PEAR developers if ($this->damblan->has($this->user, 'pear.dev')) { return true; } if ($this->damblan->has($this->user, 'pear.admin')) { return true; } return false; } elseif ($info['eligiblevoters'] == 2) { // general PHP public + PEAR developers if ($this->damblan->has($this->user, 'pear.dev')) { return true; } if ($this->damblan->has($this->user, 'pear.admin')) { return true; } if ($this->damblan->has($this->user, 'pear.voter')) { return true; } return false; } return false; } function getVoteSalt() { if (!isset($this->voteSalt)) { $this->voteSalt = date('YmdHis') . mt_rand(1, 999); } return $this->voteSalt; } function vote($id, $votes) { if (!$this->user) { return false; } if (!$this->electionExists($id)) { return false; } if ($this->hasVoted($id)) { return false; } if (!is_array($votes)) { return false; } $info = $this->electionInfo($id); if ($info['maximum_choices'] > 1) { if (count($votes) > $info['maximum_choices'] || count($votes) < $info['minimum_choices']) { return false; } $table = 'election_votes_multiple'; } else { if (count($votes) != 1) { return false; } $table = 'election_votes_single'; } $vote_hash = md5($this->user . $this->getVoteSalt()); PEAR::pushErrorHandling(PEAR_ERROR_RETURN); foreach ($votes as $vote) { $err = $this->dbh->query(' INSERT INTO ' . $table . ' (election_id, vote, vote_hash) VALUES(?,?,?)', array($id, $vote, $vote_hash)); if (PEAR::isError($err)) { // "rollback" the query $this->dbh->query('DELETE FROM ' . $table . ' WHERE election_id=? AND vote_hash=?', array($id, $vote_hash)); PEAR::popErrorHandling(); return false; } } $err = $this->dbh->query(' INSERT INTO election_handle_votes (election_id, handle) VALUES (?,?)', array($id, $this->user)); if (PEAR::isError($err)) { // "rollback" the query $this->dbh->query('DELETE FROM ' . $table . ' WHERE election_id=? AND vote_hash=?', array($id, $vote_hash)); $this->dbh->query('DELETE FROM election_handle_votes WHERE election_id=? AND handle=?', array($id, $this->user)); PEAR::popErrorHandling(); return false; } PEAR::popErrorHandling(); $this->email($info, $votes, $this->getVoteSalt()); return true; } function email($election, $votes, $salt) { $info = user::info($this->user); $email = '"' . $info['name'] . '" <' . $info['email'] . '>'; $headers = "From: bounce-no-user@php.net\n"; $headers .= "X-Mailer: PEAR election voting interface\n"; $headers .= "X-PEAR-Election: " . $election['id'] . "\n"; $subject = '[PEAR-ELECTION] Your vote in election ' . $election['purpose']; if ($votes) { $text = 'Your vote for the election: ' . $election['purpose'] . "\n" . 'has been registered. You voted for: '; $text .= implode("\n", $votes) . "\n"; $text .= 'Your vote salt is ' . $salt . "\n"; $text .= 'this is your only record of the vote salt, without it your vote ' . 'cannot be retrieved. Thank you for voting'; } else { $text = 'Your abstaining vote for the election: ' . $election['purpose'] . "\n" . 'has been registered.' . "\n"; $text .= 'Your vote salt is ' . $salt . "\n"; $text .= 'this is your only record of the vote salt, without it your vote ' . 'cannot be retrieved. Thank you for voting'; } $text .= "\nVisit http://pear.php.net/election.php to retrieve your vote"; $res = mail($email, $subject, $text, $headers, '-f bounce-no-user@php.net'); return $res; } function abstain($id) { if (!$this->user) { return false; } if (!$this->electionExists($id)) { return false; } if ($this->hasVoted($id)) { return false; } $vote_hash = md5($this->user . $this->getVoteSalt()); PEAR::pushErrorHandling(PEAR_ERROR_RETURN); $err = $this->dbh->query(' INSERT INTO election_votes_abstain (election_id, vote_hash) VALUES (?,?)', array($id, $vote_hash)); if (PEAR::isError($err)) { PEAR::popErrorHandling(); return false; } $err = $this->dbh->query(' INSERT INTO election_handle_votes (election_id, handle) VALUES (?,?)', array($id, $this->user)); if (PEAR::isError($err)) { $this->dbh->query('DELETE FROM election_votes_abstain WHERE election_id=? and vote_hash=?', array($id, $vote_hash)); PEAR::popErrorHandling(); return false; } PEAR::popErrorHandling(); $this->email($this->electionInfo($id), false, $this->getVoteSalt()); return true; } function retrieveVote($id, $salt) { if (!$this->electionExists($id)) { return false; } if (!$this->user) { return false; } if (!$this->hasVoted($id)) { return false; } $this->voteSalt = md5($this->user . $salt); if ($vote = $this->dbh->getOne(' SELECT * FROM election_votes_abstain WHERE election_id=? AND vote_hash=?', array($id, $this->voteSalt))) { return array('(abstain)'); } if ($vote = $this->dbh->getOne(' SELECT choice FROM election_votes_single e, election_choices c WHERE e.election_id=? AND e.vote_hash=? AND e.election_id = c.election_id AND e.vote = c.choice', array($id, $this->voteSalt))) { return array($vote); } $votes = $this->dbh->getAll(' SELECT choice FROM election_votes_multiple e, election_choices c WHERE e.election_id=? AND e.vote_hash=? AND e.election_id = c.election_id AND e.vote = c.choice', array($id, $this->voteSalt)); $ret = array(); foreach ($votes as $vote) { $ret[] = $vote[0]; } if (!$ret) { return false; } return $ret; } }
http://cvs.php.net/viewvc.cgi/pearweb/include/election/pear-election.php?view=markup&rev=1.1 Index: pearweb/include/election/pear-election.php +++ pearweb/include/election/pear-election.php <?php class PEAR_Election { var $dbh; var $karma; var $user; function PEAR_Election() { $this->dbh = &$GLOBALS['dbh']; $this->user = isset($GLOBALS['auth_user']) ? $GLOBALS['auth_user']->handle : false; $this->karma =& new Damblan_Karma($this->dbh); } function listElections() { if ($this->karma->has($this->user, 'pear.admin')) { return $this->dbh->getAll(' SELECT IF(votestart > NOW(),"no","yes") as active, elections.* FROM elections ORDER BY votestart DESC ', array(), DB_FETCHMODE_ASSOC); } else { // if we aren't admin, we can't touch other people's elections return $this->dbh->getAll(' SELECT IF(votestart > NOW(),"no","yes") as active, elections.* FROM elections WHERE votestart > NOW() AND creator=? ', array($this->user), DB_FETCHMODE_ASSOC); } } function electionExists($id) { return $this->dbh->getOne('SELECT id FROM elections WHERE id=?', array($id)); } function setupChoices($id, $info) { $all = $this->dbh->getAssoc(' SELECT choice,summary,summary_link FROM election_choices WHERE election_id=? ORDER BY choice ', false, array($id), DB_FETCHMODE_ASSOC); for ($i = 1; $i <= $_POST['choices']; $i++) { if (isset($all[$i])) { $info['summary' . $i] = $all[$i]['summary']; $info['summary_link' . $i] = $all[$i]['summary_link']; } else { $info['summary' . $i] = empty($_POST['summary' . $i]) ? '' : $_POST['summary' . $i]; $info['summary_link' . $i] = empty($_POST['summary_link' . $i]) ? '' : $_POST['summary_link' . $i]; } } $info['choices'] = $_POST['choices']; return $info; } function getInfo($id) { $all = $this->dbh->getAll(' SELECT purpose, detail, YEAR(votestart) as year, MONTH(votestart) as month, DAYOFMONTH(votestart) as day, voteend - votestart as length, minimum_choices as minimum, maximum_choices maximum, eligiblevoters, COUNT(c.choice) as choices FROM elections e, election_choices c WHERE id=? AND c.election_id = e.id GROUP BY c.election_id ', array($id), DB_FETCHMODE_ASSOC); if (!count($all)) { return false; } $info = $all[0]; return $info; } function validateStep1($new = true) { $error = array(); if (empty($_POST['purpose'])) { $error[] = 'Election Purpose (summary) is required'; } if (empty($_POST['choices'])) { $error[] = 'Number of Choices is required'; } else { if (!is_numeric($_POST['choices']) || ((int) $_POST['choices'] != $_POST['choices'])) { $error[] = 'Number of Choices must be an integer'; } if ($_POST['choices'] < 2 || $_POST['choices'] > 20) { $error[] = 'Number of Choices must be between 2 and 20'; } } if (empty($_POST['eligiblevoters'])) { $error[] = 'Eligible Voters is required'; } else { if (!is_numeric($_POST['eligiblevoters'])) { $error[] = 'Eligible Voters must be "PEAR Developers" or "General PHP Public"'; } if ($_POST['eligiblevoters'] != 1 && $_POST['eligiblevoters'] != 2) { $error[] = 'Eligible Voters must be "PEAR Developers" or "General PHP Public"'; } } if (empty($_POST['detail'])) { $error[] = 'Election detail is required'; } $nextyear = date('Y') + 1; $thisyear = $nextyear - 1; if (empty($_POST['year'])) { $error[] = 'Year is required'; } else { if (!is_numeric($_POST['year']) || $_POST['year'] != (int) $_POST['year']) { $error[] = 'Month is invalid'; } elseif ($_POST['year'] != $nextyear && $_POST['year'] != $thisyear) { $error[] = 'Year is invalid, must be next year or this year'; } } if (empty($_POST['month'])) { $error[] = 'Month is required'; } else { if (!is_numeric($_POST['month']) || $_POST['month'] != (int) $_POST['month']) { $error[] = 'Month is invalid'; } elseif ($_POST['month'] < 1 || $_POST['month'] > 12) { $error[] = 'Month is invalid, must be 1-12'; } } if (empty($_POST['day'])) { $error[] = 'Day is required'; } else { if (!is_numeric($_POST['day']) || $_POST['day'] != (int) $_POST['day']) { $error[] = 'Day is invalid'; } elseif ($_POST['day'] < 1 || $_POST['day'] > date('d', strtotime( $_POST['year'] . '-' . $_POST['month'] . '-01 +1 month -1 day'))) { $error[] = 'Day is invalid, must be 1-' . date('d', strtotime( $_POST['year'] . '-' . $_POST['month'] . '-01 +1 month -1 day')); } } $date = $_POST['year'] . '-' . $_POST['month'] . '-' . $_POST['day']; if (strtotime($date) != strtotime(date('Y-m-d', strtotime($date)))) { $error[] = 'Full date is invalid'; } if ($new && (strtotime($date) - time()) / 86400 < 29.0) { $error[] = 'Voting must start at least 30 days from today'; } if (empty($_POST['length'])) { $error[] = 'Election length is required'; } else { if (!is_numeric($_POST['length']) || $_POST['length'] != (int) $_POST['length']) { $error[] = 'Voting length is invalid'; } elseif ($_POST['length'] < 1 || $_POST['length'] > 7) { $error[] = 'Voting length must be between 1 and 7 days'; } } if (empty($_POST['minimum'])) { $error[] = 'Election minimum votes needed is required'; } else { if (!is_numeric($_POST['minimum']) || $_POST['minimum'] != (int) $_POST['minimum']) { $error[] = 'Minimum votes needed is invalid'; } elseif ($_POST['minimum'] < 1 || $_POST['minimum'] > 19) { $error[] = 'Minimum votes needed must be between 1 and 19'; } } if (empty($_POST['maximum'])) { $error[] = 'Election maximum votes needed is required'; } else { if (!is_numeric($_POST['maximum']) || $_POST['maximum'] != (int) $_POST['maximum']) { $error[] = 'Maximum votes needed is invalid'; } elseif ($_POST['maximum'] < 1 || $_POST['maximum'] > 19) { $error[] = 'Maximum votes needed must be between 1 and 19'; } } if ($_POST['maximum'] < $_POST['minimum']) { $error[] = 'Maximum votes needed must be greater or the same as minimum votes needed'; } if ($_POST['minimum'] > $_POST['choices']) { $error[] = 'Minimum votes needed must be less than or equal to the number of choices'; } if ($_POST['maximum'] > $_POST['choices']) { $error[] = 'Maximum votes allowed must be less than or equal to the number of choices'; } return $error; } function validateStep2() { $error = array(); if (isset($_POST['add1choice'])) { $error[] = 'Added one choice'; $_POST['choices']++; } elseif (isset($_POST['delete1choice'])) { if ($_POST['choices'] <= $_POST['maximum']) { $error[] = 'Cannot delete, must have at least the as many choices as ' . 'the Maximum votes allowed'; } else { $error[] = 'Deleted last choice'; $_POST['choices']--; } } else { for ($i = 1; $i <= $_POST['choices']; $i++) { if (empty($_POST['summary' . $i])) { $error[] = 'Summary for Choice #' . $i . ' must not be empty'; } if (empty($_POST['summary_link' . $i])) { $error[] = 'Link to more info for Choice #' . $i . ' must not be empty'; } } } return $error; } function saveNewElection() { $startdate = date('Y-m-d', strtotime($_POST['year'] . '-' . $_POST['month'] . '-' . $_POST['day'])); $enddate = date('Y-m-d', strtotime($startdate . ' +' . $_POST['length'] . ' days')); $this->dbh->query(' INSERT INTO elections (purpose, detail, votestart, voteend, creator, createdate, minimum_choices, maximum_choices, eligiblevoters) VALUES(?,?,?,?,?,NOW(),?,?,?) ', array( $_POST['purpose'], $_POST['detail'], $startdate, $enddate, $this->user, $_POST['minimum'], $_POST['maximum'], $_POST['eligiblevoters'] )); $id = $this->dbh->phptype == 'mysql' ? mysql_insert_id() : mysqli_insert_id($this->dbh->connection); for ($i = 1; $i <= $_POST['choices']; $i++) { $this->dbh->query(' INSERT INTO election_choices (election_id, choice, summary, summary_link) VALUES(?,?,?,?) ', array($id, $i, $_POST['summary' . $i], $_POST['summary_link' . $i])); } } function saveEditedElection() { $id = $_POST['election_id']; $info = $this->getInfo($id); $startdate = date('Y-m-d', strtotime($_POST['year'] . '-' . $_POST['month'] . '-' . $_POST['day'])); $enddate = date('Y-m-d', strtotime($startdate . ' +' . $_POST['length'] . ' days')); $this->dbh->query(' UPDATE elections SET purpose=?, detail=?, votestart=?, voteend=?, minimum_choices=?, maximum_choices=?, eligiblevoters=? WHERE id=? ', array( $_POST['purpose'], $_POST['detail'], $startdate, $enddate, $_POST['minimum'], $_POST['maximum'], $_POST['eligiblevoters'], $id )); $this->dbh->query('DELETE FROM election_choices WHERE election_id=?', array($id)); for ($i = 1; $i <= $_POST['choices']; $i++) { $this->dbh->query(' INSERT INTO election_choices (election_id, choice, summary, summary_link) VALUES(?,?,?,?) ', array($id, $i, $_POST['summary' . $i], $_POST['summary_link' . $i])); } } function canEdit($id) { if ('yes' == $this->dbh->getOne(' SELECT IF(votestart > NOW(),"no","yes") FROM elections WHERE id=?', array($id))) { // cannot edit active or old elections return false; } if ($this->karma->has($this->user, 'pear.admin')) { return true; } if (!$this->electionExists($id)) { return false; } if ($this->user == $this->dbh->getOne('SELECT creator FROM elections WHERE id=?', array($id))) { return true; } return false; } } http://cvs.php.net/viewvc.cgi/pearweb/include/election/pear-voter.php?view=markup&rev=1.1 Index: pearweb/include/election/pear-voter.php +++ pearweb/include/election/pear-voter.php <?php require 'Text/Wiki.php'; require_once 'Damblan/Karma.php'; class PEAR_Voter { var $dbh; var $user = false; var $voteSalt; var $damblan; function PEAR_Voter() { $this->dbh = &$GLOBALS['dbh']; $this->user = isset($GLOBALS['auth_user']) ? $GLOBALS['auth_user']->handle : false; $this->damblan = new Damblan_Karma($this->dbh); } function listCurrentElections() { if ($this->user) { $all = $this->dbh->getAll(' SELECT IF(e.votestart > NOW(),"no","yes") as active, e.purpose, e.votestart, e.voteend, e.id FROM elections e WHERE e.votestart >= NOW() OR (e.votestart < NOW() AND e.voteend >= NOW()) ORDER BY e.votestart ', array(), DB_FETCHMODE_ASSOC); foreach ($all as $i => $election) { $vote = $this->dbh->getOne('SELECT COUNT(*) FROM election_handle_votes WHERE election_id=? AND handle=?', array($election['id'], $this->user)); $all[$i]['voted'] = $vote ? 'yes' : 'no'; } } else { $all = $this->dbh->getAll(' SELECT IF(e.votestart > NOW(),"no","yes") as active, "no" as voted, e.purpose, e.votestart, e.voteend, e.id FROM elections e WHERE e.votestart >= NOW() OR (e.votestart < NOW() AND e.voteend >= NOW()) ORDER BY e.votestart ', array(), DB_FETCHMODE_ASSOC); } if (!is_array($all)) { return array(); } return $all; } function listCompletedElections() { if ($this->user) { $all = $this->dbh->getAll(' SELECT e.purpose, e.votestart, e.voteend, e.id FROM elections e, election_results r WHERE e.id = r.election_id AND e.voteend < NOW() GROUP BY e.id ORDER BY e.voteend DESC ', array(), DB_FETCHMODE_ASSOC); foreach ($all as $i => $election) { $results = $this->dbh->getAll(' SELECT * FROM election_results WHERE election_id=? ORDER BY votepercent DESC ', array($election['id']), DB_FETCHMODE_ASSOC); $vote = $this->dbh->getOne('SELECT COUNT(*) FROM election_handle_votes WHERE election_id=? AND handle=?', array($election['id'], $this->user)); $all[$i]['voted'] = $vote ? 'yes' : 'no'; $all[$i]['results'] = $results; } } else { $all = $this->dbh->getAll(' SELECT "no" as voted, e.purpose, e.votestart, e.voteend, e.id, c.summary as winner, c.summary_link as winnerlink, e.id FROM elections e, election_choices c, election_results r WHERE e.id = r.election_id AND c.election_id = e.id AND e.voteend < NOW() ORDER BY e.voteend DESC ', array(), DB_FETCHMODE_ASSOC); foreach ($all as $i => $election) { $results = $this->dbh->getAll(' SELECT * FROM election_results WHERE election_id=? ORDER BY votepercent DESC ', array($election['id']), DB_FETCHMODE_ASSOC); $all[$i]['results'] = $results; } } if (!is_array($all)) { return array(); } return $all; } function listAllElections() { if (!$this->user) { return array(); } $all = $this->dbh->getAll(' SELECT e.purpose, e.id FROM elections e, election_handle_votes v WHERE v.election_id = e.id AND v.handle=? ORDER BY e.voteend DESC ', array($this->user), DB_FETCHMODE_ASSOC); if (!is_array($all)) { return array(); } return $all; } function electionExists($id) { return $this->dbh->getOne('SELECT COUNT(id) FROM elections WHERE id=?', array($id)); } function electionInfo($id) { $info = $this->dbh->getAll(' SELECT * FROM elections WHERE id = ? ', array($id), DB_FETCHMODE_ASSOC); if (!is_array($info)) { return false; } $info = $info[0]; $choices = $this->dbh->getAll(' SELECT * FROM election_choices WHERE election_id = ? ORDER BY choice ', array($id), DB_FETCHMODE_ASSOC); $info['choices'] = $choices; $info['results'] = $this->dbh->getAll(' SELECT e.votepercent, e.votetotal, c.choice, c.summary, c.summary_link FROM election_results e, election_choices c WHERE e.election_id = ? AND c.election_id = e.election_id AND c.choice = e.choice ORDER BY e.votetotal DESC ', array($id), DB_FETCHMODE_ASSOC); // calculate winners $order = array(); foreach ($info['results'] as $result) { $order[$result['votetotal']][] = $result; } krsort($order, SORT_NUMERIC); $winners = array(); foreach ($order as $results) { if (count($winners) >= $info['maximum_choices']) { break; // done } foreach ($results as $result) { $winners[] = $result['choice']; } } $info['winners'] = $winners; $abstain = $this->dbh->getOne(' SELECT COUNT(*) FROM election_votes_abstain WHERE election_id=? ', array($id)); $allvoters = $this->dbh->getOne(" SELECT COUNT(DISTINCT k.user) FROM karma k, users u WHERE k.user = u.handle AND k.level in ('pear.dev', 'pear.voter', 'pear.admin')", array()); $votedthis = $this->dbh->getOne(' SELECT count(*) FROM election_handle_votes where election_id=? ', array($id)); $info['turnout'] = $votedthis / $allvoters; $wiki =& new Text_Wiki(); $wiki->disableRule('wikilink'); $info['detail'] = $wiki->transform($info['detail']); if ($info['maximum_choices'] > 1) { $total = $this->dbh->getOne(' SELECT COUNT(*) FROM election_votes_multiple WHERE election_id=? ', array($id)); } else { $total = $this->dbh->getOne(' SELECT COUNT(*) FROM election_votes_single WHERE election_id=? ', array($id)); } // percentage of abstaining voters if ($total + $abstain > 0) { $info['abstain'] = $abstain / ($total + $abstain); $info['abstaincount'] = $abstain; } else { $info['abstain'] = 0; $info['abstaincount'] = 0; } return $info; } function hasVoted($id) { return $this->dbh->getOne(' SELECT COUNT(*) FROM election_handle_votes WHERE election_id=? AND handle=?', array($id, $this->user)); } function canVote($id) { if ($this->hasVoted($id)) { return false; } $info = $this->electionInfo($id); if (strtotime($info['votestart']) - time() > 0) { // election is not active return false; } if (strtotime($info['voteend']) - time() < 0) { // election is finished return false; } if ($info['eligiblevoters'] == 1) { // PEAR developers if ($this->damblan->has($this->user, 'pear.dev')) { return true; } if ($this->damblan->has($this->user, 'pear.admin')) { return true; } return false; } elseif ($info['eligiblevoters'] == 2) { // general PHP public + PEAR developers if ($this->damblan->has($this->user, 'pear.dev')) { return true; } if ($this->damblan->has($this->user, 'pear.admin')) { return true; } if ($this->damblan->has($this->user, 'pear.voter')) { return true; } return false; } return false; } function getVoteSalt() { if (!isset($this->voteSalt)) { $this->voteSalt = date('YmdHis') . mt_rand(1, 999); } return $this->voteSalt; } function vote($id, $votes) { if (!$this->user) { return false; } if (!$this->electionExists($id)) { return false; } if ($this->hasVoted($id)) { return false; } if (!is_array($votes)) { return false; } $info = $this->electionInfo($id); if ($info['maximum_choices'] > 1) { if (count($votes) > $info['maximum_choices'] || count($votes) < $info['minimum_choices']) { return false; } $table = 'election_votes_multiple'; } else { if (count($votes) != 1) { return false; } $table = 'election_votes_single'; } $vote_hash = md5($this->user . $this->getVoteSalt()); PEAR::pushErrorHandling(PEAR_ERROR_RETURN); foreach ($votes as $vote) { $err = $this->dbh->query(' INSERT INTO ' . $table . ' (election_id, vote, vote_hash) VALUES(?,?,?)', array($id, $vote, $vote_hash)); if (PEAR::isError($err)) { // "rollback" the query $this->dbh->query('DELETE FROM ' . $table . ' WHERE election_id=? AND vote_hash=?', array($id, $vote_hash)); PEAR::popErrorHandling(); return false; } } $err = $this->dbh->query(' INSERT INTO election_handle_votes (election_id, handle) VALUES (?,?)', array($id, $this->user)); if (PEAR::isError($err)) { // "rollback" the query $this->dbh->query('DELETE FROM ' . $table . ' WHERE election_id=? AND vote_hash=?', array($id, $vote_hash)); $this->dbh->query('DELETE FROM election_handle_votes WHERE election_id=? AND handle=?', array($id, $this->user)); PEAR::popErrorHandling(); return false; } PEAR::popErrorHandling(); $this->email($info, $votes, $this->getVoteSalt()); return true; } function email($election, $votes, $salt) { $info = user::info($this->user); $email = '"' . $info['name'] . '" <' . $info['email'] . '>'; $headers = "From: bounce-no-user@php.net\n"; $headers .= "X-Mailer: PEAR election voting interface\n"; $headers .= "X-PEAR-Election: " . $election['id'] . "\n"; $subject = '[PEAR-ELECTION] Your vote in election ' . $election['purpose']; if ($votes) { $text = 'Your vote for the election: ' . $election['purpose'] . "\n" . 'has been registered. You voted for: '; $text .= implode("\n", $votes) . "\n"; $text .= 'Your vote salt is ' . $salt . "\n"; $text .= 'this is your only record of the vote salt, without it your vote ' . 'cannot be retrieved. Thank you for voting'; } else { $text = 'Your abstaining vote for the election: ' . $election['purpose'] . "\n" . 'has been registered.' . "\n"; $text .= 'Your vote salt is ' . $salt . "\n"; $text .= 'this is your only record of the vote salt, without it your vote ' . 'cannot be retrieved. Thank you for voting'; } $text .= "\nVisit http://pear.php.net/election.php to retrieve your vote"; $res = mail($email, $subject, $text, $headers, '-f bounce-no-user@php.net'); return $res; } function abstain($id) { if (!$this->user) { return false; } if (!$this->electionExists($id)) { return false; } if ($this->hasVoted($id)) { return false; } $vote_hash = md5($this->user . $this->getVoteSalt()); PEAR::pushErrorHandling(PEAR_ERROR_RETURN); $err = $this->dbh->query(' INSERT INTO election_votes_abstain (election_id, vote_hash) VALUES (?,?)', array($id, $vote_hash)); if (PEAR::isError($err)) { PEAR::popErrorHandling(); return false; } $err = $this->dbh->query(' INSERT INTO election_handle_votes (election_id, handle) VALUES (?,?)', array($id, $this->user)); if (PEAR::isError($err)) { $this->dbh->query('DELETE FROM election_votes_abstain WHERE election_id=? and vote_hash=?', array($id, $vote_hash)); PEAR::popErrorHandling(); return false; } PEAR::popErrorHandling(); $this->email($this->electionInfo($id), false, $this->getVoteSalt()); return true; } function retrieveVote($id, $salt) { if (!$this->electionExists($id)) { return false; } if (!$this->user) { return false; } if (!$this->hasVoted($id)) { return false; } $this->voteSalt = md5($this->user . $salt); if ($vote = $this->dbh->getOne(' SELECT * FROM election_votes_abstain WHERE election_id=? AND vote_hash=?', array($id, $this->voteSalt))) { return array('(abstain)'); } if ($vote = $this->dbh->getOne(' SELECT choice FROM election_votes_single e, election_choices c WHERE e.election_id=? AND e.vote_hash=? AND e.election_id = c.election_id AND e.vote = c.choice', array($id, $this->voteSalt))) { return array($vote); } $votes = $this->dbh->getAll(' SELECT choice FROM election_votes_multiple e, election_choices c WHERE e.election_id=? AND e.vote_hash=? AND e.election_id = c.election_id AND e.vote = c.choice', array($id, $this->voteSalt)); $ret = array(); foreach ($votes as $vote) { $ret[] = $vote[0]; } if (!$ret) { return false; } return $ret; } }