blob: 5fb7307d49b862bcc4fc23a0082aa18a86f9dc27 [file] [log] [blame]
Marc Kupietzd871d882023-03-05 18:34:16 +01001<?php
2require_once 'vendor/autoload.php';
3
4class User
5{
Marc Kupietz3417c882023-03-07 12:03:21 +01006 public $username;
7 public $first_name;
8 public $last_name;
9 public $organization;
10 public $user_hash;
11 public $password;
12 public $street;
13 public $zip;
14 public $city;
15 public $country;
16 public $email;
17 public $phone;
18 public bool $eula_signed;
19 public bool $privacy_policy_signed;
20
21 function __construct()
22 {
23 $this->username = "";
24 $this->first_name = "";
25 $this->last_name = "";
26 $this->email = "";
27 $this->organization = "";
28 $this->user_hash = "";
29 $this->password = "";
30 $this->city = "";
31 $this->country = "";
32 $this->zip = "";
33 $this->street = "";
34 $this->phone = "";
35 $this->eula_signed = false;
36 $this->privacy_policy_signed = false;
37 }
38
39 function __construct1(object $data)
40 {
41 foreach ($data as $key => $value) {
42 if (array_key_exists($key, (array) $this))
43 $this->{$key} = $value;
Marc Kupietza57befa2023-03-06 21:39:23 +010044 }
Marc Kupietz3417c882023-03-07 12:03:21 +010045 if (isset($data->password)) $this->set_password($data->password);
46 }
Marc Kupietzd871d882023-03-05 18:34:16 +010047
Marc Kupietz3417c882023-03-07 12:03:21 +010048 function init_from_object(object $data)
49 {
50 foreach ($data as $key => $value) {
51 if (array_key_exists($key, (array) $this))
52 $this->{$key} = $value;
Marc Kupietza57befa2023-03-06 21:39:23 +010053 }
Marc Kupietz3417c882023-03-07 12:03:21 +010054 if (isset($data->password)) $this->set_password($data->password);
55 }
Marc Kupietza57befa2023-03-06 21:39:23 +010056
Marc Kupietz3417c882023-03-07 12:03:21 +010057 function init_from_array(array $data)
58 {
59 foreach ($data as $key => $value) {
60 if (array_key_exists($key, (array) $this)) {
61 if (is_bool(gettype($this->{$key})) && is_string($value))
62 $this->{$key} = !empty($value);
63 else
64 $this->{$key} = $value;
65 }
66 }
67 if (!empty($data["password"])) $this->set_password($data["password"]);
68 }
69
70 function __construct6(string $username, string $first_name, string $last_name, string $email, string $password, string $organization)
71 {
72 include 'config.php';
73 $this->username = $username;
74 $this->first_name = $first_name;
75 $this->last_name = $last_name;
76 $this->email = $email;
77 $this->organization = $organization;
78 $this->set_password($password);
79 }
80
81 function set_password(string $password)
82 {
83 include 'config.php';
84 if ($ENCRYPT_PASSWORDS) {
85 $this->user_hash = "{crypt}" . crypt($password, '$6$' . generateSalt(10) . '$');
86 # $this->user_hash = "{SHA}" . base64_encode(sha1($password, true));
87 } else {
88 $this->user_hash = "{CLEAR}" . $password;
89 }
90 $this->password = $this->user_hash;
91 }
92
93 function to_string()
94 {
95 $tmp_user = $this;
96 $tmp_user->password = "***";
97 $tmp_user->user_hash = "***";
98 return json_encode($tmp_user);
99 }
100
101 function backup_in_session()
102 {
103 global $_SESSION;
104 foreach ($this as $key => $value) {
105 $_SESSION[$key] = $value;
106 }
107 }
108
109 function to_table() {
110 $table = "<table style='border: 1px solid #e1e4e8; border-collapse: collapse; width: 100%;'>";
111 foreach ($this as $key => $value) {
112 if ($key != "password" && $key != "user_hash" && !empty($value)) {
113 if (is_bool($value)) {
114 if ($value) $value = "yes";
115 else $value = "no";
Marc Kupietzd871d882023-03-05 18:34:16 +0100116 }
Marc Kupietz3417c882023-03-07 12:03:21 +0100117 $value = str_replace("\r\n", "<br>", $value);
118 $key = str_replace("_", " ", ucfirst($key));
119 $table .= " <tr>
120 <td style='border: 1px solid #e1e4e8; padding: 6px 13px;'><strong>$key</strong></td>
121 <td style='border: 1px solid #e1e4e8; padding: 6px 13px;'>$value</td>
122 </tr>\n";
123 }
Marc Kupietzd871d882023-03-05 18:34:16 +0100124 }
Marc Kupietz3417c882023-03-07 12:03:21 +0100125 $table .= "</table>";
126 return $table;
127 }
128}