summaryrefslogtreecommitdiffstats
path: root/web/lib/acctfuncs.inc.php
diff options
context:
space:
mode:
authorLukas Fleischer <lfleischer@archlinux.org>2017-04-27 09:24:11 +0200
committerLukas Fleischer <lfleischer@archlinux.org>2017-04-30 16:47:13 +0200
commita8ac2004d3f25877d9e7b4fa58f10009c39f8acf (patch)
tree968cb95c2e7617608f15ccece4823ea4006c59cf /web/lib/acctfuncs.inc.php
parent6892ec7791bf04361ac2973b38d0025b50fa4727 (diff)
downloadaur-a8ac2004d3f25877d9e7b4fa58f10009c39f8acf.tar.gz
aur-a8ac2004d3f25877d9e7b4fa58f10009c39f8acf.tar.xz
Add support for Terms of Service documents
This allows for adding Terms of Service documents to the database that registered users need to accept before using the AUR. A revision field can be used to indicate whether a document was updated. If it is increased, all users are again asked to accept the new terms. Signed-off-by: Lukas Fleischer <lfleischer@archlinux.org>
Diffstat (limited to 'web/lib/acctfuncs.inc.php')
-rw-r--r--web/lib/acctfuncs.inc.php74
1 files changed, 74 insertions, 0 deletions
diff --git a/web/lib/acctfuncs.inc.php b/web/lib/acctfuncs.inc.php
index 22b3ca8d..e45d735b 100644
--- a/web/lib/acctfuncs.inc.php
+++ b/web/lib/acctfuncs.inc.php
@@ -1325,3 +1325,77 @@ function notify($params) {
return proc_close($p);
}
+
+/*
+ * Obtain a list of terms a given user has not yet accepted.
+ *
+ * @param int $uid The ID of the user to obtain terms for.
+ *
+ * @return array A list of terms the user has not yet accepted.
+ */
+function fetch_updated_terms($uid) {
+ $dbh = DB::connect();
+
+ $q = "SELECT ID, Terms.Revision, Description, URL ";
+ $q .= "FROM Terms LEFT JOIN AcceptedTerms ";
+ $q .= "ON AcceptedTerms.TermsID = Terms.ID ";
+ $q .= "AND AcceptedTerms.UsersID = " . intval($uid) . " ";
+ $q .= "WHERE AcceptedTerms.Revision IS NULL OR ";
+ $q .= "AcceptedTerms.Revision < Terms.Revision";
+
+ $result = $dbh->query($q);
+
+ if ($result) {
+ return $result->fetchAll();
+ } else {
+ return array();
+ }
+}
+
+/*
+ * Accept a list of given terms.
+ *
+ * @param int $uid The ID of the user to accept the terms.
+ * @param array $termrev An array mapping each term to the accepted revision.
+ *
+ * @return void
+ */
+function accept_terms($uid, $termrev) {
+ $dbh = DB::connect();
+
+ $q = "SELECT TermsID, Revision FROM AcceptedTerms ";
+ $q .= "WHERE UsersID = " . intval($uid);
+
+ $result = $dbh->query($q);
+
+ if (!$result) {
+ return;
+ }
+
+ $termrev_update = array();
+ while ($row = $result->fetch(PDO::FETCH_ASSOC)) {
+ $id = $row['TermsID'];
+ if (!array_key_exists($id, $termrev)) {
+ continue;
+ }
+ if ($row['Revision'] < $termrev[$id]) {
+ $termrev_update[$id] = $termrev[$id];
+ }
+ }
+ $termrev_add = array_diff_key($termrev, $termrev_update);
+
+ foreach ($termrev_add as $id => $rev) {
+ $q = "INSERT INTO AcceptedTerms (TermsID, UsersID, Revision) ";
+ $q .= "VALUES (" . intval($id) . ", " . intval($uid) . ", ";
+ $q .= intval($rev) . ")";
+ $dbh->exec($q);
+ }
+
+ foreach ($termrev_update as $id => $rev) {
+ $q = "UPDATE AcceptedTerms ";
+ $q .= "SET Revision = " . intval($rev) . " ";
+ $q .= "WHERE TermsID = " . intval($id) . " AND ";
+ $q .= "UsersID = " . intval($uid);
+ $dbh->exec($q);
+ }
+}