Merge remote-tracking branch 'origin/bug-1239' into release
[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 /**
624 * write_user_agreement()
625 * writes a new record to the table user_agreement
626 *
627 * @param mixed $memid
628 * @param mixed $document
629 * @param mixed $method
630 * @param mixed $comment
631 * @param integer $active
632 * @param integer $secmemid
633 * @return
634 */
635 function write_user_agreement($memid, $document, $method, $comment, $active=1, $secmemid=0){
636 // write a new record to the table user_agreement
637 $query="insert into `user_agreements` set `memid`=".intval($memid).", `secmemid`=".intval($secmemid).
638 ",`document`='".mysql_real_escape_string($document)."',`date`=NOW(), `active`=".intval($active).",`method`='".mysql_real_escape_string($method)."',`comment`='".mysql_real_escape_string($comment)."'" ;
639 $res = mysql_query($query);
640 }
641
642 /**
643 * get_user_agreement_status()
644 * returns 1 if the user has an entry for the given type in user_agreement, 0 if no entry is recorded
645 * @param mixed $memid
646 * @param string $type
647 * @return
648 */
649 function get_user_agreement_status($memid, $type="CCA"){
650 $query="SELECT u.`document` FROM `user_agreements` u
651 WHERE u.`document` = '" . mysql_real_escape_string($type) . "' AND u.`memid`=" . intval($memid) ;
652 $res = mysql_query($query);
653 if(mysql_num_rows($res) <=0){
654 return 0;
655 }else{
656 return 1;
657 }
658 }
659
660 /**
661 * get_first_user_agreement()
662 * returns the first user_agreement entry of the requested type depending on thes status of active of a given user
663 * @param mixed $memid
664 * @param integer $active, 0 - passive, 1 -active
665 * @param string $type
666 * @return
667 */
668 function get_first_user_agreement($memid, $active=1, $type="CCA"){
669 //returns an array (`document`,`date`,`method`, `comment`,`active`)
670 $query="SELECT u.`document`, u.`date`, u.`method`, u.`comment`, u.`active` FROM `user_agreements` AS u
671 WHERE u.`document` = '" . mysql_real_escape_string($type) . "' AND u.`memid`=" . intval($memid) . " AND u.`active`=" . intval($active) .
672 " ORDER BY u.`date` Limit 1;";
673 $res = mysql_query($query);
674 if(mysql_num_rows($res) >0){
675 $rec = mysql_fetch_assoc($res);
676 }else{
677 $rec=array();
678 }
679 return $rec;
680 }
681
682 /**
683 * get_last_user_agreement()
684 * returns the last user_agreement entry of a given type and of a given user
685 * @param mixed $memid
686 * @param string $type
687 * @return
688 */
689 function get_last_user_agreement($memid, $type="CCA"){
690 //returns an array (`document`,`date`,`method`, `comment`,`active`)
691 $query="SELECT u.`document`, u.`date`, u.`method`, u.`comment`, u.`active` FROM user_agreements u WHERE u.`document` = '" . mysql_real_escape_string($type) . "' AND (u.`memid`=" . intval($memid) . " ) order by `date` desc limit 1 " ;
692 $res = mysql_query($query);
693 if(mysql_num_rows($res) >0){
694 $rec = mysql_fetch_assoc($res);
695 }else{
696 $rec=array();
697 }
698 return $rec;
699 }
700
701 /**
702 * delete_user_agreement()
703 * deletes all entries for a given type from user_agreement of a given user, if type is not given all
704 * @param mixed $memid
705 * @param string $type
706 * @return
707 */
708 function delete_user_agreement($memid, $type=false){
709 //deletes all entries to an user for the given type of user agreements
710 if ($type === false) {
711 $filter = '';
712 } else {
713 $filter = " and `document` = '" . mysql_real_escape_string($type) . "'";
714 }
715 mysql_query("delete from `user_agreements` where `memid`=" . intval($memid) . $filter );
716 }
717
718 // functions for 6.php (assure somebody)
719
720 function AssureHead($confirmation,$checkname)
721 {
722 ?>
723 <form method="post" action="wot.php">
724 <table align="center" valign="middle" border="0" cellspacing="0" cellpadding="0" class="wrapper" width="600">
725 <tr>
726 <td colspan="2" class="title"><?=$confirmation?></td>
727 </tr>
728 <tr>
729 <td class="DataTD" colspan="2" align="left"><?=$checkname?></td>
730 </tr>
731 <?
732 }
733
734 function AssureTextLine($field1,$field2)
735 {
736 ?>
737 <tr>
738 <td class="DataTD"><?=$field1.(empty($field1)?'':':')?></td>
739 <td class="DataTD"><?=$field2?></td>
740 </tr>
741 <?
742 }
743
744 function AssureBoxLine($type,$text,$checked)
745 {
746 ?>
747 <tr>
748 <td class="DataTD"><input type="checkbox" name="<?=$type?>" value="1" <?=$checked?"checked":""?>></td>
749 <td class="DataTD"><?=$text?></td>
750 </tr>
751 <?
752 }
753
754 function AssureMethodLine($text,$methods,$remark)
755 {
756 if (count($methods) != 1) {
757 ?>
758 <tr>
759 <td class="DataTD"><?=$text.(empty($text)?'':':')?></td>
760 <td class="DataTD">
761 <select name="method">
762 <?
763 foreach($methods as $val) {
764 ?>
765 <option value="<?=$val?>"><?=$val?></option>
766 <?
767 }
768 ?>
769 </select>
770 <br />
771 <?=$remark?>
772 </td>
773 </tr>
774 <?
775 } else {
776 ?>
777 <input type="hidden" name="<?=$val?>" value="<?=$methods[0]?>" />
778 <?
779 }
780 }
781
782 function AssureInboxLine($type,$field,$value,$description)
783 {
784 ?>
785 <tr>
786 <td class="DataTD"><?=$field.(empty($field)?'':':')?></td>
787 <td class="DataTD"><input type="text" name="<?=$type?>" value="<?=$value?>"><?=$description?></td>
788 </tr>
789 <?
790 }
791
792 function AssureFoot($oldid,$confirm)
793 {
794 ?>
795 <tr>
796 <td class="DataTD" colspan="2">
797 <input type="submit" name="process" value="<?=$confirm?>" />
798 <input type="submit" name="cancel" value="<?=_("Cancel")?>" />
799 </td>
800 </tr>
801 </table>
802 <input type="hidden" name="pagehash" value="<?=$_SESSION['_config']['wothash']?>" />
803 <input type="hidden" name="oldid" value="<?=$oldid?>" />
804 </form>
805 <?
806 }
807
808 function account_email_delete($mailid){
809 //deletes an email entry from an acount
810 //revolkes all certifcates for that email address
811 //called from www/account.php if($process != "" && $oldid == 2)
812 //called from www/diputes.php if($type == "reallyemail") / if($action == "accept")
813 //called from account_delete
814 $mailid = intval($mailid);
815 revoke_all_client_cert($mailid);
816 $query = "update `email` set `deleted`=NOW() where `id`='$mailid'";
817 mysql_query($query);
818 }
819
820 function account_domain_delete($domainid){
821 //deletes an domain entry from an acount
822 //revolkes all certifcates for that domain address
823 //called from www/account.php if($process != "" && $oldid == 9)
824 //called from www/diputes.php if($type == "reallydomain") / if($action == "accept")
825 //called from account_delete
826 $domainid = intval($domainid);
827 revoke_all_server_cert($domainid);
828 mysql_query(
829 "update `domains`
830 set `deleted`=NOW()
831 where `id` = '$domainid'");
832 }
833
834 function account_delete($id, $arbno, $adminid){
835 //deletes an account following the deleted account routnie V3
836 // called from www/account.php if($oldid == 50 && $process != "")
837 //change password
838 $id = intval($id);
839 $arbno = mysql_real_escape_string($arbno);
840 $adminid = intval($adminid);
841 $pool = 'abcdefghijklmnopqrstuvwxyz';
842 $pool .= '0123456789!()ยง';
843 $pool .= 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
844 srand ((double)microtime()*1000000);
845 $password="";
846 for($index = 0; $index < 30; $index++)
847 {
848 $password .= substr($pool,(rand()%(strlen ($pool))), 1);
849 }
850 mysql_query("update `users` set `password`=sha1('".$password."') where `id`='".$id."'");
851
852 //create new mail for arbitration number
853 $query = "insert into `email` set `email`='".$arbno."@cacert.org',`memid`='".$id."',`created`=NOW(),`modified`=NOW(), `attempts`=-1";
854 mysql_query($query);
855 $emailid = mysql_insert_id();
856
857 //set new mail as default
858 $query = "update `users` set `email`='".$arbno."@cacert.org' where `id`='".$id."'";
859 mysql_query($query);
860
861 //delete all other email address
862 $query = "select `id` from `email` where `memid`='".$id."' and `id`!='".$emailid."'" ;
863 $res=mysql_query($query);
864 while($row = mysql_fetch_assoc($res)){
865 account_email_delete($row['id']);
866 }
867
868 //delete all domains
869 $query = "select `id` from `domains` where `memid`='".$id."'";
870 $res=mysql_query($query);
871 while($row = mysql_fetch_assoc($res)){
872 account_domain_delete($row['id']);
873 }
874
875 //clear alert settings
876 mysql_query(
877 "update `alerts` set
878 `general`='0',
879 `country`='0',
880 `regional`='0',
881 `radius`='0'
882 where `memid`='$id'");
883
884 //set default location
885 $query = "update `users` set `locid`='2256755', `regid`='243', `ccid`='12' where `id`='".$id."'";
886 mysql_query($query);
887
888 //clear listings
889 $query = "update `users` set `listme`=' ',`contactinfo`=' ' where `id`='".$id."'";
890 mysql_query($query);
891
892 //set lanuage to default
893 //set default language
894 mysql_query("update `users` set `language`='en_AU' where `id`='".$id."'");
895 //delete secondary langugaes
896 mysql_query("delete from `addlang` where `userid`='".$id."'");
897
898 //change secret questions
899 for($i=1;$i<=5;$i++){
900 $q="";
901 $a="";
902 for($index = 0; $index < 30; $index++)
903 {
904 $q .= substr($pool,(rand()%(strlen ($pool))), 1);
905 $a .= substr($pool,(rand()%(strlen ($pool))), 1);
906 }
907 $query = "update `users` set `Q$i`='$q', `A$i`='$a' where `id`='".$id."'";
908 mysql_query($query);
909 }
910
911 //change personal information to arbitration number and DOB=1900-01-01
912 $query = "select `fname`,`mname`,`lname`,`suffix`,`dob` from `users` where `id`='$userid'";
913 $details = mysql_fetch_assoc(mysql_query($query));
914 $query = "insert into `adminlog` set `when`=NOW(),`old-lname`='${details['lname']}',`old-dob`='${details['dob']}',
915 `new-lname`='$arbno',`new-dob`='1900-01-01',`uid`='$id',`adminid`='".$adminid."'";
916 mysql_query($query);
917 $query = "update `users` set `fname`='".$arbno."',
918 `mname`='".$arbno."',
919 `lname`='".$arbno."',
920 `suffix`='".$arbno."',
921 `dob`='1900-01-01'
922 where `id`='".$id."'";
923 mysql_query($query);
924
925 //clear all admin and board flags
926 mysql_query(
927 "update `users` set
928 `assurer`='0',
929 `assurer_blocked`='0',
930 `codesign`='0',
931 `orgadmin`='0',
932 `ttpadmin`='0',
933 `locadmin`='0',
934 `admin`='0',
935 `adadmin`='0',
936 `tverify`='0',
937 `board`='0'
938 where `id`='$id'");
939
940 //block account
941 mysql_query("update `users` set `locked`='1' where `id`='$id'"); //, `deleted`=Now()
942 }
943
944
945 function check_email_exists($email){
946 // called from includes/account.php if($process != "" && $oldid == 1)
947 // called from includes/account.php if($oldid == 50 && $process != "")
948 $email = mysql_real_escape_string($email);
949 $query = "select 1 from `email` where `email`='$email' and `deleted`=0";
950 $res = mysql_query($query);
951 return mysql_num_rows($res) > 0;
952 }
953
954 function check_gpg_cert_running($uid,$cca=0){
955 //if $cca =0 if just expired, =1 if CCA retention +3 month should be obeyed
956 // called from includes/account.php if($oldid == 50 && $process != "")
957 $uid = intval($uid);
958 if (0==$cca) {
959 $query = "select 1 from `gpg` where `memid`='$uid' and `expire`>NOW()";
960 }else{
961 $query = "select 1 from `gpg` where `memid`='$uid' and `expire`>(NOW()-90*86400)";
962 }
963 $res = mysql_query($query);
964 return mysql_num_rows($res) > 0;
965 }
966
967 function check_client_cert_running($uid,$cca=0){
968 //if $cca =0 if just expired, =1 if CCA retention +3 month should be obeyed
969 // called from includes/account.php if($oldid == 50 && $process != "")
970 $uid = intval($uid);
971 if (0==$cca) {
972 $query1 = "select 1 from `emailcerts` where `memid`='$uid' and `expire`>NOW() and `revoked`<`created`";
973 $query2 = "select 1 from `emailcerts` where `memid`='$uid' and `revoked`>NOW()";
974 }else{
975 $query1 = "select 1 from `emailcerts` where `memid`='$uid' and `expire`>(NOW()-90*86400) and `revoked`<`created`";
976 $query2 = "select 1 from `emailcerts` where `memid`='$uid' and `revoked`>(NOW()-90*86400)";
977 }
978 $res = mysql_query($query1);
979 $r1 = mysql_num_rows($res)>0;
980 $res = mysql_query($query2);
981 $r2 = mysql_num_rows($res)>0;
982 return !!($r1 || $r2);
983 }
984
985 function check_server_cert_running($uid,$cca=0){
986 //if $cca =0 if just expired, =1 if CCA retention +3 month should be obeyed
987 // called from includes/account.php if($oldid == 50 && $process != "")
988 $uid = intval($uid);
989 if (0==$cca) {
990 $query1 = "
991 select 1 from `domaincerts` join `domains`
992 on `domaincerts`.`domid` = `domains`.`id`
993 where `domains`.`memid` = '$uid'
994 and `domaincerts`.`expire` > NOW()
995 and `domaincerts`.`revoked` < `domaincerts`.`created`";
996 $query2 = "
997 select 1 from `domaincerts` join `domains`
998 on `domaincerts`.`domid` = `domains`.`id`
999 where `domains`.`memid` = '$uid'
1000 and `revoked`>NOW()";
1001 }else{
1002 $query1 = "
1003 select 1 from `domaincerts` join `domains`
1004 on `domaincerts`.`domid` = `domains`.`id`
1005 where `domains`.`memid` = '$uid'
1006 and `expire`>(NOW()-90*86400)
1007 and `revoked`<`created`";
1008 $query2 = "
1009 select 1 from `domaincerts` join `domains`
1010 on `domaincerts`.`domid` = `domains`.`id`
1011 where `domains`.`memid` = '$uid'
1012 and `revoked`>(NOW()-90*86400)";
1013 }
1014 $res = mysql_query($query1);
1015 $r1 = mysql_num_rows($res)>0;
1016 $res = mysql_query($query2);
1017 $r2 = mysql_num_rows($res)>0;
1018 return !!($r1 || $r2);
1019 }
1020
1021 function check_is_orgadmin($uid){
1022 // called from includes/account.php if($oldid == 50 && $process != "")
1023 $uid = intval($uid);
1024 $query = "select 1 from `org` where `memid`='$uid' and `deleted`=0";
1025 $res = mysql_query($query);
1026 return mysql_num_rows($res) > 0;
1027 }
1028
1029
1030 // revokation of certificates
1031 function revoke_all_client_cert($mailid){
1032 //revokes all client certificates for an email address
1033 $mailid = intval($mailid);
1034 $query = "select `emailcerts`.`id`
1035 from `emaillink`,`emailcerts` where
1036 `emaillink`.`emailid`='$mailid' and `emaillink`.`emailcertsid`=`emailcerts`.`id` and `emailcerts`.`revoked`=0
1037 group by `emailcerts`.`id`";
1038 $dres = mysql_query($query);
1039 while($drow = mysql_fetch_assoc($dres)){
1040 mysql_query("update `emailcerts` set `revoked`='1970-01-01 10:00:01', `disablelogin`=1 where `id`='".$drow['id']."'");
1041 }
1042 }
1043
1044 function revoke_all_server_cert($domainid){
1045 //revokes all server certs for an domain
1046 $domainid = intval($domainid);
1047 $query =
1048 "select `domaincerts`.`id`
1049 from `domaincerts`
1050 where `domaincerts`.`domid` = '$domainid'
1051 union distinct
1052 select `domaincerts`.`id`
1053 from `domaincerts`, `domlink`
1054 where `domaincerts`.`id` = `domlink`.`certid`
1055 and `domlink`.`domid` = '$domainid'";
1056 $dres = mysql_query($query);
1057 while($drow = mysql_fetch_assoc($dres))
1058 {
1059 mysql_query(
1060 "update `domaincerts`
1061 set `revoked`='1970-01-01 10:00:01'
1062 where `id` = '".$drow['id']."'
1063 and `revoked` = 0");
1064 }
1065 }
1066
1067 function revoke_all_private_cert($uid){
1068 //revokes all certificates linked to a personal accounts
1069 //gpg revokation needs to be added to a later point
1070 $uid=intval($uid);
1071 $query = "select `id` from `email` where `memid`='".$uid."'";
1072 $res=mysql_query($query);
1073 while($row = mysql_fetch_assoc($res)){
1074 revoke_all_client_cert($row['id']);
1075 }
1076
1077
1078 $query = "select `id` from `domains` where `memid`='".$uid."'";
1079 $res=mysql_query($query);
1080 while($row = mysql_fetch_assoc($res)){
1081 revoke_all_server_cert($row['id']);
1082 }
1083 }
1084
1085 /**
1086 * check_date_format()
1087 * checks if the date is entered in the right date format YYYY-MM-DD and
1088 * if the date is after the 1st January of the given year
1089 *
1090 * @param mixed $date
1091 * @param integer $year
1092 * @return
1093 */
1094 function check_date_format($date, $year=2000){
1095 if (!strpos($date,'-')) {
1096 return FALSE;
1097 }
1098 $arr=explode('-',$date);
1099
1100 if ((count($arr)!=3)) {
1101 return FALSE;
1102 }
1103 if (intval($arr[0])<=$year) {
1104 return FALSE;
1105 }
1106 if (intval($arr[1])>12 or intval($arr[1])<=0) {
1107 return FALSE;
1108 }
1109 if (intval($arr[2])>31 or intval($arr[2])<=0) {
1110 return FALSE;
1111 }
1112
1113 return checkdate( intval($arr[1]), intval($arr[2]), intval($arr[0]));
1114
1115 }
1116
1117 /**
1118 * check_date_difference()
1119 * returns false if the date is larger then today + time diffrence
1120 *
1121 * @param mixed $date
1122 * @param integer $diff
1123 * @return
1124 */
1125 function check_date_difference($date, $diff=1){
1126 return (strtotime($date)<=time()+$diff*86400);
1127 }