Passed
Pull Request — master (#5697)
by David
09:34
created

BoincDb   A

Complexity

Total Complexity 26

Size/Duplication

Total Lines 106
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 58
c 1
b 0
f 0
dl 0
loc 106
rs 10
wmc 26

6 Methods

Rating   Name   Duplication   Size   Complexity  
C get_aux() 0 38 13
A close() 0 4 2
B get() 0 23 7
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
            if (self::$dbnum == $dbnum) {
109
                return self::$instance;
110
            }
111
            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) {
366
        $db = BoincDb::get();
367
		return $db->enum_fields('result', 'BoincResult', $fields, $where_clause, $order_clause);
368
	}
369
    function update($clause) {
370
        $db = BoincDb::get();
371
        return $db->update($this, 'result', $clause);
372
    }
373
    static function update_aux($clause) {
374
        $db = BoincDb::get();
375
        return $db->update_aux('result', $clause);
376
    }
377
    static function lookup_id($id) {
378
        $db = BoincDb::get();
379
        return $db->lookup_id($id, 'result', 'BoincResult');
380
    }
381
    static function lookup_name($name) {
382
        $db = BoincDb::get();
383
        $name = BoincDb::escape_string($name);
384
        return $db->lookup('result', 'BoincResult', "name='$name'");
385
    }
386
    function delete() {
387
        $db = BoincDb::get();
388
        return $db->delete($this, 'result');
389
    }
