Passed
Pull Request — master (#5620)
by David
09:28
created

pm_form()   C

Complexity

Conditions 11
Paths 44

Size

Total Lines 85
Code Lines 65

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 11
eloc 65
c 0
b 0
f 0
nc 44
nop 3
dl 0
loc 85
rs 6.6169

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) 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
    $x = "<table><tr><td align=left><small>";
32
    $x .= tra("
33
        <ul>
34
        <li> Messages may not contain content that is obscene, hate-related,
35
            sexually explicit or suggestive.
36
        <li> No commercial advertisements.
37
        <li> No links to web sites involving sexual content,
38
            gambling, or intolerance of others.
39
        <li> No messages intended to annoy or antagonize other people.
40
        <li> No messages that are deliberately hostile, threatening, or insulting.
41
        <li> No abusive comments involving race, religion,
42
            nationality, gender, class or sexuality.
43
        <li> The privileges of violators may be suspended or revoked.
44
        <li> If your account is suspended, don't create a new one.
45
        </ul>
46
    ");
47
    $x .= "</small></td></tr></table>\n";
48
    return $x;
49
}
50
51
function pm_team_form($user, $teamid, $error=null) {
52
    global $bbcode_html, $bbcode_js;
53
    $team = BoincTeam::lookup_id($teamid);
54
    if (!$team) {
55
        error_page("no such team");
56
    }
57
    if (!is_team_admin($user, $team)) {
58
        error_page("not admin");
59
    }
60
61
    page_head(tra("Send message to team"),'','','', $bbcode_js);
62
63
    $subject = post_str("subject", true);
64
    $content = post_str("content", true);
65
    if (post_str("preview", true) == tra("Preview")) {
66
        panel(tra('Preview'),
67
            function() use($content) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space after USE keyword; found 0
Loading history...
68
                echo output_transform($content, null);
69
            }
70
        );
71
    }
72
    if ($error) {
73
        echo "<p class=\"text-danger\">".$error."</p>\n";
74
    }
75
76
    echo "<form action=\"pm.php\" method=\"post\" name=\"post\" onsubmit=\"return checkForm(this)\">\n";
77
    echo "<input type=\"hidden\" name=\"action\" value=\"send\">\n";
78
    echo "<input type=\"hidden\" name=\"teamid\" value=\"$teamid\">\n";
79
    echo form_tokens($user->authenticator);
80
    start_table();
81
    row2(
82
        tra("Subject"),
83
        "<input type=\"text\" class=\"form-control\" name=\"subject\" value=\"$subject\">",
84
        null, '20%'
85
    );
86
    row2_init(tra("Message")."<small>".bbcode_info()."</small>", '20%');
87
    start_table();
88
    echo $bbcode_html;
89
    echo "<tr><td>\n";
90
    echo "<textarea name=\"content\" class=\"form-control\" rows=\"18\">$content</textarea>";
91
    echo "</td></tr>\n";
92
    end_table();
93
    echo "<tr><td></td><td>
94
        <input class=\"btn btn-primary\" type=\"submit\" name=\"preview\" value=\"".tra("Preview")."\">
95
        <input class=\"btn btn-success\" type=\"submit\" value=\"".tra("Send message")."\">
96
        </td></tr>\n
97
    ";
98
    end_table();
99
    page_tail();
100
}
101
102
function pm_form($replyto, $userid, $error = null) {
103
    global $bbcode_html, $bbcode_js;
104
    global $g_logged_in_user;
105
    page_head(tra("Send private message"),'','','', $bbcode_js);
106
107
    if (post_str("preview", true) == tra("Preview")) {
108
        $content = post_str("content", true);
109
        panel(tra('Preview'),
110
            function() use($content) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space after USE keyword; found 0
Loading history...
111
                echo output_transform($content, null);
112
            }
113
        );
114
    }
115
116
    $subject = null;
117
    $content = null;
