FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
bind9-api/create_user.php at main · getnamingo/bind9-api · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
getnamingo
/
bind9-api
Public
Notifications
You must be signed in to change notification settings
Fork
4
Star
5
Code
Issues
0
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
bind9-api
/
create_user.php
Copy path
More file actions
More file actions
Latest commit
History
History
History
44 lines (35 loc) · 1.39 KB
Breadcrumbs
bind9-api
/
create_user.php
Copy path
File metadata and controls
44 lines (35 loc) · 1.39 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
<?php
declare
(strict_types=
1
);
use
Dotenv
\
Dotenv
;
use
Namingo
\
Bind9Api
\
Database
;
require
__DIR__
.
'
/vendor/autoload.php
'
;
if
(
PHP_SAPI
!==
'
cli
'
) {
http_response_code
(
404
);
exit
;
}
$
username
=
trim
((
string
) (
$
argv
[
1
] ??
''
));
if
(!
preg_match
(
'
/^[A-Za-z0-9_.@-]{3,50}$/
'
,
$
username
)) {
fwrite
(
STDERR
,
"
Usage: printf '%s' 'a-long-password' | php create_user.php USERNAME
\n"
);
fwrite
(
STDERR
,
"
USERNAME must be 3-50 characters: letters, digits, _, ., @ or -.
\n"
);
exit
(
2
);
}
$
password
=
rtrim
((
string
)
stream_get_contents
(
STDIN
),
"\r\n"
);
if
(
strlen
(
$
password
) <
12
||
strlen
(
$
password
) >
1024
) {
fwrite
(
STDERR
,
"
Read a password of 12-1024 bytes from standard input.
\n"
);
exit
(
2
);
}
try
{
Dotenv::
createImmutable
(
__DIR__
)->
load
();
$
pdo
= (
new
Database
(
$
_ENV
))->
connect
();
$
algorithm
=
defined
(
'
PASSWORD_ARGON2ID
'
) ?
PASSWORD_ARGON2ID
:
PASSWORD_DEFAULT
;
$
hash
=
password_hash
(
$
password
,
$
algorithm
);
if
(
$
hash
===
false
) {
throw
new
RuntimeException
(
'
Unable to hash the password.
'
);
}
$
statement
=
$
pdo
->
prepare
(
'
INSERT INTO users (username, password) VALUES (:username, :password)
'
);
$
statement
->
execute
([
'
username
'
=>
$
username
,
'
password
'
=>
$
hash
]);
fwrite
(
STDOUT
,
"
User '
{
$
username
}
' created successfully.
\n"
);
}
catch
(
Throwable
$
exception
) {
fwrite
(
STDERR
,
'
Unable to create user:
'
.
$
exception
->
getMessage
() .
PHP_EOL
);
exit
(
1
);
}
Back
|
FazBrowse Home
|
New Git URL