Issues (1963)

html/inc/account.inc (4 issues)

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, 16);
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>%s</small></label>
55
        ', $name,
56
        tra("Show password")
57
    );
58
}
59
60
function create_account_form($teamid, $next_url) {
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
    if (USER_COUNTRY) {
106
        form_select(
107
            sprintf('<span title="%s">%s</span>',
108
                tra("Select the country you want to represent, if any."),
109
                tra("Country")
110
            ),
111
            "country",
112
            country_select_options()
113
        );
114
    }
115
    if (POSTAL_CODE) {
116
        form_input_text(
117
            tra("Postal or ZIP Code")."<br><small>".tra("Optional")."</small>",
118
            "postal_code"
119
        );
120
    }
121
122
    // Add terms of use to Web form. User must agree by checking the checkbox.
123
    list($checkct, $ctid) = check_consent_type(CONSENT_TYPE_ENROLL);
124
    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...
125
        $terms_of_use = trim(file_get_contents(TERMSOFUSE_FILE));
126
        if ($terms_of_use) {
127
            panel(tra('Terms of Use'), function() use($terms_of_use) {
0 ignored issues
show
Expected 1 space after USE keyword; found 0
Loading history...
128
                echo nl2br($terms_of_use);
129
            }
130
            );
131
            $myitems = array(
132
                array("agree_to_terms_of_use", "", false),
133
            );
134
            form_checkboxes(tra("Do you agree to the terms of use above?"), $myitems, 'tabindex="0"');
135
        }
136
    }
137
138
}
139
140
function login_form($next_url) {
141
    form_start(secure_url_base()."/login_action.php", "post");
142
    form_input_hidden("next_url", $next_url);
143
    if (LDAP_HOST) {
144
        $x = "Email address or LDAP user name:";
145
    } else {
146
        $x = tra("Email address:");
147
    }
148
    form_input_text($x, "email_addr", '', 'text', $attrs='autofocus tabindex="1"');
149
    form_input_text(
150
        tra("Password:").'<br><small><a href="get_passwd.php">' . tra("forgot password?") . "</a></small>",
151
        "passwd",
152
        "",
153
        "password",
154
        'id="passwd" tabindex="2"',
155
        passwd_visible_checkbox("passwd")
156
    );
157
    form_checkboxes(tra("Stay logged in"),
158
        array(array("stay_logged_in", "", true)),
159
        'tabindex="3"'
160
    );
161
    form_submit(tra("Log in"), 'tabindex="4"');
162
    form_end();
163
}
164
165
function user_agreetermsofuse_form($next_url) {
166
    form_start(secure_url_base()."/user_agreetermsofuse_action.php", "post");
167
    form_input_hidden("next_url", $next_url);
168
169
    $terms_of_use = trim(file_get_contents(TERMSOFUSE_FILE));
170
    if ($terms_of_use) {
171
        panel(tra('Terms of Use'), function() use($terms_of_use) {
0 ignored issues
show
Expected 1 space after USE keyword; found 0
Loading history...
172
            echo nl2br($terms_of_use);
173
        }
174
        );
175
        $myitems = array(
176
            array("agree_to_terms_of_use", "", false),
177
        );
178
        form_checkboxes(tra("Do you agree to the terms of use above?"), $myitems, 'tabindex="0"');
179
    }
180
    else {
0 ignored issues
show
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...
181
        // error - no terms of use for user to agree to!
182
    }
183
184
    form_submit(tra("I agree"));
185
    form_end();
186
}
187
188
?>
189