Passed
Push — server_release/1/1.4 ( fdb43e...460c6a )
by
unknown
11:29
created

show_user_info_private()   D

Complexity

Conditions 15
Paths 216

Size

Total Lines 59
Code Lines 42

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 15
eloc 42
nc 216
nop 1
dl 0
loc 59
rs 4.8833
c 0
b 0
f 0

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
        row2(tra("Stats on your cell phone"), url_base()."userw.php?id=$user->id");
191
    }
192
}
193
194
function notify_description($notify) {
195
    switch ($notify->type) {
196
    case NOTIFY_FRIEND_REQ:
197
        return friend_notify_req_web_line($notify);
198
    case NOTIFY_FRIEND_ACCEPT:
199
        return friend_notify_accept_web_line($notify);
200
    case NOTIFY_PM:
201
        return pm_web_line($notify);
202
    case NOTIFY_SUBSCRIBED_POST:
203
        return subscribed_post_web_line($notify);
204
    }
205
    return null;
206
}
207
208
function weak_auth($user) {
209
    $x = md5($user->authenticator.$user->passwd_hash);
210
    return "{$user->id}_$x";
211
}
212
213
// originally user URLs were assumed to be http://,
214
// and this prefix wasn't stored.
215
// Now the prefix can be http:// or https://.
216
// This function takes a user URL in any form and converts
217
// it to a canonical form, with the protocol prefix.
218
//
219
function normalize_user_url($url) {
220
    $url = sanitize_user_url($url);
221
    if (!$url) return '';
222
    $x = strtolower($url);
223
    if (substr($x, 0, 7) == 'http://') {
224
        return 'http://'.substr($url, 7);
225
    }
226
    if (substr($x, 0, 8) == 'https://') {
227
        return 'https://'.substr($url, 8);
228
    }
229
    return 'http://'.$url;
230
}
231
232
// show static user info (private)
233
//
234
function show_user_info_private($user) {
235
    row2(tra("Name"), $user->name);
236
    if (LDAP_HOST && is_ldap_email($user->email_addr)) {
237
        row2("LDAP ID", ldap_email_to_uid($user->email_addr));
238
    } else {
239
        $email_text = $user->email_addr;
240
        if (defined("SHOW_NONVALIDATED_EMAIL_ADDR") && !$user->email_validated) {
241
            $email_text .= " (<a href=validate_email_addr.php>must be validated</a>)";
242
        }
243
        row2(tra("Email address"), $email_text);
244
    }
245
    if (USER_URL && $user->url) {
246
        $u = normalize_user_url($user->url);
247
        row2(
248
            tra("URL"),
249
            $u?sprintf('<a href="%s">%s</a>', $u, $u):tra('Invalid URL')
250
        );
251
    }
252
    if (USER_COUNTRY) {
253
        row2(tra("Country"), $user->country);
254
    }
255
    if (POSTAL_CODE) {
256
        row2(tra("Postal code"), $user->postal_code);
257
    }
258
    row2(tra("%1 member since", PROJECT), date_str($user->create_time));
259
    $url_tokens = url_tokens($user->authenticator);
260
    if (LDAP_HOST && is_ldap_email($user->email_addr)) {
261
        // LDAP accounts can't change email or password
262
        //
263
        row2(tra("Change"),
264
            "<a href=\"edit_user_info_form.php?$url_tokens\">Account info</a>"
265
        );
266
    } else {
267
        $delete_account_str = "";
268
        $config = get_config();
269
        if (parse_bool($config, "enable_delete_account")) {
270
            $delete_account_str = " &middot; <a href=\"delete_account_request.php\">".tra("delete account")."</a>";
271
        }
272
273
        row2(tra("Change"),
274
            "<a href=\"edit_email_form.php\">".tra("email address")."</a>
275
            &middot; <a href=\"".secure_url_base()."/edit_passwd_form.php\">".tra("password")."</a>
276
            &middot; <a href=\"edit_user_info_form.php?$url_tokens\">".tra("other account info")."</a>"
277
            .$delete_account_str
278
        );
279
    }
280
    row2(tra("User ID")."<br/><p class=\"small\">".tra("Used in community functions")."</p>", $user->id);
281
    if (!NO_COMPUTING) {
282
        row2(
283
            tra("Account keys"),
284
            "<a href=\"weak_auth.php\">".tra("View")."</a>"
285
        );
286
        
287
        require_once("../inc/account_ownership.inc");
288
        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...
289
          // If the server has keys configured show the account ownership form
290
          row2(
291
              tra("Account Ownership"),
292
              "<a href=\"account_ownership.php?$url_tokens\">Generate ownership proof</a>"
293
          );
294
        }
295
296
    }
