Passed
Pull Request — master (#5697)
by David
10:07
created

BoincDb   A

Complexity

Total Complexity 25

Size/Duplication

Total Lines 103
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 56
c 1
b 0
f 0
dl 0
loc 103
rs 10
wmc 25

6 Methods

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