summaryrefslogtreecommitdiff
path: root/includes/notary.inc.php
blob: 95cd889d8155aad61b56e01dac798fe12c8f92e2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
<? /*
    LibreSSL - CAcert web application
    Copyright (C) 2004-2011  CAcert Inc.

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; version 2 of the License.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
*/ 

	function query_init ($query)
	{
		return mysql_query($query);
	}

	function query_getnextrow ($res)
	{
		$row1 = mysql_fetch_assoc($res);
		return $row1;
	}

	function query_get_number_of_rows ($resultset)
	{
		return intval(mysql_num_rows($resultset));
	}

	function get_number_of_assurances ($userid)
	{
		$res = query_init ("SELECT count(*) AS `list` FROM `notary`
		     	WHERE `method` = 'Face to Face Meeting' AND `from`='".intval($userid)."' ");
		$row = query_getnextrow($res);

		return intval($row['list']);
	}

	function get_number_of_assurees ($userid)
	{
		$res = query_init ("SELECT count(*) AS `list` FROM `notary`
			WHERE `method` = 'Face to Face Meeting' AND `to`='".intval($userid)."' ");
		$row = query_getnextrow($res);

		return intval($row['list']);
	}

	function get_top_assurer_position ($no_of_assurances)
	{
		$res = query_init ("SELECT count(*) AS `list` FROM `notary` 
			WHERE `method` = 'Face to Face Meeting' 
			GROUP BY `from` HAVING count(*) > '".intval($no_of_assurances)."'");
		return intval(query_get_number_of_rows($res)+1);
	}

	function get_top_assuree_position ($no_of_assurees)
	{
		$res = query_init ("SELECT count(*) AS `list` FROM `notary`
			WHERE `method` = 'Face to Face Meeting'
			GROUP BY .`to` HAVING count(*) > '".intval($no_of_assurees)."'");
		return intval(query_get_number_of_rows($res)+1);
	}

	function get_given_assurances ($userid)
	{
		$res = query_init ("select * from `notary` where `from`='".intval($userid)."' and `from` != `to` order by `id` asc");
		return $res;
	}

	function get_received_assurances ($userid)
	{
		$res = query_init ("select * from `notary` where `to`='".intval($userid)."' and `from` != `to` order by `id` asc ");
		return $res;
	}

	function get_given_assurances_summary ($userid)
	{
		$res = query_init ("select count(*) as number,points,awarded,method from notary where `from`='".intval($userid)."' group by points,awarded,method");
		return $res;
	}
	
	function get_received_assurances_summary ($userid)
	{
		$res = query_init ("select count(*) as number,points,awarded,method from notary where `to`='".intval($userid)."' group by points,awarded,method");
		return $res;
	}

	function get_user ($userid)
	{
		$res = query_init ("select * from `users` where `id`='".intval($userid)."'");
		return mysql_fetch_assoc($res);
	}

	function get_cats_state ($userid)
	{

		$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
			WHERE `cats_passed`.`user_id` = '".intval($userid)."'");
		return mysql_num_rows($res);
	}

	function calc_experience ($row,&$points,&$experience,&$sum_experience,&$revoked)
	{
		$points += $row['awarded'];
		$experience = "&nbsp;";
		$revoked = false;				# to be coded later (after DB-upgrade)
		if ($row['method'] == "Face to Face Meeting")
		{
			$sum_experience = $sum_experience +2;
			$experience = "2";
		}
		return $row['awarded'];
	}

	function calc_assurances ($row,&$points,&$experience,&$sumexperience,&$awarded,&$revoked)
	{
		$awarded = calc_points($row);
		$revoked = false;

		if ($awarded > 100)
		{
			$experience = $awarded - 100;		// needs to be fixed in the future (limit 50 pts and/or no experience if pts > 100)
			$awarded = 100;
		}
		else
			$experience = 0;	

		switch ($row['method'])
		{
			case 'Thawte Points Transfer':
			case 'CT Magazine - Germany':
			case 'Temporary Increase':	      // Current usage of 'Temporary Increase' may break audit aspects, needs to be reimplemented
				$awarded=sprintf("<strong style='color: red'>%s</strong>",_("Revoked"));
				$experience=0;
				$revoked=true;
				break;
			default:
				$points += $awarded;
		}
		$sumexperience = $sumexperience + $experience;
	}


	function show_user_link ($name,$userid)
	{
		$name = trim($name);
		if($name == "")
		{
			if ($userid == 0)
				$name = _("System");
			else
				$name = _("Deleted account");
		}
		else
			$name = "<a href='wot.php?id=9&amp;userid=".intval($userid)."'>".sanitizeHTML($name)."</a>";
		return $name;
	}

	function show_email_link ($email,$userid)
	{
		$email = trim($email);
		if($email != "")
			$email = "<a href='account.php?id=43&amp;userid=".intval($userid)."'>".sanitizeHTML($email)."</a>";
		return $email;
	}

	function get_assurer_ranking($userid,&$num_of_assurances,&$rank_of_assurer)
	{
		$num_of_assurances = get_number_of_assurances (intval($userid));
		$rank_of_assurer = get_top_assurer_position($num_of_assurances);
	}

	function get_assuree_ranking($userid,&$num_of_assurees,&$rank_of_assuree)
	{
		$num_of_assurees = get_number_of_assurees (intval($userid));
		$rank_of_assuree = get_top_assuree_position($num_of_assurees);
	}


// ************* html table definitions ******************

	function output_ranking($userid)
	{
		get_assurer_ranking($userid,$num_of_assurances,$rank_of_assurer);
		get_assuree_ranking($userid,$num_of_assurees,$rank_of_assuree);

?>
<table align="center" valign="middle" border="0" cellspacing="0" cellpadding="0" class="wrapper">
    <tr>
    	<td class="title"><?=_("Assurer Ranking")?></td>
    </tr>
    <tr>
	<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>
    </tr>
    <tr>
	<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>
    </tr>
</table>
<br/>
<?
	}

	function output_assurances_header($title,$support)
	{
?>
<table align="center" valign="middle" border="0" cellspacing="0" cellpadding="0" class="wrapper">
    <tr>
<?
	if ($support == "1")
	{
?>
    	<td colspan="10" class="title"><?=$title?></td>
<?
	} else {
?>
    	<td colspan="7" class="title"><?=$title?></td>
<?	}
?>
    </tr>
    <tr>
    	<td class="DataTD"><strong><?=_("ID")?></strong></td>
    	<td class="DataTD"><strong><?=_("Date")?></strong></td>
<?
	if ($support == "1")
	{
?>
    	<td class="DataTD"><strong><?=_("When")?></strong></td>
    	<td class="DataTD"><strong><?=_("Email")?></strong></td>
<?	} ?>
    	<td class="DataTD"><strong><?=_("Who")?></strong></td>
    	<td class="DataTD"><strong><?=_("Points")?></strong></td>
    	<td class="DataTD"><strong><?=_("Location")?></strong></td>
    	<td class="DataTD"><strong><?=_("Method")?></strong></td>
    	<td class="DataTD"><strong><?=_("Experience Points")?></strong></td>
<?
	if ($support == "1")
	{
?>
	<td class="DataTD"><strong><?=_("Revoke")?></strong></td>
<?
	}
?>
    </tr>
<?
	}

	function output_assurances_footer($points_txt,$points,$experience_txt,$sumexperience,$support)
	{
?>
    <tr>
    	<td class="DataTD" colspan="5"><strong><?=$points_txt?>:</strong></td>
    	<td class="DataTD"><?=$points?></td>
    	<td class="DataTD">&nbsp;</td>
    	<td class="DataTD"><strong><?=$experience_txt?>:</strong></td>
    	<td class="DataTD"><?=$sumexperience?></td>
<?
	if ($support == "1")
	{
?>
    	<td class="DataTD">&nbsp;</td>
<?
	}
?>

    </tr>
</table>
<br/>
<?
	}

	function output_assurances_row($assuranceid,$date,$when,$email,$name,$awarded,$points,$location,$method,$experience,$userid,$support,$revoked)
	{

	$tdstyle="";
	$emopen="";
	$emclose="";

	if ($awarded == $points)
	{
		if ($awarded == "0")
		{
			$tdstyle="style='background-color: #ffff80'";
			$emopen="<em>";
			$emclose="</em>";
		}
	}
?>
    <tr>
	<td class="DataTD" <?=$tdstyle?>><?=$emopen?><?=$assuranceid?><?=$emclose?></td>
	<td class="DataTD" <?=$tdstyle?>><?=$emopen?><?=$date?><?=$emclose?></td>
<?
	if ($support == "1")
	{
?>
		<td class="DataTD" <?=$tdstyle?>><?=$emopen?><?=$when?><?=$emclose?></td>
		<td class="DataTD" <?=$tdstyle?>><?=$emopen?><?=$email?><?=$emclose?></td>
<?	} 
?>
	<td class="DataTD" <?=$tdstyle?>><?=$emopen?><?=$name?><?=$emclose?></td>
	<td class="DataTD" <?=$tdstyle?>><?=$emopen?><?=$awarded?><?=$emclose?></td>
	<td class="DataTD" <?=$tdstyle?>><?=$emopen?><?=$location?><?=$emclose?></td>
	<td class="DataTD" <?=$tdstyle?>><?=$emopen?><?=$method?><?=$emclose?></td>
	<td class="DataTD" <?=$tdstyle?>><?=$emopen?><?=$experience?><?=$emclose?></td>
<?
	if ($support == "1")
	{
		if ($revoked == true)
		{
?>
			<td class="DataTD" <?=$tdstyle?>>&nbsp;</td>
<?		} else {
?>
			<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>
<?
		}
	}
?>
    </tr>
<?
	}

	function output_summary_header()
	{
?>
<table align="center" valign="middle" border="0" cellspacing="0" cellpadding="0" class="wrapper">
    <tr>
	<td colspan="4" class="title"><?=_("Summary of your Points")?></td>
    </tr>
    <tr>
	<td class="DataTD"><strong><?=_("Description")?></strong></td>
	<td class="DataTD"><strong><?=_("Points")?></strong></td>
	<td class="DataTD"><strong><?=_("Countable Points")?></strong></td>
	<td class="DataTD"><strong><?=_("Remark")?></strong></td>
    </tr>
<?
	}

	function output_summary_footer()
	{
?>
</table>
<br/>
<?
	}

	function output_summary_row($title,$points,$points_countable,$remark)
	{
?>
    <tr>
	<td class="DataTD"><strong><?=$title?></strong></td>
	<td class="DataTD"><?=$points?></td>
	<td class="DataTD"><?=$points_countable?></td>
	<td class="DataTD"><?=$remark?></td>
    </tr>
<?
	}


// ************* output given assurances ******************

	function output_given_assurances_content($userid,&$points,&$sum_experience,$support)
	{
		$points = 0;
		$sumexperience = 0;
		$res = get_given_assurances(intval($userid));
		while($row = mysql_fetch_assoc($res))
		{
			$fromuser = get_user (intval($row['to'])); 
			calc_experience ($row,$points,$experience,$sum_experience,$revoked);
			$name = show_user_link ($fromuser['fname']." ".$fromuser['lname'],intval($row['to']));
			$email = show_email_link ($fromuser['email'],intval($row['to']));
			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);
		}
	}

// ************* output received assurances ******************

	function output_received_assurances_content($userid,&$points,&$sum_experience,$support)
	{
		$points = 0;
		$sumexperience = 0;
		$res = get_received_assurances(intval($userid));
		while($row = mysql_fetch_assoc($res))
		{
			$fromuser = get_user (intval($row['from']));
			calc_assurances ($row,$points,$experience,$sum_experience,$awarded,$revoked);
			$name = show_user_link ($fromuser['fname']." ".$fromuser['lname'],intval($row['from']));
			$email = show_email_link ($fromuser['email'],intval($row['from']));
			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);
		}
	}

// ************* output summary table ******************

	function check_date_limit ($userid,$age)
	{
		$dob = date("Y-m-d", mktime(0,0,0,date("m"),date("d"),date("Y")-$age));
		$res = query_init ("select id from `users` where `id`='".$userid."' and `dob` < '$dob'");
		return intval(query_get_number_of_rows($res));
	}

	function calc_points($row)
	{
		$awarded = intval($row['awarded']);
		if ($awarded == "")
			$awarded = 0;
		if (intval($row['points']) < $awarded)
			$points = $awarded;      // if 'sum of added points' > 100, awarded shows correct value
		else
			$points = intval($row['points']);       // on very old assurances, awarded is '0' instead of correct value
		switch ($row['method'])
		{
			case 'Thawte Points Transfer':	  // revoke all Thawte-points     (as per arbitration)
			case 'CT Magazine - Germany':	   // revoke c't		   (only one test-entry)
			case 'Temporary Increase':	      // revoke 'temporary increase'  (Current usage breaks audit aspects, needs to be reimplemented)
				$points = 0;
				break;
			case 'Administrative Increase':	 // ignore AI with 2 points or less (historical for experiance points, now other calculation)
				if ($points <= 2)	       // maybe limit to 35/50 pts in the future?
					$points = 0;
				break;
			case 'Unknown':			 // to be revoked in the future? limit to max 50 pts?
			case 'Trusted Third Parties':	     // to be revoked in the future? limit to max 35 pts?
			case '':				// to be revoked in the future? limit to max 50 pts?
			case 'Face to Face Meeting':	    // normal assurances, limit to 35/50 pts in the future?
				break;
			default:				// should never happen ... ;-)
				$points = 0;
		}
		if ($points < 0)				// ignore negative points (bug needs to be fixed)
			$points = 0;
		return $points;
	}

	function max_points($userid)
	{
		return output_summary_content ($userid,0);
	}

	function output_summary_content($userid,$display_output)
	{
		$sum_points = 0;
		$sum_experience = 0;
		$sum_experience_other = 0;
		$max_points = 100;
		$max_experience = 50;

		$experience_limit_reached_txt = _("Limit reached");

		if (check_date_limit($userid,18) != 1)
		{
			$max_experience = 10;
			$experience_limit_reached_txt = _("Limit given by PoJAM reached");
		}
		if (check_date_limit($userid,14) != 1)
		{
			$max_experience = 0;
			$experience_limit_reached_txt = _("Limit given by PoJAM reached");
		}

		$res = get_received_assurances_summary($userid);
		while($row = mysql_fetch_assoc($res))
		{
			$points = calc_points ($row);

			if ($points > $max_points)			// limit to 100 points, above is experience (needs to be fixed)
			{
				$sum_experience_other = $sum_experience_other+($points-$max_points)*intval($row['number']);
				$points = $max_points;
			}
			$sum_points += $points*intval($row['number']);
		}

		$res = get_given_assurances_summary($userid);
		while($row = mysql_fetch_assoc($res))
		{
			switch ($row['method'])
			{
				case 'Face to Face Meeting':	 		// count Face to Face only
					$sum_experience += 2*intval($row['number']);
					break;
			}

		}

		if ($sum_points > $max_points)
			{
			$sum_points_countable = $max_points;
			$remark_points = _("Limit reached");
			}
		else
			{
			$sum_points_countable = $sum_points;
			$remark_points = "&nbsp;";
			}
		if ($sum_experience > $max_experience)
			{
			$sum_experience_countable = $max_experience;
			$remark_experience = $experience_limit_reached_txt;
			}
		else
			{
			$sum_experience_countable = $sum_experience;
			$remark_experience = "&nbsp;";
			}

		if ($sum_experience_countable + $sum_experience_other > $max_experience)
			{
			$sum_experience_other_countable = $max_experience-$sum_experience_countable;
			$remark_experience_other = $experience_limit_reached_txt;
			}
		else
			{
			$sum_experience_other_countable = $sum_experience_other;
			$remark_experience_other = "&nbsp;";
			}

		if ($sum_points_countable < $max_points)
			{
			if ($sum_experience_countable != 0)
				$remark_experience = _("Points on hold due to less assurance points");
			$sum_experience_countable = 0;
			if ($sum_experience_other_countable != 0)
				$remark_experience_other = _("Points on hold due to less assurance points");
			$sum_experience_other_countable = 0;
			}

		$issue_points = 0;
		$cats_test_passed = get_cats_state ($userid);
		if ($cats_test_passed == 0)
		{
			$issue_points_txt = "<strong style='color: red'>"._("You have to pass the CAcert Assurer Challenge (CATS-Test) to be an Assurer")."</strong>";
			if ($sum_points_countable < $max_points)
			{
				$issue_points_txt = "<strong style='color: red'>";
				$issue_points_txt .= sprintf(_("You need %s assurance points and the passed CATS-Test to be an Assurer"), intval($max_points));
				$issue_points_txt .= "</strong>";
			}
		}
		else
		{
			$experience_total = $sum_experience_countable+$sum_experience_other_countable;
			$issue_points_txt = "";
			if ($sum_points_countable == $max_points)
				$issue_points = 10;
			if ($experience_total >= 10)
				$issue_points = 15;
			if ($experience_total >= 20)
				$issue_points = 20;
			if ($experience_total >= 30)
				$issue_points = 25;
			if ($experience_total >= 40)
				$issue_points = 30;
			if ($experience_total >= 50)
				$issue_points = 35;
			if ($issue_points != 0)
				$issue_points_txt = sprintf(_("You may issue up to %s points"),$issue_points);
		}
		if ($display_output)
		{
			output_summary_row (_("Assurance Points you received"),$sum_points,$sum_points_countable,$remark_points);
			output_summary_row (_("Total Experience Points by Assurance"),$sum_experience,$sum_experience_countable,$remark_experience);
			output_summary_row (_("Total Experience Points (other ways)"),$sum_experience_other,$sum_experience_other_countable,$remark_experience_other);
			output_summary_row (_("Total Points"),"&nbsp;",$sum_points_countable + $sum_experience_countable + $sum_experience_other_countable,$issue_points_txt);
		}
		return $issue_points;
	}

	function output_given_assurances($userid,$support)
	{
		output_assurances_header(_("Assurance Points You Issued"),$support);
		output_given_assurances_content($userid,$points,$sum_experience,$support);
		output_assurances_footer(_("Total Points Issued"),$points,_("Total Experience Points"),$sum_experience,$support);
	}

	function output_received_assurances($userid,$support)
	{
		output_assurances_header(_("Your Assurance Points"),$support);
		output_received_assurances_content($userid,$points,$sum_experience,$support);
		output_assurances_footer(_("Total Assurance Points"),$points,_("Total Experience Points"),$sum_experience,$support);
	}

	function output_summary($userid)
	{
		output_summary_header();
		output_summary_content($userid,1);
		output_summary_footer();
	}

	function output_end_of_page()
	{
?>
	<p>[ <a href='javascript:history.go(-1)'><?=_("Go Back")?></a> ]</p>
<?
	}
?>