Completed
Pull Request — master (#2472)
by Kevin
23:34 queued 05:01
created

BoincHostDeleted   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 17
Duplicated Lines 70.59 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
dl 12
loc 17
rs 10
c 0
b 0
f 0
wmc 2
lcom 0
cbo 1

2 Methods

Rating   Name   Duplication   Size   Complexity  
A insert_hosts_for_user() 6 6 1
A delete_expired() 6 6 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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)");
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)");
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)");
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)");
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)");
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)");
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)");
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 View Code Duplication
    static function insert($clause) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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 introduced by
The property id does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
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 View Code Duplication
    static function insert($clause) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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();
0 ignored issues
show
Unused Code introduced by
$db is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
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 View Code Duplication
    static function insert($clause) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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 View Code Duplication
class BoincResult {
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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 View Code Duplication
    static function insert($clause) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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 View Code Duplication
class BoincAppVersion {
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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 introduced by
The property userid does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
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);
515
    }
516
    static function delete_aux($clause) {
517
        $db = BoincDb::get();
518
        return $db->delete_aux('profile', $clause);
519
    }
520
}
521
522 View Code Duplication
class BoincTeamAdmin {
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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 View Code Duplication
class BoincHostAppVersion {
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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 View Code Duplication
    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->version_num > $av2->version_num) {
631
                $av2->deprecated = 1;
632
            } else if ($av2->beta) {
633
                $av2->deprecated = 1;
634
            }
635
        }
636
    }
637
    $r = array();
638
    foreach ($avs as $av) {
639
        if (!$av->deprecated) {
640
            $r[] = $av;
641
        }
642
    }
643
    return $r;
644
}
645
646
// return the "latest" app versions for a given app
647
//
648
function latest_avs_app($appid) {
649
    $platforms = BoincPlatform::enum("");
650
    $r = array();
651
    foreach ($platforms as $p) {
652
        $avs = latest_avs_app_platform($appid, $p->id);
653
        $r = array_merge($r, $avs);
654
    }
655
    return $r;
656
}
657
658 View Code Duplication
class BoincBadge {
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
659
    static function enum($where_clause) {
660
        $db = BoincDb::get();
661
        return $db->enum('badge', 'BoincBadge', $where_clause);
662
    }
663
    static function insert($clause) {
664
        $db = BoincDb::get();
665
        $ret = $db->insert('badge', $clause);
666
        if (!$ret) return 0;
667
        return $db->insert_id();
668
    }
669
    function update($clause) {
670
        $db = BoincDb::get();
671
        return $db->update($this, 'badge', $clause);
672
    }
673
    static function lookup_id($id) {
674
        $db = BoincDb::get();
675
        return $db->lookup_id($id, 'badge', 'BoincBadge');
676
    }
677
    static function lookup($clause) {
678
        $db = BoincDb::get();
679
        return $db->lookup('badge', 'BoincBadge', $clause);
680
    }
681
    function delete() {
682
        $db = BoincDb::get();
683
        return $db->delete($this, 'badge');
684
    }
685
}
686
687
class BoincBadgeUser {
688
    static function enum($where_clause) {
689
        $db = BoincDb::get();
690
        return $db->enum('badge_user', 'BoincBadgeUser', $where_clause);
691
    }
692
    static function insert($clause) {
693
        $db = BoincDb::get();
694
        $ret = $db->insert('badge_user', $clause);
695
        if (!$ret) return false;
696
        return true;
697
    }
698
    static function lookup($clause) {
699
        $db = BoincDb::get();
700
        return $db->lookup('badge_user', 'BoincBadgeUser', $clause);
701
    }
702
    static function update($clause) {
703
        $db = BoincDb::get();
704
        return $db->update_aux('badge_user', $clause);
705
    }
706
    static function delete($clause) {
707
        $db = BoincDb::get();
708
        $db->delete_aux('badge_user', $clause);
709
    }
710
    static function count($clause) {
711
        $db = BoincDb::get();
712
        return $db->count('badge_user', $clause);
713
    }
714
}
715
716
class BoincBadgeTeam {
717
    static function enum($where_clause) {
718
        $db = BoincDb::get();
719
        return $db->enum('badge_team', 'BoincBadgeTeam', $where_clause);
720
    }
721
    static function insert($clause) {
722
        $db = BoincDb::get();
723
        $ret = $db->insert('badge_team', $clause);
724
        if (!$ret) return false;
725
        return true;
726
    }
727
    static function lookup($clause) {
728
        $db = BoincDb::get();
729
        return $db->lookup('badge_team', 'BoincBadgeTeam', $clause);
730
    }
731
    static function update($clause) {
732
        $db = BoincDb::get();
733
        return $db->update_aux('badge_team', $clause);
734
    }
735
    static function delete($clause) {
736
        $db = BoincDb::get();
737
        $db->delete_aux('badge_team', $clause);
738
    }
739
    static function count($clause) {
740
        $db = BoincDb::get();
741
        return $db->count('badge_team', $clause);
742
    }
743
}
744
745
class BoincCreditUser {
746
    static function lookup($clause) {
747
        $db = BoincDb::get();
748
        return $db->lookup('credit_user', 'BoincCreditUser', $clause);
749
    }
750
    static function enum($where_clause) {
751
        $db = BoincDb::get();
752
        return $db->enum('credit_user', 'BoincCreditUser', $where_clause);
753
    }
754
    static function sum($field, $clause) {
755
        $db = BoincDb::get();
756
        return $db->sum('credit_user', $field, $clause);
757
    }
758
    static function update($clause) {
759
        $db = BoincDb::get();
760
        return $db->update_aux('credit_user', $clause);
761
    }
762
    static function delete_user($user) {
763
        $db = BoincDb::get();
764
        $db->delete_aux('credit_user', "userid=$user->id");
765
    }
766
    static function get_list($where_clause, $order_clause, $limit) {
767
        $db = BoincDB::get();
768
        return $db->get_list('user', 'credit_user', 'id', 'userid', 'BoincCreditUser', '*', $where_clause, $order_clause, $limit);
769
    }
770
}
771
772
class BoincCreditTeam {
773
    static function lookup($clause) {
774
        $db = BoincDb::get();
775
        return $db->lookup('credit_team', 'BoincCreditTeam', $clause);
776
    }
777
    static function enum($where_clause) {
778
        $db = BoincDb::get();
779
        return $db->enum('credit_team', 'BoincCreditTeam', $where_clause);
780
    }
781
    static function sum($field, $clause) {
782
        $db = BoincDb::get();
783
        return $db->sum('credit_team', $field, $clause);
784
    }
785
    static function update($clause) {
786
        $db = BoincDb::get();
787
        return $db->update_aux('credit_team', $clause);
788
    }
789
    static function get_list($where_clause, $order_clause, $limit) {
790
        $db = BoincDB::get();
791
        return $db->get_list('team', 'credit_team', 'id', 'teamid', 'BoincCreditTeam', '*', $where_clause, $order_clause, $limit);
792
    }
793
}
794
795
class BoincToken {
796
797
    static function lookup($clause) {
798
        $db = BoincDb::get();
799
        return $db->lookup('token', 'BoincToken', $clause);
800
    }
801
    
802 View Code Duplication
    static function lookup_valid_token($userid, $token, $type) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
803
        $db = BoincDb::get();
0 ignored issues
show
Unused Code introduced by
$db is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
804
        $token = BoincDb::escape_string($token);
805
        $type = BoincDb::escape_string($type);
806
        $now = time();
807
        return self::lookup("userid=$userid and token='$token' and expire_time > $now and type = '$type'");
808
    }
