Passed
Pull Request — master (#5698)
by David
08:58
created

pm_team_form()   B

Complexity

Conditions 5
Paths 16

Size

Total Lines 55
Code Lines 43

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 43
c 0
b 0
f 0
nc 16
nop 3
dl 0
loc 55
rs 8.9208

How to fix   Long Method   

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) 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
require_once("boinc_db.inc");
20
require_once("sanitize_html.inc");
21
require_once("bbcode_html.inc");
22
23
function pm_header() {
24
    echo "<div>\n";
25
    echo "    <a href=\"pm.php?action=inbox\">".tra("Inbox")."</a>\n";
26
    echo "    &middot; <a href=\"pm.php?action=new\">".tra("Write")."</a>\n";
27
    echo "</div>\n";
28
}
29
30
function pm_rules() {
31
    if (defined('PM_RULES')) return PM_RULES;
32
    $x = "<table><tr><td align=left><small>";
33
    $x .= tra("
34
        <ul>
35
        <li> Messages may not contain content that is obscene, hate-related,
36
            sexually explicit or suggestive.
37
        <li> No commercial advertisements.
38
        <li> No links to web sites involving sexual content,
39
            gambling, or intolerance of others.
40
        <li> No messages intended to annoy or antagonize other people.
41
        <li> No messages that are deliberately hostile, threatening, or insulting.
42
        <li> No abusive comments involving race, religion,
43
            nationality, gender, class or sexuality.
44
        <li> The privileges of violators may be suspended or revoked.
45
        <li> If your account is suspended, don't create a new one.
46
        </ul>
47
    ");
48
    $x .= "</small></td></tr></table>\n";
49
    return $x;
50
}
51
52
function pm_team_form($user, $teamid, $error=null) {
53
    global $bbcode_html, $bbcode_js;
54
    $team = BoincTeam::lookup_id($teamid);
55
    if (!$team) {
56
        error_page("no such team");
57
    }
58
    if (!is_team_admin($user, $team)) {
59
        error_page("not admin");
60
    }
61
62
    page_head(tra("Send message to team"),'','','', $bbcode_js);
63
64
    $subject = post_str("subject", true);
65
    $content = post_str("content", true);
66
    if (post_str("preview", true) == tra("Preview")) {
67
        panel(tra('Preview'),
68
            function() use($content) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space after USE keyword; found 0
Loading history...
69
                echo output_transform($content, null);
70
            }
71
        );
72
    }
73
    if ($error) {
74
        echo "<p class=\"text-danger\">".$error."</p>\n";
75
    }
76
77
    echo "<form action=\"pm.php\" method=\"post\" name=\"post\" onsubmit=\"return checkForm(this)\">\n";
78
    echo "<input type=\"hidden\" name=\"action\" value=\"send\">\n";
79
    echo "<input type=\"hidden\" name=\"teamid\" value=\"$teamid\">\n";
80
    echo form_tokens($user->authenticator);
81
    start_table();
82
    row2(
83
        tra("Subject"),
84
        "<input type=\"text\" class=\"form-control\" name=\"subject\" value=\"$subject\">",
85
        null, '20%'
86
    );
87
    row2_init(tra("Message")."<small>".bbcode_info()."</small>", '20%');
88
    start_table();
89
    echo $bbcode_html;
90
    echo "<tr><td>\n";
91
    echo "<textarea name=\"content\" class=\"form-control\" rows=\"18\">$content</textarea>";
92
    echo "</td></tr>\n";
93
    end_table();
94
    echo sprintf(
95
        '<tr><td></td><td>
96
        <input class="btn" %s type="submit" name="preview" value="%s">
97
        <input class="btn" %s type="submit" value="%s">
98
        </td></tr>
99
        ',
100
        button_style('blue'),
101
        tra("Preview"),
102
        button_style(),
103
        tra("Send message")
104
    );
105
    end_table();
106
    page_tail();
107
}
108
109
function pm_form($replyto, $userid, $error = null) {
110
    global $bbcode_html, $bbcode_js;
111
    global $g_logged_in_user;
112
    page_head(tra("Send private message"),'','','', $bbcode_js);
113
114
    if (post_str("preview", true) == tra("Preview")) {
115
        $content = post_str("content", true);
116
        panel(tra('Preview'),
117
            function() use($content) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space after USE keyword; found 0
Loading history...
118
                echo output_transform($content, null);
119
            }
120
        );
121
    }
122
123
    $subject = '';
124
    $content = '';
