Passed
Pull Request — master (#5728)
by David
10:20 queued 27s
created

do_unsubscribe()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 9
nc 5
nop 1
dl 0
loc 13
rs 9.9666
c 0
b 0
f 0
1
<?php
2
// This file is part of BOINC.
3
// http://boinc.berkeley.edu
4
// Copyright (C) 2014 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
// Forum index
20
// shows the categories and the forums in each category
21
22
require_once('../inc/forum.inc');
23
require_once('../inc/pm.inc');
24
require_once('../inc/time.inc');
25
26
// Process request to mark all posts as read
27
//
28
if ((get_int("read", true) == 1)) {
29
    if ($user) {
30
        check_tokens($user->authenticator);
31
        $now = time();
32
        $user->prefs->update("mark_as_read_timestamp=$now");
33
        Header("Location: ".get_str("return", true));
0 ignored issues
show
Coding Style introduced by
Calls to inbuilt PHP functions must be lowercase; expected "header" but found "Header"
Loading history...
34
    }
35
}
36
37
function show_forum_summary($forum) {
38
    switch ($forum->parent_type) {
39
    case 0:
40
        $t = $forum->title;
41
        $d = $forum->description;
42
        break;
43
    case 1:
44
        $team = BoincTeam::lookup_id($forum->category);
45
        $t = $forum->title;
46
        if (!strlen($t)) $t = $team->name;
47
        $d = $forum->description;
48
        if (!strlen($d)) $d = tra("Discussion among members of %1", $team->name);
49
        break;
50
    }
51
    echo "
52
        <tr>
53
        <td>
54
            <a href=\"forum_forum.php?id=$forum->id\">$t</a>
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $t does not seem to be defined for all execution paths leading up to this point.
Loading history...
55
            <br><small>$d</small>
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $d does not seem to be defined for all execution paths leading up to this point.
Loading history...
56
        </td>
57
        <td>$forum->threads</td>
58
        <td>$forum->posts</td>
59
        <td>".time_diff_str($forum->timestamp, time())."</td>
60
    </tr>";
61
}
62
63
function main($user) {
64
    page_head(tra("Message boards"));
65
66
    show_forum_header($user);
67
68
    if (defined('FORUM_QA_MERGED_MODE') && FORUM_QA_MERGED_MODE){
69
        $categories = BoincCategory::enum("true order by orderID");
70
    } else {
71
        echo "<p>"
72
            .tra("If you have a question or problem, please use the %1 Questions & Answers %2 section of the message boards.", "<a href=\"forum_help_desk.php\">", "</a>")
73
            ."</p>"
74
        ;
0 ignored issues
show
Coding Style introduced by
Space found before semicolon; expected ""</p>";" but found ""</p>"
;"
Loading history...
75
        $categories = BoincCategory::enum("is_helpdesk=0 order by orderID");
76
    }
77
    $first = true;
78
    foreach ($categories as $category) {
79
        if ($first) {
80
            $first = false;
81
            echo "<p>";
82
            echo forum_title($category, NULL, NULL);
83
            echo "<p>";
84
            show_mark_as_read_button($user);
85
            start_table('table-striped');
86
            row_heading_array(
87
                array(
88
                    tra("Forum"),
89
                    tra("Threads"),
90
                    tra("Posts"),
91
                    tra("Last post")
0 ignored issues
show
Coding Style introduced by
There should be a trailing comma after the last value of an array declaration.
Loading history...
92
                ),
93
                array("width=30%", "", "", "")
94
            );
95
        }
96
        if (strlen($category->name)) {
97
            echo '
98
                <tr>
99
                <th class="info" colspan="4">'.$category->name.'</th>
100
                </tr>
101
            ';
102
        }
103
        $forums = BoincForum::enum("parent_type=0 and category=$category->id order by orderID");
104
        foreach ($forums as $forum) {
105
            show_forum_summary($forum);
106
        }
107
    }
108
109
    if ($user && $user->teamid) {
110
        $forum = BoincForum::lookup("parent_type=1 and category=$user->teamid");
111
        if ($forum) {
112
            show_forum_summary($forum);
113
        }
114
    }
115
    end_table();
116
117
    // show user's subscriptions (threads and forums)
118
119
    if ($user) {
120
        $subs = BoincSubscription::enum("userid=$user->id");
121
        $thread_subs = [];
122
        $forum_subs = [];
123
        foreach ($subs as $sub) {
124
            if ($sub->threadid > 0) {
125
                $thread_subs[] = $sub;
126
            } else {
127
                $forum_subs[] = $sub;
128
            }
129
        }
130
        if (count($subs)) {
131
            echo '<form action=forum_index.php method=post>
132
                <input type=hidden name=action value=unsub>
133
            ';
134
            echo "<p><h3>".tra("Subscriptions")."</h3><p>";
135
            start_table('table-striped');
136
        }
137
        if (count($thread_subs)) {
138
            thread_list_header(true);
139
            foreach ($thread_subs as $sub) {
140
                $thread = BoincThread::lookup_id($sub->threadid);
141
                if (!$thread) {
142
                    BoincSubscription::delete($user->id, $sub->threadid);
143
                    continue;
144
                }
145
                if ($thread->hidden) continue;
146
                thread_list_item($thread, $user, true);
147
            }
148
        }
149
        if (count($forum_subs)) {
150
            row_heading_array([
151
                'Forum', '', 'Last post', 'Unsubscribe'
0 ignored issues
show
Coding Style introduced by
Each value in a multi-line array must be on a new line
Loading history...
Coding Style introduced by
There should be a trailing comma after the last value of an array declaration.
Loading history...
152
            ]);
153
            foreach ($forum_subs as $sub) {
154
                $id = -$sub->threadid;
155
                $forum = BoincForum::lookup_id($id);
156
                if (!$forum) {
157
                    BoincSubscription::delete($user->id, $sub->threadid);
158
                    continue;
159
                }
160
                row_array([
161
                    "$forum->title<br><small>$forum->description</small>",
162
                    '',
163
                    time_diff_str($forum->timestamp, time()),
164
                    "<input type=checkbox name=unsub_forum_$id>"
0 ignored issues
show
Coding Style introduced by
There should be a trailing comma after the last value of an array declaration.
Loading history...
165
                ]);
166
            }
167
        }
168
        if (count($subs)) {
169
            end_table();
170
            echo '
171
                <input type=submit name=submit value="Unsubscribe from selected items">
172
                </form>
173
            ';
174
        }
175
    }
176
177
    page_tail();
178
    BoincForumLogging::cleanup();
179
}
180
181
function do_unsubscribe($user) {
182
    $subs = BoincSubscription::enum("userid=$user->id");
183
    foreach ($subs as $sub) {
184
        if ($sub->threadid > 0) {
185
            $x = "unsub_thread_$sub->threadid";
186
        } else {
187
            $x = sprintf('unsub_forum_%d', -$sub->threadid);
188
        }
189
        if (post_str($x, true)) {
190
            BoincSubscription::delete($user->id, $sub->threadid);
191
        }
192
    }
193
    header('Location: forum_index.php');
194
}
195
196
$user = get_logged_in_user(false);
197
BoincForumPrefs::lookup($user);
198
199
if (DISABLE_FORUMS && !is_admin($user)) {
200
    error_page("Forums are disabled");
201
}
202
203
$submit = post_str('submit', true);
204
if ($submit) {
205
    do_unsubscribe($user);
206
} else {
207
    main($user);
0 ignored issues
show
Unused Code introduced by
The call to main() has too many arguments starting with $user. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

207
    /** @scrutinizer ignore-call */ 
208
    main($user);

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. Please note the @ignore annotation hint above.

Loading history...
208
}
209
$cvs_version_tracker[]="\$Id$";  //Generated automatically - do not edit
210
?>
211