c7697ce8720947b784593278d4aac15fb720f997
[cacert.git] / includes / lib / account.php
1 <?php
2 /*
3 LibreSSL - CAcert web application
4 Copyright (C) 2004-2008 CAcert Inc.
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; version 2 of the License.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19
20 function fix_assurer_flag($userID)
21 {
22 // If requirements for assurers are modified see also scripts/cron/updatesort.php
23
24 // Update Assurer-Flag on users table if 100 points.
25 // Should the number of points be SUM(points) or SUM(awarded)?
26 $query = mysql_query('UPDATE `users` AS `u` SET `assurer` = 1 WHERE '.
27 '`u`.`id` = \''.(int)intval($userID).'\' AND '.
28 'EXISTS(SELECT 1 FROM `cats_passed` AS `cp`, `cats_variant` AS `cv` '.
29 'WHERE `cp`.`variant_id` = `cv`.`id` AND `cv`.`type_id` = 1 AND '.
30 '`cp`.`user_id` = `u`.`id`) AND '.
31 '(SELECT SUM(`points`) FROM `notary` AS `n` WHERE `n`.`to` = `u`.`id` '.
32 'AND (`n`.`expire` > now() OR `n`.`expire` IS NULL)) >= 100');
33 // Challenge has been passed and non-expired points >= 100
34
35 if (!$query) {
36 return false;
37 }
38
39 // Reset flag if requirements are not met
40 $query = mysql_query('UPDATE `users` AS `u` SET `assurer` = 0 WHERE '.
41 '`u`.`id` = \''.(int)intval($userID).'\' AND '.
42 '(NOT EXISTS(SELECT 1 FROM `cats_passed` AS `cp`, `cats_variant` AS '.
43 '`cv` WHERE `cp`.`variant_id` = `cv`.`id` AND `cv`.`type_id` = 1 '.
44 'AND `cp`.`user_id` = `u`.`id`) OR '.
45 '(SELECT SUM(`points`) FROM `notary` AS `n` WHERE `n`.`to` = `u`.`id` '.
46 'AND (`n`.`expire` > now() OR `n`.`expire` IS NULL)) < 100)');
47
48 if (!$query) {
49 return false;
50 }
51
52 return true;
53 }