-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.php
147 lines (121 loc) · 3.74 KB
/
index.php
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
<?php
//CHECK if user has a password to create a user
if(file_exists('./env.php')) {
include './env.php';
}
else {
die("there was no env file, please create ./env.php");
}
$output = [
"success" => true,
"extraInfo" => "",
];
$loginUser = getenv('LOGIN_USERNAME');
$password = getenv('LOGIN_PASSWORD');
$newUser = $_GET['dvuuid'];
$newUserPassword = random_str(30);
$databaseName = "analyticsdb_" . $newUser;
$usersUrl = "_users/org.couchdb.user:";
define('CREATE_USER_URL' ,getenv('HOST') . $usersUrl . $newUser);
define('DATABASE_URL' ,getenv('HOST') . $databaseName);
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => CREATE_USER_URL ,
CURLOPT_USERPWD => "$loginUser:$password",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPAUTH => CURLAUTH_BASIC,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "PUT",
CURLOPT_POSTFIELDS => '{"name" : "'.$newUser.'" ,"password" : "'.$newUserPassword.'", "roles" : [], "type" : "user"}',
CURLOPT_HTTPHEADER => array(
"cache-control: no-cache",
"content-type: application/json",
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
//echo "cURL Error #:" . $err;
$output["success"] = false;
$output["message"] = "Failed to create a new user. ERROR: " . $err;
} else {
//echo $response;
$output["extraInfo"] .= $response;
}
if ($output["success"])
{
$output["password"] = $newUserPassword;
}
$createDatabaseCurl = curl_init();
curl_setopt_array($createDatabaseCurl, array(
CURLOPT_URL => DATABASE_URL ,
CURLOPT_USERPWD => "$loginUser:$password",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPAUTH => CURLAUTH_BASIC,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "PUT"
)
);
$response = curl_exec($createDatabaseCurl);
$err = curl_error($createDatabaseCurl);
curl_close($createDatabaseCurl);
if ($err) {
//echo "cURL Error #:" . $err;
$output["success"] = false;
$output["message"] .= "Failed to create a database for the user. ERROR: " . $err .". " ;
} else {
//echo $response;
$output["extraInfo"] .= $response;
}
//UPDATE DATABASE WITH PERMISSIONS TO ALLOW USER TO EDIT
$updatePermissionCurl = curl_init();
curl_setopt_array($updatePermissionCurl, array(
CURLOPT_URL => DATABASE_URL . "/_security" ,
CURLOPT_USERPWD => "$loginUser:$password",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPAUTH => CURLAUTH_BASIC,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "PUT",
CURLOPT_POSTFIELDS => '{"admins": { "names": ["'.$newUser.'"], "roles": [] }, "members": { "names": ["'.$newUser.'"], "roles": [] } }',
CURLOPT_HTTPHEADER => array(
"cache-control: no-cache",
"content-type: application/json",
),
));
$response = curl_exec($updatePermissionCurl);
$err = curl_error($updatePermissionCurl);
curl_close($updatePermissionCurl);
if ($err) {
//echo "cURL Error #:" . $err;
$output["success"] = false;
$output["message"] .= "Failed to set permissions on the database for the user. ERROR: " . $err .". " ;
} else {
//echo $response;
$output["extraInfo"] .= $response;
}
echo(json_encode($output));
function random_str(
$length,
$keyspace = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
)
{
$str = '';
$max = mb_strlen($keyspace, '8bit') - 1;
if ($max < 1) {
throw new Exception('$keyspace must be at least two characters long');
}
for ($i = 0; $i < $length; ++$i) {
$str .= $keyspace[random_int(0, $max)];
}
return $str;
}