Passed
Pull Request — master (#5666)
by David
10:22
created

do_inbox()   C

Complexity

Conditions 14
Paths 138

Size

Total Lines 102
Code Lines 71

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 14
eloc 71
c 1
b 0
f 0
nc 138
nop 1
dl 0
loc 102
rs 5.3926

How to fix   Long Method    Complexity   

Long Method

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

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

Commonly applied refactorings include:

1
<?php
2
// This file is part of BOINC.
3
// http://boinc.berkeley.edu
4
// Copyright (C) 2021 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
require_once("../inc/boinc_db.inc");
20
require_once("../inc/email.inc");
21
require_once("../inc/pm.inc");
22
require_once("../inc/forum.inc");
23
require_once("../inc/akismet.inc");
24
25
function show_block_link($userid) {
26
    echo " <a href=\"pm.php?action=block&amp;id=$userid\">";
27
    show_image(REPORT_POST_IMAGE, tra("Block messages from this user"), tra("Block user"), REPORT_POST_IMAGE_HEIGHT);
28
    echo "</a>";
29
}
30
31
$logged_in_user = get_logged_in_user();
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $logged_in_user is correct as get_logged_in_user() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
32
BoincForumPrefs::lookup($logged_in_user);
33
34
function make_script() {
35
    echo "
36
        <script type=\"text/javascript\">
37
        function set_all(val) {
38
            f = document.msg_list;
39
            n = f.elements.length;
40
            for (i=0; i<n; i++) {
41
                e = f.elements[i];
42
                if (e.type=='checkbox') {
43
                    e.checked = val;
44
                }
45
            }
46
        }
47
        </script>
48
    ";
49
}
50
51
// show private messages,
52
// and delete notifications of new messages
53
//
54
function do_inbox($logged_in_user) {
55
    page_head(
56
        sprintf('%s: %s',
57
            tra("Private messages"),
58
            tra("Inbox")
59
        )
60
    );
61
62
    make_script();
63
    if (get_int("sent", true) == 1) {
64
        echo "<h3>".tra("Your message has been sent.")."</h3>\n";
65
    }
66
    $options = get_output_options($logged_in_user);
67
68
    BoincNotify::delete_aux("userid=$logged_in_user->id and type=".NOTIFY_PM);
69
70
    $msgs = BoincPrivateMessage::enum(
71
        "userid=$logged_in_user->id ORDER BY date DESC"
72
    );
73
    $nmsgs = count($msgs);
74
    if ($nmsgs == 0) {
75
        echo tra("You have no private messages.");
76
    } else {
77
        // see if we have to paginate messages
78
        //
79
        $nshow = $logged_in_user->prefs->display_wrap_postcount;
80
        if ($nshow < 1) $nshow = 20;
81
        $offset = 0;
82
        if ($nmsgs > $nshow) {
83
            $offset = get_int('offset', true);
84
            if ($offset === false) $offset = 0;
85
            if ($offset >= $nmsgs) $offset = 0;
86
            echo sprintf('Showing messages %d to %d of %d',
87
                $offset+1,
88
                min($offset+$nshow, $nmsgs),
89
                $nmsgs
90
            );
91
            if ($offset) {
92
                echo sprintf(
93
                    ' &middot; <a href=pm.php?action=inbox&offset=%d>Previous %d</a>',
94
                    max(0, $offset-$nshow), $nshow
95
                );
96
            }
97
            if ($offset+$nshow < $nmsgs) {
98
                echo sprintf(
99
                    ' &middot; <a href=pm.php?action=inbox&offset=%d>Next %d</a>',
100
                    $offset+$nshow, $nshow
101
                );
102
            }
103
        }
104
105
        echo "<form name=msg_list action=pm.php method=post>
106
            <input type=hidden name=action value=delete_selected>
107
        ";
108
        echo form_tokens($logged_in_user->authenticator);
109
        start_table('table-striped');
110
        row_heading_array(
111
            array(tra("Subject"), tra("Sender and date"), tra("Message")),
112
            array('style="width: 12em;"', 'style="width: 10em;"', "")
113
        );
114
        $i = 0;
115
        foreach($msgs as $msg) {
116
            if ($i<$offset) {
117
                $i++;
118
                continue;
119
            }
120
            if ($i>=$offset+$nshow) break;
121
            $i++;
122
            $sender = BoincUser::lookup_id($msg->senderid);
123
            if (!$sender) {
124
                $msg->delete();
125
                continue;
126
            }
127
            echo "<tr>\n";
128
            $checkbox = "<input type=checkbox name=pm_select_$msg->id>";
129
            if (!$msg->opened) {
130
                $msg->update("opened=1");
131
            }
132
            echo "<td valign=top> $checkbox $msg->subject </td>\n";
133
            echo "<td valign=top>".user_links($sender, BADGE_HEIGHT_SMALL);
134
            show_block_link($msg->senderid);
135
            echo "<br>".time_str($msg->date)."</td>\n";
136
            echo "<td valign=top>".output_transform($msg->content, $options)."<p>";
137
            $tokens = url_tokens($logged_in_user->authenticator);
138
            show_button("pm.php?action=new&amp;replyto=$msg->id", tra("Reply"), tra("Reply to this message"));
139
            show_button("pm.php?action=delete&amp;id=$msg->id&amp;$tokens", tra("Delete"), tra("Delete this message"));
140
            echo "</ul></td></tr>\n";
141
        }
142
        echo "
143
            <tr><td>
144
            <a href=\"javascript:set_all(1)\">".tra("Select all")."</a>
145
            |
146
            <a href=\"javascript:set_all(0)\">".tra("Unselect all")."</a>
147
            </td>
148
            <td colspan=2>
149
            <input class=\"btn btn-danger\" type=submit value=\"".tra("Delete selected messages")."\">
150
            </td></tr>
151
        ";
152
        end_table();
153
        echo "</form>\n";
154
    }
155
    page_tail();
156
}
157
158
// the following isn't currently used - we never show single messages
159
//
160
function do_read($logged_in_user) {
161
    $id = get_int("id");
162
    $message = BoincPrivateMessage::lookup_id($id);
163
    if (!$message || $message->userid != $logged_in_user->id) {
164
        error_page(tra("no such message"));
165
    }
166
    page_head(tra("Private messages")." : ".$message->subject);
167
    pm_header();
168
169
    $sender = BoincUser::lookup_id($message->senderid);
170
171
    start_table();
172
    echo "<tr><th>".tra("Subject")."</th><td>".$message->subject."</td></tr>";
173
    echo "<tr><th>".tra("Sender")."</th><td>".user_links($sender, BADGE_HEIGHT_SMALL);
174
    show_block_link($message->senderid);
175
    echo "</td></tr>";
176
    echo "<tr><th>".tra("Date")."</th><td>".time_str($message->date)."</td></tr>";
177
    echo "<tr><th>".tra("Message")."</th><td>".output_transform($message->content, $options)."</td></tr>";
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $options seems to be never defined.
Loading history...
178
    echo "<tr><td></td><td>\n";
179
    echo "<a href=\"pm.php?action=new&amp;replyto=$id\">".tra("Reply")."</a>\n";
180
    echo " &middot; <a href=\"pm.php?action=delete&amp;id=$id\">".tra("Delete")."</a>\n";
181
    echo " &middot; <a href=\"pm.php?action=inbox\">".tra("Inbox")."</a>\n";
182
    end_table();
183
184
    if ($message->opened == 0) {
185
        $message->update("opened=1");
186
    }
187
    page_tail();
188
}
189
190
function do_new($logged_in_user) {
191
    global $replyto, $userid;
192
    check_banished($logged_in_user);
193
    if (VALIDATE_EMAIL_TO_POST) {
194
        check_validated_email($logged_in_user);
195
    }
196
    pm_form($replyto, $userid);
197
}
198
199
function do_delete($logged_in_user) {
200
    $id = get_int("id", true);
201
    if ($id == null) {
202
        $id = post_int("id");
203
    }
204
    check_tokens($logged_in_user->authenticator);
205
    BoincPrivateMessage::delete_aux("userid=".$logged_in_user->id." AND id=$id");
206
    header("Location: pm.php");
207
}
208
209
function do_send_team($logged_in_user) {
210
    check_tokens($logged_in_user->authenticator);
211
    $subject = post_str("subject", true);
212
    $content = post_str("content", true);
213
    $teamid = post_int("teamid");
214
    if (post_str("preview", true) == tra("Preview")) {
215
        pm_team_form($logged_in_user, $teamid);
216
        return;
217
    }
218
219
    // make sure user is authorized, i.e. is a team admin
220
    //
221
    $team = BoincTeam::lookup_id($teamid);
222
    if (!$team) {
223
        error_page("no such team");
224
    }
225
    if (!is_team_admin($logged_in_user, $team)) {
226
        error_page("no team admin");
227
    }
228
229
    if (($subject == null) || ($content == null)) {
0 ignored issues
show
Bug introduced by
It seems like you are loosely comparing $content of type mixed|string against null; this is ambiguous if the string can be empty. Consider using a strict comparison === instead.
Loading history...
Bug introduced by
It seems like you are loosely comparing $subject of type mixed|string against null; this is ambiguous if the string can be empty. Consider using a strict comparison === instead.
Loading history...
230
        pm_team_form(
231
            $logged_in_user, $teamid,
232
            tra("You need to fill all fields to send a private message")
233
        );
234
        return;
235
    }
236
237
    $subject = "Message from team ".$team->name.": ".$subject;
238
        // don't use tra() here because we don't know language of recipient
239
        // Also, we use it in pm_count() to exclude team messages from limit check
240
    $users = BoincUser::enum("teamid=$teamid");
241
    foreach ($users as $user) {
242
        pm_send_msg($logged_in_user, $user, $subject, $content, true);
243
    }
244
    page_head(tra("Message sent"));
245
    echo tra("Your message was sent to %1 team members.", count($users));
246
    page_tail();
247
}
248
249
function do_send($logged_in_user) {
250
    global $replyto, $userid;
251
    check_banished($logged_in_user);
252
    if (VALIDATE_EMAIL_TO_POST) {
253
        check_validated_email($logged_in_user);
254
    }
255
    check_tokens($logged_in_user->authenticator);
256
257
    $to = sanitize_tags(post_str("to", true));
258
    $subject = post_str("subject", true);
259
    $content = post_str("content", true);
260
261
    if (post_str("preview", true) == tra("Preview")) {
262
        pm_form($replyto, $userid);
263
    }
264
    if (($to == null) || ($subject == null) || ($content == null)) {
0 ignored issues
show
Bug introduced by
It seems like you are loosely comparing $to of type mixed|string against null; this is ambiguous if the string can be empty. Consider using a strict comparison === instead.
Loading history...
Bug introduced by
It seems like you are loosely comparing $subject of type mixed|string against null; this is ambiguous if the string can be empty. Consider using a strict comparison === instead.
Loading history...
Bug introduced by
It seems like you are loosely comparing $content of type mixed|string against null; this is ambiguous if the string can be empty. Consider using a strict comparison === instead.
Loading history...
265
        pm_form($replyto, $userid, tra("You need to fill all fields to send a private message"));
266
        return;
267
    }
268
    if (!akismet_check($logged_in_user, $content)) {
269
        pm_form($replyto, $userid, tra("Your message was flagged as spam
270
            by the Akismet anti-spam system.
271
            Please modify your text and try again.")
272
        );
273
    }
274
    $to = str_replace(", ", ",", $to); // Filter out spaces after separator
275
    $users = explode(",", $to);
276
277
    $userlist = array();
278
    $userids = array(); // To prevent from spamming a single user by adding it multiple times
279
280
    foreach ($users as $username) {
281
        $user = explode(" ", $username);
282
        if (is_numeric($user[0])) { // user ID is given
283
            $userid = $user[0];
284
            $user = BoincUser::lookup_id($userid);
285
            if ($user == null) {
286
                pm_form($replyto, $userid, tra("Could not find user with id %1", $userid));
287
            }
288
        } else {
289
            $users = BoincUser::lookup_name($username);
290
            if (count($users) == 0) {
291
                pm_form($replyto, $userid, tra("Could not find user with username %1", $username));
292
            } elseif (count($users) > 1) { // Non-unique username
293
                pm_form($replyto, $userid, tra("%1 is not a unique username; you will have to use user ID", $username));
294
            }
295
            $user = $users[0];
296
        }
297
        BoincForumPrefs::lookup($user);
298
        if (is_ignoring($user, $logged_in_user)) {
299
            pm_form(
300
                $replyto, $userid,
301
                UNIQUE_USER_NAME
302
                ?tra("User %1 is not accepting private messages from you.",
0 ignored issues
show
Coding Style introduced by
Expected 1 space before "?"; newline found
Loading history...
303
                    $user->name
304
                )
305
                :tra("User %1 (ID: %2) is not accepting private messages from you.",
0 ignored issues
show
Coding Style introduced by
Expected 1 space before ":"; newline found
Loading history...
306
                    $user->name,
307
                    $user->id
308
                )
309
            );
310
        }
311
        if (!isset($userids[$user->id])) {
312
            $userlist[] = $user;
313
            $userids[$user->id] = true;
314
        }
315
    }
316
317
    foreach ($userlist as $user) {
318
        if (!is_moderator($logged_in_user, null)) {
319
            check_pm_count($logged_in_user->id);
320
        }
321
        pm_send_msg($logged_in_user, $user, $subject, $content, true);
322
    }
323
324
    Header("Location: pm.php?action=inbox&sent=1");
0 ignored issues
show
Coding Style introduced by
Calls to inbuilt PHP functions must be lowercase; expected "header" but found "Header"
Loading history...
325
}
326
327
function do_block($logged_in_user) {
328
    $id = get_int("id");
329
    $user = BoincUser::lookup_id($id);
330
    if (!$user) {
331
        error_page(tra("No such user"));
332
    }
333
    page_head(tra("Really block %1?", $user->name));
334
    echo "<div>".tra("Are you really sure you want to block user %1 from sending you private messages?", $user->name)."<br>\n";
335
    echo tra("Please note that you can only block a limited amount of users.")."</div>\n";
336
    echo "<div>".tra("Once the user has been blocked you can unblock it using forum preferences page.")."</div>\n";
337
338
    echo "<form action=\"pm.php\" method=\"POST\">\n";
339
    echo form_tokens($logged_in_user->authenticator);
340
    echo "<input type=\"hidden\" name=\"action\" value=\"confirmedblock\">\n";
341
    echo "<input type=\"hidden\" name=\"id\" value=\"$id\">\n";
342
    echo "<input class=\"btn btn-default\" type=\"submit\" value=\"".tra("Add user to filter")."\">\n";
343
    echo "<a href=\"pm.php?action=inbox\">".tra("No, cancel")."</a>\n";
344
    echo "</form>\n";
345
    page_tail();
346
}
347
348
function do_confirmedblock($logged_in_user) {
349
    check_tokens($logged_in_user->authenticator);
350
    $id = post_int("id");
351
    $blocked_user = BoincUser::lookup_id($id);
352
    if (!$blocked_user) error_page(tra("no such user"));
353
    add_ignored_user($logged_in_user, $blocked_user);
354
355
    page_head(tra("User %1 blocked", $blocked_user->name));
356
357
    echo "<div>".tra("User %1 has been blocked from sending you private messages.", $blocked_user->name)."\n";
358
    echo tra("To unblock, visit %1 message board preferences %2", "<a href=\"edit_forum_preferences_form.php\">", "</a>")."</div>\n";
359
    page_tail();
360
}
361
362
function do_delete_selected($logged_in_user) {
363
    check_tokens($logged_in_user->authenticator);
364
365
    $msgs = BoincPrivateMessage::enum(
366
        "userid=$logged_in_user->id"
367
    );
368
    foreach($msgs as $msg) {
369
        $x = "pm_select_$msg->id";
370
        if (post_str($x, true)) {
371
            $msg = BoincPrivateMessage::lookup_id($msg->id);
372
            $msg->delete();
373
        }
374
    }
375
    Header("Location: pm.php?action=inbox&deleted=1");
0 ignored issues
show
Coding Style introduced by
Calls to inbuilt PHP functions must be lowercase; expected "header" but found "Header"
Loading history...
376
}
377
378
$replyto = get_int("replyto", true);
379
$userid = get_int("userid", true);
380
$teamid = get_int("teamid", true);
381
if (!$teamid) {
382
    $teamid = post_int("teamid", true);
383
}
384
385
$action = sanitize_tags(get_str("action", true));
386
if (!$action) {
387
    $action = sanitize_tags(post_str("action", true));
388
}
389
390
if (!$action) {
391
    $action = "inbox";
392
}
393
394
if ($action == "inbox") {
395
    do_inbox($logged_in_user);
396
} elseif ($action == "read") {
397
    do_read($logged_in_user);
398
} elseif ($action == "new") {
399
    if (!$teamid) $teamid = post_int("teamid", true);
400
    if ($teamid) {
401
        pm_team_form($logged_in_user, $teamid);
402
    } else {
403
        do_new($logged_in_user);
404
    }
405
} elseif ($action == "delete") {
406
    do_delete($logged_in_user);
407
} elseif ($action == "send") {
408
    if ($teamid) {
409
        do_send_team($logged_in_user);
410
    } else {
411
        do_send($logged_in_user);
412
    }
413
} elseif ($action == "block") {
414
    do_block($logged_in_user);
415
} elseif ($action == "confirmedblock") {
416
    do_confirmedblock($logged_in_user);
417
} elseif ($action == "delete_selected") {
418
    do_delete_selected($logged_in_user);
419
} else {
420
    error_page(tra("Unknown action"));
421
}
422
423
$cvs_version_tracker[]="\$Id: pm.php 14077 2007-11-03 04:26:47Z davea $";
424
?>
425