Enable conference registration

Change-Id: I5e973513d78c63a48e62ff1aa3a5d9a1621f4a4d
diff --git a/db_backend.php b/db_backend.php
index 5a92dc0..e1be86e 100644
--- a/db_backend.php
+++ b/db_backend.php
@@ -5,74 +5,74 @@
 
 use \User as User;
 
-function open_db() {
-    global $DB, $DB_USER, $DB_PASS;
-    try {
-        # MS SQL Server and Sybase with PDO_DBLIB 
-        $DBH = new PDO($DB, $DB_USER, $DB_PASS);
-    }
-    catch(PDOException $e) {
-        echo $e->getMessage();
-    }
-    return $DBH;
-}
-
-function db_add_user(User $user)
+class DB
 {
-    $DBH = open_db();
-    $columns = getColumnNames($DBH, "person");
-    foreach ((array) $user as $key => $value) {
-        if (array_key_exists($key, (array) $columns)) {
-            $a[$key] = $value;
+
+    public $pdo;
+    public $log;
+
+    function __construct($log)
+    {
+        global $DB, $DB_USER, $DB_PASS;
+        $this->log = $log;
+        try {
+            $this->pdo = new PDO($DB, $DB_USER, $DB_PASS);
+            if($log)
+                $log->info("Connected to database $DB");
+        } catch (PDOException $e) {
+            echo $e->getMessage();
+            if($log)
+                $log->error($e->getMessage());
         }
     }
-    $keys = array_keys($a);
-    $sql = "INSERT INTO person (".implode(", ",$keys).") \n";
-    $sql .= "VALUES ( :".implode(", :",$keys).")";
-    $q = $DBH->prepare($sql);
-    $i=1;
-    foreach($a as $value)
-        $q->bindParam($i++, $value);
-    print $sql. "\n";
-    return $q->execute($a);
-}
 
-function getColumnNames($dbh, $table)
-{
-    $sql = "SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE table_name = :table";
-    try {
-        $stmt = $dbh->prepare($sql);
-        $stmt->bindValue(':table', $table, PDO::PARAM_STR);
-        $stmt->execute();
-        $output = array();
-        while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
-            $output[$row['COLUMN_NAME']] = $row['COLUMN_NAME'];
+    function add_user(User $user)
+    {
+        $columns = $this->getColumnNames("person");
+        foreach ((array) $user as $key => $value) {
+            if (array_key_exists($key, (array) $columns)) {
+                $a[$key] = $value;
+            }
         }
-        return $output;
-    } catch (PDOException $pe) {
-        trigger_error('Could not connect to MySQL database. ' . $pe->getMessage(), E_USER_ERROR);
+        $keys = array_keys($a);
+        $sql = "INSERT INTO person (" . implode(", ", $keys) . ") \n";
+        $sql .= "VALUES ( :" . implode(", :", $keys) . ")";
+        $q = $this->pdo->prepare($sql);
+        $i = 1;
+        foreach ($a as $value)
+            $q->bindParam($i++, $value);
+        $this->log->debug($sql);
+        return $q->execute($a);
+    }
+
+    function getColumnNames($table)
+    {
+        $sql = "SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE table_name = :table";
+        try {
+            $stmt = $this->pdo->prepare($sql);
+            $stmt->bindValue(':table', $table, PDO::PARAM_STR);
+            $stmt->execute();
+            $output = array();
+            while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
+                $output[$row['COLUMN_NAME']] = $row['COLUMN_NAME'];
+            }
+            return $output;
+        } catch (PDOException $pe) {
+            trigger_error('Could not connect to MySQL database. ' . $pe->getMessage(), E_USER_ERROR);
+        }
+    }
+
+    function mail_count($email)
+    {
+        $sql = "SELECT COUNT(*) FROM person WHERE email = :email";
+        try {
+            $stmt = $this->pdo->prepare($sql);
+            $stmt->bindValue(':email', $email, PDO::PARAM_STR);
+            $stmt->execute();
+            $row = $stmt->fetch(PDO::FETCH_ASSOC);
+            return $row['COUNT(*)'];
+        } catch (PDOException $pe) {
+            trigger_error('Could not connect to MySQL database. ' . $pe->getMessage(), E_USER_ERROR);
+        }
     }
 }
-
-try {
-    # MS SQL Server and Sybase with PDO_DBLIB 
-    $DBH = new PDO($DB, $DB_USER, $DB_PASS);
-    $table = "person";
-    $sql = "SHOW COLUMNS FROM " . $table;
-    $stmt = $DBH->prepare($sql);
-    # $stmt->bindValue(':table', $table, PDO::PARAM_STR);
-    $stmt->execute();
-    $output = array();
-    while($row = $stmt->fetch(PDO::FETCH_NUM)){
-        $output[] = $row[0];
-        print $row[0] . "\n";
-        }
-    }
-  catch(PDOException $e) {
-      echo $e->getMessage();
-  }
-
-  $user=new User();
-  $user->username="test";
-  print $user->to_string();
-  db_add_user($user);