Passed
Pull Request — master (#6643)
by David
15:30 queued 06:17
created

BoincHost::insert()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

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