Passed
Pull Request — master (#5852)
by David
10:03
created

pm_form_page()   F

Complexity

Conditions 15
Paths 336

Size

Total Lines 96
Code Lines 72

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 15
eloc 72
c 1
b 1
f 0
nc 336
nop 3
dl 0
loc 96
rs 3.3833

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