Passed
Push — master ( 6fae8e...4198df )
by Kevin
01:15 queued 23s
created

user_agreetermsofuse_form()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 21
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 12
c 0
b 0
f 0
nc 2
nop 1
dl 0
loc 21
rs 9.8666
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 related to account creation and login:
20
// - forms for create / login
21
// - function to make login token
22
23
include_once("../inc/consent.inc");
24
25
// If have recent token, return it.
26
// Else make login token, store in user record, return token
27
//
28
function make_login_token($user) {
29
    $now = time();
30
    if ($now - $user->login_token_time < 86400) {
31
        $user->update("login_token_time=$now");
32
        return $user->login_token;
33
    }
34
    $token = substr(random_string(), 0, 8);
35
    $user->update("login_token='$token', login_token_time=$now");
36
    return $token;
37
}
38
39
// return HTML string for a checkbox for toggling password visibility
40
//
41
function passwd_visible_checkbox($name) {
42
    return sprintf('
43
<script>
44
function toggle_passwd() {
45
    var c = document.getElementById("passwd_visible");
46
    var x = document.getElementById("%s");
47
    if (c.checked) {
48
        x.type = "text";
49
    } else {
50
        x.type = "password";
51
    }
52
}
53
</script>
54
<input type="checkbox" id="passwd_visible" onclick="toggle_passwd()"> <label for="passwd_visible"><small>Show password</small></label>
55
        ', $name
56
    );
57
}
58
59
function create_account_form($teamid, $next_url) {
60
    global $recaptcha_public_key;
61
    form_input_hidden('next_url', $next_url);
62
63
    if ($teamid) {
64
        form_input_hidden('teamid', $teamid);
65
    }
66
67
    // Using invitation codes to restrict access?
68
    //
69
    if (defined('INVITE_CODES')) {
70
        form_input_text(
71
            sprintf('<span title="%s">%s</span>',
72
                tra("An invitation code is required to create an account."),
73
                tra("Invitation code")
74
            ),
75
            "invite_code"
76
        );
77
    }
78
79
    form_input_text(
80
        sprintf('<span title="%s">%s</span>',
81
            tra("Identifies you on our web site. Use your real name or a nickname."),
82
            tra("Screen name")
83
        ),
84
        "new_name"
85
    );
86
    form_input_text(
87
        sprintf('<span title="%s">%s</span>',
88
            tra("Must be a valid address of the form 'name@domain'."),
89
            tra("Email address")
90
        ),
91
        "new_email_addr"
92
    );
93
    $min_passwd_length = parse_element(get_config(), "<min_passwd_length>");
94
    if (!$min_passwd_length) {
95
        $min_passwd_length = 6;
96
    }
97
98
    form_input_text(
99
        sprintf('<span title="%s">%s</span>',
100
            tra("Must be at least %1 characters", $min_passwd_length),
101
            tra("Password")
102
        ),
103
        "passwd", "", "password",'id="passwd"',passwd_visible_checkbox("passwd")
104
    );
105
    form_select(
106
        sprintf('<span title="%s">%s</span>',
107
            tra("Select the country you want to represent, if any."),
108
            tra("Country")
109
        ),
110
        "country",
111
        country_select_options()
112
    );
113
    if (POSTAL_CODE) {
114
        form_input_text(
115
            tra("Postal or ZIP Code")."<br><small>".tra("Optional")."</small>",
116
            "postal_code"
117
        );
118
    }
119
120
    // Add terms of use to Web form. User must agree by checking the checkbox.
121
    list($checkct, $ctid) = check_consent_type(CONSENT_TYPE_ENROLL);
122
    if ($checkct and check_termsofuse()) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
Using logical operators such as and instead of && is generally not recommended.

PHP has two types of connecting operators (logical operators, and boolean operators):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning 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 have lower precedence:
$f = false or true;

// is executed like this:
($f = false) or true;


// Boolean operators have higher precedence:
$f = false || true;

// is executed like this:
$f = (false || true);

Logical Operators are used for Control-Flow

One case where you explicitly want to use logical operators is for control-flow such as this:

$x === 5
    or die('$x must be 5.');

// Instead of
if ($x !== 5) {
    die('$x must be 5.');
}

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 with throw at this point:

// The following is currently a parse error.
$x === 5
    or throw new RuntimeException('$x must be 5.');

These limitations lead to logical operators rarely being of use in current PHP code.

Loading history...
123
        $terms_of_use = trim(file_get_contents(TERMSOFUSE_FILE));
124
        if ($terms_of_use) {
125
            panel(tra('Terms of Use'), function() use($terms_of_use) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space after USE keyword; found 0
Loading history...
126
                echo nl2br($terms_of_use);
127
            }
128
            );
129
            $myitems = array(
130
                array("agree_to_terms_of_use", "", false),
131
            );
132
            form_checkboxes(tra("Do you agree to the terms of use above?"), $myitems, 'tabindex="0"');
133
        }
134
    }
135
136
}
137
138
function login_form($next_url) {
139
    form_start(secure_url_base()."/login_action.php", "post");
140
    form_input_hidden("next_url", $next_url);
141
    if (LDAP_HOST) {
142
        $x = "Email address or LDAP user name:";
143
    } else {
144
        $x = tra("Email address:");
145
    }
146
    form_input_text($x, "email_addr", '', 'text', $attrs='autofocus tabindex="1"');
147
    form_input_text(
148
        tra("Password:").'<br><small><a href="get_passwd.php">' . tra("forgot password?") . "</a></small>",
149
        "passwd",
150
        "",
151
        "password",
152
        'id="passwd" tabindex="2"',
153
        passwd_visible_checkbox("passwd")
154
    );
155
    form_checkboxes(tra("Stay logged in"),
156
        array(array("stay_logged_in", "", true)), 
157
        'tabindex="3"'
158
    );
159
    form_submit("Log in", 'tabindex="4"');
160
    form_end();
161
}
162
163
function user_agreetermsofuse_form($next_url) {
164
    form_start(secure_url_base()."/user_agreetermsofuse_action.php", "post");
165
    form_input_hidden("next_url", $next_url);
166
167
    $terms_of_use = trim(file_get_contents(TERMSOFUSE_FILE));
168
    if ($terms_of_use) {
169
        panel(tra('Terms of Use'), function() use($terms_of_use) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space after USE keyword; found 0
Loading history...
170
            echo nl2br($terms_of_use);
171
        }
172
        );
173
        $myitems = array(
174
            array("agree_to_terms_of_use", "", false),
175
        );
176
        form_checkboxes(tra("Do you agree to the terms of use above?"), $myitems, 'tabindex="0"');
177
    }
178
    else {
0 ignored issues
show
Unused Code introduced by
This else statement is empty and can be removed.

This check looks for the else branches of if statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These else branches can be removed.

if (rand(1, 6) > 3) {
print "Check failed";
} else {
    //print "Check succeeded";
}

could be turned into

if (rand(1, 6) > 3) {
    print "Check failed";
}

This is much more concise to read.

Loading history...
179
        // error - no terms of use for user to agree to!
180
    }
181
182
    form_submit(tra("I agree"));
183
    form_end();
184
}
185
186
?>
187