390
}
391
392
#[AllowDynamicProperties]
0 ignored issues
show
Coding Style introduced by
Perl-style comments are not allowed. Use "// Comment." or "/* comment */" instead.
Loading history...
393
class BoincWorkunit {
394
    static function lookup_id($id) {
395
        $db = BoincDb::get();
396
        return $db->lookup_id($id, 'workunit', 'BoincWorkunit');
397
    }
398
    static function lookup($clause) {
399
        $db = BoincDb::get();
400
        return $db->lookup('workunit', 'BoincWorkunit', $clause);
401
    }
402
    static function insert($clause) {
403
        $db = BoincDb::get();
404
        $ret = $db->insert('workunit', $clause);
405
        if (!$ret) return $ret;
406
        return $db->insert_id();
407
    }
408
    static function enum($where_clause) {
409
        $db = BoincDb::get();
410
        return $db->enum('workunit', 'BoincWorkunit', $where_clause);
411
    }
412
    function update($clause) {
413
        $db = BoincDb::get();
414
        return $db->update($this, 'workunit', $clause);
415
    }
416
    static function update_aux($clause) {
417
        $db = BoincDb::get();
418
        return $db->update_aux('workunit', $clause);
419
    }
420
    static function count($clause) {
421
        $db = BoincDb::get();
422
        return $db->count('workunit', $clause);
423
    }
424
}
425
426
#[AllowDynamicProperties]
0 ignored issues
show
Coding Style introduced by
Perl-style comments are not allowed. Use "// Comment." or "/* comment */" instead.
Loading history...
427
class BoincApp {
428
    static function lookup_id($id) {
429
        $db = BoincDb::get();
430
        return $db->lookup_id($id, 'app', 'BoincApp');
431
    }
432
    static function lookup($clause) {
433
        $db = BoincDb::get();
434
        return $db->lookup('app', 'BoincApp', $clause);
435
    }
436
    static function enum($where_clause) {
437
        $db = BoincDb::get();
438
        return $db->enum('app', 'BoincApp', $where_clause);
439
    }
440
    static function insert($clause) {
441
        $db = BoincDb::get();
442
        $ret = $db->insert('app', $clause);
443
        if (!$ret) return $ret;
444
        return $db->insert_id();
445
    }
446
    function update($clause) {
447
        $db = BoincDb::get();
448
        return $db->update($this, 'app', $clause);
449
    }
450
    static function sum($field, $clause=null) {
451
        $db = BoincDb::get();
452
        return $db->sum('app', $field, $clause);
453
    }
454
}
455
456
#[AllowDynamicProperties]
0 ignored issues
show
Coding Style introduced by
Perl-style comments are not allowed. Use "// Comment." or "/* comment */" instead.
Loading history...
457
class BoincAppVersion {
458
    static function enum($where_clause) {
459
        $db = BoincDb::get();
460
        return $db->enum('app_version', 'BoincAppVersion', $where_clause);
461
    }
462
    static function lookup($clause) {
463
        $db = BoincDb::get();
464
        return $db->lookup('app_version', 'BoincAppVersion', $clause);
465
    }
466
    static function lookup_id($id) {
467
        $db = BoincDb::get();
468
        return $db->lookup_id($id, 'app_version', 'BoincAppVersion');
469
    }
470
    static function insert($clause) {
471
        $db = BoincDb::get();
472
        $ret = $db->insert('app_version', $clause);
473
        if (!$ret) return $ret;
474
        return $db->insert_id();
475
    }
476
    function update($clause) {
477
        $db = BoincDb::get();
478
        return $db->update($this, 'app_version', $clause);
479
    }
480
}
481
482
#[AllowDynamicProperties]
0 ignored issues
show
Coding Style introduced by
Perl-style comments are not allowed. Use "// Comment." or "/* comment */" instead.
Loading history...
483
class BoincProfile {
484
    static function lookup_fields($fields, $clause) {
485
        $db = BoincDb::get();
486
        return $db->lookup_fields('profile', 'BoincProfile', $fields, $clause);
487
    }
488
    static function lookup($clause) {
489
        $db = BoincDb::get();
490
        return $db->lookup('profile', 'BoincProfile', $clause);
491
    }
492
    static function lookup_userid($userid) {
493
        $db = BoincDb::get();
494
        return $db->lookup('profile', 'BoincProfile', 'userid='.$userid);
495
    }
496
    function update($clause) {
497
        $db = BoincDb::get();
498
        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...
499
    }
500
    static function update_aux($clause) {
501
        $db = BoincDb::get();
502
        return $db->update_aux('profile', $clause);
503
    }
504
    static function insert($clause) {
505
        $db = BoincDb::get();
506
        return $db->insert('profile', $clause);
507
    }
508
    static function enum($where_clause=null, $order_clause=null) {
509
        $db = BoincDb::get();
510
        return $db->enum('profile', 'BoincProfile', $where_clause, $order_clause);
511
    }
512
    static function enum_fields($fields, $where_clause=null, $order_clause=null) {
513
        $db = BoincDb::get();
514
        return $db->enum_fields('profile', 'BoincProfile', $fields, $where_clause, $order_clause);
515
    }
516
    function delete() {
517
        $db = BoincDb::get();
518
        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...
519
    }
520
    static function delete_aux($clause) {
521
        $db = BoincDb::get();
522
        return $db->delete_aux('profile', $clause);
523
    }
524
}
525
526
#[AllowDynamicProperties]
0 ignored issues
show
Coding Style introduced by
Perl-style comments are not allowed. Use "// Comment." or "/* comment */" instead.
Loading history...
527
class BoincTeamAdmin {
528
    static function insert($clause) {
529
        $db = BoincDb::get();
530
        return $db->insert('team_admin', $clause);
531
    }
532
    static function enum($where_clause) {
533
        $db = BoincDb::get();
534
        return $db->enum('team_admin', 'BoincTeamAdmin', $where_clause);
535
    }
536
    static function delete($clause) {
537
        $db = BoincDb::get();
538
        return $db->delete_aux('team_admin', $clause);
539
    }
540
    static function lookup($teamid, $userid) {
541
        $db = BoincDb::get();
542
        return $db->lookup('team_admin', 'BoincTeamAdmin', "teamid=$teamid and userid=$userid");
543
    }
544
}
545
546
#[AllowDynamicProperties]
0 ignored issues
show
Coding Style introduced by
Perl-style comments are not allowed. Use "// Comment." or "/* comment */" instead.
Loading history...
547
class BoincPrivateMessage {
548
    static function lookup_id($id) {
549
        $db = BoincDb::get();
550
        return $db->lookup_id($id, 'private_messages', 'BoincPrivateMessage');
551
    }
552
    function update($clause) {
553
        $db = BoincDb::get();
554
        return $db->update($this, 'private_messages', $clause);
555
    }
556
    static function enum($where_clause) {
557
        $db = BoincDb::get();
558
        return $db->enum('private_messages', 'BoincPrivateMessage', $where_clause);
559
    }
560
    static function insert($clause) {
561
        $db = BoincDb::get();
562
        $ret = $db->insert('private_messages', $clause);
563
        if (!$ret) return $ret;
564
        return $db->insert_id();
565
    }
566
    static function count($clause) {
567
        $db = BoincDb::get();
568
        return $db->count('private_messages', $clause);
569
    }
570
    function delete() {
571
        $db = BoincDb::get();
572
        return $db->delete($this, 'private_messages');
573
    }
574
    static function delete_aux($clause) {
575
        $db = BoincDb::get();
576
        return $db->delete_aux('private_messages', $clause);
577
    }
578
}
579
580
#[AllowDynamicProperties]
0 ignored issues
show
Coding Style introduced by
Perl-style comments are not allowed. Use "// Comment." or "/* comment */" instead.
Loading history...
581
class BoincPlatform {
582
    static function enum($where_clause) {
583
        $db = BoincDb::get();
584
        return $db->enum('platform', 'BoincPlatform', $where_clause);
585
    }
586
    static function lookup_id($id) {
587
        $db = BoincDb::get();
588
        return $db->lookup_id($id, 'platform', 'BoincPlatform');
589
    }
590
    static function lookup($clause) {
591
        $db = BoincDb::get();
592
        return $db->lookup('platform', 'BoincPlatform', $clause);
593
    }
594
    function update($clause) {
595
        $db = BoincDb::get();
596
        return $db->update($this, 'platform', $clause);
597
    }
598
    static function insert($clause) {
599
        $db = BoincDb::get();
600
        return $db->insert('platform', $clause);
601
    }
602
}
603
604
#[AllowDynamicProperties]
0 ignored issues
show
Coding Style introduced by
Perl-style comments are not allowed. Use "// Comment." or "/* comment */" instead.
Loading history...
605
class BoincHostAppVersion {
606
    static function enum($where_clause) {
607
        $db = BoincDb::get();
608
        return $db->enum('host_app_version', 'BoincHostAppVersion', $where_clause);
609
    }
610
    static function lookup($host_id, $app_version_id) {
611
        $db = BoincDb::get();
612
        return $db->lookup(
613
            'host_app_version', 'BoincHostAppVersion',
614
            "host_id=$host_id and app_version_id=$app_version_id"
615
        );
616
    }
617
    static function update_aux($clause) {
618
        $db = BoincDb::get();
619
        return $db->update_aux('host_app_version', $clause);
620
    }
621
    static function delete_for_user($user_id) {
622
        $db = BoincDb::get();
623
        return $db->delete_aux('host_app_version', "host_id in (select id from host where userid = $user_id)");
624
    }
625
}
626
627
// DB utility functions
628
629
// return the "latest" app versions for a given app and platform
630
//
631
function latest_avs_app_platform($appid, $platformid) {
632
    $avs = BoincAppVersion::enum(
633
        "appid=$appid and platformid = $platformid and deprecated=0"
634
    );
635
    foreach ($avs as $av) {
636
        foreach ($avs as $av2) {
637
            if ($av->id == $av2->id) continue;
638
            if ($av->plan_class == $av2->plan_class && $av->beta == $av2->beta && $av->version_num > $av2->version_num) {
639
                $av2->deprecated = 1;
640
            }
641
        }
642
    }
643
    $r = array();
644
    foreach ($avs as $av) {
645
        if (!$av->deprecated) {
646
            $r[] = $av;
647
        }
648
    }
649
    return $r;
650
}
651
652
// return the "latest" app versions for a given app
653
//
654
function latest_avs_app($appid) {
655
    $platforms = BoincPlatform::enum("");
656
    $r = array();
657
    foreach ($platforms as $p) {
658
        $avs = latest_avs_app_platform($appid, $p->id);
659
        $r = array_merge($r, $avs);
660
    }
661
    return $r;
662
}
663
664
#[AllowDynamicProperties]
0 ignored issues
show
Coding Style introduced by
Perl-style comments are not allowed. Use "// Comment." or "/* comment */" instead.
Loading history...
665
class BoincBadge {
666
    static function enum($where_clause) {
667
        $db = BoincDb::get();
668
        return $db->enum('badge', 'BoincBadge', $where_clause);
669
    }
670
    static function insert($clause) {
671
        $db = BoincDb::get();
672
        $ret = $db->insert('badge', $clause);
673
        if (!$ret) return 0;
674
        return $db->insert_id();
675
    }
676
    function update($clause) {
677
        $db = BoincDb::get();
678
        return $db->update($this, 'badge', $clause);
679
    }
680
    static function lookup_id($id) {
681
        $db = BoincDb::get();
682
        return $db->lookup_id($id, 'badge', 'BoincBadge');
683
    }
684
    static function lookup($clause) {
685
        $db = BoincDb::get();
686
        return $db->lookup('badge', 'BoincBadge', $clause);
687
    }
688
    function delete() {
689
        $db = BoincDb::get();
690
        return $db->delete($this, 'badge');
691
    }
692
}
693
694
#[AllowDynamicProperties]
0 ignored issues
show
Coding Style introduced by
Perl-style comments are not allowed. Use "// Comment." or "/* comment */" instead.
Loading history...
695
class BoincBadgeUser {
696
    static function enum($where_clause) {
697
        $db = BoincDb::get();
698
        return $db->enum('badge_user', 'BoincBadgeUser', $where_clause);
699
    }
700
    static function insert($clause) {
701
        $db = BoincDb::get();
702
        $ret = $db->insert('badge_user', $clause);
703
        if (!$ret) return false;
704
        return true;
705
    }
706
    static function lookup($clause) {
707
        $db = BoincDb::get();
708
        return $db->lookup('badge_user', 'BoincBadgeUser', $clause);
709
    }
710
    static function update($clause) {
711
        $db = BoincDb::get();
712
        return $db->update_aux('badge_user', $clause);
713
    }
714
    static function delete($clause) {
715
        $db = BoincDb::get();
716
        $db->delete_aux('badge_user', $clause);
717
    }
718
    static function count($clause) {
719
        $db = BoincDb::get();
720
        return $db->count('badge_user', $clause);
721
    }
722
}
723
724
#[AllowDynamicProperties]
0 ignored issues
show
Coding Style introduced by
Perl-style comments are not allowed. Use "// Comment." or "/* comment */" instead.
Loading history...
725
class BoincBadgeTeam {
726
    static function enum($where_clause) {
727
        $db = BoincDb::get();
728
        return $db->enum('badge_team', 'BoincBadgeTeam', $where_clause);
729
    }
730
    static function insert($clause) {
731
        $db = BoincDb::get();
732
        $ret = $db->insert('badge_team', $clause);
733
        if (!$ret) return false;
734
        return true;
735
    }
736
    static function lookup($clause) {
737
        $db = BoincDb::get();
738
        return $db->lookup('badge_team', 'BoincBadgeTeam', $clause);
739
    }
740
    static function update($clause) {
741
        $db = BoincDb::get();
742
        return $db->update_aux('badge_team', $clause);
743
    }
744
    static function delete($clause) {
745
        $db = BoincDb::get();
746
        $db->delete_aux('badge_team', $clause);
747
    }
748
    static function count($clause) {
749
        $db = BoincDb::get();
750
        return $db->count('badge_team', $clause);
751
    }
752
}
753
754
#[AllowDynamicProperties]
0 ignored issues
show
Coding Style introduced by
Perl-style comments are not allowed. Use "// Comment." or "/* comment */" instead.
Loading history...
755
class BoincCreditUser {
756
    static function lookup($clause) {
757
        $db = BoincDb::get();
758
        return $db->lookup('credit_user', 'BoincCreditUser', $clause);
759
    }
760
    static function enum($where_clause) {
761
        $db = BoincDb::get();
762
        return $db->enum('credit_user', 'BoincCreditUser', $where_clause);
763
    }
764
    static function sum($field, $clause) {
765
        $db = BoincDb::get();
766
        return $db->sum('credit_user', $field, $clause);
767
    }
768
    static function update($clause) {
769
        $db = BoincDb::get();
770
        return $db->update_aux('credit_user', $clause);
771
    }
772
    static function delete_user($user) {
773
        $db = BoincDb::get();
774
        $db->delete_aux('credit_user', "userid=$user->id");
775
    }
776
    static function get_list($where_clause, $order_clause, $limit) {
777
        $db = BoincDB::get();
778
        return $db->get_list('user', 'credit_user', 'id', 'userid', 'BoincCreditUser', '*', $where_clause, $order_clause, $limit);
779
    }
780
}
781
782
#[AllowDynamicProperties]
0 ignored issues
show
Coding Style introduced by
Perl-style comments are not allowed. Use "// Comment." or "/* comment */" instead.
Loading history...
783
class BoincCreditTeam {
784
    static function lookup($clause) {
785
        $db = BoincDb::get();
786
        return $db->lookup('credit_team', 'BoincCreditTeam', $clause);
787
    }
788
    static function enum($where_clause) {
789
        $db = BoincDb::get();
790
        return $db->enum('credit_team', 'BoincCreditTeam', $where_clause);
791
    }
792
    static function sum($field, $clause) {
793
        $db = BoincDb::get();
794
        return $db->sum('credit_team', $field, $clause);
795
    }
796
    static function update($clause) {
797
        $db = BoincDb::get();
798
        return $db->update_aux('credit_team', $clause);
799
    }
800
    static function get_list($where_clause, $order_clause, $limit) {
801
        $db = BoincDB::get();
802
        return $db->get_list('team', 'credit_team', 'id', 'teamid', 'BoincCreditTeam', '*', $where_clause, $order_clause, $limit);
803
    }
804
}
805
806
#[AllowDynamicProperties]
0 ignored issues
show
Coding Style introduced by
Perl-style comments are not allowed. Use "// Comment." or "/* comment */" instead.
Loading history...
807
class BoincToken {
808
809
    static function lookup($clause) {
810
        $db = BoincDb::get();
811
        return $db->lookup('token', 'BoincToken', $clause);
812
    }
813
814
    static function lookup_valid_token($userid, $token, $type) {
815
        $db = BoincDb::get();
816
        $token = BoincDb::escape_string($token);
817
        $type = BoincDb::escape_string($type);
818
        $now = time();
819
        return self::lookup("userid=$userid and token='$token' and expire_time > $now and type = '$type'");
820
    }
821
822
    static function enum($where_clause) {
823
        $db = BoincDb::get();
824
        return $db->enum('token', 'BoincToken', $where_clause);
825
    }
826
827
    static function insert($clause) {
828
        $db = BoincDb::get();
829
        return $db->insert('token', $clause);
830
    }
831
832
    static function get_list($where_clause, $order_clause, $limit) {
833
        $db = BoincDB::get();
834
        return $db->get_list('token', 'userid', 'type', 'create_time', 'expire_time', 'BoincToken', '*', $where_clause, $order_clause, $limit);
835
    }
836
837
    static function delete_token($where_clause) {
838
        $db = BoincDb::get();
839
        $db->delete_aux('token', $where_clause);
840
        return $db->affected_rows();
841
    }
842
843
    static function delete_expired() {
844
        $db = BoincDb::get();
845
        $now = time();
846
        $db->delete_aux('token', "expire_time < $now");
847
        return $db->affected_rows();
848
    }
849
850
    static function delete_for_user($user_id) {
851
        $db = BoincDb::get();
852
        $db->delete_aux('token', "userid=$user_id");
853
        return $db->affected_rows();
854
    }
855
856
}
857
858
#[AllowDynamicProperties]
0 ignored issues
show
Coding Style introduced by
Perl-style comments are not allowed. Use "// Comment." or "/* comment */" instead.
Loading history...
859
class BoincUserDeleted {
860
    static function insert_user($user) {
861
        $now = time();
862
        $cpid = md5($user->cross_project_id.$user->email_addr);
863
        $clause = "(userid, public_cross_project_id, create_time) values ($user->id, '$cpid', $now)";
864
        $db = BoincDb::get();
865
        return $db->insert('user_deleted', $clause);
866
    }
867
868
    static function delete_expired() {
869
        $db = BoincDb::get();
870
        $expire_time = time() - 60*86400; //60 days ago
871
        $db->delete_aux('user_deleted', "create_time < $expire_time");
872
        return $db->affected_rows();
873
    }
874
875
}
876
877
#[AllowDynamicProperties]
0 ignored issues
show
Coding Style introduced by
Perl-style comments are not allowed. Use "// Comment." or "/* comment */" instead.
Loading history...
878
class BoincHostDeleted {
879
    static function insert_hosts_for_user($user) {
880
        $now = time();
881
        $clause = "select id, host_cpid, $now from host where userid = $user->id";
882
        $db = BoincDb::get();
883
        return $db->insert('host_deleted', $clause);
884
    }
885
886
    static function delete_expired() {
887
        $db = BoincDb::get();
888
        $expire_time = time() - 60*86400; //60 days ago
889
        $db->delete_aux('host_deleted', "create_time < $expire_time");
890
        return $db->affected_rows();
891
    }
892
893
}
894
895
#[AllowDynamicProperties]
0 ignored issues
show
Coding Style introduced by
Perl-style comments are not allowed. Use "// Comment." or "/* comment */" instead.
Loading history...
896
class BoincConsent {
897
    static function lookup($clause) {
898
        $db = BoincDb::get();
899
        return $db->lookup('consent', 'BoincConsent', $clause);
900
    }
901
902
    static function enum($where_clause) {
903
        $db = BoincDb::get();
904
        return $db->enum('consent', 'BoincConsent', $where_clause);
905
    }
906
907
    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...
908
        $db = BoincDb::get();
909
        return $db->insert('consent', $clause);
910
    }
