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