-
Notifications
You must be signed in to change notification settings - Fork 7
/
bp_claim.php
287 lines (258 loc) · 8.56 KB
/
bp_claim.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
<?php
// This file is part of BOINC.
// http://boinc.berkeley.edu
// Copyright (C) 2019 University of California
//
// BOINC is free software; you can redistribute it and/or modify it
// under the terms of the GNU Lesser General Public License
// as published by the Free Software Foundation,
// either version 3 of the License, or (at your option) any later version.
//
// BOINC is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
// See the GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with BOINC. If not, see <http://www.gnu.org/licenses/>.
// let a user claim BOINC credit.
//
// 1) show a form where they pick a project and enter credentials
// 2) send an email w/ a code, and save code in user record
// 3) show a form for entering the code
// 4) fetch CPID, get credit info from netsoft
// 5) show user a page w/ credit info;
// make DB records:
// credit_claim
// (userid, email, project_id, CPID, date)
// credit_claim_project
// credit_claim_id, project_id, credit, (user)id,
// index project_id, user_id
//
// what stops a user from changing the email address of all their BOINC
// accounts, then claiming credit again (possibly as a different user?)
// after 1), if already a credit_claim record for that
// email/project, reject
// in 5), lookup each (project_id, user_id) in response.
// If already a credit_claim_project record, reject
require_once("../inc/util.inc");
require_once("../inc/su_project_infos.inc");
require_once("../inc/bp_db.inc");
function lookup_project_id($url) {
global $project_infos;
foreach ($project_infos as $id=>$p) {
if ($p->url == $url) {
return $id;
}
}
return 0;
}
// the user has filled out the confirm-email form
//
function action() {
global $project_infos, $user;
$passwd = post_str('passwd');
$code = post_str('code');
$cc_id = post_str('cc_id');
$cc = BPCreditClaim::lookup_id($cc_id);
if (!$cc || $cc->user_id != $user->id) {
error_page("No credit claim");
}
if (strtolower($code) != $cc->code) {
sleep(5);
error_page("Invalid code");
}
$email_addr = $cc->email_addr;
// check if anyone has successfully claimed this email
//
$cc2 = BPCreditClaim::lookup("email_addr='$email_addr' and status=2");
if ($cc2) {
error_page("Already claimed credit");
}
$project_id = $cc->project_id;
$project = $project_infos[$project_id];
// At this point everything looks OK.
// do a lookup_account() RPC to get authenticator
//
$passwd_hash = md5($passwd.strtolower($email_addr));
$url = $project->url."lookup_account.php?email_addr=$email_addr&passwd_hash=$passwd_hash";
$x = url_get_contents($url);
$e = parse_element($x, "<error_msg>");
if ($e) {
error_page("Error: $e");
}
$auth = parse_element($x, "<authenticator>");
if (!$auth) {
error_page("Couldn't get account info - try again later.");
}
// do a am_get_info() RPC to get the cross-project ID
//
$url = $project->url."am_get_info.php?account_key=$auth";
$x = url_get_contents($url);
$cpid = parse_element($x, "<cpid>");
// get the project/credit info from netsoft
//
$url = "http://boinc.netsoft-online.com/get_user.php?cpid=".$cpid;
$x = url_get_contents($url);
if (!$x) {
error_page("Can't get credit data - please try again later.");
}
//echo $x;
$xml_object = @simplexml_load_string($x);
$projects = @json_decode(json_encode((array)$xml_object))->project;
// see if anyone has claimed credit for any of the accounts
//
foreach ($projects as $p) {
$project_id = lookup_project_id($p->url);
if (!$project_id) continue;
$puid = $p->id;
$ccp = BPCreditClaimProject::lookup(
"project_id=$project_id and project_user_id=$puid"
);
if ($ccp) {
error_page("Credit already claimed");
}
}
page_head("Claimed credit for $email_addr");
start_table();
$total_credit = 0;
foreach ($projects as $p) {
$project_id = lookup_project_id($p->url);
if (!$project_id) continue;
$c = $p->total_credit;
row2($p->name, $c);
$total_credit += $c;
$puid = $p->id;
BPCreditClaimProject::insert(
"(credit_claim_id, project_id, credit, project_user_id) values ($cc->id, $project_id, $c, $puid)"
);
}
row2("Total credit", $total_credit);
end_table();
echo "<p>Next step: <a href=download_software.php>Download BOINC</a>";
page_tail();
$cc->update("status=2, total_credit=$total_credit");
}
// user filled out project/email form.
// check that this email isn't already claimed.
// then email them a code, and ask them to enter it
//
function code_form() {
global $user;
$project_id = post_str('project_id');
$email_addr = post_str('email_addr');
$passwd = post_str('passwd');
// see if there's a completed claim for this email
//
$cc = BPCreditClaim::lookup("email_addr='$email_addr' and status=2");
if ($cc) {
error_page("Credit for $email_addr has already been claimed.");
}
// if user claimed this email earlier, use that DB record
//
$now = time();
$cc = BPCreditClaim::lookup("email_addr='$email_addr' and user_id=$user->id");
if ($cc) {
$cc->update("create_time=$now, project_id=$project_id, status=0");
$code = $cc->code;
$cc_id = $cc->id;
} else {
$code = substr(random_string(), 0, 6);
$cc_id = BPCreditClaim::insert(
"(user_id, create_time, project_id, email_addr, code, status) values ($user->id, $now, $project_id, '$email_addr', '$code', 0)"
);
}
$subject = PROJECT.": verify email address";
$body = "Your email verification code is $code.
Please enter this code in your web browser.
";
$ret = send_email(null, $subject, $body, null, $email_addr);
if (!$ret) {
error_page("Couldn't send email to $email_addr");
}
page_head("Validate email address");
echo "
<p>
To confirm this email address,
a code has been sent to $email_addr.
";
form_start("bp_claim.php", "post");
form_input_hidden('passwd', $passwd);
form_input_hidden('cc_id', $cc_id);
form_input_text("Please enter the code here:", 'code');
form_submit('OK');
form_end();
page_tail();
}
function form() {
global $project_infos;
page_head("Claim BOINC credit");
form_start("bp_claim.php", "post");
$x = array();
foreach ($project_infos as $id=>$p) {
$x[] = array($id, $p->name);
}
form_select("Choose a BOINC project where you have an account:",
'project_id', $x
);
form_input_text("Email address of that account:", 'email_addr');
form_input_text("Password of that account:", 'passwd', '', 'password');
form_submit('OK');
form_end();
page_tail();
}
function display_cc($cc) {
global $project_infos;
$ccps = BPCreditClaimProject::enum("credit_claim_id=$cc->id");
$p = $project_infos[$cc->project_id];
start_table();
row2("Project", $p->name);
row2("Email address", $cc->email_addr);
foreach ($ccps as $ccp) {
$p = $project_infos[$ccp->project_id];
row2($p->name, $ccp->credit);
}
end_table();
}
function display() {
global $user;
page_head("Claimed credit");
$ccs = BPCreditClaim::enum("user_id=$user->id and status=2");
if (count($ccs) == 0) {
echo "No claimed credit so far.";
} else {
foreach ($ccs as $cc) {
display_cc($cc);
}
}
echo "<a href=bp_claim.php?form=1>Claim more credit<a/>\n";
page_tail();
}
function intro() {
page_head("Claim BOINC credit");
echo "
<p>
".PROJECT."grants 'tokens' for your computing.
If you're already running BOINC,
you can convert your existing credit into tokens.
Do you want to do this?
<p>
<a href=bp_claim.php?form=1>Yes - I'm already running BOINC<a/>.
<p>
<a href=download_software.php?dev=1>No - I'm new to BOINC<a/>.
";
page_tail();
}
$user = get_logged_in_user();
if (get_str('form', true)) {
form();
} else if (get_str('display', true)) {
display();
} else if (post_str('code', true)) {
action();
} else if (post_str('project_id', true)) {
code_form();
} else {
intro();
}
?>