Passed
Push — dpa_submit26 ( 7309fb )
by David
09:05
created

BoincResult::delete_aux()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
1
<?php
2
// This file is part of BOINC.
3
// https://boinc.berkeley.edu
4
// Copyright (C) 2024 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
// A project can have one or more BOINC databases:
20
// DB 0:
21
//      the main DB; read/write
22
//      identified in config file by db_host, db_name, db_user, db_passwd
23
//      db_host defaults to localhost
24
// DB 1:
25
//      read-only replica; identified by
26
//      replica_db_host/name/user/passwd (must include all)
27
// DB 2:
28
//      read-only replica; identified by
29
//      replica2_db_host/name/user/passwd (must include all)
30
// ... and potentially more
31
32
function incs() {
33
    $d = dirname(__FILE__);
34
    require_once("$d/db_conn.inc");
35
    require_once("$d/util_basic.inc");
36
}
37
38
incs();
39
40
// class BoincDb represents a connection to a BOINC database.
41
// All its members are static, so there's only 1 connection at a time.
42
// get(n) establishes a connection to DB n,
43
// or DB 0 if that fails or doesn't exit.
44
// close() closes the connection.
45
46
class BoincDb {
47
    static $instance;       // a DbConn object, or null
48
    static $dbnum;          // which replica we're connected to
49
50
    // connect to DB $dbnum (0, 1, ...)
51
    // If the requested DB doesn't exist or connection fails, connect to DB 0.
52
    // Set self::$instance; no return value
53
    //
54
    static function get_aux($dbnum) {
55
        $instance = new DbConn();
56
        self::$instance = null;
57
        $config = get_config();
58
        if ($dbnum) {
59
            $r = $dbnum==1?'':strval($dbnum);
60
            $host = parse_config($config, sprintf('<replica%s_db_host>', $r));
61
            $name = parse_config($config, sprintf('<replica%s_db_name>', $r));
62
            $user = parse_config($config, sprintf('<replica%s_db_user>', $r));
63
            $passwd = parse_config($config, sprintf('<replica%s_db_passwd>', $r));
64
            if ($host && $name && $user && $passwd) {
65
                $retval = $instance->init_conn($user, $passwd, $host, $name);
66
                if ($retval) {
67
                    //error_log("BoincDb::get_aux(): connected to replica DB $dbnum");
68
                    self::$instance = $instance;
69
                    self::$dbnum = $dbnum;
70
                    return;
71
                }
72
            }
73
            // if can't connect to replica, fall through and try DB 0
74
        }
75
        $host = parse_config($config, '<db_host>');
76
        if (!$host) $host = 'localhost';
77
        $user = parse_config($config, '<db_user>');
78
        $name = parse_config($config, '<db_name>');
79
        $passwd = parse_config($config, '<db_passwd>');
80
        if (!$name || !$user || !$passwd) {
81
            error_log("BoincDb::get_aux(): must specify DB name, user, passwd");
0 ignored issues
show
Coding Style introduced by
The use of function error_log() is discouraged
Loading history...
82
            return;
83
        }
84
        $retval = $instance->init_conn($user, $passwd, $host, $name);
85
        if ($retval) {
86
            //error_log("BoincDb::get_aux(): connected to DB $dbnum");
87
            self::$instance = $instance;
88
            self::$dbnum = 0;
89
            return;
90
        }
91
        error_log("BoincDb::get_aux(): Couldn't connect to DB $dbnum");
0 ignored issues
show
Coding Style introduced by
The use of function error_log() is discouraged
Loading history...
92
    }
93
94
    // connect to DB $dbnum, but first:
95
    // 1) check for a cached connection
96
    // 2) check whether the "stop_web" trigger file is present
97
    //
98
    // If there's a page that's guaranteed to do only reads, put
99
    // BoincDb::get(true);
100
    // at the top of it.
101
    //
102
    // Note: true == 1.
103
    // You can also use 2, 3... to select other replicas
104
    //
105
    static function get($dbnum = 0) {
106
        global $generating_xml;
107
        if (isset(self::$instance)) {
108
            if (intval(self::$dbnum) == intval($dbnum)) {
109
                return self::$instance;
110
            }
111
            self::close();
112
        }
113
        if (web_stopped()) {
114
            if ($generating_xml) {
115
                xml_error(-183, "project down for maintenance");
116
            } else {
117
                show_project_down();
118
            }
119
        }
120
        self::get_aux($dbnum);
121
        if (self::$instance) {
122
            return self::$instance;
123
        }
124
        if ($generating_xml) {
125
            xml_error(-138, "Can't connect to database");
126
        } else {
127
            error_page("Can't connect to database");
128
        }
129
    }
130
131
    static function close() {
132
        if (isset(self::$instance)) {
133
            self::$instance->close();
134
            self::$instance = null;
135
        }
136
    }
137
138
    static function escape_string($string) {
139
        if (!$string) return '';
140
        $db = self::get();
141
        return $db->base_escape_string(trim($string));
142
    }
143
    static function error() {
144
        $db = self::get();
145
        return $db->base_error();
146
    }
147
148
    // if your DB connection times out, call this, then call get() again
149
    //
150
    static function reset_connection() {
151
        self::$instance = null;
152
    }
153
}
154
155
#[AllowDynamicProperties]
0 ignored issues
show
Coding Style introduced by
Perl-style comments are not allowed. Use "// Comment." or "/* comment */" instead.
Loading history...
156
class BoincUser {
157
    public $prefs;
158
    static $cache;
159
    static function lookup($clause) {
160
        $db = BoincDb::get();
161
        return $db->lookup('user', 'BoincUser', $clause);
162
    }
163
164
    static function lookup_id_nocache($id) {
165
        $db = BoincDb::get();
166
        return $db->lookup_id($id, 'user', 'BoincUser');
167
    }
168
    static function lookup_id($id) {
169
        if (!isset(self::$cache[$id])) {
170
            self::$cache[$id] = self::lookup_id_nocache($id);
171
        }
172
        return self::$cache[$id];
173
    }
174
    static function lookup_auth($auth) {
175
        $auth = BoincDb::escape_string($auth);
176
        return self::lookup("authenticator='$auth'");
177
    }
178
    static function lookup_email_addr($email_addr) {
179
        $email_addr = strtolower(BoincDb::escape_string($email_addr));
180
        return self::lookup("email_addr='$email_addr'");
181
    }
182
    //This is to find email addresses that have changed in the past 7 days.
183
    static function lookup_prev_email_addr($email_addr) {
184
        $email_addr = strtolower(BoincDb::escape_string($email_addr));
185
        $mytime = time() - 604800;
186
        return self::lookup("email_addr_change_time > $mytime and previous_email_addr='$email_addr'");
187
    }
188
    // name is not necessarily unique
189
    //
190
    static function lookup_name($name) {
191
        $name = BoincDb::escape_string($name);
192
        $users = BoincUser::enum("name='$name'");
193
        return $users;
194
    }
195
    static function count($clause) {
196
        $db = BoincDb::get();
197
        return $db->count('user', $clause);
198
    }
199
    static function max($field) {
200
        $db = BoincDb::get();
201
        return $db->max('user', $field);
202
    }
203
    function update($clause) {
204
        $db = BoincDb::get();
205
        return $db->update($this, 'user', $clause);
206
    }
207
    static function enum($where_clause, $order_clause=null) {
208
        $db = BoincDb::get();
209
        return $db->enum('user', 'BoincUser', $where_clause, $order_clause);
210
    }
211
    static function enum_fields($fields, $where_clause, $order_clause=null) {
212
        $db = BoincDb::get();
213
        return $db->enum_fields(
214
            'user', 'BoincUser', $fields, $where_clause, $order_clause
215
        );
216
    }
217
    static function insert($clause) {
218
        $db = BoincDb::get();
219
        $ret = $db->insert('user', $clause);
220
        if (!$ret) return 0;
221
        return $db->insert_id();
222
    }
223
    function delete() {
224
        $db = BoincDb::get();
225
        $db->delete_aux('profile', "userid=$this->id");
0 ignored issues
show
Bug Best Practice introduced by
The property id does not exist on BoincUser. Did you maybe forget to declare it?
Loading history...
226
        return $db->delete($this, 'user');
227
    }
228
    static function sum($field) {
229
        $db = BoincDb::get();
230
        return $db->sum('user', $field);
231
    }
232
    static function percentile($field, $clause, $pct) {
233
        $db = BoincDb::get();
234
        return $db->percentile('user', $field, $clause, $pct);
235
    }
236
}
237
238
#[AllowDynamicProperties]
0 ignored issues
show
Coding Style introduced by
Perl-style comments are not allowed. Use "// Comment." or "/* comment */" instead.
Loading history...
239
class BoincTeam {
240
    static $cache;
241
    static function insert($clause) {
242
        $db = BoincDb::get();
243
        $ret = $db->insert('team', $clause);
244
        if (!$ret) return 0;
245
        return $db->insert_id();
246
    }
247
    static function lookup_id_nocache($id) {
248
        $db = BoincDb::get();
249
        return $db->lookup_id($id, 'team', 'BoincTeam');
250
    }
251
    static function lookup_id($id) {
252
        if (!isset(self::$cache[$id])) {
253
            self::$cache[$id] = self::lookup_id_nocache($id);
254
        }
255
        return self::$cache[$id];
256
    }
257
    function update($clause) {
258
        $db = BoincDb::get();
259
        return $db->update($this, 'team', $clause);
260
    }
261
    static function enum($where_clause, $order_clause=null) {
262
        $db = BoincDb::get();
263
        return $db->enum('team', 'BoincTeam', $where_clause, $order_clause);
264
    }
265
    static function lookup($clause) {
266
        $db = BoincDb::get();
267
        return $db->lookup('team', 'BoincTeam', $clause);
268
    }
269
    static function lookup_name($name) {
270
        $db = BoincDb::get();
271
        $name = BoincDb::escape_string($name);
272
        return self::lookup("name='$name'");
273
    }
274
    function delete() {
275
        $db = BoincDb::get();
276
        return $db->delete($this, 'team');
277
    }
278
    static function percentile($field, $clause, $pct) {
279
        $db = BoincDb::get();
280
        return $db->percentile('team', $field, $clause, $pct);
281
    }
282
    static function max($field) {
283
        $db = BoincDb::get();
284
        return $db->max('team', $field);
285
    }
286
    static function enum_fields($fields, $where_clause, $order_clause=null) {
287
        $db = BoincDb::get();
288
        return $db->enum_fields(
289
            'team', 'BoincTeam', $fields, $where_clause, $order_clause
290
        );
291
    }
292
}
293
294
#[AllowDynamicProperties]
0 ignored issues
show
Coding Style introduced by
Perl-style comments are not allowed. Use "// Comment." or "/* comment */" instead.
Loading history...
295
class BoincTeamDelta {
296
    static function insert($clause) {
297
        $db = BoincDb::get();
298
        return $db->insert('team_delta', $clause);
299
    }
300
    static function enum($where_clause) {
301
        $db = BoincDb::get();
302
        return $db->enum('team_delta', 'BoincTeamDelta', $where_clause);
303
    }
304
    static function delete_for_user($user_id) {
305
        $db = BoincDb::get();
306
        return $db->delete_aux('team_delta', "userid=$user_id");
307
    }
308
}
309
310
#[AllowDynamicProperties]
0 ignored issues
show
Coding Style introduced by
Perl-style comments are not allowed. Use "// Comment." or "/* comment */" instead.
Loading history...
311
class BoincHost {
312
    static function lookup_id($id) {
313
        $db = BoincDb::get();
314
        return $db->lookup_id($id, 'host', 'BoincHost');
315
    }
316
    function update($clause) {
317
        $db = BoincDb::get();
318
        return $db->update($this, 'host', $clause);
319
    }
320
    function delete() {
321
        $db = BoincDb::get();
322
        return $db->delete($this, 'host');
323
    }
324
    static function enum($where_clause, $order_clause=null) {
325
        $db = BoincDb::get();
326
        return $db->enum('host', 'BoincHost', $where_clause, $order_clause);
327
    }
328
    static function enum_fields($fields, $where_clause, $order_clause=null) {
329
        $db = BoincDb::get();
330
        return $db->enum_fields(
331
            'host', 'BoincHost', $fields, $where_clause, $order_clause
332
        );
333
    }
334
    static function count($clause) {
335
        $db = BoincDb::get();
336
        return $db->count('host', $clause);
337
    }
338
    static function lookup_cpid($cpid) {
339
        $db = BoincDb::get();
340
        $cpid = BoincDb::escape_string($cpid);
341
        return $db->lookup('host', 'BoincHost', "host_cpid='$cpid'");
342
    }
343
    static function insert($clause) {
344
        $db = BoincDb::get();
345
        $ret = $db->insert('host', $clause);
346
        if (!$ret) return $ret;
347
        return $db->insert_id();
348
    }
349
    static function delete_for_user($user_id) {
350
        $db = BoincDb::get();
351
        return $db->delete_aux('host', "userid=$user_id");
352
    }
353
}
354
355
#[AllowDynamicProperties]
0 ignored issues
show
Coding Style introduced by
Perl-style comments are not allowed. Use "// Comment." or "/* comment */" instead.
Loading history...
356
class BoincResult {
357
    static function count($clause) {
358
        $db = BoincDb::get();
359
        return $db->count('result', $clause);
360
    }
361
    static function enum($where_clause) {
362
        $db = BoincDb::get();
363
        return $db->enum('result', 'BoincResult', $where_clause);
364
    }
365
	static function enum_fields($fields, $where_clause, $order_clause=null) {
366
        $db = BoincDb::get();
367
		return $db->enum_fields(
368
            'result', 'BoincResult', $fields, $where_clause, $order_clause
369
        );
370
	}
371
    function update($clause) {
372
        $db = BoincDb::get();
373
        return $db->update($this, 'result', $clause);
374
    }
375
    static function update_aux($clause) {
376
        $db = BoincDb::get();
377
        return $db->update_aux('result', $clause);
378
    }
379
    static function lookup_id($id) {
380
        $db = BoincDb::get();
381
        return $db->lookup_id($id, 'result', 'BoincResult');
382
    }
383
    static function lookup_name($name) {
384
        $db = BoincDb::get();
385
        $name = BoincDb::escape_string($name);
386
        return $db->lookup('result', 'BoincResult', "name='$name'");
387
    }
388
    function delete() {
389
        $db = BoincDb::get();
390
        return $db->delete($this, 'result');
391
    }
392
    static function delete_aux($clause) {
393
        $db = BoincDb::get();
394
        return $db->delete_aux('result', $clause);
395
    }
396
}
397
398
#[AllowDynamicProperties]
0 ignored issues
show
Coding Style introduced by
Perl-style comments are not allowed. Use "// Comment." or "/* comment */" instead.
Loading history...
399
class BoincWorkunit {
400
    static function lookup_id($id) {
401
        $db = BoincDb::get();
402
        return $db->lookup_id($id, 'workunit', 'BoincWorkunit');
403
    }
404
    static function lookup($clause) {
405
        $db = BoincDb::get();
406
        return $db->lookup('workunit', 'BoincWorkunit', $clause);
407
    }
408
    static function insert($clause) {
409
        $db = BoincDb::get();
410
        $ret = $db->insert('workunit', $clause);
411
        if (!$ret) return $ret;
412
        return $db->insert_id();
413
    }
414
    static function enum($where_clause) {
415
        $db = BoincDb::get();
416
        return $db->enum('workunit', 'BoincWorkunit', $where_clause);
417
    }
418
    static function enum_fields($fields, $where_clause, $order_clause=null) {
419
        $db = BoincDb::get();
420
        return $db->enum_fields('workunit', 'BoincWorkunit', $fields, $where_clause, $order_clause);
421
    }
422
    function update($clause) {
423
        $db = BoincDb::get();
424
        return $db->update($this, 'workunit', $clause);
425
    }
426
    static function update_aux($clause) {
427
        $db = BoincDb::get();
428
        return $db->update_aux('workunit', $clause);
429
    }
430
    static function count($clause) {
431
        $db = BoincDb::get();
432
        return $db->count('workunit', $clause);
433
    }
434
    static function delete_aux($clause) {
435
        $db = BoincDb::get();
436
        return $db->delete_aux('workunit', $clause);
437
    }
438
}
439
440
#[AllowDynamicProperties]
0 ignored issues
show
Coding Style introduced by
Perl-style comments are not allowed. Use "// Comment." or "/* comment */" instead.
Loading history...
441
class BoincApp {
442
    static function lookup_id($id) {
443
        $db = BoincDb::get();
444
        return $db->lookup_id($id, 'app', 'BoincApp');
445
    }
446
    static function lookup($clause) {
447
        $db = BoincDb::get();
448
        return $db->lookup('app', 'BoincApp', $clause);
449
    }
450
    static function enum($where_clause) {
451
        $db = BoincDb::get();
452
        return $db->enum('app', 'BoincApp', $where_clause);
453
    }
454
    static function insert($clause) {
455
        $db = BoincDb::get();
456
        $ret = $db->insert('app', $clause);
457
        if (!$ret) return $ret;
458
        return $db->insert_id();
459
    }
460
    function update($clause) {
461
        $db = BoincDb::get();
462
        return $db->update($this, 'app', $clause);
463
    }
464
    static function sum($field, $clause=null) {
465
        $db = BoincDb::get();
466
        return $db->sum('app', $field, $clause);
467
    }
468
}
469
470
#[AllowDynamicProperties]
0 ignored issues
show
Coding Style introduced by
Perl-style comments are not allowed. Use "// Comment." or "/* comment */" instead.
Loading history...
471
class BoincAppVersion {
472
    static function enum($where_clause) {
473
        $db = BoincDb::get();
474
        return $db->enum('app_version', 'BoincAppVersion', $where_clause);
475
    }
476
    static function lookup($clause) {
477
        $db = BoincDb::get();
478
        return $db->lookup('app_version', 'BoincAppVersion', $clause);
479
    }
480
    static function lookup_id($id) {
481
        $db = BoincDb::get();
482
        return $db->lookup_id($id, 'app_version', 'BoincAppVersion');
483
    }
484
    static function insert($clause) {
485
        $db = BoincDb::get();
486
        $ret = $db->insert('app_version', $clause);
487
        if (!$ret) return $ret;
488
        return $db->insert_id();
489
    }
490
    function update($clause) {
491
        $db = BoincDb::get();
492
        return $db->update($this, 'app_version', $clause);
493
    }
494
}
495
496
#[AllowDynamicProperties]
0 ignored issues
show
Coding Style introduced by
Perl-style comments are not allowed. Use "// Comment." or "/* comment */" instead.
Loading history...
497
class BoincProfile {
498
    static function lookup_fields($fields, $clause) {
499
        $db = BoincDb::get();
500
        return $db->lookup_fields('profile', 'BoincProfile', $fields, $clause);
501
    }
502
    static function lookup($clause) {
503
        $db = BoincDb::get();
504
        return $db->lookup('profile', 'BoincProfile', $clause);
505
    }
506
    static function lookup_userid($userid) {
507
        $db = BoincDb::get();
508
        return $db->lookup('profile', 'BoincProfile', 'userid='.$userid);
509
    }
510
    function update($clause) {
511
        $db = BoincDb::get();
512
        return $db->update_aux('profile', $clause.' where userid='.$this->userid);
0 ignored issues
show
Bug Best Practice introduced by
The property userid does not exist on BoincProfile. Did you maybe forget to declare it?
Loading history...
513
    }
514
    static function update_aux($clause) {
515
        $db = BoincDb::get();
516
        return $db->update_aux('profile', $clause);
517
    }
518
    static function insert($clause) {
519
        $db = BoincDb::get();
520
        return $db->insert('profile', $clause);
521
    }
522
    static function enum($where_clause=null, $order_clause=null) {
523
        $db = BoincDb::get();
524
        return $db->enum('profile', 'BoincProfile', $where_clause, $order_clause);
525
    }
526
    static function enum_fields($fields, $where_clause=null, $order_clause=null) {
527
        $db = BoincDb::get();
528
        return $db->enum_fields('profile', 'BoincProfile', $fields, $where_clause, $order_clause);
529
    }
530
    function delete() {
531
        $db = BoincDb::get();
532
        return $db->delete_aux('profile', 'userid='.$this->userid);
0 ignored issues
show
Bug Best Practice introduced by
The property userid does not exist on BoincProfile. Did you maybe forget to declare it?
Loading history...
533
    }
534
    static function delete_aux($clause) {
535
        $db = BoincDb::get();
536
        return $db->delete_aux('profile', $clause);
537
    }
538
}
539
540
#[AllowDynamicProperties]
0 ignored issues
show
Coding Style introduced by
Perl-style comments are not allowed. Use "// Comment." or "/* comment */" instead.
Loading history...
541
class BoincTeamAdmin {
542
    static function insert($clause) {
543
        $db = BoincDb::get();
544
        return $db->insert('team_admin', $clause);
545
    }
546
    static function enum($where_clause) {
547
        $db = BoincDb::get();
548
        return $db->enum('team_admin', 'BoincTeamAdmin', $where_clause);
549
    }
550
    static function delete($clause) {
551
        $db = BoincDb::get();
552
        return $db->delete_aux('team_admin', $clause);
553
    }
554
    static function lookup($teamid, $userid) {
555
        $db = BoincDb::get();
556
        return $db->lookup('team_admin', 'BoincTeamAdmin', "teamid=$teamid and userid=$userid");
557
    }
558
}
559
560
#[AllowDynamicProperties]
0 ignored issues
show
Coding Style introduced by
Perl-style comments are not allowed. Use "// Comment." or "/* comment */" instead.
Loading history...
561
class BoincPrivateMessage {
562
    static function lookup_id($id) {
563
        $db = BoincDb::get();
564
        return $db->lookup_id($id, 'private_messages', 'BoincPrivateMessage');
565
    }
566
    function update($clause) {
567
        $db = BoincDb::get();
568
        return $db->update($this, 'private_messages', $clause);
569
    }
570
    static function enum($where_clause) {
571
        $db = BoincDb::get();
572
        return $db->enum('private_messages', 'BoincPrivateMessage', $where_clause);
573
    }
574
    static function insert($clause) {
575
        $db = BoincDb::get();
576
        $ret = $db->insert('private_messages', $clause);
577
        if (!$ret) return $ret;
578
        return $db->insert_id();
579
    }
580
    static function count($clause) {
581
        $db = BoincDb::get();
582
        return $db->count('private_messages', $clause);
583
    }
584
    function delete() {
585
        $db = BoincDb::get();
586
        return $db->delete($this, 'private_messages');
587
    }
588
    static function delete_aux($clause) {
589
        $db = BoincDb::get();
590
        return $db->delete_aux('private_messages', $clause);
591
    }
592
}
593
594
#[AllowDynamicProperties]
0 ignored issues
show
Coding Style introduced by
Perl-style comments are not allowed. Use "// Comment." or "/* comment */" instead.
Loading history...
595
class BoincPlatform {
596
    static function enum($where_clause) {
597
        $db = BoincDb::get();
598
        return $db->enum('platform', 'BoincPlatform', $where_clause);
599
    }
600
    static function lookup_id($id) {
601
        $db = BoincDb::get();
602
        return $db->lookup_id($id, 'platform', 'BoincPlatform');
603
    }
604
    static function lookup($clause) {
605
        $db = BoincDb::get();
606
        return $db->lookup('platform', 'BoincPlatform', $clause);
607
    }
608
    function update($clause) {
609
        $db = BoincDb::get();
610
        return $db->update($this, 'platform', $clause);
611
    }
612
    static function insert($clause) {
613
        $db = BoincDb::get();
614
        return $db->insert('platform', $clause);
615
    }
616
}
617
618
#[AllowDynamicProperties]
0 ignored issues
show
Coding Style introduced by
Perl-style comments are not allowed. Use "// Comment." or "/* comment */" instead.
Loading history...
619
class BoincHostAppVersion {
620
    static function enum($where_clause) {
621
        $db = BoincDb::get();
622
        return $db->enum('host_app_version', 'BoincHostAppVersion', $where_clause);
623
    }
624
    static function lookup($host_id, $app_version_id) {
625
        $db = BoincDb::get();
626
        return $db->lookup(
627
            'host_app_version', 'BoincHostAppVersion',
628
            "host_id=$host_id and app_version_id=$app_version_id"
629
        );
630
    }
631
    static function update_aux($clause) {
632
        $db = BoincDb::get();
633
        return $db->update_aux('host_app_version', $clause);
634
    }
635
    static function delete_for_user($user_id) {
636
        $db = BoincDb::get();
637
        return $db->delete_aux('host_app_version', "host_id in (select id from host where userid = $user_id)");
638
    }
639
}
640
641
// DB utility functions
642
643
// return the "latest" app versions for a given app and platform
644
//
645
function latest_avs_app_platform($appid, $platformid) {
646
    $avs = BoincAppVersion::enum(
647
        "appid=$appid and platformid = $platformid and deprecated=0"
648
    );
649
    foreach ($avs as $av) {
650
        foreach ($avs as $av2) {
651
            if ($av->id == $av2->id) continue;
652
            if ($av->plan_class == $av2->plan_class && $av->beta == $av2->beta && $av->version_num > $av2->version_num) {
653
                $av2->deprecated = 1;
654
            }
655
        }
656
    }
657
    $r = array();
658
    foreach ($avs as $av) {
659
        if (!$av->deprecated) {
660
            $r[] = $av;
661
        }
662
    }
663
    return $r;
664
}
665
666
// return the "latest" app versions for a given app
667
//
668
function latest_avs_app($appid) {
669
    $platforms = BoincPlatform::enum("");
670
    $r = array();
671
    foreach ($platforms as $p) {
672
        $avs = latest_avs_app_platform($appid, $p->id);
673
        $r = array_merge($r, $avs);
674
    }
675
    return $r;
676
}
677
678
#[AllowDynamicProperties]
0 ignored issues
show
Coding Style introduced by
Perl-style comments are not allowed. Use "// Comment." or "/* comment */" instead.
Loading history...
679
class BoincBadge {
680
    static function enum($where_clause) {
681
        $db = BoincDb::get();
682
        return $db->enum('badge', 'BoincBadge', $where_clause);
683
    }
684
    static function insert($clause) {
685
        $db = BoincDb::get();
686
        $ret = $db->insert('badge', $clause);
687
        if (!$ret) return 0;
688
        return $db->insert_id();
689
    }
690
    function update($clause) {
691
        $db = BoincDb::get();
692
        return $db->update($this, 'badge', $clause);
693
    }
694
    static function lookup_id($id) {
695
        $db = BoincDb::get();
696
        return $db->lookup_id($id, 'badge', 'BoincBadge');
697
    }
698
    static function lookup($clause) {
699
        $db = BoincDb::get();
700
        return $db->lookup('badge', 'BoincBadge', $clause);
701
    }
702
    function delete() {
703
        $db = BoincDb::get();
704
        return $db->delete($this, 'badge');
705
    }
706
}
707
708
#[AllowDynamicProperties]
0 ignored issues
show
Coding Style introduced by
Perl-style comments are not allowed. Use "// Comment." or "/* comment */" instead.
Loading history...
709
class BoincBadgeUser {
710
    static function enum($where_clause) {
711
        $db = BoincDb::get();
712
        return $db->enum('badge_user', 'BoincBadgeUser', $where_clause);
713
    }
714
    static function insert($clause) {
715
        $db = BoincDb::get();
716
        $ret = $db->insert('badge_user', $clause);
717
        if (!$ret) return false;
718
        return true;
719
    }
720
    static function lookup($clause) {
721
        $db = BoincDb::get();
722
        return $db->lookup('badge_user', 'BoincBadgeUser', $clause);
723
    }
724
    static function update($clause) {
725
        $db = BoincDb::get();
726
        return $db->update_aux('badge_user', $clause);
727
    }
728
    static function delete($clause) {
729
        $db = BoincDb::get();
730
        $db->delete_aux('badge_user', $clause);
731
    }
732
    static function count($clause) {
733
        $db = BoincDb::get();
734
        return $db->count('badge_user', $clause);
735
    }
736
}
737
738
#[AllowDynamicProperties]
0 ignored issues
show
Coding Style introduced by
Perl-style comments are not allowed. Use "// Comment." or "/* comment */" instead.
Loading history...
739
class BoincBadgeTeam {
740
    static function enum($where_clause) {
741
        $db = BoincDb::get();
742
        return $db->enum('badge_team', 'BoincBadgeTeam', $where_clause);
743
    }
744
    static function insert($clause) {
745
        $db = BoincDb::get();
746
        $ret = $db->insert('badge_team', $clause);
747
        if (!$ret) return false;
748
        return true;
749
    }
750
    static function lookup($clause) {
751
        $db = BoincDb::get();
752
        return $db->lookup('badge_team', 'BoincBadgeTeam', $clause);
753
    }
754
    static function update($clause) {
755
        $db = BoincDb::get();
756
        return $db->update_aux('badge_team', $clause);
757
    }
758
    static function delete($clause) {
759
        $db = BoincDb::get();
760
        $db->delete_aux('badge_team', $clause);
761
    }
762
    static function count($clause) {
763
        $db = BoincDb::get();
764
        return $db->count('badge_team', $clause);
765
    }
766
}
767
768
#[AllowDynamicProperties]
0 ignored issues
show
Coding Style introduced by
Perl-style comments are not allowed. Use "// Comment." or "/* comment */" instead.
Loading history...
769
class BoincCreditUser {
770
    static function lookup($clause) {
771
        $db = BoincDb::get();
772
        return $db->lookup('credit_user', 'BoincCreditUser', $clause);
773
    }
774
    static function enum($where_clause) {
775
        $db = BoincDb::get();
776
        return $db->enum('credit_user', 'BoincCreditUser', $where_clause);
777
    }
778
    static function sum($field, $clause) {
779
        $db = BoincDb::get();
780
        return $db->sum('credit_user', $field, $clause);
781
    }
782
    static function update($clause) {
783
        $db = BoincDb::get();
784
        return $db->update_aux('credit_user', $clause);
785
    }
786
    static function delete_user($user) {
787
        $db = BoincDb::get();
788
        $db->delete_aux('credit_user', "userid=$user->id");
789
    }
790
    static function get_list($where_clause, $order_clause, $limit) {
791
        $db = BoincDB::get();
792
        return $db->get_list('user', 'credit_user', 'id', 'userid', 'BoincCreditUser', '*', $where_clause, $order_clause, $limit);
793
    }
794
}
795
796
#[AllowDynamicProperties]
0 ignored issues
show
Coding Style introduced by
Perl-style comments are not allowed. Use "// Comment." or "/* comment */" instead.
Loading history...
797
class BoincCreditTeam {
798
    static function lookup($clause) {
799
        $db = BoincDb::get();
800
        return $db->lookup('credit_team', 'BoincCreditTeam', $clause);
801
    }
802
    static function enum($where_clause) {
803
        $db = BoincDb::get();
804
        return $db->enum('credit_team', 'BoincCreditTeam', $where_clause);
805
    }
806
    static function sum($field, $clause) {
807
        $db = BoincDb::get();
808
        return $db->sum('credit_team', $field, $clause);
809
    }
810
    static function update($clause) {
811
        $db = BoincDb::get();
812
        return $db->update_aux('credit_team', $clause);
813
    }
814
    static function get_list($where_clause, $order_clause, $limit) {
815
        $db = BoincDB::get();
816
        return $db->get_list('team', 'credit_team', 'id', 'teamid', 'BoincCreditTeam', '*', $where_clause, $order_clause, $limit);
817
    }
818
}
819
820
#[AllowDynamicProperties]
0 ignored issues
show
Coding Style introduced by
Perl-style comments are not allowed. Use "// Comment." or "/* comment */" instead.
Loading history...
821
class BoincToken {
822
823
    static function lookup($clause) {
824
        $db = BoincDb::get();
825
        return $db->lookup('token', 'BoincToken', $clause);
826
    }
827
828
    static function lookup_valid_token($userid, $token, $type) {
829
        $db = BoincDb::get();
830
        $token = BoincDb::escape_string($token);
831
        $type = BoincDb::escape_string($type);
832
        $now = time();
833
        return self::lookup("userid=$userid and token='$token' and expire_time > $now and type = '$type'");
834
    }
835
836
    static function enum($where_clause) {
837
        $db = BoincDb::get();
838
        return $db->enum('token', 'BoincToken', $where_clause);
839
    }
840
841
    static function insert($clause) {
842
        $db = BoincDb::get();
843
        return $db->insert('token', $clause);
844
    }
845
846
    static function get_list($where_clause, $order_clause, $limit) {
847
        $db = BoincDB::get();
848
        return $db->get_list('token', 'userid', 'type', 'create_time', 'expire_time', 'BoincToken', '*', $where_clause, $order_clause, $limit);
849
    }
850
851
    static function delete_token($where_clause) {
852
        $db = BoincDb::get();
853
        $db->delete_aux('token', $where_clause);
854
        return $db->affected_rows();
855
    }
856
857
    static function delete_expired() {
858
        $db = BoincDb::get();
859
        $now = time();
860
        $db->delete_aux('token', "expire_time < $now");
861
        return $db->affected_rows();
862
    }
863
864
    static function delete_for_user($user_id) {
865
        $db = BoincDb::get();
866
        $db->delete_aux('token', "userid=$user_id");
867
        return $db->affected_rows();
868
    }
869
870
}
871
872
#[AllowDynamicProperties]
0 ignored issues
show
Coding Style introduced by
Perl-style comments are not allowed. Use "// Comment." or "/* comment */" instead.
Loading history...
873
class BoincUserDeleted {
874
    static function insert_user($user) {
875
        $now = time();
876
        $cpid = md5($user->cross_project_id.$user->email_addr);
877
        $clause = "(userid, public_cross_project_id, create_time) values ($user->id, '$cpid', $now)";
878
        $db = BoincDb::get();
879
        return $db->insert('user_deleted', $clause);
880
    }
881
882
    static function delete_expired() {
883
        $db = BoincDb::get();
884
        $expire_time = time() - 60*86400; //60 days ago
885
        $db->delete_aux('user_deleted', "create_time < $expire_time");
886
        return $db->affected_rows();
887
    }
888
889
}
890
891
#[AllowDynamicProperties]
0 ignored issues
show
Coding Style introduced by
Perl-style comments are not allowed. Use "// Comment." or "/* comment */" instead.
Loading history...
892
class BoincHostDeleted {
893
    static function insert_hosts_for_user($user) {
894
        $now = time();
895
        $clause = "select id, host_cpid, $now from host where userid = $user->id";
896
        $db = BoincDb::get();
897
        return $db->insert('host_deleted', $clause);
898
    }
899
900
    static function delete_expired() {
901
        $db = BoincDb::get();
902
        $expire_time = time() - 60*86400; //60 days ago
903
        $db->delete_aux('host_deleted', "create_time < $expire_time");
904
        return $db->affected_rows();
905
    }
906
907
}
908
909
#[AllowDynamicProperties]
0 ignored issues
show
Coding Style introduced by
Perl-style comments are not allowed. Use "// Comment." or "/* comment */" instead.
Loading history...
910
class BoincConsent {
911
    static function lookup($clause) {
912
        $db = BoincDb::get();
913
        return $db->lookup('consent', 'BoincConsent', $clause);
914
    }
915
916
    static function enum($where_clause) {
917
        $db = BoincDb::get();
918
        return $db->enum('consent', 'BoincConsent', $where_clause);
919
    }
920
921
    static function insert ($clause) {
0 ignored issues
show
Coding Style introduced by
Expected "function abc(...)"; found "function abc (...)"
Loading history...
Coding Style introduced by
Expected 0 spaces before opening parenthesis; 1 found
Loading history...
922
        $db = BoincDb::get();
923
        return $db->insert('consent', $clause);
924
    }
925
926
    static function update ($clause) {
0 ignored issues
show
Coding Style introduced by
Expected "function abc(...)"; found "function abc (...)"
Loading history...
Coding Style introduced by
Expected 0 spaces before opening parenthesis; 1 found
Loading history...
927
        $db = BoincDb::get();
928
        return $db->update_aux('consent', $clause);
929
    }
930
931
    static function delete($clause) {
932
        $db = BoincDb::get();
933
        return $db->delete_aux('consent', $clause);
934
    }
935
936
    static function delete_for_user($user_id) {
937
        $db = BoincDb::get();
938
        $db->delete_aux('consent', "userid=$user_id");
939
        return $db->affected_rows();
940
    }
941
942
}
943
944
#[AllowDynamicProperties]
0 ignored issues
show
Coding Style introduced by
Perl-style comments are not allowed. Use "// Comment." or "/* comment */" instead.
Loading history...
945
class BoincConsentType {
946
    static function lookup($clause) {
947
        $db = BoincDb::get();
948
        return $db->lookup('consent_type', 'BoincConsentType', $clause);
949
    }
950
951
    static function enum($where_clause, $order_clause=null) {
952
        $db = BoincDb::get();
953
        return $db->enum('consent_type', 'BoincConsentType', $where_clause, $order_clause);
954
    }
955
956
    static function insert ($clause) {
0 ignored issues
show
Coding Style introduced by
Expected "function abc(...)"; found "function abc (...)"
Loading history...
Coding Style introduced by
Expected 0 spaces before opening parenthesis; 1 found
Loading history...
957
        $db = BoincDb::get();
958
        return $db->insert('consent_type', $clause);
959
    }
960
961
    static function update ($clause) {
0 ignored issues
show
Coding Style introduced by
Expected "function abc(...)"; found "function abc (...)"
Loading history...
Coding Style introduced by
Expected 0 spaces before opening parenthesis; 1 found
Loading history...
962
        $db = BoincDb::get();
963
        return $db->update_aux('consent_type', $clause);
964
    }
965
966
    function delete() {
967
        $db = BoincDb::get();
968
        return $db->delete($this, 'consent_type');
969
    }
970
971
    function delete_aux($clause) {
972
        $db = BoincDb::get();
973
        return $db->delete_aux('consent_type', $clause);
974
    }
975
976
}
977
978
// Class to interface with SQL View latest_consent. Only read
979
// operations permitted.
980
#[AllowDynamicProperties]
0 ignored issues
show
Coding Style introduced by
Perl-style comments are not allowed. Use "// Comment." or "/* comment */" instead.
Loading history...
981
class BoincLatestConsent {
982
    static function lookup($clause) {
983
        $db = BoincDb::get();
984
        return $db->lookup('latest_consent', 'BoincLatestConsent', $clause);
985
    }
986
987
    static function enum($where_clause, $order_clause=null) {
988
        $db = BoincDb::get();
989
        return $db->enum('latest_consent', 'BoincLatestConsent', $where_clause, $order_clause);
990
    }
991
}
992
993
// DEPRECATED: use BoincDb::escape_string where possible
994
//
995
// apply this to any user-supplied strings used in queries
996
//
997
function boinc_real_escape_string($x) {
998
    if (version_compare(phpversion(),"4.3.0")>=0) {
999
        return BoincDb::escape_string($x);
1000
    } else {
1001
        $x = str_replace("'", "\'", $x);
1002
        $x = str_replace("\"", "\\\"", $x);
1003
        return $x;
1004
    }
1005
}
1006
1007
?>
1008