Passed
Pull Request — master (#5697)
by David
16:47
created

BoincDb::get_aux()   C

Complexity

Conditions 14
Paths 29

Size

Total Lines 40
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 14
eloc 28
nc 29
nop 1
dl 0
loc 40
rs 6.2666
c 0
b 0
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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