809
    
810
    static function enum($where_clause) {
811
        $db = BoincDb::get();
812
        return $db->enum('token', 'BoincToken', $where_clause);
813
    }
814
815
    static function insert($clause) {
816
        $db = BoincDb::get();
817
        return $db->insert('token', $clause);
818
    }
819
820
    static function get_list($where_clause, $order_clause, $limit) {
821
        $db = BoincDB::get();
822
        return $db->get_list('token', 'userid', 'type', 'create_time', 'expire_time', 'BoincToken', '*', $where_clause, $order_clause, $limit);
0 ignored issues
show
Unused Code introduced by
The call to DbConn::get_list() has too many arguments starting with $limit.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
823
    }
824
825
    static function delete_token($where_clause) {
826
        $db = BoincDb::get();
827
        $db->delete_aux('token', $where_clause);
828
        return $db->affected_rows();
829
    }
830
    
831
    static function delete_expired() {
832
        $db = BoincDb::get();
833
        $now = time();
834
        $db->delete_aux('token', "expire_time < $now");
835
        return $db->affected_rows();
836
    }
837
838
    static function delete_for_user($user_id) {
839
        $db = BoincDb::get();
840
        $db->delete_aux('token', "userid=$user_id");
841
        return $db->affected_rows();
842
    }
843
844
}
845
846
class BoincUserDeleted {
847
    
848
    static function insert_user($user) {
849
        $now = time();
850
        $cpid = md5($user->cross_project_id.$user->email_addr);
851
        $clause = "(userid, public_cross_project_id, create_time) values ($user->id, '$cpid', $now)";
852
        $db = BoincDb::get();
853
        return $db->insert('user_deleted', $clause);
854
    }
855
    
856 View Code Duplication
    static function delete_expired() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
857
        $db = BoincDb::get();
858
        $expire_time = time() - 60*86400; //60 days ago
859
        $db->delete_aux('user_deleted', "create_time < $expire_time");
860
        return $db->affected_rows();
861
    }
862
863
}
864
865
class BoincHostDeleted {
866
    
867 View Code Duplication
    static function insert_hosts_for_user($user) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
868
        $now = time();
869
        $clause = "select id, host_cpid, $now from host where userid = $user->id";
870
        $db = BoincDb::get();
871
        return $db->insert('host_deleted', $clause);
872
    }
873
    
874 View Code Duplication
    static function delete_expired() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
875
        $db = BoincDb::get();
876
        $expire_time = time() - 60*86400; //60 days ago
877
        $db->delete_aux('host_deleted', "create_time < $expire_time");
878
        return $db->affected_rows();
879
    }
880
881
}
882
883
884
// DEPRECATED: use BoincDb::escape_string where possible
885
// 
886
// apply this to any user-supplied strings used in queries
887
// 
888
function boinc_real_escape_string($x) {
889
    if (version_compare(phpversion(),"4.3.0")>=0) {
890
        return BoincDb::escape_string($x);
891
    } else {
892
        $x = str_replace("'", "\'", $x);
893
        $x = str_replace("\"", "\\\"", $x);
894
        return $x;
895
    }
896
}
897
898
?>
899