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