932e6a4aa7ce774f4cff11a9fe2e30743b1abc53
[cacert-boardvoting.git] / database.php
1 <?php
2 class DB {
3 var $board = "cacert-board@lists.cacert.org";
4 var $notices = "testsympa@lists.cacert.org";
5
6 function __construct() {
7 $this->dbh = new PDO("sqlite:".dirname(__FILE__)."/database.sqlite");
8 $this->statement = array();
9 $this->statement['list decisions'] = $this->dbh->prepare("SELECT decisions.id AS id, decisions.tag AS tag, voters.name AS proposer, decisions.proposed, decisions.title, decisions.content, decisions.votetype, decisions.status, decisions.due, decisions.modified, (SELECT COUNT(*) FROM votes WHERE decision=decisions.id AND vote=1) AS ayes, (SELECT COUNT(*) FROM votes WHERE decision=decisions.id AND vote=-1) AS nayes, (SELECT COUNT(*) FROM votes WHERE decision=decisions.id AND vote=0) AS abstains FROM decisions, voters WHERE decisions.proponent=voters.id ORDER BY proposed DESC LIMIT 10 OFFSET 10 * (:page - 1);");
10 $this->statement['list my unvoted decisions'] = $this->dbh->prepare("SELECT * FROM (SELECT decisions.id AS id, decisions.tag AS tag, voters.name AS proposer,decisions.proposed AS proposed, decisions.title AS title, decisions.content AS content, decisions.votetype AS votetype, decisions.status AS status, decisions.due AS due, decisions.modified AS modified,(SELECT COUNT(*) AS ayes FROM votes WHERE decision=decisions.id AND vote=1), (SELECT COUNT(*) AS nayes FROM votes WHERE decision=decisions.id AND vote=-1), (SELECT COUNT(*) AS abstains FROM votes WHERE decision=decisions.id AND vote=0) FROM decisions, voters WHERE decisions.proponent=voters.id AND decisions.status=0) WHERE NOT EXISTS (SELECT vote FROM votes WHERE votes.decision=id AND votes.voter=:id) ORDER BY proposed DESC LIMIT 10 OFFSET 10 * (:page - 1);");
11 $this->statement['list decision'] = $this->dbh->prepare("SELECT decisions.id AS id, decisions.tag AS tag, voters.name AS proposer, decisions.proposed, decisions.title, decisions.content, decisions.votetype, decisions.status, decisions.due, decisions.modified, (SELECT COUNT(*) FROM votes WHERE decision=decisions.id AND vote=1) AS ayes, (SELECT COUNT(*) FROM votes WHERE decision=decisions.id AND vote=-1) AS nayes, (SELECT COUNT(*) FROM votes WHERE decision=decisions.id AND vote=0) AS abstains FROM decisions, voters WHERE decisions.proponent=voters.id AND decisions.tag=:id ORDER BY proposed DESC;");
12 $this->statement['closed decisions'] = $this->dbh->prepare("SELECT decisions.id, decisions.tag, voters.name AS proposer, decisions.proposed, decisions.title, decisions.content, decisions.votetype, decisions.status, decisions.due, decisions.modified, (SELECT COUNT(*) FROM votes WHERE decision=decisions.id AND vote=1) AS ayes, (SELECT COUNT(*) FROM votes WHERE decision=decisions.id AND vote=-1) AS nayes, (SELECT COUNT(*) FROM votes WHERE decision=decisions.id AND vote=0) AS abstains FROM decisions, voters WHERE decisions.proponent=voters.id AND decisions.status=0 AND datetime('now','utc') > datetime(due);");
13 $this->statement['get decision'] = $this->dbh->prepare("SELECT decisions.id, decisions.tag, decisions.proponent, voters.name AS proposer, decisions.proposed, decisions.title, decisions.content, decisions.votetype, decisions.status, decisions.due, decisions.modified, (SELECT COUNT(*) FROM votes WHERE decision=decisions.id AND vote=1) AS ayes, (SELECT COUNT(*) FROM votes WHERE decision=decisions.id AND vote=-1) AS nayes, (SELECT COUNT(*) FROM votes WHERE decision=decisions.id AND vote=0) AS abstains FROM decisions, voters WHERE decisions.proponent=voters.id AND decisions.id=:decision;");
14 $this->statement['get new decision'] = $this->dbh->prepare("SELECT decisions.id, decisions.tag, decisions.proponent, voters.name AS proposer, decisions.proposed, decisions.title, decisions.content, decisions.votetype, decisions.status, decisions.due, decisions.modified, (SELECT COUNT(*) FROM votes WHERE decision=decisions.id AND vote=1) AS ayes, (SELECT COUNT(*) FROM votes WHERE decision=decisions.id AND vote=-1) AS nayes, (SELECT COUNT(*) FROM votes WHERE decision=decisions.id AND vote=0) AS abstains FROM decisions, voters WHERE decisions.proponent=voters.id AND decisions.id=last_insert_rowid();");
15 $this->statement['get voter'] = $this->dbh->prepare("SELECT voters.id, voters.name FROM voters, emails WHERE voters.id=emails.voter AND emails.address=? AND voters.enabled=1");
16 $this->statement['get voter by id'] = $this->dbh->prepare("SELECT voters.id, voters.name FROM voters WHERE id=:id;");
17 $this->statement['get voters'] = $this->dbh->prepare("SELECT voters.id, voters.name FROM voters WHERE voters.enabled=1 ORDER BY name ASC;");
18 $this->statement['get reminder voters'] = $this->dbh->prepare("SELECT voters.id, voters.name, voters.reminder AS email FROM voters WHERE voters.enabled=1 AND voters.reminder!='' ORDER BY name ASC;");
19 $this->statement['del vote'] = $this->dbh->prepare("DELETE FROM votes WHERE decision=:decision AND voter=:voter;");
20 $this->statement['do vote'] = $this->dbh->prepare("INSERT INTO votes (decision, voter, vote, voted, notes) VALUES (:decision, :voter, :vote, datetime('now','utc'), :notes);");
21 $this->statement['stats'] = $this->dbh->prepare("SELECT COUNT(*) AS voters FROM voters WHERE enabled=1;");
22 $this->statement['list votes'] = $this->dbh->prepare("SELECT voters.name AS name, votes.vote AS vote FROM voters,votes WHERE voters.id=votes.voter AND votes.decision=:id;");
23 $this->statement['create decision'] = $this->dbh->prepare("INSERT INTO decisions (proposed, proponent, title, content, votetype, status, due, modified,tag) VALUES (datetime('now','utc'), :proponent, :title, :content, :votetype, 0, datetime('now','utc', :due), datetime('now','utc'),'m' || strftime('%Y%m%d','now') || '.' || (select count(*)+1 as num from decisions where proposed between date('now') and date('now','1 day')));");
24 $this->statement['update decision'] = $this->dbh->prepare("UPDATE decisions SET proposed=datetime('now','utc'), proponent=:proponent, title=:title, content=:content, votetype=:votetype, status=0, due=datetime('now','utc',:due), modified=datetime('now','utc') WHERE id=:id;");
25 $this->statement['close decision'] = $this->dbh->prepare("UPDATE decisions SET status=:status, modified=datetime('now','utc') WHERE id=:decision");
26 ini_set('mbstring.internal_encoding', 'UTF-8');
27 }
28 function getStatement($name) {
29 return $this->statement[$name];
30 }
31 function closeVotes() {
32 $stmt = $this->getStatement("closed decisions");
33 $upd = $this->getStatement("close decision");
34 if ($stmt->execute()) {
35 while ($decision = $stmt->fetch()) {
36 switch ($decision['votetype']) {
37 case 0: // motion
38 $quorum = 3; $majority = 50; break;
39 case 1: // veto
40 default:
41 $quorum = 1; $majority = 99; break;
42 }
43 $votes = $decision['ayes'] + $decision['nayes'] + $decision['abstains'];
44 if ($votes < $quorum) {
45 $decision['status'] = -1;
46 } else {
47 $votes = $decision['ayes'] + $decision['nayes'];
48 if (($decision['ayes'] / $votes) > ($majority / 100)) {
49 $decision['status'] = 1;
50 } else {
51 $decision['status'] = -1;
52 }
53 }
54 $upd->bindParam(":decision",$decision['id']);
55 $upd->bindParam(":status",$decision['status']);
56 $upd->execute();
57 $state = $decision['status']==1?"accepted":"declined";
58 $tag = $decision['tag'];
59 $title = $decision['title'];
60 $content = $decision['content'];
61 $votetype = !$decision['votetype']?'motion':'veto';
62 $ayes = $decision['ayes'];
63 $nayes = $decision['nayes'];
64 $abstains = $decision['abstains'];
65 $totalvotes = $decision['ayes']+$decision['nayes'];
66 if ($totalvotes <= 0) $percent = 0;
67 else $percent = $decision['ayes'] * 100 / $totalvotes;
68 $body = <<<BODY
69 Dear Board,
70
71 The motion with the identifier $tag has been $state.
72
73 Motion:
74 $title
75 $content
76
77 Vote type: $votetype
78
79 Ayes: $ayes
80 Nayes: $nayes
81 Abstentions: $abstains
82
83 Percentage: $percent%
84
85 Kind regards,
86 the voting system.
87
88 BODY;
89 $this->notify("Re: ".$decision['tag']." - ".$decision['title']." - finalised",$body,$decision['tag']);
90 }
91 }
92 }
93 function notify($subject,$body,$tag,$first=FALSE)
94 {
95 $header = "Content-Type: text/plain; charset=UTF-8\r\n";
96 if ($first) {
97 $header .= "Message-id: <".$tag.">\r\n";
98 } else {
99 $header .= "References: <".$tag.">\r\nIn-reply-to: <".$tag.">\r\n";
100 }
101 mail($this->board, mb_encode_mimeheader($subject,"UTF-8", "B", "\n"),$body,$header."From: Voting System <returns@cacert.org>");
102 }
103 function vote_notify($subject,$body,$tag)
104 {
105 $header = "Content-Type: text/plain; charset=UTF-8\r\n";
106 $header .= "References: <".$tag.">\r\nIn-reply-to: <".$tag.">\r\n";
107 mail($this->notices, mb_encode_mimeheader($subject,"UTF-8", "B", "\n"),$body,$header."From: Voting System <returns@cacert.org>");
108 }
109 function remind_notify($email,$subject,$body)
110 {
111 $header = "Content-Type: text/plain; charset=UTF-8\r\n";
112 mail($email,$subject,$body,$header."From: Voting System <returns@cacert.org>");
113 }
114 function auth()
115 {
116 $stmt = $this->getStatement("get voter");
117 $stmt->execute(array($_SERVER['REMOTE_USER']));
118 $user = $stmt->fetch();
119 if ($user) return $user;
120 if ($_SERVER['SSL_CLIENT_S_DN_EMAIL']) {
121 $stmt->execute(array($_SERVER['SSL_CLIENT_S_DN_EMAIL']));
122 $user = $stmt->fetch();
123 if ($user) return $user;
124 }
125 $d=0;
126 while ($email=$_SERVER["SSL_CLIENT_S_DN_EMAIL_$d"]) {
127 $stmt->execute(array($email));
128 $user = $stmt->fetch();
129 if ($user) return $user;
130 ++$d;
131 }
132 if (preg_match_all('/\/emailAddress=([^\/]*)/',$dn,$reg,PREG_SET_ORDER)) {
133 foreach ($reg as $emailarr) {
134 $stmt->execute(array($emailarr[1]));
135 $user = $stmt->fetch();
136 if ($user) return $user;
137 }
138 }
139 if ($_SERVER['SSL_CLIENT_CERT']) {
140 # subjectAltName unpresented by Apache http://httpd.apache.org/docs/trunk/mod/mod_ssl.html
141 # subjectAltName http://tools.ietf.org/html/rfc5280#section-4.2.1.6
142 # WARNING WARNING openssl_x509_parse is an unstable PHP API
143 $x509 = openssl_x509_parse($_SERVER['SSL_CLIENT_CERT']);
144 $subjectAltName = $x509['extensions']['subjectAltName']; // going off https://foaf.me/testSSL.php
145 #print_r(split("[, ]",$subjectAltName));
146 #print_r($x509);
147 #echo $subjectAltName;
148 if (preg_match_all('/email:([^, ]*)/',$subjectAltName,$reg,PREG_SET_ORDER)) {
149 foreach ($reg as $emailarr) {
150 $stmt->execute(array($emailarr[1]));
151 $user = $stmt->fetch();
152 if ($user) return $user;
153 }
154 }
155 }
156 return FALSE;
157 }
158 }
159 ?>