297
}
298
299
function show_preference_links() {
300
    row1("<a name=\"prefs\"></a>".tra("Preferences"));
301
    if (!NO_GLOBAL_PREFS) {
302
        row2(
303
            tra("When and how BOINC uses your computer"),
304
            "<a href=\"prefs.php?subset=global\">".tra("Computing preferences")."</a>"
305
        );
306
    }
307
    row2(tra("Message boards and private messages"),
308
        "<a href=\"edit_forum_preferences_form.php\">".tra("Community preferences")."</a>"
309
    );
310
    if (!NO_COMPUTING) {
311
        row2(tra("Preferences for this project"),
312
            "<a href=\"prefs.php?subset=project\">".tra("%1 preferences", PROJECT)."</a>"
313
        );
314
    }
315
}
316
317
function friend_links($user) {
318
    if (is_banished($user)) {
319
        return "";
320
    }
321
    $x = "<table height=\"100\" width=\"150\" border=\"0\" cellpadding=\"4\"><tr><td class=\"friend\">";
322
    if ($user->has_profile) {
323
        $profile = BoincProfile::lookup_fields("has_picture", "userid=$user->id");
324
        if ($profile && $profile->has_picture) {
325
            $img_url = profile_thumb_url($user->id);
326
        } else {
327
            $img_url = url_base()."img/head_20.png";
328
        }
329
        $title = tra("View the profile of %1", $user->name);
330
        $alt = tra("Profile");
331
        $x .= ' <a href="'.url_base().'view_profile.php?userid='.$user->id.'"><img title="'.$title.'" src="'.$img_url.'" alt="'.$alt.'"></a><br>';
332
    }
333
    $x .= " <a href=\"".url_base()."show_user.php?userid=".$user->id."\">".$user->name."</a>";
334
    if (function_exists("project_user_links")) {
335
        $x .= project_user_links($user);
336
    }
337
    $x .= "</td></tr></table>\n";
338
    return $x;
339
}
340
341
// show user name, with links to profile if present.
342
// if $badge_height is > 0, show badges
343
// if $name_limit, limit name to N chars
344
//
345
function user_links($user, $badge_height=0, $name_limit=0) {
346
    BoincForumPrefs::lookup($user);
347
    if (is_banished($user)) {
348
        return "(banished: ID $user->id)";
349
    }
350
    $x = "";
351
    if ($user->has_profile) {
352
        $img_url = url_base()."img/head_20.png";
353
        $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>';
354
    }
355
    $name = $user->name;
356
    if ($name_limit && strlen($name) > $name_limit) {
357
        $name = substr($name, 0, $name_limit)."...";
358
    }
359
    $x .= " <a href=\"".url_base()."show_user.php?userid=".$user->id."\">".$name."</a>";
360
    if (function_exists("project_user_links")){
361
        $x .= project_user_links($user);
362
    }
363
    if ($badge_height) {
364
        $x .= badges_string(true, $user, $badge_height);
365
    }
366
    return $name_limit?"<nobr>$x</nobr>":$x;
367
}
368
369
function show_community_private($user) {
370
    show_badges_row(true, $user);
371
    if (!DISABLE_PROFILES) {
372
        if ($user->has_profile) {
373
            $x = "<a href=\"view_profile.php?userid=$user->id\">".tra("View")."</a> &middot; <a href=\"delete_profile.php\">".tra("Delete")."</a>";
374
        } else {
375
            $x = "<a href=\"create_profile.php\">".tra("Create")."</a>";
376
        }
377
        row2(tra("Profile"), $x);
378
    }
379
    if (!DISABLE_FORUMS) {
380
        $tot = total_posts($user);
381
        if ($tot) {
382
            row2(tra("Message boards"), "<a href=\"".url_base()."forum_user_posts.php?userid=$user->id\">".tra("%1 posts", $tot)."</a>");
383
        }
384
    }
385
386
    row2(tra("Private messages"), pm_notification($user).pm_email_remind($user));
387
388
    $notifies = BoincNotify::enum("userid=$user->id");
389
    if (count($notifies)) {
390
        $x = "";
391
        foreach ($notifies as $notify) {
392
            $y = notify_description($notify);
393
            if ($y) {
394
                $x .= "&bull; $y<br>";
395
            } else {
396
                $notify->delete();
397
            }
398
        }
399
        $x .= "<a href=\"".notify_rss_url($user)."\"><img vspace=\"4\" border=\"0\" src=\"img/rss_icon.gif\" alt=\"RSS\" /></a>";
400
        row2(tra("Notifications"), $x);
401
    }
402
403
    if (!DISABLE_TEAMS) {
404
        if ($user->teamid && ($team = BoincTeam::lookup_id($user->teamid))) {
405
            $x = "<a href=\"team_display.php?teamid=$team->id\">$team->name</a>
406
                &middot; <a href=\"team_quit_form.php\">".tra("Quit team")."</a>";
407
            if (is_team_admin($user, $team)) {
408
                $x .= " &middot; <a href=\"team_manage.php?teamid=$user->teamid\">".tra("Administer")."</a>";
409
            }
410
411
            // if there's a foundership request, notify the founder
412
            //
413
            if ($user->id==$team->userid && $team->ping_user >0) {
414
                $x .= "<p class=\"text-danger\">".tra("(foundership change request pending)")."</p>";
415
            }
416
            row2(tra("Member of team"), $x);
417
        } else {
418
            row2(tra("Team"), tra("None")." &middot; <a href=\"team_search.php\">".tra("find a team")."</a>");
419
        }
420
421
        $teams_founded = BoincTeam::enum("userid=$user->id");
422
        foreach ($teams_founded as $team) {
423
            if ($team->id != $user->teamid) {
424
                $x = "<a href=\"team_display.php?teamid=$team->id\">$team->name</a>";
425
                $x .= " | <a href=\"team_manage.php?teamid=".$team->id."\">".tra("Administer")."</a>";
426
                if ($team->ping_user > 0) {
427
                    $x .= "<p class=\"text-danger\">".tra("(foundership change request pending)")."</span>";
428
                }
429
                row2(tra("Founder but not member of"), $x);
430
            }
431
        }
432
    }
433
434
    $friends = BoincFriend::enum("user_src=$user->id and reciprocated=1");
435
    $x = "<a href=\"user_search.php\">".tra("Find friends")."</a><br/>\n";
436
    $n = count($friends);
437
    if ($n) {
438
        foreach($friends as $friend) {
439
            $fuser = BoincUser::lookup_id($friend->user_dest);
440
            if (!$fuser) continue;
441
            $x .= friend_links($fuser);
442
        }
443
        row2(tra("Friends")." ($n)", $x);
444
    } else {
445
        row2(tra("Friends"), $x);
446
    }
447
}
448
449
// show summary of dynamic and static info (public)
450
//
451
function show_user_summary_public($user) {
452
    global $g_logged_in_user;
453
    row2(tra("User ID"), $user->id);
454
    row2(tra("%1 member since", PROJECT), date_str($user->create_time));
455
    if (USER_COUNTRY) {
456
        row2(tra("Country"), $user->country);
457
    }
458
    if (USER_URL) {
459
        // don't show URL if user has no recent credit (spam suppression)
460
        //
461
        if ($user->url) {
462
            if (!NO_COMPUTING || $user->expavg_credit > 1) {
463
                $u = normalize_user_url($user->url);
464
                row2(tra("URL"), sprintf('<a href="%s">%s</a>', $u, $u));
465
            }
466
        }
467
    }
468
    if (!NO_COMPUTING) {
469
        show_credit($user);
470
471
        if ($user->show_hosts) {
472
            row2(tra("Computers"), "<a href=\"".url_base()."hosts_user.php?userid=$user->id\">".tra("View")."</a>");
473
        } else {
474
            row2(tra("Computers"), tra("hidden"));
475
        }
476
    }
477
    if (function_exists("project_user_summary_public")) {
478
        project_user_summary_public($user);
479
    }
480
}
481
482
// Returns a cacheable community links data object
483
// @param user The user to produce a community links object for
484
485
function get_community_links_object($user){
486
    $cache_object = new StdClass;
487
    $cache_object->post_count = total_posts($user);
488
    $cache_object->user = $user;
489
    $cache_object->team = BoincTeam::lookup_id($user->teamid);
490
    $cache_object->friends = array();
491
492
    $friends = BoincFriend::enum("user_src=$user->id and reciprocated=1");
493
    foreach($friends as $friend) {
494
        $fuser = BoincUser::lookup_id($friend->user_dest);
495
        if (!$fuser) continue;
496
        $cache_object->friends[] = $fuser;
497
    }
498
    return $cache_object;
499
}
500
501
function community_links($clo, $logged_in_user){
502
    $user = $clo->user;
503
    $team = $clo->team;
504
    $friends = $clo->friends;
505
    $tot = $clo->post_count;
506
507
    if (!DISABLE_TEAMS) {
508
        if ($user->teamid && $team) {
509
            row2(tra("Team"), "<a href=\"".url_base()."team_display.php?teamid=$team->id\">$team->name</a>");
510
        } else {
511
            row2(tra("Team"), tra("None"));
512
        }
513
    }
514
    if (!DISABLE_FORUMS) {
515
        if ($tot) {
516
            row2(tra("Message boards"), "<a href=\"".url_base()."forum_user_posts.php?userid=$user->id\">".tra("%1 posts", $tot)."</a>");
517
        }
518
    }
519
    if ($logged_in_user && $logged_in_user->id != $user->id) {
520
        row2(tra("Contact"), "<a href=\"pm.php?action=new&userid=".$user->id."\">".tra("Send private message")."</a>");
521
        $friend = BoincFriend::lookup($logged_in_user->id, $user->id);
522
        if ($friend && $friend->reciprocated) {
523
            row2(tra("This person is a friend"),
524
                "<a href=\"friend.php?action=cancel_confirm&userid=$user->id\">".tra("Cancel friendship")."</a>"
525
            );
526
        } else if ($friend) {
527
            row2(tra("Friends"),  "<a href=\"friend.php?action=add&userid=$user->id\">".tra("Request pending")."</a>");
528
        } else {
529
            row2(tra("Friends"),  "<a href=\"friend.php?action=add&userid=$user->id\">".tra("Add as friend")."</a>");
530
        }
531
    }
532
533
    if ($friends) {
534
        $x = "";
535
        foreach($friends as $friend) {
536
            $x .= friend_links($friend);
537
        }
538
        row2(tra("Friends")." (".sizeof($friends).")", $x);
539
    }
540
}
541
542
function show_profile_link($user) {
543
    if ($user->has_profile) {
544
        row2(tra("Profile"), "<a href=\"view_profile.php?userid=$user->id\">".tra("View")."</a>");
545
    }
546
}
547
548
function show_account_private($user) {
549
    grid(
550
        false,
551
        function() use ($user) {
552
            start_table();
553
            row1(tra("Account information"), 2, 'heading');
554
            show_user_info_private($user);
555
            show_preference_links();
556
            show_user_stats_private($user);
557
558
            if (function_exists('show_user_donations_private')) {
559
                show_user_donations_private($user);
560
            }
561
            end_table();
562
            if (!NO_COMPUTING) {
563
                show_other_projects($user, true);
564
            }
565
            if (function_exists("project_user_page_private")) {
566
                project_user_page_private($user);
567
            }
568
        },
569
        function() use ($user) {
570
            start_table();
571
            row1(tra("Community"));
572
            show_community_private($user);
573
            end_table();
574
        }
575
    );
576
}
577
578
579
$cvs_version_tracker[]="\$Id$";  //Generated automatically - do not edit
580
581
?>
582