show_user_info_private()   D
last analyzed

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
    }
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
    row2(tra("User ID")."<br/><p class=\"small\">".tra("Used in community functions")."</p>", $user->id);
290
    if (!NO_COMPUTING) {
291
        row2(
292
            tra("Account keys"),
293
            "<a href=\"weak_auth.php\">".tra("View")."</a>"
294
        );
295
296
        require_once("../inc/account_ownership.inc");
297
        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...
298
          // If the server has keys configured show the account ownership form
299
          row2(
300
              tra("Account Ownership"),
301
              "<a href=\"account_ownership.php?$url_tokens\">Generate ownership proof</a>"
302
          );
303
        }
304
305
    }
306
}
307
308
function show_preference_links() {
309
    row1("<a name=\"prefs\"></a>".tra("Preferences"));
310
    if (!NO_GLOBAL_PREFS) {
311
        row2(
312
            tra("When and how BOINC uses your computer"),
313
            "<a href=\"prefs.php?subset=global\">".tra("Computing preferences")."</a>"
314
        );
315
    }
316
    row2(tra("Message boards and private messages"),
317
        "<a href=\"edit_forum_preferences_form.php\">".tra("Community preferences")."</a>"
318
    );
319
    if (!NO_COMPUTING) {
320
        row2(tra("Preferences for this project"),
321
            "<a href=\"prefs.php?subset=project\">".tra("%1 preferences", PROJECT)."</a>"
322
        );
323
    }
324
}
325
326
function friend_links($user) {
327
    if (is_banished($user)) {
328
        return "";
329
    }
330
    $x = "<table height=\"100\" width=\"150\" border=\"0\" cellpadding=\"4\"><tr><td class=\"friend\">";
331
    if ($user->has_profile) {
332
        $profile = BoincProfile::lookup_fields("has_picture", "userid=$user->id");
333
        if ($profile && $profile->has_picture) {
334
            $img_url = profile_thumb_url($user->id);
335
        } else {
336
            $img_url = url_base()."img/head_20.png";
337
        }
338
        $title = tra("View the profile of %1", $user->name);
339
        $alt = tra("Profile");
340
        $x .= ' <a href="'.url_base().'view_profile.php?userid='.$user->id.'"><img title="'.$title.'" src="'.$img_url.'" alt="'.$alt.'"></a><br>';
341
    }
342
    $x .= " <a href=\"".url_base()."show_user.php?userid=".$user->id."\">".$user->name."</a>";
343
    if (function_exists("project_user_links")) {
344
        $x .= project_user_links($user);
345
    }
346
    $x .= "</td></tr></table>\n";
347
    return $x;
348
}
349
350
// show user name, with links to profile if present.
351
// if $badge_height is > 0, show badges
352
// if $name_limit, limit name to N chars
353
//
354
function user_links($user, $badge_height=0, $name_limit=0) {
355
    BoincForumPrefs::lookup($user);
356
    if (is_banished($user)) {
357
        return "(banished: ID $user->id)";
358
    }
359
    $x = "";
360
    if ($user->has_profile) {
361
        $img_url = url_base()."img/head_20.png";
362
        $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>';
363
    }
364
    $name = $user->name;
365
    if ($name_limit && strlen($name) > $name_limit) {
366
        $name = substr($name, 0, $name_limit)."...";
367
    }
368
    $x .= " <a href=\"".url_base()."show_user.php?userid=".$user->id."\">".$name."</a>";
369
    if (function_exists("project_user_links")){
370
        $x .= project_user_links($user);
371
    }
372
    if ($badge_height) {
373
        $x .= badges_string(true, $user, $badge_height);
374
    }
375
    return $name_limit?"<nobr>$x</nobr>":$x;
376
}
377
378
function show_community_private($user) {
379
    show_badges_row(true, $user);
380
    if (!DISABLE_PROFILES) {
381
        if ($user->has_profile) {
382
            $x = "<a href=\"view_profile.php?userid=$user->id\">".tra("View")."</a> &middot; <a href=\"delete_profile.php\">".tra("Delete")."</a>";
383
        } else {
384
            $x = "<a href=\"create_profile.php\">".tra("Create")."</a>";
385
        }
386
        row2(tra("Profile"), $x);
387
    }
388
    if (!DISABLE_FORUMS) {
389
        $tot = total_posts($user);
390
        if ($tot) {
391
            row2(tra("Message boards"), "<a href=\"".url_base()."forum_user_posts.php?userid=$user->id\">".tra("%1 posts", $tot)."</a>");
392
        }
393
    }
394
395
    row2(tra("Private messages"), pm_notification($user).pm_email_remind($user));
396
397
    $notifies = BoincNotify::enum("userid=$user->id");
398
    if (count($notifies)) {
399
        $x = "";
400
        foreach ($notifies as $notify) {
401
            $y = notify_description($notify);
402
            if ($y) {
403
                $x .= "&bull; $y<br>";
404
            } else {
405
                $notify->delete();
406
            }
407
        }
408
        $x .= "<a href=\"".notify_rss_url($user)."\"><img vspace=\"4\" border=\"0\" src=\"img/rss_icon.gif\" alt=\"RSS\" /></a>";
409
        row2(tra("Notifications"), $x);
410
    }
411
412
    if (!DISABLE_TEAMS) {
413
        if ($user->teamid && ($team = BoincTeam::lookup_id($user->teamid))) {
414
            $x = "<a href=\"team_display.php?teamid=$team->id\">$team->name</a>
415
                &middot; <a href=\"team_quit_form.php\">".tra("Quit team")."</a>";
416
            if (is_team_admin($user, $team)) {
417
                $x .= " &middot; <a href=\"team_manage.php?teamid=$user->teamid\">".tra("Administer")."</a>";
418
            }
419
420
            // if there's a foundership request, notify the founder
421
            //
422
            if ($user->id==$team->userid && $team->ping_user >0) {
423
                $x .= "<p class=\"text-danger\">".tra("(foundership change request pending)")."</p>";
424
            }
425
            row2(tra("Member of team"), $x);
426
        } else {
427
            row2(tra("Team"), tra("None")." &middot; <a href=\"team_search.php\">".tra("find a team")."</a>");
428
        }
429
430
        $teams_founded = BoincTeam::enum("userid=$user->id");
431
        foreach ($teams_founded as $team) {
432
            if ($team->id != $user->teamid) {
433
                $x = "<a href=\"team_display.php?teamid=$team->id\">$team->name</a>";
434
                $x .= " | <a href=\"team_manage.php?teamid=".$team->id."\">".tra("Administer")."</a>";
435
                if ($team->ping_user > 0) {
436
                    $x .= "<p class=\"text-danger\">".tra("(foundership change request pending)")."</span>";
437
                }
438
                row2(tra("Founder but not member of"), $x);
439
            }
440
        }
441
    }
442
443
    $friends = BoincFriend::enum("user_src=$user->id and reciprocated=1");
444
    $x = "<a href=\"user_search.php\">".tra("Find friends")."</a><br/>\n";
445
    $n = count($friends);
446
    if ($n) {
447
        foreach($friends as $friend) {
448
            $fuser = BoincUser::lookup_id($friend->user_dest);
449
            if (!$fuser) continue;
450
            $x .= friend_links($fuser);
451
        }
452
        row2(tra("Friends")." ($n)", $x);
453
    } else {
454
        row2(tra("Friends"), $x);
455
    }
456
}
457
458
// show summary of dynamic and static info (public)
459
//
460
function show_user_summary_public($user) {
461
    global $g_logged_in_user;
462
    row2(tra("User ID"), $user->id);
463
    row2(tra("%1 member since", PROJECT), date_str($user->create_time));
464
    if (USER_COUNTRY) {
465
        row2(tra("Country"), $user->country);
466
    }
467
    if (USER_URL && $user->url) {
468
        // don't show URL if user has no recent credit (spam suppression)
469
        //
470
        if (!NO_COMPUTING || $user->expavg_credit > 1) {
471
            $u = normalize_user_url($user->url);
472
            row2(tra("URL"), sprintf('<a href="%s">%s</a>', $u, $u));
473
        }
474
    }
475
    if (!NO_COMPUTING) {
476
        show_credit($user);
477
478
        if ($user->show_hosts) {
479
            row2(tra("Computers"), "<a href=\"".url_base()."hosts_user.php?userid=$user->id\">".tra("View")."</a>");
480
        } else {
481
            row2(tra("Computers"), tra("hidden"));
482
        }
483
    }
484
    if (function_exists("project_user_summary_public")) {
485
        project_user_summary_public($user);
486
    }
487
}
488
489
// Returns a cacheable community links data object
490
// @param user The user to produce a community links object for
491
492
function get_community_links_object($user){
493
    $cache_object = new StdClass;
494
    $cache_object->post_count = total_posts($user);
495
    $cache_object->user = $user;
496
    $cache_object->team = BoincTeam::lookup_id($user->teamid);
497
    $cache_object->friends = array();
498
499
    $friends = BoincFriend::enum("user_src=$user->id and reciprocated=1");
500
    foreach($friends as $friend) {
501
        $fuser = BoincUser::lookup_id($friend->user_dest);
502
        if (!$fuser) continue;
503
        $cache_object->friends[] = $fuser;
504
    }
505
    return $cache_object;
506
}
507
508
function community_links($clo, $logged_in_user){
509
    $user = $clo->user;
510
    $team = $clo->team;
511
    $friends = $clo->friends;
512
    $tot = $clo->post_count;
513
514
    if (!DISABLE_TEAMS) {
515
        if ($user->teamid && $team) {
516
            row2(tra("Team"), "<a href=\"".url_base()."team_display.php?teamid=$team->id\">$team->name</a>");
517
        } else {
518
            row2(tra("Team"), tra("None"));
519
        }
520
    }
521
    if (!DISABLE_FORUMS) {
522
        if ($tot) {
523
            row2(tra("Message boards"), "<a href=\"".url_base()."forum_user_posts.php?userid=$user->id\">".tra("%1 posts", $tot)."</a>");
524
        }
525
    }
526
    if ($logged_in_user && $logged_in_user->id != $user->id) {
527
        row2(tra("Contact"), "<a href=\"pm.php?action=new&userid=".$user->id."\">".tra("Send private message")."</a>");
528
        $friend = BoincFriend::lookup($logged_in_user->id, $user->id);
529
        if ($friend && $friend->reciprocated) {
530
            row2(tra("This person is a friend"),
531
                "<a href=\"friend.php?action=cancel_confirm&userid=$user->id\">".tra("Cancel friendship")."</a>"
532
            );
533
        } else if ($friend) {
534
            row2(tra("Friends"),  "<a href=\"friend.php?action=add&userid=$user->id\">".tra("Request pending")."</a>");
535
        } else {
536
            row2(tra("Friends"),  "<a href=\"friend.php?action=add&userid=$user->id\">".tra("Add as friend")."</a>");
537
        }
538
    }
539
540
    if ($friends) {
541
        $x = "";
542
        foreach($friends as $friend) {
543
            $x .= friend_links($friend);
544
        }
545
        row2(tra("Friends")." (".sizeof($friends).")", $x);
546
    }
547
}
548
549
function show_profile_link($user) {
550
    if ($user->has_profile) {
551
        row2(tra("Profile"), "<a href=\"view_profile.php?userid=$user->id\">".tra("View")."</a>");
552
    }
553
}
554
555
function show_account_private($user) {
556
    grid(
557
        false,
558
        function() use ($user) {
559
            start_table();
560
            row1(tra("Account information"), 2, 'heading');
561
            show_user_info_private($user);
562
            show_preference_links();
563
            show_user_stats_private($user);
564
565
            if (function_exists('show_user_donations_private')) {
566
                show_user_donations_private($user);
567
            }
568
            end_table();
569
            if (!NO_COMPUTING) {
570
                show_other_projects($user, true);
571
            }
572
            if (function_exists("project_user_page_private")) {
573
                project_user_page_private($user);
574
            }
575
        },
576
        function() use ($user) {
577
            start_table();
578
            row1(tra("Community"));
579
            show_community_private($user);
580
            end_table();
581
        }
582
    );
583
}
584
585
586
$cvs_version_tracker[]="\$Id$";  //Generated automatically - do not edit
587
588
?>
589