summaryrefslogtreecommitdiff
path: root/includes/temp_functions.php
blob: c9eb02082307cd10c918b1be7a0bd2bfad5cbdb5 (plain)
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
<?php
//just temoprary file to find all function needed for account delete

function account_email_delete($mailid){
//deletes an email entry from an acount
//revolkes all certifcates for that email address
//called from www/account.php if($process != "" && $oldid == 2)
//called from www/diputes.php if($type == "reallyemail") / if($action == "accept")
//called from account_delete
	$mailid = intval($mailid);
	revoke_all_client_cert($mailid);
	$query = "update `email` set `deleted`=NOW() where `id`='$mailid'";
	mysql_query($query);
}

function account_domain_delete($domainid){
//deletes an domain entry from an acount
//revolkes all certifcates for that domain address
//called from www/account.php if($process != "" && $oldid == 9)
//called from www/diputes.php if($type == "reallydomain") / if($action == "accept")
//called from account_delete
	$domainid = intval($domainid);
	revoke_all_server_cert($domainid);
	mysql_query(
		"update `domains`
		set `deleted`=NOW()
		where `id` = '$domainid'");
}

function account_delete($id, $arbno, $adminid){
//deletes an account following the deleted account routnie V3
// called from www/account.php if($oldid == 50 && $process != "")
//change password
	$id = intval($id);
	$arbno = mysql_real_escape_string($arbno);
	$adminid = intval($adminid);
	$pool = 'abcdefghijklmnopqrstuvwxyz';
	$pool .= '0123456789!()ยง';
	$pool .= 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
	srand ((double)microtime()*1000000);
	$password="";
	for($index = 0; $index < 30; $index++)
	{
		$password .= substr($pool,(rand()%(strlen ($pool))), 1);
	}
	mysql_query("update `users` set `password`=sha1('".$password."') where `id`='".$id."'");

//create new mail for arbitration number
	$query = "insert into `email` set `email`='".$arbno."@cacert.org',`memid`='".$id."',`created`=NOW(),`modified`=NOW(), `attempts`=-1";
	mysql_query($query);
	$emailid = mysql_insert_id();

//set new mail as default
	$query = "update `users` set `email`='".$arbno."@cacert.org' where `id`='".$id."'";
	mysql_query($query);

//delete all other email address
	$query = "select * from `email` where `memid`='".$id."' and `id`!='".$emailid."'" ;
	$res=mysql_query($query);
	while($row = mysql_fetch_assoc($res)){
		account_email_delete($row['id']);
	}

//delete all domains
	$query = "select * from `domains` where `memid`='".$id."'";
	$res=mysql_query($query);
	while($row = mysql_fetch_assoc($res)){
		account_domain_delete($row['id']);
	}

//clear alert settings
	mysql_query("update `alerts` set `general`='0' where `memid`='$id'");
	mysql_query("update `alerts` set `country`='0' where `memid`='$id'");
	mysql_query("update `alerts` set `regional`='0' where `memid`='$id'");
	mysql_query("update `alerts` set `radius`='0' where `memid`='$id'");

//set default location
	$query = "update `users` set `locid`='2256755', `regid`='243', `ccid`='12' where `id`='".$id."'";
	mysql_query($query);

//clear listings
	$query = "update `users` set `listme`=' ',`contactinfo`=' ' where `id`='".$id."'";
	mysql_query($query);

//set lanuage to default
	//set default language
	mysql_query("update `users` set `language`='en_AU' where `id`='".$id."'");
	//delete secondary langugaes
	mysql_query("delete from `addlang` where `userid`='".$id."'");

//change secret questions
	for($i=1;$i<=5;$i++){
		$q="";
		$a="";
		for($index = 0; $index < 30; $index++)
		{
			$q .= substr($pool,(rand()%(strlen ($pool))), 1);
			$a .= substr($pool,(rand()%(strlen ($pool))), 1);
		}
		$query = "update `users` set `Q$i`='$q', `A$i`='$a' where `id`='".$id."'";
		mysql_query($query);
	}

//change personal information to arbitration number and DOB=1900-01-01
	$query = "select `fname`,`mname`,`lname`,`suffix`,`dob` from `users` where `id`='$userid'";
	$details = mysql_fetch_assoc(mysql_query($query));
	$query = "insert into `adminlog` set `when`=NOW(),`old-lname`='${details['lname']}',`old-dob`='${details['dob']}',
		`new-lname`='$arbno',`new-dob`='1900-01-01',`uid`='$id',`adminid`='".$adminid."'";
	mysql_query($query);
	$query = "update `users` set `fname`='".$arbno."',
		`mname`='".$arbno."',
		`lname`='".$arbno."',
		`suffix`='".$arbno."',
		`dob`='1900-01-01'
		where `id`='".$id."'";
	mysql_query($query);

//clear all admin and board flags
	mysql_query("update `users` set `assurer`='0' where `id`='$id'");
	mysql_query("update `users` set `assurer_blocked`='0' where `id`='$id'");
	mysql_query("update `users` set `codesign`='0' where `id`='$id'");
	mysql_query("update `users` set `orgadmin`='0' where `id`='$id'");
	mysql_query("update `users` set `ttpadmin`='0' where `id`='$id'");
	mysql_query("update `users` set `locadmin`='0' where `id`='$id'");
	mysql_query("update `users` set `admin`='0' where `id`='$id'");
	mysql_query("update `users` set `adadmin`='0' where `id`='$id'");
	mysql_query("update `users` set `tverify`='0' where `id`='$id'");
	mysql_query("update `users` set `board`='0' where `id`='$id'");

//block account
	mysql_query("update `users` set `locked`='1' where `id`='$id'");  //, `deleted`=Now()
}


function check_email_exists($email){
// called from includes/account.php if($process != "" && $oldid == 1)
// called from includes/account.php	if($oldid == 50 && $process != "")
	$email = mysql_real_escape_string($email);
	$query = "select * from `email` where `email`='$email' and `deleted`=0";
	$res = mysql_query($query);
	return mysql_num_rows($res) > 0;
}

function check_gpg_cert_running($uid,$cca=0){
	//if $cca =0 if just expired, =1 if CCA retention +3 month should be obeyed
	// called from includes/account.php	if($oldid == 50 && $process != "")
	$uid = intval($uid);
	if (0==$cca) {
		$query = "select * from `gpg` where `memid`='$uid' and `expire`>NOW()";
	}else{
		$query = "select * from `gpg` where `memid`='$uid' and `expire`>NOW()+90*86400";
	}
	$res = mysql_query($query);
	return mysql_num_rows($res) > 0;
}

function check_client_cert_running($uid,$cca=0){
	//if $cca =0 if just expired, =1 if CCA retention +3 month should be obeyed
	// called from includes/account.php	if($oldid == 50 && $process != "")
	$uid = intval($uid);
	if (0==$cca) {
		$query1 = "select from `domiancerts` where `memid`='$uid' and `expire`>NOW()";
		$query2 = "select from `domiancerts` where `memid`='$uid' and `revoked`>NOW()";
	}else{
		$query1 = "select from `emailcerts` where `memid`='$uid' and `expire`>NOW()+90*86400";
		$query2 = "select from `emailcerts` where `memid`='$uid' and `revoked`>NOW()+90*86400";
	}
	$res = mysql_query($query1);
	$r1 = mysql_num_rows($res)>0;
	$res = mysql_query($query2);
	$r2 = mysql_num_rows($res)>0;
	return !!($r1 || $r2);
}

function check_server_cert_running($uid,$cca=0){
	//if $cca =0 if just expired, =1 if CCA retention +3 month should be obeyed
	// called from includes/account.php	if($oldid == 50 && $process != "")
	$uid = intval($uid);
	if (0==$cca) {
		$query1 = "select from `domiancerts` where `memid`='$uid' and `expire`>NOW()";
		$query2 = "select from `domiancerts` where `memid`='$uid' and `revoked`>NOW()";
	}else{
		$query1 = "select from `domiancerts` where `memid`='$uid' and `expire`>NOW()+90*86400";
		$query2 = "select from `domiancerts` where `memid`='$uid' and `revoked`>NOW()+90*86400";
	}
	$res = mysql_query($query1);
	$r1 = mysql_num_rows($res)>0;
	$res = mysql_query($query2);
	$r2 = mysql_num_rows($res)>0;
	return !!($r1 || $r2);
}
function check_is_orgadmin($uid){
	// called from includes/account.php	if($oldid == 50 && $process != "")
	$uid = intval($uid);
	$query = "select * from `org` where `memid`='$uid' and `deleted`=0";
	$res = mysql_query($query);
	return mysql_num_rows($res) > 0;
}


// revokation of certificates
function revoke_all_client_cert($mailid){
	//revokes all client certificates for an email address
	$mailid = intval($mailid);
	$query = "select `emailcerts`.`id`
		from `emaillink`,`emailcerts` where
		`emailid`='$mailid' and `emaillink`.`emailcertsid`=`emailcerts`.`id` and `revoked`=0
		group by `emailcerts`.`id`";
	$dres = mysql_query($query);
	while($drow = mysql_fetch_assoc($dres)){
		mysql_query("update `emailcerts` set `revoked`='1970-01-01 10:00:01', `disablelogin`=1 where `id`='".$drow['id']."'");
	}
}

function revoke_all_server_cert($domainid){
	//revokes all server certs for an domain
	$domainid = intval($domainid);
	$query = "select distinct `domaincerts`.`id`
		from `domaincerts`, `domlink`
		where `domaincerts`.`domid` = '$domainid'
		or (
		`domaincerts`.`id` = `domlink`.`certid`
		and `domlink`.`domid` = '$domainid')";
	$dres = mysql_query($query);
	while($drow = mysql_fetch_assoc($dres))
	{
		mysql_query(
		"update `domaincerts`
			set `revoked`='1970-01-01 10:00:01'
			where `id` = '".$drow['id']."'
			and `revoked` = 0");
	}
}

function revoke_all_private_cert($uid){
	//revokes all certificates linked to a personal accounts
	//gpg revokation needs to be added to a later point
	$uid=intval($uid);
	$query = "select `id` from `email` where `memid`='".$uid."'";
	$res=mysql_query($query);
	while($row = mysql_fetch_assoc($res)){
		revoke_all_client_cert($row['id']);
	}


	$query = "select `id` from `domains` where `memid`='".$uid."'";
	$res=mysql_query($query);
	while($row = mysql_fetch_assoc($res)){
		revoke_all_server_cert($row['id']);
	}

}

?>