BoincHost::enum()   A
last analyzed

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