125
    if ($replyto) {
126
        $message = BoincPrivateMessage::lookup_id($replyto);
127
        if (!$message || $message->userid != $g_logged_in_user->id) {
128
            error_page(tra("no such message"));
129
        }
130
        $content = "[quote]".$message->content."[/quote]\n";
131
        $userid = $message->senderid;
132
        $user = BoincUser::lookup_id($userid);
133
        if (!$user) {
134
            error_page("Sender no longer exists");
135
        }
136
        $writeto = UNIQUE_USER_NAME?$user->name:$userid." (".$user->name.")";
137
        $subject = $message->subject;
138
        if (substr($subject, 0, 3) != "re:") {
139
            $subject = "re: ".$subject;
140
        }
141
    } elseif ($userid) {
142
        $user = BoincUser::lookup_id($userid);
143
        if (!$user) {
144
            error_page("Sender no longer exists");
145
        }
146
        $writeto = UNIQUE_USER_NAME?$user->name:$userid." (".$user->name.")";
147
    } else {
148
        $writeto = sanitize_tags(post_str("to", true));
149
        $subject = post_str("subject", true);
150
        $content = post_str("content", true);
151
    }
152
153
    $content = htmlspecialchars($content);
154
    $subject = htmlspecialchars($subject);
155
156
    if ($error != null) {
157
        echo "<p class=\"text-danger\">".$error."</p>\n";
158
    }
159
160
    echo "<form action=\"pm.php\" method=\"post\" name=\"post\" onsubmit=\"return checkForm(this)\">\n";
161
    echo "<input type=\"hidden\" name=\"action\" value=\"send\">\n";
162
    echo form_tokens($g_logged_in_user->authenticator);
163
    start_table();
164
    row2(
165
        sprintf('%s <br><small>%s</small>',
166
            tra("To"),
167
            UNIQUE_USER_NAME
168
                ?tra('User names, separated with commas')
0 ignored issues
show
Coding Style introduced by
Expected 1 space before "?"; newline found
Loading history...
169
                :tra("User IDs or unique usernames, separated with commas")
0 ignored issues
show
Coding Style introduced by
Expected 1 space before ":"; newline found
Loading history...
170
        ),
171
        "<input type=\"text\" class=\"form-control\" name=\"to\" value=\"$writeto\">",
172
        null, '20%'
173
    );
174
    row2(
175
        tra("Subject"),
176
            "<input type=\"text\" class=\"form-control\" name=\"subject\" value=\"$subject\">",
177
        null, '20%'
178
    );
179
    row2_init(
180
        tra("Message")."<small>".bbcode_info().pm_rules()."</small>",
181
        '20%'
182
    );
183
    start_table();
184
    echo $bbcode_html;
185
    echo "<tr><td>\n";
186
    echo "<textarea name=\"content\" class=\"form-control\" rows=\"18\">$content</textarea>";
187
    echo "</td></tr>\n";
188
    end_table();
189
    echo "<tr><td></td><td><input class=\"btn btn-primary\" type=\"submit\" name=\"preview\" value=\"".tra("Preview")."\"> <input class=\"btn btn-success\" type=\"submit\" value=\"".tra("Send message")."\"></td></tr>\n";
190
    end_table();
191
192
    page_tail();
