Marc Kupietz | ab11a14 | 2023-03-09 08:15:35 +0100 | [diff] [blame] | 1 | <?php |
| 2 | |
| 3 | include_once "User.php"; |
| 4 | include_once "config.php"; |
| 5 | |
| 6 | use \User as User; |
| 7 | |
Marc Kupietz | 87a1431 | 2023-03-09 20:39:31 +0100 | [diff] [blame] | 8 | class DB |
Marc Kupietz | ab11a14 | 2023-03-09 08:15:35 +0100 | [diff] [blame] | 9 | { |
Marc Kupietz | 87a1431 | 2023-03-09 20:39:31 +0100 | [diff] [blame] | 10 | |
| 11 | public $pdo; |
| 12 | public $log; |
| 13 | |
| 14 | function __construct($log) |
| 15 | { |
| 16 | global $DB, $DB_USER, $DB_PASS; |
| 17 | $this->log = $log; |
| 18 | try { |
Marc Kupietz | ce67631 | 2023-03-10 08:29:41 +0100 | [diff] [blame^] | 19 | $this->pdo = new PDO($DB, $DB_USER, $DB_PASS, array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8") ); |
Marc Kupietz | 87a1431 | 2023-03-09 20:39:31 +0100 | [diff] [blame] | 20 | if($log) |
| 21 | $log->info("Connected to database $DB"); |
| 22 | } catch (PDOException $e) { |
| 23 | echo $e->getMessage(); |
| 24 | if($log) |
| 25 | $log->error($e->getMessage()); |
Marc Kupietz | ab11a14 | 2023-03-09 08:15:35 +0100 | [diff] [blame] | 26 | } |
| 27 | } |
Marc Kupietz | ab11a14 | 2023-03-09 08:15:35 +0100 | [diff] [blame] | 28 | |
Marc Kupietz | 87a1431 | 2023-03-09 20:39:31 +0100 | [diff] [blame] | 29 | function add_user(User $user) |
| 30 | { |
| 31 | $columns = $this->getColumnNames("person"); |
| 32 | foreach ((array) $user as $key => $value) { |
| 33 | if (array_key_exists($key, (array) $columns)) { |
| 34 | $a[$key] = $value; |
| 35 | } |
Marc Kupietz | ab11a14 | 2023-03-09 08:15:35 +0100 | [diff] [blame] | 36 | } |
Marc Kupietz | ce67631 | 2023-03-10 08:29:41 +0100 | [diff] [blame^] | 37 | $a["participation_confirmed_at"] = date("Y-m-d H:i:s UTC"); |
Marc Kupietz | 87a1431 | 2023-03-09 20:39:31 +0100 | [diff] [blame] | 38 | $keys = array_keys($a); |
| 39 | $sql = "INSERT INTO person (" . implode(", ", $keys) . ") \n"; |
| 40 | $sql .= "VALUES ( :" . implode(", :", $keys) . ")"; |
| 41 | $q = $this->pdo->prepare($sql); |
| 42 | $i = 1; |
| 43 | foreach ($a as $value) |
| 44 | $q->bindParam($i++, $value); |
| 45 | $this->log->debug($sql); |
Marc Kupietz | ce67631 | 2023-03-10 08:29:41 +0100 | [diff] [blame^] | 46 | $res = $q->execute($a); |
| 47 | $user->id = $this->pdo->lastInsertId(); |
| 48 | return $res; |
Marc Kupietz | 87a1431 | 2023-03-09 20:39:31 +0100 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | function getColumnNames($table) |
| 52 | { |
| 53 | $sql = "SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE table_name = :table"; |
| 54 | try { |
| 55 | $stmt = $this->pdo->prepare($sql); |
| 56 | $stmt->bindValue(':table', $table, PDO::PARAM_STR); |
| 57 | $stmt->execute(); |
| 58 | $output = array(); |
| 59 | while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) { |
| 60 | $output[$row['COLUMN_NAME']] = $row['COLUMN_NAME']; |
| 61 | } |
| 62 | return $output; |
| 63 | } catch (PDOException $pe) { |
| 64 | trigger_error('Could not connect to MySQL database. ' . $pe->getMessage(), E_USER_ERROR); |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | function mail_count($email) |
| 69 | { |
| 70 | $sql = "SELECT COUNT(*) FROM person WHERE email = :email"; |
| 71 | try { |
| 72 | $stmt = $this->pdo->prepare($sql); |
| 73 | $stmt->bindValue(':email', $email, PDO::PARAM_STR); |
| 74 | $stmt->execute(); |
| 75 | $row = $stmt->fetch(PDO::FETCH_ASSOC); |
| 76 | return $row['COUNT(*)']; |
| 77 | } catch (PDOException $pe) { |
| 78 | trigger_error('Could not connect to MySQL database. ' . $pe->getMessage(), E_USER_ERROR); |
| 79 | } |
Marc Kupietz | ab11a14 | 2023-03-09 08:15:35 +0100 | [diff] [blame] | 80 | } |
| 81 | } |