Source code taken from cacert-20111007.tar.bz2
[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_assurees ($userid)
45 {
46 $res = query_init ("SELECT count(*) AS `list` FROM `notary`
47 WHERE `method` = 'Face to Face Meeting' AND `to`='".intval($userid)."' ");
48 $row = query_getnextrow($res);
49
50 return intval($row['list']);
51 }
52
53 function get_top_assurer_position ($no_of_assurances)
54 {
55 $res = query_init ("SELECT count(*) AS `list` FROM `notary`
56 WHERE `method` = 'Face to Face Meeting'
57 GROUP BY `from` HAVING count(*) > '".intval($no_of_assurances)."'");
58 return intval(query_get_number_of_rows($res)+1);
59 }
60
61 function get_top_assuree_position ($no_of_assurees)
62 {
63 $res = query_init ("SELECT count(*) AS `list` FROM `notary`
64 WHERE `method` = 'Face to Face Meeting'
65 GROUP BY .`to` HAVING count(*) > '".intval($no_of_assurees)."'");
66 return intval(query_get_number_of_rows($res)+1);
67 }
68
69 function get_given_assurances ($userid)
70 {
71 $res = query_init ("select * from `notary` where `from`='".intval($userid)."' and `from` != `to` order by `id` asc");
72 return $res;
73 }
74
75 function get_received_assurances ($userid)
76 {
77 $res = query_init ("select * from `notary` where `to`='".intval($userid)."' and `from` != `to` order by `id` asc ");
78 return $res;
79 }
80
81 function get_given_assurances_summary ($userid)
82 {
83 $res = query_init ("select count(*) as number,points,awarded,method from notary where `from`='".intval($userid)."' group by points,awarded,method");
84 return $res;
85 }
86
87 function get_received_assurances_summary ($userid)
88 {
89 $res = query_init ("select count(*) as number,points,awarded,method from notary where `to`='".intval($userid)."' group by points,awarded,method");
90 return $res;
91 }
92
93 function get_user ($userid)
94 {
95 $res = query_init ("select * from `users` where `id`='".intval($userid)."'");
96 return mysql_fetch_assoc($res);
97 }
98
99 function get_cats_state ($userid)
100 {
101
102 $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
103 WHERE `cats_passed`.`user_id` = '".intval($userid)."'");
104 return mysql_num_rows($res);
105 }
106
107 function calc_experience ($row,&$points,&$experience,&$sum_experience,&$revoked)
108 {
109 $points += $row['awarded'];
110 $experience = "&nbsp;";
111 $revoked = false; # to be coded later (after DB-upgrade)
112 if ($row['method'] == "Face to Face Meeting")
113 {
114 $sum_experience = $sum_experience +2;
115 $experience = "2";
116 }
117 return $row['awarded'];
118 }
119
120 function calc_assurances ($row,&$points,&$experience,&$sumexperience,&$awarded,&$revoked)
121 {
122 $awarded = calc_points($row);
123 $revoked = false;
124
125 if ($awarded > 100)
126 {
127 $experience = $awarded - 100; // needs to be fixed in the future (limit 50 pts and/or no experience if pts > 100)
128 $awarded = 100;
129 }
130 else
131 $experience = 0;
132
133 switch ($row['method'])
134 {
135 case 'Thawte Points Transfer':
136 case 'CT Magazine - Germany':
137 case 'Temporary Increase': // Current usage of 'Temporary Increase' may break audit aspects, needs to be reimplemented
138 $awarded=sprintf("<strong style='color: red'>%s</strong>",_("Revoked"));
139 $experience=0;
140 $revoked=true;
141 break;
142 default:
143 $points += $awarded;
144 }
145 $sumexperience = $sumexperience + $experience;
146 }
147
148
149 function show_user_link ($name,$userid)
150 {
151 $name = trim($name);
152 if($name == "")
153 {
154 if ($userid == 0)
155 $name = _("System");
156 else
157 $name = _("Deleted account");
158 }
159 else
160 $name = "<a href='wot.php?id=9&amp;userid=".intval($userid)."'>".sanitizeHTML($name)."</a>";
161 return $name;
162 }
163
164 function show_email_link ($email,$userid)
165 {
166 $email = trim($email);
167 if($email != "")
168 $email = "<a href='account.php?id=43&amp;userid=".intval($userid)."'>".sanitizeHTML($email)."</a>";
169 return $email;
170 }
171
172 function get_assurer_ranking($userid,&$num_of_assurances,&$rank_of_assurer)
173 {
174 $num_of_assurances = get_number_of_assurances (intval($userid));
175 $rank_of_assurer = get_top_assurer_position($num_of_assurances);
176 }
177
178 function get_assuree_ranking($userid,&$num_of_assurees,&$rank_of_assuree)
179 {
180 $num_of_assurees = get_number_of_assurees (intval($userid));
181 $rank_of_assuree = get_top_assuree_position($num_of_assurees);
182 }
183
184
185 // ************* html table definitions ******************
186
187 function output_ranking($userid)
188 {
189 get_assurer_ranking($userid,$num_of_assurances,$rank_of_assurer);
190 get_assuree_ranking($userid,$num_of_assurees,$rank_of_assuree);
191
192 ?>
193 <table align="center" valign="middle" border="0" cellspacing="0" cellpadding="0" class="wrapper">
194 <tr>
195 <td class="title"><?=_("Assurer Ranking")?></td>
196 </tr>
197 <tr>
198 <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>
199 </tr>
200 <tr>
201 <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>
202 </tr>
203 </table>
204 <br/>
205 <?
206 }
207
208 function output_assurances_header($title,$support)
209 {
210 ?>
211 <table align="center" valign="middle" border="0" cellspacing="0" cellpadding="0" class="wrapper">
212 <tr>
213 <?
214 if ($support == "1")
215 {
216 ?>
217 <td colspan="10" class="title"><?=$title?></td>
218 <?
219 } else {
220 ?>
221 <td colspan="7" class="title"><?=$title?></td>
222 <? }
223 ?>
224 </tr>
225 <tr>
226 <td class="DataTD"><strong><?=_("ID")?></strong></td>
227 <td class="DataTD"><strong><?=_("Date")?></strong></td>
228 <?
229 if ($support == "1")
230 {
231 ?>
232 <td class="DataTD"><strong><?=_("When")?></strong></td>
233 <td class="DataTD"><strong><?=_("Email")?></strong></td>
234 <? } ?>
235 <td class="DataTD"><strong><?=_("Who")?></strong></td>
236 <td class="DataTD"><strong><?=_("Points")?></strong></td>
237 <td class="DataTD"><strong><?=_("Location")?></strong></td>
238 <td class="DataTD"><strong><?=_("Method")?></strong></td>
239 <td class="DataTD"><strong><?=_("Experience Points")?></strong></td>
240 <?
241 if ($support == "1")
242 {
243 ?>
244 <td class="DataTD"><strong><?=_("Revoke")?></strong></td>
245 <?
246 }
247 ?>
248 </tr>
249 <?
250 }
251
252 function output_assurances_footer($points_txt,$points,$experience_txt,$sumexperience,$support)
253 {
254 ?>
255 <tr>
256 <td class="DataTD" colspan="5"><strong><?=$points_txt?>:</strong></td>
257 <td class="DataTD"><?=$points?></td>
258 <td class="DataTD">&nbsp;</td>
259 <td class="DataTD"><strong><?=$experience_txt?>:</strong></td>
260 <td class="DataTD"><?=$sumexperience?></td>
261 <?
262 if ($support == "1")
263 {
264 ?>
265 <td class="DataTD">&nbsp;</td>
266 <?
267 }
268 ?>
269
270 </tr>
271 </table>
272 <br/>
273 <?
274 }
275
276 function output_assurances_row($assuranceid,$date,$when,$email,$name,$awarded,$points,$location,$method,$experience,$userid,$support,$revoked)
277 {
278
279 $tdstyle="";
280 $emopen="";
281 $emclose="";
282
283 if ($awarded == $points)
284 {
285 if ($awarded == "0")
286 {
287 $tdstyle="style='background-color: #ffff80'";
288 $emopen="<em>";
289 $emclose="</em>";
290 }
291 }
292 ?>
293 <tr>
294 <td class="DataTD" <?=$tdstyle?>><?=$emopen?><?=$assuranceid?><?=$emclose?></td>
295 <td class="DataTD" <?=$tdstyle?>><?=$emopen?><?=$date?><?=$emclose?></td>
296 <?
297 if ($support == "1")
298 {
299 ?>
300 <td class="DataTD" <?=$tdstyle?>><?=$emopen?><?=$when?><?=$emclose?></td>
301 <td class="DataTD" <?=$tdstyle?>><?=$emopen?><?=$email?><?=$emclose?></td>
302 <? }
303 ?>
304 <td class="DataTD" <?=$tdstyle?>><?=$emopen?><?=$name?><?=$emclose?></td>
305 <td class="DataTD" <?=$tdstyle?>><?=$emopen?><?=$awarded?><?=$emclose?></td>
306 <td class="DataTD" <?=$tdstyle?>><?=$emopen?><?=$location?><?=$emclose?></td>
307 <td class="DataTD" <?=$tdstyle?>><?=$emopen?><?=$method?><?=$emclose?></td>
308 <td class="DataTD" <?=$tdstyle?>><?=$emopen?><?=$experience?><?=$emclose?></td>
309 <?
310 if ($support == "1")
311 {
312 if ($revoked == true)
313 {
314 ?>
315 <td class="DataTD" <?=$tdstyle?>>&nbsp;</td>
316 <? } else {
317 ?>
318 <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('<?=_("Are you sure you want to revoke this assurance?")?>');"><?=_("Revoke")?></a><?=$emclose?></td>
319 <?
320 }
321 }
322 ?>
323 </tr>
324 <?
325 }
326
327 function output_summary_header()
328 {
329 ?>
330 <table align="center" valign="middle" border="0" cellspacing="0" cellpadding="0" class="wrapper">
331 <tr>
332 <td colspan="4" class="title"><?=_("Summary of your Points")?></td>
333 </tr>
334 <tr>
335 <td class="DataTD"><strong><?=_("Description")?></strong></td>
336 <td class="DataTD"><strong><?=_("Points")?></strong></td>
337 <td class="DataTD"><strong><?=_("Countable Points")?></strong></td>
338 <td class="DataTD"><strong><?=_("Remark")?></strong></td>
339 </tr>
340 <?
341 }
342
343 function output_summary_footer()
344 {
345 ?>
346 </table>
347 <br/>
348 <?
349 }
350
351 function output_summary_row($title,$points,$points_countable,$remark)
352 {
353 ?>
354 <tr>
355 <td class="DataTD"><strong><?=$title?></strong></td>
356 <td class="DataTD"><?=$points?></td>
357 <td class="DataTD"><?=$points_countable?></td>
358 <td class="DataTD"><?=$remark?></td>
359 </tr>
360 <?
361 }
362
363
364 // ************* output given assurances ******************
365
366 function output_given_assurances_content($userid,&$points,&$sum_experience,$support)
367 {
368 $points = 0;
369 $sumexperience = 0;
370 $res = get_given_assurances(intval($userid));
371 while($row = mysql_fetch_assoc($res))
372 {
373 $fromuser = get_user (intval($row['to']));
374 calc_experience ($row,$points,$experience,$sum_experience,$revoked);
375 $name = show_user_link ($fromuser['fname']." ".$fromuser['lname'],intval($row['to']));
376 $email = show_email_link ($fromuser['email'],intval($row['to']));
377 output_assurances_row (intval($row['id']),$row['date'],$row['when'],$email,$name,intval($row['awarded']),intval($row['points']),$row['location'],$row['method']==""?"":_(sprintf("%s", $row['method'])),$experience,$userid,$support,$revoked);
378 }
379 }
380
381 // ************* output received assurances ******************
382
383 function output_received_assurances_content($userid,&$points,&$sum_experience,$support)
384 {
385 $points = 0;
386 $sumexperience = 0;
387 $res = get_received_assurances(intval($userid));
388 while($row = mysql_fetch_assoc($res))
389 {
390 $fromuser = get_user (intval($row['from']));
391 calc_assurances ($row,$points,$experience,$sum_experience,$awarded,$revoked);
392 $name = show_user_link ($fromuser['fname']." ".$fromuser['lname'],intval($row['from']));
393 $email = show_email_link ($fromuser['email'],intval($row['from']));
394 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);
395 }
396 }
397
398 // ************* output summary table ******************
399
400 function check_date_limit ($userid,$age)
401 {
402 $dob = date("Y-m-d", mktime(0,0,0,date("m"),date("d"),date("Y")-$age));
403 $res = query_init ("select id from `users` where `id`='".$userid."' and `dob` < '$dob'");
404 return intval(query_get_number_of_rows($res));
405 }
406
407 function calc_points($row)
408 {
409 $awarded = intval($row['awarded']);
410 if ($awarded == "")
411 $awarded = 0;
412 if (intval($row['points']) < $awarded)
413 $points = $awarded; // if 'sum of added points' > 100, awarded shows correct value
414 else
415 $points = intval($row['points']); // on very old assurances, awarded is '0' instead of correct value
416 switch ($row['method'])
417 {
418 case 'Thawte Points Transfer': // revoke all Thawte-points (as per arbitration)
419 case 'CT Magazine - Germany': // revoke c't (only one test-entry)
420 case 'Temporary Increase': // revoke 'temporary increase' (Current usage breaks audit aspects, needs to be reimplemented)
421 $points = 0;
422 break;
423 case 'Administrative Increase': // ignore AI with 2 points or less (historical for experiance points, now other calculation)
424 if ($points <= 2) // maybe limit to 35/50 pts in the future?
425 $points = 0;
426 break;
427 case 'Unknown': // to be revoked in the future? limit to max 50 pts?
428 case 'Trusted Third Parties': // to be revoked in the future? limit to max 35 pts?
429 case '': // to be revoked in the future? limit to max 50 pts?
430 case 'Face to Face Meeting': // normal assurances, limit to 35/50 pts in the future?
431 break;
432 default: // should never happen ... ;-)
433 $points = 0;
434 }
435 if ($points < 0) // ignore negative points (bug needs to be fixed)
436 $points = 0;
437 return $points;
438 }
439
440 function max_points($userid)
441 {
442 return output_summary_content ($userid,0);
443 }
444
445 function output_summary_content($userid,$display_output)
446 {
447 $sum_points = 0;
448 $sum_experience = 0;
449 $sum_experience_other = 0;
450 $max_points = 100;
451 $max_experience = 50;
452
453 $experience_limit_reached_txt = _("Limit reached");
454
455 if (check_date_limit($userid,18) != 1)
456 {
457 $max_experience = 10;
458 $experience_limit_reached_txt = _("Limit given by PoJAM reached");
459 }
460 if (check_date_limit($userid,14) != 1)
461 {
462 $max_experience = 0;
463 $experience_limit_reached_txt = _("Limit given by PoJAM reached");
464 }
465
466 $res = get_received_assurances_summary($userid);
467 while($row = mysql_fetch_assoc($res))
468 {
469 $points = calc_points ($row);
470
471 if ($points > $max_points) // limit to 100 points, above is experience (needs to be fixed)
472 {
473 $sum_experience_other = $sum_experience_other+($points-$max_points)*intval($row['number']);
474 $points = $max_points;
475 }
476 $sum_points += $points*intval($row['number']);
477 }
478
479 $res = get_given_assurances_summary($userid);
480 while($row = mysql_fetch_assoc($res))
481 {
482 switch ($row['method'])
483 {
484 case 'Face to Face Meeting': // count Face to Face only
485 $sum_experience += 2*intval($row['number']);
486 break;
487 }
488
489 }
490
491 if ($sum_points > $max_points)
492 {
493 $sum_points_countable = $max_points;
494 $remark_points = _("Limit reached");
495 }
496 else
497 {
498 $sum_points_countable = $sum_points;
499 $remark_points = "&nbsp;";
500 }
501 if ($sum_experience > $max_experience)
502 {
503 $sum_experience_countable = $max_experience;
504 $remark_experience = $experience_limit_reached_txt;
505 }
506 else
507 {
508 $sum_experience_countable = $sum_experience;
509 $remark_experience = "&nbsp;";
510 }
511
512 if ($sum_experience_countable + $sum_experience_other > $max_experience)
513 {
514 $sum_experience_other_countable = $max_experience-$sum_experience_countable;
515 $remark_experience_other = $experience_limit_reached_txt;
516 }
517 else
518 {
519 $sum_experience_other_countable = $sum_experience_other;
520 $remark_experience_other = "&nbsp;";
521 }
522
523 if ($sum_points_countable < $max_points)
524 {
525 if ($sum_experience_countable != 0)
526 $remark_experience = _("Points on hold due to less assurance points");
527 $sum_experience_countable = 0;
528 if ($sum_experience_other_countable != 0)
529 $remark_experience_other = _("Points on hold due to less assurance points");
530 $sum_experience_other_countable = 0;
531 }
532
533 $issue_points = 0;
534 $cats_test_passed = get_cats_state ($userid);
535 if ($cats_test_passed == 0)
536 {
537 $issue_points_txt = "<strong style='color: red'>"._("You have to pass the CAcert Assurer Challenge (CATS-Test) to be an Assurer")."</strong>";
538 if ($sum_points_countable < $max_points)
539 {
540 $issue_points_txt = "<strong style='color: red'>";
541 $issue_points_txt .= sprintf(_("You need %s assurance points and the passed CATS-Test to be an Assurer"), intval($max_points));
542 $issue_points_txt .= "</strong>";
543 }
544 }
545 else
546 {
547 $experience_total = $sum_experience_countable+$sum_experience_other_countable;
548 $issue_points_txt = "";
549 if ($sum_points_countable == $max_points)
550 $issue_points = 10;
551 if ($experience_total >= 10)
552 $issue_points = 15;
553 if ($experience_total >= 20)
554 $issue_points = 20;
555 if ($experience_total >= 30)
556 $issue_points = 25;
557 if ($experience_total >= 40)
558 $issue_points = 30;
559 if ($experience_total >= 50)
560 $issue_points = 35;
561 if ($issue_points != 0)
562 $issue_points_txt = sprintf(_("You may issue up to %s points"),$issue_points);
563 }
564 if ($display_output)
565 {
566 output_summary_row (_("Assurance Points you received"),$sum_points,$sum_points_countable,$remark_points);
567 output_summary_row (_("Total Experience Points by Assurance"),$sum_experience,$sum_experience_countable,$remark_experience);
568 output_summary_row (_("Total Experience Points (other ways)"),$sum_experience_other,$sum_experience_other_countable,$remark_experience_other);
569 output_summary_row (_("Total Points"),"&nbsp;",$sum_points_countable + $sum_experience_countable + $sum_experience_other_countable,$issue_points_txt);
570 }
571 return $issue_points;
572 }
573
574 function output_given_assurances($userid,$support)
575 {
576 output_assurances_header(_("Assurance Points You Issued"),$support);
577 output_given_assurances_content($userid,$points,$sum_experience,$support);
578 output_assurances_footer(_("Total Points Issued"),$points,_("Total Experience Points"),$sum_experience,$support);
579 }
580
581 function output_received_assurances($userid,$support)
582 {
583 output_assurances_header(_("Your Assurance Points"),$support);
584 output_received_assurances_content($userid,$points,$sum_experience,$support);
585 output_assurances_footer(_("Total Assurance Points"),$points,_("Total Experience Points"),$sum_experience,$support);
586 }
587
588 function output_summary($userid)
589 {
590 output_summary_header();
591 output_summary_content($userid,1);
592 output_summary_footer();
593 }
594
595 function output_end_of_page()
596 {
597 ?>
598 <p>[ <a href='javascript:history.go(-1)'><?=_("Go Back")?></a> ]</p>
599 <?
600 }
601 ?>