Issues (1963)

html/inc/delete_account.inc (2 issues)

1
<?php
2
// This file is part of BOINC.
3
// https://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 <https://www.gnu.org/licenses/>.
18
19
require_once("../inc/common_defs.inc");
20
require_once("../inc/util.inc");
21
require_once("../inc/user.inc");
22
require_once("../inc/user_util.inc");
23
require_once("../inc/host.inc");
24
require_once("../inc/friend.inc");
25
require_once("../inc/boinc_db.inc");
26
require_once("../inc/submit_util.inc");
27
require_once("../project/project.inc");
28
29
// Constants for different methods of deleting accounts
30
// These correspond to the value used in the config.xml
31
// field of <enable_delete_account/>
32
//
33
define("DELETE_ACCOUNT_METHOD_OBFUSCATE", 1);
34
define("DELETE_ACCOUNT_METHOD_WIPE", 2);
35
define("DELETE_ACCOUNT_METHOD_PROJECT_DEFINED", 3);
36
37
// Constant for how long to sleep after invalidating authenticator
38
// before proceeding with rest of delete
39
// This is done on the chance that there is an active scheduler request
40
// in progress
41
//
42
if (!defined("DELETE_DELAY")) define("DELETE_DELAY", 2);
43
44
function is_delete_account_token_valid($userid, $token) {
45
    if (!is_valid_token($userid, $token, TOKEN_TYPE_DELETE_ACCOUNT) ) {
46
        sleep(LOGIN_FAIL_SLEEP_SEC);
47
        return false;
48
    }
49
    return true;
50
}
51
52
// Save the minimal information from the user and their hosts
53
// so that db_dump can provide the information necessary
54
// to export the deleted_user and deleted_host files.
55
// These records are deleted after 60 days by the
56
// daily ops task "delete_expired_users_and_hosts.php"
57
//
58
function insert_deleted_records($user) {
59
    BoincUserDeleted::insert_user($user);
60
    BoincHostDeleted::insert_hosts_for_user($user);
61
}
62
63
// This method selects which delete method to utilize.
64
// Projects can implement their own method
65
// and make that a third mechanism if they have a need to
66
//
67
function delete_account($user) {
68
    $config = get_config();
69
    $enable_delete_account = parse_config($config, "<enable_delete_account>");
70
    if ($enable_delete_account == DELETE_ACCOUNT_METHOD_OBFUSCATE) {
71
        return obfuscate_account($user);
72
    } else if ($enable_delete_account == DELETE_ACCOUNT_METHOD_WIPE) {
73
        return wipe_account($user);
74
    } else if ($enable_delete_account == DELETE_ACCOUNT_METHOD_PROJECT_DEFINED) {
75
        return project_delete_account($user);
0 ignored issues
show
Are you sure the usage of project_delete_account($user) is correct as it seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
76
    }
77
    return ERR_NO_OPTION;
78
}
79
80
// invalidate the authenticator and then sleep for DELETE_DELAY seconds
81
// in order to let any active scheduler requests complete.
82
//
83
function invalidate_authenticator($user) {
84
    $x = "deleted_".time()."_".random_string();
85
    $retval = $user->update("authenticator='$x'");
86
    if (!$retval) return ERR_DB_NOT_FOUND;
87
    sleep(DELETE_DELAY);
88
    return 0;
89
}
90
91
// "obfuscate" an account: leave user record (for DB consistency) but:
92
//  - set email address and authenticator to "deleted_time_randomstring"
93
//  - clear name, country, postal_code
94
//  - remove from team
95
//  - delete posts, subscriptions, and forum prefs
96
//  - delete private messages (sent and received)
97
//  - delete profile and associated image
98
// for each host:
99
//  - clear domain_name, last_ip_addr
100
//
101
function obfuscate_account($user) {
102
    $retval = invalidate_authenticator($user);
103
    if ($retval) return $retval;
104
    insert_deleted_records($user);
105
    $x = "deleted_".time()."_".random_string();
106
    $retval = $user->update("email_addr='$x', authenticator='$x', name='deleted', country='', postal_code='', has_profile=0");
107
    if (!$retval) return ERR_DB_NOT_FOUND;
108
    user_quit_team($user);
109
    forum_delete_user($user);
110
    pm_delete_user($user);
111
    anonymize_hosts($user);
112
    delete_profile($user);
113
    delete_friends($user);
114
    return 0;
115
}
116
117
118
// return true if the result is in progress
119
//
120
function is_in_progress($res) {
121
    return ($res->server_state == RESULT_SERVER_STATE_IN_PROGRESS);
122
}
123
124
// returns true if the result finished successfully but is either
125
// pending validation or inconclusive
126
//
127
function is_over_but_not_validated($res) {
128
    if ($res->server_state == RESULT_SERVER_STATE_OVER && $res->outcome == RESULT_OUTCOME_SUCCESS &&
129
            ($res->validate_state == VALIDATE_STATE_INIT || $res->validate_state == VALIDATE_STATE_INCONCLUSIVE) ) {
130
        return true;
131
    }
132
    return false;
133
}
134
135
function transition_workunit($res) {
136
    $now = time();
137
    BoincWorkunit::update_aux("transition_time=$now where id=$res->workunitid");
138
}
139
140
// This method handles dissassociating the user from their results.
141
// It will cancel all in progress or returned, but not yet validated
142
// results for a user.  For other results, it will set the userid and
143
// hostid fields to 0
144
//
145
function cancel_results_for_user($user) {
146
    $ress = BoincResult::enum("userid = $user->id");
147
    $cancel_clause="server_state=".RESULT_SERVER_STATE_OVER.", outcome=".RESULT_OUTCOME_CLIENT_DETACHED.", validate_state=".VALIDATE_STATE_INVALID;
148
    $set_id_clause="hostid=0, userid=0";
149
    foreach ($ress as $res) {
150
        if (is_in_progress($res) || is_over_but_not_validated($res)) {
151
            $res->update($cancel_clause.", ".$set_id_clause);
152
            transition_workunit($res);
153
        } else {
154
            $res->update($set_id_clause);
155
        }
156
    }
157
}
158
159
// This method deletes all rows from the database associated with the user
160
//
161
function wipe_account($user) {
162
    $retval = invalidate_authenticator($user);
163
    if ($retval) return $retval;
164
165
    //insert records into tables for db_dump to announce deletion of user
166
    //
167
    insert_deleted_records($user);
168
169
    // delete remote submit user
170
    //
171
    delete_remote_submit_user($user);  // from submit_util.inc
172
173
    // remove user's team records
174
    //
175
    user_erase_team_owner($user);      // from team.inc
176
    user_quit_team($user);        // from team.inc
177
    user_erase_team_delta($user); // from team.inc
178
179
    // Items that do not have logic elsewhere
180
    // and do not have objects in boinc_db.inc
181
    //
182
    $db = BoincDb::get();
183
    if (!$db) die("no DB connection");
0 ignored issues
show
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
184
    $db->do_query("delete from credited_job where userid = $user->id");
185
    $db->do_query("delete from donation_paypal where userid = $user->id");
186
    $db->do_query("delete from banishment_vote where userid = $user->id");
187
    $db->do_query("delete from post_ratings where post in ( select id from post where user = $user->id )");
188
    $db->do_query("delete from post_ratings where user = $user->id");
189
    $db->do_query("delete from msg_from_host where hostid in (select id from host where userid = $user->id )");
190
    $db->do_query("delete from msg_to_host where hostid in (select id from host where userid = $user->id )");
191
    $db->do_query("delete from sent_email where userid = $user->id");
192
193
    //It is much faster to update results with single query
194
    //
195
    cancel_results_for_user($user);
196
197
    BoincHostAppVersion::delete_for_user($user->id);
198
    BoincHost::delete_for_user($user->id);
199
    BoincConsent::delete_for_user($user->id);
200
    BoincTeamAdmin::delete("userid=$user->id");
201
202
    // final action
203
    delete_user($user);  //from user_util.inc
204
    return 0;
205
}
206