Passed
Push — dpa_kw_prefs ( 267ee0 )
by David
16:33 queued 06:49
created

read_kw_prefs()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 13
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 12
c 1
b 0
f 0
nc 5
nop 1
dl 0
loc 13
rs 9.8666
1
<?php
2
// This file is part of BOINC.
3
// https://boinc.berkeley.edu
4
// Copyright (C) 2024 University of California
5
//
6
// BOINC is free software; you can redistribute it and/or modify it
7
// under the terms of the GNU Lesser General Public License
8
// as published by the Free Software Foundation,
9
// either version 3 of the License, or (at your option) any later version.
10
//
11
// BOINC is distributed in the hope that it will be useful,
12
// but WITHOUT ANY WARRANTY; without even the implied warranty of
13
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14
// See the GNU Lesser General Public License for more details.
15
//
16
// You should have received a copy of the GNU Lesser General Public License
17
// along with BOINC.  If not, see <http://www.gnu.org/licenses/>.
18
// read user prefs; return lists of yes and no keywords
19
//
20
21
// utility functions for user keyword prefs
22
23
define('KW_YES', 1);
24
define('KW_MAYBE', 0);
25
define('KW_NO', -1);
26
27
// read user prefs; return lists of yes and no keywords
28
//
29
function read_kw_prefs($user) {
30
    $path = "../../kw_prefs/$user->id";
31
    if (file_exists($path)) {
32
        $x = file($path);
33
        $yes = explode(' ', trim($x[0]));
34
        if ($yes[0]=='') $yes=[];
35
        $yes = array_map('intval', $yes);
36
        $no = explode(' ', trim($x[1]));
37
        if ($no[0]=='') $no=[];
38
        $no = array_map('intval', $no);
39
        return [$yes, $no];
40
    } else {
41
        return [[],[]];
42
    }
43
}
44
45
function write_kw_prefs($user, $yes, $no) {
46
    $y = implode(' ', array_map('strval', $yes));
47
    $n = implode(' ', array_map('strval', $no));
48
    $path = "../../kw_prefs/$user->id";
49
    file_put_contents($path, "$y\n$n\n");
50
}
51
52
?>
53