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