193
    exit();
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
194
}
195
196
function send_pm_notification_email(
197
    $logged_in_user, $to_user, $subject, $content
0 ignored issues
show
Coding Style introduced by
Multi-line function declarations must define one parameter per line
Loading history...
198
) {
199
    $message  = "
200
You have received a new private message at ".PROJECT.".
201
202
From: $logged_in_user->name (ID $logged_in_user->id)
203
Subject: $subject
204
205
$content
206
207
--------------------------
208
To delete or respond to this message, visit:
209
".secure_url_base()."pm.php
210
211
To change email preferences, visit:
212
".secure_url_base()."edit_forum_preferences_form.php
213
Do not reply to this message.
214
" ;
0 ignored issues
show
Coding Style introduced by
Space found before semicolon; expected "";" but found "" ;"
Loading history...
215
    send_email($to_user, "[".PROJECT."] - private message", $message);
216
}
217
218
function pm_email_line($notify) {
219
    $pm = BoincPrivateMessage::lookup_id($notify->opaque);
220
    $from_user = BoincUser::lookup_id($pm->senderid);
221
    if (!$pm || !$from_user) return null;
222
    return "$from_user->name ".tra("sent you a private message; subject:")." '$pm->subject'";
223
}
224
225
function pm_web_line($notify) {
226
    $pm = BoincPrivateMessage::lookup_id($notify->opaque);
227
    $from_user = BoincUser::lookup_id($pm->senderid);
228
    if (!$pm || !$from_user) return null;
229
    return "<a href=pm.php>".tra("Private message%1 from %2, subject:" , "</a>", $from_user->name )." $pm->subject";
230
}
231
232
function pm_send_msg($from_user, $to_user, $subject, $content, $send_email) {
233
    $sql_subject = BoincDb::escape_string(sanitize_tags($subject));
234
    $sql_content = BoincDb::escape_string($content);
235
    $mid = BoincPrivateMessage::insert("(userid, senderid, date, subject, content) VALUES ($to_user->id, $from_user->id, UNIX_TIMESTAMP(), '$sql_subject', '$sql_content')");
236
    if (!$mid) {
237
        error_page(tra("Couldn't create message"));
238
    }
239
    // send email notification if needed
240
    //
241
    if ($send_email) {
242
        BoincForumPrefs::lookup($to_user);
243
        switch ($to_user->prefs->pm_notification) {
244
        case 0:
0 ignored issues
show
Coding Style introduced by
Empty CASE statements are not allowed
Loading history...
245
        case 2:
0 ignored issues
show
Coding Style introduced by
Empty CASE statements are not allowed
Loading history...
246
            break;
247
        case 1:
248
            send_pm_notification_email(
249
                $from_user, $to_user, $subject, $content
250
            );
251
            break;
252
        }
253
    }
254
255
    // create notification in any case
256
    //
257
    BoincNotify::insert("(userid, create_time, type, opaque) values ($to_user->id, ".time().", ".NOTIFY_PM.", $mid)");
258
}
259
260
function pm_count($userid, $duration) {
261
    $time = time() - $duration;
262
263
    // we don't want to include team messages in this count.
264
    // Kludge for excluding them based on subject.
265
    // Should add a flag to private_message to distinguish them.
266
    //
267
    return BoincPrivateMessage::count(
268
        "senderid=$userid AND date>$time AND subject not like 'Message from team%'"
269
    );
270
}
271
272
function check_pm_count($userid) {
273
    if ((pm_count($userid, 60) >= 2) || (pm_count($userid, 600) >= 5) ||
274
        (pm_count($userid, 3600) >= 15) || (pm_count($userid, 86400) >= 50)) {
275
        error_page(tra("You are not allowed to send privates messages so often. Please wait some time before sending more messages."));
276
    }
277
}
278
279
function pm_notification($user) {
280
    $output = "";
281
    $unread = BoincPrivateMessage::count("userid=$user->id AND opened=0");
282
283
    $output .= "<a href=\"pm.php?action=inbox\">".tra("Inbox")."</a>";
284
    if ($unread) {
285
        $output .= "<span class=\"inboxunread\"> ($unread ".tra("unread").")</span>\n";
286
    }
287
    $output .= " &middot; <a href=\"pm.php?action=new\">".tra("Write")."</a>\n";
288
    return $output;
289
}
290
291
function pm_email_remind($user) {
292
    if (!$user->prefs->pm_notification) {
293
        return "<br><small>" .
294
            tra(
295
                "For email notification, %1 edit community prefs %2",
296
                '<a href="edit_forum_preferences_form.php">', '</a>'
297
            ) .
298
            "</small>"
299
        ;
0 ignored issues
show
Coding Style introduced by
Space found before semicolon; expected ""</small>";" but found ""</small>"
;"
Loading history...
300
    }
301
    return "";
302
}
303
304
function pm_rss($notify, &$title, &$msg, &$url) {
305
    $pm = BoincPrivateMessage::lookup_id($notify->opaque);
306
    $from_user = BoincUser::lookup_id($pm->senderid);
307
    if (!$pm || !$from_user) {
308
        $msg = null;
309
        return;
310
    }
311
    $title = tra("Private message");
312
    $msg = "You have received a <a href=".secure_url_base()."pm.php>private message</a>.";
313
    $url = secure_url_base()."pm.php";
314
}
315
316
function pm_delete_user($user) {
317
    $mm = BoincPrivateMessage::enum("userid=$user->id or senderid=$user->id");
318
    foreach ($mm as $m) {
319
        $m->delete();
320
    }
321
}
322
323
$cvs_version_tracker[]="\$Id: pm.inc 14019 2007-11-01 23:04:39Z davea $";
324
?>
325