118
    if ($replyto) {
119
        $message = BoincPrivateMessage::lookup_id($replyto);
120
        if (!$message || $message->userid != $g_logged_in_user->id) {
121
            error_page(tra("no such message"));
122
        }
123
        $content = "[quote]".$message->content."[/quote]\n";
124
        $userid = $message->senderid;
125
        $user = BoincUser::lookup_id($userid);
126
        if (!$user) {
127
            error_page("Sender no longer exists");
128
        }
129
        $writeto = $userid." (".$user->name.")";
130
        $subject = $message->subject;
131
        if (substr($subject, 0, 3) != "re:") {
132
            $subject = "re: ".$subject;
133
        }
134
    } elseif ($userid) {
135
        $user = BoincUser::lookup_id($userid);
136
        if (!$user) {
137
            error_page("Sender no longer exists");
138
        }
139
        $writeto = $userid." (".$user->name.")";
140
    } else {
141
        $writeto = sanitize_tags(post_str("to", true));
142
        $subject = post_str("subject", true);
143
        $content = post_str("content", true);
144
    }
145
146
    $content = htmlspecialchars($content);
147
    $subject = htmlspecialchars($subject);
148
149
    if ($error != null) {
150
        echo "<p class=\"text-danger\">".$error."</p>\n";
151
    }
152
153
    echo "<form action=\"pm.php\" method=\"post\" name=\"post\" onsubmit=\"return checkForm(this)\">\n";
154
    echo "<input type=\"hidden\" name=\"action\" value=\"send\">\n";
155
    echo form_tokens($g_logged_in_user->authenticator);
156
    start_table();
157
    row2(
158
        sprintf('%s <br><small>%s</small>',
159
            tra("To"),
160
            UNIQUE_USER_NAME
161
                ?tra('User names, separated with commas')
0 ignored issues
show
Coding Style introduced by
Expected 1 space before "?"; newline found
Loading history...
162
                :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...
163
        ),
164
        "<input type=\"text\" class=\"form-control\" name=\"to\" value=\"$writeto\">",
165
        null, '20%'
166
    );
167
    row2(
168
        tra("Subject"),
169
            "<input type=\"text\" class=\"form-control\" name=\"subject\" value=\"$subject\">",
170
        null, '20%'
171
    );
172
    row2_init(
173
        tra("Message")."<small>".bbcode_info().pm_rules()."</small>",
174
        '20%'
175
    );
176
    start_table();
177
    echo $bbcode_html;
178
    echo "<tr><td>\n";
179
    echo "<textarea name=\"content\" class=\"form-control\" rows=\"18\">$content</textarea>";
180
    echo "</td></tr>\n";
181
    end_table();
182
    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";
183
    end_table();
184
185
    page_tail();
186
    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...