911
912
    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...
913
        $db = BoincDb::get();
914
        return $db->update_aux('consent', $clause);
915
    }
916
917
    static function delete($clause) {
918
        $db = BoincDb::get();
919
        return $db->delete_aux('consent', $clause);
920
    }
921
922
    static function delete_for_user($user_id) {
923
        $db = BoincDb::get();
924
        $db->delete_aux('consent', "userid=$user_id");
925
        return $db->affected_rows();
926
    }
927
928
}
929
930
#[AllowDynamicProperties]
0 ignored issues
show
Coding Style introduced by
Perl-style comments are not allowed. Use "// Comment." or "/* comment */" instead.
Loading history...
931
class BoincConsentType {
932
    static function lookup($clause) {
933
        $db = BoincDb::get();
934
        return $db->lookup('consent_type', 'BoincConsentType', $clause);
935
    }
936
937
    static function enum($where_clause, $order_clause=null) {
938
        $db = BoincDb::get();
939
        return $db->enum('consent_type', 'BoincConsentType', $where_clause, $order_clause);
940
    }
941
942
    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...
943
        $db = BoincDb::get();
944
        return $db->insert('consent_type', $clause);
945
    }
946
947
    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...
948
        $db = BoincDb::get();
949
        return $db->update_aux('consent_type', $clause);
