Passed
Push — master ( 274801...346e7c )
by Kevin
11:49 queued 04:25
created

user_links()   B

Complexity

Conditions 8
Paths 33

Size

Total Lines 22
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 8
eloc 16
c 1
b 1
f 0
nc 33
nop 3
dl 0
loc 22
rs 8.4444
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
// if $name_limit, limit name to N chars
334
//
335
function user_links($user, $badge_height=0, $name_limit=0) {
336
    BoincForumPrefs::lookup($user);
337
    if (is_banished($user)) {
338
        return "(banished: ID $user->id)";
339
    }
340
    $x = "";
341
    if ($user->has_profile) {
342
        $img_url = url_base()."img/head_20.png";
343
        $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>';
344
    }
345
    $name = $user->name;
346
    if ($name_limit && strlen($name) > $name_limit) {
347
        $name = substr($name, 0, $name_limit)."...";
348
    }
349
    $x .= " <a href=\"".url_base()."show_user.php?userid=".$user->id."\">".$name."</a>";
350
    if (function_exists("project_user_links")){
351
        $x .= project_user_links($user);
352
    }
353
    if ($badge_height) {
354
        $x .= badges_string(true, $user, $badge_height);
355
    }
356
    return $name_limit?"<nobr>$x</nobr>":$x;
357
}
358
359
function show_community_private($user) {
360
    show_badges_row(true, $user);
361
    if (!DISABLE_PROFILES) {
362
        if ($user->has_profile) {
363
            $x = "<a href=\"view_profile.php?userid=$user->id\">".tra("View")."</a> &middot; <a href=\"delete_profile.php\">".tra("Delete")."</a>";
364
        } else {
365
            $x = "<a href=\"create_profile.php\">".tra("Create")."</a>";
366
        }
367
        row2(tra("Profile"), $x);
368
    }
369
    if (!DISABLE_FORUMS) {
370
        $tot = total_posts($user);
371
        if ($tot) {
372
            row2(tra("Message boards"), "<a href=\"".url_base()."forum_user_posts.php?userid=$user->id\">".tra("%1 posts", $tot)."</a>");
373
        }
374
    }
375
    
376
    row2(tra("Private messages"), pm_notification($user).pm_email_remind($user));
377
378
    $notifies = BoincNotify::enum("userid=$user->id");
379
    if (count($notifies)) {
380
        $x = "";
381
        foreach ($notifies as $notify) {
382
            $y = notify_description($notify);
383
            if ($y) {
384
                $x .= "&bull; $y<br>";
385
            } else {
386
                $notify->delete();
387
            }
388
        }
389
        $x .= "<a href=\"".notify_rss_url($user)."\"><img vspace=\"4\" border=\"0\" src=\"img/rss_icon.gif\" alt=\"RSS\" /></a>";
390
        row2(tra("Notifications"), $x);
391
    }
392
393
    if (!DISABLE_TEAMS) {
394
        if ($user->teamid && ($team = BoincTeam::lookup_id($user->teamid))) {
395
            $x = "<a href=\"team_display.php?teamid=$team->id\">$team->name</a>
396
                &middot; <a href=\"team_quit_form.php\">".tra("Quit team")."</a>";
397
            if (is_team_admin($user, $team)) {
398
                $x .= " &middot; <a href=\"team_manage.php?teamid=$user->teamid\">".tra("Administer")."</a>";
399
            }
400
401
            // if there's a foundership request, notify the founder
402
            //
403
            if ($user->id==$team->userid && $team->ping_user >0) {
404
                $x .= "<p class=\"text-danger\">".tra("(foundership change request pending)")."</p>";
405
            }
406
            row2(tra("Member of team"), $x);
407
        } else {
408
            row2(tra("Team"), tra("None")." &middot; <a href=\"team_search.php\">".tra("find a team")."</a>");
409
        }
410
        
411
        $teams_founded = BoincTeam::enum("userid=$user->id");
412
        foreach ($teams_founded as $team) {
413
            if ($team->id != $user->teamid) {
414
                $x = "<a href=\"team_display.php?teamid=$team->id\">$team->name</a>";
415
                $x .= " | <a href=\"team_manage.php?teamid=".$team->id."\">".tra("Administer")."</a>";
416
                if ($team->ping_user > 0) {
417
                    $x .= "<p class=\"text-danger\">".tra("(foundership change request pending)")."</span>";
418
                }
419
                row2(tra("Founder but not member of"), $x);
420
            }
421
        }
422
    }
423
424
    $friends = BoincFriend::enum("user_src=$user->id and reciprocated=1");
425
    $x = "<a href=\"user_search.php\">".tra("Find friends")."</a><br/>\n";
426
    $n = count($friends);
427
    if ($n) {
428
        foreach($friends as $friend) {
429
            $fuser = BoincUser::lookup_id($friend->user_dest);
430
            if (!$fuser) continue;
431
            $x .= friend_links($fuser);
432
        }
433
        row2(tra("Friends")." ($n)", $x);
434
    } else {
435
        row2(tra("Friends"), $x);
436
    }
437
}
438
439
// show summary of dynamic and static info (public)
440
//
441
function show_user_summary_public($user) {
442
    global $g_logged_in_user;
443
    row2(tra("User ID"), $user->id);
444
    row2(tra("%1 member since", PROJECT), date_str($user->create_time));
445
    if (USER_COUNTRY) {
446
        row2(tra("Country"), $user->country);
447
    }
448
    if (USER_URL) {
449
        // don't show URL if user has no recent credit (spam suppression)
450
        //
451
        if (strlen($user->url)) {
452
            if (!NO_COMPUTING || $user->expavg_credit > 1) {
453
                $u = normalize_user_url($user->url);
454
                row2(tra("URL"), sprintf('<a href="%s">%s</a>', $u, $u));
455
            }
456
        }
457
    }
458
    if (!NO_COMPUTING) {
459
        show_credit($user);
460
461
        if ($user->show_hosts) {
462
            row2(tra("Computers"), "<a href=\"".url_base()."hosts_user.php?userid=$user->id\">".tra("View")."</a>");
463
        } else {
464
            row2(tra("Computers"), tra("hidden"));
465
        }
466
    }
467
    if (function_exists("project_user_summary_public")) {
468
        project_user_summary_public($user);
469
    }
470
}
471
472
// Returns a cacheable community links data object
473
// @param user The user to produce a community links object for
474
475
function get_community_links_object($user){
476
    $cache_object = new StdClass;
477
    $cache_object->post_count = total_posts($user);
478
    $cache_object->user = $user;
479
    $cache_object->team = BoincTeam::lookup_id($user->teamid);
480
    $cache_object->friends = array();
481
482
    $friends = BoincFriend::enum("user_src=$user->id and reciprocated=1");
483
    foreach($friends as $friend) {
484
        $fuser = BoincUser::lookup_id($friend->user_dest);
485
        if (!$fuser) continue;
486
        $cache_object->friends[] = $fuser;
487
    }
488
    return $cache_object;
489
}
490
491
function community_links($clo, $logged_in_user){
492
    $user = $clo->user;
493
    $team = $clo->team;
494
    $friends = $clo->friends;
495
    $tot = $clo->post_count;
496
    
497
    if (!DISABLE_TEAMS) {
498
        if ($user->teamid && $team) {
499
            row2(tra("Team"), "<a href=\"".url_base()."team_display.php?teamid=$team->id\">$team->name</a>");
500
        } else {
501
            row2(tra("Team"), tra("None"));
502
        }
503
    }
504
    if (!DISABLE_FORUMS) {
505
        if ($tot) {
506
            row2(tra("Message boards"), "<a href=\"".url_base()."forum_user_posts.php?userid=$user->id\">".tra("%1 posts", $tot)."</a>");
507
        }
508
    }
509
    if ($logged_in_user && $logged_in_user->id != $user->id) {
510
        row2(tra("Contact"), "<a href=\"pm.php?action=new&userid=".$user->id."\">".tra("Send private message")."</a>");
511
        $friend = BoincFriend::lookup($logged_in_user->id, $user->id);
512
        if ($friend && $friend->reciprocated) {
513
            row2(tra("This person is a friend"), 
514
                "<a href=\"friend.php?action=cancel_confirm&userid=$user->id\">".tra("Cancel friendship")."</a>"
515
            );
516
        } else if ($friend) {
517
            row2(tra("Friends"),  "<a href=\"friend.php?action=add&userid=$user->id\">".tra("Request pending")."</a>");
518
        } else {
519
            row2(tra("Friends"),  "<a href=\"friend.php?action=add&userid=$user->id\">".tra("Add as friend")."</a>");
520
        }
521
    }
522
    
523
    if ($friends) {
524
        $x = "";
525
        foreach($friends as $friend) {
526
            $x .= friend_links($friend);
527
        }
528
        row2(tra("Friends")." (".sizeof($friends).")", $x);
529
    }
530
}
531
532
function show_profile_link($user) {
533
    if ($user->has_profile) {
534
        row2(tra("Profile"), "<a href=\"view_profile.php?userid=$user->id\">".tra("View")."</a>");
535
    }
536
}
537
538
function show_account_private($user) {
539
    grid(
540
        false,
541
        function() use ($user) {
542
            start_table();
543
            row1(tra("Account information"), 2, 'heading');
544
            show_user_info_private($user);
545
            show_preference_links();
546
            show_user_stats_private($user);
547
548
            if (function_exists('show_user_donations_private')) {
549
                show_user_donations_private($user);
550
            }
551
            end_table();
552
            if (!NO_COMPUTING) {
553
                show_other_projects($user, true);
554
            }
555
            if (function_exists("project_user_page_private")) {
556
                project_user_page_private($user);
557
            }
558
        },
559
        function() use ($user) {
560
            start_table();
561
            row1(tra("Community"));
562
            show_community_private($user);
563
            end_table();
564
        }
565
    );
566
}
567
568
569
$cvs_version_tracker[]="\$Id$";  //Generated automatically - do not edit
570
571
?>
572