1
|
|
|
<?php |
2
|
|
|
// This file is part of BOINC. |
3
|
|
|
// http://boinc.berkeley.edu |
4
|
|
|
// Copyright (C) 2018 University of California |
5
|
|
|
// |
6
|
|
|
// BOINC is free software; you can redistribute it and/or modify it |
7
|
|
|
// under the terms of the GNU Lesser General Public License |
8
|
|
|
// as published by the Free Software Foundation, |
9
|
|
|
// either version 3 of the License, or (at your option) any later version. |
10
|
|
|
// |
11
|
|
|
// BOINC is distributed in the hope that it will be useful, |
12
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of |
13
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
14
|
|
|
// See the GNU Lesser General Public License for more details. |
15
|
|
|
// |
16
|
|
|
// You should have received a copy of the GNU Lesser General Public License |
17
|
|
|
// along with BOINC. If not, see <http://www.gnu.org/licenses/>. |
18
|
|
|
|
19
|
|
|
// functions dealing with the consent and consent_type tables. |
20
|
|
|
|
21
|
|
|
require_once("../inc/boinc_db.inc"); |
22
|
|
|
require_once("../inc/util.inc"); |
23
|
|
|
|
24
|
|
|
define('CONSENT_TYPE_ENROLL','ENROLL'); |
25
|
|
|
|
26
|
|
|
function check_termsofuse() { |
27
|
|
|
return defined('TERMSOFUSE_FILE') and file_exists(TERMSOFUSE_FILE); |
|
|
|
|
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
function consent_to_a_policy( |
31
|
|
|
$user, $consent_type_id, $consent_flag, $consent_not_required, |
|
|
|
|
32
|
|
|
$source, $ctime = 0 |
|
|
|
|
33
|
|
|
) { |
34
|
|
|
$mys = BoincDb::escape_string($source); |
35
|
|
|
if ($ctime==0) { |
36
|
|
|
$mytime = $user->create_time; |
37
|
|
|
} else { |
38
|
|
|
$mytime = $ctime; |
39
|
|
|
} |
40
|
|
|
return BoincConsent::insert( |
41
|
|
|
"(userid, consent_type_id, consent_time, consent_flag, consent_not_required, source) " . |
42
|
|
|
"values($user->id, $consent_type_id, $mytime, $consent_flag, $consent_not_required, '$mys')" |
43
|
|
|
); |
44
|
|
|
|
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
// Checks to see if a user has consented to specfic consent_type_id. |
48
|
|
|
function check_user_consent($user, $consent_name) { |
49
|
|
|
list($checkct, $ctid) = check_consent_type($consent_name); |
50
|
|
|
if ($checkct) { |
51
|
|
|
$consent_result = BoincLatestConsent::lookup( |
52
|
|
|
"userid=$user->id AND consent_type_id=$ctid AND consent_flag=1" |
53
|
|
|
); |
54
|
|
|
if ($consent_result) { |
55
|
|
|
return TRUE; |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
return FALSE; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
// Check if a particular consent_type name is available. |
62
|
|
|
// Returns an array of format: (BOOLEAN, INTEGER). |
63
|
|
|
// The boolean is T/F depending on whether that consent_type exists, |
64
|
|
|
// and if checkenabled=TRUE, if the consent_type is enabled/available for use. |
65
|
|
|
// The integer is the consent_type_id- the id from consent_type table. |
66
|
|
|
// If the boolean is FALSE, the integer returned is -1. |
67
|
|
|
// |
68
|
|
|
function check_consent_type($name, $checkenabled=TRUE) { |
69
|
|
|
$name = BoincDb::escape_string($name); |
70
|
|
|
$ct = BoincConsentType::lookup("shortname = '$name'"); |
71
|
|
|
if ($ct and ( !$checkenabled or ($ct->enabled)) ) { |
|
|
|
|
72
|
|
|
return array(TRUE, $ct->id); |
73
|
|
|
} |
74
|
|
|
return array(FALSE, -1); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
// When a user uses the Web site to login, this function checks the |
78
|
|
|
// ENROLL consent and intercepts the login, |
79
|
|
|
// presenting the terms of use page Web form before they can continue. |
80
|
|
|
// |
81
|
|
|
function intercept_login($user, $perm, $in_next_url = "") { |
82
|
|
|
list($checkct, $ctid) = check_consent_type(CONSENT_TYPE_ENROLL); |
83
|
|
|
$config = get_config(); |
84
|
|
|
if (parse_bool($config, "enable_login_mustagree_termsofuse") |
85
|
|
|
and $checkct |
|
|
|
|
86
|
|
|
and check_termsofuse() |
|
|
|
|
87
|
|
|
and (!check_user_consent($user, CONSENT_TYPE_ENROLL)) |
|
|
|
|
88
|
|
|
) { |
89
|
|
|
// sent user to terms-of-use Web form after login |
90
|
|
|
$mytoken = create_token($user->id, TOKEN_TYPE_LOGIN_INTERCEPT, TOKEN_DURATION_TWO_HOURS); |
91
|
|
|
send_cookie('logintoken', $mytoken, false); |
92
|
|
|
send_cookie('tempuserid', $user->id, false); |
93
|
|
|
send_cookie('tempperm', $perm, false); |
94
|
|
|
$save_url = $in_next_url; |
95
|
|
|
return "user_agreetermsofuse.php?next_url=$save_url"; |
96
|
|
|
} else { |
97
|
|
|
send_cookie('auth', $user->authenticator, $perm); |
98
|
|
|
return $in_next_url; |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|
PHP has two types of connecting operators (logical operators, and boolean operators):
and
&&
or
||
The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like
&&
, or||
.Let’s take a look at a few examples:
Logical Operators are used for Control-Flow
One case where you explicitly want to use logical operators is for control-flow such as this:
Since
die
introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined withthrow
at this point:These limitations lead to logical operators rarely being of use in current PHP code.