bug 1094: Added new message regarding disputes on organisation domains
[cacert-devel.git] / www / disputes.php
1 <? /*
2 LibreSSL - CAcert web application
3 Copyright (C) 2004-2008 CAcert Inc.
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; version 2 of the License.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17 */ ?>
18 <?
19 require_once("../includes/loggedin.php");
20
21 loadem("account");
22
23 $type=""; if(array_key_exists('type',$_REQUEST)) $type=$_REQUEST['type'];
24 $action=""; if(array_key_exists('action',$_REQUEST)) $action=sanitizeHTML($_REQUEST['action']);
25
26 if($type == "reallyemail")
27 {
28 $emailid = intval($_SESSION['_config']['emailid']);
29 $hash = mysql_escape_string(trim($_SESSION['_config']['hash']));
30
31 $res = mysql_query("select * from `disputeemail` where `id`='$emailid' and `hash`='$hash'");
32 if(mysql_num_rows($res) <= 0)
33 {
34 showheader(_("Email Dispute"));
35 echo _("This dispute no longer seems to be in the database, can't continue.");
36 showfooter();
37 exit;
38 }
39 $row = mysql_fetch_assoc($res);
40 $oldmemid = $row['oldmemid'];
41
42 if($action == "reject")
43 {
44 mysql_query("update `disputeemail` set hash='',action='reject' where `id`='".intval($emailid)."'");
45 showheader(_("Email Dispute"));
46 echo _("You have opted to reject this dispute and the request will be removed from the database");
47 showfooter();
48 exit;
49 }
50 if($action == "accept")
51 {
52 showheader(_("Email Dispute"));
53 echo "<p>"._("You have opted to accept this dispute and the request will now remove this email address from the existing account, and revoke any current certificates.")."</p>";
54 echo "<p>"._("The following accounts have been removed:")."<br>\n";
55 $query = "select * from `email` where `id`='".intval($emailid)."' and deleted=0";
56 $res = mysql_query($query);
57 if(mysql_num_rows($res) > 0)
58 {
59 $row = mysql_fetch_assoc($res);
60 echo $row['email']."<br>\n";
61 $query = "select `emailcerts`.`id`
62 from `emaillink`,`emailcerts` where
63 `emailid`='$emailid' and `emaillink`.`emailcertsid`=`emailcerts`.`id` and
64 `revoked`=0 and UNIX_TIMESTAMP(`expire`)-UNIX_TIMESTAMP() > 0
65 group by `emailcerts`.`id`";
66 $dres = mysql_query($query);
67 while($drow = mysql_fetch_assoc($dres))
68 mysql_query("update `emailcerts` set `revoked`='1970-01-01 10:00:01' where `id`='".intval($drow['id'])."'");
69
70 $do = `../scripts/runclient`;
71 $query = "update `email` set `deleted`=NOW() where `id`='".intval($emailid)."'";
72 mysql_query($query);
73 }
74 mysql_query("update `disputeemail` set hash='',action='accept' where `id`='$emailid'");
75 $rc = mysql_num_rows(mysql_query("select * from `domains` where `memid`='$oldmemid' and `deleted`=0"));
76 $rc = mysql_num_rows(mysql_query("select * from `email` where `memid`='$oldmemid' and `deleted`=0 and `id`!='$emailid'"));
77 $res = mysql_query("select * from `users` where `id`='$oldmemid'");
78 $user = mysql_fetch_assoc($res);
79 if($rc == 0 && $rc2 == 0 && $_SESSION['_config']['email'] == $user['email'])
80 {
81 mysql_query("update `users` set `deleted`=NOW() where `id`='$oldmemid'");
82 echo _("This was the primary email on the account, and no emails or domains were left linked so the account has also been removed from the system.");
83 }
84
85 showfooter();
86 exit;
87 }
88 }
89
90 if($type == "email")
91 {
92 $emailid = intval($_REQUEST['emailid']);
93 $hash = trim(mysql_escape_string(stripslashes($_REQUEST['hash'])));
94 if($emailid <= 0 || $hash == "")
95 {
96 showheader(_("Email Dispute"));
97 echo _("Invalid request. Can't continue.");
98 showfooter();
99 exit;
100 }
101
102 $res = mysql_query("select * from `disputeemail` where `id`='$emailid' and `hash`='$hash'");
103 if(mysql_num_rows($res) <= 0)
104 {
105 $res = mysql_query("select * from `disputeemail` where `id`='$emailid' and hash!=''");
106 if(mysql_num_rows($res) > 0)
107 {
108 $row = mysql_fetch_assoc($res);
109 mysql_query("update `disputeemail` set `attempts`='".intval($row['attempts'] + 1)."' where `id`='".$row['id']."'");
110 showheader(_("Email Dispute"));
111 if($row['attempts'] >= 3)
112 {
113 echo _("Your attempt to accept or reject a disputed email is invalid due to the hash string not matching with the email ID. Your attempt has been logged and the request will be removed from the system as a result.");
114 mysql_query("update `disputeemail` set hash='',action='failed' where `id`='$emailid'");
115 } else
116 echo _("Your attempt to accept or reject a disputed email is invalid due to the hash string not matching with the email ID.");
117 showfooter();
118 exit;
119 } else {
120 showheader(_("Email Dispute"));
121 echo _("Invalid request. Can't continue.");
122 showfooter();
123 exit;
124 }
125 }
126 $_SESSION['_config']['emailid'] = $emailid;
127 $_SESSION['_config']['hash'] = $hash;
128 $row = mysql_fetch_assoc(mysql_query("select * from `disputeemail` where `id`='$emailid'"));
129 $_SESSION['_config']['email'] = $row['email'];
130 showheader(_("Email Dispute"));
131 includeit("4", "disputes");
132 showfooter();
133 exit;
134 }
135
136 if($type == "reallydomain")
137 {
138 $domainid = intval($_SESSION['_config']['domainid']);
139 $hash = mysql_escape_string(trim($_SESSION['_config']['hash']));
140
141 $res = mysql_query("select * from `disputedomain` where `id`='$domainid' and `hash`='$hash'");
142 if(mysql_num_rows($res) <= 0)
143 {
144 showheader(_("Domain Dispute"));
145 echo _("This dispute no longer seems to be in the database, can't continue.");
146 showfooter();
147 exit;
148 }
149
150 if($action == "reject")
151 {
152 mysql_query("update `disputedomain` set hash='',action='reject' where `id`='$domainid'");
153 showheader(_("Domain Dispute"));
154 echo _("You have opted to reject this dispute and the request will be removed from the database");
155 showfooter();
156 exit;
157 }
158 if($action == "accept")
159 {
160 showheader(_("Domain Dispute"));
161 echo "<p>"._("You have opted to accept this dispute and the request will now remove this domain from the existing account, and revoke any current certificates.")."</p>";
162 echo "<p>"._("The following accounts have been removed:")."<br>\n";
163 $query = "select * from `domains` where `id`='$domainid' and deleted=0";
164 $res = mysql_query($query);
165 if(mysql_num_rows($res) > 0)
166 {
167 echo $_SESSION['_config']['domain']."<br>\n";
168 mysql_query("update `domains` set `deleted`=NOW() where `id`='$domainid'");
169 $query = "select * from `domlink` where `domid`='$domainid'";
170 $res = mysql_query($query);
171 while($row = mysql_fetch_assoc($res))
172 mysql_query("update `domaincerts` set `revoked`='1970-01-01 10:00:01' where `id`='".$row['certid']."' and `revoked`=0 and UNIX_TIMESTAMP(`expire`)-UNIX_TIMESTAMP() > 0");
173 $do = `../scripts/runserver`;
174 }
175 mysql_query("update `disputedomain` set hash='',action='accept' where `id`='$domainid'");
176 showfooter();
177 exit;
178 }
179 }
180
181 if($type == "domain")
182 {
183 $domainid = intval($_REQUEST['domainid']);
184 $hash = trim(mysql_escape_string(stripslashes($_REQUEST['hash'])));
185 if($domainid <= 0 || $hash == "")
186 {
187 showheader(_("Domain Dispute"));
188 echo _("Invalid request. Can't continue.");
189 showfooter();
190 exit;
191 }
192
193 $res = mysql_query("select * from `disputedomain` where `id`='$domainid' and `hash`='$hash'");
194 if(mysql_num_rows($res) <= 0)
195 {
196 $res = mysql_query("select * from `disputedomain` where `id`='$domainid' and hash!=''");
197 if(mysql_num_rows($res) > 0)
198 {
199 $row = mysql_fetch_assoc($res);
200 mysql_query("update `disputedomain` set `attempts`='".intval($row['attempts'] + 1)."' where `id`='".$row['id']."'");
201 showheader(_("Domain Dispute"));
202 if($row['attempts'] >= 3)
203 {
204 echo _("Your attempt to accept or reject a disputed domain is invalid due to the hash string not matching with the domain ID. Your attempt has been logged and the request will be removed from the system as a result.");
205 mysql_query("update `disputedomain` set hash='',action='failed' where `id`='$domainid'");
206 } else
207 echo _("Your attempt to accept or reject a disputed domain is invalid due to the hash string not matching with the domain ID.");
208 showfooter();
209 exit;
210 } else {
211 showheader(_("Domain Dispute"));
212 echo _("Invalid request. Can't continue.");
213 showfooter();
214 exit;
215 }
216 }
217 $_SESSION['_config']['domainid'] = $domainid;
218 $_SESSION['_config']['hash'] = $hash;
219 $row = mysql_fetch_assoc(mysql_query("select * from `disputedomain` where `id`='$domainid'"));
220 $_SESSION['_config']['domain'] = $row['domain'];
221 showheader(_("Domain Dispute"));
222 includeit("6", "disputes");
223 showfooter();
224 exit;
225 }
226
227 if($oldid == "1")
228 {
229 csrf_check('emaildispute');
230 $email = trim(mysql_escape_string(stripslashes($_REQUEST['dispute'])));
231 if($email == "")
232 {
233 showheader(_("Email Dispute"));
234 echo _("Not a valid email address. Can't continue.");
235 showfooter();
236 exit;
237 }
238
239 $res = mysql_query("select * from `disputeemail` where `email`='$email' and hash!=''");
240 if(mysql_num_rows($res) > 0)
241 {
242 showheader(_("Email Dispute"));
243 printf(_("The email address '%s' already exists in the dispute system. Can't continue."), sanitizeHTML($email));
244 showfooter();
245 exit;
246 }
247
248 unset($oldid);
249 $query = "select * from `email` where `email`='$email' and `deleted`=0";
250 $res = mysql_query($query);
251 if(mysql_num_rows($res) <= 0)
252 {
253 showheader(_("Email Dispute"));
254 printf(_("The email address '%s' doesn't exist in the system. Can't continue."), sanitizeHTML($email));
255 showfooter();
256 exit;
257 }
258 $row = mysql_fetch_assoc($res);
259 $oldmemid = $row['memid'];
260 $emailid = $row['id'];
261 if($_SESSION['profile']['id'] == $oldmemid)
262 {
263 showheader(_("Email Dispute"));
264 echo _("You aren't allowed to dispute your own email addresses. Can't continue.");
265 showfooter();
266 exit;
267 }
268
269 $res = mysql_query("select * from `users` where `id`='$oldmemid'");
270 $user = mysql_fetch_assoc($res);
271 $rc = mysql_num_rows(mysql_query("select * from `domains` where `memid`='$oldmemid' and `deleted`=0"));
272 $rc2 = mysql_num_rows(mysql_query("select * from `email` where `memid`='$oldmemid' and `deleted`=0 and `id`!='$emailid'"));
273 if($user['email'] == $email && ($rc > 0 || $rc2 > 0))
274 {
275 showheader(_("Email Dispute"));
276 echo _("You only dispute the primary email address of an account if there is no longer any email addresses or domains linked to it.");
277 showfooter();
278 exit;
279 }
280
281 $hash = make_hash();
282 $query = "insert into `disputeemail` set `email`='$email',`memid`='".intval($_SESSION['profile']['id'])."',
283 `oldmemid`='$oldmemid',`created`=NOW(),`hash`='$hash',`id`='".intval($emailid)."',
284 `IP`='".$_SERVER['REMOTE_ADDR']."'";
285 mysql_query($query);
286
287 $body = sprintf(_("You have been sent this email as the email address '%s' is being disputed. You have the option to accept or reject this request, after 2 days the request will automatically be discarded. Click the following link to accept or reject the dispute:"), $email)."\n\n";
288 $body .= "https://".$_SESSION['_config']['normalhostname']."/disputes.php?type=email&emailid=$emailid&hash=$hash\n\n";
289 $body .= _("Best regards")."\n"._("CAcert.org Support!");
290
291 sendmail($email, "[CAcert.org] "._("Dispute Probe"), $body, "support@cacert.org", "", "", "CAcert Support");
292
293 showheader(_("Email Dispute"));
294 printf(_("The email address '%s' has been entered into the dispute system, the email address will now be sent an email which will give the recipent the option of accepting or rejecting the request, if after 2 days we haven't received a valid response for or against we will discard the request."), sanitizeHTML($email));
295 showfooter();
296 exit;
297 }
298
299 if($oldid == "2")
300 {
301 csrf_check('domaindispute');
302 $domain = trim(mysql_escape_string(stripslashes($_REQUEST['dispute'])));
303 if($domain == "")
304 {
305 showheader(_("Domain Dispute"));
306 echo _("Not a valid Domain. Can't continue.");
307 showfooter();
308 exit;
309 }
310
311 $query = "select * from `disputedomain` where `domain`='$domain' and hash!=''";
312 $res = mysql_query($query);
313 if(mysql_num_rows($res) > 0)
314 {
315 showheader(_("Domain Dispute"));
316 printf(_("The domain '%s' already exists in the dispute system. Can't continue."), sanitizeHTML($domain));
317 showfooter();
318 exit;
319 }
320 unset($oldid);
321 $query = "select * from `domains` where `domain`='$domain' and `deleted`=0";
322 $email = ""; if(array_key_exists('email',$_REQUEST)) $email=trim(mysql_real_escape_string($_REQUEST['email']));
323 $res = mysql_query($query);
324 if(mysql_num_rows($res) <= 0)
325 {
326 $query = "select 1 from `orgdomains` where `domain`='$domain'";
327 $res = mysql_query($query);
328 if(mysql_num_rows($res) > 0)
329 {
330 showheader(_("Domain Dispute"));
331 printf(_("The domain '%s' is included in an organisation account. Please send a mail to support@cacert.org to dispute this domain."), sanitizeHTML($domain));
332 showfooter();
333 exit;
334 }
335 showheader(_("Domain Dispute"));
336 printf(_("The domain '%s' doesn't exist in the system. Can't continue."), sanitizeHTML($email));
337 showfooter();
338 exit;
339 }
340 $row = mysql_fetch_assoc($res);
341 $oldmemid = $row['memid'];
342 if($_SESSION['profile']['id'] == $oldmemid)
343 {
344 showheader(_("Domain Dispute"));
345 echo _("You aren't allowed to dispute your own domains. Can't continue.");
346 showfooter();
347 exit;
348 }
349
350 $domainid = $row['id'];
351 $_SESSION['_config']['domainid'] = $domainid;
352 $_SESSION['_config']['memid'] = array_key_exists('memid',$_REQUEST)?intval($_REQUEST['memid']):0;
353 $_SESSION['_config']['domain'] = $domain;
354 $_SESSION['_config']['oldmemid'] = $oldmemid;
355
356 $addy = array();
357 $domtmp = escapeshellarg($domain);
358 if(strtolower(substr($domtmp, -4, 3)) != ".jp")
359 $adds = explode("\n", trim(`whois $domtmp|grep \@`));
360 if(substr($domain, -4) == ".org" || substr($domain, -5) == ".info")
361 {
362 if(is_array($adds))
363 foreach($adds as $line)
364 {
365 $bits = explode(":", $line, 2);
366 $line = trim($bits[1]);
367 if(!in_array($line, $addy) && $line != "")
368 $addy[] = trim(mysql_escape_string(stripslashes($line)));
369 }
370 } else {
371 if(is_array($adds))
372 foreach($adds as $line)
373 {
374 $line = trim(str_replace("\t", " ", $line));
375 $line = trim(str_replace("(", "", $line));
376 $line = trim(str_replace(")", " ", $line));
377
378 $bits = explode(" ", $line);
379 foreach($bits as $bit)
380 {
381 if(strstr($bit, "@"))
382 $line = $bit;
383 }
384 if(!in_array($line, $addy) && $line != "")
385 $addy[] = trim(mysql_escape_string(stripslashes($line)));
386 }
387 }
388
389 $rfc = array("root@$domain", "hostmaster@$domain", "postmaster@$domain", "admin@$domain", "webmaster@$domain");
390 foreach($rfc as $sub)
391 if(!in_array($sub, $addy))
392 $addy[] = $sub;
393 $_SESSION['_config']['addy'] = $addy;
394 showheader(_("Domain Dispute"));
395 includeit("5", "disputes");
396 showfooter();
397 exit;
398 }
399
400 if($oldid == "5")
401 {
402 $authaddy = trim(mysql_escape_string(stripslashes($_REQUEST['authaddy'])));
403
404 if(!in_array($authaddy, $_SESSION['_config']['addy']) || $authaddy == "")
405 {
406 showheader(_("My CAcert.org Account!"));
407 echo _("The address you submitted isn't a valid authority address for the domain.");
408 showfooter();
409 exit;
410 }
411
412 $query = "select * from `domains` where `domain`='".$_SESSION['_config']['domain']."' and `deleted`=0";
413 $res = mysql_query($query);
414 if(mysql_num_rows($res) <= 0)
415 {
416 showheader(_("Domain Dispute!"));
417 printf(_("The domain '%s' isn't in the system. Can't continue."), sanitizeHTML($_SESSION['_config']['domain']));
418 showfooter();
419 exit;
420 }
421
422 $domainid = intval($_SESSION['_config']['domainid']);
423 $memid = intval($_SESSION['_config']['memid']);
424 $oldmemid = intval($_SESSION['_config']['oldmemid']);
425 $domain = mysql_escape_string($_SESSION['_config']['domain']);
426
427 $hash = make_hash();
428 $query = "insert into `disputedomain` set `domain`='$domain',`memid`='".$_SESSION['profile']['id']."',
429 `oldmemid`='$oldmemid',`created`=NOW(),`hash`='$hash',`id`='$domainid'";
430 mysql_query($query);
431
432 $body = sprintf(_("You have been sent this email as the domain '%s' is being disputed. You have the option to accept or reject this request, after 2 days the request will automatically be discarded. Click the following link to accept or reject the dispute:"), $domain)."\n\n";
433 $body .= "https://".$_SESSION['_config']['normalhostname']."/disputes.php?type=domain&domainid=$domainid&hash=$hash\n\n";
434 $body .= _("Best regards")."\n"._("CAcert.org Support!");
435
436 sendmail($authaddy, "[CAcert.org] "._("Dispute Probe"), $body, "support@cacert.org", "", "", "CAcert Support");
437
438 showheader(_("Domain Dispute"));
439 printf(_("The domain '%s' has been entered into the dispute system, the email address you choose will now be sent an email which will give the recipent the option of accepting or rejecting the request, if after 2 days we haven't received a valid response for or against we will discard the request."), sanitizeHTML($domain));
440 showfooter();
441 exit;
442 }
443
444 showheader(_("Domain and Email Disputes"));
445 includeit($id, "disputes");
446 showfooter();
447 ?>