Passed
Push — master ( 7e05e0...ebb1f7 )
by Kevin
16:51
created

show_user_info_private()   C

Complexity

Conditions 13
Paths 216

Size

Total Lines 50
Code Lines 36

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 13
eloc 36
nc 216
nop 1
dl 0
loc 50
rs 5.5833
c 0
b 0
f 0

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