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

show_user_info_private()   F

Complexity

Conditions 16
Paths 432

Size

Total Lines 61
Code Lines 43

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 16
eloc 43
c 0
b 0
f 0
nc 432
nop 1
dl 0
loc 61
rs 2.1888

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("../inc/credit.inc");
20
require_once("../inc/email.inc");
21
require_once("../inc/util.inc");
22
require_once("../inc/team.inc");
23
require_once("../inc/friend.inc");
24
require_once("../inc/forum_db.inc");
25
require_once("../inc/notify.inc");
26
require_once("../inc/ldap.inc");
27
28
if (!defined('REMOTE_PROJECTS_TTL')) {
29
    define('REMOTE_PROJECTS_TTL', 86400);
30
}
31
32
// add an element "projects" to user consisting of array of projects
33
// they've participated in
34
//
35
function get_other_projects($user) {
36
    $cpid = md5($user->cross_project_id . $user->email_addr);
37
    $url = "http://boinc.netsoft-online.com/get_user.php?cpid=".$cpid;
38
39
    // Check the cache for that URL
40
    //
41
    $cacheddata = get_cached_data(REMOTE_PROJECTS_TTL, $url);
42
    if ($cacheddata) {
43
        $remote = unserialize($cacheddata);
44
        if (!$remote) $remote = [];
45
    } else {
46
        // Fetch the XML, use curl if fopen() is disallowed
47
        //
48
        if (ini_get('allow_url_fopen')) {
49
            $timeout = 3;
50
            $old_timeout = ini_set('default_socket_timeout', $timeout);
51
            $xml_object = null;
52
            $f = @file_get_contents($url);
53
            if ($f) {
54
                $xml_object = @simplexml_load_string($f);
55
            }
56
            ini_set('default_socket_timeout', $old_timeout);
57
            if (!$xml_object) {
58
                return $user;
59
            }
60
        } else {
61
            $ch = curl_init($url);
62
            curl_setopt($ch, CURLOPT_HEADER, false);
63
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
64
            curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
65
            curl_setopt($ch, CURLOPT_MAXREDIRS, 3);
66
            curl_setopt($ch, CURLOPT_TIMEOUT, 3);
67
            $rawxml = @curl_exec($ch);
68
            $xml_object = null;
69
            if ($rawxml) {
70
                $xml_object = @simplexml_load_string($rawxml);
0 ignored issues
show
Bug introduced by
It seems like $rawxml can also be of type true; however, parameter $data of simplexml_load_string() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

70
                $xml_object = @simplexml_load_string(/** @scrutinizer ignore-type */ $rawxml);
Loading history...
71
            }
72
            curl_close($ch);
73
            if (!$xml_object) {
74
                return $user;
75
            }
76
        }
77
78
        // auto-cast the project list to an array of stdClass projects
79
        //
80
        $remote = @json_decode(json_encode((array)$xml_object))->project;
81
        if (!$remote) $remote = [];
82
        if (!is_array($remote)) {
83
            $remote = [$remote];
84
        }
85
86
        // Cache the results
87
        set_cached_data(REMOTE_PROJECTS_TTL, serialize($remote), $url);
88
    }
89
    $user->projects = $remote;
90
    return $user;
91
}
92
93
function show_project($project) {
94
    if ($project->url == "http://www.worldcommunitygrid.org/") {
95
        $x = $project->name;
96
    } else {
97
        $x = "<a href=\"$project->url"."show_user.php?userid=$project->id\">$project->name</a>";
98
    }
99
    echo "<tr>
100
        <td>$x</td>
101
        <td align=\"right\">".number_format($project->total_credit, 0)."</td>
102
        <td align=\"right\">".number_format($project->expavg_credit, 0)."</td>
103
        <td align=\"right\">".date_str($project->create_time)."</td>
104
        </tr>
105
    ";
106
}
107
108
function cmp($a, $b) {
109
    if ($a->expavg_credit == $b->expavg_credit) return 0;
110
    return ($a->expavg_credit < $b->expavg_credit) ? 1 : -1;
111
}
112
113
function show_other_projects($user, $personal) {
114
    if (!isset($user->projects)) return;
115
    if (count($user->projects) < 2) return;
116
117
    usort($user->projects, "cmp");
118
    if ($personal) {
119
        echo "<h3>".tra("Projects in which you are participating")."</h3>";
120
    } else {
121
        echo "<h3>".tra("Projects in which %1 is participating", $user->name)."</h3>";
122
    }
123
    start_table('table-striped');
124
    row_heading_array(
125
        array(
126
            tra("Project")."<br/><small>".tra("Click for user page")."</small>",
127
            tra("Total credit"),
128
            tra("Average credit"),
129
            tra("Since")
0 ignored issues
show
Coding Style introduced by
There should be a trailing comma after the last value of an array declaration.
Loading history...
130
        ),
131
        array("", ALIGN_RIGHT, ALIGN_RIGHT, ALIGN_RIGHT)
132
    );
133
    foreach ($user->projects as $project) {
134
        show_project($project);
135
    }
136
    end_table();
137
}
138
139
function total_posts($user) {
140
    return BoincPost::count("user=$user->id");
141
}
142
143
function show_credit($user) {
144
    row2(tra("Total credit"), format_credit_large($user->total_credit));
145
    row2(tra("Recent average credit"), format_credit($user->expavg_credit));
146
    if (function_exists("project_user_credit")) {
147
        project_user_credit($user);
148
    }
149
}
150
151
require_once("../inc/stats_sites.inc");
152
// show dynamic user info (private)
153
//
154
function show_user_stats_private($user) {
155
    global $cpid_stats_sites;
156
157
    if (NO_COMPUTING && NO_STATS && NO_HOSTS) {
158
        return;
159
    }
160
    row1(tra("Computing"));
161
162
    if (!NO_STATS) {
163
        show_credit($user);
164
    }
165
166
    if (!NO_HOSTS) {
167
        row2(tra("Computers on this account"), "<a href=\"hosts_user.php\">".tra("View")."</a>");
168
    }
169
    if (!NO_COMPUTING) {
170
        row2(tra("Tasks"), "<a href=\"results.php?userid=$user->id\">".tra("View")."</a>");
171
    }
172
173
    if (!NO_STATS) {
174
        $cpid = md5($user->cross_project_id . $user->email_addr);
175
        $x = "";
176
        shuffle($cpid_stats_sites);
177
        foreach ($cpid_stats_sites as $site) {
178
            $name = $site[0];
179
            $y = sprintf($site[1], $cpid);
180
            $x .= "<a href=\"$y\">$name</a><br/>\n";
181
        }
182
        $x .= "<br/><small>".tra("Cross-project ID").": $cpid</small>\n";
183
        row2(tra("Cross-project statistics"), $x);
184
        $x = sprintf('<a href="%s">%s</a>', cert_filename(), tra("Account"));
185
        if ($user->teamid) {
186
            $x .= ' &middot; <a href="cert_team.php">'.tra("Team").'</a>';
187
        }
188
        $x .= ' &middot; <a href="cert_all.php">'.tra("Cross-project").'</a>';
189
        row2(tra("Certificate"), $x);
190
    }
191
}
192
193
function notify_description($notify) {
194
    switch ($notify->type) {
195
    case NOTIFY_FRIEND_REQ:
196
        return friend_notify_req_web_line($notify);
197
    case NOTIFY_FRIEND_ACCEPT:
198
        return friend_notify_accept_web_line($notify);
199
    case NOTIFY_PM:
200
        return pm_web_line($notify);
201
    case NOTIFY_SUBSCRIBED_POST:
202
        return subscribed_post_web_line($notify);
203
    }
204
    return null;
205
}
206
207
// a string that can be used to authenticate some operations,
208
// but can't be used to log in to the account
209
// (e.g. can't be used to change email addr or passwd)
210
//
211
// this is a function of
212
// - authenticator (never changes)
213
// - user ID (never changes)
214
// - password
215
// - email addr
216
//
217
function weak_auth($user) {
218
    $x = md5($user->authenticator.$user->passwd_hash);
219
    return "{$user->id}_$x";
220
}
221
222
// originally user URLs were assumed to be http://,
223
// and this prefix wasn't stored.
224
// Now the prefix can be http:// or https://.
225
// This function takes a user URL in any form and converts
226
// it to a canonical form, with the protocol prefix.
227
//
228
function normalize_user_url($url) {
229
    $url = sanitize_user_url($url);
230
    if (!$url) return '';
231
    $x = strtolower($url);
232
    if (substr($x, 0, 7) == 'http://') {
233
        return 'http://'.substr($url, 7);
234
    }
235
    if (substr($x, 0, 8) == 'https://') {
236
        return 'https://'.substr($url, 8);
237
    }
238
    return 'http://'.$url;
239
}
240
241
// show static user info (private)
242
//
243
function show_user_info_private($user) {
244
    row2(tra("Name"), $user->name);
245
    if (LDAP_HOST && is_ldap_email($user->email_addr)) {
246
        row2("LDAP ID", ldap_email_to_uid($user->email_addr));
247
    } else {
248
        $email_text = $user->email_addr;
249
        if (defined("SHOW_NONVALIDATED_EMAIL_ADDR") && !$user->email_validated) {
250
            $email_text .= " (<a href=validate_email_addr.php>must be validated</a>)";
251
        }
252
        row2(tra("Email address"), $email_text);
253
    }
254
    if (USER_URL && $user->url) {
255
        $u = normalize_user_url($user->url);
256
        row2(
257
            tra("URL"),
258
            $u?sprintf('<a href="%s">%s</a>', $u, $u):tra('Invalid URL')
259
        );
260
    }
261
    if (USER_COUNTRY) {
262
        row2(tra("Country"), $user->country);
263
    }
264
    if (POSTAL_CODE) {
265
        row2(tra("Postal code"), $user->postal_code);
266
    }
267
    row2(tra("%1 member since", PROJECT), date_str($user->create_time));
268
    $url_tokens = url_tokens($user->authenticator);
269
    if (LDAP_HOST && is_ldap_email($user->email_addr)) {
270
        // LDAP accounts can't change email or password
271
        //
272
        row2(tra("Change"),
273
            "<a href=\"edit_user_info_form.php?$url_tokens\">Account info</a>"
274
        );
275
    } else {
276
        $delete_account_str = "";
277
        $config = get_config();
278
        if (parse_bool($config, "enable_delete_account")) {
279
            $delete_account_str = " &middot; <a href=\"delete_account_request.php\">".tra("delete account")."</a>";
280
        }
281
282
        row2(tra("Change"),
283
            "<a href=\"edit_email_form.php\">".tra("email address")."</a>
284
            &middot; <a href=\"".secure_url_base()."/edit_passwd_form.php\">".tra("password")."</a>
285
            &middot; <a href=\"edit_user_info_form.php?$url_tokens\">".tra("other account info")."</a>"
286
            .$delete_account_str
287
        );
288
    }
289
    if (!UNIQUE_USER_NAME) {
290
        row2(tra("User ID")."<br/><p class=\"small\">".tra("Used in community functions")."</p>", $user->id);
291
    }
292
    if (!NO_COMPUTING) {
293
        row2(
294
            tra("Account keys"),
295
            "<a href=\"weak_auth.php\">".tra("View")."</a>"
296
        );
297
298
        require_once("../inc/account_ownership.inc");
299
        if (file_exists($account_ownership_private_key_file_path)) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $account_ownership_private_key_file_path seems to be never defined.
Loading history...
300
          // If the server has keys configured show the account ownership form
301
          row2(
302
              tra("Account Ownership"),
303
              "<a href=\"account_ownership.php?$url_tokens\">Generate ownership proof</a>"
304
          );
305
        }
306
307
    }
