users.php (4848B)
1 <?php
2
3 function destroy_session(
4 ) {
5 header(
6 "Set-Cookie: " .
7 "session=; " .
8 "Max-Age=" . "-1; " .
9 "Domain=" . $_SERVER["SERVER_NAME"] . "; " .
10 "SameSite=Strict;"
11 );
12 unset($_COOKIE["session"]);
13 return true;
14 }
15
16 function create_password_hash(
17 string $pass
18 ) {
19
20 /* check if salt is set properly */
21 if (!array_key_exists("pw_salt", $GLOBALS)) {
22 exit("Your password salt was not set properly!");
23 }
24
25 /* Create hash from given password */
26 $token = hash("sha256", $pass . $GLOBALS["pw_salt"], false);
27
28 /* return resulting token */
29 return $token;
30 }
31
32 function auth_user(
33 string $user,
34 string $token
35 ) {
36
37 if (empty($user) || empty($token)) {
38 return false;
39 }
40
41 /* read token file of user */
42 $tokens_storage = $GLOBALS["data_dir"] . "/" . $user . "/tokens.json";
43 if (is_file($tokens_storage)) {
44 $tokens_true = json_decode(
45 file_get_contents($tokens_storage)
46 );
47 } else {
48 return false;
49 }
50
51 /* find any matches between given hash and tokens */
52 $matches = preg_grep(
53 "/^" . $token . "$/",
54 $tokens_true
55 );
56
57 /* If matches between given hash and tokens were found, return "true" */
58 if (count($matches) > 0 && $matches !== false) {
59 return true;
60 } else {
61 return false;
62 }
63 }
64
65 function set_auth_cookie(
66 string $user,
67 string $token,
68 int $cookie_time
69 ) {
70
71 /* set longtime or session cookie, according to preferences */
72 $cookie_content = "Set-Cookie: " .
73 "session=" . $user . "|" . $token . "; " .
74 "Domain=" . $_SERVER["SERVER_NAME"] . "; " .
75 "SameSite=Lax;";
76 if ($cookie_time > 0) {
77 $cookie_content = $cookie_content .
78 "Max-Age=" . (60 * 60 * 24 * $cookie_time) . "; ";
79 }
80 header($cookie_content);
81
82 /* return true */
83 return true;
84 }
85
86 function create_auth(
87 string $user,
88 string $pass
89 ) {
90
91 /* create hash from given password */
92 $token = create_password_hash($pass);
93
94 /* generate token path */
95 $user_path = "./" . $GLOBALS["data_dir"] . "/" . $user;
96 $tokens_storage = $user_path . "/tokens.json";
97
98 /* Only create new auth, if no previous token file exists already */
99 if (file_exists($token_storage)) {
100 // FIXME: create change_auth()
101 exit("Token already exists! Use \"change_auth()\" instead!");
102 }
103
104 /* create user directory if it does not exist */
105 if (opendir($user_path) === false) {
106 mkdir(
107 $user_path, /* directory */
108 0770, /* Permissions: rwxrwx--- */
109 true /* recursive */
110 );
111 }
112
113 /* create file-handle */
114 $file = fopen(
115 $tokens_storage,
116 "w"
117 );
118
119 /* Write to file */
120 $bytes = fwrite(
121 $file,
122 json_encode(array($token))
123 );
124
125 /* close file-hanle */
126 fclose($file);
127
128 /* return "true" on success and "false" otherwise */
129 return $bytes !== false;
130
131 }
132
133 function add_auth(
134 string $user,
135 string $pass
136 ) {
137
138 /* create hash from given password */
139 $token = create_password_hash($pass);
140
141 /* generate token path */
142 $tokens_storage = $GLOBALS["data_dir"] . "/" . $user . "/tokens.json";
143
144 /* get filesize */
145 $filesize = filesize($tokens_storage);
146
147 if ($filesize > 0){
148
149 /* create file-handle */
150 $file = fopen(
151 $tokens_storage,
152 "r+"
153 );
154
155 /* Read from file */
156 $tokens = json_decode(fread($file, $filesize));
157
158 /* add new token to existing tokens */
159 array_push($tokens, $token);
160
161 /* go back to start of the file for writing */
162 fseek($file, 0);
163
164 /* Write to file */
165 $bytes = fwrite(
166 $file,
167 json_encode($tokens)
168 );
169
170 /* close file-hanle */
171 fclose($file);
172
173 /* return token on success and "false" otherwise */
174 if ($bytes !== false) {
175 return $token;
176 } else {
177 return false;
178 }
179
180 } else {
181 return false;
182 }
183
184 } // function
185
186
187 function update_tokens(
188 string $user
189 ) {
190
191 /* gather tokens from POST */
192 $tokens = array();
193 $i = 0;
194 while (array_key_exists("token_" . $i, $_POST)) {
195 $token = $_POST["token_" . $i];
196 if (!empty($token)) {
197 array_push($tokens, $token);
198 }
199 $i++;
200 }
201
202 /* generate token path */
203 $tokens_storage = $GLOBALS["data_dir"] . "/" . $user . "/tokens.json";
204
205 /* write file */
206 if (file_exists($tokens_storage)) {
207 file_put_contents(
208 $tokens_storage,
209 json_encode($tokens)
210 );
211 } else {
212 return false;
213 }
214
215 /* return "true" */
216 return true;
217
218 } // function
219
220 ?>