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