950
    }
951
952
    function delete() {
953
        $db = BoincDb::get();
954
        return $db->delete($this, 'consent_type');
955
    }
956
957
    function delete_aux($clause) {
958
        $db = BoincDb::get();
959
        return $db->delete_aux('consent_type', $clause);
960
    }
961
962
}
963
964
// Class to interface with SQL View latest_consent. Only read
965
// operations permitted.
966
#[AllowDynamicProperties]
0 ignored issues
show
Coding Style introduced by
Perl-style comments are not allowed. Use "// Comment." or "/* comment */" instead.
Loading history...
967
class BoincLatestConsent {
968
    static function lookup($clause) {
969
        $db = BoincDb::get();
970
        return $db->lookup('latest_consent', 'BoincLatestConsent', $clause);
971
    }
972
973
    static function enum($where_clause, $order_clause=null) {
974
        $db = BoincDb::get();
975
        return $db->enum('latest_consent', 'BoincLatestConsent', $where_clause, $order_clause);
976
    }
977
}
978
979
// DEPRECATED: use BoincDb::escape_string where possible
980
//
981
// apply this to any user-supplied strings used in queries
982
//
983
function boinc_real_escape_string($x) {
984
    if (version_compare(phpversion(),"4.3.0")>=0) {
985
        return BoincDb::escape_string($x);
986
    } else {
987
        $x = str_replace("'", "\'", $x);
988
        $x = str_replace("\"", "\\\"", $x);
989
        return $x;
990
    }
991
}
992
993
?>
994