cosmetic fix around "Proposed"
[cacert-boardvoting.git] / database.php
1 <?php
2 class DB {
3 //var $board = "cacert-board@lists.cacert.org";
4 var $board = "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.quorum, decisions.majority, 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.quorum AS quorum, decisions.majority AS majority, 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.quorum, decisions.majority, 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.quorum, decisions.majority, 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.quorum, decisions.majority, 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.quorum, decisions.majority, 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['del vote'] = $this->dbh->prepare("DELETE FROM votes WHERE decision=:decision AND voter=:voter;");
19 $this->statement['do vote'] = $this->dbh->prepare("INSERT INTO votes (decision, voter, vote, voted, notes) VALUES (:decision, :voter, :vote, datetime('now','utc'), :notes);");
20 $this->statement['stats'] = $this->dbh->prepare("SELECT COUNT(*) AS voters FROM voters WHERE enabled=1;");
21 $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;");
22 $this->statement['create decision'] = $this->dbh->prepare("INSERT INTO decisions (proposed, proponent, title, content, quorum, majority, status, due, modified) VALUES (datetime('now','utc'), :proponent, :title, :content, :quorum, :majority, 0, datetime('now','utc', :due), datetime('now','utc'));");
23 $this->statement['post create'] = $this->dbh->prepare(" UPDATE decisions SET tag='m' || strftime('%Y%m%d','now') || '.' || id WHERE id=last_insert_rowid();");
24 $this->statement['update decision'] = $this->dbh->prepare("UPDATE decisions SET proposed=datetime('now','utc'), proponent=:proponent, title=:title, content=:content, quorum=:quorum, majority=:majority, 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 }
27 function getStatement($name) {
28 return $this->statement[$name];
29 }
30 function closeVotes() {
31 $stmt = $this->getStatement("closed decisions");
32 $upd = $this->getStatement("close decision");
33 if ($stmt->execute()) {
34 while ($decision = $stmt->fetch()) {
35 $votes = $decision['ayes'] + $decision['nayes'] + $decision['abstains'];
36 if ($votes < $decision['quorum']) {
37 $decision['status'] = -1;
38 } else {
39 $votes = $decision['ayes'] + $decision['nayes'];
40 if (($decision['ayes'] / $votes) >= ($decision['majority'] / 100)) {
41 $decision['status'] = 1;
42 } else {
43 $decision['status'] = -1;
44 }
45 }
46 $upd->bindParam(":decision",$decision['id']);
47 $upd->bindParam(":status",$decision['status']);
48 $upd->execute();
49 $state = $decision['status']==1?"accepted":"declined";
50 $tag = $decision['tag'];
51 $title = $decision['title'];
52 $content = $decision['content'];
53 $quorum = $decision['quorum'];
54 $majority = $decision['majority'];
55 $ayes = $decision['ayes'];
56 $nayes = $decision['nayes'];
57 $abstains = $decision['abstains'];
58 $totalvotes = $decision['ayes']+$decision['nayes'];
59 if ($totalvotes <= 0) $percent = 0;
60 else $percent = $decision['ayes'] * 100 / $totalvotes;
61 $body = <<<BODY
62 Dear Board,
63
64 The motion with the identifier $tag has been $state.
65
66 Motion:
67 $title
68 $content
69
70 Votes:
71 Quorum: $quorum
72 Majority: $majority%
73
74 Ayes: $ayes
75 Nayes: $nayes
76 Abstentions: $abstains
77
78 Percentage: $percent%
79
80 Kind regards,
81 the voting system.
82
83 BODY;
84 $this->notify("Re: ".$decision['tag']." - ".$decision['title']." - finalised",$body,$decision['tag']);
85 }
86 }
87 }
88 function notify($subject,$body,$tag,$first=FALSE)
89 {
90 if ($first) {
91 $header = "Message-id: <".$tag.">\r\n";
92 } else {
93 $header = "References: <".$tag.">\r\nIn-reply-to: <".$tag.">\r\n";
94 }
95 mail($this->board,$subject,$body,$header."From: Voting System <returns@cacert.org>");
96 //mail("testsympa@lists.cacert.org",$subject,$body,"From: Voting System <returns@cacert.org>");
97 }
98 function auth()
99 {
100 $stmt = $this->getStatement("get voter");
101 $stmt->execute(array($_SERVER['REMOTE_USER']));
102 $user = $stmt->fetch();
103 if ($user) return $user;
104 if ($_SERVER['SSL_CLIENT_S_DN_EMAIL']) {
105 $stmt->execute(array($_SERVER['SSL_CLIENT_S_DN_EMAIL']));
106 $user = $stmt->fetch();
107 if ($user) return $user;
108 }
109 $d=0;
110 while ($email=$_SERVER["SSL_CLIENT_S_DN_EMAIL_$d"]) {
111 $stmt->execute(array($email));
112 $user = $stmt->fetch();
113 if ($user) return $user;
114 ++$d;
115 }
116 if (preg_match_all('/\/emailAddress=([^\/]*)/',$dn,$reg,PREG_SET_ORDER)) {
117 foreach ($reg as $emailarr) {
118 $stmt->execute(array($emailarr[1]));
119 $user = $stmt->fetch();
120 if ($user) return $user;
121 }
122 }
123 if ($_SERVER['SSL_CLIENT_CERT']) {
124 # subjectAltName unpresented by Apache http://httpd.apache.org/docs/trunk/mod/mod_ssl.html
125 # subjectAltName http://tools.ietf.org/html/rfc5280#section-4.2.1.6
126 # WARNING WARNING openssl_x509_parse is an unstable PHP API
127 $x509 = openssl_x509_parse($_SERVER['SSL_CLIENT_CERT']);
128 $subjectAltName = $x509['extensions']['subjectAltName']; // going off https://foaf.me/testSSL.php
129 #print_r(split("[, ]",$subjectAltName));
130 #print_r($x509);
131 #echo $subjectAltName;
132 if (preg_match_all('/email:([^, ]*)/',$subjectAltName,$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 }
140 return FALSE;
141 }
142 }
143 ?>