Merge branch 'bug-893' into bug-1136
[cacert-devel.git] / includes / notary.inc.php
1 <? /*
2 LibreSSL - CAcert web application
3 Copyright (C) 2004-2011 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 function query_init ($query)
20 {
21 return mysql_query($query);
22 }
23
24 function query_getnextrow ($res)
25 {
26 $row1 = mysql_fetch_assoc($res);
27 return $row1;
28 }
29
30 function query_get_number_of_rows ($resultset)
31 {
32 return intval(mysql_num_rows($resultset));
33 }
34
35 function get_number_of_assurances ($userid)
36 {
37 $res = query_init ("SELECT count(*) AS `list` FROM `notary`
38 WHERE `method` = 'Face to Face Meeting' AND `from`='".intval($userid)."' ");
39 $row = query_getnextrow($res);
40
41 return intval($row['list']);
42 }
43
44 function get_number_of_ttpassurances ($userid)
45 {
46 $res = query_init ("SELECT count(*) AS `list` FROM `notary`
47 WHERE (`method`='Trusted Third Parties' or `method`='TTP-Assisted') AND `to`='".intval($userid)."' ");
48 $row = query_getnextrow($res);
49
50 return intval($row['list']);
51 }
52
53 function get_number_of_assurees ($userid)
54 {
55 $res = query_init ("SELECT count(*) AS `list` FROM `notary`
56 WHERE `method` = 'Face to Face Meeting' AND `to`='".intval($userid)."' ");
57 $row = query_getnextrow($res);
58
59 return intval($row['list']);
60 }
61
62 function get_top_assurer_position ($no_of_assurances)
63 {
64 $res = query_init ("SELECT count(*) AS `list` FROM `notary`
65 WHERE `method` = 'Face to Face Meeting'
66 GROUP BY `from` HAVING count(*) > '".intval($no_of_assurances)."'");
67 return intval(query_get_number_of_rows($res)+1);
68 }
69
70 function get_top_assuree_position ($no_of_assurees)
71 {
72 $res = query_init ("SELECT count(*) AS `list` FROM `notary`
73 WHERE `method` = 'Face to Face Meeting'
74 GROUP BY `to` HAVING count(*) > '".intval($no_of_assurees)."'");
75 return intval(query_get_number_of_rows($res)+1);
76 }
77
78 function get_given_assurances ($userid)
79 {
80 $res = query_init ("select * from `notary` where `from`='".intval($userid)."' and `from` != `to` order by `id` asc");
81 return $res;
82 }
83
84 function get_received_assurances ($userid)
85 {
86 $res = query_init ("select * from `notary` where `to`='".intval($userid)."' and `from` != `to` order by `id` asc ");
87 return $res;
88 }
89
90 function get_given_assurances_summary ($userid)
91 {
92 $res = query_init ("select count(*) as number,points,awarded,method from notary where `from`='".intval($userid)."' group by points,awarded,method");
93 return $res;
94 }
95
96 function get_received_assurances_summary ($userid)
97 {
98 $res = query_init ("select count(*) as number,points,awarded,method from notary where `to`='".intval($userid)."' group by points,awarded,method");
99 return $res;
100 }
101
102 function get_user ($userid)
103 {
104 $res = query_init ("select * from `users` where `id`='".intval($userid)."'");
105 return mysql_fetch_assoc($res);
106 }
107
108 function get_cats_state ($userid)
109 {
110
111 $res = query_init ("select * from `cats_passed` inner join `cats_variant` on `cats_passed`.`variant_id` = `cats_variant`.`id` and `cats_variant`.`type_id` = 1
112 WHERE `cats_passed`.`user_id` = '".intval($userid)."'");
113 return mysql_num_rows($res);
114 }
115
116 function calc_experience ($row,&$points,&$experience,&$sum_experience,&$revoked)
117 {
118 $apoints = max($row['points'], $row['awarded']);
119 $points += $apoints;
120 $experience = "&nbsp;";
121 $revoked = false; # to be coded later (after DB-upgrade)
122 if ($row['method'] == "Face to Face Meeting")
123 {
124 $sum_experience = $sum_experience +2;
125 $experience = "2";
126 }
127 return $apoints;
128 }
129
130 function calc_assurances ($row,&$points,&$experience,&$sumexperience,&$awarded,&$revoked)
131 {
132 $awarded = calc_points($row);
133 $revoked = false;
134
135 if ($awarded > 100)
136 {
137 $experience = $awarded - 100; // needs to be fixed in the future (limit 50 pts and/or no experience if pts > 100)
138 $awarded = 100;
139 }
140 else
141 $experience = 0;
142
143 switch ($row['method'])
144 {
145 case 'Thawte Points Transfer':
146 case 'CT Magazine - Germany':
147 case 'Temporary Increase': // Current usage of 'Temporary Increase' may break audit aspects, needs to be reimplemented
148 $awarded=sprintf("<strong style='color: red'>%s</strong>",_("Revoked"));
149 $experience=0;
150 $revoked=true;
151 break;
152 default:
153 $points += $awarded;
154 }
155 $sumexperience = $sumexperience + $experience;
156 }
157
158
159 function show_user_link ($name,$userid)
160 {
161 $name = trim($name);
162 if($name == "")
163 {
164 if ($userid == 0)
165 $name = _("System");
166 else
167 $name = _("Deleted account");
168 }
169 else
170 $name = "<a href='wot.php?id=9&amp;userid=".intval($userid)."'>".sanitizeHTML($name)."</a>";
171 return $name;
172 }
173
174 function show_email_link ($email,$userid)
175 {
176 $email = trim($email);
177 if($email != "")
178 $email = "<a href='account.php?id=43&amp;userid=".intval($userid)."'>".sanitizeHTML($email)."</a>";
179 return $email;
180 }
181
182 function get_assurer_ranking($userid,&$num_of_assurances,&$rank_of_assurer)
183 {
184 $num_of_assurances = get_number_of_assurances (intval($userid));
185 $rank_of_assurer = get_top_assurer_position($num_of_assurances);
186 }
187
188 function get_assuree_ranking($userid,&$num_of_assurees,&$rank_of_assuree)
189 {
190 $num_of_assurees = get_number_of_assurees (intval($userid));
191 $rank_of_assuree = get_top_assuree_position($num_of_assurees);
192 }
193
194
195 // ************* html table definitions ******************
196
197 function output_ranking($userid)
198 {
199 get_assurer_ranking($userid,$num_of_assurances,$rank_of_assurer);
200 get_assuree_ranking($userid,$num_of_assurees,$rank_of_assuree);
201
202 ?>
203 <table align="center" valign="middle" border="0" cellspacing="0" cellpadding="0" class="wrapper">
204 <tr>
205 <td class="title"><?=_("Assurer Ranking")?></td>
206 </tr>
207 <tr>
208 <td class="DataTD"><?=sprintf(_("You have made %s assurances which ranks you as the #%s top assurer."), intval($num_of_assurances), intval($rank_of_assurer) )?></td>
209 </tr>
210 <tr>
211 <td class="DataTD"><?=sprintf(_("You have received %s assurances which ranks you as the #%s top assuree."), intval($num_of_assurees), intval($rank_of_assuree) )?></td>
212 </tr>
213 </table>
214 <br/>
215 <?
216 }
217
218 function output_assurances_header($title,$support)
219 {
220 ?>
221 <table align="center" valign="middle" border="0" cellspacing="0" cellpadding="0" class="wrapper">
222 <tr>
223 <?
224 if ($support == "1")
225 {
226 ?>
227 <td colspan="10" class="title"><?=$title?></td>
228 <?
229 } else {
230 ?>
231 <td colspan="7" class="title"><?=$title?></td>
232 <?
233 }
234 ?>
235 </tr>
236 <tr>
237 <td class="DataTD"><strong><?=_("ID")?></strong></td>
238 <td class="DataTD"><strong><?=_("Date")?></strong></td>
239 <?
240 if ($support == "1")
241 {
242 ?>
243 <td class="DataTD"><strong><?=_("When")?></strong></td>
244 <td class="DataTD"><strong><?=_("Email")?></strong></td>
245 <?
246 }
247 ?>
248 <td class="DataTD"><strong><?=_("Who")?></strong></td>
249 <td class="DataTD"><strong><?=_("Points")?></strong></td>
250 <td class="DataTD"><strong><?=_("Location")?></strong></td>
251 <td class="DataTD"><strong><?=_("Method")?></strong></td>
252 <td class="DataTD"><strong><?=_("Experience Points")?></strong></td>
253 <?
254 if ($support == "1")
255 {
256 ?>
257 <td class="DataTD"><strong><?=_("Revoke")?></strong></td>
258 <?
259 }
260 ?>
261 </tr>
262 <?
263 }
264
265 function output_assurances_footer($points_txt,$points,$experience_txt,$sumexperience,$support)
266 {
267 ?>
268 <tr>
269 <td<?=($support == "1")?' colspan="5"':' colspan="3"'?> class="DataTD"><strong><?=$points_txt?>:</strong></td>
270 <td class="DataTD"><?=$points?></td>
271 <td class="DataTD">&nbsp;</td>
272 <td class="DataTD"><strong><?=$experience_txt?>:</strong></td>
273 <td class="DataTD"><?=$sumexperience?></td>
274 <?
275 if ($support == "1")
276 {
277 ?>
278 <td class="DataTD">&nbsp;</td>
279 <?
280 }
281 ?>
282
283 </tr>
284 </table>
285 <br/>
286 <?
287 }
288
289 function output_assurances_row($assuranceid,$date,$when,$email,$name,$awarded,$points,$location,$method,$experience,$userid,$support,$revoked)
290 {
291
292 $tdstyle="";
293 $emopen="";
294 $emclose="";
295
296 if ($awarded == $points)
297 {
298 if ($awarded == "0")
299 {
300 if ($when < "2006-09-01")
301 {
302 $tdstyle="style='background-color: #ffff80'";
303 $emopen="<em>";
304 $emclose="</em>";
305 }
306 }
307 }
308 ?>
309 <tr>
310 <td class="DataTD" <?=$tdstyle?>><?=$emopen?><?=$assuranceid?><?=$emclose?></td>
311 <td class="DataTD" <?=$tdstyle?>><?=$emopen?><?=$date?><?=$emclose?></td>
312 <?
313 if ($support == "1")
314 {
315 ?>
316 <td class="DataTD" <?=$tdstyle?>><?=$emopen?><?=$when?><?=$emclose?></td>
317 <td class="DataTD" <?=$tdstyle?>><?=$emopen?><?=$email?><?=$emclose?></td>
318 <?
319 }
320 ?>
321 <td class="DataTD" <?=$tdstyle?>><?=$emopen?><?=$name?><?=$emclose?></td>
322 <td class="DataTD" <?=$tdstyle?>><?=$emopen?><?=$awarded?><?=$emclose?></td>
323 <td class="DataTD" <?=$tdstyle?>><?=$emopen?><?=$location?><?=$emclose?></td>
324 <td class="DataTD" <?=$tdstyle?>><?=$emopen?><?=$method?><?=$emclose?></td>
325 <td class="DataTD" <?=$tdstyle?>><?=$emopen?><?=$experience?><?=$emclose?></td>
326 <?
327 if ($support == "1")
328 {
329 if ($revoked == true)
330 {
331 ?>
332 <td class="DataTD" <?=$tdstyle?>>&nbsp;</td>
333 <?
334 } else {
335 ?>
336 <td class="DataTD" <?=$tdstyle?>><?=$emopen?><a href="account.php?id=43&amp;userid=<?=intval($userid)?>&amp;assurance=<?=intval($assuranceid)?>&amp;csrf=<?=make_csrf('admdelassurance')?>" onclick="return confirm('<?=sprintf(_("Are you sure you want to revoke the assurance with ID &quot;%s&quot;?"),$assuranceid)?>');"><?=_("Revoke")?></a><?=$emclose?></td>
337 <?
338 }
339 }
340 ?>
341 </tr>
342 <?
343 }
344
345 function output_summary_header()
346 {
347 ?>
348 <table align="center" valign="middle" border="0" cellspacing="0" cellpadding="0" class="wrapper">
349 <tr>
350 <td colspan="4" class="title"><?=_("Summary of your Points")?></td>
351 </tr>
352 <tr>
353 <td class="DataTD"><strong><?=_("Description")?></strong></td>
354 <td class="DataTD"><strong><?=_("Points")?></strong></td>
355 <td class="DataTD"><strong><?=_("Countable Points")?></strong></td>
356 <td class="DataTD"><strong><?=_("Remark")?></strong></td>
357 </tr>
358 <?
359 }
360
361 function output_summary_footer()
362 {
363 ?>
364 </table>
365 <br/>
366 <?
367 }
368
369 function output_summary_row($title,$points,$points_countable,$remark)
370 {
371 ?>
372 <tr>
373 <td class="DataTD"><strong><?=$title?></strong></td>
374 <td class="DataTD"><?=$points?></td>
375 <td class="DataTD"><?=$points_countable?></td>
376 <td class="DataTD"><?=$remark?></td>
377 </tr>
378 <?
379 }
380
381
382 // ************* output given assurances ******************
383
384 function output_given_assurances_content($userid,&$points,&$sum_experience,$support)
385 {
386 $points = 0;
387 $sumexperience = 0;
388 $res = get_given_assurances(intval($userid));
389 while($row = mysql_fetch_assoc($res))
390 {
391 $fromuser = get_user (intval($row['to']));
392 $apoints = calc_experience ($row,$points,$experience,$sum_experience,$revoked);
393 $name = show_user_link ($fromuser['fname']." ".$fromuser['lname'],intval($row['to']));
394 $email = show_email_link ($fromuser['email'],intval($row['to']));
395 output_assurances_row (intval($row['id']),$row['date'],$row['when'],$email,$name,$apoints,intval($row['points']),$row['location'],$row['method']==""?"":_(sprintf("%s", $row['method'])),$experience,$userid,$support,$revoked);
396 }
397 }
398
399 // ************* output received assurances ******************
400
401 function output_received_assurances_content($userid,&$points,&$sum_experience,$support)
402 {
403 $points = 0;
404 $sumexperience = 0;
405 $res = get_received_assurances(intval($userid));
406 while($row = mysql_fetch_assoc($res))
407 {
408 $fromuser = get_user (intval($row['from']));
409 calc_assurances ($row,$points,$experience,$sum_experience,$awarded,$revoked);
410 $name = show_user_link ($fromuser['fname']." ".$fromuser['lname'],intval($row['from']));
411 $email = show_email_link ($fromuser['email'],intval($row['from']));
412 output_assurances_row (intval($row['id']),$row['date'],$row['when'],$email,$name,$awarded,intval($row['points']),$row['location'],$row['method']==""?"":_(sprintf("%s", $row['method'])),$experience,$userid,$support,$revoked);
413 }
414 }
415
416 // ************* output summary table ******************
417
418 function check_date_limit ($userid,$age)
419 {
420 $dob = date("Y-m-d", mktime(0,0,0,date("m"),date("d"),date("Y")-$age));
421 $res = query_init ("select id from `users` where `id`='".$userid."' and `dob` < '$dob'");
422 return intval(query_get_number_of_rows($res));
423 }
424
425 function calc_points($row)
426 {
427 $awarded = intval($row['awarded']);
428 if ($awarded == "")
429 $awarded = 0;
430 if (intval($row['points']) < $awarded)
431 $points = $awarded; // if 'sum of added points' > 100, awarded shows correct value
432 else
433 $points = intval($row['points']); // on very old assurances, awarded is '0' instead of correct value
434 switch ($row['method'])
435 {
436 case 'Thawte Points Transfer': // revoke all Thawte-points (as per arbitration)
437 case 'CT Magazine - Germany': // revoke c't (only one test-entry)
438 case 'Temporary Increase': // revoke 'temporary increase' (Current usage breaks audit aspects, needs to be reimplemented)
439 $points = 0;
440 break;
441 case 'Administrative Increase': // ignore AI with 2 points or less (historical for experiance points, now other calculation)
442 if ($points <= 2) // maybe limit to 35/50 pts in the future?
443 $points = 0;
444 break;
445 case 'Unknown': // to be revoked in the future? limit to max 50 pts?
446 case 'Trusted Third Parties': // to be revoked in the future? limit to max 35 pts?
447 case 'TTP-Assisted': // TTP assurances, limit to 35
448 case 'TOPUP': // TOPUP to be delevoped in the future, limit to 30
449 case '': // to be revoked in the future? limit to max 50 pts?
450 case 'Face to Face Meeting': // normal assurances, limit to 35/50 pts in the future?
451 break;
452 default: // should never happen ... ;-)
453 $points = 0;
454 }
455 if ($points < 0) // ignore negative points (bug needs to be fixed)
456 $points = 0;
457 return $points;
458 }
459
460 function max_points($userid)
461 {
462 return output_summary_content ($userid,0);
463 }
464
465 function output_summary_content($userid,$display_output)
466 {
467 $sum_points = 0;
468 $sum_experience = 0;
469 $sum_experience_other = 0;
470 $max_points = 100;
471 $max_experience = 50;
472
473 $experience_limit_reached_txt = _("Limit reached");
474
475 if (check_date_limit($userid,18) != 1)
476 {
477 $max_experience = 10;
478 $experience_limit_reached_txt = _("Limit given by PoJAM reached");
479 }
480 if (check_date_limit($userid,14) != 1)
481 {
482 $max_experience = 0;
483 $experience_limit_reached_txt = _("Limit given by PoJAM reached");
484 }
485
486 $res = get_received_assurances_summary($userid);
487 while($row = mysql_fetch_assoc($res))
488 {
489 $points = calc_points ($row);
490
491 if ($points > $max_points) // limit to 100 points, above is experience (needs to be fixed)
492 {
493 $sum_experience_other = $sum_experience_other+($points-$max_points)*intval($row['number']);
494 $points = $max_points;
495 }
496 $sum_points += $points*intval($row['number']);
497 }
498
499 $res = get_given_assurances_summary($userid);
500 while($row = mysql_fetch_assoc($res))
501 {
502 switch ($row['method'])
503 {
504 case 'Face to Face Meeting': // count Face to Face only
505 $sum_experience += 2*intval($row['number']);
506 break;
507 }
508
509 }
510
511 if ($sum_points > $max_points)
512 {
513 $sum_points_countable = $max_points;
514 $remark_points = _("Limit reached");
515 }
516 else
517 {
518 $sum_points_countable = $sum_points;
519 $remark_points = "&nbsp;";
520 }
521 if ($sum_experience > $max_experience)
522 {
523 $sum_experience_countable = $max_experience;
524 $remark_experience = $experience_limit_reached_txt;
525 }
526 else
527 {
528 $sum_experience_countable = $sum_experience;
529 $remark_experience = "&nbsp;";
530 }
531
532 if ($sum_experience_countable + $sum_experience_other > $max_experience)
533 {
534 $sum_experience_other_countable = $max_experience-$sum_experience_countable;
535 $remark_experience_other = $experience_limit_reached_txt;
536 }
537 else
538 {
539 $sum_experience_other_countable = $sum_experience_other;
540 $remark_experience_other = "&nbsp;";
541 }
542
543 if ($sum_points_countable < $max_points)
544 {
545 if ($sum_experience_countable != 0)
546 $remark_experience = _("Points on hold due to less assurance points");
547 $sum_experience_countable = 0;
548 if ($sum_experience_other_countable != 0)
549 $remark_experience_other = _("Points on hold due to less assurance points");
550 $sum_experience_other_countable = 0;
551 }
552
553 $issue_points = 0;
554 $cats_test_passed = get_cats_state ($userid);
555 if ($cats_test_passed == 0)
556 {
557 $issue_points_txt = "<strong style='color: red'>"._("You have to pass the CAcert Assurer Challenge (CATS-Test) to be an Assurer")."</strong>";
558 if ($sum_points_countable < $max_points)
559 {
560 $issue_points_txt = "<strong style='color: red'>";
561 $issue_points_txt .= sprintf(_("You need %s assurance points and the passed CATS-Test to be an Assurer"), intval($max_points));
562 $issue_points_txt .= "</strong>";
563 }
564 }
565 else
566 {
567 $experience_total = $sum_experience_countable+$sum_experience_other_countable;
568 $issue_points_txt = "";
569 if ($sum_points_countable == $max_points)
570 $issue_points = 10;
571 if ($experience_total >= 10)
572 $issue_points = 15;
573 if ($experience_total >= 20)
574 $issue_points = 20;
575 if ($experience_total >= 30)
576 $issue_points = 25;
577 if ($experience_total >= 40)
578 $issue_points = 30;
579 if ($experience_total >= 50)
580 $issue_points = 35;
581 if ($issue_points != 0)
582 $issue_points_txt = sprintf(_("You may issue up to %s points"),$issue_points);
583 }
584 if ($display_output)
585 {
586 output_summary_row (_("Assurance Points you received"),$sum_points,$sum_points_countable,$remark_points);
587 output_summary_row (_("Total Experience Points by Assurance"),$sum_experience,$sum_experience_countable,$remark_experience);
588 output_summary_row (_("Total Experience Points (other ways)"),$sum_experience_other,$sum_experience_other_countable,$remark_experience_other);
589 output_summary_row (_("Total Points"),"&nbsp;",$sum_points_countable + $sum_experience_countable + $sum_experience_other_countable,$issue_points_txt);
590 }
591 return $issue_points;
592 }
593
594 function output_given_assurances($userid,$support=0)
595 {
596 output_assurances_header(_("Assurance Points You Issued"),$support);
597 output_given_assurances_content($userid,$points,$sum_experience,$support);
598 output_assurances_footer(_("Total Points Issued"),$points,_("Total Experience Points"),$sum_experience,$support);
599 }
600
601 function output_received_assurances($userid,$support=0)
602 {
603 output_assurances_header(_("Your Assurance Points"),$support);
604 output_received_assurances_content($userid,$points,$sum_experience,$support);
605 output_assurances_footer(_("Total Assurance Points"),$points,_("Total Experience Points"),$sum_experience,$support);
606 }
607
608 function output_summary($userid)
609 {
610 output_summary_header();
611 output_summary_content($userid,1);
612 output_summary_footer();
613 }
614
615 function output_end_of_page()
616 {
617 ?>
618 <p>[ <a href='javascript:history.go(-1)'><?=_("Go Back")?></a> ]</p>
619 <?
620 }
621
622 //functions to do with recording user agreements
623 function write_user_agreement($memid, $document, $method, $comment, $active=1, $secmemid=0){
624 // write a new record to the table user_agreement
625 $query="insert into `user_agreements` set `memid`=".$memid.", `secmemid`=".$secmemid.
626 ",`document`='".$document."',`date`=NOW(), `active`=".$active.",`method`='".$method."',`comment`='".$comment."'" ;
627 $res = mysql_query($query);
628 }
629
630 function get_user_agreement_status($memid, $type="CCA"){
631 //returns 0 - no user agreement, 1- at least one entry
632 $query="SELECT u.`document` FROM `user_agreements` u
633 WHERE u.`document` = '".$type."' AND (u.`memid`=".$memid." or u.`secmemid`=".$memid.")" ;
634 $res = mysql_query($query);
635 if(mysql_num_rows($res) <=0){
636 return 0;
637 }else{
638 return 1;
639 }
640 }
641
642 function get_first_user_agreement($memid, $active=1, $type="CCA"){
643 //returns an array (`document`,`date`,`method`, `comment`,`active`)
644 if($active==1){
645 $filter="u.`memid`=".$memid;
646 }else{
647 $filter="u.`secmemid`=".$memid;
648 }
649 $query="SELECT u.`document`, u.`date`, u.`method`, u.`comment`, u.`active` FROM `user_agreements` u
650 WHERE u.`document` = '".$type."' AND ".$filter."
651 ORDER BY u.`date` Limit 1;";
652 $res = mysql_query($query);
653 if(mysql_num_rows($res) >0){
654 $row = mysql_fetch_assoc($res);
655 $rec['document']= $row['document'];
656 $rec['date']= $row['date'];
657 $rec['method']= $row['method'];
658 $rec['comment']= $row['comment'];
659 $rec['active']= $row['active'];
660 }else{
661 $rec=array();
662 }
663 return $rec;
664 }
665
666 function get_last_user_agreement($memid, $type="CCA"){
667 //returns an array (`document`,`date`,`method`, `comment`,`active`)
668 $query="(SELECT u.`document`, u.`date`, u.`method`, u.`comment`, 1 as `active` FROM user_agreements u WHERE u.`document` = '".$type."' AND (u.`memid`=".$memid." ) order by `date` desc limit 1)
669 union
670 (SELECT u.`document`, u.`date`, u.`method`, u.`comment`, 0 as `active` FROM user_agreements u WHERE u.`document` = '".$type."' AND ( u.`secmemid`=".$memid.")) order by `date` desc limit 1" ;
671 $res = mysql_query($query);
672 if(mysql_num_rows($res) >0){
673 $row = mysql_fetch_assoc($res);
674 $rec['document']= $row['document'];
675 $rec['date']= $row['date'];
676 $rec['method']= $row['method'];
677 $rec['comment']= $row['comment'];
678 $rec['active']= $row['active'];
679 }else{
680 $rec=array();
681 }
682 return $rec;
683 }
684
685 function delete_user_agreement($memid, $type="CCA"){
686 //deletes all entries to an user for the given type of user agreements
687 mysql_query("delete from `user_agreements` where `memid`='".$memid."'");
688 mysql_query("delete from `user_agreements` where `secmemid`='".$memid."'");
689 }
690
691 // functions for 6.php (assure somebody)
692
693 function AssureHead($confirmation,$checkname)
694 {
695 ?>
696 <form method="post" action="wot.php">
697 <table align="center" valign="middle" border="0" cellspacing="0" cellpadding="0" class="wrapper" width="600">
698 <tr>
699 <td colspan="2" class="title"><?=$confirmation?></td>
700 </tr>
701 <tr>
702 <td class="DataTD" colspan="2" align="left"><?=$checkname?></td>
703 </tr>
704 <?
705 }
706
707 function AssureTextLine($field1,$field2)
708 {
709 ?>
710 <tr>
711 <td class="DataTD"><?=$field1?>:</td>
712 <td class="DataTD"><?=$field2?></td>
713 </tr>
714 <?
715 }
716
717 function AssureCCABoxLine($type,$text)
718 {
719 return;
720 AssureBoxLine($type,$text);
721 }
722
723 function AssureBoxLine($type,$text,$checked)
724 {
725 ?>
726 <tr>
727 <td class="DataTD"><input type="checkbox" name="<?=$type?>" value="1" <?=$checked?"checked":""?>></td>
728 <td class="DataTD"><?=$text?></td>
729 </tr>
730 <?
731 }
732
733 function AssureMethodLine($text,$methods,$remark)
734 {
735 if (count($methods) != 1) {
736 ?>
737 <tr>
738 <td class="DataTD"><?=$text?></td>
739 <td class="DataTD">
740 <select name="method">
741 <?
742 foreach($methods as $val) {
743 ?>
744 <option value="<?=$val?>"><?=$val?></option>
745 <?
746 }
747 ?>
748 </select>
749 <br />
750 <?=$remark?>
751 </td>
752 </tr>
753 <?
754 } else {
755 ?>
756 <input type="hidden" name="<?=$val?>" value="<?=$methods[0]?>" />
757 <?
758 }
759 }
760
761 function AssureInboxLine($type,$field,$value,$description)
762 {
763 ?>
764 <tr>
765 <td class="DataTD"><?=$field?>:</td>
766 <td class="DataTD"><input type="text" name="<?=$type?>" value="<?=$value?>"><?=$description?></td>
767 </tr>
768 <?
769 }
770
771 function AssureFoot($oldid,$confirm)
772 {
773 ?>
774 <tr>
775 <td class="DataTD" colspan="2">
776 <input type="submit" name="process" value="<?=$confirm?>" />
777 <input type="submit" name="cancel" value="<?=_("Cancel")?>" />
778 </td>
779 </tr>
780 </table>
781 <input type="hidden" name="pagehash" value="<?=$_SESSION['_config']['wothash']?>" />
782 <input type="hidden" name="oldid" value="<?=$oldid?>" />
783 </form>
784 <?
785 }
786
787 function account_email_delete($mailid){
788 //deletes an email entry from an acount
789 //revolkes all certifcates for that email address
790 //called from www/account.php if($process != "" && $oldid == 2)
791 //called from www/diputes.php if($type == "reallyemail") / if($action == "accept")
792 //called from account_delete
793 $mailid = intval($mailid);
794 revoke_all_client_cert($mailid);
795 $query = "update `email` set `deleted`=NOW() where `id`='$mailid'";
796 mysql_query($query);
797 }
798
799 function account_domain_delete($domainid){
800 //deletes an domain entry from an acount
801 //revolkes all certifcates for that domain address
802 //called from www/account.php if($process != "" && $oldid == 9)
803 //called from www/diputes.php if($type == "reallydomain") / if($action == "accept")
804 //called from account_delete
805 $domainid = intval($domainid);
806 revoke_all_server_cert($domainid);
807 mysql_query(
808 "update `domains`
809 set `deleted`=NOW()
810 where `id` = '$domainid'");
811 }
812
813 function account_delete($id, $arbno, $adminid){
814 //deletes an account following the deleted account routnie V3
815 // called from www/account.php if($oldid == 50 && $process != "")
816 //change password
817 $id = intval($id);
818 $arbno = mysql_real_escape_string($arbno);
819 $adminid = intval($adminid);
820 $pool = 'abcdefghijklmnopqrstuvwxyz';
821 $pool .= '0123456789!()ยง';
822 $pool .= 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
823 srand ((double)microtime()*1000000);
824 $password="";
825 for($index = 0; $index < 30; $index++)
826 {
827 $password .= substr($pool,(rand()%(strlen ($pool))), 1);
828 }
829 mysql_query("update `users` set `password`=sha1('".$password."') where `id`='".$id."'");
830
831 //create new mail for arbitration number
832 $query = "insert into `email` set `email`='".$arbno."@cacert.org',`memid`='".$id."',`created`=NOW(),`modified`=NOW(), `attempts`=-1";
833 mysql_query($query);
834 $emailid = mysql_insert_id();
835
836 //set new mail as default
837 $query = "update `users` set `email`='".$arbno."@cacert.org' where `id`='".$id."'";
838 mysql_query($query);
839
840 //delete all other email address
841 $query = "select `id` from `email` where `memid`='".$id."' and `id`!='".$emailid."'" ;
842 $res=mysql_query($query);
843 while($row = mysql_fetch_assoc($res)){
844 account_email_delete($row['id']);
845 }
846
847 //delete all domains
848 $query = "select `id` from `domains` where `memid`='".$id."'";
849 $res=mysql_query($query);
850 while($row = mysql_fetch_assoc($res)){
851 account_domain_delete($row['id']);
852 }
853
854 //clear alert settings
855 mysql_query(
856 "update `alerts` set
857 `general`='0',
858 `country`='0',
859 `regional`='0',
860 `radius`='0'
861 where `memid`='$id'");
862
863 //set default location
864 $query = "update `users` set `locid`='2256755', `regid`='243', `ccid`='12' where `id`='".$id."'";
865 mysql_query($query);
866
867 //clear listings
868 $query = "update `users` set `listme`=' ',`contactinfo`=' ' where `id`='".$id."'";
869 mysql_query($query);
870
871 //set lanuage to default
872 //set default language
873 mysql_query("update `users` set `language`='en_AU' where `id`='".$id."'");
874 //delete secondary langugaes
875 mysql_query("delete from `addlang` where `userid`='".$id."'");
876
877 //change secret questions
878 for($i=1;$i<=5;$i++){
879 $q="";
880 $a="";
881 for($index = 0; $index < 30; $index++)
882 {
883 $q .= substr($pool,(rand()%(strlen ($pool))), 1);
884 $a .= substr($pool,(rand()%(strlen ($pool))), 1);
885 }
886 $query = "update `users` set `Q$i`='$q', `A$i`='$a' where `id`='".$id."'";
887 mysql_query($query);
888 }
889
890 //change personal information to arbitration number and DOB=1900-01-01
891 $query = "select `fname`,`mname`,`lname`,`suffix`,`dob` from `users` where `id`='$userid'";
892 $details = mysql_fetch_assoc(mysql_query($query));
893 $query = "insert into `adminlog` set `when`=NOW(),`old-lname`='${details['lname']}',`old-dob`='${details['dob']}',
894 `new-lname`='$arbno',`new-dob`='1900-01-01',`uid`='$id',`adminid`='".$adminid."'";
895 mysql_query($query);
896 $query = "update `users` set `fname`='".$arbno."',
897 `mname`='".$arbno."',
898 `lname`='".$arbno."',
899 `suffix`='".$arbno."',
900 `dob`='1900-01-01'
901 where `id`='".$id."'";
902 mysql_query($query);
903
904 //clear all admin and board flags
905 mysql_query(
906 "update `users` set
907 `assurer`='0',
908 `assurer_blocked`='0',
909 `codesign`='0',
910 `orgadmin`='0',
911 `ttpadmin`='0',
912 `locadmin`='0',
913 `admin`='0',
914 `adadmin`='0',
915 `tverify`='0',
916 `board`='0'
917 where `id`='$id'");
918
919 //block account
920 mysql_query("update `users` set `locked`='1' where `id`='$id'"); //, `deleted`=Now()
921 }
922
923
924 function check_email_exists($email){
925 // called from includes/account.php if($process != "" && $oldid == 1)
926 // called from includes/account.php if($oldid == 50 && $process != "")
927 $email = mysql_real_escape_string($email);
928 $query = "select * from `email` where `email`='$email' and `deleted`=0";
929 $res = mysql_query($query);
930 return mysql_num_rows($res) > 0;
931 }
932
933 function check_gpg_cert_running($uid,$cca=0){
934 //if $cca =0 if just expired, =1 if CCA retention +3 month should be obeyed
935 // called from includes/account.php if($oldid == 50 && $process != "")
936 $uid = intval($uid);
937 if (0==$cca) {
938 $query = "select * from `gpg` where `memid`='$uid' and `expire`>NOW()";
939 }else{
940 $query = "select * from `gpg` where `memid`='$uid' and `expire`>NOW()+90*86400";
941 }
942 $res = mysql_query($query);
943 return mysql_num_rows($res) > 0;
944 }
945
946 function check_client_cert_running($uid,$cca=0){
947 //if $cca =0 if just expired, =1 if CCA retention +3 month should be obeyed
948 // called from includes/account.php if($oldid == 50 && $process != "")
949 $uid = intval($uid);
950 if (0==$cca) {
951 $query1 = "select 1 from `emailcerts` where `memid`='$uid' and `expire`>NOW() and `revoked`<`created`";
952 $query2 = "select 1 from `emailcerts` where `memid`='$uid' and `revoked`>NOW()";
953 }else{
954 $query1 = "select 1 from `emailcerts` where `memid`='$uid' and `expire`>(NOW()-90*86400) and `revoked`<`created`";
955 $query2 = "select 1 from `emailcerts` where `memid`='$uid' and `revoked`>(NOW()-90*86400)";
956 }
957 $res = mysql_query($query1);
958 $r1 = mysql_num_rows($res)>0;
959 $res = mysql_query($query2);
960 $r2 = mysql_num_rows($res)>0;
961 return !!($r1 || $r2);
962 }
963
964 function check_server_cert_running($uid,$cca=0){
965 //if $cca =0 if just expired, =1 if CCA retention +3 month should be obeyed
966 // called from includes/account.php if($oldid == 50 && $process != "")
967 $uid = intval($uid);
968 if (0==$cca) {
969 $query1 = "select 1 from `domaincerts` where `memid`='$uid' and `expire`>NOW() and `revoked`<`created`";
970 $query2 = "select 1 from `domaincerts` where `memid`='$uid' and `revoked`>NOW()";
971 }else{
972 $query1 = "select 1 from `domaincerts` where `memid`='$uid' and `expire`>(NOW()-90*86400) and `revoked`<`created`";
973 $query2 = "select 1 from `domaincerts` where `memid`='$uid' and `revoked`>(NOW()-90*86400)";
974 }
975 $res = mysql_query($query1);
976 $r1 = mysql_num_rows($res)>0;
977 $res = mysql_query($query2);
978 $r2 = mysql_num_rows($res)>0;
979 return !!($r1 || $r2);
980 }
981
982 function check_is_orgadmin($uid){
983 // called from includes/account.php if($oldid == 50 && $process != "")
984 $uid = intval($uid);
985 $query = "select * from `org` where `memid`='$uid' and `deleted`=0";
986 $res = mysql_query($query);
987 return mysql_num_rows($res) > 0;
988 }
989
990
991 // revokation of certificates
992 function revoke_all_client_cert($mailid){
993 //revokes all client certificates for an email address
994 $mailid = intval($mailid);
995 $query = "select `emailcerts`.`id`
996 from `emaillink`,`emailcerts` where
997 `emailid`='$mailid' and `emaillink`.`emailcertsid`=`emailcerts`.`id` and `revoked`=0
998 group by `emailcerts`.`id`";
999 $dres = mysql_query($query);
1000 while($drow = mysql_fetch_assoc($dres)){
1001 mysql_query("update `emailcerts` set `revoked`='1970-01-01 10:00:01', `disablelogin`=1 where `id`='".$drow['id']."'");
1002 }
1003 }
1004
1005 function revoke_all_server_cert($domainid){
1006 //revokes all server certs for an domain
1007 $domainid = intval($domainid);
1008 $query = "select distinct `domaincerts`.`id`
1009 from `domaincerts`, `domlink`
1010 where `domaincerts`.`domid` = '$domainid'
1011 or (
1012 `domaincerts`.`id` = `domlink`.`certid`
1013 and `domlink`.`domid` = '$domainid')";
1014 $dres = mysql_query($query);
1015 while($drow = mysql_fetch_assoc($dres))
1016 {
1017 mysql_query(
1018 "update `domaincerts`
1019 set `revoked`='1970-01-01 10:00:01'
1020 where `id` = '".$drow['id']."'
1021 and `revoked` = 0");
1022 }
1023 }
1024
1025 function revoke_all_private_cert($uid){
1026 //revokes all certificates linked to a personal accounts
1027 //gpg revokation needs to be added to a later point
1028 $uid=intval($uid);
1029 $query = "select `id` from `email` where `memid`='".$uid."'";
1030 $res=mysql_query($query);
1031 while($row = mysql_fetch_assoc($res)){
1032 revoke_all_client_cert($row['id']);
1033 }
1034
1035
1036 $query = "select `id` from `domains` where `memid`='".$uid."'";
1037 $res=mysql_query($query);
1038 while($row = mysql_fetch_assoc($res)){
1039 revoke_all_server_cert($row['id']);
1040 }
1041
1042 }