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