-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
db-functions.php
151 lines (117 loc) · 5.35 KB
/
db-functions.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
148
149
150
<?php
global $config;
include_once('config.php');
try {
$dbh = new PDO($config['dsn'], $config['dbUser'], $config['dbPass'], array(PDO::ATTR_PERSISTENT => true));
$dbh->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
} catch (PDOException $e) {
die('Connection failed: ' . $e->getMessage());
}
function get_domain_id($domain) {
global $dbh;
$domainData = $dbh->prepare('select `domain_id` from `virtual_domains` where `domain` = :domain and `domain_id` > 0');
$domainData->bindValue(':domain', $domain, PDO::PARAM_STR);
$domainData->execute();
handleError($domainData);
$domainData = $domainData->fetch();
if(is_array($domainData) && is_numeric($domainData['domain_id'])) {
return $domainData['domain_id'];
}
return false;
}
function get_domain_data($domain) {
global $dbh;
$domainData = $dbh->prepare('select `domain_id`, `backend_type`, `backend_uri`, `ldap_bind_dn`, `ldap_bind_password`, `ldap_base_dn`, `ldap_sync_cron` from `virtual_domains` where `domain` = :domain and `domain_id` > 0');
$domainData->bindValue(':domain', $domain, PDO::PARAM_STR);
$domainData->execute();
handleError($domainData);
return $domainData->fetch();
}
function get_userdata($email, $domain_id) {
global $dbh;
$userDetails = $dbh->prepare('Select `user_id`, `secondary_mail`, `forward_to_secondary`, `password_usage_report` from `virtual_users` where `email` = :email and `domain_id` = :domain_id');
$userDetails->bindValue(':email', $email, PDO::PARAM_STR);
$userDetails->bindValue(':domain_id', $domain_id, PDO::PARAM_INT);
$userDetails->execute();
handleError($userDetails);
return $userDetails->fetch();
}
function update_userdata($email, $domain_id, $settings) {
global $dbh;
if (!is_array($settings)) {
return -1;
}
$updateUserDetails = $dbh->prepare('update `virtual_users` set `secondary_mail` = :secondary_mail, `forward_to_secondary` = :forward, `password_usage_report` = :usage where `email` = :email and `domain_id` = :domain_id');
$updateUserDetails->bindValue(':secondary_mail', $settings['secondary-address'], PDO::PARAM_STR);
$updateUserDetails->bindValue(':forward', $settings['forward-email'], PDO::PARAM_BOOL);
$updateUserDetails->bindValue(':usage', $settings['password-usage-report'], PDO::PARAM_BOOL);
$updateUserDetails->bindValue(':email', $email, PDO::PARAM_STR);
$updateUserDetails->bindValue(':domain_id', $domain_id, PDO::PARAM_INT);
$updateUserDetails->execute();
handleError($updateUserDetails);
return 0;
}
function create_user($email, $domain_id) {
global $dbh;
$createUser = $dbh->prepare('insert into `virtual_users` set `email` = :email, `domain_id` = :domain_id, `password_usage_report` = 0, `forward_to_secondary` = 0');
$createUser->bindValue(':email', $email, PDO::PARAM_STR);
$createUser->bindValue(':domain_id', $domain_id, PDO::PARAM_INT);
$createUser->execute();
handleError($createUser);
header('Location: https://'.$_SERVER['SERVER_NAME']);
}
function is_superuser($email) {
global $dbh;
$isSuperuser = $dbh->prepare('select exists(select * from `system_info` where `key` like "super_user%" and `value` = :email) as isSuperuser');
$isSuperuser->bindValue(':email', $email, PDO::PARAM_STR);
$isSuperuser->execute();
handleError($isSuperuser);
return $isSuperuser->fetch();
}
function create_app_password($passName, $hash, $user_id) {
global $dbh;
$createAppPass = $dbh->prepare('insert into `virtual_application_passwords` set `user_id` = :user_id, `application_username` = :user, `application_password` = :pass');
$createAppPass->bindValue(':user', $passName, PDO::PARAM_STR);
$createAppPass->bindValue(':pass', $hash, PDO::PARAM_STR);
$createAppPass->bindValue(':user_id', $user_id, PDO::PARAM_INT);
$createAppPass->execute();
}
function update_app_password($hash, $pass_id) {
global $dbh;
$updateAppPass = $dbh->prepare('update `virtual_application_passwords` set `application_password` = :pass, `password_changed` = now() where `password_id` = :pass_id');
$updateAppPass->bindValue(':pass', $hash, PDO::PARAM_STR);
$updateAppPass->bindValue(':pass_id', $pass_id, PDO::PARAM_INT);
$updateAppPass->execute();
}
function delete_app_password($pass_id) {
global $dbh;
$deleteAppPass = $dbh->prepare('delete from `virtual_application_passwords` where `password_id` = :pass_id');
$deleteAppPass->bindValue(':pass_id', $pass_id, PDO::PARAM_INT);
$deleteAppPass->execute();
}
function get_user_passwords($user_id) {
global $dbh;
$userPasswords = $dbh->prepare('select `application_username`, `password_id` from `virtual_application_passwords` where `user_id` = :user_id');
$userPasswords->bindValue(':user_id', $user_id, PDO::PARAM_INT);
$userPasswords->execute();
handleError($userPasswords);
return $userPasswords->fetchAll();
}
function is_password_owner($user_id, $pass_id) {
global $dbh;
$verifyPasswordOwnership = $dbh->prepare('select count(*) as `isPasswordOwner`, `application_username` from `virtual_application_passwords` where `user_id` = :user_id and `password_id` = :pass_id');
$verifyPasswordOwnership->bindValue(':user_id', $user_id, PDO::PARAM_INT);
$verifyPasswordOwnership->bindValue(':pass_id', $pass_id, PDO::PARAM_INT);
$verifyPasswordOwnership->execute();
handleError($verifyPasswordOwnership);
return $verifyPasswordOwnership->fetch();
}
function handleError($pdoObject) {
if($pdoObject->errorCode() != '00000') {
die(print_r($pdoObject->errorInfo()));
return false;
} else {
return true;
}
}
?>