308
}
309
310
function show_preference_links() {
311
    row1("<a name=\"prefs\"></a>".tra("Preferences"));
312
    if (!NO_GLOBAL_PREFS) {
313
        row2(
314
            tra("When and how BOINC uses your computer"),
315
            "<a href=\"prefs.php?subset=global\">".tra("Computing preferences")."</a>"
316
        );
317
    }
318
    row2(tra("Message boards and private messages"),
319
        "<a href=\"edit_forum_preferences_form.php\">".tra("Community preferences")."</a>"
320
    );
321
    if (!NO_COMPUTING) {
322
        row2(tra("Preferences for this project"),
323
            "<a href=\"prefs.php?subset=project\">".tra("%1 preferences", PROJECT)."</a>"
324
        );
325
    }
326
}
327
328
function friend_links($user) {
329
    if (is_banished($user)) {
330
        return "";
331
    }
332
    $x = "<table height=\"100\" width=\"150\" border=\"0\" cellpadding=\"4\"><tr><td class=\"friend\">";
333
    if ($user->has_profile) {
334
        $profile = BoincProfile::lookup_fields("has_picture", "userid=$user->id");
335
        if ($profile && $profile->has_picture) {
336
            $img_url = profile_thumb_url($user->id);
337
        } else {
338
            $img_url = url_base()."img/head_20.png";
339
        }
340
        $title = tra("View the profile of %1", $user->name);
341
        $alt = tra("Profile");
342
        $x .= ' <a href="'.url_base().'view_profile.php?userid='.$user->id.'"><img title="'.$title.'" src="'.$img_url.'" alt="'.$alt.'"></a><br>';
343
    }
344
    $x .= " <a href=\"".url_base()."show_user.php?userid=".$user->id."\">".$user->name."</a>";
345
    if (function_exists("project_user_links")) {
346
        $x .= project_user_links($user);
347
    }
348
    $x .= "</td></tr></table>\n";
349
    return $x;
350
}
351
352
// show user name, with links to profile if present.
353
// if $badge_height is > 0, show badges
354
// if $name_limit, limit name to N chars
355
//
356
function user_links($user, $badge_height=0, $name_limit=0) {
357
    BoincForumPrefs::lookup($user);
358
    if (is_banished($user)) {
359
        return "(banished: ID $user->id)";
360
    }
361
    $x = "";
362
    if ($user->has_profile) {
363
        $img_url = url_base()."img/head_20.png";
364
        $x .= ' <a href="'.url_base().'view_profile.php?userid='.$user->id.'"><img title="View the profile of '.$user->name.'" src="'.$img_url.'" alt="Profile"></a>';
365
    }
366
    $name = $user->name;
367
    if ($name_limit && strlen($name) > $name_limit) {
368
        $name = substr($name, 0, $name_limit)."...";
369
    }
370
    $x .= " <a href=\"".url_base()."show_user.php?userid=".$user->id."\">".$name."</a>";
371
    if (function_exists("project_user_links")){
372
        $x .= project_user_links($user);
373
    }
374
    if ($badge_height) {
375
        $x .= badges_string(true, $user, $badge_height);
376
    }
377
    return $name_limit?"<nobr>$x</nobr>":$x;
378
}
379
380
function show_community_private($user) {
381
    show_badges_row(true, $user);
382
    if (!DISABLE_PROFILES) {
383
        if ($user->has_profile) {
384
            $x = "<a href=\"view_profile.php?userid=$user->id\">".tra("View")."</a> &middot; <a href=\"delete_profile.php\">".tra("Delete")."</a>";
385
        } else {
386
            $x = "<a href=\"create_profile.php\">".tra("Create")."</a>";
387
        }
388
        row2(tra("Profile"), $x);
389
    }
390
    if (!DISABLE_FORUMS) {
391
        $tot = total_posts($user);
392
        if ($tot) {
393
            row2(tra("Message boards"), "<a href=\"".url_base()."forum_user_posts.php?userid=$user->id\">".tra("%1 posts", $tot)."</a>");
394
        }
395
    }
396
397
    row2(tra("Private messages"), pm_notification($user).pm_email_remind($user));
398
399
    $notifies = BoincNotify::enum("userid=$user->id");
400
    if (count($notifies)) {
401
        $x = "";
402
        foreach ($notifies as $notify) {
403
            $y = notify_description($notify);
404
            if ($y) {
405
                $x .= "&bull; $y<br>";
406
            } else {
407
                $notify->delete();
408
            }
409
        }
410
        $x .= "<a href=\"".notify_rss_url($user)."\"><img vspace=\"4\" border=\"0\" src=\"img/rss_icon.gif\" alt=\"RSS\" /></a>";
411
        row2(tra("Notifications"), $x);
412
    }
413
414
    if (!DISABLE_TEAMS) {
415
        if ($user->teamid && ($team = BoincTeam::lookup_id($user->teamid))) {
416
            $x = "<a href=\"team_display.php?teamid=$team->id\">$team->name</a>
417
                &middot; <a href=\"team_quit_form.php\">".tra("Quit team")."</a>";
418
            if (is_team_admin($user, $team)) {
419
                $x .= " &middot; <a href=\"team_manage.php?teamid=$user->teamid\">".tra("Administer")."</a>";
420
            }
421
422
            // if there's a foundership request, notify the founder
423
            //
424
            if ($user->id==$team->userid && $team->ping_user >0) {
425
                $x .= "<p class=\"text-danger\">".tra("(foundership change request pending)")."</p>";
426
            }
427
            row2(tra("Member of team"), $x);
428
        } else {
429
            row2(tra("Team"), tra("None")." &middot; <a href=\"team_search.php\">".tra("find a team")."</a>");
430
        }
431
432
        $teams_founded = BoincTeam::enum("userid=$user->id");
433
        foreach ($teams_founded as $team) {
434
            if ($team->id != $user->teamid) {
435
                $x = "<a href=\"team_display.php?teamid=$team->id\">$team->name</a>";
436
                $x .= " | <a href=\"team_manage.php?teamid=".$team->id."\">".tra("Administer")."</a>";
437
                if ($team->ping_user > 0) {
438
                    $x .= "<p class=\"text-danger\">".tra("(foundership change request pending)")."</span>";
439
                }
440
                row2(tra("Founder but not member of"), $x);
441
            }
442
        }
443
    }
444
445
    $friends = BoincFriend::enum("user_src=$user->id and reciprocated=1");
446
    $x = "<a href=\"user_search.php\">".tra("Find friends")."</a><br/>\n";
447
    $n = count($friends);
448
    if ($n) {
449
        foreach($friends as $friend) {
450
            $fuser = BoincUser::lookup_id($friend->user_dest);
451
            if (!$fuser) continue;
452
            $x .= friend_links($fuser);
453
        }
454
        row2(tra("Friends")." ($n)", $x);
455
    } else {
456
        row2(tra("Friends"), $x);
457
    }
458
}
459
460
// show summary of dynamic and static info (public)
461
//
462
function show_user_summary_public($user) {
463
    global $g_logged_in_user;
464
    if (!UNIQUE_USER_NAME) {
465
        row2(tra("User ID"), $user->id);
466
    }
467
    row2(tra("%1 member since", PROJECT), date_str($user->create_time));
468
    if (USER_COUNTRY) {
469
        row2(tra("Country"), $user->country);
470
    }
471
    if (USER_URL && $user->url) {
472
        // don't show URL if user has no recent credit (spam suppression)
473
        //
474
        if (!NO_COMPUTING || $user->expavg_credit > 1) {
475
            $u = normalize_user_url($user->url);
476
            row2(tra("URL"), sprintf('<a href="%s">%s</a>', $u, $u));
477
        }
478
    }
479
    if (!NO_COMPUTING) {
480
        show_credit($user);
481
482
        if ($user->show_hosts) {
483
            row2(tra("Computers"), "<a href=\"".url_base()."hosts_user.php?userid=$user->id\">".tra("View")."</a>");
484
        } else {
485
            row2(tra("Computers"), tra("hidden"));
486
        }
487
    }
488
    if (function_exists("project_user_summary_public")) {
489
        project_user_summary_public($user);
490
    }
491
}
492
493
// Returns a cacheable community links data object
494
// @param user The user to produce a community links object for
495
496
function get_community_links_object($user){
497
    $cache_object = new StdClass;
498
    $cache_object->post_count = total_posts($user);
499
    $cache_object->user = $user;
500
    $cache_object->team = BoincTeam::lookup_id($user->teamid);
501
    $cache_object->friends = array();
502
503
    $friends = BoincFriend::enum("user_src=$user->id and reciprocated=1");
504
    foreach($friends as $friend) {
505
        $fuser = BoincUser::lookup_id($friend->user_dest);
506
        if (!$fuser) continue;
507
        $cache_object->friends[] = $fuser;
508
    }
509
    return $cache_object;
510
}
511
512
function community_links($clo, $logged_in_user){
513
    $user = $clo->user;
514
    $team = $clo->team;
515
    $friends = $clo->friends;
516
    $tot = $clo->post_count;
517
518
    if (!DISABLE_TEAMS) {
519
        if ($user->teamid && $team) {
520
            row2(tra("Team"), "<a href=\"".url_base()."team_display.php?teamid=$team->id\">$team->name</a>");
521
        } else {
522
            row2(tra("Team"), tra("None"));
523
        }
524
    }
525
    if (!DISABLE_FORUMS) {
526
        if ($tot) {
527
            row2(tra("Message boards"), "<a href=\"".url_base()."forum_user_posts.php?userid=$user->id\">".tra("%1 posts", $tot)."</a>");
528
        }
529
    }
530
    if ($logged_in_user && $logged_in_user->id != $user->id) {
531
        row2(tra("Contact"), "<a href=\"pm.php?action=new&userid=".$user->id."\">".tra("Send private message")."</a>");
532
        $friend = BoincFriend::lookup($logged_in_user->id, $user->id);
533
        if ($friend && $friend->reciprocated) {
534
            row2(tra("This person is a friend"),
535
                "<a href=\"friend.php?action=cancel_confirm&userid=$user->id\">".tra("Cancel friendship")."</a>"
536
            );
537
        } else if ($friend) {
538
            row2(tra("Friends"),  "<a href=\"friend.php?action=add&userid=$user->id\">".tra("Request pending")."</a>");
539
        } else {
540
            row2(tra("Friends"),  "<a href=\"friend.php?action=add&userid=$user->id\">".tra("Add as friend")."</a>");
541
        }
542
    }
543
544
    if ($friends) {
545
        $x = "";
546
        foreach($friends as $friend) {
547
            $x .= friend_links($friend);
548
        }
549
        row2(tra("Friends")." (".sizeof($friends).")", $x);
550
    }
551
}
552
553
function show_profile_link($user) {
554
    if ($user->has_profile) {
555
        row2(tra("Profile"), "<a href=\"view_profile.php?userid=$user->id\">".tra("View")."</a>");
556
    }
557
}
558
559
function show_account_private($user) {
560
    grid(
561
        false,
562
        function() use ($user) {
563
            start_table();
564
            row1(tra("Account information"), 2, 'heading');
565
            show_user_info_private($user);
566
            show_preference_links();
567
            show_user_stats_private($user);
568
569
            if (function_exists('show_user_donations_private')) {
570
                show_user_donations_private($user);
571
            }
572
            end_table();
573
            if (!NO_COMPUTING) {
574
                show_other_projects($user, true);
575
            }
576
            if (function_exists("project_user_page_private")) {
577
                project_user_page_private($user);
578
            }
579
        },
580
        function() use ($user) {
581
            start_table();
582
            row1(tra("Community"));
583
            show_community_private($user);
584
            end_table();
585
        }
586
    );
587
}
588
589
590
$cvs_version_tracker[]="\$Id$";  //Generated automatically - do not edit
591
592
?>
593