Passed
Pull Request — master (#2504)
by
unknown
13:40
created

BoincConsentType::lookup()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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