187
}
188
189
function send_pm_notification_email(
190
    $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...
191
) {
192
    $message  = "
193
You have received a new private message at ".PROJECT.".
194
195
From: $logged_in_user->name (ID $logged_in_user->id)
196
Subject: $subject
197
198
$content
199
200
--------------------------
201
To delete or respond to this message, visit:
202
".secure_url_base()."pm.php
203
204
To change email preferences, visit:
205
".secure_url_base()."edit_forum_preferences_form.php
206
Do not reply to this message.
207
" ;
0 ignored issues
show
Coding Style introduced by
Space found before semicolon; expected "";" but found "" ;"
Loading history...
208
    send_email($to_user, "[".PROJECT."] - private message", $message);
209
}
210
211
function pm_email_line($notify) {
212
    $pm = BoincPrivateMessage::lookup_id($notify->opaque);
213
    $from_user = BoincUser::lookup_id($pm->senderid);
214
    if (!$pm || !$from_user) return null;
215
    return "$from_user->name ".tra("sent you a private message; subject:")." '$pm->subject'";
216
}
217
218
function pm_web_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 "<a href=pm.php>".tra("Private message%1 from %2, subject:" , "</a>", $from_user->name )." $pm->subject";
223
}
224
225
function pm_send_msg($from_user, $to_user, $subject, $content, $send_email) {
226
    $sql_subject = BoincDb::escape_string(sanitize_tags($subject));
227
    $sql_content = BoincDb::escape_string($content);
228
    $mid = BoincPrivateMessage::insert("(userid, senderid, date, subject, content) VALUES ($to_user->id, $from_user->id, UNIX_TIMESTAMP(), '$sql_subject', '$sql_content')");
229
    if (!$mid) {
230
        error_page(tra("Couldn't create message"));
231
    }
232
    // send email notification if needed
233
    //
234
    if ($send_email) {
235
        BoincForumPrefs::lookup($to_user);
236
        switch ($to_user->prefs->pm_notification) {
237
        case 0:
0 ignored issues
show
Coding Style introduced by
Empty CASE statements are not allowed
Loading history...
238
        case 2:
0 ignored issues
show
Coding Style introduced by
Empty CASE statements are not allowed
Loading history...
239
            break;
240
        case 1:
241
            send_pm_notification_email(
242
                $from_user, $to_user, $subject, $content
243
            );
244
            break;
245
        }
246
    }
247
248
    // create notification in any case
249
    //
250
    BoincNotify::insert("(userid, create_time, type, opaque) values ($to_user->id, ".time().", ".NOTIFY_PM.", $mid)");
251
}
252
253
function pm_count($userid, $duration) {
254
    $time = time() - $duration;
255
256
    // we don't want to include team messages in this count.
257
    // Kludge for excluding them based on subject.
258
    // Should add a flag to private_message to distinguish them.
259
    //
260
    return BoincPrivateMessage::count(
261
        "senderid=$userid AND date>$time AND subject not like 'Message from team%'"
262
    );
263
}
264
265
function check_pm_count($userid) {
266
    if ((pm_count($userid, 60) >= 2) || (pm_count($userid, 600) >= 5) ||
267
        (pm_count($userid, 3600) >= 15) || (pm_count($userid, 86400) >= 50)) {
268
        error_page(tra("You are not allowed to send privates messages so often. Please wait some time before sending more messages."));
269
    }
270
}
271
272
function pm_notification($user) {
273
    $output = "";
274
    $unread = BoincPrivateMessage::count("userid=$user->id AND opened=0");
275
276
    $output .= "<a href=\"pm.php?action=inbox\">".tra("Inbox")."</a>";
277
    if ($unread) {
278
        $output .= "<span class=\"inboxunread\"> ($unread ".tra("unread").")</span>\n";
279
    }
280
    $output .= " &middot; <a href=\"pm.php?action=new\">".tra("Write")."</a>\n";
281
    return $output;
282
}
283
284
function pm_email_remind($user) {
285
    if (!$user->prefs->pm_notification) {
286
        return "<br><small>" .
287
            tra(
288
                "For email notification, %1 edit community prefs %2",
289
                '<a href="edit_forum_preferences_form.php">', '</a>'
290
            ) .
291
            "</small>"
292
        ;
0 ignored issues
show
Coding Style introduced by
Space found before semicolon; expected ""</small>";" but found ""</small>"
;"
Loading history...
293
    }
294
    return "";
295
}
296
297
function pm_rss($notify, &$title, &$msg, &$url) {
298
    $pm = BoincPrivateMessage::lookup_id($notify->opaque);
299
    $from_user = BoincUser::lookup_id($pm->senderid);
300
    if (!$pm || !$from_user) {
301
        $msg = null;
302
        return;
303
    }
304
    $title = tra("Private message");
305
    $msg = "You have received a <a href=".secure_url_base()."pm.php>private message</a>.";
306
    $url = secure_url_base()."pm.php";
307
}
308
309
function pm_delete_user($user) {
310
    $mm = BoincPrivateMessage::enum("userid=$user->id or senderid=$user->id");
311
    foreach ($mm as $m) {
312
        $m->delete();
313
    }
314
}
315
316
$cvs_version_tracker[]="\$Id: pm.inc 14019 2007-11-01 23:04:39Z davea $";
317
?>
318