Passed
Pull Request — master (#5695)
by
unknown
11:12
created

BoincDb::get()   B

Complexity

Conditions 7
Paths 10

Size

Total Lines 21
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

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